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
UnaryNode.h
Go to the documentation of this file.
1
11
12#ifndef PBAT_GEOMETRY_SDF_UNARYNODE_H
13#define PBAT_GEOMETRY_SDF_UNARYNODE_H
14
15#include "TypeDefs.h"
16#include "pbat/HostDevice.h"
18#include "pbat/math/linalg/mini/BinaryOperations.h"
19#include "pbat/math/linalg/mini/UnaryOperations.h"
20
21#include <algorithm>
22#include <cmath>
23#include <numbers>
24
25namespace pbat::geometry::sdf {
26
31{
32};
33
38template <common::CArithmetic TScalar>
39struct Scale : public UnaryNode
40{
41 using ScalarType = TScalar;
42 ScalarType s{TScalar(1)};
46 Scale() = default;
51 explicit Scale(ScalarType s_) : s(s_) {}
59 template <class FSdf>
60 PBAT_HOST_DEVICE ScalarType Eval(Vec3<ScalarType> const& p, FSdf&& sdf) const
61 {
62 return s * sdf(Vec3<ScalarType>(p / s));
63 }
64};
65
70template <common::CArithmetic TScalar>
71struct Elongate : public UnaryNode
72{
73 using ScalarType = TScalar;
74 Vec3<ScalarType> h{TScalar(1), TScalar(1), TScalar(1)};
78 Elongate() = default;
83 explicit Elongate(Vec3<ScalarType> const& h_) : h(h_) {}
91 template <class FSdf>
92 PBAT_HOST_DEVICE ScalarType Eval(Vec3<ScalarType> const& p, FSdf&& sdf) const
93 {
94 Vec3<ScalarType> q = Abs(p) - h;
96 using namespace std;
97 return sdf(pp) + min(max(q(0), max(q(1), q(2))), ScalarType(0));
98 }
99};
100
105template <common::CArithmetic TScalar>
106struct Round : public UnaryNode
107{
108 using ScalarType = TScalar;
109 ScalarType r{TScalar(0.01)};
113 Round() = default;
118 explicit Round(ScalarType r_) : r(r_) {}
126 template <class FSdf>
127 PBAT_HOST_DEVICE ScalarType Eval(Vec3<ScalarType> const& p, FSdf&& sdf) const
128 {
129 return sdf(p) - r;
130 }
131};
132
137template <common::CArithmetic TScalar>
138struct Onion : public UnaryNode
139{
140 using ScalarType = TScalar;
141 ScalarType t{TScalar(0.01)};
145 Onion() = default;
150 explicit Onion(ScalarType t_) : t(t_) {}
158 template <class FSdf>
159 PBAT_HOST_DEVICE ScalarType Eval(Vec3<ScalarType> const& p, FSdf&& sdf) const
160 {
161 using namespace std;
162 return abs(sdf(p)) - t;
163 }
164};
165
170template <common::CArithmetic TScalar>
171struct Symmetrize : public UnaryNode
172{
173 using ScalarType = TScalar;
181 template <class FSdf>
182 PBAT_HOST_DEVICE ScalarType Eval(Vec3<ScalarType> p, FSdf&& sdf) const
183 {
184 using namespace std;
185 p(0) = abs(p(0));
186 p(2) = abs(p(2));
187 return sdf(p);
188 }
189};
190
195template <common::CArithmetic TScalar>
196struct Repeat : public UnaryNode
197{
198 using ScalarType = TScalar;
199 ScalarType s{TScalar(1)};
201 TScalar(5),
202 TScalar(5),
203 TScalar(5)};
204
207 Repeat() = default;
213 explicit Repeat(ScalarType s_, Vec3<ScalarType> const& l_) : s(s_), l(l_) {}
221 template <class FSdf>
222 PBAT_HOST_DEVICE ScalarType Eval(Vec3<ScalarType> const& p, FSdf&& sdf) const
223 {
224 using namespace std;
226 clamp(round(p(0) / s), -l(0), l(0)),
227 clamp(round(p(1) / s), -l(1), l(1)),
228 clamp(round(p(2) / s), -l(2), l(2))};
229 Vec3<ScalarType> q = p - s * pc;
230 return sdf(q);
231 }
232};
233
238template <common::CArithmetic TScalar>
240{
241 using ScalarType = TScalar;
242 ScalarType n{TScalar(1)};
246 RotationalRepeat() = default;
251 explicit RotationalRepeat(ScalarType n_) : n(n_) {}
259 template <class FSdf>
260 PBAT_HOST_DEVICE ScalarType Eval(Vec3<ScalarType> const& p, FSdf&& sdf) const
261 {
262 using namespace std;
263 ScalarType sp = 2 * std::numbers::pi_v<ScalarType> / n;
264 ScalarType an = atan2(p(1), p(0));
265 ScalarType id = floor(an / sp);
266 ScalarType a1 = sp * (id);
267 ScalarType a2 = sp * (id + 1);
268 ScalarType cosa1 = cos(a1);
269 ScalarType sina1 = sin(a1);
270 ScalarType cosa2 = cos(a2);
271 ScalarType sina2 = sin(a2);
272 Vec3<ScalarType> r1{cosa1 * p(0) + sina1 * p(1), -sina1 * p(0) + cosa1 * p(1), p(2)};
273 Vec3<ScalarType> r2{cosa2 * p(0) + sina2 * p(1), -sina2 * p(0) + cosa2 * p(1), p(2)};
274 return min(sdf(r1), sdf(r2));
275 }
276};
277
282template <common::CArithmetic TScalar>
283struct Bump : public UnaryNode
284{
285 using ScalarType = TScalar;
286 Vec3<ScalarType> f{TScalar(1), TScalar(1), TScalar(1)};
287 Vec3<ScalarType> g{TScalar(1), TScalar(1), TScalar(1)};
291 Bump() = default;
297 explicit Bump(Vec3<ScalarType> const& f_, Vec3<ScalarType> const& g_) : f(f_), g(g_) {}
305 template <class FSdf>
306 PBAT_HOST_DEVICE ScalarType Eval(Vec3<ScalarType> const& p, FSdf&& sdf) const
307 {
308 using namespace std;
309 // clang-format off
310 ScalarType d =
311 g(0)*sin(f(0)*p(0)) *
312 g(1)*sin(f(1)*p(1)) *
313 g(2)*sin(f(2)*p(2));
314 // clang-format on
315 return sdf(p) + d;
316 }
317};
318
323template <common::CArithmetic TScalar>
324struct Twist : public UnaryNode
325{
326 using ScalarType = TScalar;
331 Twist() = default;
336 explicit Twist(ScalarType k_) : k(k_) {}
344 template <class FSdf>
345 PBAT_HOST_DEVICE ScalarType Eval(Vec3<ScalarType> const& p, FSdf&& sdf) const
346 {
347 using namespace std;
348 ScalarType c = cos(k * p(1));
349 ScalarType s = sin(k * p(1));
350 Vec3<ScalarType> q{c * p(0) - s * p(2), s * p(0) + c * p(2), p(1)};
351 return sdf(q);
352 }
353};
354
359template <common::CArithmetic TScalar>
360struct Bend : public UnaryNode
361{
362 using ScalarType = TScalar;
367 Bend() = default;
372 explicit Bend(ScalarType k_) : k(k_) {}
380 template <class FSdf>
381 PBAT_HOST_DEVICE ScalarType Eval(Vec3<ScalarType> const& p, FSdf&& sdf) const
382 {
383 using namespace std;
384 ScalarType c = cos(k * p(0));
385 ScalarType s = sin(k * p(0));
386 Vec3<ScalarType> q{c * p(0) - s * p(1), s * p(0) + c * p(1), p(2)};
387 return sdf(q);
388 }
389};
390
391} // namespace pbat::geometry::sdf
392
393#endif // PBAT_GEOMETRY_SDF_UNARYNODE_H
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, 3 > Vec3
3D vector type
Definition TypeDefs.h:23
math::linalg::mini::Zeros< TScalar, 3, 1 > Zero3
3D zero vector type
Definition TypeDefs.h:38
Bend()=default
Default constructor.
PBAT_HOST_DEVICE ScalarType Eval(Vec3< ScalarType > const &p, FSdf &&sdf) const
Evaluate the signed distance function at a point.
Definition UnaryNode.h:381
TScalar ScalarType
Scalar type.
Definition UnaryNode.h:362
Bend(ScalarType k_)
Construct a new Bend object.
Definition UnaryNode.h:372
ScalarType k
Bend factor.
Definition UnaryNode.h:363
Bump(Vec3< ScalarType > const &f_, Vec3< ScalarType > const &g_)
Construct a new Bump object.
Definition UnaryNode.h:297
Bump()=default
Default constructor.
Vec3< ScalarType > f
Frequency along each axis.
Definition UnaryNode.h:286
TScalar ScalarType
Scalar type.
Definition UnaryNode.h:285
Vec3< ScalarType > g
Amplitude of the wave displacement.
Definition UnaryNode.h:287
PBAT_HOST_DEVICE ScalarType Eval(Vec3< ScalarType > const &p, FSdf &&sdf) const
Evaluate the signed distance function at a point.
Definition UnaryNode.h:306
Vec3< ScalarType > h
Elongation vector.
Definition UnaryNode.h:74
Elongate(Vec3< ScalarType > const &h_)
Construct a new Elongate object.
Definition UnaryNode.h:83
TScalar ScalarType
Scalar type.
Definition UnaryNode.h:73
PBAT_HOST_DEVICE ScalarType Eval(Vec3< ScalarType > const &p, FSdf &&sdf) const
Evaluate the signed distance function at a point.
Definition UnaryNode.h:92
Elongate()=default
Default constructor.
Onion()=default
Default constructor.
ScalarType t
Onion thickness.
Definition UnaryNode.h:141
PBAT_HOST_DEVICE ScalarType Eval(Vec3< ScalarType > const &p, FSdf &&sdf) const
Evaluate the signed distance function at a point.
Definition UnaryNode.h:159
Onion(ScalarType t_)
Construct a new Onion object.
Definition UnaryNode.h:150
TScalar ScalarType
Scalar type.
Definition UnaryNode.h:140
TScalar ScalarType
Scalar type.
Definition UnaryNode.h:198
ScalarType s
Uniform repetition spacing.
Definition UnaryNode.h:199
Repeat(ScalarType s_, Vec3< ScalarType > const &l_)
Construct a new Repeat object.
Definition UnaryNode.h:213
PBAT_HOST_DEVICE ScalarType Eval(Vec3< ScalarType > const &p, FSdf &&sdf) const
Evaluate the signed distance function at a point.
Definition UnaryNode.h:222
Repeat()=default
Default constructor.
Vec3< ScalarType > l
Half number of repetitions along each axis.
Definition UnaryNode.h:200
RotationalRepeat()=default
Default constructor.
ScalarType n
Number of repetitions.
Definition UnaryNode.h:242
TScalar ScalarType
Scalar type.
Definition UnaryNode.h:241
PBAT_HOST_DEVICE ScalarType Eval(Vec3< ScalarType > const &p, FSdf &&sdf) const
Evaluate the signed distance function at a point.
Definition UnaryNode.h:260
RotationalRepeat(ScalarType n_)
Construct a new RotationalRepeat object.
Definition UnaryNode.h:251
Round(ScalarType r_)
Construct a new Round object.
Definition UnaryNode.h:118
TScalar ScalarType
Scalar type.
Definition UnaryNode.h:108
PBAT_HOST_DEVICE ScalarType Eval(Vec3< ScalarType > const &p, FSdf &&sdf) const
Evaluate the signed distance function at a point.
Definition UnaryNode.h:127
ScalarType r
Rounding radius.
Definition UnaryNode.h:109
Round()=default
Default constructor.
Scale()=default
Default constructor.
ScalarType s
Scaling factor.
Definition UnaryNode.h:42
Scale(ScalarType s_)
Construct a new Scale object.
Definition UnaryNode.h:51
PBAT_HOST_DEVICE ScalarType Eval(Vec3< ScalarType > const &p, FSdf &&sdf) const
Evaluate the signed distance function at a point.
Definition UnaryNode.h:60
TScalar ScalarType
Scalar type.
Definition UnaryNode.h:41
Symmetrization operation along the x axis.
Definition UnaryNode.h:172
TScalar ScalarType
Scalar type.
Definition UnaryNode.h:173
PBAT_HOST_DEVICE ScalarType Eval(Vec3< ScalarType > p, FSdf &&sdf) const
Evaluate the signed distance function at a point.
Definition UnaryNode.h:182
TScalar ScalarType
Scalar type.
Definition UnaryNode.h:326
ScalarType k
Twist factor.
Definition UnaryNode.h:327
PBAT_HOST_DEVICE ScalarType Eval(Vec3< ScalarType > const &p, FSdf &&sdf) const
Evaluate the signed distance function at a point.
Definition UnaryNode.h:345
Twist()=default
Default constructor.
Twist(ScalarType k_)
Construct a new Twist object.
Definition UnaryNode.h:336
Base struct for all unary nodes.
Definition UnaryNode.h:31