22#ifndef PECLET_CORE_GEOM_SCENE_HPP
23#define PECLET_CORE_GEOM_SCENE_HPP
126template <
class Real,
class Pool>
134 const int ix = (int)cx, iy = (
int)cy, iz = (int)cz;
135 const int ix1 = ix < d.
nx - 1 ? ix + 1 : ix;
136 const int iy1 = iy < d.
ny - 1 ? iy + 1 : iy;
137 const int iz1 = iz < d.
nz - 1 ? iz + 1 : iz;
138 const Real tx = cx - ix, ty = cy - iy, tz = cz - iz;
139 const long nxny = (long)d.
nx * d.
ny;
141 const auto at = [&](
int x,
int y,
int z) {
142 return (
Real)pool(off + (
long)z * nxny + (
long)y * d.
nx + x);
144 const Real c00 = at(ix, iy, iz) * (1 - tx) + at(ix1, iy, iz) * tx;
145 const Real c10 = at(ix, iy1, iz) * (1 - tx) + at(ix1, iy1, iz) * tx;
146 const Real c01 = at(ix, iy, iz1) * (1 - tx) + at(ix1, iy, iz1) * tx;
147 const Real c11 = at(ix, iy1, iz1) * (1 - tx) + at(ix1, iy1, iz1) * tx;
148 const Real c0 = c00 * (1 - ty) + c10 * ty;
149 const Real c1 = c01 * (1 - ty) + c11 * ty;
150 const Real val = c0 * (1 - tz) + c1 * tz;
162template <
class Real,
class Gr
ids,
class Pool>
253template <
class Real,
class Nodes,
class Gr
ids,
class Pool>
255 const Grids& grids,
const Pool& pool) {
267 if (root < 0 || root >= nodeCount)
271 const auto& n = nodes(root);
272 stack[0] = Frame{root, 0,
toCanonical(n.transform, p), n.transform.scale,
Real(0)};
277 Frame& f = stack[sp - 1];
278 const auto& n = nodes(f.node);
281 result = f.scale *
evalLeaf(n, f.can, grids, pool);
283 }
else if (f.stage == 0) {
284 if (n.aux0 < 0 || n.aux0 >= nodeCount || sp >=
kMaxTreeDepth)
287 const auto& c = nodes(n.aux0);
288 stack[sp] = Frame{n.aux0, 0,
toCanonical(c.transform, f.can), c.transform.scale,
Real(0)};
291 }
else if (f.stage == 1) {
292 if (n.aux1 < 0 || n.aux1 >= nodeCount || sp >=
kMaxTreeDepth)
296 const auto& c = nodes(n.aux1);
297 stack[sp] = Frame{n.aux1, 0,
toCanonical(c.transform, f.can), c.transform.scale,
Real(0)};
301 const Real l = f.left, r = result;
309 result = f.scale * combined;
319template <
class Real,
class Gr
ids,
class Pool>
376template <
class Real,
class Nodes,
class Gr
ids,
class Pool>
378 const Grids& grids,
const Pool& pool,
Vec3<Real>& grad) {
393 if (root < 0 || root >= nodeCount)
397 const auto& n = nodes(root);
404 Frame& f = stack[sp - 1];
405 const auto& n = nodes(f.node);
408 result = f.scale *
evalLeaf(n, f.can, grids, pool);
409 resultGrad =
rotate(n.transform.rotation,
leafGrad(n, f.can, grids, pool));
411 }
else if (f.stage == 0) {
412 if (n.aux0 < 0 || n.aux0 >= nodeCount || sp >=
kMaxTreeDepth) {
417 const auto& c = nodes(n.aux0);
418 stack[sp] = Frame{n.aux0,
426 }
else if (f.stage == 1) {
427 if (n.aux1 < 0 || n.aux1 >= nodeCount || sp >=
kMaxTreeDepth) {
432 f.leftGrad = resultGrad;
434 const auto& c = nodes(n.aux1);
435 stack[sp] = Frame{n.aux1,
444 const Real l = f.left, r = result;
449 g = (l <= r) ? f.leftGrad : resultGrad;
452 g = (l >= r) ? f.leftGrad : resultGrad;
455 g = (l >= -r) ? f.leftGrad :
Vec3<Real>{-resultGrad.
x, -resultGrad.
y, -resultGrad.
z};
457 result = f.scale * combined;
458 resultGrad =
rotate(n.transform.rotation, g);
487 for (
int k = 0; k < nCandidates; ++k) {
PECLET_HD Real hdMin(Real a, Real b)
PECLET_HD Real hdMax(Real a, Real b)
PECLET_HD Real hdSqrt(Real v)
constexpr int kMaxTreeDepth
Maximum shape-tree depth the iterative evaluator supports.
PECLET_HD Real evalTreeGrad(const Nodes &nodes, int nodeCount, int root, Vec3< Real > p, const Grids &grids, const Pool &pool, Vec3< Real > &grad)
evalTree WITH its analytic gradient, one traversal (Layer 0's runtime layer catching up with the comp...
@ kSphere
params[0] = radius
@ kDifference
max(L, -R) — L minus R
@ kEllipsoid
params[0..2] = rx, ry, rz (BOUND, under-estimates)
@ kTorus
params[0..1] = majorRadius, minorRadius (exact, y axis)
@ kBox
params[0..2] = hx, hy, hz
@ kSuperquadric
params[0..3] = rx, ry, rz, exponent (BOUND, under-estimates)
@ kGrid
sampled field; aux0 = index into the GridDesc table
@ kHollowCylinderShell
params[0..2] = rOuter, rInner, height (sign-exact, z axis)
@ kCapsule
params[0..1] = radius, halfLength (exact, y axis)
@ kCone
params[0..2] = rBottom, rTop, halfHeight (exact, y axis)
@ kHollowCylinder
params[0..2] = rOuter, height, thickness (distance-exact, y axis)
@ kUnion
min(L, R) — aux0/aux1 = child node indices
PECLET_HD Real evalCandidates(const SceneView< Real > &sc, Vec3< Real > p, const int *candidates, int nCandidates)
Signed distance of a scene over a CANDIDATE LIST (contract 9): the union of the listed instances,...
PECLET_HD Vec3< Real > instanceVelocity(const Instance< Real > &inst, Vec3< Real > p)
PECLET_HD Vec3< Real > instanceCentre(const Instance< Real > &inst)
Rigid-body surface velocity of instance inst at world point p: linVel + angVel x (p - c),...
PECLET_HD Real evalLeaf(const ShapeNode< Real > &n, Vec3< Real > p, const Grids &grids, const Pool &pool)
Evaluate a single LEAF node at a point already in that node's canonical frame.
PECLET_HD Real evalTree(const Nodes &nodes, int nodeCount, int root, Vec3< Real > p, const Grids &grids, const Pool &pool)
Evaluate the shape tree rooted at root at point p (in the root's parent frame).
PECLET_HD Vec3< Real > leafGrad(const ShapeNode< Real > &n, Vec3< Real > p, const Grids &grids, const Pool &pool)
Closed-form gradient of one leaf in its canonical frame (unnormalised, mirroring the prim::* contract...
PECLET_HD Real evalScene(const SceneView< Real > &sc, Vec3< Real > p)
Convenience: the union of EVERY instance.
PECLET_HD Real sampleGrid(Vec3< Real > p, const GridDesc< Real > &d, const Pool &pool)
Trilinear sample of a grid field, with the clamp-plus-residual off-grid extension selected by d....
GridExtension
NOT YET A NODE KIND: prim::Rounded (offset/rounding) stays a compile-time wrapper for now.
PECLET_HD Real evalInstance(const SceneView< Real > &sc, int i, Vec3< Real > p)
Signed distance of ONE instance at world point p.
PECLET_HD Vec3< Real > toCanonical(const Transform< Real > &tr, Vec3< Real > p)
Map a point from the parent frame into this transform's canonical frame.
double Real
Default host floating type. Device kernels may use float; conversions happen at the boundary.
PECLET_HD Vec3< Real > rotate(Quat< Real > q, Vec3< Real > v)
Rotate v by q.
PECLET_HD Vec3< Real > add(Vec3< Real > a, Vec3< Real > b)
PECLET_HD Vec3< Real > invRotate(Quat< Real > q, Vec3< Real > v)
Rotate v by the conjugate of q — dem invRotateVector.
PECLET_HD Vec3< Real > sub(Vec3< Real > a, Vec3< Real > b)
PECLET_HD Vec3< Real > scale(Vec3< Real > a, Real s)
PECLET_HD Vec3< Real > cross(Vec3< Real > a, Vec3< Real > b)
Quaternion, {x, y, z, w} — dem's F4 ordering (dem_portable.hpp), NOT {w, x, y, z}.
Real-templated POD 3-vector — the geometry layer's point type on host and device.
Descriptor for one sampled field inside a shared sample pool.
Vec3< Real > invSpacing
1 / node spacing, per axis (dem stores the inverse too)
int offset
start index of this field's samples in the pool
One placed copy of a shape tree (contract 7).
int materialId
consumer-defined; -1 = unset
bool centerPinned
true: center is an explicit world point (the origin included); false: the centre follows the body (co...
Vec3< Real > center
reference point of the rotation, in world coordinates
Transform< Real > transform
int shapeRoot
index of the root ShapeNode of this instance's tree
Adaptors so a raw pointer can stand in for a Kokkos View (Views already provide operator()),...
PECLET_HD T operator()(long i) const
Non-owning bundle of everything a scene evaluation needs (resolved decision 4).
const Instance< Real > * instances
const GridDesc< Real > * grids
const ShapeNode< Real > * nodes
One node of a flat shape tree. POD, trivially copyable, safe in a Kokkos View.
int aux0
CSG: left child index. kGrid: GridDesc table index.
Transform< Real > transform
int aux1
CSG: right child index.
PECLET_HD const T & operator()(long i) const
Axis-aligned solid box of half-extents (hx, hy, hz), centred on the origin.
PECLET_HD Real eval(Vec3< Real > p) const
PECLET_HD Vec3< Real > grad(Vec3< Real > p) const
Solid capsule about the y axis: a segment from (0,-halfLength,0) to (0,+halfLength,...
PECLET_HD Vec3< Real > grad(Vec3< Real > p) const
PECLET_HD Real eval(Vec3< Real > p) const
Solid capped cone (frustum) about the y axis: radius rBottom at y = -halfHeight, radius rTop at y = +...
PECLET_HD Real eval(Vec3< Real > p) const
PECLET_HD Vec3< Real > grad(Vec3< Real > p) const
Finite differences over this leaf's own eval (the piecewise capped-cone form has no gradient worth ha...
Solid ellipsoid with semi-axes (rx, ry, rz), centred on the origin.
PECLET_HD Vec3< Real > grad(Vec3< Real > p) const
Finite differences over this leaf's own eval (the normalised implicit has no gradient worth hand-deri...
PECLET_HD Real eval(Vec3< Real > p) const
Solid tube wall about the z axis as a CSG intersection of half-spaces: max(r - rOuter,...
PECLET_HD Real eval(Vec3< Real > p) const
PECLET_HD Vec3< Real > grad(Vec3< Real > p) const
Solid tube wall about the y axis: outer radius rOuter, full height height, wall thickness thickness (...
PECLET_HD Vec3< Real > grad(Vec3< Real > p) const
PECLET_HD Real eval(Vec3< Real > p) const
Solid ball of radius radius, centred on the origin.
PECLET_HD Vec3< Real > grad(Vec3< Real > p) const
PECLET_HD Real eval(Vec3< Real > p) const
Solid superquadric with semi-axes (rx, ry, rz) and exponent e: the implicit |x/rx|^e + |y/ry|^e + |z/...
PECLET_HD Real eval(Vec3< Real > p) const
PECLET_HD Vec3< Real > grad(Vec3< Real > p) const
Finite differences over this leaf's own eval (the Taubin estimate has no gradient worth hand-deriving...
Solid torus with its axis along y: tube of minor radius minorRadius swept around a circle of radius m...
PECLET_HD Real eval(Vec3< Real > p) const
PECLET_HD Vec3< Real > grad(Vec3< Real > p) const