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
Composite.h
Go to the documentation of this file.
1
11#ifndef PBAT_GEOMETRY_SDF_COMPOSITE_H
12#define PBAT_GEOMETRY_SDF_COMPOSITE_H
13
14#include "BinaryNode.h"
15#include "PhysicsBasedAnimationToolkitExport.h"
16#include "Primitive.h"
17#include "Transform.h"
18#include "TypeDefs.h"
19#include "UnaryNode.h"
20#include "pbat/HostDevice.h"
21
22#include <algorithm>
23#include <array>
24#include <span>
25#include <type_traits>
26#include <utility>
27#include <variant>
28#include <vector>
29
30namespace pbat::geometry::sdf {
31
35template <common::CArithmetic TScalar>
36using Node = std::variant<
37 /* primitives */
61 /* unary nodes */
72 /* binary nodes */
80
89
93template <common::CArithmetic TScalar>
95{
96 using ScalarType = TScalar;
104 PBAT_HOST_DEVICE
105 Composite(
106 std::span<Node<ScalarType> const> nodes,
107 std::span<Transform<ScalarType> const> transforms,
108 std::span<std::pair<int, int> const> children,
109 std::span<int const> roots);
115 PBAT_HOST_DEVICE
116 ScalarType Eval(Vec3<ScalarType> const& p) const;
123 PBAT_HOST_DEVICE
124 ScalarType Eval(int n, Vec3<ScalarType> const& p) const;
135 PBAT_HOST_DEVICE
141 PBAT_HOST_DEVICE
142 ECompositeStatus Status() const { return mStatus; }
143
144 std::span<Node<ScalarType> const> mNodes;
145 std::span<Transform<ScalarType> const>
147 std::span<std::pair<int, int> const>
152 std::span<int const> mRoots;
154};
155
162PBAT_API auto FindRootsAndParents(std::span<std::pair<int, int> const> children)
163 -> std::pair<std::vector<int>, std::vector<int>>;
164
165template <common::CArithmetic TScalar>
167 std::span<Node<ScalarType> const> nodes,
168 std::span<Transform<ScalarType> const> transforms,
169 std::span<std::pair<int, int> const> children,
170 std::span<int const> roots)
171 : mNodes(nodes),
172 mTransforms(transforms),
173 mChildren(children),
174 mRoots(roots),
176{
177 std::size_t const nNodes = mNodes.size();
178 for (std::size_t n = 0ULL; n < nNodes; ++n)
179 {
181 break;
182 std::visit(
183 [&](auto const& node) {
184 using NodeType = std::remove_cvref_t<decltype(node)>;
185 if constexpr (std::is_base_of_v<Primitive, NodeType>)
186 {
187 if (mChildren[n].first >= 0 || mChildren[n].second >= 0)
189 }
190 else if constexpr (std::is_base_of_v<UnaryNode, NodeType>)
191 {
192 if (mChildren[n].first < 0)
194 if (mChildren[n].second >= 0)
196 }
197 else if constexpr (std::is_base_of_v<BinaryNode, NodeType>)
198 {
199 if (mChildren[n].first < 0 || mChildren[n].second < 0)
201 }
202 },
203 mNodes[n]);
204 }
205}
206
207template <common::CArithmetic TScalar>
208inline TScalar Composite<TScalar>::Eval(Vec3<ScalarType> const& p) const
209{
210 using namespace std;
211 TScalar sd = numeric_limits<TScalar>::max();
212 for (auto root : mRoots)
213 sd = min(sd, Eval(root, p));
214 return sd;
215}
216
217template <common::CArithmetic TScalar>
218inline TScalar Composite<TScalar>::Eval(int n, Vec3<ScalarType> const& p) const
219{
220 std::size_t const nStl = static_cast<std::size_t>(n);
221 ScalarType sd;
222 Vec3<ScalarType> q = mTransforms[nStl] / p;
223 std::visit(
224 [&](auto const& node) {
225 using NodeType = std::remove_cvref_t<decltype(node)>;
226 if constexpr (std::is_base_of_v<Primitive, NodeType>)
227 {
228 sd = node.Eval(q);
229 }
230 else if constexpr (std::is_base_of_v<UnaryNode, NodeType>)
231 {
232 int const child = mChildren[nStl].first;
233 sd = node.Eval(q, [this, child](Vec3<ScalarType> const& x) {
234 return Eval(child, x);
235 });
236 }
237 else if constexpr (std::is_base_of_v<BinaryNode, NodeType>)
238 {
239 auto ci = mChildren[nStl].first;
240 auto cj = mChildren[nStl].second;
241 sd = node.Eval(Eval(ci, q), Eval(cj, q));
242 }
243 },
244 mNodes[nStl]);
245 return sd;
246}
247
248template <common::CArithmetic TScalar>
250{
251 ScalarType constexpr one{1};
252 ScalarType constexpr none{-1};
254 Vec3<ScalarType> k{one, none, none};
255 g = k * Eval(p + h * k);
256 k = Vec3<ScalarType>{none, none, one};
257 g += k * Eval(p + h * k);
258 k = Vec3<ScalarType>{none, one, none};
259 g += k * Eval(p + h * k);
260 k = Vec3<ScalarType>{one, one, one};
261 g += k * Eval(p + h * k);
262 g *= ScalarType(0.25) / h;
263 return g;
264}
265
266} // namespace pbat::geometry::sdf
267
268#endif // PBAT_GEOMETRY_SDF_COMPOSITE_H
This file defines binary nodes for a SDF compositions.
This file defines primitive SDF shapes.
This file defines transforms useful for moving SDFs around.
This file defines common type definitions used in the SDF module.
This file defines unary nodes for SDF compositions.
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
ECompositeStatus
Status of the composite SDF.
Definition Composite.h:84
@ InvalidForest
The SDF forest topology is invalid (wrong children/ancestor relations)
Definition Composite.h:86
@ UnexpectedNodeType
An unexpected node type was encountered.
Definition Composite.h:87
@ Valid
The composite SDF can be evaluated.
Definition Composite.h:85
std::variant< Sphere< TScalar >, Box< TScalar >, BoxFrame< TScalar >, Torus< TScalar >, CappedTorus< TScalar >, Link< TScalar >, InfiniteCylinder< TScalar >, Cone< TScalar >, InfiniteCone< TScalar >, Plane< TScalar >, HexagonalPrism< TScalar >, Capsule< TScalar >, VerticalCapsule< TScalar >, CappedCylinder< TScalar >, VerticalCappedCylinder< TScalar >, RoundedCylinder< TScalar >, VerticalCappedCone< TScalar >, CutHollowSphere< TScalar >, VerticalRoundCone< TScalar >, Octahedron< TScalar >, Pyramid< TScalar >, Triangle< TScalar >, Quadrilateral< TScalar >, Scale< TScalar >, Elongate< TScalar >, Round< TScalar >, Onion< TScalar >, Symmetrize< TScalar >, Repeat< TScalar >, RotationalRepeat< TScalar >, Bump< TScalar >, Twist< TScalar >, Bend< TScalar >, Union< TScalar >, Difference< TScalar >, Intersection< TScalar >, ExclusiveOr< TScalar >, SmoothUnion< TScalar >, SmoothDifference< TScalar >, SmoothIntersection< TScalar > > Node
Variant type representing a node in the composite SDF forest.
Definition Composite.h:36
auto FindRootsAndParents(std::span< std::pair< int, int > const > children) -> std::pair< std::vector< int >, std::vector< int > >
Find the roots and parents of a composite SDF forest given its children.
Definition Composite.cpp:5
Bend operation around the z axis.
Definition UnaryNode.h:361
Box frame with half extents and thickness .
Definition Primitive.h:120
Axis-aligned box centered in with half extents .
Definition Primitive.h:81
Wave-like bumpiness operation along the axes.
Definition UnaryNode.h:284
Capped cylinder shape with endpoints and radius .
Definition Primitive.h:563
Capped torus centered in with parameters sc, ra, rb.
Definition Primitive.h:207
Capsule shape with endpoints and radius .
Definition Primitive.h:482
PBAT_HOST_DEVICE Vec3< ScalarType > Grad(Vec3< ScalarType > const &p, ScalarType h) const
Evaluate the (numerical) gradient of the signed distance function at a given point using a central di...
Definition Composite.h:249
std::span< Transform< ScalarType > const > mTransforms
|# nodes| transforms associated to each node
Definition Composite.h:146
std::span< int const > mRoots
|# roots| root indices of the composite SDF
Definition Composite.h:152
TScalar ScalarType
Scalar type.
Definition Composite.h:96
PBAT_HOST_DEVICE ECompositeStatus Status() const
Get the status of the composite SDF.
Definition Composite.h:142
std::span< Node< ScalarType > const > mNodes
|# nodes| nodes in the composite SDF
Definition Composite.h:144
PBAT_HOST_DEVICE ScalarType Eval(Vec3< ScalarType > const &p) const
Evaluate the signed distance function at a given point.
Definition Composite.h:208
std::span< std::pair< int, int > const > mChildren
Definition Composite.h:148
PBAT_HOST_DEVICE Composite(std::span< Node< ScalarType > const > nodes, std::span< Transform< ScalarType > const > transforms, std::span< std::pair< int, int > const > children, std::span< int const > roots)
Construct a composite SDF from nodes, transforms, children, ancestors and roots.
Definition Composite.h:166
ECompositeStatus mStatus
Status of the composite SDF.
Definition Composite.h:153
Cone shape.
Definition Primitive.h:331
Cut hollow sphere shape with radius , cut height and thickness .
Definition Primitive.h:753
Difference operation.
Definition BinaryNode.h:58
Elongation operation along the axes.
Definition UnaryNode.h:72
Exclusive or operation.
Definition BinaryNode.h:100
Hexagonal prism shape.
Definition Primitive.h:436
Infinite cone shape.
Definition Primitive.h:377
Infinite cylinder.
Definition Primitive.h:294
Intersection operation.
Definition BinaryNode.h:79
Octahedron shape.
Definition Primitive.h:848
Onion operation (i.e. carving interior)
Definition UnaryNode.h:139
Plane shape with normal and point on the plane .
Definition Primitive.h:416
Pyramid shape.
Definition Primitive.h:896
Quadrilateral shape with vertices .
Definition Primitive.h:1019
Grid-like repetition operation along the axes.
Definition UnaryNode.h:197
Circular repetition operation around axe.
Definition UnaryNode.h:240
Rounding operation (i.e. positive offset surface)
Definition UnaryNode.h:107
Rounded cylinder shape with height , radius and rounding radius .
Definition Primitive.h:656
Uniform scaling operation.
Definition UnaryNode.h:40
Smooth difference operation.
Definition BinaryNode.h:154
Smooth intersection operation.
Definition BinaryNode.h:186
Smooth union operation.
Definition BinaryNode.h:121
Sphere centered in with radius .
Definition Primitive.h:50
Symmetrization operation along the x axis.
Definition UnaryNode.h:172
Torus centered in with minor+major radius .
Definition Primitive.h:172
A 3D rigid transform.
Definition Transform.h:31
Triangle shape.
Definition Primitive.h:955
Twist operation around the y axis.
Definition UnaryNode.h:325
Union operation.
Definition BinaryNode.h:35
Capped cone shape with height and minor+major radius .
Definition Primitive.h:700
Vertical capped cylinder shape with height and radius .
Definition Primitive.h:615
Capsule shape with height and radius .
Definition Primitive.h:525
Vertical round cone shape with height , radii at endpoints.
Definition Primitive.h:798