12#ifndef PBAT_GEOMETRY_SDF_PRIMITIVE_H
13#define PBAT_GEOMETRY_SDF_PRIMITIVE_H
16#include "pbat/HostDevice.h"
31template <common::CArithmetic TScalar>
34 return static_cast<TScalar
>(x > 0) -
static_cast<TScalar
>(x < 0);
48template <common::CArithmetic TScalar>
79template <common::CArithmetic TScalar>
110 return Norm(Max(q, zero3)) + min(max(q(0), max(q(1), q(2))),
ScalarType(0));
118template <common::CArithmetic TScalar>
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)
160 Norm(Max(
Vec3<ScalarType>{q(0),q(1),p(2)},zero3)) + min(max(q(0),max(q(1),p(2))), zero)
170template <common::CArithmetic TScalar>
196 return Norm(q) -
t(1);
205template <common::CArithmetic TScalar>
238 bool const bk =
sc(1) * p(0) >
sc(0) * p(1);
239 auto pxy = p.template Slice<2, 1>(0, 0);
242 ScalarType k = bk * (Dot(pxy,
sc)) + (not bk) * Norm(pxy);
252template <common::CArithmetic TScalar>
283 auto qxy = q.template Slice<2, 1>(0, 0);
292template <common::CArithmetic TScalar>
321 return Norm(d) -
c(2);
329template <common::CArithmetic TScalar>
366 ScalarType s = max(k * (w(0) * q(1) - w(1) * q(0)), k * (w(1) - q(1)));
367 return sqrt(d) *
sign(s);
375template <common::CArithmetic TScalar>
405 bool bd = (q(0) *
sc(1) - q(1) *
sc(0) < zero);
406 return d * (bd * (-one) + (not bd) * one);
414template <common::CArithmetic TScalar>
434template <common::CArithmetic TScalar>
464 auto pxy = p.template Slice<2, 1>(0, 0);
465 auto kxy = k.template Slice<2, 1>(0, 0);
467 pxy -=
ScalarType(2) * min(Dot(kxy, pxy), zero) * kxy;
480template <common::CArithmetic TScalar>
515 return Norm(pa - h * ba) -
r;
523template <common::CArithmetic TScalar>
561template <common::CArithmetic TScalar>
597 ScalarType x = Norm(baba * pa - paba * ba) -
r * baba;
604 (((x > zero) ? x2 : zero) + ((y > zero) ? y2 : zero));
605 return sign(d) * sqrt(abs(d)) / baba;
613template <common::CArithmetic TScalar>
654template <common::CArithmetic TScalar>
690 return min(max(d(0), d(1)), zero) + Norm(Max(d, zero2)) -
rb;
698template <common::CArithmetic TScalar>
736 bool const brqy = (q(1) < zero);
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);
742 return s * sqrt(min(SquaredNorm(ca), SquaredNorm(cb)));
751template <common::CArithmetic TScalar>
786 bool b = (
h * q(0) < w * q(1));
796template <common::CArithmetic TScalar>
846template <common::CArithmetic TScalar>
876 if (three * p(0) < m)
878 else if (three * p(1) < m)
880 else if (three * p(2) < m)
894template <common::CArithmetic TScalar>
929 bool bzx = (apz > apx);
930 p(0) = bzx * apz + (not bzx) * apx - half;
931 p(2) = bzx * apx + (not bzx) * apz - half;
936 ScalarType t = clamp((q(1) - half * p(2)) / (m2 + quarter), zero, one);
938 ScalarType a = m2 * (q(0) + s) * (q(0) + s) + q(1) * q(1);
940 m2 * (q(0) + half * t) * (q(0) + half * t) + (q(1) - m2 * t) * (q(1) - m2 * t);
942 bool bd2 = min(q(1), -q(0) * m2 - q(1) * half) > zero;
943 ScalarType d2 = bd2 * zero + (not bd2) * min(a, b);
945 return sqrt((d2 + q(2) * q(2)) / m2) *
sign(max(q(2), -p(1)));
953template <common::CArithmetic TScalar>
999 (
sign(Dot(Cross(ba, nor), pa)) +
sign(Dot(Cross(cb, nor), pb)) +
1000 sign(Dot(Cross(ac, nor), pc)) <
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));
1017template <common::CArithmetic TScalar>
1055 using namespace std;
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)) <
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));
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
TScalar ScalarType
Scalar type.
Definition Primitive.h:255
PBAT_HOST_DEVICE ScalarType Eval(Vec3< ScalarType > const &p) const
Evaluate the signed distance function at a point.
Definition Primitive.h:278
Vec2< ScalarType > t
t[0]: minor radius, t[1]: major radius
Definition Primitive.h:256
Link()=default
Default constructor.
ScalarType le
Elongation length.
Definition Primitive.h:257
Link(Vec2< ScalarType > const &t_, ScalarType le_)
Construct a new Link object.
Definition Primitive.h:267
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