53#ifndef PECLET_CORE_GEOM_SCENE_QUERY_HPP
54#define PECLET_CORE_GEOM_SCENE_QUERY_HPP
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);
119 return cd::hdFma(d.
z, d.
z, cd::hdFma(d.
y, d.
y, d.
x * d.
x));
131 for (
int k = 0; k < count; ++k) {
139 for (
int k = 0; k < count; ++k) {
140 const int i = items[k];
155 for (
int i = 0; i < u.
n; ++i) {
163 for (
int i = 0; i < u.
n; ++i) {
198 }
else if (i < 0 || i >=
nx || j < 0 || j >=
ny || k < 0 || k >=
nz) {
201 return i + (long)
nx * (j + (
long)
ny * k);
211 const long b = g.
binOf(p);
241template <
class Real,
class NbAxis>
244 m.
nx = nbAxis(extent.
x);
245 m.
ny = nbAxis(extent.
y);
246 m.
nz = nbAxis(extent.
z);
250 m.
bx = extent.
x / m.
nx;
251 m.
by = extent.
y / m.
ny;
252 m.
bz = extent.
z / m.
nz;
259 g.
offsets.resize((std::size_t)nbins + 1);
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(),
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;
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;
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) {
293 fn(ii + (
long)m.
nx * (jj + (
long)m.
ny * kk));
313 throw std::invalid_argument(
"buildSphereCandidateGrid: empty union");
315 for (
int i = 0; i < u.
n; ++i)
318 auto nbAxis = [&](
Real L) {
321 int nb = rMean > 0 ?
static_cast<int>(L / (rMean *
Real(0.5))) : 16;
322 return nb < 2 ? 2 : (nb > 96 ? 96 : nb);
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);
331 const Real slack =
Real(1e-12) * (extent.
x + extent.
y + extent.
z);
334 const long i = b % m.nx, j = (b / m.nx) % m.ny, k = b / ((
long)m.nx * m.ny);
336 m.oz + (k +
Real(0.5)) * m.bz};
340 return std::sqrt(d.
x * d.
x + d.
y * d.
y + d.
z * d.
z);
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)
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;
363 if (v <
Real(1e300) && v > Umax)
365 std::vector<std::vector<int>> lists((std::size_t)nbins);
366 for (
int i = 0; i < u.
n; ++i)
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);
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) {
415 boundR <
Real(0) || (da[a] <
Real(0) ? -da[a] : da[a]) > Ls[a] *
Real(0.5) - boundR;
417 alt[a][1] = da[a] <
Real(0) ? da[a] + Ls[a] : da[a] - Ls[a];
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]};
497 localR = std::sqrt(ro * ro + h2 * h2);
511 localR = std::sqrt(rb * rb + hh * hh);
517 localR = std::sqrt(ro * ro + h2 * h2);
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) {
551 }
else if (dist + a.
r <= b.
r) {
555 const Real R = (dist + a.
r + b.
r) *
Real(0.5);
596 const int i = g.
always[k];
601 const long b = g.
binOf(p);
604 return full < m ? full : m;
614 return full < m ? full : m;
616 for (
int k = lo; k < hi; ++k) {
617 const int i = g.
items[k];
633 Real binSizeHint = 0) {
637 std::vector<InstanceBound<Real>> ib((std::size_t)sc.
instanceCount);
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;
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);
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);
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);
665 m.oz + (k +
Real(0.5)) * m.bz};
678 std::vector<int> certIdx;
679 certIdx.reserve((std::size_t)nCert);
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) {
688 for (std::size_t k = 0; k < certIdx.size(); ++k) {
689 const int i = certIdx[k];
691 const Real up = row[k] + halfDiag;
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]);
730 if (v < best || (v == best && i < bestI)) {
743 Real& best,
int& bestI) {
745 for (
int k = 0; k < count; ++k) {
746 const int i = items[k];
751 for (
int k = 0; k < count; ++k) {
752 const int i = items[k];
766 for (
int i = 0; i < u.
n; ++i)
771 for (
int i = 0; i < u.
n; ++i)
784 const long b = g.
binOf(p);
801 const Real* boundR =
nullptr) {
817 int& own,
const Real* boundR =
nullptr) {
821 const int i = g.
always[k];
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) {
834 for (
int k = lo; k < hi; ++k) {
835 const int i = g.
items[k];
906 if (i < 0 || i >= m.
n)
909 v.
x = m.
lin[3 * i + 0];
910 v.
y = m.
lin[3 * i + 1];
911 v.
z = m.
lin[3 * i + 2];
915 const Real wx = m.
ang[3 * i + 0], wy = m.
ang[3 * i + 1], wz = m.
ang[3 * i + 2];
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;
936 const Real g2 = cd::hdFma(grad.
z, grad.
z, cd::hdFma(grad.
y, grad.
y, grad.
x * grad.
x));
939 const Real s = sdf / cd::hdSqrt(g2);
949 for (
int i = 0; i < 3 * m.
n; ++i) {
1001template <
class Real>
1003 std::vector<Real>& cy, std::vector<Real>& cz, std::vector<Real>& r) {
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]);
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");
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();
1054 union_.equalR =
true;
1055 for (
int i = 1; i < n; ++i)
1057 union_.equalR =
false;
1060 Vec3<double>{extent[0], extent[1], extent[2]}, box_, nbHint);
1061 gview_ = grid_.view();
1073 return grid_.offsets.empty() ? 0.0
1074 : double(grid_.items.size()) / double(grid_.offsets.size() - 1);
1078 std::vector<double> cx_, cy_, cz_, r_;
1081 CandidateGrid<double> grid_;
1082 CandidateGridView<double> gview_{};
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)
PECLET_HD Real hdSqrt(Real v)
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
@ 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)
@ 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 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)
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).
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.
PECLET_HD Vec3< Real > rotate(Quat< Real > q, Vec3< Real > v)
Rotate v by q.
std::array< Real, Dim > Vec
Multi-dimensional real vector.
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)
Real-templated POD 3-vector — the geometry layer's point type on host and device.
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
std::vector< Real > instBoundR
std::vector< int > offsets
std::vector< int > always
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).
Transform< Real > transform
int shapeRoot
index of the root ShapeNode of this instance's tree
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()),...
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.
SphereUnionView< Real > u
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).
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.
Non-owning union-of-spheres view.