1 #include "algorithm/geom_algorithm.hpp"
2 #include "util/debug.hpp"
10 std::vector<double> lengths(N - 1);
11 double x0 = trajectory.
get_x(0);
12 double y0 = trajectory.
get_y(0);
13 for (
int i = 1; i < N; ++i) {
14 double x1 = trajectory.
get_x(i);
15 double y1 = trajectory.
get_y(i);
18 lengths[i - 1] = sqrt(dx * dx + dy * dy);
29 for (
int i = 0; i < Npoints; ++i) {
40 for (
int i = Npoints - 1; i >= 0; --i) {
49 std::vector<FMM::CORE::LineString> segments;
51 segments.push_back(line);
58 double length_visited = 0;
61 double cx = line.
get_x(0);
62 double cy = line.
get_y(0);
65 double nx = line.
get_x(i);
66 double ny = line.
get_y(i);
67 double temp = std::sqrt((nx - cx) * (nx - cx) + (ny - cy) * (ny - cy));
68 if (length_visited <= delta
69 && length_visited + temp > delta) {
71 double ratio = (delta - length_visited) / temp;
73 cx = ratio * (nx - cx) + cx;
74 cy = ratio * (ny - cy) + cy;
76 segments.push_back(interpolated_line);
77 interpolated_line.
clear();
84 length_visited += temp;
88 segments.push_back(interpolated_line);
99 int distance_count = distances.size();
100 double length_visited = 0;
103 while (i < Npoints - 1 && j < distance_count) {
104 double x1 = line.
get_x(i);
105 double y1 = line.
get_y(i);
106 double x2 = line.
get_x(i + 1);
107 double y2 = line.
get_y(i + 1);
108 double temp = std::sqrt((x2 - x1) * (x2 - x1) + (y2 - y1) * (y2 - y1));
109 while (j < distance_count && length_visited <= distances[j]
110 && length_visited + temp > distances[j]) {
111 double ratio = (distances[j] - length_visited) / temp;
113 interpolated_line.
add_point(ratio * (x2 - x1) + x1,
114 ratio * (y2 - y1) + y1);
117 length_visited += temp;
120 if (length_visited < distances.back()) {
122 line.
get_y(Npoints - 1));
124 return interpolated_line;
130 int k = (int) ceil(length / distance);
131 std::vector<double> distances;
132 for (
int i = 0; i < k + 1; ++i) {
133 distances.push_back(distance * i);
141 std::vector<double> distances;
142 for (
int i = 0; i < k; ++i) {
143 distances.push_back((
double) rand() / RAND_MAX * length);
145 std::sort(distances.begin(), distances.end());
153 double *x2,
double *y2) {
160 for (
int i = 0; i < Npoints; ++i) {
161 x = linestring.
get_x(i);
162 y = linestring.
get_y(i);
163 if (x < *x1) *x1 = x;
164 if (y < *y1) *y1 = y;
165 if (x > *x2) *x2 = x;
166 if (y > *y2) *y2 = y;
173 if (N < 2)
return std::vector<double>();
174 std::vector<double> result(N - 1);
175 double x0 = geom.
get_x(0);
176 double y0 = geom.
get_y(0);
177 for (
int i = 1; i < N; ++i) {
178 double x1 = geom.
get_x(i);
179 double y1 = geom.
get_y(i);
182 result[i - 1] = sqrt(dx * dx + dy * dy);
187 for (
int i = N - 2; i >= 0; --i) {
188 result[i] = temp + result[i];
195 double x,
double y,
double x1,
double y1,
double x2,
double y2,
196 double *dist,
double *offset) {
197 double L2 = (x2 - x1) * (x2 - x1) + (y2 - y1) * (y2 - y1);
199 *dist = std::sqrt((x - x1) * (x - x1) + (y - y1) * (y - y1));
203 double x1_x = x - x1;
204 double y1_y = y - y1;
205 double x1_x2 = x2 - x1;
206 double y1_y2 = y2 - y1;
207 double ratio = (x1_x * x1_x2 + y1_y * y1_y2) / L2;
208 ratio = (ratio > 1) ? 1 : ratio;
209 ratio = (ratio < 0) ? 0 : ratio;
210 double prj_x = x1 + ratio * (x1_x2);
211 double prj_y = y1 + ratio * (y1_y2);
212 *offset = std::sqrt((prj_x - x1) * (prj_x - x1) +
213 (prj_y - y1) * (prj_y - y1));
214 *dist = std::sqrt((prj_x - x) * (prj_x - x) + (prj_y - y) * (prj_y - y));
218 double x,
double y,
double x1,
double y1,
219 double x2,
double y2,
double *dist,
220 double *offset,
double *closest_x,
222 double L2 = (x2 - x1) * (x2 - x1) + (y2 - y1) * (y2 - y1);
224 *dist = std::sqrt((x - x1) * (x - x1) + (y - y1) * (y - y1));
230 double x1_x = x - x1;
231 double y1_y = y - y1;
232 double x1_x2 = x2 - x1;
233 double y1_y2 = y2 - y1;
234 double ratio = (x1_x * x1_x2 + y1_y * y1_y2) / L2;
235 ratio = (ratio > 1) ? 1 : ratio;
236 ratio = (ratio < 0) ? 0 : ratio;
237 double prj_x = x1 + ratio * (x1_x2);
238 double prj_y = y1 + ratio * (y1_y2);
239 *offset = std::sqrt((prj_x - x1) * (prj_x - x1) +
240 (prj_y - y1) * (prj_y - y1));
241 *dist = std::sqrt((prj_x - x) * (prj_x - x) + (prj_y - y) * (prj_y - y));
248 double *result_dist,
double *result_offset) {
250 double min_dist = DBL_MAX;
251 double final_offset = DBL_MAX;
252 double length_parsed = 0;
256 while (i < Npoints - 1) {
257 double x1 = linestring.
get_x(i);
258 double y1 = linestring.
get_y(i);
259 double x2 = linestring.
get_x(i + 1);
260 double y2 = linestring.
get_y(i + 1);
261 double temp_min_dist;
262 double temp_min_offset;
264 &temp_min_dist, &temp_min_offset);
265 if (temp_min_dist < min_dist) {
266 min_dist = temp_min_dist;
267 final_offset = length_parsed + temp_min_offset;
269 length_parsed += std::sqrt((x2 - x1) * (x2 - x1) + (y2 - y1) * (y2 - y1));
272 *result_dist = min_dist;
273 *result_offset = final_offset;
278 double *result_dist,
double *result_offset,
279 double *proj_x,
double *proj_y) {
281 double min_dist = DBL_MAX;
282 double temp_x = 0, temp_y = 0;
283 double final_offset = DBL_MAX;
284 double length_parsed = 0;
288 while (i < Npoints - 1) {
289 double x1 = linestring.
get_x(i);
290 double y1 = linestring.
get_y(i);
291 double x2 = linestring.
get_x(i + 1);
292 double y2 = linestring.
get_y(i + 1);
293 double temp_min_dist;
294 double temp_min_offset;
296 &temp_min_dist, &temp_min_offset,
298 if (temp_min_dist < min_dist) {
299 min_dist = temp_min_dist;
300 final_offset = length_parsed + temp_min_offset;
304 length_parsed += std::sqrt((x2 - x1) * (x2 - x1) + (y2 - y1) * (y2 - y1));
307 *result_dist = min_dist;
308 *result_offset = final_offset;
313 double *x,
double *y) {
316 *x = linestring.
get_x(0);
317 *y = linestring.
get_y(0);
320 double L_processed = 0;
326 while (i < Npoints - 1) {
327 double x1 = linestring.
get_x(i);
328 double y1 = linestring.
get_y(i);
329 double x2 = linestring.
get_x(i + 1);
330 double y2 = linestring.
get_y(i + 1);
331 double deltaL = std::sqrt((x2 - x1) * (x2 - x1) + (y2 - y1) * (y2 - y1));
332 double ratio = (offset - L_processed) / deltaL;
333 if (offset >= L_processed && offset <= L_processed + deltaL) {
334 px = x1 + ratio * (x2 - x1);
335 py = y1 + ratio * (y2 - y1);
340 L_processed += deltaL;
346 *x = linestring.
get_x(Npoints - 1);
347 *y = linestring.
get_y(Npoints - 1);
354 SPDLOG_TRACE(
"Offset1 {} Offset2 {}", offset1, offset2);
358 double x1 = linestring.
get_x(0);
359 double y1 = linestring.
get_y(0);
360 double x2 = linestring.
get_x(1);
361 double y2 = linestring.
get_y(1);
362 double L = std::sqrt((x2 - x1) * (x2 - x1) + (y2 - y1) * (y2 - y1));
363 double ratio1 = offset1 / L;
364 double new_x1 = x1 + ratio1 * (x2 - x1);
365 double new_y1 = y1 + ratio1 * (y2 - y1);
366 double ratio2 = offset2 / L;
367 double new_x2 = x1 + ratio2 * (x2 - x1);
368 double new_y2 = y1 + ratio2 * (y2 - y1);
376 while (i < Npoints - 1) {
377 double x1 = linestring.
get_x(i);
378 double y1 = linestring.
get_y(i);
379 double x2 = linestring.
get_x(i + 1);
380 double y2 = linestring.
get_y(i + 1);
381 double deltaL = std::sqrt((x2 - x1) * (x2 - x1) + (y2 - y1) * (y2 - y1));
384 SPDLOG_TRACE(
" L1 {} L2 {} ", l1, l2);
385 if (l1 >= offset1 && l1 <= offset2) {
387 SPDLOG_TRACE(
" add p1 {} {}", x1, y1);
391 if (offset1 > l1 && offset1 < l2) {
392 double ratio1 = (offset1 - l1) / deltaL;
393 double px = x1 + ratio1 * (x2 - x1);
394 double py = y1 + ratio1 * (y2 - y1);
396 SPDLOG_TRACE(
" add p {} {} between p1 p2", px, py);
399 if (offset2 > l1 && offset2 < l2) {
400 double ratio2 = (offset2 - l1) / deltaL;
401 double px = x1 + ratio2 * (x2 - x1);
402 double py = y1 + ratio2 * (y2 - y1);
404 SPDLOG_TRACE(
" add p {} {} between p1 p2", px, py);
408 if (i == Npoints - 2 && offset2 >= l2) {
410 SPDLOG_TRACE(
" add p2 {} {} for last point", x2, y2);
423 double offset1, offset2;