22 return {lhs.
x + rhs.
x, lhs.
y + rhs.
y, lhs.
z + rhs.
z};
27 return {lhs.
x - rhs.
x, lhs.
y - rhs.
y, lhs.
z - rhs.
z};
32 return {value.
x * scale, value.
y * scale, value.
z * scale};
37 return lhs.
x * rhs.
x + lhs.
y * rhs.
y + lhs.
z * rhs.
z;
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};
49 return std::sqrt(
dot(value, value));
54 const double length =
norm(value);
58 return value * (1.0 / length);
81 const double length =
norm(finish - start);
82 if(!std::isfinite(length) || length <= 1e-12) {
101 return (
line.finish -
line.start) * (1.0 /
line.length);
106 return {0.0, 0.0, 0.0};
111 return {0.0, 0.0, 0.0};
175 return std::atan2(point.
y - center.y, point.
x - center.x);
180 constexpr double Tau = 6.28318530717958647692;
181 if(orientation >= 0.0) {
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));
210 if(!std::isfinite(d) || std::fabs(d) <= 1e-12) {
214 const double a2 = ax * ax + ay * ay;
215 const double b2 = bx * bx + by * by;
216 const double c2 = cx * cx + cy * cy;
218 (a2 * (by - cy) + b2 * (cy - ay) + c2 * (ay - by)) / d + gx,
219 (a2 * (cx - bx) + b2 * (ax - cx) + c2 * (bx - ax)) / d + gy,
222 const double radius =
norm({start.
x - center.x, start.
y - center.y, 0.0});
223 if(!std::isfinite(radius) || radius <= 1e-12) {
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;
232 if(std::fabs(sweep) <= 1e-12) {
242 arc.start_angle = start_angle;
244 arc.length = std::fabs(sweep) * radius;
250 if(
arc.local_basis) {
251 if(arclength <= 0.0) {
254 if(arclength >=
arc.length) {
257 const double ratio = arclength /
arc.length;
258 const double phi =
arc.sweep * ratio;
259 const double s_half = std::sin(0.5 * phi);
261 (
arc.u * (-2.0 * s_half * s_half) +
arc.v * std::sin(phi)) *
arc.radius;
263 return arc.start + delta;
266 arc.start.x + delta.
x,
267 arc.start.y + delta.
y,
268 arc.start.z + (
arc.finish.z -
arc.start.z) * ratio,
272 if(arclength <= 0.0) {
275 if(arclength >=
arc.length) {
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;
282 if(
arc.full_circle) {
283 if(arclength <= 0.0) {
286 if(arclength >=
arc.length) {
287 return {
arc.start.x,
arc.start.y,
arc.finish.z};
291 const double angle =
arc.start_angle +
arc.sweep * ratio;
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,
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;
308 return {-std::sin(angle) * direction, std::cos(angle) * direction, 0.0};
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;
318 return (
arc.u * (-std::sin(angle)) +
arc.v * std::cos(angle)) * (
arc.radius * omega);
320 if(
arc.local_basis) {
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};
325 return {-
arc.radius * std::sin(angle) * omega,
326 arc.radius * std::cos(angle) * omega,
327 (
arc.finish.z -
arc.start.z) /
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);
339 return {-
arc.radius * std::cos(angle) * omega2,
340 -
arc.radius * std::sin(angle) * omega2,
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);
354 return {
arc.radius * std::sin(angle) * omega3,
355 -
arc.radius * std::cos(angle) * omega3,
362 bool counter_clockwise)
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)) {
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) {
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;
382 sweep = counter_clockwise ? Tau : -Tau;
384 double raw =
angle_of(finish, center) - start_angle;
385 if(counter_clockwise) {
402 const double length = std::fabs(sweep) * r0;
403 if(!std::isfinite(sweep) || std::fabs(sweep) <= 1e-12 || !std::isfinite(length) ||
410 arc.finish = is_full ?
Vec3{start.
x, start.
y, finish.z} : finish;
411 arc.center = {center.x, center.y, 0.0};
413 arc.start_angle = start_angle;
417 arc.full_circle = is_full;
423 double signed_radius,
424 bool counter_clockwise)
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)) {
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)) {
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;
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;
453 (counter_clockwise ? 1.0 : -1.0) * (signed_radius > 0.0 ? 1.0 : -1.0);
455 0.5 * (start.
x + finish.x) + side * h * mx,
456 0.5 * (start.
y + finish.y) + side * h * my,
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) {
468 const double ux = -ratio * ex - side * cos_half * mx;
469 const double uy = -ratio * ey - side * cos_half * my;
475 arc.u = {ux, uy, 0.0};
476 arc.v = {-uy, ux, 0.0};
478 arc.start_angle = 0.0;
482 arc.full_circle =
false;
483 arc.local_basis =
true;
491 bool counter_clockwise)
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)) {
501 const double n_ref_len =
norm(ref_normal);
502 if(!std::isfinite(n_ref_len) || n_ref_len <= 1e-12) {
505 const Vec3 n_ref = ref_normal * (1.0 / n_ref_len);
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) {
518 const double nw =
norm(w);
519 if(!std::isfinite(nw) || nw <= 1e-12 * la * lb) {
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) {
527 const bool derived_ccw = align > 0.0;
528 if(derived_ccw != counter_clockwise) {
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;
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) {
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) {
565 const Vec3 n_basis = counter_clockwise ? n_tri : n_tri * (-1.0);
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) {
578 while(sweep >= 0.0) {
581 while(sweep < -Tau) {
585 if(!std::isfinite(sweep) || std::fabs(sweep) <= 1e-12) {
597 arc.start_angle = 0.0;
599 arc.length = std::fabs(sweep) * radius;
608 bool counter_clockwise)
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)) {
618 const double n_len =
norm(plane_normal);
619 if(!std::isfinite(n_len) || n_len <= 1e-12) {
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) {
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) {
640 const Vec3 u = r0_in_plane * (1.0 / r0_plane_len);
642 if(!std::isfinite(v.x) || !std::isfinite(v.y) || !std::isfinite(v.z) ||
646 const double chord =
norm(finish - start);
647 const bool is_full = chord <= 1e-12 * rad0;
650 sweep = counter_clockwise ? Tau : -Tau;
652 double phi = std::atan2(
dot(r1, v),
dot(r1, u));
653 if(counter_clockwise) {
670 const double length = std::fabs(sweep) * rad0;
671 if(!std::isfinite(sweep) || std::fabs(sweep) <= 1e-12 || !std::isfinite(length) ||
679 arc.finish = is_full ? start : finish;
684 arc.start_angle = 0.0;
688 arc.full_circle = is_full;
694 double signed_radius,
696 bool counter_clockwise)
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)) {
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)) {
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) {
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) {
723 const Vec3 e = d_in_plane * (1.0 / d_plane_len);
725 if(!std::isfinite(m.x) || !std::isfinite(m.y) || !std::isfinite(m.z) ||
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;
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;
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) {
750 const Vec3 u = e * (-ratio) + m * (-side * cos_half);
761 arc.start_angle = 0.0;
765 arc.full_circle =
false;
766 arc.local_basis =
true;
775 return std::isfinite(value.
x) && std::isfinite(value.
y) && std::isfinite(value.
z);
779template <
typename DerivFn>
782 constexpr double Nodes[5] = {
783 -0.90617984593866399280,
784 -0.53846931010568309104,
786 0.53846931010568309104,
787 0.90617984593866399280,
789 constexpr double Weights[5] = {
790 0.23692688505618908751,
791 0.47862867049936646804,
792 0.56888888888888888889,
793 0.47862867049936646804,
794 0.23692688505618908751,
796 const double half = 0.5 * (u_b - u_a);
797 if(std::fabs(half) <= 1e-18) {
800 const double mid = 0.5 * (u_a + u_b);
802 for(
int i = 0; i < 5; ++i) {
803 sum += Weights[i] *
norm(deriv_fn(mid + half * Nodes[i]));
808template <std::
size_t TableSize,
typename DerivFn>
811 constexpr std::size_t Intervals = TableSize - 1;
812 double accumulated = 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);
818 cumulative[i + 1] = accumulated;
823template <std::
size_t TableSize,
typename DerivFn>
829 if(total_length <= 0.0 || !std::isfinite(total_length)) {
836 if(target >= total_length) {
840 constexpr std::size_t Last = TableSize - 1;
842 std::size_t hi = Last - 1;
844 const std::size_t mid = lo + (hi - lo + 1) / 2;
845 if(cumulative[mid] <= target) {
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;
858 const double tol = 1e-13 * (total_length > 1.0 ? total_length : 1.0);
861 for(
int step = 0; step < 4; ++step) {
863 if(!std::isfinite(s_est)) {
864 u = 0.5 * (b_lo + b_hi);
867 const double err = s_est - target;
868 if(std::fabs(err) <= tol) {
880 const double speed =
norm(deriv_fn(u));
881 if(!std::isfinite(speed) || speed <= 1e-15) {
882 u = 0.5 * (b_lo + b_hi);
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);
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);
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));
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));
923 if(std::fabs(a) <= 1e-18) {
924 if(std::fabs(b) > 1e-18) {
929 const double disc = b * b - 4.0 * a * c;
930 if(disc < 0.0 || !std::isfinite(disc)) {
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));
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)));
956 const double sigma =
norm(d1);
958 return {0.0, 0.0, 0.0};
960 return d1 * (1.0 / sigma);
965 const double sigma2 =
dot(d1, d1);
966 if(sigma2 <= 1e-30) {
967 return {0.0, 0.0, 0.0};
969 return (d2 - d1 * (
dot(d2, d1) / sigma2)) * (1.0 / sigma2);
974 const double sigma2 =
dot(d1, d1);
975 if(sigma2 <= 1e-30) {
976 return {0.0, 0.0, 0.0};
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)) *
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) +
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);
1006 return (p2 - p1 * 2.0 + p0) * (6.0 * (1.0 - u)) +
1007 (p3 - p2 * 2.0 + p1) * (6.0 * u);
1012 return (p3 - p2 * 3.0 + p1 * 3.0 - p0) * 6.0;
1017 const double one_minus = 1.0 - u;
1018 return p0 * (one_minus * one_minus) + p1 * (2.0 * one_minus * u) + p2 * (u * u);
1023 return (p1 - p0) * (2.0 * (1.0 - u)) + (p2 - p1) * (2.0 * u);
1028 return (p2 - p1 * 2.0 + p0) * 2.0;
1043 curve.
cumulative, [&](
double u) { return bezier_derivative(p0, p1, p2, p3, u); });
1044 if(!std::isfinite(curve.
length) || curve.
length <= 1e-12) {
1048 if(!std::isfinite(min_speed) || min_speed <= 1e-9 * curve.
length) {
1057 return bezier_derivative(curve.p0, curve.p1, curve.p2, curve.p3, u);
1107 const Vec3 chord = finish - start;
1108 const double chord_length =
norm(chord);
1109 if(chord_length <= 1e-12) {
1114 blend.
start = start;
1118 return quadratic_derivative(start, control, finish, u);
1120 if(!std::isfinite(blend.
length) || blend.
length <= 1e-12) {
1124 if(!std::isfinite(min_speed) || min_speed <= 1e-9 * blend.
length) {
1128 const double deviation =
1129 norm(control - (start + chord * (
dot(control - start, chord) /
dot(chord, chord)))) * 0.5;
1130 if(deviation > tolerance + 1e-12) {
1141 return quadratic_derivative(blend.start, blend.control, blend.finish, u);
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);
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);
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);
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);
1232 if(tolerance <= 0.0 || !std::isfinite(tolerance)) {
1235 const Vec3 in = corner - start;
1236 const Vec3 out = finish - corner;
1237 if(
norm(in) <= 1e-12 ||
norm(out) <= 1e-12) {
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);
1255 double accumulated = 0.0;
1262 accumulated +=
norm(current - previous);
1266 blend.
length = accumulated;
1267 if(!std::isfinite(blend.
length) || blend.
length <= 1e-12) {
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;
1276 const double speed =
norm(d1);
1277 if(speed <= 1e-12) {
1280 const double curvature =
norm(
cross(d1, d2)) / (speed * speed * speed);
1281 if(curvature > max_curvature) {
1282 max_curvature = curvature;
1293 std::size_t low = 0;
1294 for(std::size_t i = 1; i <= Last; ++i) {
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);
1320 const double sigma =
norm(d1);
1321 if(sigma <= 1e-15) {
1322 return {0.0, 0.0, 0.0};
1324 return d1 * (1.0 / sigma);
1332 const double sigma2 =
dot(d1, d1);
1333 if(sigma2 <= 1e-30) {
1334 return {0.0, 0.0, 0.0};
1336 return (d2 - d1 * (
dot(d2, d1) / sigma2)) * (1.0 / sigma2);
1345 const double sigma2 =
dot(d1, d1);
1346 if(sigma2 <= 1e-30) {
1347 return {0.0, 0.0, 0.0};
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)) *
1496 segment.
cubic = cubic;
1504 segment.
blend = blend;
1522 for(std::size_t i = 0; i < Capacity; ++i) {
1523 const double ratio =
static_cast<double>(i) /
static_cast<double>(Capacity - 1);
1534 return lengths_.size();
1539 return lengths_.empty() ? 0.0 : lengths_[lengths_.size() - 1];
1551 static_assert(Capacity >= 2,
"ArcLengthTable capacity must be at least two");
double total_length() const
double parameter_at_length(double arclength) const
rt::ErrorCode build(const PathSegment &segment)
static constexpr Result success(const T &value)
static constexpr Result failure(ErrorCode error)
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)
bool finite_vec3(Vec3 value)
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 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
double cumulative[TableSize]
Vec3 sample(double arclength) const
Vec3 path_derivative(double arclength) const
QuadraticBlendSegment blend
Vec3 path_third_derivative(double arclength) const
Vec3 tangent(double arclength) const
Vec3 path_second_derivative(double arclength) const
QuinticBlendSegment quintic
double cumulative[TableSize]
static constexpr std::size_t TableSize
double cumulative[TableSize]
static constexpr std::size_t TableSize