|
| 1 | +// Copyright 2025 TIER IV, Inc. |
| 2 | +// |
| 3 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +// you may not use this file except in compliance with the License. |
| 5 | +// You may obtain a copy of the License at |
| 6 | +// |
| 7 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +// |
| 9 | +// Unless required by applicable law or agreed to in writing, software |
| 10 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +// See the License for the specific language governing permissions and |
| 13 | +// limitations under the License. |
| 14 | + |
| 15 | +#include "autoware/trajectory/point.hpp" |
| 16 | +#include "autoware/trajectory/utils/shift.hpp" |
| 17 | + |
| 18 | +#include <matplotlibcpp17/pyplot.h> |
| 19 | + |
| 20 | +#include <iostream> |
| 21 | +#include <vector> |
| 22 | + |
| 23 | +geometry_msgs::msg::Point point(double x, double y) |
| 24 | +{ |
| 25 | + geometry_msgs::msg::Point p; |
| 26 | + p.x = x; |
| 27 | + p.y = y; |
| 28 | + return p; |
| 29 | +} |
| 30 | + |
| 31 | +int main() |
| 32 | +{ |
| 33 | + pybind11::scoped_interpreter guard{}; |
| 34 | + auto plt = matplotlibcpp17::pyplot::import(); |
| 35 | + |
| 36 | + geometry_msgs::msg::Point p; |
| 37 | + (void)(p); |
| 38 | + |
| 39 | + std::vector<geometry_msgs::msg::Point> points = { |
| 40 | + point(0.49, 0.59), point(0.61, 1.22), point(0.86, 1.93), point(1.20, 2.56), point(1.51, 3.17), |
| 41 | + point(1.85, 3.76), point(2.14, 4.26), point(2.60, 4.56), point(3.07, 4.55), point(3.61, 4.30), |
| 42 | + point(3.95, 4.01), point(4.29, 3.68), point(4.90, 3.25), point(5.54, 3.10), point(6.24, 3.18), |
| 43 | + point(6.88, 3.54), point(7.51, 4.25), point(7.85, 4.93), point(8.03, 5.73), point(8.16, 6.52), |
| 44 | + point(8.31, 7.28), point(8.45, 7.93), point(8.68, 8.45), point(8.96, 8.96), point(9.32, 9.36)}; |
| 45 | + |
| 46 | + auto trajectory = |
| 47 | + autoware::trajectory::Trajectory<geometry_msgs::msg::Point>::Builder{}.build(points); |
| 48 | + |
| 49 | + if (!trajectory) { |
| 50 | + return 1; |
| 51 | + } |
| 52 | + |
| 53 | + std::cout << "length: " << trajectory->length() << std::endl; |
| 54 | + |
| 55 | + { |
| 56 | + std::vector<double> x; |
| 57 | + std::vector<double> y; |
| 58 | + for (double s = 0.0; s < trajectory->length(); s += 0.01) { |
| 59 | + auto p = trajectory->compute(s); |
| 60 | + x.push_back(p.x); |
| 61 | + y.push_back(p.y); |
| 62 | + } |
| 63 | + plt.plot(Args(x, y), Kwargs("label"_a = "original")); |
| 64 | + |
| 65 | + x.clear(); |
| 66 | + y.clear(); |
| 67 | + |
| 68 | + autoware::trajectory::ShiftInterval shift_interval; |
| 69 | + shift_interval.end = -1.0; |
| 70 | + shift_interval.lateral_offset = 0.5; |
| 71 | + |
| 72 | + auto shifted_trajectory = autoware::trajectory::shift(*trajectory, shift_interval); |
| 73 | + |
| 74 | + for (double s = 0.0; s < shifted_trajectory.length(); s += 0.01) { |
| 75 | + auto p = shifted_trajectory.compute(s); |
| 76 | + x.push_back(p.x); |
| 77 | + y.push_back(p.y); |
| 78 | + } |
| 79 | + |
| 80 | + plt.axis(Args("equal")); |
| 81 | + plt.plot(Args(x, y), Kwargs("label"_a = "shifted")); |
| 82 | + plt.legend(); |
| 83 | + plt.show(); |
| 84 | + } |
| 85 | + |
| 86 | + { |
| 87 | + std::vector<double> x; |
| 88 | + std::vector<double> y; |
| 89 | + for (double s = 0.0; s < trajectory->length(); s += 0.01) { |
| 90 | + auto p = trajectory->compute(s); |
| 91 | + x.push_back(p.x); |
| 92 | + y.push_back(p.y); |
| 93 | + } |
| 94 | + plt.plot(Args(x, y), Kwargs("label"_a = "original")); |
| 95 | + |
| 96 | + x.clear(); |
| 97 | + y.clear(); |
| 98 | + |
| 99 | + autoware::trajectory::ShiftInterval shift_interval; |
| 100 | + shift_interval.start = trajectory->length() / 4.0; |
| 101 | + shift_interval.end = trajectory->length() * 3.0 / 4.0; |
| 102 | + shift_interval.lateral_offset = 0.5; |
| 103 | + auto shifted_trajectory = autoware::trajectory::shift(*trajectory, shift_interval); |
| 104 | + |
| 105 | + for (double s = 0.0; s < shifted_trajectory.length(); s += 0.01) { |
| 106 | + auto p = shifted_trajectory.compute(s); |
| 107 | + x.push_back(p.x); |
| 108 | + y.push_back(p.y); |
| 109 | + } |
| 110 | + |
| 111 | + plt.axis(Args("equal")); |
| 112 | + plt.plot(Args(x, y), Kwargs("label"_a = "shifted")); |
| 113 | + plt.legend(); |
| 114 | + plt.show(); |
| 115 | + } |
| 116 | + |
| 117 | + { |
| 118 | + std::vector<double> x; |
| 119 | + std::vector<double> y; |
| 120 | + for (double s = 0.0; s < trajectory->length(); s += 0.01) { |
| 121 | + auto p = trajectory->compute(s); |
| 122 | + x.push_back(p.x); |
| 123 | + y.push_back(p.y); |
| 124 | + } |
| 125 | + plt.plot(Args(x, y), Kwargs("label"_a = "original")); |
| 126 | + |
| 127 | + x.clear(); |
| 128 | + y.clear(); |
| 129 | + |
| 130 | + autoware::trajectory::ShiftInterval shift_interval; |
| 131 | + shift_interval.start = trajectory->length() * 3.0 / 4.0; |
| 132 | + shift_interval.end = trajectory->length() / 4.0; |
| 133 | + shift_interval.lateral_offset = 0.5; |
| 134 | + auto shifted_trajectory = autoware::trajectory::shift(*trajectory, shift_interval); |
| 135 | + |
| 136 | + for (double s = 0.0; s < shifted_trajectory.length(); s += 0.01) { |
| 137 | + auto p = shifted_trajectory.compute(s); |
| 138 | + x.push_back(p.x); |
| 139 | + y.push_back(p.y); |
| 140 | + } |
| 141 | + |
| 142 | + plt.axis(Args("equal")); |
| 143 | + plt.plot(Args(x, y), Kwargs("label"_a = "shifted")); |
| 144 | + plt.legend(); |
| 145 | + plt.show(); |
| 146 | + } |
| 147 | + |
| 148 | + { |
| 149 | + std::vector<double> x; |
| 150 | + std::vector<double> y; |
| 151 | + for (double s = 0.0; s < trajectory->length(); s += 0.01) { |
| 152 | + auto p = trajectory->compute(s); |
| 153 | + x.push_back(p.x); |
| 154 | + y.push_back(p.y); |
| 155 | + } |
| 156 | + plt.plot(Args(x, y), Kwargs("label"_a = "original")); |
| 157 | + |
| 158 | + x.clear(); |
| 159 | + y.clear(); |
| 160 | + |
| 161 | + autoware::trajectory::ShiftInterval shift_interval1; |
| 162 | + shift_interval1.start = trajectory->length() / 4.0; |
| 163 | + shift_interval1.end = trajectory->length() * 2.0 / 4.0; |
| 164 | + shift_interval1.lateral_offset = 0.5; |
| 165 | + |
| 166 | + autoware::trajectory::ShiftInterval shift_interval2; |
| 167 | + shift_interval2.start = trajectory->length() * 2.0 / 4.0; |
| 168 | + shift_interval2.end = trajectory->length() * 3.0 / 4.0; |
| 169 | + shift_interval2.lateral_offset = -0.5; |
| 170 | + |
| 171 | + auto shifted_trajectory = |
| 172 | + autoware::trajectory::shift(*trajectory, {shift_interval1, shift_interval2}); |
| 173 | + |
| 174 | + for (double s = 0.0; s < shifted_trajectory.length(); s += 0.01) { |
| 175 | + auto p = shifted_trajectory.compute(s); |
| 176 | + x.push_back(p.x); |
| 177 | + y.push_back(p.y); |
| 178 | + } |
| 179 | + |
| 180 | + plt.axis(Args("equal")); |
| 181 | + plt.plot(Args(x, y), Kwargs("label"_a = "shifted")); |
| 182 | + plt.legend(); |
| 183 | + plt.show(); |
| 184 | + } |
| 185 | + |
| 186 | + return 0; |
| 187 | +} |
0 commit comments