15 #ifndef EBGeometry_AnalyticDistanceFunctions
16 #define EBGeometry_AnalyticDistanceFunctions
36 clamp(
const T& v,
const T& lo,
const T& hi)
38 return (v < lo) ? lo : (v > hi) ? hi : v;
58 constexpr
static std::array<int, 256> s_perlinPermutationTable = {
59 151, 160, 137, 91, 90, 15, 131, 13, 201, 95, 96, 53, 194, 233, 7, 225, 140, 36, 103, 30, 69, 142,
60 8, 99, 37, 240, 21, 10, 23, 190, 6, 148, 247, 120, 234, 75, 0, 26, 197, 62, 94, 252, 219, 203,
61 117, 35, 11, 32, 57, 177, 33, 88, 237, 149, 56, 87, 174, 20, 125, 136, 171, 168, 68, 175, 74, 165,
62 71, 134, 139, 48, 27, 166, 77, 146, 158, 231, 83, 111, 229, 122, 60, 211, 133, 230, 220, 105, 92, 41,
63 55, 46, 245, 40, 244, 102, 143, 54, 65, 25, 63, 161, 1, 216, 80, 73, 209, 76, 132, 187, 208, 89,
64 18, 169, 200, 196, 135, 130, 116, 188, 159, 86, 164, 100, 109, 198, 173, 186, 3, 64, 52, 217, 226, 250,
65 124, 123, 5, 202, 38, 147, 118, 126, 255, 82, 85, 212, 207, 206, 59, 227, 47, 16, 58, 17, 182, 189,
66 28, 42, 223, 183, 170, 213, 119, 248, 152, 2, 44, 154, 163, 70, 221, 153, 101, 155, 167, 43, 172, 9,
67 129, 22, 39, 253, 19, 98, 108, 110, 79, 113, 224, 232, 178, 185, 112, 104, 218, 246, 97, 228, 251, 34,
68 242, 193, 238, 210, 144, 12, 191, 179, 162, 241, 81, 51, 145, 235, 249, 14, 239, 107, 49, 192, 214, 31,
69 181, 199, 106, 157, 184, 84, 204, 176, 115, 121, 50, 45, 127, 4, 150, 254, 138, 236, 205, 93, 222, 114,
70 67, 29, 24, 72, 243, 141, 128, 195, 78, 66, 215, 61, 156, 180};
92 m_normal = a_normal / a_normal.length();
337 TorusSDF(
const Vec3T<T>& a_center,
const T& a_majorRadius,
const T& a_minorRadius) noexcept
417 const T rho = sqrt(p[0] * p[0] + p[1] * p[1]) -
m_majorRadius;
513 T d = std::numeric_limits<T>::infinity();
522 const T h = std::abs(para) - 0.5 *
m_length;
525 constexpr T zero = T(0.0);
527 if (w <= zero && h <= zero) {
528 d = (std::abs(w) < std::abs(h)) ? w : h;
530 else if (w <= zero && h > zero) {
533 else if (w > zero && h < zero) {
538 d = sqrt(w * w + h * h);
655 const Vec3T<T> axis = (a_tip2 - a_tip1) /
length(a_tip2 - a_tip1);
671 const T h =
clamp(
dot(v1, v2) /
dot(v2, v2), T(0.0), T(1.0));
713 constexpr T pi = 3.14159265358979323846;
716 m_c.x = std::sin(0.5 * a_angle * pi / 180.0);
717 m_c.y = std::cos(0.5 * a_angle * pi / 180.0);
734 const Vec2T<T> q(sqrt(delta[0] * delta[0] + delta[1] * delta[1]), -delta[2]);
737 const T d2 = d1 * ((q.
x *
m_c.y - q.
y *
m_c.x < 0.0) ? -1.0 : 1.0);
774 constexpr T pi = 3.14159265358979323846;
778 m_c.x = std::sin(0.5 * a_angle * pi / 180.0);
779 m_c.y = std::cos(0.5 * a_angle * pi / 180.0);
796 const T dr = sqrt(delta[0] * delta[0] + delta[1] * delta[1]);
797 const T dz = delta[2];
799 constexpr T zero = T(0.0);
800 constexpr T one = T(1.0);
807 auto sign = [](
const T& x) {
return (x > zero) - (x < zero); };
809 const T k = sign(q.
y);
811 const T s =
std::max(k * (w.
x * q.
y - w.
y * q.
x), k * (w.
y - q.
y));
813 return sqrt(d) * sign(s);
902 const T a_noisePersistence,
903 const unsigned int a_noiseOctaves) noexcept
911 for (
int i = 0; i < 256; i++) {
917 std::random_device rd;
942 for (
unsigned int curOctave = 0; curOctave <
m_noiseOctaves; curOctave++) {
943 ret += 0.5 * curAmp * (1 + this->
noise(a_point * curFreq));
960 template <
class URNG>
965 for (
unsigned int i = 0; i < 256; i++) {
971 for (
int i = 0; i < 256; i++) {
980 std::array<int, 512>&
1019 lerp(
const T t,
const T a,
const T b)
const noexcept
1021 return a + t * (b - a);
1031 return t * t * t * (t * (t * 6 - 15) + 10);
1042 grad(
const int hash,
const T x,
const T y,
const T z)
const noexcept
1044 const int h = hash & 15;
1045 const T u = h < 8 ? x : y;
1046 const T v = h < 4 ? y : h == 12 || h == 14 ? x : y;
1047 return ((h & 1) == 0 ? u : -u) + ((h & 2) == 0 ? v : -v);
1058 const int X =
static_cast<int>(std::floor(a_point[0])) & 255;
1059 const int Y =
static_cast<int>(std::floor(a_point[1])) & 255;
1060 const int Z =
static_cast<int>(std::floor(a_point[2])) & 255;
1063 const double x = a_point[0] - std::floor(a_point[0]);
1064 const double y = a_point[1] - std::floor(a_point[1]);
1065 const double z = a_point[2] - std::floor(a_point[2]);
1068 const double u =
fade(x);
1069 const double v =
fade(y);
1070 const double w =
fade(z);
constexpr const T clamp(const T &v, const T &lo, const T &hi)
Clamp function. Returns lo if v < lo and hi if v > hi. Otherwise returns v.
Definition: EBGeometry_AnalyticDistanceFunctions.hpp:36
Declaration of a various bounding volumes used for bounding volume hierarchy.
Abstract base class for representing a signed distance function.
Vec2T< T > max(const Vec2T< T > &u, const Vec2T< T > &v) noexcept
Maximum function. Returns new vector with component-wise minimums.
T length(const Vec2T< T > &v) noexcept
Length function.
T dot(const Vec2T< T > &u, const Vec2T< T > &v) noexcept
Dot product function.
Vec2T< T > min(const Vec2T< T > &u, const Vec2T< T > &v) noexcept
Minimum function. Returns new vector with component-wise minimums.
Signed distance field for an axis-aligned box.
Definition: EBGeometry_AnalyticDistanceFunctions.hpp:217
const Vec3T< T > & getHighCorner() const noexcept
Get upper-right corner.
Definition: EBGeometry_AnalyticDistanceFunctions.hpp:266
const Vec3T< T > & getLowCorner() const noexcept
Get lower-left corner.
Definition: EBGeometry_AnalyticDistanceFunctions.hpp:246
BoxSDF()=delete
Disallowed default constructor.
Vec3T< T > & getHighCorner() noexcept
Get upper-right corner.
Definition: EBGeometry_AnalyticDistanceFunctions.hpp:276
virtual T signedDistance(const Vec3T< T > &a_point) const noexcept override
Signed distance function for sphere.
Definition: EBGeometry_AnalyticDistanceFunctions.hpp:286
Vec3T< T > m_loCorner
Low box corner.
Definition: EBGeometry_AnalyticDistanceFunctions.hpp:310
Vec3T< T > m_hiCorner
High box corner.
Definition: EBGeometry_AnalyticDistanceFunctions.hpp:315
Vec3T< T > & getLowCorner() noexcept
Get lower-left corner.
Definition: EBGeometry_AnalyticDistanceFunctions.hpp:256
BoxSDF(const Vec3T< T > &a_loCorner, const Vec3T< T > &a_hiCorner) noexcept
Full constructor. Sets the low and high corner.
Definition: EBGeometry_AnalyticDistanceFunctions.hpp:229
virtual ~BoxSDF() noexcept
Destructor (does nothing).
Definition: EBGeometry_AnalyticDistanceFunctions.hpp:238
Capsulate/pill SDF. Basically a cylinder with spherical endcaps, oriented along the specified axis.
Definition: EBGeometry_AnalyticDistanceFunctions.hpp:640
virtual T signedDistance(const Vec3T< T > &a_point) const noexcept override
Implementation of the signed distance function.
Definition: EBGeometry_AnalyticDistanceFunctions.hpp:666
Vec3T< T > m_center1
Capsule center1.
Definition: EBGeometry_AnalyticDistanceFunctions.hpp:681
CapsuleSDF(const Vec3T< T > &a_tip1, const Vec3T< T > a_tip2, const T &a_radius) noexcept
Full constructor.
Definition: EBGeometry_AnalyticDistanceFunctions.hpp:653
T m_radius
Capsule radius.
Definition: EBGeometry_AnalyticDistanceFunctions.hpp:691
Vec3T< T > m_center2
Capsule center2.
Definition: EBGeometry_AnalyticDistanceFunctions.hpp:686
CapsuleSDF()=delete
No weak construction.
Signed distance field for an finite cone. Oriented along +z.
Definition: EBGeometry_AnalyticDistanceFunctions.hpp:759
T m_height
Cone height.
Definition: EBGeometry_AnalyticDistanceFunctions.hpp:830
virtual ~ConeSDF()
Destructor – does nothing.
Definition: EBGeometry_AnalyticDistanceFunctions.hpp:785
virtual T signedDistance(const Vec3T< T > &a_point) const noexcept override
Implementation of the signed distance function.
Definition: EBGeometry_AnalyticDistanceFunctions.hpp:793
Vec3T< T > m_tip
Tip position.
Definition: EBGeometry_AnalyticDistanceFunctions.hpp:820
Vec2T< T > m_c
Sine/cosine of angle.
Definition: EBGeometry_AnalyticDistanceFunctions.hpp:825
ConeSDF(const Vec3T< T > &a_tip, const T &a_height, const T &a_angle) noexcept
Finite cone function.
Definition: EBGeometry_AnalyticDistanceFunctions.hpp:772
ConeSDF()=delete
Disallowed weak construction.
Signed distance field for a cylinder.
Definition: EBGeometry_AnalyticDistanceFunctions.hpp:445
Vec3T< T > m_center2
The other endpoint.
Definition: EBGeometry_AnalyticDistanceFunctions.hpp:554
Vec3T< T > m_axis
"Axis", pointing from m_center1 to m_center2.
Definition: EBGeometry_AnalyticDistanceFunctions.hpp:564
virtual T signedDistance(const Vec3T< T > &a_point) const noexcept override
Signed distance function for a torus.
Definition: EBGeometry_AnalyticDistanceFunctions.hpp:511
Vec3T< T > m_center
Halfway between center1 and m_center2.
Definition: EBGeometry_AnalyticDistanceFunctions.hpp:559
CylinderSDF()=delete
Disallowed weak construction. Use one of the full constructors.
const T & getRadius() const noexcept
Get radius.
Definition: EBGeometry_AnalyticDistanceFunctions.hpp:501
CylinderSDF(const Vec3T< T > &a_center1, const Vec3T< T > &a_center2, const T &a_radius) noexcept
Full constructor.
Definition: EBGeometry_AnalyticDistanceFunctions.hpp:458
T m_length
Cylinder length.
Definition: EBGeometry_AnalyticDistanceFunctions.hpp:569
const Vec3T< T > & getCenter1() const noexcept
Get one endpoint.
Definition: EBGeometry_AnalyticDistanceFunctions.hpp:481
const Vec3T< T > & getCenter2() const noexcept
Get the other endpoint.
Definition: EBGeometry_AnalyticDistanceFunctions.hpp:491
virtual ~CylinderSDF() noexcept
Destructor (does nothing).
Definition: EBGeometry_AnalyticDistanceFunctions.hpp:473
Vec3T< T > m_center1
One endpoint.
Definition: EBGeometry_AnalyticDistanceFunctions.hpp:549
T m_radius
radius.
Definition: EBGeometry_AnalyticDistanceFunctions.hpp:574
Signed distance field for an infinite cone. Oriented along +z.
Definition: EBGeometry_AnalyticDistanceFunctions.hpp:699
Vec2T< T > m_c
Sine/cosine of angle.
Definition: EBGeometry_AnalyticDistanceFunctions.hpp:751
InfiniteConeSDF()=delete
Disallowed weak construction.
InfiniteConeSDF(const Vec3T< T > &a_tip, const T &a_angle) noexcept
Infinite cone function.
Definition: EBGeometry_AnalyticDistanceFunctions.hpp:711
virtual T signedDistance(const Vec3T< T > &a_point) const noexcept override
Implementation of the signed distance function.
Definition: EBGeometry_AnalyticDistanceFunctions.hpp:731
virtual ~InfiniteConeSDF()
Destructor – does nothing.
Definition: EBGeometry_AnalyticDistanceFunctions.hpp:723
Vec3T< T > m_tip
Tip position.
Definition: EBGeometry_AnalyticDistanceFunctions.hpp:746
Inifinitely long cylinder class. Oriented along the y-axis.
Definition: EBGeometry_AnalyticDistanceFunctions.hpp:582
Vec3T< T > m_center
Cylinder center.
Definition: EBGeometry_AnalyticDistanceFunctions.hpp:621
virtual T signedDistance(const Vec3T< T > &a_point) const noexcept override
Signed distance function.
Definition: EBGeometry_AnalyticDistanceFunctions.hpp:607
InfiniteCylinderSDF(const Vec3T< T > &a_center, const T &a_radius, const size_t a_axis) noexcept
Full constructor.
Definition: EBGeometry_AnalyticDistanceFunctions.hpp:595
T m_radius
Cylinder radius.
Definition: EBGeometry_AnalyticDistanceFunctions.hpp:626
InfiniteCylinderSDF()=delete
No weak construction.
size_t m_axis
Axis.
Definition: EBGeometry_AnalyticDistanceFunctions.hpp:631
Ken Perlins gradient noise function.
Definition: EBGeometry_AnalyticDistanceFunctions.hpp:891
void shuffle(URNG &g) noexcept
Shuffle the permutation with the input RNG.
Definition: EBGeometry_AnalyticDistanceFunctions.hpp:962
unsigned int m_noiseOctaves
Number of noise octaves.
Definition: EBGeometry_AnalyticDistanceFunctions.hpp:1010
T m_noisePersistence
Noise persistence.
Definition: EBGeometry_AnalyticDistanceFunctions.hpp:1000
virtual T fade(const T t) const noexcept
Ken Perlin's fade function.
Definition: EBGeometry_AnalyticDistanceFunctions.hpp:1029
virtual ~PerlinSDF() noexcept
Destructor (does nothing)
Definition: EBGeometry_AnalyticDistanceFunctions.hpp:927
PerlinSDF(const T a_noiseAmplitude, const Vec3T< T > a_noiseFrequency, const T a_noisePersistence, const unsigned int a_noiseOctaves) noexcept
Full constructor.
Definition: EBGeometry_AnalyticDistanceFunctions.hpp:900
std::array< int, 512 > & getPermutationTable() noexcept
Get the internal permutation table.
Definition: EBGeometry_AnalyticDistanceFunctions.hpp:981
T noise(const Vec3T< T > &a_point) const noexcept
Octave noise function.
Definition: EBGeometry_AnalyticDistanceFunctions.hpp:1055
T grad(const int hash, const T x, const T y, const T z) const noexcept
Ken Perlins grad function.
Definition: EBGeometry_AnalyticDistanceFunctions.hpp:1042
std::array< T, 512 > m_permutationTable
Permutation table.
Definition: EBGeometry_AnalyticDistanceFunctions.hpp:1005
virtual T lerp(const T t, const T a, const T b) const noexcept
Ken Perlin's lerp function.
Definition: EBGeometry_AnalyticDistanceFunctions.hpp:1019
Vec3T< T > m_noiseFrequency
Noise frequency.
Definition: EBGeometry_AnalyticDistanceFunctions.hpp:990
T m_noiseAmplitude
Noise amplitude.
Definition: EBGeometry_AnalyticDistanceFunctions.hpp:995
virtual T signedDistance(const Vec3T< T > &a_point) const noexcept override
Signed distance function. Generates a distance on [0,curAmplitude].
Definition: EBGeometry_AnalyticDistanceFunctions.hpp:935
Signed distance function for a plane.
Definition: EBGeometry_AnalyticDistanceFunctions.hpp:77
Vec3T< T > m_normal
Plane normal vector.
Definition: EBGeometry_AnalyticDistanceFunctions.hpp:114
PlaneSDF(const Vec3T< T > &a_point, const Vec3T< T > &a_normal) noexcept
Full constructor.
Definition: EBGeometry_AnalyticDistanceFunctions.hpp:89
Vec3T< T > m_point
Point on plane.
Definition: EBGeometry_AnalyticDistanceFunctions.hpp:109
virtual T signedDistance(const Vec3T< T > &a_point) const noexcept override
Signed distance function for sphere.
Definition: EBGeometry_AnalyticDistanceFunctions.hpp:100
PlaneSDF()=delete
Disallowed weak construction.
Box of arbitrary dimensions centered at the origin, with rounded corners.
Definition: EBGeometry_AnalyticDistanceFunctions.hpp:838
std::shared_ptr< SphereSDF< T > > m_sphere
Sphere at origin with radius a_curvature.
Definition: EBGeometry_AnalyticDistanceFunctions.hpp:878
RoundedBoxSDF(const Vec3T< T > &a_dimensions, const T a_curvature) noexcept
Full constructor. User inputs dimensions and corner curvature.
Definition: EBGeometry_AnalyticDistanceFunctions.hpp:852
RoundedBoxSDF()=delete
Disallowed default constructor.
virtual ~RoundedBoxSDF() noexcept
Destructor (does nothing).
Definition: EBGeometry_AnalyticDistanceFunctions.hpp:862
virtual T signedDistance(const Vec3T< T > &a_point) const noexcept override
Signed distance function for the rounded box.
Definition: EBGeometry_AnalyticDistanceFunctions.hpp:869
Vec3T< T > m_dimensions
Box dimensions.
Definition: EBGeometry_AnalyticDistanceFunctions.hpp:883
Abstract representation of a signed distance function.
Definition: EBGeometry_SignedDistanceFunction.hpp:32
Signed distance field for sphere.
Definition: EBGeometry_AnalyticDistanceFunctions.hpp:122
T & getRadius() noexcept
Get radius.
Definition: EBGeometry_AnalyticDistanceFunctions.hpp:185
virtual ~SphereSDF() noexcept=default
Destructor.
const T & getRadius() const noexcept
Get radius.
Definition: EBGeometry_AnalyticDistanceFunctions.hpp:176
Vec3T< T > & getCenter() noexcept
Get center.
Definition: EBGeometry_AnalyticDistanceFunctions.hpp:167
SphereSDF(const Vec3T< T > &a_center, const T &a_radius) noexcept
Default constructor.
Definition: EBGeometry_AnalyticDistanceFunctions.hpp:134
const Vec3T< T > & getCenter() const noexcept
Get center.
Definition: EBGeometry_AnalyticDistanceFunctions.hpp:158
virtual T signedDistance(const Vec3T< T > &a_point) const noexcept override
Signed distance function for sphere.
Definition: EBGeometry_AnalyticDistanceFunctions.hpp:195
SphereSDF(const SphereSDF &a_other) noexcept
Copy constructor.
Definition: EBGeometry_AnalyticDistanceFunctions.hpp:143
Vec3T< T > m_center
Sphere center.
Definition: EBGeometry_AnalyticDistanceFunctions.hpp:204
T m_radius
Sphere radius.
Definition: EBGeometry_AnalyticDistanceFunctions.hpp:209
SphereSDF()=delete
Disallowed weak construction.
Signed distance field for a torus.
Definition: EBGeometry_AnalyticDistanceFunctions.hpp:324
const Vec3T< T > & getCenter() const noexcept
Get torus center.
Definition: EBGeometry_AnalyticDistanceFunctions.hpp:355
T & getMinorRadius() noexcept
Get minor radius.
Definition: EBGeometry_AnalyticDistanceFunctions.hpp:405
T m_majorRadius
Major torus radius.
Definition: EBGeometry_AnalyticDistanceFunctions.hpp:432
virtual T signedDistance(const Vec3T< T > &a_point) const noexcept override
Signed distance function for a torus.
Definition: EBGeometry_AnalyticDistanceFunctions.hpp:414
const T & getMinorRadius() const noexcept
Get minor radius.
Definition: EBGeometry_AnalyticDistanceFunctions.hpp:395
TorusSDF()=delete
Disallowed weak construction.
T & getMajorRadius() noexcept
Get major radius.
Definition: EBGeometry_AnalyticDistanceFunctions.hpp:385
TorusSDF(const Vec3T< T > &a_center, const T &a_majorRadius, const T &a_minorRadius) noexcept
Full constructor.
Definition: EBGeometry_AnalyticDistanceFunctions.hpp:337
Vec3T< T > m_center
Torus center.
Definition: EBGeometry_AnalyticDistanceFunctions.hpp:427
virtual ~TorusSDF() noexcept
Destructor (does nothing).
Definition: EBGeometry_AnalyticDistanceFunctions.hpp:347
const T & getMajorRadius() const noexcept
Get major radius.
Definition: EBGeometry_AnalyticDistanceFunctions.hpp:375
Vec3T< T > & getCenter() noexcept
Get torus center.
Definition: EBGeometry_AnalyticDistanceFunctions.hpp:365
T m_minorRadius
Minor torus radius.
Definition: EBGeometry_AnalyticDistanceFunctions.hpp:437
Two-dimensional vector class with arithmetic operators.
Definition: EBGeometry_Vec.hpp:31
T x
First component in the vector.
Definition: EBGeometry_Vec.hpp:61
T y
Second component in the vector.
Definition: EBGeometry_Vec.hpp:66
Three-dimensional vector class with arithmetic operators.
Definition: EBGeometry_Vec.hpp:218
static constexpr Vec3T< T > zero() noexcept
Return av vector with x = y = z = 0.
constexpr T length() const noexcept
Compute vector length.
size_t maxDir(const bool a_doAbs) const noexcept
Return the direction which has the largest component (can be absolute)