-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathAnimation2.cpp
48 lines (38 loc) · 1.63 KB
/
Animation2.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
#include <iostream>
#include <Eigen/Dense>
#include <vector>
#include <numeric>
#include "matplotlibcpp.h"
#include "CubicSpline1D.h"
using namespace std;
namespace plt = matplotlibcpp;
void plot_waypoints(const vector<double>& x_vals, const vector<double>& y_vals, const string& style = "bo-") {
plt::plot(x_vals, y_vals, style);
plt::xlabel("X");
plt::ylabel("Y");
plt::title("Waypoints");
plt::grid(true);
}
int main() {
// Given waypoints
std::vector<double> x = {1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 6.0,6.0,6.0,6.0,6.0,6.0,6.0, 5.0,4.0,3.0,2.0,1.0,0.0,-1.0,-2.0,-3.0,-4.0,-5.0,-6.0,-7.0,-7.0,-7.0,-7.0,-7.0,-7.0,-7.0,-7.0, -6.0,-5.0,-4.0,-3.0,-2.0};
std::vector<double> y = {0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 1.0,2.0,3.0,4.0,5.0,6.0,7.0, 7.0,7.0,7.0,7.0,7.0,7.0,7.0,7.0,7.0,7.0,7.0,7.0,7.0, 6.0,5.0,4.0,3.0,2.0,1.0,0.0,0.0,0.0,0.0,0.0,0.0};
// Generating a list of indices for the waypoints:
std::vector<double> t_values(x.size());
std::iota(t_values.begin(), t_values.end(), 0); // fills t_values with increasing values starting from 0
// Create the splines using these indices as the independent variable
CubicSpline1D spline_x(t_values, x);
CubicSpline1D spline_y(t_values, y);
// Interpolate waypoints using the t_values range
std::vector<double> x_new, y_new;
for (double t = 0; t < t_values.back(); t += 0.01) {
x_new.push_back(spline_x.calc_der0(t));
y_new.push_back(spline_y.calc_der0(t));
}
// Plot original waypoints
plot_waypoints(x, y, "bo-");
// Plot interpolated waypoints
plot_waypoints(x_new, y_new, "r-");
plt::show();
return 0;
}