|
| 1 | +// Copyright 2024 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/motion_utils/trajectory_container/interpolator/linear.hpp" |
| 16 | + |
| 17 | +#include <autoware/motion_utils/trajectory_container/interpolator.hpp> |
| 18 | + |
| 19 | +#include <matplotlibcpp17/pyplot.h> |
| 20 | + |
| 21 | +#include <random> |
| 22 | +#include <vector> |
| 23 | + |
| 24 | +int main() |
| 25 | +{ |
| 26 | + pybind11::scoped_interpreter guard{}; |
| 27 | + auto plt = matplotlibcpp17::pyplot::import(); |
| 28 | + |
| 29 | + // create random values |
| 30 | + std::vector<double> axis = {0.0, 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0}; |
| 31 | + std::vector<double> values; |
| 32 | + std::random_device seed_gen; |
| 33 | + std::mt19937 engine(seed_gen()); |
| 34 | + std::uniform_real_distribution<> dist(-1.0, 1.0); |
| 35 | + for (size_t i = 0; i < axis.size(); ++i) { |
| 36 | + values.push_back(dist(engine)); |
| 37 | + } |
| 38 | + // Scatter Data |
| 39 | + plt.scatter(Args(axis, values)); |
| 40 | + |
| 41 | + using autoware::motion_utils::trajectory_container::interpolator::Interpolator; |
| 42 | + using autoware::motion_utils::trajectory_container::interpolator::InterpolatorCreator; |
| 43 | + // Linear Interpolator |
| 44 | + { |
| 45 | + using autoware::motion_utils::trajectory_container::interpolator::Linear; |
| 46 | + auto interpolator = *InterpolatorCreator<Linear>().set_axis(axis).set_values(values).create(); |
| 47 | + std::vector<double> x; |
| 48 | + std::vector<double> y; |
| 49 | + for (double i = axis.front(); i < axis.back(); i += 0.01) { |
| 50 | + x.push_back(i); |
| 51 | + y.push_back(interpolator.compute(i)); |
| 52 | + } |
| 53 | + plt.plot(Args(x, y), Kwargs("label"_a = "Linear")); |
| 54 | + } |
| 55 | + |
| 56 | + // AkimaSpline Interpolator |
| 57 | + { |
| 58 | + using autoware::motion_utils::trajectory_container::interpolator::AkimaSpline; |
| 59 | + auto interpolator = |
| 60 | + *InterpolatorCreator<AkimaSpline>().set_axis(axis).set_values(values).create(); |
| 61 | + std::vector<double> x; |
| 62 | + std::vector<double> y; |
| 63 | + for (double i = axis.front(); i < axis.back(); i += 0.01) { |
| 64 | + x.push_back(i); |
| 65 | + y.push_back(interpolator.compute(i)); |
| 66 | + } |
| 67 | + plt.plot(Args(x, y), Kwargs("label"_a = "AkimaSpline")); |
| 68 | + } |
| 69 | + |
| 70 | + // CubicSpline Interpolator |
| 71 | + { |
| 72 | + using autoware::motion_utils::trajectory_container::interpolator::CubicSpline; |
| 73 | + auto interpolator = |
| 74 | + *InterpolatorCreator<CubicSpline>().set_axis(axis).set_values(values).create(); |
| 75 | + std::vector<double> x; |
| 76 | + std::vector<double> y; |
| 77 | + for (double i = axis.front(); i < axis.back(); i += 0.01) { |
| 78 | + x.push_back(i); |
| 79 | + y.push_back(interpolator.compute(i)); |
| 80 | + } |
| 81 | + plt.plot(Args(x, y), Kwargs("label"_a = "CubicSpline")); |
| 82 | + } |
| 83 | + |
| 84 | + // NearestNeighbor Interpolator |
| 85 | + { |
| 86 | + using autoware::motion_utils::trajectory_container::interpolator::NearestNeighbor; |
| 87 | + auto interpolator = |
| 88 | + *InterpolatorCreator<NearestNeighbor<double>>().set_axis(axis).set_values(values).create(); |
| 89 | + std::vector<double> x; |
| 90 | + std::vector<double> y; |
| 91 | + for (double i = axis.front(); i < axis.back(); i += 0.01) { |
| 92 | + x.push_back(i); |
| 93 | + y.push_back(interpolator.compute(i)); |
| 94 | + } |
| 95 | + plt.plot(Args(x, y), Kwargs("label"_a = "NearestNeighbor")); |
| 96 | + } |
| 97 | + |
| 98 | + // Stairstep Interpolator |
| 99 | + { |
| 100 | + using autoware::motion_utils::trajectory_container::interpolator::Stairstep; |
| 101 | + auto interpolator = |
| 102 | + *InterpolatorCreator<Stairstep<double>>().set_axis(axis).set_values(values).create(); |
| 103 | + std::vector<double> x; |
| 104 | + std::vector<double> y; |
| 105 | + for (double i = axis.front(); i < axis.back(); i += 0.01) { |
| 106 | + x.push_back(i); |
| 107 | + y.push_back(interpolator.compute(i)); |
| 108 | + } |
| 109 | + plt.plot(Args(x, y), Kwargs("label"_a = "Stairstep")); |
| 110 | + } |
| 111 | + |
| 112 | + plt.legend(); |
| 113 | + plt.show(); |
| 114 | + return 0; |
| 115 | +} |
0 commit comments