core 1.8.1
Shared MPI block decomposition, asynchronous ghost-layer exchange, SDF geometry, load balancing (header-only C++20)
Loading...
Searching...
No Matches
scene_query.hpp
Go to the documentation of this file.
1// core — accelerated scene queries: periodic evaluation, geometry-driven candidate grids, and the
2// sphere-union fast paths the porous-media consumers live on.
3//
4// Layer 2-for-core of suite/docs/ANALYTIC_SDF_GEOMETRY.md, shaped by the AMR consumer's measured
5// requirements (docs/AMR_GEOMETRY_SETUP_REQUIREMENTS.md): 94% of AmrFlow::setSolid on the
6// 180-sphere RCP bed is SDF evaluation — 101 evals/leaf x 1.18 us/eval brute force over all
7// primitives. This header owns the cost-per-eval side of that product:
8//
9// PeriodicBox / minImage min-image periodicity, the AMR stopgap's EXACT expression
10// (d -= L*nearbyint(d/L)) so results are BIT-IDENTICAL, not merely
11// sign-agreeing at the probe scale.
12// SphereUnionView compact union-of-spheres form (the workhorse geometry) with an
13// equal-radius fast path: min over SQUARED center distances, ONE
14// sqrt per query instead of one per sphere.
15// CandidateGridView + build per-bin candidate lists over a UNIFORM auxiliary lattice, built
16// geometry-driven and pruned with exact bounds, so a query costs
17// bin arithmetic + a 1-5 entry list instead of a scan over every
18// primitive.
19// SphereBedQuery the host-callable object that slots straight into
20// AmrFlow::setSolid's templated SdfFn parameter — the stopgap
21// retirement path.
22//
23// WHY UNIFORM BINS AND NOT OCTREE-LEAF LISTS. The directive behind contract 9 is that geometry
24// drives (each primitive splats onto the cells its band overlaps) and that far-field cells never
25// traverse an acceleration structure. Keying the lists by a fixed auxiliary lattice satisfies both
26// with no octree coupling at all: a query is O(1) bin arithmetic from the POINT's own coordinates,
27// so leaves of every level — and probe points at any h, virtual positions across 2:1 jumps, coarse
28// MG face centers — hit the right list with no per-level machinery and no leavesInBox dependency.
29// A leaf-keyed variant can be layered on top by the AMR side (list(leaf) = list(binOf(center))
30// unioned over the leaf's span) without any new API here.
31//
32// EXACTNESS CONTRACT (the float-sign classification requirement). The candidate list of a bin is a
33// conservative superset of the argmin set over every point of the bin, built with the classic
34// bound pruning: keep primitive i iff lower_i(B) <= U(B), where lower_i is a lower bound of i's
35// contribution anywhere in B and U(B) = min_j upper_j(B) is an upper bound of the union anywhere
36// in B. min over a superset that contains the argmin IS the min — bit-identically, since each
37// member's value is computed with the identical expression — so acceleration changes NOTHING about
38// the result: not the value, not the sign, not under list reordering (min is order-independent as
39// a value). Queries outside the grid's coverage, or in a bin whose list is empty (possible far
40// from all geometry when bounds never reached it), FALL BACK to the full scan — exact, and rare
41// where it matters.
42//
43// The bounds argument needs eval_i >= (distance to a ball containing i's solid) pointwise, which
44// holds for exact-distance leaves and is PRESERVED by union (min over children, all certified),
45// intersection and difference (max(a, ...) >= a, so the LEFT child's ball and certificate
46// suffice). It FAILS for the under-estimating leaves (ellipsoid, superquadric,
47// HollowCylinderShell — measured under-run up to 4x in Layer 0's gate) and for grid leaves: those
48// instances are never pruned — they ride on an ALWAYS list evaluated for every query. The
49// workhorse geometries (sphere beds, box/cylinder/capsule/torus stirrers and their CSG) prune.
50//
51// Host-compilable (PECLET_HD); the Kokkos-owning DeviceScene and batched drivers live in
52// device_scene.hpp.
53#ifndef PECLET_CORE_GEOM_SCENE_QUERY_HPP
54#define PECLET_CORE_GEOM_SCENE_QUERY_HPP
55
56#include <cstddef>
57#include <stdexcept>
58#include <vector>
59
62
63namespace peclet::core::geom {
64
65// ---------------------------------------------------------------------------------------------
66// Periodicity (AMR requirement 4)
67// ---------------------------------------------------------------------------------------------
68
70template <class Real>
72 Real Lx = 0, Ly = 0, Lz = 0;
73 bool on = false;
74};
75
83template <class Real>
85 namespace cd = peclet::core::detail;
86 if (b.on) {
87 d.x = cd::hdFma(-cd::hdNearbyint(d.x / b.Lx), b.Lx, d.x);
88 d.y = cd::hdFma(-cd::hdNearbyint(d.y / b.Ly), b.Ly, d.y);
89 d.z = cd::hdFma(-cd::hdNearbyint(d.z / b.Lz), b.Lz, d.z);
90 }
91 return d;
92}
93
94// ---------------------------------------------------------------------------------------------
95// Sphere union (the porous-media workhorse)
96// ---------------------------------------------------------------------------------------------
97
100template <class Real>
102 const Real* cx = nullptr;
103 const Real* cy = nullptr;
104 const Real* cz = nullptr;
105 const Real* r = nullptr;
106 int n = 0;
107 bool equalR = false;
108 Real r0 = 0;
109};
110
114template <class Real>
116 const PeriodicBox<Real>& box) {
117 namespace cd = peclet::core::detail;
118 const Vec3<Real> d = minImage(Vec3<Real>{p.x - u.cx[i], p.y - u.cy[i], p.z - u.cz[i]}, box);
119 return cd::hdFma(d.z, d.z, cd::hdFma(d.y, d.y, d.x * d.x));
120}
121
126template <class Real>
128 const PeriodicBox<Real>& box, const int* items, int count) {
129 if (u.equalR) {
130 Real best2 = Real(1e300);
131 for (int k = 0; k < count; ++k) {
132 const Real d2 = sphereDist2(u, items[k], p, box);
133 if (d2 < best2)
134 best2 = d2;
135 }
136 return peclet::core::detail::hdSqrt(best2) - u.r0;
137 }
138 Real best = Real(1e300);
139 for (int k = 0; k < count; ++k) {
140 const int i = items[k];
141 const Real v = peclet::core::detail::hdSqrt(sphereDist2(u, i, p, box)) - u.r[i];
142 if (v < best)
143 best = v;
144 }
145 return best;
146}
147
150template <class Real>
152 const PeriodicBox<Real>& box) {
153 if (u.equalR) {
154 Real best2 = Real(1e300);
155 for (int i = 0; i < u.n; ++i) {
156 const Real d2 = sphereDist2(u, i, p, box);
157 if (d2 < best2)
158 best2 = d2;
159 }
160 return peclet::core::detail::hdSqrt(best2) - u.r0;
161 }
162 Real best = Real(1e300);
163 for (int i = 0; i < u.n; ++i) {
164 const Real v = peclet::core::detail::hdSqrt(sphereDist2(u, i, p, box)) - u.r[i];
165 if (v < best)
166 best = v;
167 }
168 return best;
169}
170
171// ---------------------------------------------------------------------------------------------
172// Candidate grid
173// ---------------------------------------------------------------------------------------------
174
177template <class Real>
179 int nx = 0, ny = 0, nz = 0;
180 Real ox = 0, oy = 0, oz = 0; // origin
181 Real bx = 1, by = 1, bz = 1; // bin size
182 const int* offsets = nullptr; // nx*ny*nz + 1
183 const int* items = nullptr;
184 const int* always = nullptr; // non-prunable entries, evaluated for every query
185 int alwaysCount = 0;
186 bool wrap = false; // periodic bin lookup
187
190 PECLET_HD long binOf(Vec3<Real> p) const {
191 long i = (long)peclet::core::detail::hdFloor((p.x - ox) / bx);
192 long j = (long)peclet::core::detail::hdFloor((p.y - oy) / by);
193 long k = (long)peclet::core::detail::hdFloor((p.z - oz) / bz);
194 if (wrap) {
195 i = ((i % nx) + nx) % nx;
196 j = ((j % ny) + ny) % ny;
197 k = ((k % nz) + nz) % nz;
198 } else if (i < 0 || i >= nx || j < 0 || j >= ny || k < 0 || k >= nz) {
199 return -1;
200 }
201 return i + (long)nx * (j + (long)ny * k);
202 }
203};
204
208template <class Real>
210 const PeriodicBox<Real>& box, const CandidateGridView<Real>& g) {
211 const long b = g.binOf(p);
212 if (b < 0)
213 return evalSphereUnion(u, p, box);
214 const int lo = g.offsets[b], hi = g.offsets[b + 1];
215 if (lo == hi)
216 return evalSphereUnion(u, p, box);
217 return evalSphereUnionSubset(u, p, box, g.items + lo, hi - lo);
218}
219
221template <class Real>
223 std::vector<int> offsets, items, always;
224 std::vector<Real> instBoundR; // per-instance world bounding radii (general-scene builds)
225 CandidateGridView<Real> meta; // pointers unset; use view()
226
229 v.offsets = offsets.data();
230 v.items = items.data();
231 v.always = always.data();
232 v.alwaysCount = static_cast<int>(always.size());
233 return v;
234 }
235};
236
237namespace query_detail {
238
241template <class Real, class NbAxis>
243 const PeriodicBox<Real>& box, NbAxis&& nbAxis) {
244 m.nx = nbAxis(extent.x);
245 m.ny = nbAxis(extent.y);
246 m.nz = nbAxis(extent.z);
247 m.ox = origin.x;
248 m.oy = origin.y;
249 m.oz = origin.z;
250 m.bx = extent.x / m.nx;
251 m.by = extent.y / m.ny;
252 m.bz = extent.z / m.nz;
253 m.wrap = box.on;
254}
255
257template <class Real>
258void packLists(CandidateGrid<Real>& g, const std::vector<std::vector<int>>& lists, long nbins) {
259 g.offsets.resize((std::size_t)nbins + 1);
260 g.offsets[0] = 0;
261 for (long b = 0; b < nbins; ++b)
262 g.offsets[(std::size_t)b + 1] =
263 g.offsets[(std::size_t)b] + static_cast<int>(lists[(std::size_t)b].size());
264 g.items.resize((std::size_t)g.offsets[(std::size_t)nbins]);
265 for (long b = 0; b < nbins; ++b)
266 std::copy(lists[(std::size_t)b].begin(), lists[(std::size_t)b].end(),
267 g.items.begin() + g.offsets[(std::size_t)b]);
268}
269
273template <class Real, class Fn>
275 Real reach, Fn&& fn) {
276 const int riX = static_cast<int>(reach / m.bx) + 1;
277 const int riY = static_cast<int>(reach / m.by) + 1;
278 const int riZ = static_cast<int>(reach / m.bz) + 1;
279 const int ci = static_cast<int>(peclet::core::detail::hdFloor((u.cx[i] - m.ox) / m.bx));
280 const int cj = static_cast<int>(peclet::core::detail::hdFloor((u.cy[i] - m.oy) / m.by));
281 const int ck = static_cast<int>(peclet::core::detail::hdFloor((u.cz[i] - m.oz) / m.bz));
282 for (int dk = -riZ; dk <= riZ; ++dk)
283 for (int dj = -riY; dj <= riY; ++dj)
284 for (int di = -riX; di <= riX; ++di) {
285 long ii = ci + di, jj = cj + dj, kk = ck + dk;
286 if (m.wrap) {
287 ii = ((ii % m.nx) + m.nx) % m.nx;
288 jj = ((jj % m.ny) + m.ny) % m.ny;
289 kk = ((kk % m.nz) + m.nz) % m.nz;
290 } else if (ii < 0 || ii >= m.nx || jj < 0 || jj >= m.ny || kk < 0 || kk >= m.nz) {
291 continue;
292 }
293 fn(ii + (long)m.nx * (jj + (long)m.ny * kk));
294 }
295}
296
297} // namespace query_detail
298
308template <class Real>
310 Vec3<Real> extent, const PeriodicBox<Real>& box,
311 int nbHint = 0) {
312 if (u.n <= 0)
313 throw std::invalid_argument("buildSphereCandidateGrid: empty union");
314 Real rMean = 0;
315 for (int i = 0; i < u.n; ++i)
316 rMean += u.r[i];
317 rMean /= u.n;
318 auto nbAxis = [&](Real L) {
319 if (nbHint > 0)
320 return nbHint;
321 int nb = rMean > 0 ? static_cast<int>(L / (rMean * Real(0.5))) : 16;
322 return nb < 2 ? 2 : (nb > 96 ? 96 : nb);
323 };
325 auto& m = g.meta;
326 query_detail::layoutBins(m, origin, extent, box, nbAxis);
327 const long nbins = (long)m.nx * m.ny * m.nz;
328 const Real halfDiag = Real(0.5) * std::sqrt(m.bx * m.bx + m.by * m.by + m.bz * m.bz);
329 // Conservative slack absorbing the float rounding of the bounds themselves; supersets stay
330 // supersets, lists grow by (rarely) one entry.
331 const Real slack = Real(1e-12) * (extent.x + extent.y + extent.z);
332
333 auto binCenter = [&](long b, Vec3<Real>& c) {
334 const long i = b % m.nx, j = (b / m.nx) % m.ny, k = b / ((long)m.nx * m.ny);
335 c = Vec3<Real>{m.ox + (i + Real(0.5)) * m.bx, m.oy + (j + Real(0.5)) * m.by,
336 m.oz + (k + Real(0.5)) * m.bz};
337 };
338 auto dist = [&](const Vec3<Real>& a, int i) {
339 const Vec3<Real> d = minImage(Vec3<Real>{a.x - u.cx[i], a.y - u.cy[i], a.z - u.cz[i]}, box);
340 return std::sqrt(d.x * d.x + d.y * d.y + d.z * d.z);
341 };
342 // Pass 1: U(B) = min_i upper_i(B), splat within each sphere's competitive reach. The reach must
343 // cover every bin where this sphere could OWN the best upper bound; beyond max over spheres of
344 // (their own reach), U stays +inf and queries there fall back. Reach heuristic: the largest
345 // min-image distance possible is bounded by the box half-diagonal, so cap there.
346 std::vector<Real> U((std::size_t)nbins, Real(1e300));
347 const Real capReach =
348 box.on ? Real(0.5) * std::sqrt(box.Lx * box.Lx + box.Ly * box.Ly + box.Lz * box.Lz) + halfDiag
349 : std::sqrt(extent.x * extent.x + extent.y * extent.y + extent.z * extent.z);
350 for (int i = 0; i < u.n; ++i)
351 query_detail::forEachBinNearSphere(m, u, i, capReach, [&](long b) {
352 Vec3<Real> c;
353 binCenter(b, c);
354 const Real up = dist(c, i) + halfDiag - u.r[i];
355 if (up < U[(std::size_t)b])
356 U[(std::size_t)b] = up;
357 });
358 // Pass 2: membership by the exact criterion. Each sphere visits bins within its own competitive
359 // reach: lower_i(B) <= U(B) needs d <= r_i + halfDiag + U(B); U is bounded above by the max
360 // over the field, so cap the walk there (plus its own radius + diag).
361 Real Umax = 0;
362 for (Real v : U)
363 if (v < Real(1e300) && v > Umax)
364 Umax = v;
365 std::vector<std::vector<int>> lists((std::size_t)nbins);
366 for (int i = 0; i < u.n; ++i)
367 query_detail::forEachBinNearSphere(m, u, i, u.r[i] + halfDiag + Umax + slack, [&](long b) {
368 Vec3<Real> c;
369 binCenter(b, c);
370 const Real lower = dist(c, i) - halfDiag - u.r[i];
371 if (lower <= U[(std::size_t)b] + slack)
372 lists[(std::size_t)b].push_back(i);
373 });
374 query_detail::packLists(g, lists, nbins);
375 return g;
376}
377
378// ---------------------------------------------------------------------------------------------
379// General scenes: periodic evaluation, bounds, candidate grids
380// ---------------------------------------------------------------------------------------------
381
393template <class Real>
395 const PeriodicBox<Real>& box, Real boundR = Real(-1)) {
396 if (i < 0 || i >= sc.instanceCount)
397 return Real(1e9);
398 const Instance<Real>& inst = sc.instances[i];
399 if (!box.on) {
400 const Vec3<Real> q =
401 scale(invRotate(inst.transform.rotation, sub(p, inst.transform.translation)),
402 Real(1) / inst.transform.scale);
403 return inst.transform.scale *
406 }
407 const Vec3<Real> d0 = minImage(sub(p, inst.transform.translation), box);
408 // which axes need the neighbouring image? |d_a| > L_a/2 - boundR (all of them when unknown)
409 const Real Ls[3] = {box.Lx, box.Ly, box.Lz};
410 const Real da[3] = {d0.x, d0.y, d0.z};
411 int nAlt[3] = {1, 1, 1};
412 Real alt[3][2] = {{d0.x, 0}, {d0.y, 0}, {d0.z, 0}};
413 for (int a = 0; a < 3; ++a) {
414 const bool need =
415 boundR < Real(0) || (da[a] < Real(0) ? -da[a] : da[a]) > Ls[a] * Real(0.5) - boundR;
416 if (need) {
417 alt[a][1] = da[a] < Real(0) ? da[a] + Ls[a] : da[a] - Ls[a];
418 nAlt[a] = 2;
419 }
420 }
421 Real best = Real(1e300);
422 for (int kz = 0; kz < nAlt[2]; ++kz)
423 for (int ky = 0; ky < nAlt[1]; ++ky)
424 for (int kx = 0; kx < nAlt[0]; ++kx) {
425 const Vec3<Real> d{alt[0][kx], alt[1][ky], alt[2][kz]};
426 const Vec3<Real> q =
427 scale(invRotate(inst.transform.rotation, d), Real(1) / inst.transform.scale);
428 const Real v = inst.transform.scale * evalTree<Real>(TablePtr<ShapeNode<Real>>{sc.nodes},
429 sc.nodeCount, inst.shapeRoot, q,
432 if (v < best)
433 best = v;
434 }
435 return best;
436}
437
441template <class Real>
443 const PeriodicBox<Real>& box, const Real* boundR = nullptr) {
444 Real m = Real(1e9);
445 for (int i = 0; i < sc.instanceCount; ++i) {
446 const Real d = evalInstancePeriodic(sc, i, p, box, boundR ? boundR[i] : Real(-1));
447 if (d < m)
448 m = d;
449 }
450 return m;
451}
452
460template <class Real>
462 Vec3<Real> c{0, 0, 0};
463 Real r = 0;
464 bool certified = false;
465};
466
467namespace query_detail {
468
469template <class Real>
470struct NodeBound {
471 Vec3<Real> c{0, 0, 0}; // in the node's PARENT frame
472 Real r = 0;
473 bool certified = false;
474};
475
476template <class Real>
477inline NodeBound<Real> nodeBound(const SceneView<Real>& sc, int node) {
479 if (node < 0 || node >= sc.nodeCount)
480 return nb;
481 const ShapeNode<Real>& n = sc.nodes[node];
482 Real localR = 0;
483 bool cert = false;
484 if (n.kind < kCsgBase) {
485 switch (n.kind) {
486 case kSphere:
487 localR = n.params[0];
488 cert = true;
489 break;
490 case kBox:
491 localR = std::sqrt(n.params[0] * n.params[0] + n.params[1] * n.params[1] +
492 n.params[2] * n.params[2]);
493 cert = true;
494 break;
495 case kHollowCylinder: { // (rOuter, height, thickness), about y -- distance-exact
496 const Real ro = n.params[0], h2 = n.params[1] * Real(0.5);
497 localR = std::sqrt(ro * ro + h2 * h2);
498 cert = true;
499 break;
500 }
501 case kCapsule:
502 localR = n.params[1] + n.params[0];
503 cert = true;
504 break;
505 case kTorus:
506 localR = n.params[0] + n.params[1];
507 cert = true;
508 break;
509 case kCone: {
510 const Real rb = std::fmax(n.params[0], n.params[1]), hh = n.params[2];
511 localR = std::sqrt(rb * rb + hh * hh);
512 cert = true;
513 break;
514 }
515 case kHollowCylinderShell: { // sign-exact only: bounded but NOT certified
516 const Real ro = n.params[0], h2 = n.params[2] * Real(0.5);
517 localR = std::sqrt(ro * ro + h2 * h2);
518 cert = false;
519 break;
520 }
521 case kEllipsoid:
522 localR = std::fmax(n.params[0], std::fmax(n.params[1], n.params[2]));
523 cert = false;
524 break;
525 case kSuperquadric:
526 localR = std::sqrt(n.params[0] * n.params[0] + n.params[1] * n.params[1] +
527 n.params[2] * n.params[2]);
528 cert = false;
529 break;
530 default: // kGrid or unknown: no useful bound; never pruned
531 localR = Real(1e30);
532 cert = false;
533 break;
534 }
535 nb.c = n.transform.translation; // canonical leaves are origin-centred
536 nb.r = n.transform.scale * localR;
537 nb.certified = cert;
538 return nb;
539 }
540 // CSG: children live in this node's frame.
541 const NodeBound<Real> a = nodeBound(sc, n.aux0);
542 const NodeBound<Real> b = nodeBound(sc, n.aux1);
543 NodeBound<Real> comb;
544 if (n.kind == kUnion) {
545 // enclosing sphere of the two child spheres; certified iff BOTH children are
546 const Vec3<Real> d = sub(b.c, a.c);
547 const Real dist = std::sqrt(d.x * d.x + d.y * d.y + d.z * d.z);
548 if (dist + b.r <= a.r) {
549 comb.c = a.c;
550 comb.r = a.r;
551 } else if (dist + a.r <= b.r) {
552 comb.c = b.c;
553 comb.r = b.r;
554 } else {
555 const Real R = (dist + a.r + b.r) * Real(0.5);
556 const Real t = dist > Real(0) ? (R - a.r) / dist : Real(0);
557 comb.c = Vec3<Real>{a.c.x + d.x * t, a.c.y + d.y * t, a.c.z + d.z * t};
558 comb.r = R;
559 }
560 comb.certified = a.certified && b.certified;
561 } else { // intersection / difference: the LEFT child's ball + certificate suffice
562 comb = a;
563 }
564 // apply this node's own transform to the child-frame bound
565 comb.c =
566 add(rotate(n.transform.rotation, scale(comb.c, n.transform.scale)), n.transform.translation);
567 comb.r *= n.transform.scale;
568 return comb;
569}
570
571} // namespace query_detail
572
574template <class Real>
577 if (i < 0 || i >= sc.instanceCount)
578 return ib;
579 const Instance<Real>& inst = sc.instances[i];
581 ib.c = add(rotate(inst.transform.rotation, scale(nb.c, inst.transform.scale)),
582 inst.transform.translation);
583 ib.r = inst.transform.scale * nb.r;
584 ib.certified = nb.certified && nb.r < Real(1e29);
585 return ib;
586}
587
591template <class Real>
593 const CandidateGridView<Real>& g, const Real* boundR = nullptr) {
594 Real m = Real(1e9);
595 for (int k = 0; k < g.alwaysCount; ++k) {
596 const int i = g.always[k];
597 const Real d = evalInstancePeriodic(sc, i, p, box, boundR ? boundR[i] : Real(-1));
598 if (d < m)
599 m = d;
600 }
601 const long b = g.binOf(p);
602 if (b < 0) { // out of coverage: full scan of the certified instances too
603 const Real full = evalScenePeriodic(sc, p, box, boundR);
604 return full < m ? full : m;
605 }
606 const int lo = g.offsets[b], hi = g.offsets[b + 1];
607 if (lo == hi) {
608 // An EMPTY certified list means no certified instance's band reached this bin — the far
609 // field — where a certified instance may still be the minimum. Fall back to the full scan
610 // (the always-list min in m composes via min; re-evaluating those instances is harmless).
611 // Returning only the always-list here was a bug: probes in uncovered bins got the distance
612 // to the nearest UNCERTIFIED body only.
613 const Real full = evalScenePeriodic(sc, p, box, boundR);
614 return full < m ? full : m;
615 }
616 for (int k = lo; k < hi; ++k) {
617 const int i = g.items[k];
618 const Real d = evalInstancePeriodic(sc, i, p, box, boundR ? boundR[i] : Real(-1));
619 if (d < m)
620 m = d;
621 }
622 return m;
623}
624
630template <class Real>
632 Vec3<Real> extent, const PeriodicBox<Real>& box,
633 Real binSizeHint = 0) {
635 auto& m = g.meta;
636 // default bin ~ half the median certified bounding radius (mirrors the sphere build's target)
637 std::vector<InstanceBound<Real>> ib((std::size_t)sc.instanceCount);
638 g.instBoundR.resize((std::size_t)sc.instanceCount);
639 Real rSum = 0;
640 int nCert = 0;
641 for (int i = 0; i < sc.instanceCount; ++i) {
642 ib[(std::size_t)i] = instanceBound(sc, i);
643 g.instBoundR[(std::size_t)i] = ib[(std::size_t)i].r;
644 if (ib[(std::size_t)i].certified) {
645 rSum += ib[(std::size_t)i].r;
646 ++nCert;
647 } else {
648 g.always.push_back(i);
649 }
650 }
651 const Real rMean = nCert ? rSum / nCert : extent.x / 8;
652 const Real bin = binSizeHint > 0 ? binSizeHint : std::fmax(rMean * Real(0.5), extent.x / 96);
653 auto nbAxis = [&](Real L) {
654 int nb = static_cast<int>(L / bin);
655 return nb < 2 ? 2 : (nb > 96 ? 96 : nb);
656 };
657 query_detail::layoutBins(m, origin, extent, box, nbAxis);
658 const long nbins = (long)m.nx * m.ny * m.nz;
659 const Real halfDiag = Real(0.5) * std::sqrt(m.bx * m.bx + m.by * m.by + m.bz * m.bz);
660 const Real slack = Real(1e-12) * (extent.x + extent.y + extent.z);
661
662 auto binCenter = [&](long b) {
663 const long i = b % m.nx, j = (b / m.nx) % m.ny, k = b / ((long)m.nx * m.ny);
664 return Vec3<Real>{m.ox + (i + Real(0.5)) * m.bx, m.oy + (j + Real(0.5)) * m.by,
665 m.oz + (k + Real(0.5)) * m.bz};
666 };
667 // Certified instances are 1-Lipschitz (their periodic eval is the true torus distance to the
668 // body), so at a bin center bc: upper(B) = eval(bc) + halfDiag, lower(B) = eval(bc) - halfDiag.
669 //
670 // COVERAGE MUST BE TOTAL. A bounded per-instance splat reach reproduces the far-field bug this
671 // replaced: a bin beyond instance i's reach never membership-tests i, so its (non-empty) list
672 // can miss the true argmin — measured as O(1e-2) absolute distance errors in the far field.
673 // Every (bin, certified instance) pair is therefore evaluated once, row-cached per bin:
674 // U(B) = min_i (e_i + hd), keep i iff e_i - hd <= U(B). Cost = nbins x nCertified tree walks —
675 // fine for stirrer-scale scenes (~1e6 walks); MANY-instance scenes belong on the sphere-union
676 // path, and the build() wrapper refuses to build a general grid beyond a cost cap rather than
677 // silently taking minutes.
678 std::vector<int> certIdx;
679 certIdx.reserve((std::size_t)nCert);
680 for (int i = 0; i < sc.instanceCount; ++i)
681 if (ib[(std::size_t)i].certified)
682 certIdx.push_back(i);
683 std::vector<Real> row(certIdx.size());
684 std::vector<std::vector<int>> lists((std::size_t)nbins);
685 for (long b = 0; b < nbins; ++b) {
686 const Vec3<Real> bc = binCenter(b);
687 Real U = Real(1e300);
688 for (std::size_t k = 0; k < certIdx.size(); ++k) {
689 const int i = certIdx[k];
690 row[k] = evalInstancePeriodic(sc, i, bc, box, ib[(std::size_t)i].r);
691 const Real up = row[k] + halfDiag;
692 if (up < U)
693 U = up;
694 }
695 for (std::size_t k = 0; k < certIdx.size(); ++k)
696 if (row[k] - halfDiag <= U + slack)
697 lists[(std::size_t)b].push_back(certIdx[k]);
698 }
699 query_detail::packLists(g, lists, nbins);
700 return g;
701}
702
703// ---------------------------------------------------------------------------------------------
704// Cut ownership (Layer 3 rung 1 of suite/docs/ANALYTIC_SDF_GEOMETRY.md)
705//
706// "Which body owns the surface nearest to p" — the attribution a moving-geometry solver needs to
707// read a wall velocity off the right instance, and a resolved CFD-DEM solver needs to post a
708// hydrodynamic force back to the right grain.
709//
710// DETERMINISM CONTRACT. The winner is the argmin of exactly the value `eval` returns, and ties
711// break to the LOWEST index — enforced by the comparison itself (`v < best || (v == best && i <
712// best_i)`), NOT by scan order. That matters because the same point can be answered through three
713// different orders: a candidate list, the always-list + candidate list, or the full-scan fallback.
714// All three therefore agree, and a per-bin shuffle of the candidate lists cannot change the owner.
715//
716// The equal-radius sphere path carries SQUARED center distances through the scan and converts
717// once at the end, exactly as evalSphereUnion does — so `evalOwner` returns a value BITWISE equal
718// to `eval`'s, and the argmin is the same one sqrt(.) - r0 would have chosen (it is strictly
719// increasing in d2).
720//
721// EVERY owner path is written as an eval-AND-owner primitive, with `owner()` discarding the value.
722// One traversal answers both, which is what keeps a per-cell owner field free for a consumer that
723// is sampling the scene anyway (flow's set_solid_from_scene).
724// ---------------------------------------------------------------------------------------------
725
726namespace owner_detail {
728template <class Real>
729PECLET_HD void take(Real v, int i, Real& best, int& bestI) {
730 if (v < best || (v == best && i < bestI)) {
731 best = v;
732 bestI = i;
733 }
734}
735} // namespace owner_detail
736
740template <class Real>
742 const PeriodicBox<Real>& box, const int* items, int count,
743 Real& best, int& bestI) {
744 if (u.equalR) {
745 for (int k = 0; k < count; ++k) {
746 const int i = items[k];
747 owner_detail::take(sphereDist2(u, i, p, box), i, best, bestI);
748 }
749 return;
750 }
751 for (int k = 0; k < count; ++k) {
752 const int i = items[k];
753 owner_detail::take(peclet::core::detail::hdSqrt(sphereDist2(u, i, p, box)) - u.r[i], i, best,
754 bestI);
755 }
756}
757
760template <class Real>
762 const PeriodicBox<Real>& box, int& own) {
763 Real best = Real(1e300);
764 int bestI = -1;
765 if (u.equalR) {
766 for (int i = 0; i < u.n; ++i)
767 owner_detail::take(sphereDist2(u, i, p, box), i, best, bestI);
768 own = bestI;
769 return peclet::core::detail::hdSqrt(best) - u.r0;
770 }
771 for (int i = 0; i < u.n; ++i)
772 owner_detail::take(peclet::core::detail::hdSqrt(sphereDist2(u, i, p, box)) - u.r[i], i, best,
773 bestI);
774 own = bestI;
775 return best;
776}
777
780template <class Real>
782 const PeriodicBox<Real>& box,
783 const CandidateGridView<Real>& g, int& own) {
784 const long b = g.binOf(p);
785 if (b < 0)
786 return sphereUnionEvalOwner(u, p, box, own);
787 const int lo = g.offsets[b], hi = g.offsets[b + 1];
788 if (lo == hi)
789 return sphereUnionEvalOwner(u, p, box, own);
790 Real best = Real(1e300);
791 int bestI = -1;
792 sphereUnionOwnerSubset(u, p, box, g.items + lo, hi - lo, best, bestI);
793 own = bestI;
794 return u.equalR ? peclet::core::detail::hdSqrt(best) - u.r0 : best;
795}
796
798template <class Real>
800 const PeriodicBox<Real>& box, int& own,
801 const Real* boundR = nullptr) {
802 Real best = Real(1e9);
803 int bestI = -1;
804 for (int i = 0; i < sc.instanceCount; ++i)
805 owner_detail::take(evalInstancePeriodic(sc, i, p, box, boundR ? boundR[i] : Real(-1)), i, best,
806 bestI);
807 own = bestI;
808 return best;
809}
810
814template <class Real>
816 const PeriodicBox<Real>& box, const CandidateGridView<Real>& g,
817 int& own, const Real* boundR = nullptr) {
818 Real best = Real(1e9);
819 int bestI = -1;
820 for (int k = 0; k < g.alwaysCount; ++k) {
821 const int i = g.always[k];
822 owner_detail::take(evalInstancePeriodic(sc, i, p, box, boundR ? boundR[i] : Real(-1)), i, best,
823 bestI);
824 }
825 const long b = g.binOf(p);
826 const int lo = b < 0 ? 0 : g.offsets[b], hi = b < 0 ? 0 : g.offsets[b + 1];
827 if (b < 0 || lo == hi) {
828 for (int i = 0; i < sc.instanceCount; ++i)
829 owner_detail::take(evalInstancePeriodic(sc, i, p, box, boundR ? boundR[i] : Real(-1)), i,
830 best, bestI);
831 own = bestI;
832 return best;
833 }
834 for (int k = lo; k < hi; ++k) {
835 const int i = g.items[k];
836 owner_detail::take(evalInstancePeriodic(sc, i, p, box, boundR ? boundR[i] : Real(-1)), i, best,
837 bestI);
838 }
839 own = bestI;
840 return best;
841}
842
844template <class Real>
846 const PeriodicBox<Real>& box) {
847 int own = -1;
848 (void)sphereUnionEvalOwner(u, p, box, own);
849 return own;
850}
851template <class Real>
853 const PeriodicBox<Real>& box, const CandidateGridView<Real>& g) {
854 int own = -1;
855 (void)sphereUnionEvalOwnerGrid(u, p, box, g, own);
856 return own;
857}
858template <class Real>
860 const PeriodicBox<Real>& box, const Real* boundR = nullptr) {
861 int own = -1;
862 (void)sceneEvalOwnerPeriodic(sc, p, box, own, boundR);
863 return own;
864}
865template <class Real>
867 const CandidateGridView<Real>& g, const Real* boundR = nullptr) {
868 int own = -1;
869 (void)sceneEvalOwnerGrid(sc, p, box, g, own, boundR);
870 return own;
871}
872
873// ---------------------------------------------------------------------------------------------
874// Rigid-body instance motion (Layer 3 rung 2 of suite/docs/ANALYTIC_SDF_GEOMETRY.md)
875//
876// The kinematic half of moving geometry: given the owner of a wall point (above), what velocity
877// does that wall carry? Kept here, beside the query it pairs with, so every consumer -- flow's
878// staggered momentum operator today, the AMR solver when its campaign resumes -- reads the same
879// rigid-body law instead of re-deriving it.
880// ---------------------------------------------------------------------------------------------
881
886template <class Real>
888 const Real* cen = nullptr; // 3*n world centres of rotation
889 const Real* lin = nullptr; // 3*n linear velocities
890 const Real* ang = nullptr; // 3*n angular velocities
891 int n = 0;
892};
893
902template <class Real>
904 const PeriodicBox<Real>& box) {
905 Vec3<Real> v{0, 0, 0};
906 if (i < 0 || i >= m.n)
907 return v;
908 if (m.lin) {
909 v.x = m.lin[3 * i + 0];
910 v.y = m.lin[3 * i + 1];
911 v.z = m.lin[3 * i + 2];
912 }
913 if (!m.ang || !m.cen)
914 return v;
915 const Real wx = m.ang[3 * i + 0], wy = m.ang[3 * i + 1], wz = m.ang[3 * i + 2];
916 if (wx == Real(0) && wy == Real(0) && wz == Real(0))
917 return v;
918 const Vec3<Real> r = minImage(
919 Vec3<Real>{p.x - m.cen[3 * i + 0], p.y - m.cen[3 * i + 1], p.z - m.cen[3 * i + 2]}, box);
920 v.x += wy * r.z - wz * r.y;
921 v.y += wz * r.x - wx * r.z;
922 v.z += wx * r.y - wy * r.x;
923 return v;
924}
925
933template <class Real>
935 namespace cd = peclet::core::detail;
936 const Real g2 = cd::hdFma(grad.z, grad.z, cd::hdFma(grad.y, grad.y, grad.x * grad.x));
937 if (!(g2 > Real(0)))
938 return p;
939 const Real s = sdf / cd::hdSqrt(g2);
940 return Vec3<Real>{p.x - s * grad.x, p.y - s * grad.y, p.z - s * grad.z};
941}
942
945template <class Real>
947 if (m.n <= 0)
948 return true;
949 for (int i = 0; i < 3 * m.n; ++i) {
950 if (m.lin && m.lin[i] != Real(0))
951 return false;
952 if (m.ang && m.ang[i] != Real(0))
953 return false;
954 }
955 return true;
956}
957
963template <class Real>
965 SphereUnionView<Real> u{}; // u.n > 0 => sphere fast path
966 SceneView<Real> scene{}; // otherwise: general scene
967 CandidateGridView<Real> grid{}; // offsets == nullptr => no acceleration
969 const Real* instBoundR = nullptr; // per-instance bounds (general periodic seam handling)
970
972 if (u.n > 0)
973 return grid.offsets ? evalSphereUnionGrid(u, p, box, grid) : evalSphereUnion(u, p, box);
974 return grid.offsets ? evalSceneGrid(scene, p, box, grid, instBoundR)
976 }
977
982 PECLET_HD Real evalOwner(Vec3<Real> p, int& own) const {
983 if (u.n > 0)
984 return grid.offsets ? sphereUnionEvalOwnerGrid(u, p, box, grid, own)
985 : sphereUnionEvalOwner(u, p, box, own);
986 return grid.offsets ? sceneEvalOwnerGrid(scene, p, box, grid, own, instBoundR)
988 }
989
992 int own = -1;
993 (void)evalOwner(p, own);
994 return own;
995 }
996};
997
1001template <class Real>
1002inline bool extractSphereUnion(const SceneView<Real>& sc, std::vector<Real>& cx,
1003 std::vector<Real>& cy, std::vector<Real>& cz, std::vector<Real>& r) {
1004 cx.clear();
1005 cy.clear();
1006 cz.clear();
1007 r.clear();
1008 auto identity = [](const Transform<Real>& t, bool allowTranslate) {
1009 const bool q = t.rotation.x == Real(0) && t.rotation.y == Real(0) && t.rotation.z == Real(0) &&
1010 t.rotation.w == Real(1) && t.scale == Real(1);
1011 const bool tr = allowTranslate || (t.translation.x == Real(0) && t.translation.y == Real(0) &&
1012 t.translation.z == Real(0));
1013 return q && tr;
1014 };
1015 for (int i = 0; i < sc.instanceCount; ++i) {
1016 const Instance<Real>& inst = sc.instances[i];
1017 if (inst.shapeRoot < 0 || inst.shapeRoot >= sc.nodeCount)
1018 return false;
1019 const ShapeNode<Real>& n = sc.nodes[inst.shapeRoot];
1020 if (n.kind != kSphere || !identity(n.transform, false) || !identity(inst.transform, true))
1021 return false;
1022 cx.push_back(inst.transform.translation.x);
1023 cy.push_back(inst.transform.translation.y);
1024 cz.push_back(inst.transform.translation.z);
1025 r.push_back(n.params[0]);
1026 }
1027 return !cx.empty();
1028}
1029
1030// ---------------------------------------------------------------------------------------------
1031// The callable that retires the AMR stopgap
1032// ---------------------------------------------------------------------------------------------
1033
1038 public:
1039 SphereBedQuery(std::vector<double> cx, std::vector<double> cy, std::vector<double> cz,
1040 std::vector<double> r, Vec<3> origin, Vec<3> extent, bool periodic, int nbHint = 0)
1041 : cx_(std::move(cx)), cy_(std::move(cy)), cz_(std::move(cz)), r_(std::move(r)) {
1042 const int n = static_cast<int>(cx_.size());
1043 if (n == 0 || cy_.size() != cx_.size() || cz_.size() != cx_.size() || r_.size() != cx_.size())
1044 throw std::invalid_argument("SphereBedQuery: centers/radii sizes disagree");
1045 box_.on = periodic;
1046 box_.Lx = extent[0];
1047 box_.Ly = extent[1];
1048 box_.Lz = extent[2];
1049 union_.cx = cx_.data();
1050 union_.cy = cy_.data();
1051 union_.cz = cz_.data();
1052 union_.r = r_.data();
1053 union_.n = n;
1054 union_.equalR = true;
1055 for (int i = 1; i < n; ++i)
1056 if (r_[i] != r_[0])
1057 union_.equalR = false;
1058 union_.r0 = r_[0];
1059 grid_ = buildSphereCandidateGrid(union_, Vec3<double>{origin[0], origin[1], origin[2]},
1060 Vec3<double>{extent[0], extent[1], extent[2]}, box_, nbHint);
1061 gview_ = grid_.view();
1062 }
1063
1064 double operator()(const Vec<3>& p) const {
1065 return evalSphereUnionGrid(union_, Vec3<double>{p[0], p[1], p[2]}, box_, gview_);
1066 }
1067
1068 const SphereUnionView<double>& sphereUnion() const { return union_; }
1069 const PeriodicBox<double>& box() const { return box_; }
1070 const CandidateGridView<double>& grid() const { return gview_; }
1072 double meanListLength() const {
1073 return grid_.offsets.empty() ? 0.0
1074 : double(grid_.items.size()) / double(grid_.offsets.size() - 1);
1075 }
1076
1077 private:
1078 std::vector<double> cx_, cy_, cz_, r_;
1080 PeriodicBox<double> box_{};
1081 CandidateGrid<double> grid_;
1082 CandidateGridView<double> gview_{};
1083};
1084
1085} // namespace peclet::core::geom
1086
1087#endif // PECLET_CORE_GEOM_SCENE_QUERY_HPP
const SphereUnionView< double > & sphereUnion() const
double meanListLength() const
Mean candidate-list length (diagnostics for the acceptance report).
SphereBedQuery(std::vector< double > cx, std::vector< double > cy, std::vector< double > cz, std::vector< double > r, Vec< 3 > origin, Vec< 3 > extent, bool periodic, int nbHint=0)
double operator()(const Vec< 3 > &p) const
const PeriodicBox< double > & box() const
const CandidateGridView< double > & grid() const
PECLET_HD Real hdFloor(Real v)
Definition portable.hpp:167
PECLET_HD Real hdSqrt(Real v)
Definition portable.hpp:143
PECLET_HD void take(Real v, int i, Real &best, int &bestI)
The single tie-break rule (lowest index wins an exact tie), shared by every owner path.
void forEachBinNearSphere(const CandidateGridView< Real > m, const SphereUnionView< Real > u, int i, Real reach, Fn &&fn)
The bin neighbourhood sphere i of u splats: bins of m whose center lies within reach of the sphere ce...
void packLists(CandidateGrid< Real > &g, const std::vector< std::vector< int > > &lists, long nbins)
The per-bin candidate lists packed into the grid's CSR (offsets, items).
NodeBound< Real > nodeBound(const SceneView< Real > &sc, int node)
void layoutBins(CandidateGridView< Real > &m, Vec3< Real > origin, Vec3< Real > extent, const PeriodicBox< Real > &box, NbAxis &&nbAxis)
Bin layout of a candidate grid over [origin, origin + extent): nbAxis(L) bins along an axis of length...
PECLET_HD Real evalSphereUnionGrid(const SphereUnionView< Real > &u, Vec3< Real > p, const PeriodicBox< Real > &box, const CandidateGridView< Real > &g)
Union SDF through the candidate grid: bin lookup + short list, full-scan fallback when the bin is out...
PECLET_HD Real sphereUnionEvalOwner(const SphereUnionView< Real > &u, Vec3< Real > p, const PeriodicBox< Real > &box, int &own)
Brute-force value + owner (all spheres) — the reference the candidate path must match,...
PECLET_HD int sphereUnionOwner(const SphereUnionView< Real > &u, Vec3< Real > p, const PeriodicBox< Real > &box)
Owner-only wrappers (the value is discarded by the compiler on every backend).
PECLET_HD Real evalSceneGrid(const SceneView< Real > &sc, Vec3< Real > p, const PeriodicBox< Real > &box, const CandidateGridView< Real > &g, const Real *boundR=nullptr)
Union over a candidate list + the always-list, min-image periodic.
CandidateGrid< Real > buildSceneCandidateGrid(const SceneView< Real > &sc, Vec3< Real > origin, Vec3< Real > extent, const PeriodicBox< Real > &box, Real binSizeHint=0)
Candidate grid for a GENERAL scene: certified instances splat Lipschitz bounds evaluated at bin cente...
PECLET_HD Real sphereDist2(const SphereUnionView< Real > &u, int i, Vec3< Real > p, const PeriodicBox< Real > &box)
Squared min-image distance from p to sphere i — THE per-sphere expression, shared by every path (brut...
bool extractSphereUnion(const SceneView< Real > &sc, std::vector< Real > &cx, std::vector< Real > &cy, std::vector< Real > &cz, std::vector< Real > &r)
Try to view a scene as a PLAIN sphere union: every instance a single kSphere leaf with a bitwise-iden...
InstanceBound< Real > instanceBound(const SceneView< Real > &sc, int i)
World bounding sphere + certificate of one instance (host-side; used by candidate builds).
PECLET_HD void sphereUnionOwnerSubset(const SphereUnionView< Real > &u, Vec3< Real > p, const PeriodicBox< Real > &box, const int *items, int count, Real &best, int &bestI)
Accumulate the argmin over an explicit index subset (a candidate list) into (best,...
PECLET_HD Real sphereUnionEvalOwnerGrid(const SphereUnionView< Real > &u, Vec3< Real > p, const PeriodicBox< Real > &box, const CandidateGridView< Real > &g, int &own)
Value + owner through the candidate grid.
bool motionIsStatic(const InstanceMotionView< Real > &m)
True when no instance carries motion – the predicate a consumer uses to keep the static path bit-iden...
@ kSphere
params[0] = radius
Definition scene.hpp:37
@ kEllipsoid
params[0..2] = rx, ry, rz (BOUND, under-estimates)
Definition scene.hpp:44
@ kTorus
params[0..1] = majorRadius, minorRadius (exact, y axis)
Definition scene.hpp:42
@ kBox
params[0..2] = hx, hy, hz
Definition scene.hpp:39
@ kSuperquadric
params[0..3] = rx, ry, rz, exponent (BOUND, under-estimates)
Definition scene.hpp:45
@ kHollowCylinderShell
params[0..2] = rOuter, rInner, height (sign-exact, z axis)
Definition scene.hpp:40
@ kCapsule
params[0..1] = radius, halfLength (exact, y axis)
Definition scene.hpp:41
@ kCone
params[0..2] = rBottom, rTop, halfHeight (exact, y axis)
Definition scene.hpp:43
@ kHollowCylinder
params[0..2] = rOuter, height, thickness (distance-exact, y axis)
Definition scene.hpp:38
@ kUnion
min(L, R) — aux0/aux1 = child node indices
Definition scene.hpp:48
PECLET_HD Real evalScenePeriodic(const SceneView< Real > &sc, Vec3< Real > p, const PeriodicBox< Real > &box, const Real *boundR=nullptr)
Union over every instance, min-image periodic.
PECLET_HD Real evalInstancePeriodic(const SceneView< Real > &sc, int i, Vec3< Real > p, const PeriodicBox< Real > &box, Real boundR=Real(-1))
Instance evaluation with MIN-IMAGE periodicity.
PECLET_HD Vec3< Real > instanceVelocity(const Instance< Real > &inst, Vec3< Real > p)
Definition scene.hpp:219
PECLET_HD Real sceneEvalOwnerPeriodic(const SceneView< Real > &sc, Vec3< Real > p, const PeriodicBox< Real > &box, int &own, const Real *boundR=nullptr)
Value + owner over every instance of a general scene, min-image periodic.
PECLET_HD Vec3< Real > wallPoint(Vec3< Real > p, Real sdf, Vec3< Real > grad)
Project a probe onto the zero level set along the local gradient: w = p - sdf(p) * n_hat.
PECLET_HD int sceneOwnerPeriodic(const SceneView< Real > &sc, Vec3< Real > p, const PeriodicBox< Real > &box, const Real *boundR=nullptr)
PECLET_HD int sphereUnionOwnerGrid(const SphereUnionView< Real > &u, Vec3< Real > p, const PeriodicBox< Real > &box, const CandidateGridView< Real > &g)
PECLET_HD Vec3< Real > minImage(Vec3< Real > d, const PeriodicBox< Real > &b)
Min-image a displacement.
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).
Definition scene.hpp:254
PECLET_HD Real evalSphereUnion(const SphereUnionView< Real > &u, Vec3< Real > p, const PeriodicBox< Real > &box)
Brute-force union SDF (all spheres) — the reference the candidate path must match bit-for-bit,...
PECLET_HD Real evalSphereUnionSubset(const SphereUnionView< Real > &u, Vec3< Real > p, const PeriodicBox< Real > &box, const int *items, int count)
Union SDF over an index subset (a candidate list).
PECLET_HD int sceneOwnerGrid(const SceneView< Real > &sc, Vec3< Real > p, const PeriodicBox< Real > &box, const CandidateGridView< Real > &g, const Real *boundR=nullptr)
PECLET_HD Real sceneEvalOwnerGrid(const SceneView< Real > &sc, Vec3< Real > p, const PeriodicBox< Real > &box, const CandidateGridView< Real > &g, int &own, const Real *boundR=nullptr)
Value + owner through a general-scene candidate grid: always-list ∪ candidate list,...
CandidateGrid< Real > buildSphereCandidateGrid(const SphereUnionView< Real > &u, Vec3< Real > origin, Vec3< Real > extent, const PeriodicBox< Real > &box, int nbHint=0)
Geometry-driven candidate build for a sphere union over [origin, origin+extent).
double Real
Default host floating type. Device kernels may use float; conversions happen at the boundary.
Definition types.hpp:18
PECLET_HD Vec3< Real > rotate(Quat< Real > q, Vec3< Real > v)
Rotate v by q.
Definition portable.hpp:81
std::array< Real, Dim > Vec
Multi-dimensional real vector.
Definition types.hpp:26
PECLET_HD Vec3< Real > add(Vec3< Real > a, Vec3< Real > b)
Definition portable.hpp:58
PECLET_HD Vec3< Real > invRotate(Quat< Real > q, Vec3< Real > v)
Rotate v by the conjugate of q — dem invRotateVector.
Definition portable.hpp:89
PECLET_HD Vec3< Real > sub(Vec3< Real > a, Vec3< Real > b)
Definition portable.hpp:62
PECLET_HD Vec3< Real > scale(Vec3< Real > a, Real s)
Definition portable.hpp:66
#define PECLET_HD
Definition portable.hpp:35
Real-templated POD 3-vector — the geometry layer's point type on host and device.
Definition portable.hpp:45
Non-owning candidate grid: uniform bins over [origin, origin+extent), CSR lists of primitive (or inst...
PECLET_HD long binOf(Vec3< Real > p) const
Flat bin index of a point, or -1 when the point is outside a non-periodic grid's coverage (the caller...
Owning host-side candidate grid.
CandidateGridView< Real > meta
CandidateGridView< Real > view() const
World bounding sphere of an instance + the PRUNING CERTIFICATE.
Non-owning, POD, device-copyable rigid-body motion state for a scene's instances.
One placed copy of a shape tree (contract 7).
Definition scene.hpp:199
Transform< Real > transform
Definition scene.hpp:202
int shapeRoot
index of the root ShapeNode of this instance's tree
Definition scene.hpp:200
Min-image box. on = false disables wrapping (all extents ignored).
Adaptors so a raw pointer can stand in for a Kokkos View (Views already provide operator()),...
Definition scene.hpp:113
The one POD every consumer captures: sphere-union fast path when the scene is a plain sphere union,...
PECLET_HD int owner(Vec3< Real > p) const
Owner only.
PECLET_HD Real evalOwner(Vec3< Real > p, int &own) const
eval(p) AND the index of the instance (sphere-union mode: the sphere) whose surface is nearest — the ...
PECLET_HD Real eval(Vec3< Real > p) const
CandidateGridView< Real > grid
Non-owning bundle of everything a scene evaluation needs (resolved decision 4).
Definition scene.hpp:229
const Instance< Real > * instances
Definition scene.hpp:236
const GridDesc< Real > * grids
Definition scene.hpp:232
const ShapeNode< Real > * nodes
Definition scene.hpp:230
One node of a flat shape tree. POD, trivially copyable, safe in a Kokkos View.
Definition scene.hpp:101
int aux0
CSG: left child index. kGrid: GridDesc table index.
Definition scene.hpp:103
Transform< Real > transform
Definition scene.hpp:106
int aux1
CSG: right child index.
Definition scene.hpp:104
Non-owning union-of-spheres view.
Conformal placement: canonical -> parent frame. Identity by default.
Definition scene.hpp:85