32 #include <Eigen/Dense> 38 const std::vector<cv::Vec2d>& points,
40 std::vector<std::vector<cv::Vec2d> >& splines)
47 size_t num_points = points.size();
50 splines.resize(num_points - 1);
53 std::vector<double>
s = std::vector<double>(num_points);
56 std::vector<double> ds = std::vector<double>(num_points);
59 for (
size_t i = 1; i < num_points; i++)
61 double dx = points[i][0] - points[i - 1][0];
62 double dy = points[i][1] - points[i - 1][1];
64 ds[i - 1] = std::sqrt(std::pow(dx, 2) + std::pow(dy, 2));
65 s[i] = s[i - 1] + ds[i - 1];
69 double totalDistance = s[num_points - 1];
70 for (
size_t i = 0; i < num_points; i++)
72 s[i] /= totalDistance;
75 Eigen::MatrixX2d u(num_points, 2);
76 Eigen::MatrixXd A(num_points, num_points);
77 Eigen::MatrixX2d z(num_points, 2);
83 for (
size_t i = 1; i < num_points; i++)
89 for (
size_t i = 1; i < num_points - 1; i++)
91 double hi = s[i + 1] - s[i];
92 double himl = s[i] - s[i - 1];
94 u(i, 0) = 6.0 * ((points[i + 1][0] - points[i][0]) / hi - (points[i][0]
95 - points[i - 1][0]) / himl);
96 u(i, 1) = 6.0 * ((points[i + 1][1] - points[i][1]) / hi - (points[i][1]
97 - points[i - 1][1]) / himl);
99 for (
size_t j = 0; j < i - 1; j++)
104 A(i, i) = 2 * (himl + hi);
106 for (
size_t j = i + 2; j < num_points; j++)
113 u(num_points - 1, 0) = 0;
114 u(num_points - 1, 1) = 0;
115 for (
size_t j = 0; j < num_points - 1; j++)
117 A(num_points - 1, j) = 0;
119 A(num_points - 1, num_points - 1) = 1;
122 z = A.colPivHouseholderQr().solve(u);
124 for (
size_t i = 0; i < num_points - 1; i++)
126 std::vector<cv::Vec2d>& spline = splines[i];
128 spline.push_back(points[i]);
130 double hi = s[i + 1] - s[i];
131 double act_dist = ds[i];
132 int32_t num_to_add =
static_cast<int32_t
> (act_dist / delta);
133 double normalizedDelta = hi / (num_to_add + 1);
136 for (int32_t j = 1; j <= num_to_add; j++)
139 double si = s[i] + j * normalizedDelta;
140 double xx = (z(i + 1, 0) * pow(si - s[i], 3) + z(i, 0) * pow(s[i + 1]
141 - si, 3)) / (6.0 * hi) + (points[i + 1][0] / hi - hi * z(i + 1, 0)
142 / 6.0) * (si - s[i]) + (points[i][0] / hi - hi * z(i, 0) / 6.0)
144 double yy = (z(i + 1, 1) * pow(si - s[i], 3) + z(i, 1) * pow(s[i + 1]
145 - si, 3)) / (6.0 * hi) + (points[i + 1][1] / hi - hi * z(i + 1, 1)
146 / 6.0) * (si - s[i]) + (points[i][1] / hi - hi * z(i, 1) / 6.0)
150 spline.push_back(cv::Vec2d(xx, yy));
153 spline.push_back(points[i + 1]);
160 const std::vector<tf::Vector3>& points,
162 std::vector<std::vector<tf::Vector3> >& splines)
164 std::vector<cv::Vec2d> cv_points(points.size());
165 for (
size_t i = 0; i < points.size(); i++)
167 cv_points[i] = cv::Vec2d(points[i].x(), points[i].y());
170 std::vector<std::vector<cv::Vec2d> > cv_splines;
173 splines.resize(cv_splines.size());
174 for (
size_t i = 0; i < cv_splines.size(); i++)
176 splines[i].resize(cv_splines[i].size());
177 for (
size_t j = 0; j < cv_splines[i].size(); j++)
179 splines[i][j] = tf::Vector3(cv_splines[i][j][0], cv_splines[i][j][1], 0);
bool CubicSplineInterpolation(const std::vector< cv::Vec2d > &points, double delta, std::vector< std::vector< cv::Vec2d > > &splines)