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
BinaryNode.h
Go to the documentation of this file.
1
11#ifndef PBAT_GEOMETRY_SDF_BINARYNODE_H
12#define PBAT_GEOMETRY_SDF_BINARYNODE_H
13
14#include "pbat/HostDevice.h"
16
17#include <algorithm>
18#include <cmath>
19
20namespace pbat::geometry::sdf {
21
26{
27};
28
33template <common::CArithmetic TScalar>
34struct Union : public BinaryNode
35{
36 using ScalarType = TScalar;
45 PBAT_HOST_DEVICE ScalarType Eval(ScalarType sd1, ScalarType sd2) const
46 {
47 using namespace std;
48 return min(sd1, sd2);
49 }
50};
51
56template <common::CArithmetic TScalar>
57struct Difference : public BinaryNode
58{
59 using ScalarType = TScalar;
66 PBAT_HOST_DEVICE ScalarType Eval(ScalarType sd1, ScalarType sd2) const
67 {
68 using namespace std;
69 return max(-sd2, sd1);
70 }
71};
72
77template <common::CArithmetic TScalar>
78struct Intersection : public BinaryNode
79{
80 using ScalarType = TScalar;
87 PBAT_HOST_DEVICE ScalarType Eval(ScalarType sd1, ScalarType sd2) const
88 {
89 using namespace std;
90 return max(sd1, sd2);
91 }
92};
93
98template <common::CArithmetic TScalar>
99struct ExclusiveOr : public BinaryNode
100{
101 using ScalarType = TScalar;
108 PBAT_HOST_DEVICE ScalarType Eval(ScalarType sd1, ScalarType sd2) const
109 {
110 using namespace std;
111 return max(min(sd1, sd2), -max(sd1, sd2));
112 }
113};
114
119template <common::CArithmetic TScalar>
120struct SmoothUnion : public BinaryNode
121{
122 using ScalarType = TScalar;
123 ScalarType k{TScalar(0.25)};
127 SmoothUnion() = default;
132 explicit SmoothUnion(ScalarType k_) : k(k_) {}
139 PBAT_HOST_DEVICE ScalarType Eval(ScalarType sd1, ScalarType sd2) const
140 {
141 using namespace std;
142 ScalarType kk = 4 * k;
143 ScalarType h = max(kk - abs(sd1 - sd2), ScalarType(0));
144 return min(sd1, sd2) - h * h * ScalarType(0.25) / kk;
145 }
146};
147
152template <common::CArithmetic TScalar>
154{
155 using ScalarType = TScalar;
156 ScalarType k{TScalar(0.25)};
160 SmoothDifference() = default;
165 explicit SmoothDifference(ScalarType k_) : k(k_) {}
172 PBAT_HOST_DEVICE ScalarType Eval(ScalarType sd1, ScalarType sd2) const
173 {
175 U.k = k;
176 return -U.Eval(sd2, -sd1);
177 }
178};
179
184template <common::CArithmetic TScalar>
186{
187 using ScalarType = TScalar;
188 ScalarType k{TScalar(0.25)};
197 explicit SmoothIntersection(ScalarType k_) : k(k_) {}
204 PBAT_HOST_DEVICE ScalarType Eval(ScalarType sd1, ScalarType sd2) const
205 {
207 U.k = k;
208 return -U.Eval(-sd1, -sd2);
209 }
210};
211
212} // namespace pbat::geometry::sdf
213
214#endif // PBAT_GEOMETRY_SDF_BINARYNODE_H
Concepts for common types.
Namespace for signed distance functions (SDFs) and related operations.
Definition BinaryNode.cpp:3
Base struct for all binary nodes.
Definition BinaryNode.h:26
Difference operation.
Definition BinaryNode.h:58
TScalar ScalarType
Scalar type.
Definition BinaryNode.h:59
PBAT_HOST_DEVICE ScalarType Eval(ScalarType sd1, ScalarType sd2) const
Evaluate the signed distance function of the difference of two shapes.
Definition BinaryNode.h:66
Exclusive or operation.
Definition BinaryNode.h:100
PBAT_HOST_DEVICE ScalarType Eval(ScalarType sd1, ScalarType sd2) const
Evaluate the signed distance function of the exclusive or of two shapes.
Definition BinaryNode.h:108
TScalar ScalarType
Scalar type.
Definition BinaryNode.h:101
Intersection operation.
Definition BinaryNode.h:79
TScalar ScalarType
Scalar type.
Definition BinaryNode.h:80
PBAT_HOST_DEVICE ScalarType Eval(ScalarType sd1, ScalarType sd2) const
Evaluate the signed distance function of the intersection of two shapes.
Definition BinaryNode.h:87
SmoothDifference(ScalarType k_)
Construct a new Smooth Difference object.
Definition BinaryNode.h:165
TScalar ScalarType
Scalar type.
Definition BinaryNode.h:155
SmoothDifference()=default
Default constructor.
PBAT_HOST_DEVICE ScalarType Eval(ScalarType sd1, ScalarType sd2) const
Evaluate the signed distance function of the smooth difference of two shapes.
Definition BinaryNode.h:172
ScalarType k
Smoothness factor.
Definition BinaryNode.h:156
PBAT_HOST_DEVICE ScalarType Eval(ScalarType sd1, ScalarType sd2) const
Evaluate the signed distance function of the smooth intersection of two shapes.
Definition BinaryNode.h:204
SmoothIntersection(ScalarType k_)
Construct a new Smooth Intersection object.
Definition BinaryNode.h:197
ScalarType k
Smoothness factor.
Definition BinaryNode.h:188
SmoothIntersection()=default
Default constructor.
TScalar ScalarType
Scalar type.
Definition BinaryNode.h:187
Smooth union operation.
Definition BinaryNode.h:121
SmoothUnion(ScalarType k_)
Construct a new Smooth Union object.
Definition BinaryNode.h:132
SmoothUnion()=default
Default constructor.
PBAT_HOST_DEVICE ScalarType Eval(ScalarType sd1, ScalarType sd2) const
Evaluate the signed distance function of the smooth union of two shapes.
Definition BinaryNode.h:139
TScalar ScalarType
Scalar type.
Definition BinaryNode.h:122
ScalarType k
Smoothness factor.
Definition BinaryNode.h:123
Union operation.
Definition BinaryNode.h:35
TScalar ScalarType
Scalar type.
Definition BinaryNode.h:36
PBAT_HOST_DEVICE ScalarType Eval(ScalarType sd1, ScalarType sd2) const
Evaluate the signed distance function of the union of two shapes.
Definition BinaryNode.h:45