plcopen 0.22.0
载入中...
搜索中...
未找到
geometry.h
浏览该文件的文档.
1#pragma once
2
3#include <algorithm>
4#include <cmath>
5#include <cstddef>
6
7#include "rt/error.h"
8#include "rt/static_vector.h"
9
10namespace plcopen::core::geom
11{
12
13struct Vec3
14{
15 double x = 0.0;
16 double y = 0.0;
17 double z = 0.0;
18};
19
20inline Vec3 operator+(Vec3 lhs, Vec3 rhs)
21{
22 return {lhs.x + rhs.x, lhs.y + rhs.y, lhs.z + rhs.z};
23}
24
25inline Vec3 operator-(Vec3 lhs, Vec3 rhs)
26{
27 return {lhs.x - rhs.x, lhs.y - rhs.y, lhs.z - rhs.z};
28}
29
30inline Vec3 operator*(Vec3 value, double scale)
31{
32 return {value.x * scale, value.y * scale, value.z * scale};
33}
34
35inline double dot(Vec3 lhs, Vec3 rhs)
36{
37 return lhs.x * rhs.x + lhs.y * rhs.y + lhs.z * rhs.z;
38}
39
40inline Vec3 cross(Vec3 lhs, Vec3 rhs)
41{
42 return {lhs.y * rhs.z - lhs.z * rhs.y,
43 lhs.z * rhs.x - lhs.x * rhs.z,
44 lhs.x * rhs.y - lhs.y * rhs.x};
45}
46
47inline double norm(Vec3 value)
48{
49 return std::sqrt(dot(value, value));
50}
51
52inline Vec3 normalize(Vec3 value)
53{
54 const double length = norm(value);
55 if(length <= 0.0) {
56 return {};
57 }
58 return value * (1.0 / length);
59}
60
61inline double clamp_arclength(double value, double length)
62{
63 if(value <= 0.0) {
64 return 0.0;
65 }
66 if(value >= length) {
67 return length;
68 }
69 return value;
70}
71
73{
76 double length = 0.0;
77};
78
80{
81 const double length = norm(finish - start);
82 if(!std::isfinite(length) || length <= 1e-12) {
84 }
85 return rt::Result<LineSegment>::success({start, finish, length});
86}
87
88inline Vec3 sample(LineSegment line, double arclength)
89{
90 const double ratio = clamp_arclength(arclength, line.length) / line.length;
91 return line.start + (line.finish - line.start) * ratio;
92}
93
95{
96 return normalize(line.finish - line.start);
97}
98
100{
101 return (line.finish - line.start) * (1.0 / line.length);
102}
103
105{
106 return {0.0, 0.0, 0.0};
107}
108
110{
111 return {0.0, 0.0, 0.0};
112}
113
115{
120 Vec3 u{1.0, 0.0, 0.0};
121 Vec3 v{0.0, 1.0, 0.0};
122 double radius = 0.0;
123 double start_angle = 0.0;
124 double sweep = 0.0;
125 double length = 0.0;
126 bool spatial = false;
127 bool full_circle = false;
128 bool local_basis = false;
129};
130
132{
133 static constexpr std::size_t TableSize = 65;
138 double length = 0.0;
139 double cumulative[TableSize] = {};
140};
141
143{
144 static constexpr std::size_t TableSize = 65;
148 double length = 0.0;
149 double max_deviation = 0.0;
150 double cumulative[TableSize] = {};
151};
152
153// A4 corner blend: symmetric quintic Bezier with collinear control triples
154// (P0,P1,P2 on the incoming line, P3,P4,P5 on the outgoing line), which makes
155// the curve C2 against straight lines: tangent along the lines and zero
156// curvature at both junctions. Sampling runs through an embedded arc-length
157// table so the spatial speed stays continuous across the junctions.
159{
160 static constexpr std::size_t TableSize = 65;
167 double length = 0.0;
168 double max_deviation = 0.0;
169 double max_curvature = 0.0;
170 double cumulative[TableSize] = {};
171};
172
173inline double angle_of(Vec3 point, Vec3 center)
174{
175 return std::atan2(point.y - center.y, point.x - center.x);
176}
177
178inline double normalize_sweep(double sweep, double orientation)
179{
180 constexpr double Tau = 6.28318530717958647692;
181 if(orientation >= 0.0) {
182 while(sweep < 0.0) {
183 sweep += Tau;
184 }
185 } else {
186 while(sweep > 0.0) {
187 sweep -= Tau;
188 }
189 }
190 return sweep;
191}
192
193inline rt::Result<ArcSegment> make_arc(Vec3 start, Vec3 via, Vec3 finish)
194{
195 // Solve the circumcenter in centroid-relative coordinates: the |p|^2
196 // terms then cancel on the triangle's own scale instead of against the
197 // absolute origin, so the fit stays conditioned at machine offsets
198 // (PCS translation, tool offsets). Mathematically identical to the
199 // absolute-coordinate determinant form.
200 const double gx = (start.x + via.x + finish.x) / 3.0;
201 const double gy = (start.y + via.y + finish.y) / 3.0;
202 const double ax = start.x - gx;
203 const double ay = start.y - gy;
204 const double bx = via.x - gx;
205 const double by = via.y - gy;
206 const double cx = finish.x - gx;
207 const double cy = finish.y - gy;
208 const double d = 2.0 * (ax * (by - cy) + bx * (cy - ay) + cx * (ay - by));
209
210 if(!std::isfinite(d) || std::fabs(d) <= 1e-12) {
212 }
213
214 const double a2 = ax * ax + ay * ay;
215 const double b2 = bx * bx + by * by;
216 const double c2 = cx * cx + cy * cy;
217 const Vec3 center{
218 (a2 * (by - cy) + b2 * (cy - ay) + c2 * (ay - by)) / d + gx,
219 (a2 * (cx - bx) + b2 * (ax - cx) + c2 * (bx - ax)) / d + gy,
220 0.0,
221 };
222 const double radius = norm({start.x - center.x, start.y - center.y, 0.0});
223 if(!std::isfinite(radius) || radius <= 1e-12) {
225 }
226
227 const double orientation =
228 (via.x - start.x) * (finish.y - via.y) - (via.y - start.y) * (finish.x - via.x);
229 const double start_angle = angle_of(start, center);
230 const double raw_sweep = angle_of(finish, center) - start_angle;
231 const double sweep = normalize_sweep(raw_sweep, orientation);
232 if(std::fabs(sweep) <= 1e-12) {
234 }
235
236 ArcSegment arc{};
237 arc.start = start;
238 arc.via = via;
239 arc.finish = finish;
240 arc.center = center;
241 arc.radius = radius;
242 arc.start_angle = start_angle;
243 arc.sweep = sweep;
244 arc.length = std::fabs(sweep) * radius;
246}
247
248inline Vec3 sample(ArcSegment arc, double arclength)
249{
250 if(arc.local_basis) {
251 if(arclength <= 0.0) {
252 return arc.start;
253 }
254 if(arclength >= arc.length) {
255 return arc.finish;
256 }
257 const double ratio = arclength / arc.length;
258 const double phi = arc.sweep * ratio;
259 const double s_half = std::sin(0.5 * phi);
260 const Vec3 delta =
261 (arc.u * (-2.0 * s_half * s_half) + arc.v * std::sin(phi)) * arc.radius;
262 if(arc.spatial) {
263 return arc.start + delta;
264 }
265 return {
266 arc.start.x + delta.x,
267 arc.start.y + delta.y,
268 arc.start.z + (arc.finish.z - arc.start.z) * ratio,
269 };
270 }
271 if(arc.spatial) {
272 if(arclength <= 0.0) {
273 return arc.start;
274 }
275 if(arclength >= arc.length) {
276 return arc.finish;
277 }
278 const double ratio = arclength / arc.length;
279 const double angle = arc.start_angle + arc.sweep * ratio;
280 return arc.center + (arc.u * std::cos(angle) + arc.v * std::sin(angle)) * arc.radius;
281 }
282 if(arc.full_circle) {
283 if(arclength <= 0.0) {
284 return arc.start;
285 }
286 if(arclength >= arc.length) {
287 return {arc.start.x, arc.start.y, arc.finish.z};
288 }
289 }
290 const double ratio = clamp_arclength(arclength, arc.length) / arc.length;
291 const double angle = arc.start_angle + arc.sweep * ratio;
292 return {
293 arc.center.x + std::cos(angle) * arc.radius,
294 arc.center.y + std::sin(angle) * arc.radius,
295 arc.start.z + (arc.finish.z - arc.start.z) * ratio,
296 };
297}
298
299inline Vec3 tangent(ArcSegment arc, double arclength)
300{
301 const double ratio = clamp_arclength(arclength, arc.length) / arc.length;
302 const double angle = arc.local_basis ? (arc.sweep * ratio)
303 : (arc.start_angle + arc.sweep * ratio);
304 const double direction = arc.sweep >= 0.0 ? 1.0 : -1.0;
305 if(arc.spatial || arc.local_basis) {
306 return (arc.u * (-std::sin(angle)) + arc.v * std::cos(angle)) * direction;
307 }
308 return {-std::sin(angle) * direction, std::cos(angle) * direction, 0.0};
309}
310
311inline Vec3 path_derivative(ArcSegment arc, double arclength)
312{
313 const double ratio = clamp_arclength(arclength, arc.length) / arc.length;
314 const double angle = arc.local_basis ? (arc.sweep * ratio)
315 : (arc.start_angle + arc.sweep * ratio);
316 const double omega = arc.sweep / arc.length;
317 if(arc.spatial) {
318 return (arc.u * (-std::sin(angle)) + arc.v * std::cos(angle)) * (arc.radius * omega);
319 }
320 if(arc.local_basis) {
321 const Vec3 d =
322 (arc.u * (-std::sin(angle)) + arc.v * std::cos(angle)) * (arc.radius * omega);
323 return {d.x, d.y, (arc.finish.z - arc.start.z) / arc.length};
324 }
325 return {-arc.radius * std::sin(angle) * omega,
326 arc.radius * std::cos(angle) * omega,
327 (arc.finish.z - arc.start.z) / arc.length};
328}
329
330inline Vec3 path_second_derivative(ArcSegment arc, double arclength)
331{
332 const double ratio = clamp_arclength(arclength, arc.length) / arc.length;
333 const double angle = arc.local_basis ? (arc.sweep * ratio)
334 : (arc.start_angle + arc.sweep * ratio);
335 const double omega2 = (arc.sweep / arc.length) * (arc.sweep / arc.length);
336 if(arc.spatial || arc.local_basis) {
337 return (arc.u * (-std::cos(angle)) + arc.v * (-std::sin(angle))) * (arc.radius * omega2);
338 }
339 return {-arc.radius * std::cos(angle) * omega2,
340 -arc.radius * std::sin(angle) * omega2,
341 0.0};
342}
343
344inline Vec3 path_third_derivative(ArcSegment arc, double arclength)
345{
346 const double ratio = clamp_arclength(arclength, arc.length) / arc.length;
347 const double angle = arc.local_basis ? (arc.sweep * ratio)
348 : (arc.start_angle + arc.sweep * ratio);
349 const double omega = arc.sweep / arc.length;
350 const double omega3 = omega * omega * omega;
351 if(arc.spatial || arc.local_basis) {
352 return (arc.u * std::sin(angle) + arc.v * (-std::cos(angle))) * (arc.radius * omega3);
353 }
354 return {arc.radius * std::sin(angle) * omega3,
355 -arc.radius * std::cos(angle) * omega3,
356 0.0};
357}
358
360 Vec3 center,
361 Vec3 finish,
362 bool counter_clockwise)
363{
364 constexpr double Tau = 6.28318530717958647692;
365 if(!std::isfinite(start.x) || !std::isfinite(start.y) || !std::isfinite(start.z) ||
366 !std::isfinite(center.x) || !std::isfinite(center.y) || !std::isfinite(center.z) ||
367 !std::isfinite(finish.x) || !std::isfinite(finish.y) || !std::isfinite(finish.z)) {
369 }
370 const double r0 = norm({start.x - center.x, start.y - center.y, 0.0});
371 const double r1 = norm({finish.x - center.x, finish.y - center.y, 0.0});
372 const double rtol = 1e-9 * r0;
373 if(!std::isfinite(r0) || !std::isfinite(r1) || r0 <= 1e-12 ||
374 std::fabs(r1 - r0) > rtol) {
376 }
377 const double start_angle = angle_of(start, center);
378 const double chord_xy = norm({finish.x - start.x, finish.y - start.y, 0.0});
379 const bool is_full = chord_xy <= 1e-12 * r0;
380 double sweep = 0.0;
381 if(is_full) {
382 sweep = counter_clockwise ? Tau : -Tau;
383 } else {
384 double raw = angle_of(finish, center) - start_angle;
385 if(counter_clockwise) {
386 while(raw <= 0.0) {
387 raw += Tau;
388 }
389 while(raw > Tau) {
390 raw -= Tau;
391 }
392 } else {
393 while(raw >= 0.0) {
394 raw -= Tau;
395 }
396 while(raw < -Tau) {
397 raw += Tau;
398 }
399 }
400 sweep = raw;
401 }
402 const double length = std::fabs(sweep) * r0;
403 if(!std::isfinite(sweep) || std::fabs(sweep) <= 1e-12 || !std::isfinite(length) ||
404 length <= 0.0) {
406 }
407 ArcSegment arc{};
408 arc.start = start;
409 arc.via = center;
410 arc.finish = is_full ? Vec3{start.x, start.y, finish.z} : finish;
411 arc.center = {center.x, center.y, 0.0};
412 arc.radius = r0;
413 arc.start_angle = start_angle;
414 arc.sweep = sweep;
415 arc.length = length;
416 arc.spatial = false;
417 arc.full_circle = is_full;
419}
420
422 Vec3 finish,
423 double signed_radius,
424 bool counter_clockwise)
425{
426 constexpr double Tau = 6.28318530717958647692;
427 if(!std::isfinite(start.x) || !std::isfinite(start.y) || !std::isfinite(start.z) ||
428 !std::isfinite(finish.x) || !std::isfinite(finish.y) || !std::isfinite(finish.z) ||
429 !std::isfinite(signed_radius)) {
431 }
432 const double r = std::fabs(signed_radius);
433 const double dx = finish.x - start.x;
434 const double dy = finish.y - start.y;
435 const double d = std::sqrt(dx * dx + dy * dy);
436 if(r <= 1e-12 || !std::isfinite(d) || d <= 1e-12 || d > 2.0 * r * (1.0 + 1e-12)) {
438 }
439 const double half_d = 0.5 * d;
440 const double eff_r = half_d > r ? half_d : r;
441 double ratio = half_d / eff_r;
442 if(ratio > 1.0) {
443 ratio = 1.0;
444 }
445 const double cos_half_sq = 1.0 - ratio * ratio;
446 const double cos_half = cos_half_sq > 0.0 ? std::sqrt(cos_half_sq) : 0.0;
447 const double h = eff_r * cos_half;
448 const double ex = dx / d;
449 const double ey = dy / d;
450 const double mx = -ey;
451 const double my = ex;
452 const double side =
453 (counter_clockwise ? 1.0 : -1.0) * (signed_radius > 0.0 ? 1.0 : -1.0);
454 const Vec3 center{
455 0.5 * (start.x + finish.x) + side * h * mx,
456 0.5 * (start.y + finish.y) + side * h * my,
457 0.0,
458 };
459 const double minor_sweep = 2.0 * std::asin(ratio);
460 const double mag_sweep = signed_radius > 0.0 ? minor_sweep : (Tau - minor_sweep);
461 const double sweep = counter_clockwise ? mag_sweep : -mag_sweep;
462 const double length = mag_sweep * eff_r;
463 if(!std::isfinite(center.x) || !std::isfinite(center.y) ||
464 !std::isfinite(sweep) || std::fabs(sweep) <= 0.0 ||
465 !std::isfinite(length) || length <= 0.0) {
467 }
468 const double ux = -ratio * ex - side * cos_half * mx;
469 const double uy = -ratio * ey - side * cos_half * my;
470 ArcSegment arc{};
471 arc.start = start;
472 arc.via = center;
473 arc.finish = finish;
474 arc.center = center;
475 arc.u = {ux, uy, 0.0};
476 arc.v = {-uy, ux, 0.0};
477 arc.radius = eff_r;
478 arc.start_angle = 0.0;
479 arc.sweep = sweep;
480 arc.length = length;
481 arc.spatial = false;
482 arc.full_circle = false;
483 arc.local_basis = true;
485}
486
488 Vec3 via,
489 Vec3 finish,
490 Vec3 ref_normal,
491 bool counter_clockwise)
492{
493 constexpr double Tau = 6.28318530717958647692;
494 if(!std::isfinite(start.x) || !std::isfinite(start.y) || !std::isfinite(start.z) ||
495 !std::isfinite(via.x) || !std::isfinite(via.y) || !std::isfinite(via.z) ||
496 !std::isfinite(finish.x) || !std::isfinite(finish.y) || !std::isfinite(finish.z) ||
497 !std::isfinite(ref_normal.x) || !std::isfinite(ref_normal.y) ||
498 !std::isfinite(ref_normal.z)) {
500 }
501 const double n_ref_len = norm(ref_normal);
502 if(!std::isfinite(n_ref_len) || n_ref_len <= 1e-12) {
504 }
505 const Vec3 n_ref = ref_normal * (1.0 / n_ref_len);
506
507 const Vec3 a = via - start;
508 const Vec3 b = finish - start;
509 const Vec3 c = finish - via;
510 const double la = norm(a);
511 const double lb = norm(b);
512 const double lc = norm(c);
513 if(!std::isfinite(la) || !std::isfinite(lb) || !std::isfinite(lc) ||
514 la <= 1e-12 || lb <= 1e-12 || lc <= 1e-12) {
516 }
517 const Vec3 w = cross(a, b);
518 const double nw = norm(w);
519 if(!std::isfinite(nw) || nw <= 1e-12 * la * lb) {
521 }
522 const Vec3 n_tri = w * (1.0 / nw);
523 const double align = dot(n_tri, n_ref);
524 if(std::fabs(align) < 1e-6) {
526 }
527 const bool derived_ccw = align > 0.0;
528 if(derived_ccw != counter_clockwise) {
530 }
531
532 // Centroid-shifted 2D projection inside the arc plane preserves KB-097
533 // conditioning at large machine offsets (1e6).
534 const Vec3 g{(start.x + via.x + finish.x) / 3.0,
535 (start.y + via.y + finish.y) / 3.0,
536 (start.z + via.z + finish.z) / 3.0};
537 const Vec3 p0 = start - g;
538 const Vec3 p1 = via - g;
539 const Vec3 p2 = finish - g;
540 const Vec3 e1 = normalize(p1 - p0);
541 const Vec3 e2 = cross(n_tri, e1);
542 const double ax = dot(p0, e1);
543 const double ay = dot(p0, e2);
544 const double bx = dot(p1, e1);
545 const double by = dot(p1, e2);
546 const double cx = dot(p2, e1);
547 const double cy = dot(p2, e2);
548 const double det = 2.0 * (ax * (by - cy) + bx * (cy - ay) + cx * (ay - by));
549 if(!std::isfinite(det) || std::fabs(det) <= 1e-12 * la * lb) {
551 }
552 const double a2 = ax * ax + ay * ay;
553 const double b2 = bx * bx + by * by;
554 const double c2 = cx * cx + cy * cy;
555 const double ox = (a2 * (by - cy) + b2 * (cy - ay) + c2 * (ay - by)) / det;
556 const double oy = (a2 * (cx - bx) + b2 * (ax - cx) + c2 * (bx - ax)) / det;
557 const Vec3 center = g + e1 * ox + e2 * oy;
558 const double radius = norm(start - center);
559 if(!std::isfinite(radius) || radius <= 1e-12 || radius > lb * 1e6) {
561 }
562
563 // Orient plane basis (u, v) so n_basis aligns with +n_ref: CCW has
564 // sweep > 0 and CW has sweep < 0.
565 const Vec3 n_basis = counter_clockwise ? n_tri : n_tri * (-1.0);
566 const Vec3 u = normalize(start - center);
567 const Vec3 v = cross(n_basis, u);
568 const Vec3 rf = finish - center;
569 double sweep = std::atan2(dot(rf, v), dot(rf, u));
570 if(counter_clockwise) {
571 while(sweep <= 0.0) {
572 sweep += Tau;
573 }
574 while(sweep > Tau) {
575 sweep -= Tau;
576 }
577 } else {
578 while(sweep >= 0.0) {
579 sweep -= Tau;
580 }
581 while(sweep < -Tau) {
582 sweep += Tau;
583 }
584 }
585 if(!std::isfinite(sweep) || std::fabs(sweep) <= 1e-12) {
587 }
588
589 ArcSegment arc{};
590 arc.start = start;
591 arc.via = via;
592 arc.finish = finish;
593 arc.center = center;
594 arc.u = u;
595 arc.v = v;
596 arc.radius = radius;
597 arc.start_angle = 0.0;
598 arc.sweep = sweep;
599 arc.length = std::fabs(sweep) * radius;
600 arc.spatial = true;
602}
603
605 Vec3 center,
606 Vec3 finish,
607 Vec3 plane_normal,
608 bool counter_clockwise)
609{
610 constexpr double Tau = 6.28318530717958647692;
611 if(!std::isfinite(start.x) || !std::isfinite(start.y) || !std::isfinite(start.z) ||
612 !std::isfinite(center.x) || !std::isfinite(center.y) || !std::isfinite(center.z) ||
613 !std::isfinite(finish.x) || !std::isfinite(finish.y) || !std::isfinite(finish.z) ||
614 !std::isfinite(plane_normal.x) || !std::isfinite(plane_normal.y) ||
615 !std::isfinite(plane_normal.z)) {
617 }
618 const double n_len = norm(plane_normal);
619 if(!std::isfinite(n_len) || n_len <= 1e-12) {
621 }
622 const Vec3 n = plane_normal * (1.0 / n_len);
623 const Vec3 r0 = start - center;
624 const Vec3 r1 = finish - center;
625 const double rad0 = norm(r0);
626 const double rad1 = norm(r1);
627 const double rtol = 1e-9 * rad0;
628 if(!std::isfinite(rad0) || !std::isfinite(rad1) || rad0 <= 1e-12 ||
629 std::fabs(rad1 - rad0) > rtol || std::fabs(dot(r0, n)) > rtol ||
630 std::fabs(dot(r1, n)) > rtol) {
632 }
633
634 const Vec3 r0_in_plane = r0 - n * dot(r0, n);
635 const double r0_plane_len = norm(r0_in_plane);
636 if(!std::isfinite(r0_plane_len) || r0_plane_len <= 1e-12 ||
637 r0_plane_len < 0.5 * rad0) {
639 }
640 const Vec3 u = r0_in_plane * (1.0 / r0_plane_len);
641 const Vec3 v = cross(n, u);
642 if(!std::isfinite(v.x) || !std::isfinite(v.y) || !std::isfinite(v.z) ||
643 norm(v) < 0.5) {
645 }
646 const double chord = norm(finish - start);
647 const bool is_full = chord <= 1e-12 * rad0;
648 double sweep = 0.0;
649 if(is_full) {
650 sweep = counter_clockwise ? Tau : -Tau;
651 } else {
652 double phi = std::atan2(dot(r1, v), dot(r1, u));
653 if(counter_clockwise) {
654 while(phi <= 0.0) {
655 phi += Tau;
656 }
657 while(phi > Tau) {
658 phi -= Tau;
659 }
660 } else {
661 while(phi >= 0.0) {
662 phi -= Tau;
663 }
664 while(phi < -Tau) {
665 phi += Tau;
666 }
667 }
668 sweep = phi;
669 }
670 const double length = std::fabs(sweep) * rad0;
671 if(!std::isfinite(sweep) || std::fabs(sweep) <= 1e-12 || !std::isfinite(length) ||
672 length <= 0.0) {
674 }
675
676 ArcSegment arc{};
677 arc.start = start;
678 arc.via = center;
679 arc.finish = is_full ? start : finish;
680 arc.center = center;
681 arc.u = u;
682 arc.v = v;
683 arc.radius = rad0;
684 arc.start_angle = 0.0;
685 arc.sweep = sweep;
686 arc.length = length;
687 arc.spatial = true;
688 arc.full_circle = is_full;
690}
691
693 Vec3 finish,
694 double signed_radius,
695 Vec3 plane_normal,
696 bool counter_clockwise)
697{
698 constexpr double Tau = 6.28318530717958647692;
699 if(!std::isfinite(start.x) || !std::isfinite(start.y) || !std::isfinite(start.z) ||
700 !std::isfinite(finish.x) || !std::isfinite(finish.y) || !std::isfinite(finish.z) ||
701 !std::isfinite(signed_radius) || !std::isfinite(plane_normal.x) ||
702 !std::isfinite(plane_normal.y) || !std::isfinite(plane_normal.z)) {
704 }
705 const double n_len = norm(plane_normal);
706 const double r = std::fabs(signed_radius);
707 const Vec3 d_vec = finish - start;
708 const double d = norm(d_vec);
709 if(!std::isfinite(n_len) || n_len <= 1e-12 || r <= 1e-12 ||
710 !std::isfinite(d) || d <= 1e-12 || d > 2.0 * r * (1.0 + 1e-12)) {
712 }
713 const Vec3 n = plane_normal * (1.0 / n_len);
714 const double plane_tol = 1e-9 * d;
715 if(std::fabs(dot(d_vec, n)) > plane_tol) {
717 }
718 const Vec3 d_in_plane = d_vec - n * dot(d_vec, n);
719 const double d_plane_len = norm(d_in_plane);
720 if(!std::isfinite(d_plane_len) || d_plane_len <= 1e-12 || d_plane_len < 0.5 * d) {
722 }
723 const Vec3 e = d_in_plane * (1.0 / d_plane_len);
724 const Vec3 m = cross(n, e);
725 if(!std::isfinite(m.x) || !std::isfinite(m.y) || !std::isfinite(m.z) ||
726 norm(m) < 0.5) {
728 }
729 const double half_d = 0.5 * d;
730 const double eff_r = half_d > r ? half_d : r;
731 double ratio = half_d / eff_r;
732 if(ratio > 1.0) {
733 ratio = 1.0;
734 }
735 const double cos_half_sq = 1.0 - ratio * ratio;
736 const double cos_half = cos_half_sq > 0.0 ? std::sqrt(cos_half_sq) : 0.0;
737 const double h = eff_r * cos_half;
738 const double side =
739 (counter_clockwise ? 1.0 : -1.0) * (signed_radius > 0.0 ? 1.0 : -1.0);
740 const Vec3 center = (start + finish) * 0.5 + m * (side * h);
741 const double minor_sweep = 2.0 * std::asin(ratio);
742 const double mag_sweep = signed_radius > 0.0 ? minor_sweep : (Tau - minor_sweep);
743 const double sweep = counter_clockwise ? mag_sweep : -mag_sweep;
744 const double length = mag_sweep * eff_r;
745 if(!std::isfinite(center.x) || !std::isfinite(center.y) || !std::isfinite(center.z) ||
746 !std::isfinite(sweep) || std::fabs(sweep) <= 0.0 ||
747 !std::isfinite(length) || length <= 0.0) {
749 }
750 const Vec3 u = e * (-ratio) + m * (-side * cos_half);
751 const Vec3 v = cross(n, u);
752
753 ArcSegment arc{};
754 arc.start = start;
755 arc.via = center;
756 arc.finish = finish;
757 arc.center = center;
758 arc.u = u;
759 arc.v = v;
760 arc.radius = eff_r;
761 arc.start_angle = 0.0;
762 arc.sweep = sweep;
763 arc.length = length;
764 arc.spatial = true;
765 arc.full_circle = false;
766 arc.local_basis = true;
768}
769
770namespace detail
771{
772
773inline bool finite_vec3(Vec3 value)
774{
775 return std::isfinite(value.x) && std::isfinite(value.y) && std::isfinite(value.z);
776}
777
778// 5-point Gauss-Legendre quadrature of ||d1(u)|| on [u_a, u_b].
779template <typename DerivFn>
780inline double gauss_legendre_5_arclength(double u_a, double u_b, DerivFn deriv_fn)
781{
782 constexpr double Nodes[5] = {
783 -0.90617984593866399280,
784 -0.53846931010568309104,
785 0.0,
786 0.53846931010568309104,
787 0.90617984593866399280,
788 };
789 constexpr double Weights[5] = {
790 0.23692688505618908751,
791 0.47862867049936646804,
792 0.56888888888888888889,
793 0.47862867049936646804,
794 0.23692688505618908751,
795 };
796 const double half = 0.5 * (u_b - u_a);
797 if(std::fabs(half) <= 1e-18) {
798 return 0.0;
799 }
800 const double mid = 0.5 * (u_a + u_b);
801 double sum = 0.0;
802 for(int i = 0; i < 5; ++i) {
803 sum += Weights[i] * norm(deriv_fn(mid + half * Nodes[i]));
804 }
805 return half * sum;
806}
807
808template <std::size_t TableSize, typename DerivFn>
809inline double build_arclength_table(double (&cumulative)[TableSize], DerivFn deriv_fn)
810{
811 constexpr std::size_t Intervals = TableSize - 1;
812 double accumulated = 0.0;
813 cumulative[0] = 0.0;
814 for(std::size_t i = 0; i < Intervals; ++i) {
815 const double u_a = static_cast<double>(i) / static_cast<double>(Intervals);
816 const double u_b = static_cast<double>(i + 1) / static_cast<double>(Intervals);
817 accumulated += gauss_legendre_5_arclength(u_a, u_b, deriv_fn);
818 cumulative[i + 1] = accumulated;
819 }
820 return accumulated;
821}
822
823template <std::size_t TableSize, typename DerivFn>
824inline double invert_arclength_table(const double (&cumulative)[TableSize],
825 double total_length,
826 double arclength,
827 DerivFn deriv_fn)
828{
829 if(total_length <= 0.0 || !std::isfinite(total_length)) {
830 return 0.0;
831 }
832 const double target = clamp_arclength(arclength, total_length);
833 if(target <= 0.0) {
834 return 0.0;
835 }
836 if(target >= total_length) {
837 return 1.0;
838 }
839
840 constexpr std::size_t Last = TableSize - 1;
841 std::size_t lo = 0;
842 std::size_t hi = Last - 1;
843 while(lo < hi) {
844 const std::size_t mid = lo + (hi - lo + 1) / 2;
845 if(cumulative[mid] <= target) {
846 lo = mid;
847 } else {
848 hi = mid - 1;
849 }
850 }
851
852 const double u_a = static_cast<double>(lo) / static_cast<double>(Last);
853 const double u_b = static_cast<double>(lo + 1) / static_cast<double>(Last);
854 const double s_a = cumulative[lo];
855 const double seg = cumulative[lo + 1] - s_a;
856 double u = seg > 0.0 ? u_a + (u_b - u_a) * ((target - s_a) / seg) : u_a;
857
858 const double tol = 1e-13 * (total_length > 1.0 ? total_length : 1.0);
859 double b_lo = u_a;
860 double b_hi = u_b;
861 for(int step = 0; step < 4; ++step) {
862 const double s_est = s_a + gauss_legendre_5_arclength(u_a, u, deriv_fn);
863 if(!std::isfinite(s_est)) {
864 u = 0.5 * (b_lo + b_hi);
865 break;
866 }
867 const double err = s_est - target;
868 if(std::fabs(err) <= tol) {
869 break;
870 }
871 if(err < 0.0) {
872 b_lo = u;
873 } else {
874 b_hi = u;
875 }
876 if(b_hi <= b_lo) {
877 u = b_lo;
878 break;
879 }
880 const double speed = norm(deriv_fn(u));
881 if(!std::isfinite(speed) || speed <= 1e-15) {
882 u = 0.5 * (b_lo + b_hi);
883 continue;
884 }
885 double next_u = u - err / speed;
886 if(!std::isfinite(next_u) || next_u <= b_lo || next_u >= b_hi) {
887 next_u = 0.5 * (b_lo + b_hi);
888 }
889 u = next_u;
890 }
891 return u;
892}
893
894inline double quadratic_min_speed(Vec3 start, Vec3 control, Vec3 finish)
895{
896 const Vec3 a = (control - start) * 2.0;
897 const Vec3 b = (finish - control * 2.0 + start) * 2.0;
898 double min_speed = std::min(norm(a), norm(a + b));
899 const double b2 = dot(b, b);
900 if(b2 > 1e-30) {
901 double u_star = -dot(a, b) / b2;
902 if(u_star > 0.0 && u_star < 1.0) {
903 min_speed = std::min(min_speed, norm(a + b * u_star));
904 }
905 }
906 return min_speed;
907}
908
909inline void sample_quadratic_roots_speed(double a,
910 double b,
911 double c,
912 Vec3 c0,
913 Vec3 c1,
914 Vec3 c2,
915 double &min_speed)
916{
917 auto eval_speed = [&](double u) {
918 if(u > 0.0 && u < 1.0) {
919 const Vec3 d = c0 + c1 * u + c2 * (u * u);
920 min_speed = std::min(min_speed, norm(d));
921 }
922 };
923 if(std::fabs(a) <= 1e-18) {
924 if(std::fabs(b) > 1e-18) {
925 eval_speed(-c / b);
926 }
927 return;
928 }
929 const double disc = b * b - 4.0 * a * c;
930 if(disc < 0.0 || !std::isfinite(disc)) {
931 return;
932 }
933 const double root_disc = std::sqrt(disc);
934 eval_speed((-b - root_disc) / (2.0 * a));
935 eval_speed((-b + root_disc) / (2.0 * a));
936}
937
938inline double cubic_min_speed(Vec3 p0, Vec3 p1, Vec3 p2, Vec3 p3)
939{
940 const Vec3 c0 = (p1 - p0) * 3.0;
941 const Vec3 c1 = (p2 - p1 * 2.0 + p0) * 6.0;
942 const Vec3 c2 = (p3 - p2 * 3.0 + p1 * 3.0 - p0) * 3.0;
943 double min_speed = std::min(norm(c0), norm(c0 + c1 + c2));
944 for(int k = 1; k < 64; ++k) {
945 const double u = static_cast<double>(k) / 64.0;
946 min_speed = std::min(min_speed, norm(c0 + c1 * u + c2 * (u * u)));
947 }
948 sample_quadratic_roots_speed(c2.x, c1.x, c0.x, c0, c1, c2, min_speed);
949 sample_quadratic_roots_speed(c2.y, c1.y, c0.y, c0, c1, c2, min_speed);
950 sample_quadratic_roots_speed(c2.z, c1.z, c0.z, c0, c1, c2, min_speed);
951 return min_speed;
952}
953
955{
956 const double sigma = norm(d1);
957 if(sigma <= 1e-15) {
958 return {0.0, 0.0, 0.0};
959 }
960 return d1 * (1.0 / sigma);
961}
962
964{
965 const double sigma2 = dot(d1, d1);
966 if(sigma2 <= 1e-30) {
967 return {0.0, 0.0, 0.0};
968 }
969 return (d2 - d1 * (dot(d2, d1) / sigma2)) * (1.0 / sigma2);
970}
971
973{
974 const double sigma2 = dot(d1, d1);
975 if(sigma2 <= 1e-30) {
976 return {0.0, 0.0, 0.0};
977 }
978 const double alpha = dot(d1, d2);
979 const double beta = dot(d2, d2) + dot(d1, d3);
980 const double sigma5 = sigma2 * sigma2 * std::sqrt(sigma2);
981 return (d3 * sigma2 - d2 * (3.0 * alpha) + d1 * (4.0 * alpha * alpha / sigma2 - beta)) *
982 (1.0 / sigma5);
983}
984
985} // namespace detail
986
987inline Vec3 bezier_point(Vec3 p0, Vec3 p1, Vec3 p2, Vec3 p3, double u)
988{
989 const double one_minus = 1.0 - u;
990 return p0 * (one_minus * one_minus * one_minus) +
991 p1 * (3.0 * one_minus * one_minus * u) +
992 p2 * (3.0 * one_minus * u * u) +
993 p3 * (u * u * u);
994}
995
996inline Vec3 bezier_derivative(Vec3 p0, Vec3 p1, Vec3 p2, Vec3 p3, double u)
997{
998 const double one_minus = 1.0 - u;
999 return (p1 - p0) * (3.0 * one_minus * one_minus) +
1000 (p2 - p1) * (6.0 * one_minus * u) +
1001 (p3 - p2) * (3.0 * u * u);
1002}
1003
1004inline Vec3 bezier_second_derivative(Vec3 p0, Vec3 p1, Vec3 p2, Vec3 p3, double u)
1005{
1006 return (p2 - p1 * 2.0 + p0) * (6.0 * (1.0 - u)) +
1007 (p3 - p2 * 2.0 + p1) * (6.0 * u);
1008}
1009
1011{
1012 return (p3 - p2 * 3.0 + p1 * 3.0 - p0) * 6.0;
1013}
1014
1015inline Vec3 quadratic_point(Vec3 p0, Vec3 p1, Vec3 p2, double u)
1016{
1017 const double one_minus = 1.0 - u;
1018 return p0 * (one_minus * one_minus) + p1 * (2.0 * one_minus * u) + p2 * (u * u);
1019}
1020
1021inline Vec3 quadratic_derivative(Vec3 p0, Vec3 p1, Vec3 p2, double u)
1022{
1023 return (p1 - p0) * (2.0 * (1.0 - u)) + (p2 - p1) * (2.0 * u);
1024}
1025
1027{
1028 return (p2 - p1 * 2.0 + p0) * 2.0;
1029}
1030
1032{
1034 !detail::finite_vec3(p3)) {
1036 }
1037 CubicBezierSegment curve{};
1038 curve.p0 = p0;
1039 curve.p1 = p1;
1040 curve.p2 = p2;
1041 curve.p3 = p3;
1043 curve.cumulative, [&](double u) { return bezier_derivative(p0, p1, p2, p3, u); });
1044 if(!std::isfinite(curve.length) || curve.length <= 1e-12) {
1046 }
1047 const double min_speed = detail::cubic_min_speed(p0, p1, p2, p3);
1048 if(!std::isfinite(min_speed) || min_speed <= 1e-9 * curve.length) {
1050 }
1052}
1053
1054inline double cubic_parameter_at_length(const CubicBezierSegment &curve, double arclength)
1055{
1056 return detail::invert_arclength_table(curve.cumulative, curve.length, arclength, [&](double u) {
1057 return bezier_derivative(curve.p0, curve.p1, curve.p2, curve.p3, u);
1058 });
1059}
1060
1061inline Vec3 sample(const CubicBezierSegment &curve, double arclength)
1062{
1063 const double u = cubic_parameter_at_length(curve, arclength);
1064 return bezier_point(curve.p0, curve.p1, curve.p2, curve.p3, u);
1065}
1066
1067inline Vec3 tangent(const CubicBezierSegment &curve, double arclength)
1068{
1069 const double u = cubic_parameter_at_length(curve, arclength);
1070 return normalize(bezier_derivative(curve.p0, curve.p1, curve.p2, curve.p3, u));
1071}
1072
1073inline Vec3 path_derivative(const CubicBezierSegment &curve, double arclength)
1074{
1075 const double u = cubic_parameter_at_length(curve, arclength);
1077 bezier_derivative(curve.p0, curve.p1, curve.p2, curve.p3, u));
1078}
1079
1080inline Vec3 path_second_derivative(const CubicBezierSegment &curve, double arclength)
1081{
1082 const double u = cubic_parameter_at_length(curve, arclength);
1084 bezier_derivative(curve.p0, curve.p1, curve.p2, curve.p3, u),
1085 bezier_second_derivative(curve.p0, curve.p1, curve.p2, curve.p3, u));
1086}
1087
1088inline Vec3 path_third_derivative(const CubicBezierSegment &curve, double arclength)
1089{
1090 const double u = cubic_parameter_at_length(curve, arclength);
1092 bezier_derivative(curve.p0, curve.p1, curve.p2, curve.p3, u),
1093 bezier_second_derivative(curve.p0, curve.p1, curve.p2, curve.p3, u),
1094 bezier_third_derivative(curve.p0, curve.p1, curve.p2, curve.p3));
1095}
1096
1098 Vec3 control,
1099 Vec3 finish,
1100 double tolerance)
1101{
1102 if(tolerance <= 0.0 || !std::isfinite(tolerance) || !detail::finite_vec3(start) ||
1103 !detail::finite_vec3(control) || !detail::finite_vec3(finish)) {
1105 }
1106
1107 const Vec3 chord = finish - start;
1108 const double chord_length = norm(chord);
1109 if(chord_length <= 1e-12) {
1111 }
1112
1113 QuadraticBlendSegment blend{};
1114 blend.start = start;
1115 blend.control = control;
1116 blend.finish = finish;
1117 blend.length = detail::build_arclength_table(blend.cumulative, [&](double u) {
1118 return quadratic_derivative(start, control, finish, u);
1119 });
1120 if(!std::isfinite(blend.length) || blend.length <= 1e-12) {
1122 }
1123 const double min_speed = detail::quadratic_min_speed(start, control, finish);
1124 if(!std::isfinite(min_speed) || min_speed <= 1e-9 * blend.length) {
1126 }
1127
1128 const double deviation =
1129 norm(control - (start + chord * (dot(control - start, chord) / dot(chord, chord)))) * 0.5;
1130 if(deviation > tolerance + 1e-12) {
1132 }
1133 blend.max_deviation = deviation;
1134
1136}
1137
1138inline double quadratic_parameter_at_length(const QuadraticBlendSegment &blend, double arclength)
1139{
1140 return detail::invert_arclength_table(blend.cumulative, blend.length, arclength, [&](double u) {
1141 return quadratic_derivative(blend.start, blend.control, blend.finish, u);
1142 });
1143}
1144
1145inline Vec3 sample(const QuadraticBlendSegment &blend, double arclength)
1146{
1147 const double u = quadratic_parameter_at_length(blend, arclength);
1148 return quadratic_point(blend.start, blend.control, blend.finish, u);
1149}
1150
1151inline Vec3 tangent(const QuadraticBlendSegment &blend, double arclength)
1152{
1153 const double u = quadratic_parameter_at_length(blend, arclength);
1154 return normalize(quadratic_derivative(blend.start, blend.control, blend.finish, u));
1155}
1156
1157inline Vec3 path_derivative(const QuadraticBlendSegment &blend, double arclength)
1158{
1159 const double u = quadratic_parameter_at_length(blend, arclength);
1161 quadratic_derivative(blend.start, blend.control, blend.finish, u));
1162}
1163
1164inline Vec3 path_second_derivative(const QuadraticBlendSegment &blend, double arclength)
1165{
1166 const double u = quadratic_parameter_at_length(blend, arclength);
1168 quadratic_derivative(blend.start, blend.control, blend.finish, u),
1169 quadratic_second_derivative(blend.start, blend.control, blend.finish));
1170}
1171
1172inline Vec3 path_third_derivative(const QuadraticBlendSegment &blend, double arclength)
1173{
1174 const double u = quadratic_parameter_at_length(blend, arclength);
1176 quadratic_derivative(blend.start, blend.control, blend.finish, u),
1177 quadratic_second_derivative(blend.start, blend.control, blend.finish),
1178 {0.0, 0.0, 0.0});
1179}
1180
1181inline Vec3 quintic_point(const QuinticBlendSegment &blend, double u)
1182{
1183 const double v = 1.0 - u;
1184 const double v2 = v * v;
1185 const double u2 = u * u;
1186 return blend.p0 * (v2 * v2 * v) + blend.p1 * (5.0 * v2 * v2 * u) +
1187 blend.p2 * (10.0 * v2 * v * u2) + blend.p3 * (10.0 * v2 * u2 * u) +
1188 blend.p4 * (5.0 * v * u2 * u2) + blend.p5 * (u2 * u2 * u);
1189}
1190
1191inline Vec3 quintic_derivative(const QuinticBlendSegment &blend, double u)
1192{
1193 const double v = 1.0 - u;
1194 const double v2 = v * v;
1195 const double u2 = u * u;
1196 return (blend.p1 - blend.p0) * (5.0 * v2 * v2) +
1197 (blend.p2 - blend.p1) * (20.0 * v2 * v * u) +
1198 (blend.p3 - blend.p2) * (30.0 * v2 * u2) +
1199 (blend.p4 - blend.p3) * (20.0 * v * u2 * u) +
1200 (blend.p5 - blend.p4) * (5.0 * u2 * u2);
1201}
1202
1204{
1205 const double v = 1.0 - u;
1206 const Vec3 d0 = blend.p2 - blend.p1 * 2.0 + blend.p0;
1207 const Vec3 d1 = blend.p3 - blend.p2 * 2.0 + blend.p1;
1208 const Vec3 d2 = blend.p4 - blend.p3 * 2.0 + blend.p2;
1209 const Vec3 d3 = blend.p5 - blend.p4 * 2.0 + blend.p3;
1210 return d0 * (20.0 * v * v * v) + d1 * (60.0 * v * v * u) + d2 * (60.0 * v * u * u) +
1211 d3 * (20.0 * u * u * u);
1212}
1213
1215{
1216 const double v = 1.0 - u;
1217 const Vec3 e0 = blend.p3 - blend.p2 * 3.0 + blend.p1 * 3.0 - blend.p0;
1218 const Vec3 e1 = blend.p4 - blend.p3 * 3.0 + blend.p2 * 3.0 - blend.p1;
1219 const Vec3 e2 = blend.p5 - blend.p4 * 3.0 + blend.p3 * 3.0 - blend.p2;
1220 return e0 * (60.0 * v * v) + e1 * (120.0 * v * u) + e2 * (60.0 * u * u);
1221}
1222
1223// start/finish are the truncated junction points on the adjacent segments;
1224// corner is the original path corner. The maximum deviation of the symmetric
1225// uniform-thirds construction is closed-form: |B(1/2) - corner| =
1226// (23/96)|(finish-corner) - (corner-start)|.
1228 Vec3 corner,
1229 Vec3 finish,
1230 double tolerance)
1231{
1232 if(tolerance <= 0.0 || !std::isfinite(tolerance)) {
1234 }
1235 const Vec3 in = corner - start;
1236 const Vec3 out = finish - corner;
1237 if(norm(in) <= 1e-12 || norm(out) <= 1e-12) {
1239 }
1240
1241 QuinticBlendSegment blend{};
1242 blend.p0 = start;
1243 blend.p1 = corner - in * (2.0 / 3.0);
1244 blend.p2 = corner - in * (1.0 / 3.0);
1245 blend.p3 = corner + out * (1.0 / 3.0);
1246 blend.p4 = corner + out * (2.0 / 3.0);
1247 blend.p5 = finish;
1248
1249 blend.max_deviation = norm(quintic_point(blend, 0.5) - corner);
1250 if(blend.max_deviation > tolerance + 1e-12) {
1252 }
1253
1254 // Arc-length table (planning-phase work) for junction-continuous sampling.
1255 double accumulated = 0.0;
1256 Vec3 previous = quintic_point(blend, 0.0);
1257 blend.cumulative[0] = 0.0;
1258 for(std::size_t i = 1; i < QuinticBlendSegment::TableSize; ++i) {
1259 const double u =
1260 static_cast<double>(i) / static_cast<double>(QuinticBlendSegment::TableSize - 1);
1261 const Vec3 current = quintic_point(blend, u);
1262 accumulated += norm(current - previous);
1263 blend.cumulative[i] = accumulated;
1264 previous = current;
1265 }
1266 blend.length = accumulated;
1267 if(!std::isfinite(blend.length) || blend.length <= 1e-12) {
1269 }
1270
1271 double max_curvature = 0.0;
1272 for(std::size_t i = 0; i <= 64; ++i) {
1273 const double u = static_cast<double>(i) / 64.0;
1274 const Vec3 d1 = quintic_derivative(blend, u);
1275 const Vec3 d2 = quintic_second_derivative(blend, u);
1276 const double speed = norm(d1);
1277 if(speed <= 1e-12) {
1278 continue;
1279 }
1280 const double curvature = norm(cross(d1, d2)) / (speed * speed * speed);
1281 if(curvature > max_curvature) {
1282 max_curvature = curvature;
1283 }
1284 }
1285 blend.max_curvature = max_curvature;
1287}
1288
1289inline double quintic_parameter_at_length(const QuinticBlendSegment &blend, double arclength)
1290{
1291 const double target = clamp_arclength(arclength, blend.length);
1292 constexpr std::size_t Last = QuinticBlendSegment::TableSize - 1;
1293 std::size_t low = 0;
1294 for(std::size_t i = 1; i <= Last; ++i) {
1295 if(blend.cumulative[i] >= target) {
1296 low = i - 1;
1297 break;
1298 }
1299 low = i - 1;
1300 }
1301 const double segment = blend.cumulative[low + 1] - blend.cumulative[low];
1302 const double fraction = segment > 0.0 ? (target - blend.cumulative[low]) / segment : 0.0;
1303 return (static_cast<double>(low) + fraction) / static_cast<double>(Last);
1304}
1305
1306inline Vec3 sample(const QuinticBlendSegment &blend, double arclength)
1307{
1308 return quintic_point(blend, quintic_parameter_at_length(blend, arclength));
1309}
1310
1311inline Vec3 tangent(const QuinticBlendSegment &blend, double arclength)
1312{
1313 return normalize(quintic_derivative(blend, quintic_parameter_at_length(blend, arclength)));
1314}
1315
1316inline Vec3 path_derivative(const QuinticBlendSegment &blend, double arclength)
1317{
1318 const double u = quintic_parameter_at_length(blend, arclength);
1319 const Vec3 d1 = quintic_derivative(blend, u);
1320 const double sigma = norm(d1);
1321 if(sigma <= 1e-15) {
1322 return {0.0, 0.0, 0.0};
1323 }
1324 return d1 * (1.0 / sigma);
1325}
1326
1327inline Vec3 path_second_derivative(const QuinticBlendSegment &blend, double arclength)
1328{
1329 const double u = quintic_parameter_at_length(blend, arclength);
1330 const Vec3 d1 = quintic_derivative(blend, u);
1331 const Vec3 d2 = quintic_second_derivative(blend, u);
1332 const double sigma2 = dot(d1, d1);
1333 if(sigma2 <= 1e-30) {
1334 return {0.0, 0.0, 0.0};
1335 }
1336 return (d2 - d1 * (dot(d2, d1) / sigma2)) * (1.0 / sigma2);
1337}
1338
1339inline Vec3 path_third_derivative(const QuinticBlendSegment &blend, double arclength)
1340{
1341 const double u = quintic_parameter_at_length(blend, arclength);
1342 const Vec3 d1 = quintic_derivative(blend, u);
1343 const Vec3 d2 = quintic_second_derivative(blend, u);
1344 const Vec3 d3 = quintic_third_derivative(blend, u);
1345 const double sigma2 = dot(d1, d1);
1346 if(sigma2 <= 1e-30) {
1347 return {0.0, 0.0, 0.0};
1348 }
1349 const double alpha = dot(d1, d2);
1350 const double beta = dot(d2, d2) + dot(d1, d3);
1351 const double sigma5 = sigma2 * sigma2 * std::sqrt(sigma2);
1352 return (d3 * sigma2 - d2 * (3.0 * alpha) + d1 * (4.0 * alpha * alpha / sigma2 - beta)) *
1353 (1.0 / sigma5);
1354}
1355
1364
1366{
1373
1374 double length() const
1375 {
1376 if(kind == SegmentKind::line) {
1377 return line.length;
1378 }
1379 if(kind == SegmentKind::arc) {
1380 return arc.length;
1381 }
1383 return quintic.length;
1384 }
1385 return kind == SegmentKind::cubic_bezier ? cubic.length : blend.length;
1386 }
1387
1388 Vec3 sample(double arclength) const
1389 {
1390 if(kind == SegmentKind::line) {
1391 return geom::sample(line, arclength);
1392 }
1393 if(kind == SegmentKind::arc) {
1394 return geom::sample(arc, arclength);
1395 }
1397 return geom::sample(quintic, arclength);
1398 }
1399 return kind == SegmentKind::cubic_bezier ? geom::sample(cubic, arclength)
1400 : geom::sample(blend, arclength);
1401 }
1402
1403 Vec3 tangent(double arclength) const
1404 {
1405 if(kind == SegmentKind::line) {
1406 return geom::tangent(line);
1407 }
1408 if(kind == SegmentKind::arc) {
1409 return geom::tangent(arc, arclength);
1410 }
1412 return geom::tangent(quintic, arclength);
1413 }
1414 return kind == SegmentKind::cubic_bezier ? geom::tangent(cubic, arclength)
1415 : geom::tangent(blend, arclength);
1416 }
1417
1418 Vec3 path_derivative(double arclength) const
1419 {
1420 if(kind == SegmentKind::line) {
1422 }
1423 if(kind == SegmentKind::arc) {
1424 return geom::path_derivative(arc, arclength);
1425 }
1427 return geom::path_derivative(quintic, arclength);
1428 }
1430 : geom::path_derivative(blend, arclength);
1431 }
1432
1433 Vec3 path_second_derivative(double arclength) const
1434 {
1435 if(kind == SegmentKind::line) {
1437 }
1438 if(kind == SegmentKind::arc) {
1439 return geom::path_second_derivative(arc, arclength);
1440 }
1442 return geom::path_second_derivative(quintic, arclength);
1443 }
1446 : geom::path_second_derivative(blend, arclength);
1447 }
1448
1449 Vec3 path_third_derivative(double arclength) const
1450 {
1451 if(kind == SegmentKind::line) {
1453 }
1454 if(kind == SegmentKind::arc) {
1455 return geom::path_third_derivative(arc, arclength);
1456 }
1458 return geom::path_third_derivative(quintic, arclength);
1459 }
1462 : geom::path_third_derivative(blend, arclength);
1463 }
1464
1465 Vec3 start() const
1466 {
1467 return sample(0.0);
1468 }
1469
1470 Vec3 finish() const
1471 {
1472 return sample(length());
1473 }
1474};
1475
1477{
1478 PathSegment segment{};
1479 segment.kind = SegmentKind::line;
1480 segment.line = line;
1481 return segment;
1482}
1483
1485{
1486 PathSegment segment{};
1487 segment.kind = SegmentKind::arc;
1488 segment.arc = arc;
1489 return segment;
1490}
1491
1493{
1494 PathSegment segment{};
1496 segment.cubic = cubic;
1497 return segment;
1498}
1499
1501{
1502 PathSegment segment{};
1504 segment.blend = blend;
1505 return segment;
1506}
1507
1509{
1510 PathSegment segment{};
1512 segment.quintic = blend;
1513 return segment;
1514}
1515
1516template <std::size_t Capacity> class ArcLengthTable
1517{
1518public:
1520 {
1521 lengths_.clear();
1522 for(std::size_t i = 0; i < Capacity; ++i) {
1523 const double ratio = static_cast<double>(i) / static_cast<double>(Capacity - 1);
1524 const rt::ErrorCode pushed = lengths_.push_back(segment.length() * ratio);
1525 if(pushed != rt::ErrorCode::ok) {
1526 return pushed;
1527 }
1528 }
1529 return rt::ErrorCode::ok;
1530 }
1531
1532 std::size_t size() const
1533 {
1534 return lengths_.size();
1535 }
1536
1537 double total_length() const
1538 {
1539 return lengths_.empty() ? 0.0 : lengths_[lengths_.size() - 1];
1540 }
1541
1542 double parameter_at_length(double arclength) const
1543 {
1544 if(lengths_.empty() || total_length() <= 0.0) {
1545 return 0.0;
1546 }
1547 return clamp_arclength(arclength, total_length()) / total_length();
1548 }
1549
1550private:
1551 static_assert(Capacity >= 2, "ArcLengthTable capacity must be at least two");
1553};
1554
1555} // namespace plcopen::core::geom
double parameter_at_length(double arclength) const
rt::ErrorCode build(const PathSegment &segment)
static constexpr Result success(const T &value)
定义 error.h:24
static constexpr Result failure(ErrorCode error)
定义 error.h:29
Vec3 arc_length_first_derivative(Vec3 d1)
double build_arclength_table(double(&cumulative)[TableSize], DerivFn deriv_fn)
double cubic_min_speed(Vec3 p0, Vec3 p1, Vec3 p2, Vec3 p3)
double gauss_legendre_5_arclength(double u_a, double u_b, DerivFn deriv_fn)
void sample_quadratic_roots_speed(double a, double b, double c, Vec3 c0, Vec3 c1, Vec3 c2, double &min_speed)
double quadratic_min_speed(Vec3 start, Vec3 control, Vec3 finish)
Vec3 arc_length_second_derivative(Vec3 d1, Vec3 d2)
double invert_arclength_table(const double(&cumulative)[TableSize], double total_length, double arclength, DerivFn deriv_fn)
Vec3 arc_length_third_derivative(Vec3 d1, Vec3 d2, Vec3 d3)
Vec3 bezier_point(Vec3 p0, Vec3 p1, Vec3 p2, Vec3 p3, double u)
Vec3 operator+(Vec3 lhs, Vec3 rhs)
Vec3 quintic_third_derivative(const QuinticBlendSegment &blend, double u)
rt::Result< ArcSegment > make_arc_center_xy(Vec3 start, Vec3 center, Vec3 finish, bool counter_clockwise)
Vec3 quintic_second_derivative(const QuinticBlendSegment &blend, double u)
Vec3 operator-(Vec3 lhs, Vec3 rhs)
double quadratic_parameter_at_length(const QuadraticBlendSegment &blend, double arclength)
rt::Result< CubicBezierSegment > make_cubic_bezier(Vec3 p0, Vec3 p1, Vec3 p2, Vec3 p3)
Vec3 bezier_second_derivative(Vec3 p0, Vec3 p1, Vec3 p2, Vec3 p3, double u)
Vec3 bezier_derivative(Vec3 p0, Vec3 p1, Vec3 p2, Vec3 p3, double u)
Vec3 path_derivative(LineSegment line)
PathSegment as_path_segment(LineSegment line)
Vec3 sample(LineSegment line, double arclength)
Vec3 cross(Vec3 lhs, Vec3 rhs)
Vec3 operator*(Vec3 value, double scale)
Vec3 quadratic_derivative(Vec3 p0, Vec3 p1, Vec3 p2, double u)
Vec3 quadratic_point(Vec3 p0, Vec3 p1, Vec3 p2, double u)
rt::Result< ArcSegment > make_spatial_arc_center(Vec3 start, Vec3 center, Vec3 finish, Vec3 plane_normal, bool counter_clockwise)
Vec3 tangent(LineSegment line)
double norm(Vec3 value)
double quintic_parameter_at_length(const QuinticBlendSegment &blend, double arclength)
double dot(Vec3 lhs, Vec3 rhs)
rt::Result< ArcSegment > make_arc_radius_xy(Vec3 start, Vec3 finish, double signed_radius, bool counter_clockwise)
rt::Result< ArcSegment > make_arc(Vec3 start, Vec3 via, Vec3 finish)
rt::Result< LineSegment > make_line(Vec3 start, Vec3 finish)
Vec3 path_second_derivative(LineSegment)
rt::Result< ArcSegment > make_spatial_arc_border(Vec3 start, Vec3 via, Vec3 finish, Vec3 ref_normal, bool counter_clockwise)
double angle_of(Vec3 point, Vec3 center)
double clamp_arclength(double value, double length)
double cubic_parameter_at_length(const CubicBezierSegment &curve, double arclength)
Vec3 quintic_point(const QuinticBlendSegment &blend, double u)
rt::Result< QuadraticBlendSegment > make_quadratic_blend(Vec3 start, Vec3 control, Vec3 finish, double tolerance)
double normalize_sweep(double sweep, double orientation)
Vec3 path_third_derivative(LineSegment)
Vec3 normalize(Vec3 value)
Vec3 quintic_derivative(const QuinticBlendSegment &blend, double u)
Vec3 bezier_third_derivative(Vec3 p0, Vec3 p1, Vec3 p2, Vec3 p3)
rt::Result< QuinticBlendSegment > make_quintic_blend(Vec3 start, Vec3 corner, Vec3 finish, double tolerance)
Vec3 quadratic_second_derivative(Vec3 p0, Vec3 p1, Vec3 p2)
rt::Result< ArcSegment > make_spatial_arc_radius(Vec3 start, Vec3 finish, double signed_radius, Vec3 plane_normal, bool counter_clockwise)
static constexpr std::size_t TableSize
Vec3 sample(double arclength) const
Vec3 path_derivative(double arclength) const
Vec3 path_third_derivative(double arclength) const
Vec3 tangent(double arclength) const
Vec3 path_second_derivative(double arclength) const
static constexpr std::size_t TableSize
static constexpr std::size_t TableSize