31 const std::vector<std::pair<double, double>>& xy_points,
32 std::vector<double>* headings, std::vector<double>* accumulated_s,
33 std::vector<double>* kappas, std::vector<double>* dkappas) {
34 CHECK_NOTNULL(headings);
35 CHECK_NOTNULL(kappas);
36 CHECK_NOTNULL(dkappas);
41 if (xy_points.size() < 2) {
44 std::vector<double> dxs;
45 std::vector<double> dys;
46 std::vector<double> y_over_s_first_derivatives;
47 std::vector<double> x_over_s_first_derivatives;
48 std::vector<double> y_over_s_second_derivatives;
49 std::vector<double> x_over_s_second_derivatives;
53 std::size_t points_size = xy_points.size();
54 for (std::size_t i = 0; i < points_size; ++i) {
58 x_delta = (xy_points[i + 1].first - xy_points[i].first);
59 y_delta = (xy_points[i + 1].second - xy_points[i].second);
60 }
else if (i == points_size - 1) {
61 x_delta = (xy_points[i].first - xy_points[i - 1].first);
62 y_delta = (xy_points[i].second - xy_points[i - 1].second);
64 x_delta = 0.5 * (xy_points[i + 1].first - xy_points[i - 1].first);
65 y_delta = 0.5 * (xy_points[i + 1].second - xy_points[i - 1].second);
67 dxs.push_back(x_delta);
68 dys.push_back(y_delta);
72 for (std::size_t i = 0; i < points_size; ++i) {
73 headings->push_back(std::atan2(dys[i], dxs[i]));
77 double distance = 0.0;
78 accumulated_s->push_back(distance);
79 double fx = xy_points[0].first;
80 double fy = xy_points[0].second;
83 for (std::size_t i = 1; i < points_size; ++i) {
84 nx = xy_points[i].first;
85 ny = xy_points[i].second;
86 double end_segment_s =
87 std::sqrt((fx - nx) * (fx - nx) + (fy - ny) * (fy - ny));
88 accumulated_s->push_back(end_segment_s + distance);
89 distance += end_segment_s;
96 for (std::size_t i = 0; i < points_size; ++i) {
100 xds = (xy_points[i + 1].first - xy_points[i].first) /
101 (accumulated_s->at(i + 1) - accumulated_s->at(i));
102 yds = (xy_points[i + 1].second - xy_points[i].second) /
103 (accumulated_s->at(i + 1) - accumulated_s->at(i));
104 }
else if (i == points_size - 1) {
105 xds = (xy_points[i].first - xy_points[i - 1].first) /
106 (accumulated_s->at(i) - accumulated_s->at(i - 1));
107 yds = (xy_points[i].second - xy_points[i - 1].second) /
108 (accumulated_s->at(i) - accumulated_s->at(i - 1));
110 xds = (xy_points[i + 1].first - xy_points[i - 1].first) /
111 (accumulated_s->at(i + 1) - accumulated_s->at(i - 1));
112 yds = (xy_points[i + 1].second - xy_points[i - 1].second) /
113 (accumulated_s->at(i + 1) - accumulated_s->at(i - 1));
115 x_over_s_first_derivatives.push_back(xds);
116 y_over_s_first_derivatives.push_back(yds);
121 for (std::size_t i = 0; i < points_size; ++i) {
126 (x_over_s_first_derivatives[i + 1] - x_over_s_first_derivatives[i]) /
127 (accumulated_s->at(i + 1) - accumulated_s->at(i));
129 (y_over_s_first_derivatives[i + 1] - y_over_s_first_derivatives[i]) /
130 (accumulated_s->at(i + 1) - accumulated_s->at(i));
131 }
else if (i == points_size - 1) {
133 (x_over_s_first_derivatives[i] - x_over_s_first_derivatives[i - 1]) /
134 (accumulated_s->at(i) - accumulated_s->at(i - 1));
136 (y_over_s_first_derivatives[i] - y_over_s_first_derivatives[i - 1]) /
137 (accumulated_s->at(i) - accumulated_s->at(i - 1));
139 xdds = (x_over_s_first_derivatives[i + 1] -
140 x_over_s_first_derivatives[i - 1]) /
141 (accumulated_s->at(i + 1) - accumulated_s->at(i - 1));
142 ydds = (y_over_s_first_derivatives[i + 1] -
143 y_over_s_first_derivatives[i - 1]) /
144 (accumulated_s->at(i + 1) - accumulated_s->at(i - 1));
146 x_over_s_second_derivatives.push_back(xdds);
147 y_over_s_second_derivatives.push_back(ydds);
150 for (std::size_t i = 0; i < points_size; ++i) {
151 double xds = x_over_s_first_derivatives[i];
152 double yds = y_over_s_first_derivatives[i];
153 double xdds = x_over_s_second_derivatives[i];
154 double ydds = y_over_s_second_derivatives[i];
156 (xds * ydds - yds * xdds) /
157 (std::sqrt(xds * xds + yds * yds) * (xds * xds + yds * yds) + 1e-6);
158 kappas->push_back(kappa);
162 for (std::size_t i = 0; i < points_size; ++i) {
165 dkappa = (kappas->at(i + 1) - kappas->at(i)) /
166 (accumulated_s->at(i + 1) - accumulated_s->at(i));
167 }
else if (i == points_size - 1) {
168 dkappa = (kappas->at(i) - kappas->at(i - 1)) /
169 (accumulated_s->at(i) - accumulated_s->at(i - 1));
171 dkappa = (kappas->at(i + 1) - kappas->at(i - 1)) /
172 (accumulated_s->at(i + 1) - accumulated_s->at(i - 1));
174 dkappas->push_back(dkappa);