PhysicsBasedAnimationToolkit 0.0.10
Cross-platform C++20 library of algorithms and data structures commonly used in computer graphics research on physically-based simulation.
Loading...
Searching...
No Matches
Primitive.h
Go to the documentation of this file.
1
12#ifndef PBAT_GEOMETRY_SDF_PRIMITIVE_H
13#define PBAT_GEOMETRY_SDF_PRIMITIVE_H
14
15#include "TypeDefs.h"
16#include "pbat/HostDevice.h"
19
20#include <algorithm>
21#include <cmath>
22
23namespace pbat::geometry::sdf {
31template <common::CArithmetic TScalar>
32TScalar sign(TScalar x)
33{
34 return static_cast<TScalar>(x > 0) - static_cast<TScalar>(x < 0);
35};
36
41{
42};
43
48template <common::CArithmetic TScalar>
49struct Sphere : public Primitive
50{
51 using ScalarType = TScalar;
56 Sphere() = default;
61 explicit Sphere(ScalarType R_)
62 : R(R_)
63 {
64 }
65
71 PBAT_HOST_DEVICE ScalarType Eval(Vec3<ScalarType> const& p) const { return Norm(p) - R; }
72};
73
79template <common::CArithmetic TScalar>
80struct Box : public Primitive
81{
82 using ScalarType = TScalar;
84 TScalar(0.5),
85 TScalar(0.5),
86 TScalar(0.5)};
87
90 Box() = default;
95 explicit Box(Vec3<ScalarType> const& he_)
96 : he(he_)
97 {
98 }
99
105 PBAT_HOST_DEVICE ScalarType Eval(Vec3<ScalarType> const& p) const
106 {
107 Vec3<ScalarType> q = Abs(p) - he;
108 Zero3<ScalarType> constexpr zero3{};
109 using namespace std;
110 return Norm(Max(q, zero3)) + min(max(q(0), max(q(1), q(2))), ScalarType(0));
111 }
112};
113
118template <common::CArithmetic TScalar>
119struct BoxFrame : public Primitive
120{
121 using ScalarType = TScalar;
123 TScalar(0.5),
124 TScalar(0.5),
125 TScalar(0.5)};
126 ScalarType t{TScalar(0.1)};
130 BoxFrame() = default;
136 explicit BoxFrame(Vec3<ScalarType> const& he_, ScalarType t_)
137 : he(he_),
138 t(t_)
139 {
140 }
141
147 PBAT_HOST_DEVICE ScalarType Eval(Vec3<ScalarType> p) const
148 {
149 p = Abs(p) - he;
150 Vec3<ScalarType> q = Abs(p + t) - t;
151 Zero3<ScalarType> constexpr zero3{};
152 ScalarType constexpr zero{0};
153 using namespace std;
154 // clang-format off
155 return min(
156 min(
157 Norm(Max(Vec3<ScalarType>{p(0),q(1),q(2)},zero3)) + min(max(p(0),max(q(1),q(2))), zero),
158 Norm(Max(Vec3<ScalarType>{q(0),p(1),q(2)},zero3)) + min(max(q(0),max(p(1),q(2))), zero)
159 ),
160 Norm(Max(Vec3<ScalarType>{q(0),q(1),p(2)},zero3)) + min(max(q(0),max(q(1),p(2))), zero)
161 );
162 // clang-format on
163 }
164};
165
170template <common::CArithmetic TScalar>
171struct Torus : public Primitive
172{
173 using ScalarType = TScalar;
174 Vec2<ScalarType> t{TScalar(1), TScalar(0.5)};
178 Torus() = default;
183 explicit Torus(Vec2<ScalarType> const& t_)
184 : t(t_)
185 {
186 }
187
193 PBAT_HOST_DEVICE ScalarType Eval(Vec3<ScalarType> const& p) const
194 {
195 Vec2<ScalarType> q = Vec2<ScalarType>{Norm(Vec2<ScalarType>{p(0), p(2)}) - t(0), p(1)};
196 return Norm(q) - t(1);
197 }
198};
199
205template <common::CArithmetic TScalar>
206struct CappedTorus : public Primitive
207{
208 using ScalarType = TScalar;
209 Vec2<ScalarType> sc{TScalar(0.833), TScalar(-0.545)};
215 CappedTorus() = default;
222 explicit CappedTorus(Vec2<ScalarType> const& sc_, ScalarType ra_, ScalarType rb_)
223 : sc(sc_),
224 ra(ra_),
225 rb(rb_)
226 {
227 }
228
234 PBAT_HOST_DEVICE ScalarType Eval(Vec3<ScalarType> p) const
235 {
236 using namespace std;
237 p(0) = abs(p(0));
238 bool const bk = sc(1) * p(0) > sc(0) * p(1);
239 auto pxy = p.template Slice<2, 1>(0, 0);
240 // NOTE: Not sure if better to do branchless but compute a norm (i.e. expensive sqrt), or
241 // use ternary operator and add divergent branching, but save the sqrt when possible.
242 ScalarType k = bk * (Dot(pxy, sc)) + (not bk) * Norm(pxy);
243 return sqrt(SquaredNorm(p) + ra * ra - ScalarType(2) * ra * k) - rb;
244 }
245};
246
252template <common::CArithmetic TScalar>
253struct Link : public Primitive
254{
255 using ScalarType = TScalar;
256 Vec2<ScalarType> t{TScalar(1), TScalar(0.5)};
257 ScalarType le{TScalar(1)};
261 Link() = default;
267 explicit Link(Vec2<ScalarType> const& t_, ScalarType le_)
268 : t(t_),
269 le(le_)
270 {
271 }
272
278 PBAT_HOST_DEVICE ScalarType Eval(Vec3<ScalarType> const& p) const
279 {
280 using namespace std;
281 ScalarType constexpr zero{0};
282 Vec3<ScalarType> q = Vec3<ScalarType>{p(0), max(abs(p(1)) - le, zero), p(2)};
283 auto qxy = q.template Slice<2, 1>(0, 0);
284 return Norm(Vec2<ScalarType>{Norm(qxy) - t(0), q(2)}) - t(1);
285 }
286};
287
292template <common::CArithmetic TScalar>
294{
295 using ScalarType = TScalar;
297 TScalar(0),
298 TScalar(0),
299 TScalar(0.5)};
300
303 InfiniteCylinder() = default;
309 : c(c_)
310 {
311 }
312
318 PBAT_HOST_DEVICE ScalarType Eval(Vec3<ScalarType> const& p) const
319 {
320 Vec2<ScalarType> d{p(0) - c(0), p(2) - c(1)};
321 return Norm(d) - c(2);
322 }
323};
324
329template <common::CArithmetic TScalar>
330struct Cone : public Primitive
331{
332 using ScalarType = TScalar;
333 Vec2<ScalarType> sc{TScalar(0.5), TScalar(0.5)};
334 ScalarType h{TScalar(1)};
338 Cone() = default;
344 explicit Cone(Vec2<ScalarType> const& sc_, ScalarType h_)
345 : sc(sc_),
346 h(h_)
347 {
348 }
349
355 PBAT_HOST_DEVICE ScalarType Eval(Vec3<ScalarType> const& p) const
356 {
357 using namespace std;
358 ScalarType constexpr zero{0};
359 ScalarType constexpr one{1};
361 Vec2<ScalarType> w{Norm(Vec2<ScalarType>{p(0), p(2)}), p(1)};
362 Vec2<ScalarType> a = w - q * clamp(Dot(w, q) / Dot(q, q), zero, one);
363 Vec2<ScalarType> b = w - q * Vec2<ScalarType>{clamp(w(0) / q(0), zero, one), one};
364 ScalarType k = sign(q(1));
365 ScalarType d = min(Dot(a, a), Dot(b, b));
366 ScalarType s = max(k * (w(0) * q(1) - w(1) * q(0)), k * (w(1) - q(1)));
367 return sqrt(d) * sign(s);
368 }
369};
370
375template <common::CArithmetic TScalar>
376struct InfiniteCone : public Primitive
377{
378 using ScalarType = TScalar;
379 Vec2<ScalarType> sc{TScalar(0.5), TScalar(0.5)};
383 InfiniteCone() = default;
388 explicit InfiniteCone(Vec2<ScalarType> const& sc_)
389 : sc(sc_)
390 {
391 }
392
398 PBAT_HOST_DEVICE ScalarType Eval(Vec3<ScalarType> const& p) const
399 {
400 using namespace std;
401 ScalarType constexpr zero{0};
402 ScalarType constexpr one{1};
403 Vec2<ScalarType> q = Vec2<ScalarType>{Norm(Vec2<ScalarType>{p(0), p(2)}), -p(1)};
404 ScalarType d = Norm(q - max(Dot(q, sc), zero) * sc);
405 bool bd = (q(0) * sc(1) - q(1) * sc(0) < zero);
406 return d * (bd * (-one) + (not bd) * one);
407 }
408};
409
414template <common::CArithmetic TScalar>
415struct Plane : public Primitive
416{
417 using ScalarType = TScalar;
423 PBAT_HOST_DEVICE ScalarType Eval(Vec3<ScalarType> const& p) const
424 {
425 // n^T (p - o)
426 return p(2);
427 }
428};
429
434template <common::CArithmetic TScalar>
436{
437 using ScalarType = TScalar;
439 TScalar(1),
440 TScalar(1)};
441
444 HexagonalPrism() = default;
450 : h(h_)
451 {
452 }
453
459 PBAT_HOST_DEVICE ScalarType Eval(Vec3<ScalarType> p) const
460 {
461 using namespace std;
462 Vec3<ScalarType> const k{ScalarType{-0.8660254}, ScalarType{0.5}, ScalarType{0.57735}};
463 p = Abs(p);
464 auto pxy = p.template Slice<2, 1>(0, 0);
465 auto kxy = k.template Slice<2, 1>(0, 0);
466 ScalarType constexpr zero{0};
467 pxy -= ScalarType(2) * min(Dot(kxy, pxy), zero) * kxy;
469 Norm(pxy - Vec2<ScalarType>{clamp(p(0), -k(2) * h(0), k(2) * h(0)), h(0)}) *
470 sign(p(1) - h(0)),
471 p(2) - h(1)};
472 return min(max(d(0), d(1)), zero) + Norm(Max(d, Zero2<ScalarType>{}));
473 }
474};
475
480template <common::CArithmetic TScalar>
481struct Capsule : public Primitive
482{
483 using ScalarType = TScalar;
484 Vec3<ScalarType> a{TScalar(-1), TScalar(0), TScalar(0)};
485 Vec3<ScalarType> b{TScalar(1), TScalar(0), TScalar(0)};
486 ScalarType r{TScalar(0.2)};
490 Capsule() = default;
497 explicit Capsule(Vec3<ScalarType> const& a_, Vec3<ScalarType> const& b_, ScalarType r_)
498 : a(a_),
499 b(b_),
500 r(r_)
501 {
502 }
503
509 PBAT_HOST_DEVICE ScalarType Eval(Vec3<ScalarType> const& p) const
510 {
511 using namespace std;
512 Vec3<ScalarType> pa = p - a;
513 Vec3<ScalarType> ba = b - a;
514 ScalarType h = clamp(Dot(pa, ba) / Dot(ba, ba), ScalarType(0), ScalarType(1));
515 return Norm(pa - h * ba) - r;
516 }
517};
518
523template <common::CArithmetic TScalar>
525{
526 using ScalarType = TScalar;
527 ScalarType h{TScalar(1)};
528 ScalarType r{TScalar(0.2)};
532 VerticalCapsule() = default;
539 : h(h_),
540 r(r_)
541 {
542 }
543
549 PBAT_HOST_DEVICE ScalarType Eval(Vec3<ScalarType> p) const
550 {
551 using namespace std;
552 p(1) -= clamp(p(1), ScalarType(0), h);
553 return Norm(p) - r;
554 }
555};
556
561template <common::CArithmetic TScalar>
563{
564 using ScalarType = TScalar;
565 Vec3<ScalarType> a{TScalar(-1), TScalar(0), TScalar(0)};
566 Vec3<ScalarType> b{TScalar(1), TScalar(0), TScalar(0)};
567 ScalarType r{TScalar(0.2)};
571 CappedCylinder() = default;
579 : a(a_),
580 b(b_),
581 r(r_)
582 {
583 }
584
590 PBAT_HOST_DEVICE ScalarType Eval(Vec3<ScalarType> const& p) const
591 {
592 using namespace std;
593 Vec3<ScalarType> ba = b - a;
594 Vec3<ScalarType> pa = p - a;
595 ScalarType baba = Dot(ba, ba);
596 ScalarType paba = Dot(pa, ba);
597 ScalarType x = Norm(baba * pa - paba * ba) - r * baba;
598 ScalarType y = abs(paba - baba * ScalarType(0.5)) - baba * ScalarType(0.5);
599 ScalarType x2 = x * x;
600 ScalarType y2 = y * y * baba;
601 ScalarType constexpr zero{0};
602 ScalarType d = (max(x, y) < zero) ?
603 -min(x2, y2) :
604 (((x > zero) ? x2 : zero) + ((y > zero) ? y2 : zero));
605 return sign(d) * sqrt(abs(d)) / baba;
606 }
607};
608
613template <common::CArithmetic TScalar>
615{
616 using ScalarType = TScalar;
617 ScalarType h{TScalar(1)};
618 ScalarType r{TScalar(0.2)};
629 : h(h_),
630 r(r_)
631 {
632 }
633
639 PBAT_HOST_DEVICE ScalarType Eval(Vec3<ScalarType> const& p) const
640 {
641 using namespace std;
642 Vec2<ScalarType> pxz{p(0), p(2)};
643 Vec2<ScalarType> d = Abs(Vec2<ScalarType>{Norm(pxz), p(1)}) - Vec2<ScalarType>{r, h};
644 ScalarType constexpr zero{0};
645 return min(max(d(0), d(1)), zero) + Norm(Max(d, Zero2<ScalarType>{}));
646 }
647};
648
654template <common::CArithmetic TScalar>
656{
657 using ScalarType = TScalar;
658 ScalarType h{TScalar(1)};
659 ScalarType ra{TScalar(0.2)};
660 ScalarType rb{TScalar(0.05)};
664 RoundedCylinder() = default;
672 : h(h_),
673 ra(ra_),
674 rb(rb_)
675 {
676 }
677
683 PBAT_HOST_DEVICE ScalarType Eval(Vec3<ScalarType> const& p) const
684 {
685 using namespace std;
686 Vec2<ScalarType> pxz{p(0), p(2)};
687 Vec2<ScalarType> d{Norm(pxz) - ScalarType(2) * ra + rb, abs(p(1)) - h};
688 ScalarType constexpr zero{0};
689 Zero2<ScalarType> constexpr zero2{};
690 return min(max(d(0), d(1)), zero) + Norm(Max(d, zero2)) - rb;
691 }
692};
693
698template <common::CArithmetic TScalar>
700{
701 using ScalarType = TScalar;
702 ScalarType h{TScalar(1)};
703 ScalarType r1{TScalar(0.2)};
704 ScalarType r2{TScalar(0.4)};
716 : h(h_),
717 r1(r1_),
718 r2(r2_)
719 {
720 }
721
727 PBAT_HOST_DEVICE ScalarType Eval(Vec3<ScalarType> const& p) const
728 {
729 using namespace std;
730 ScalarType constexpr zero{0};
731 ScalarType constexpr one{1};
732 Vec2<ScalarType> pxz{p(0), p(2)};
733 Vec2<ScalarType> q{Norm(pxz), p(1)};
734 Vec2<ScalarType> k1{r2, h};
735 Vec2<ScalarType> k2{r2 - r1, ScalarType(2) * h};
736 bool const brqy = (q(1) < zero);
737 ScalarType rqy = brqy * r1 + (not brqy) * r2;
738 Vec2<ScalarType> ca{q(0) - min(q(0), rqy), abs(q(1)) - h};
739 Vec2<ScalarType> cb = q - k1 + k2 * clamp(Dot(k1 - q, k2) / SquaredNorm(k2), zero, one);
740 bool const bs = (cb(0) < zero and ca(1) < zero);
741 ScalarType s = bs * (-one) + (not bs) * one;
742 return s * sqrt(min(SquaredNorm(ca), SquaredNorm(cb)));
743 }
744};
745
751template <common::CArithmetic TScalar>
753{
754 using ScalarType = TScalar;
755 ScalarType r{TScalar(1)};
756 ScalarType h{TScalar(0.5)};
757 ScalarType t{TScalar(0.1)};
761 CutHollowSphere() = default;
769 : r(r_),
770 h(h_),
771 t(t_)
772 {
773 }
774
780 PBAT_HOST_DEVICE ScalarType Eval(Vec3<ScalarType> const& p) const
781 {
782 using namespace std;
783 ScalarType w = sqrt(r * r - h * h);
784 Vec2<ScalarType> pxz{p(0), p(2)};
785 Vec2<ScalarType> q{Norm(pxz), p(1)};
786 bool b = (h * q(0) < w * q(1));
787 return b * (Norm(q - Vec2<ScalarType>{w, h})) + (not b) * (abs(Norm(q) - r) - t);
788 }
789};
790
796template <common::CArithmetic TScalar>
798{
799 using ScalarType = TScalar;
800 ScalarType h{TScalar(1)};
801 ScalarType r1{TScalar(0.2)};
802 ScalarType r2{TScalar(0.4)};
806 VerticalRoundCone() = default;
814 : h(h_),
815 r1(r1_),
816 r2(r2_)
817 {
818 }
819
825 PBAT_HOST_DEVICE ScalarType Eval(Vec3<ScalarType> const& p) const
826 {
827 using namespace std;
828 ScalarType b = (r1 - r2) / h;
829 ScalarType a = sqrt(ScalarType(1) - b * b);
830 Vec2<ScalarType> pxz{p(0), p(2)};
831 Vec2<ScalarType> q{Norm(pxz), p(1)};
832 ScalarType k = Dot(q, Vec2<ScalarType>{-b, a});
833 ScalarType constexpr zero{0};
834 if (k < zero)
835 return Norm(q) - r1;
836 if (k > a * h)
837 return Norm(q - Vec2<ScalarType>{zero, h}) - r2;
838 return Dot(q, Vec2<ScalarType>{a, b}) - r1;
839 }
840};
841
846template <common::CArithmetic TScalar>
847struct Octahedron : public Primitive
848{
849 using ScalarType = TScalar;
850 ScalarType s{TScalar(1)};
854 Octahedron() = default;
860 : s(s_)
861 {
862 }
863
869 PBAT_HOST_DEVICE ScalarType Eval(Vec3<ScalarType> p) const
870 {
871 using namespace std;
872 p = Abs(p);
873 ScalarType m = p(0) + p(1) + p(2) - s;
874 ScalarType constexpr three{3};
876 if (three * p(0) < m)
877 q = p;
878 else if (three * p(1) < m)
879 q = Vec3<ScalarType>{p(1), p(2), p(0)};
880 else if (three * p(2) < m)
881 q = Vec3<ScalarType>{p(2), p(0), p(1)};
882 else
883 return m * ScalarType(0.57735027);
884 ScalarType constexpr zero{0};
885 ScalarType k = clamp(ScalarType(0.5) * (q(2) - q(1) + s), zero, s);
886 return Norm(Vec3<ScalarType>{q(0), q(1) - s + k, q(2) - k});
887 }
888};
889
894template <common::CArithmetic TScalar>
895struct Pyramid : public Primitive
896{
897 using ScalarType = TScalar;
898 ScalarType h{TScalar(1)};
902 Pyramid() = default;
907 explicit Pyramid(ScalarType h_)
908 : h(h_)
909 {
910 }
911
917 PBAT_HOST_DEVICE ScalarType Eval(Vec3<ScalarType> p) const
918 {
919 using namespace std;
920 ScalarType constexpr quarter{0.25};
921 ScalarType constexpr half{0.5};
922 ScalarType constexpr zero{0};
923 ScalarType constexpr one{1};
924
925 ScalarType m2 = h * h + quarter;
926
927 ScalarType apx = abs(p(0));
928 ScalarType apz = abs(p(2));
929 bool bzx = (apz > apx);
930 p(0) = bzx * apz + (not bzx) * apx - half;
931 p(2) = bzx * apx + (not bzx) * apz - half;
932
933 Vec3<ScalarType> q{p(2), h * p(1) - half * p(0), h * p(0) + half * p(1)};
934
935 ScalarType s = max(-q(0), zero);
936 ScalarType t = clamp((q(1) - half * p(2)) / (m2 + quarter), zero, one);
937
938 ScalarType a = m2 * (q(0) + s) * (q(0) + s) + q(1) * q(1);
939 ScalarType b =
940 m2 * (q(0) + half * t) * (q(0) + half * t) + (q(1) - m2 * t) * (q(1) - m2 * t);
941
942 bool bd2 = min(q(1), -q(0) * m2 - q(1) * half) > zero;
943 ScalarType d2 = bd2 * zero + (not bd2) * min(a, b);
944
945 return sqrt((d2 + q(2) * q(2)) / m2) * sign(max(q(2), -p(1)));
946 }
947};
948
953template <common::CArithmetic TScalar>
954struct Triangle : public Primitive
955{
956 using ScalarType = TScalar;
957 Vec3<ScalarType> a{TScalar(0), TScalar(0), TScalar(0)};
958 Vec3<ScalarType> b{TScalar(1), TScalar(0), TScalar(0)};
959 Vec3<ScalarType> c{TScalar(0), TScalar(0), TScalar(1)};
963 Triangle() = default;
970 explicit Triangle(
971 Vec3<ScalarType> const& a_,
972 Vec3<ScalarType> const& b_,
973 Vec3<ScalarType> const& c_)
974 : a(a_),
975 b(b_),
976 c(c_)
977 {
978 }
979
985 PBAT_HOST_DEVICE ScalarType Eval(Vec3<ScalarType> const& p) const
986 {
987 using namespace std;
988 Vec3<ScalarType> ba = b - a;
989 Vec3<ScalarType> pa = p - a;
990 Vec3<ScalarType> cb = c - b;
991 Vec3<ScalarType> pb = p - b;
992 Vec3<ScalarType> ac = a - c;
993 Vec3<ScalarType> pc = p - c;
994 Vec3<ScalarType> nor = Cross(ba, ac);
995 ScalarType constexpr zero{0};
996 ScalarType constexpr one{1};
997 ScalarType constexpr two{2};
998 bool bs =
999 (sign(Dot(Cross(ba, nor), pa)) + sign(Dot(Cross(cb, nor), pb)) +
1000 sign(Dot(Cross(ac, nor), pc)) <
1001 two);
1002 return sqrt(
1003 bs ?
1004 min(
1005 min(
1006 SquaredNorm(ba * clamp(Dot(ba, pa) / SquaredNorm(ba), zero, one) - pa),
1007 SquaredNorm(cb * clamp(Dot(cb, pb) / SquaredNorm(cb), zero, one) - pb)),
1008 SquaredNorm(ac * clamp(Dot(ac, pc) / SquaredNorm(ac), zero, one) - pc)) :
1009 Dot(nor, pa) * Dot(nor, pa) / SquaredNorm(nor));
1010 }
1011};
1012
1017template <common::CArithmetic TScalar>
1019{
1020 using ScalarType = TScalar;
1021 Vec3<ScalarType> a{TScalar(0), TScalar(0), TScalar(0)};
1022 Vec3<ScalarType> b{TScalar(1), TScalar(0), TScalar(0)};
1023 Vec3<ScalarType> c{TScalar(1), TScalar(0), TScalar(1)};
1024 Vec3<ScalarType> d{TScalar(0), TScalar(0), TScalar(1)};
1028 Quadrilateral() = default;
1037 Vec3<ScalarType> const& a_,
1038 Vec3<ScalarType> const& b_,
1039 Vec3<ScalarType> const& c_,
1040 Vec3<ScalarType> const& d_)
1041 : a(a_),
1042 b(b_),
1043 c(c_),
1044 d(d_)
1045 {
1046 }
1047
1053 PBAT_HOST_DEVICE ScalarType Eval(Vec3<ScalarType> const& p) const
1054 {
1055 using namespace std;
1056 Vec3<ScalarType> ba = b - a;
1057 Vec3<ScalarType> pa = p - a;
1058 Vec3<ScalarType> cb = c - b;
1059 Vec3<ScalarType> pb = p - b;
1060 Vec3<ScalarType> dc = d - c;
1061 Vec3<ScalarType> pc = p - c;
1062 Vec3<ScalarType> ad = a - d;
1063 Vec3<ScalarType> pd = p - d;
1064 Vec3<ScalarType> nor = Cross(ba, ad);
1065 ScalarType constexpr zero{0};
1066 ScalarType constexpr one{1};
1067 ScalarType constexpr three{3};
1068 bool bs =
1069 (sign(Dot(Cross(ba, nor), pa)) + sign(Dot(Cross(cb, nor), pb)) +
1070 sign(Dot(Cross(dc, nor), pc)) + sign(Dot(Cross(ad, nor), pd)) <
1071 three);
1072 return sqrt(
1073 bs ?
1074 min(
1075 min(
1076 min(
1077 SquaredNorm(ba * clamp(Dot(ba, pa) / SquaredNorm(ba), zero, one) - pa),
1078 SquaredNorm(cb * clamp(Dot(cb, pb) / SquaredNorm(cb), zero, one) - pb)),
1079 SquaredNorm(dc * clamp(Dot(dc, pc) / SquaredNorm(dc), zero, one) - pc)),
1080 SquaredNorm(ad * clamp(Dot(ad, pd) / SquaredNorm(ad), zero, one) - pd)) :
1081 Dot(nor, pa) * Dot(nor, pa) / SquaredNorm(nor));
1082 }
1083};
1084} // namespace pbat::geometry::sdf
1085
1086#endif // PBAT_GEOMETRY_SDF_PRIMITIVE_H
This file includes all the mini linear algebra headers.
This file defines common type definitions used in the SDF module.
Concepts for common types.
Namespace for signed distance functions (SDFs) and related operations.
Definition BinaryNode.cpp:3
math::linalg::mini::SVector< TScalar, 2 > Vec2
2D vector type
Definition TypeDefs.h:20
math::linalg::mini::Zeros< TScalar, 2, 1 > Zero2
2D zero vector type
Definition TypeDefs.h:35
math::linalg::mini::SVector< TScalar, 3 > Vec3
3D vector type
Definition TypeDefs.h:23
TScalar sign(TScalar x)
Sign function, i.e. .
Definition Primitive.h:32
math::linalg::mini::Zeros< TScalar, 3, 1 > Zero3
3D zero vector type
Definition TypeDefs.h:38
PBAT_HOST_DEVICE ScalarType Eval(Vec3< ScalarType > p) const
Evaluate the signed distance function at a point.
Definition Primitive.h:147
TScalar ScalarType
Scalar type.
Definition Primitive.h:121
Vec3< ScalarType > he
Half extents of the box frame along each axis.
Definition Primitive.h:122
ScalarType t
Thickness of the box frame.
Definition Primitive.h:126
BoxFrame()=default
Default constructor.
BoxFrame(Vec3< ScalarType > const &he_, ScalarType t_)
Construct a new Box Frame object.
Definition Primitive.h:136
Box(Vec3< ScalarType > const &he_)
Construct a new Box object.
Definition Primitive.h:95
Vec3< ScalarType > he
Half extents of the box along each axis.
Definition Primitive.h:83
Box()=default
Default constructor.
TScalar ScalarType
Scalar type.
Definition Primitive.h:82
PBAT_HOST_DEVICE ScalarType Eval(Vec3< ScalarType > const &p) const
Evaluate the signed distance function at a point.
Definition Primitive.h:105
Vec3< ScalarType > a
Endpoint a of the capped cylinder.
Definition Primitive.h:565
CappedCylinder()=default
Default constructor.
TScalar ScalarType
Scalar type.
Definition Primitive.h:564
CappedCylinder(Vec3< ScalarType > const &a_, Vec3< ScalarType > const &b_, ScalarType r_)
Construct a new Capped Cylinder object.
Definition Primitive.h:578
PBAT_HOST_DEVICE ScalarType Eval(Vec3< ScalarType > const &p) const
Evaluate the signed distance function at a point.
Definition Primitive.h:590
ScalarType r
Radius of the capped cylinder.
Definition Primitive.h:567
Vec3< ScalarType > b
Endpoint b of the capped cylinder.
Definition Primitive.h:566
Vec2< ScalarType > sc
Sin/Cos.
Definition Primitive.h:209
TScalar ScalarType
Scalar type.
Definition Primitive.h:208
PBAT_HOST_DEVICE ScalarType Eval(Vec3< ScalarType > p) const
Evaluate the signed distance function at a point.
Definition Primitive.h:234
CappedTorus()=default
Default constructor.
CappedTorus(Vec2< ScalarType > const &sc_, ScalarType ra_, ScalarType rb_)
Construct a new Capped Torus object.
Definition Primitive.h:222
ScalarType rb
Radius 2.
Definition Primitive.h:211
ScalarType ra
Radius 1.
Definition Primitive.h:210
ScalarType r
Radius of the capsule.
Definition Primitive.h:486
Capsule(Vec3< ScalarType > const &a_, Vec3< ScalarType > const &b_, ScalarType r_)
Construct a new Capsule object.
Definition Primitive.h:497
PBAT_HOST_DEVICE ScalarType Eval(Vec3< ScalarType > const &p) const
Evaluate the signed distance function at a point.
Definition Primitive.h:509
Capsule()=default
Default constructor.
Vec3< ScalarType > a
Endpoint a of the capsule.
Definition Primitive.h:484
TScalar ScalarType
Scalar type.
Definition Primitive.h:483
Vec3< ScalarType > b
Endpoint b of the capsule.
Definition Primitive.h:485
PBAT_HOST_DEVICE ScalarType Eval(Vec3< ScalarType > const &p) const
Evaluate the signed distance function at a point.
Definition Primitive.h:355
Cone(Vec2< ScalarType > const &sc_, ScalarType h_)
Construct a new Cone object.
Definition Primitive.h:344
TScalar ScalarType
Scalar type.
Definition Primitive.h:332
Vec2< ScalarType > sc
sin/cos of the angle
Definition Primitive.h:333
Cone()=default
Default constructor.
ScalarType h
Height of the cone.
Definition Primitive.h:334
ScalarType r
Radius of the hollow sphere.
Definition Primitive.h:755
CutHollowSphere()=default
Default constructor.
ScalarType t
Thickness of the hollow sphere.
Definition Primitive.h:757
TScalar ScalarType
Scalar type.
Definition Primitive.h:754
PBAT_HOST_DEVICE ScalarType Eval(Vec3< ScalarType > const &p) const
Evaluate the signed distance function at a point.
Definition Primitive.h:780
CutHollowSphere(ScalarType r_, ScalarType h_, ScalarType t_)
Construct a new Cut Hollow Sphere object.
Definition Primitive.h:768
ScalarType h
Cut height.
Definition Primitive.h:756
HexagonalPrism()=default
Default constructor.
PBAT_HOST_DEVICE ScalarType Eval(Vec3< ScalarType > p) const
Evaluate the signed distance function at a point.
Definition Primitive.h:459
Vec2< ScalarType > h
h[0]: radius of the hexagon, h[1]: half height of the prism
Definition Primitive.h:438
TScalar ScalarType
Scalar type.
Definition Primitive.h:437
HexagonalPrism(Vec2< ScalarType > const &h_)
Construct a new Hexagonal Prism object.
Definition Primitive.h:449
Vec2< ScalarType > sc
sin/cos of the angle
Definition Primitive.h:379
TScalar ScalarType
Scalar type.
Definition Primitive.h:378
PBAT_HOST_DEVICE ScalarType Eval(Vec3< ScalarType > const &p) const
Evaluate the signed distance function at a point.
Definition Primitive.h:398
InfiniteCone(Vec2< ScalarType > const &sc_)
Construct a new Infinite Cone object.
Definition Primitive.h:388
InfiniteCone()=default
Default constructor.
Vec3< ScalarType > c
Center of the cylinder (on the axis) in c(0), c(1) and radius in c(2)
Definition Primitive.h:296
TScalar ScalarType
Scalar type.
Definition Primitive.h:295
InfiniteCylinder(Vec3< ScalarType > const &c_)
Construct a new Infinite Cylinder object.
Definition Primitive.h:308
InfiniteCylinder()=default
Default constructor.
PBAT_HOST_DEVICE ScalarType Eval(Vec3< ScalarType > const &p) const
Evaluate the signed distance function at a point.
Definition Primitive.h:318
PBAT_HOST_DEVICE ScalarType Eval(Vec3< ScalarType > p) const
Evaluate the signed distance function at a point.
Definition Primitive.h:869
Octahedron()=default
Default constructor.
TScalar ScalarType
Scalar type.
Definition Primitive.h:849
ScalarType s
Size of the octahedron.
Definition Primitive.h:850
Octahedron(ScalarType s_)
Construct a new Octahedron object.
Definition Primitive.h:859
Plane shape with normal and point on the plane .
Definition Primitive.h:416
TScalar ScalarType
Scalar type.
Definition Primitive.h:417
PBAT_HOST_DEVICE ScalarType Eval(Vec3< ScalarType > const &p) const
Evaluate the signed distance function at a point.
Definition Primitive.h:423
Base struct for all primitive shapes.
Definition Primitive.h:41
ScalarType h
Height of the pyramid.
Definition Primitive.h:898
PBAT_HOST_DEVICE ScalarType Eval(Vec3< ScalarType > p) const
Evaluate the signed distance function at a point.
Definition Primitive.h:917
TScalar ScalarType
Scalar type.
Definition Primitive.h:897
Pyramid(ScalarType h_)
Construct a new Pyramid object.
Definition Primitive.h:907
Pyramid()=default
Default constructor.
Quadrilateral()=default
Default constructor.
PBAT_HOST_DEVICE ScalarType Eval(Vec3< ScalarType > const &p) const
Evaluate the signed distance function at a point.
Definition Primitive.h:1053
TScalar ScalarType
Scalar type.
Definition Primitive.h:1020
Vec3< ScalarType > c
Vertex c of the quadrilateral.
Definition Primitive.h:1023
Vec3< ScalarType > b
Vertex b of the quadrilateral.
Definition Primitive.h:1022
Vec3< ScalarType > d
Vertex d of the quadrilateral.
Definition Primitive.h:1024
Vec3< ScalarType > a
Vertex a of the quadrilateral.
Definition Primitive.h:1021
Quadrilateral(Vec3< ScalarType > const &a_, Vec3< ScalarType > const &b_, Vec3< ScalarType > const &c_, Vec3< ScalarType > const &d_)
Construct a new Quadrilateral object.
Definition Primitive.h:1036
ScalarType h
Height of the rounded cylinder.
Definition Primitive.h:658
ScalarType ra
Radius of the rounded cylinder.
Definition Primitive.h:659
TScalar ScalarType
Scalar type.
Definition Primitive.h:657
ScalarType rb
Rounding radius at edges.
Definition Primitive.h:660
PBAT_HOST_DEVICE ScalarType Eval(Vec3< ScalarType > const &p) const
Evaluate the signed distance function at a point.
Definition Primitive.h:683
RoundedCylinder(ScalarType h_, ScalarType ra_, ScalarType rb_)
Construct a new Rounded Cylinder object.
Definition Primitive.h:671
RoundedCylinder()=default
Default constructor.
ScalarType R
Sphere radius.
Definition Primitive.h:52
PBAT_HOST_DEVICE ScalarType Eval(Vec3< ScalarType > const &p) const
Evaluate the signed distance function at a point.
Definition Primitive.h:71
TScalar ScalarType
Scalar type.
Definition Primitive.h:51
Sphere()=default
Default constructor.
Sphere(ScalarType R_)
Construct a new Sphere object.
Definition Primitive.h:61
Torus()=default
Default constructor.
Vec2< ScalarType > t
t[0]: minor radius, t[1]: major radius
Definition Primitive.h:174
TScalar ScalarType
Scalar type.
Definition Primitive.h:173
PBAT_HOST_DEVICE ScalarType Eval(Vec3< ScalarType > const &p) const
Evaluate the signed distance function at a point.
Definition Primitive.h:193
Torus(Vec2< ScalarType > const &t_)
Construct a new Torus object.
Definition Primitive.h:183
PBAT_HOST_DEVICE ScalarType Eval(Vec3< ScalarType > const &p) const
Evaluate the signed distance function at a point.
Definition Primitive.h:985
Triangle(Vec3< ScalarType > const &a_, Vec3< ScalarType > const &b_, Vec3< ScalarType > const &c_)
Construct a new Triangle object.
Definition Primitive.h:970
TScalar ScalarType
Scalar type.
Definition Primitive.h:956
Vec3< ScalarType > a
Vertex a of the triangle.
Definition Primitive.h:957
Triangle()=default
Default constructor.
Vec3< ScalarType > b
Vertex b of the triangle.
Definition Primitive.h:958
Vec3< ScalarType > c
Vertex c of the triangle.
Definition Primitive.h:959
VerticalCappedCone()=default
Default constructor.
TScalar ScalarType
Scalar type.
Definition Primitive.h:701
ScalarType r2
Major radius of the capped cone.
Definition Primitive.h:704
VerticalCappedCone(ScalarType h_, ScalarType r1_, ScalarType r2_)
Construct a new Vertical Capped Cone object.
Definition Primitive.h:715
ScalarType r1
Minor radius of the capped cone.
Definition Primitive.h:703
PBAT_HOST_DEVICE ScalarType Eval(Vec3< ScalarType > const &p) const
Evaluate the signed distance function at a point.
Definition Primitive.h:727
ScalarType h
Height of the capped cone.
Definition Primitive.h:702
ScalarType h
Height of the capped cylinder.
Definition Primitive.h:617
TScalar ScalarType
Scalar type.
Definition Primitive.h:616
VerticalCappedCylinder()=default
Default constructor.
PBAT_HOST_DEVICE ScalarType Eval(Vec3< ScalarType > const &p) const
Evaluate the signed distance function at a point.
Definition Primitive.h:639
VerticalCappedCylinder(ScalarType h_, ScalarType r_)
Construct a new Vertical Capped Cylinder object.
Definition Primitive.h:628
ScalarType r
Radius of the capped cylinder.
Definition Primitive.h:618
VerticalCapsule()=default
Default constructor.
PBAT_HOST_DEVICE ScalarType Eval(Vec3< ScalarType > p) const
Evaluate the signed distance function at a point.
Definition Primitive.h:549
ScalarType r
Radius of the capsule.
Definition Primitive.h:528
VerticalCapsule(ScalarType h_, ScalarType r_)
Construct a new Vertical Capsule object.
Definition Primitive.h:538
TScalar ScalarType
Scalar type.
Definition Primitive.h:526
ScalarType h
Height of the capsule.
Definition Primitive.h:527
VerticalRoundCone()=default
Default constructor.
PBAT_HOST_DEVICE ScalarType Eval(Vec3< ScalarType > const &p) const
Evaluate the signed distance function at a point.
Definition Primitive.h:825
VerticalRoundCone(ScalarType h_, ScalarType r1_, ScalarType r2_)
Construct a new Vertical Round Cone object.
Definition Primitive.h:813
ScalarType h
Height of the round cone.
Definition Primitive.h:800
TScalar ScalarType
Scalar type.
Definition Primitive.h:799
ScalarType r1
Radius at the bottom of the round cone.
Definition Primitive.h:801
ScalarType r2
Radius at the top of the round cone.
Definition Primitive.h:802