peclet.voro 1.0.4
Device-native moving-particle Voronoi dynamics
Loading...
Searching...
No Matches
convex_cell.hpp
Go to the documentation of this file.
1
24#ifndef PECLET_VORO_CONVEX_CELL_HPP
25#define PECLET_VORO_CONVEX_CELL_HPP
26
27#include <Kokkos_Core.hpp>
28
29#include "peclet/voro/params.hpp" // kMaxPlanes / kMaxTriangles: the capacity defaults
30#include "peclet/voro/plane_policy.hpp" // Voronoi/Power plane-from-DOF policies (leaf: Kokkos only)
31
32// Loop unroll hint, applied ONLY in the CUDA/HIP device passes (where it lets the compiler
33// scalarize small dynamically-indexed local arrays into registers); a no-op on the host so gcc sees
34// no unknown pragma.
35#if defined(__CUDA_ARCH__) || defined(__HIP_DEVICE_COMPILE__)
36#define VOR_UNROLL _Pragma("unroll")
37#else
38#define VOR_UNROLL
39#endif
40
41namespace peclet::voro {
42
43namespace detail {
44// Minimal forward-mode dual number (value + K partials) used by ConvexCell::geomVolumeAreaGrad (the
45// geomFull area-Jacobian). Templated on Real so it routes through nvcc/hipcc like the rest of the
46// header.
47template <class Real, int K>
48struct Dual {
49 Real v;
50 Real d[K];
51};
52template <class Real, int K>
53KOKKOS_INLINE_FUNCTION Dual<Real, K> dnum(Real c) {
55 r.v = c;
56 for (int i = 0; i < K; ++i)
57 r.d[i] = Real(0);
58 return r;
59}
60template <class Real, int K>
61KOKKOS_INLINE_FUNCTION Dual<Real, K> dseed(Real c, int s) {
63 r.d[s] = Real(1);
64 return r;
65}
66template <class Real, int K>
69 r.v = a.v + b.v;
70 for (int i = 0; i < K; ++i)
71 r.d[i] = a.d[i] + b.d[i];
72 return r;
73}
74template <class Real, int K>
77 r.v = a.v - b.v;
78 for (int i = 0; i < K; ++i)
79 r.d[i] = a.d[i] - b.d[i];
80 return r;
81}
82template <class Real, int K>
85 r.v = a.v * b.v;
86 for (int i = 0; i < K; ++i)
87 r.d[i] = a.d[i] * b.v + a.v * b.d[i];
88 return r;
89}
90template <class Real, int K>
93 const Real inv = Real(1) / b.v;
94 r.v = a.v * inv;
95 for (int i = 0; i < K; ++i)
96 r.d[i] = (a.d[i] - r.v * b.d[i]) * inv;
97 return r;
98}
99template <class Real, int K>
102 r.v = Kokkos::sqrt(a.v);
103 const Real inv = (r.v > Real(0)) ? Real(1) / (Real(2) * r.v) : Real(0);
104 for (int i = 0; i < K; ++i)
105 r.d[i] = a.d[i] * inv;
106 return r;
107}
108template <class Real, int K>
110 return a[0] * b[0] + a[1] * b[1] + a[2] * b[2];
111}
112template <class Real, int K>
114 Dual<Real, K> o[3]) {
115 o[0] = a[1] * b[2] - a[2] * b[1];
116 o[1] = a[2] * b[0] - a[0] * b[2];
117 o[2] = a[0] * b[1] - a[1] * b[0];
118}
119template <class Real, int K>
121 const Dual<Real, K> c[3]) {
122 Dual<Real, K> bc[3];
123 dcross(b, c, bc);
124 return ddot(a, bc);
125}
126template <class Real, int K>
128 Dual<Real, K> f[3]) {
129 const Dual<Real, K> s = ddot(v, c) / ddot(c, c);
130 f[0] = v[0] - s * c[0];
131 f[1] = v[1] - s * c[1];
132 f[2] = v[2] - s * c[2];
133}
134} // namespace detail
135
139template <class Real, int MAXP = kMaxPlanes, int MAXT = kMaxTriangles, bool TrackAdj = false>
141 static_assert(MAXP <= 255, "plane index must fit in unsigned char");
142 static constexpr int kMaxP = MAXP;
143 static constexpr int kMaxT = MAXT;
144 static constexpr bool kTrackAdj =
145 TrackAdj;
152 Real n[MAXP][3];
153 Real nn[MAXP];
154 int pnbr[MAXP];
155 int np;
156 unsigned char t0[MAXT], t1[MAXT], t2[MAXT];
157 Real vx[MAXT], vy[MAXT], vz[MAXT];
158 bool alive[MAXT];
159 int nt;
160 bool overflow;
164
178 Kokkos::Array<int, (TrackAdj ? MAXT : 0) * 3> adj;
179
182 KOKKOS_INLINE_FUNCTION void initBox(Real L0, Real L1, Real L2) {
183 const Real h[3] = {Real(0.5) * L0, Real(0.5) * L1, Real(0.5) * L2};
184 for (int ax = 0; ax < 3; ++ax) {
185 for (int s = 0; s < 2; ++s) {
186 const int k = 2 * ax + s;
187 n[k][0] = n[k][1] = n[k][2] = 0;
188 n[k][ax] = (s == 0) ? h[ax] : -h[ax]; // foot point = (±h) e_ax
189 nn[k] = h[ax] * h[ax];
190 pnbr[k] = -1;
191 }
192 }
193 np = 6;
194 nt = 0;
195 overflow = false;
196 degenerate = false;
197 // 8 corners: x-plane (0|1), y-plane (2|3), z-plane (4|5)
198 for (int sx = 0; sx < 2; ++sx)
199 for (int sy = 0; sy < 2; ++sy)
200 for (int sz = 0; sz < 2; ++sz) {
201 t0[nt] = (unsigned char)(0 + sx);
202 t1[nt] = (unsigned char)(2 + sy);
203 t2[nt] = (unsigned char)(4 + sz);
204 alive[nt] = true;
206 ++nt;
207 }
208 if constexpr (TrackAdj)
209 rebuildAdjacency(); // one-time bootstrap (~12 box triangles)
210 }
211
217 KOKKOS_INLINE_FUNCTION void setHalfSpace(int k, const Real p[3], Real d) {
218 const Real l2 = p[0] * p[0] + p[1] * p[1] + p[2] * p[2];
219 if (!(l2 > Real(0))) {
220 const Real a = Real(0);
221 n[k][0] = a * p[0];
222 n[k][1] = a * p[1];
223 n[k][2] = a * p[2];
224 nn[k] = n[k][0] * n[k][0] + n[k][1] * n[k][1] + n[k][2] * n[k][2];
225 return;
226 }
227 if (d == Real(0))
228 d = Kokkos::Experimental::epsilon_v<Real> * l2;
229 if (d > Real(0)) {
230 const Real a = d / l2;
231 n[k][0] = a * p[0];
232 n[k][1] = a * p[1];
233 n[k][2] = a * p[2];
234 nn[k] = n[k][0] * n[k][0] + n[k][1] * n[k][1] + n[k][2] * n[k][2];
235 } else { // the seed is outside this half-space
236 const Real a = (-d) / l2;
237 n[k][0] = a * p[0];
238 n[k][1] = a * p[1];
239 n[k][2] = a * p[2];
240 nn[k] = -(n[k][0] * n[k][0] + n[k][1] * n[k][1] + n[k][2] * n[k][2]);
241 }
242 }
244 KOKKOS_INLINE_FUNCTION void footPoint(int k, Real out[3]) const {
245 if (nn[k] < Real(0)) {
246 out[0] = -n[k][0];
247 out[1] = -n[k][1];
248 out[2] = -n[k][2];
249 } else {
250 out[0] = n[k][0];
251 out[1] = n[k][1];
252 out[2] = n[k][2];
253 }
254 }
256 KOKKOS_INLINE_FUNCTION Real sigmaOf(int k) const { return nn[k] < Real(0) ? Real(-1) : Real(1); }
258 KOKKOS_INLINE_FUNCTION Real planeDistance(int k) const {
259 return Kokkos::sqrt(Kokkos::fabs(nn[k]));
260 }
262 KOKKOS_INLINE_FUNCTION Real signedSupport(int k) const {
263 return nn[k] >= Real(0) ? Kokkos::sqrt(nn[k]) : -Kokkos::sqrt(-nn[k]);
264 }
265
268 KOKKOS_INLINE_FUNCTION bool anyLiveNegOffset() const {
269 bool anyNeg = false; // O(np) pre-check: no negative plane at all is the common case
270 for (int k = 0; k < np; ++k)
271 anyNeg = anyNeg || nn[k] < Real(0);
272 if (!anyNeg)
273 return false;
274 for (int t = 0; t < nt; ++t)
275 if (alive[t] && (nn[t0[t]] < Real(0) || nn[t1[t]] < Real(0) || nn[t2[t]] < Real(0)))
276 return true;
277 return false;
278 }
279
284 KOKKOS_INLINE_FUNCTION void computeVertex(int t) {
285 const Real* a = n[t0[t]];
286 const Real* b = n[t1[t]];
287 const Real* c = n[t2[t]];
288 const Real da = nn[t0[t]], db = nn[t1[t]], dc = nn[t2[t]];
289 const Real bc[3] = {b[1] * c[2] - b[2] * c[1], b[2] * c[0] - b[0] * c[2],
290 b[0] * c[1] - b[1] * c[0]};
291 const Real ca[3] = {c[1] * a[2] - c[2] * a[1], c[2] * a[0] - c[0] * a[2],
292 c[0] * a[1] - c[1] * a[0]};
293 const Real ab[3] = {a[1] * b[2] - a[2] * b[1], a[2] * b[0] - a[0] * b[2],
294 a[0] * b[1] - a[1] * b[0]};
295 const Real det = a[0] * bc[0] + a[1] * bc[1] + a[2] * bc[2];
296 const Real inv = det != Real(0) ? Real(1) / det : Real(0);
297 vx[t] = (da * bc[0] + db * ca[0] + dc * ab[0]) * inv;
298 vy[t] = (da * bc[1] + db * ca[1] + dc * ab[1]) * inv;
299 vz[t] = (da * bc[2] + db * ca[2] + dc * ab[2]) * inv;
300 }
301
306 KOKKOS_INLINE_FUNCTION void initBoxPlanes(Real L0, Real L1, Real L2) {
307 const Real h[3] = {Real(0.5) * L0, Real(0.5) * L1, Real(0.5) * L2};
308 for (int ax = 0; ax < 3; ++ax)
309 for (int s = 0; s < 2; ++s) {
310 const int k = 2 * ax + s;
311 n[k][0] = n[k][1] = n[k][2] = 0;
312 n[k][ax] = (s == 0) ? h[ax] : -h[ax]; // foot point = (±h) e_ax
313 nn[k] = h[ax] * h[ax];
314 pnbr[k] = -1;
315 }
316 np = 6;
317 overflow = false;
318 degenerate = false;
319 }
320
334 template <class Policy = Voronoi>
335 KOKKOS_INLINE_FUNCTION void reevalGeometry(Real sx, Real sy, Real sz, const Real* pos, Real L,
336 Real wSelf = Real(0), const Real* weight = nullptr) {
337 const Real Lh = Real(0.5) * L;
338 for (int k = 0; k < np; ++k) {
339 if (pnbr[k] < 0)
340 continue; // bounding-box / SDF-wall plane — does not move
341 const int g = pnbr[k];
342 Real rx = pos[3 * g] - sx, ry = pos[3 * g + 1] - sy, rz = pos[3 * g + 2] - sz;
343 rx = rx > Lh ? rx - L : (rx < -Lh ? rx + L : rx);
344 ry = ry > Lh ? ry - L : (ry < -Lh ? ry + L : ry);
345 rz = rz > Lh ? rz - L : (rz < -Lh ? rz + L : rz);
346 if constexpr (Policy::kHasWeightDof) {
347 // radical plane {x : r·x <= d}, d = ½(|r|² + w_self − w_nbr). d > 0 (the seed inside):
348 // the legacy foot-point expressions verbatim, so nvcc contracts them as before (G6 on
349 // CUDA); setHalfSpace's d > 0 branch is the same arithmetic. d <= 0: sign-safe overwrite.
350 const Real r[3] = {rx, ry, rz};
351 const Real rho = rx * rx + ry * ry + rz * rz;
352 const Real off = Policy::template offsetFromRel<Real>(r, wSelf, weight[g]);
353 const Real a = rho > Real(0) ? off / rho : Real(0);
354 n[k][0] = a * rx;
355 n[k][1] = a * ry;
356 n[k][2] = a * rz;
357 nn[k] = n[k][0] * n[k][0] + n[k][1] * n[k][1] + n[k][2] * n[k][2];
358 if (!(off > Real(0)))
359 setHalfSpace(k, r, off);
360 } else {
361 // foot point of the Voronoi bisector: n = ½r, offset nn = |n|² = ¼|r|²
362 const Real hx = Real(0.5) * rx, hy = Real(0.5) * ry, hz = Real(0.5) * rz;
363 n[k][0] = hx;
364 n[k][1] = hy;
365 n[k][2] = hz;
366 nn[k] = hx * hx + hy * hy + hz * hz;
367 }
368 }
369 for (int t = 0; t < nt; ++t)
370 if (alive[t])
371 computeVertex(t);
372 }
373
384 KOKKOS_INLINE_FUNCTION bool isSelfConsistent(Real tol) const {
385 // Branch-free max-slack accumulation (no per-thread early-out divergence): the whole-cell worst
386 // perpendicular poke is compared to `tol` once at the end. The brute scan is the complete
387 // certificate — it catches far-pokes by NON-adjacent planes too, which the local (Lawson) form
388 // deliberately does not. (An early-out variant would only help when np is large AND fail-fast
389 // rate is high; measure first.)
390 Real maxd = Real(0);
391 for (int t = 0; t < nt; ++t) {
392 if (!alive[t])
393 continue;
394 const Real v[3] = {vx[t], vy[t], vz[t]};
395 for (int k = 0; k < np; ++k) {
396 if (t0[t] == k || t1[t] == k || t2[t] == k)
397 continue; // a plane that defines this vertex
398 const Real s = n[k][0] * v[0] + n[k][1] * v[1] + n[k][2] * v[2] - nn[k];
399 if (s >
400 Real(0)) { // v outside plane k; convert to a perpendicular distance for the tol test
401 const Real invlen =
402 (nn[k] != Real(0)) ? Real(1) / Kokkos::sqrt(Kokkos::fabs(nn[k])) : Real(0);
403 maxd = Kokkos::max(maxd, s * invlen);
404 }
405 }
406 }
407 return maxd <= tol;
408 }
409
424 KOKKOS_INLINE_FUNCTION bool isSelfConsistentPartners(Real tol, int* outPartners, int maxPartners,
425 int& nPartners) const {
426 nPartners = 0;
427 bool consistent = true;
428 for (int t = 0; t < nt; ++t) {
429 if (!alive[t])
430 continue;
431 const Real v[3] = {vx[t], vy[t], vz[t]};
432 for (int k = 0; k < np; ++k) {
433 if (t0[t] == k || t1[t] == k || t2[t] == k)
434 continue; // a plane that defines this vertex
435 const Real s = n[k][0] * v[0] + n[k][1] * v[1] + n[k][2] * v[2] - nn[k];
436 if (s > Real(0)) { // v outside plane k; perpendicular distance vs tol
437 const Real invlen =
438 (nn[k] != Real(0)) ? Real(1) / Kokkos::sqrt(Kokkos::fabs(nn[k])) : Real(0);
439 if (s * invlen > tol) {
440 consistent = false;
441 const int part = pnbr[k];
442 if (part >= 0) { // box/SDF planes (pnbr<0) have no partner cell
443 bool seen = false;
444 for (int q = 0; q < nPartners; ++q)
445 if (outPartners[q] == part) {
446 seen = true;
447 break;
448 }
449 if (!seen && nPartners < maxPartners)
450 outPartners[nPartners++] = part;
451 }
452 }
453 }
454 }
455 }
456 return consistent;
457 }
458
461 KOKKOS_INLINE_FUNCTION int oppositePlane(int s, int x, int y) const {
462 const int a = t0[s], b = t1[s], c = t2[s];
463 return (a != x && a != y) ? a : (b != x && b != y) ? b : c;
464 }
465
474 KOKKOS_INLINE_FUNCTION void computePoke4(unsigned char* out) const {
475 static_assert(TrackAdj, "computePoke4 requires TrackAdj");
476 for (int t = 0; t < nt; ++t) {
477 if (!alive[t]) {
478 out[t * 4 + 0] = out[t * 4 + 1] = out[t * 4 + 2] = out[t * 4 + 3] = 0xff;
479 continue;
480 }
481 const int vt[3] = {t0[t], t1[t], t2[t]};
482 int eo[3];
483 for (int e = 0; e < 3; ++e) {
484 const int s = adj[t * 3 + e];
485 eo[e] = (s >= 0 && s < nt && alive[s]) ? oppositePlane(s, vt[e], vt[(e + 1) % 3]) : 0xff;
486 out[t * 4 + e] = (unsigned char)(eo[e] & 0xff);
487 }
488 const Real v[3] = {vx[t], vy[t], vz[t]};
489 Real best = Real(3.4e38);
490 int g = 0xff;
491 for (int k = 0; k < np; ++k) {
492 if (k == vt[0] || k == vt[1] || k == vt[2])
493 continue; // defining plane
494 if (k == eo[0] || k == eo[1] || k == eo[2])
495 continue; // already an edge-opposite
496 const Real slack = nn[k] - (n[k][0] * v[0] + n[k][1] * v[1] + n[k][2] * v[2]);
497 if (slack >= Real(0) && slack < best) {
498 best = slack;
499 g = k;
500 }
501 }
502 out[t * 4 + 3] = (unsigned char)(g & 0xff);
503 }
504 }
505
512 KOKKOS_INLINE_FUNCTION bool isLocallyConvex(const unsigned char* poke4, Real tol) const {
513 Real maxd = Real(0);
514 for (int t = 0; t < nt; ++t) {
515 if (!alive[t])
516 continue;
517 const Real v[3] = {vx[t], vy[t], vz[t]};
518 for (int j = 0; j < 4; ++j) {
519 const int k = (int)poke4[t * 4 + j];
520 if (k == 0xff)
521 continue;
522 const Real sl = n[k][0] * v[0] + n[k][1] * v[1] + n[k][2] * v[2] - nn[k];
523 if (sl > Real(0)) {
524 const Real invlen =
525 (nn[k] != Real(0)) ? Real(1) / Kokkos::sqrt(Kokkos::fabs(nn[k])) : Real(0);
526 maxd = Kokkos::max(maxd, sl * invlen);
527 }
528 }
529 }
530 return maxd <= tol;
531 }
532
537 KOKKOS_INLINE_FUNCTION bool isLocallyConvexPartners(const unsigned char* poke4, Real tol,
538 int* outPartners, int maxPartners,
539 int& nPartners) const {
540 nPartners = 0;
541 bool consistent = true;
542 for (int t = 0; t < nt; ++t) {
543 if (!alive[t])
544 continue;
545 const Real v[3] = {vx[t], vy[t], vz[t]};
546 for (int j = 0; j < 4; ++j) {
547 const int k = (int)poke4[t * 4 + j];
548 if (k == 0xff)
549 continue;
550 const Real sl = n[k][0] * v[0] + n[k][1] * v[1] + n[k][2] * v[2] - nn[k];
551 if (sl > Real(0)) {
552 const Real invlen =
553 (nn[k] != Real(0)) ? Real(1) / Kokkos::sqrt(Kokkos::fabs(nn[k])) : Real(0);
554 if (sl * invlen > tol) {
555 consistent = false;
556 const int part = pnbr[k];
557 if (part >= 0) {
558 bool seen = false;
559 for (int q = 0; q < nPartners; ++q)
560 if (outPartners[q] == part) {
561 seen = true;
562 break;
563 }
564 if (!seen && nPartners < maxPartners)
565 outPartners[nPartners++] = part;
566 }
567 }
568 }
569 }
570 }
571 return consistent;
572 }
573
578 KOKKOS_INLINE_FUNCTION Real faceMoment2(int k) const {
579 Real fx[MAXFV], fy[MAXFV], fz[MAXFV];
580 const int m = faceOrdered(k, fx, fy, fz);
581 if (m < 3)
582 return Real(0);
583 const Real nl = planeDistance(k);
584 if (!(nl > Real(0)))
585 return Real(0);
586 const Real ux = n[k][0] / nl, uy = n[k][1] / nl, uz = n[k][2] / nl;
587 Real q[3];
588 footPoint(k, q); // reference point in the facet plane
589 const Real nx = q[0], ny = q[1], nz = q[2];
590 Real M2 = Real(0);
591 for (int q = 0; q < m; ++q) {
592 const int r = (q + 1) % m;
593 const Real a[3] = {fx[q] - nx, fy[q] - ny, fz[q] - nz};
594 const Real b[3] = {fx[r] - nx, fy[r] - ny, fz[r] - nz};
595 const Real cr[3] = {a[1] * b[2] - a[2] * b[1], a[2] * b[0] - a[0] * b[2],
596 a[0] * b[1] - a[1] * b[0]};
597 const Real At = Real(0.5) * (cr[0] * ux + cr[1] * uy + cr[2] * uz);
598 const Real aa = a[0] * a[0] + a[1] * a[1] + a[2] * a[2];
599 const Real bb = b[0] * b[0] + b[1] * b[1] + b[2] * b[2];
600 const Real ab = a[0] * b[0] + a[1] * b[1] + a[2] * b[2];
601 M2 += At / Real(6) * (aa + bb + ab); // (A/12)(|a|²+|b|²+|a+b|²)
602 }
603 return M2;
604 }
605
611 KOKKOS_INLINE_FUNCTION Real planeGap(const Real pdir[3], Real d) const {
612 const Real l2 = pdir[0] * pdir[0] + pdir[1] * pdir[1] + pdir[2] * pdir[2];
613 if (!(l2 > Real(0)))
614 return Real(-1e30);
615 Real m = Real(-1e30);
616 for (int t = 0; t < nt; ++t) {
617 if (!alive[t])
618 continue;
619 const Real s = pdir[0] * vx[t] + pdir[1] * vy[t] + pdir[2] * vz[t] - d;
620 if (s > m)
621 m = s;
622 }
623 return m / Kokkos::sqrt(l2);
624 }
625
633 KOKKOS_INLINE_FUNCTION int compactPlanes() {
634 unsigned char used[MAXP];
635 if (np > MAXP) // never true (clip caps np at MAXP); bounds the loops for the compiler
636 np = MAXP;
637 for (int k = 0; k < np; ++k)
638 used[k] = (k < 6) ? 1 : 0;
639 for (int t = 0; t < nt; ++t)
640 if (alive[t]) {
641 used[t0[t]] = 1;
642 used[t1[t]] = 1;
643 used[t2[t]] = 1;
644 }
645 unsigned char map[MAXP];
646 int m = 0;
647 for (int k = 0; k < np; ++k) {
648 if (!used[k]) {
649 map[k] = 0;
650 continue;
651 }
652 map[k] = (unsigned char)m;
653 if (m != k) {
654 n[m][0] = n[k][0];
655 n[m][1] = n[k][1];
656 n[m][2] = n[k][2];
657 nn[m] = nn[k];
658 pnbr[m] = pnbr[k];
659 }
660 ++m;
661 }
662 const int removed = np - m;
663 if (removed > 0) {
664 for (int t = 0; t < nt; ++t)
665 if (alive[t]) {
666 t0[t] = map[t0[t]];
667 t1[t] = map[t1[t]];
668 t2[t] = map[t2[t]];
669 }
670 np = m;
671 }
672 return removed;
673 }
674
676 KOKKOS_INLINE_FUNCTION Real maxVertexRsq() const {
677 Real m = 0;
678 for (int t = 0; t < nt; ++t) {
679 if (!alive[t])
680 continue;
681 const Real r = vx[t] * vx[t] + vy[t] * vy[t] + vz[t] * vz[t];
682 if (r > m)
683 m = r;
684 }
685 return m;
686 }
687
694 KOKKOS_INLINE_FUNCTION int findSharing(int self, int x, int y) const {
695 for (int s = 0; s < nt; ++s) {
696 if (s == self || !alive[s])
697 continue;
698 const int a = t0[s], b = t1[s], c = t2[s];
699 const bool hasX = (a == x || b == x || c == x);
700 const bool hasY = (a == y || b == y || c == y);
701 if (hasX && hasY)
702 return s;
703 }
704 return -1;
705 }
706
709 KOKKOS_INLINE_FUNCTION void rebuildAdjacency() {
710 if constexpr (TrackAdj) {
711 for (int t = 0; t < nt; ++t) {
712 if (!alive[t])
713 continue;
714 const int vt[3] = {t0[t], t1[t], t2[t]};
715 for (int e = 0; e < 3; ++e)
716 adj[t * 3 + e] = findSharing(t, vt[e], vt[(e + 1) % 3]);
717 }
718 }
719 }
720
725 KOKKOS_INLINE_FUNCTION bool checkAdjacencyInvariant() const {
726 if constexpr (!TrackAdj) {
727 return true;
728 } else {
729 for (int t = 0; t < nt; ++t) {
730 if (!alive[t])
731 continue;
732 const int vt[3] = {t0[t], t1[t], t2[t]};
733 for (int e = 0; e < 3; ++e) {
734 const int x = vt[e], y = vt[(e + 1) % 3];
735 const int s = adj[t * 3 + e];
736 if (s < 0 || s >= nt || !alive[s])
737 return false; // live target
738 const int vs[3] = {t0[s], t1[s], t2[s]};
739 const bool hasX = (vs[0] == x || vs[1] == x || vs[2] == x);
740 const bool hasY = (vs[0] == y || vs[1] == y || vs[2] == y);
741 if (!(hasX && hasY))
742 return false; // shares the edge
743 bool back = false; // symmetry
744 for (int f = 0; f < 3; ++f)
745 if (adj[s * 3 + f] == t)
746 back = true;
747 if (!back)
748 return false;
749 }
750 }
751 return true;
752 }
753 }
754
759 static constexpr Real kFanSine2 = sizeof(Real) == sizeof(float) ? Real(6.0e-10) : Real(1.4e-21);
760
777 KOKKOS_INLINE_FUNCTION bool inconsistentFan(int x, int y, int z, int p) const {
778 const Real* a = n[x];
779 const Real* b = n[y];
780 const Real d[3] = {a[1] * b[2] - a[2] * b[1], a[2] * b[0] - a[0] * b[2],
781 a[0] * b[1] - a[1] * b[0]}; // direction of the edge x∩y
782 const Real dp = n[p][0] * d[0] + n[p][1] * d[1] + n[p][2] * d[2]; // det(x, y, p)
783 const Real dz = n[z][0] * d[0] + n[z][1] * d[1] + n[z][2] * d[2]; // det(x, y, z)
784 return !(dp * dz > Real(0)) ||
785 dp * dp <= kFanSine2 * Kokkos::fabs(nn[x]) * Kokkos::fabs(nn[y]) * Kokkos::fabs(nn[p]);
786 }
787
788 KOKKOS_INLINE_FUNCTION bool anyKilled(const bool* kill) const {
789 for (int t = 0; t < nt; ++t)
790 if (kill[t])
791 return true;
792 return false;
793 }
794
797 KOKKOS_INLINE_FUNCTION bool allLiveKilled(const bool* kill) const {
798 for (int t = 0; t < nt; ++t)
799 if (alive[t] && !kill[t])
800 return false;
801 return true;
802 }
803
807 KOKKOS_INLINE_FUNCTION bool diskHorizon(int k, int H, const unsigned char* nA,
808 const unsigned char* nB) const {
809 return k <= 2 ? H == k + 2 : simpleHorizonEdges(nA, nB, H);
810 }
811
816 KOKKOS_INLINE_FUNCTION bool simpleHorizonEdges(const unsigned char* nA, const unsigned char* nB,
817 int H) const {
818 if (H < 3)
819 return false;
820 // e1/e2[p] = 1 + the (at most two) horizon edges at plane p; then follow the chain once.
821 unsigned short e1[MAXP], e2[MAXP];
822 for (int i = 0; i < H; ++i)
823 e1[nA[i]] = e2[nA[i]] = e1[nB[i]] = e2[nB[i]] = 0;
824 for (int i = 0; i < H; ++i) {
825 const int ends[2] = {nA[i], nB[i]};
826 for (int k = 0; k < 2; ++k) {
827 const int p = ends[k];
828 if (!e1[p])
829 e1[p] = (unsigned short)(i + 1);
830 else if (!e2[p])
831 e2[p] = (unsigned short)(i + 1);
832 else
833 return false; // a pinch: plane p on three or more horizon edges
834 }
835 }
836 int prev = 0, cur = nB[0], steps = 1;
837 while (cur != nA[0]) {
838 const int nx = (e1[cur] - 1 == prev ? e2[cur] : e1[cur]) - 1;
839 if (nx < 0 || ++steps > H)
840 return false;
841 cur = (nA[nx] == cur) ? nB[nx] : nA[nx];
842 prev = nx;
843 }
844 return steps == H; // one loop through every edge
845 }
846
850 KOKKOS_INLINE_FUNCTION bool simpleHorizonCycle(const bool* kill, const int* hA, int H) const {
851 if constexpr (TrackAdj) {
852 if (H < 3)
853 return false;
854 unsigned char seen[MAXP];
855 for (int i = 0; i < H; ++i)
856 seen[hA[i]] = 0;
857 for (int i = 0; i < H; ++i)
858 if (seen[hA[i]]++)
859 return false;
860 int total = 0;
861 for (int t = 0; t < nt; ++t)
862 if (kill[t])
863 for (int e = 0; e < 3; ++e) {
864 const int s = adj[t * 3 + e];
865 total += (s >= 0 && alive[s] && !kill[s]) ? 1 : 0;
866 }
867 return total == H;
868 } else {
869 (void)kill;
870 (void)hA;
871 return H >= 3;
872 }
873 }
874
875 KOKKOS_INLINE_FUNCTION int allocTri() {
876 for (int s = 0; s < nt; ++s)
877 if (!alive[s])
878 return s;
879 if (nt < MAXT)
880 return nt++;
881 overflow = true;
882 return -1;
883 }
884
895 KOKKOS_INLINE_FUNCTION bool clip(const Real pdir[3], Real d, int nbr, Real* gapOut = nullptr) {
896 // The plane list keeps every plane that cut when it was committed, including those a later
897 // plane made redundant (no live triangle any more) — typically 2–3× the face count. At the
898 // cap, drop those first (compactPlanes: geometry and topology unchanged, indices renumbered)
899 // and overflow only if the LIVE planes fill MAXP. Without this the overflow depended on the
900 // insertion order: a 29-face cell committed 59 planes in one gather order and 64 in another
901 // (thread-scheduling of the counting-sort scatter), and was silently published with zero
902 // volume in the latter. Callers holding plane indices across a clip must keep headroom
903 // (clipCellAgainstSdf compacts up front for exactly that reason).
904 if (np >= MAXP && compactPlanes() == 0) {
905 overflow = true;
906 if (gapOut)
907 *gapOut = Real(-1e30);
908 return false;
909 }
910 const int pi = np; // tentative index
911 setHalfSpace(pi, pdir, d);
912 pnbr[pi] = nbr;
913
914 // Mark triangles whose dual vertex is outside the new half-space (n·v > nn).
915 bool kill[MAXT];
916 int nk = 0; // killed triangles
917 Real smax = Real(-1e30);
918 for (int t = 0; t < nt; ++t) {
919 kill[t] = false;
920 if (!alive[t])
921 continue;
922 const Real s = n[pi][0] * vx[t] + n[pi][1] * vy[t] + n[pi][2] * vz[t] - nn[pi];
923 if (s > smax)
924 smax = s;
925 if (s > 0) {
926 kill[t] = true;
927 ++nk;
928 }
929 }
930 if (gapOut) { // s = n·v − nn is |n| times the distance beyond the plane
931 const Real nl = planeDistance(pi);
932 *gapOut = nl > Real(0) ? smax / nl : Real(-1e30);
933 }
934 if (nk == 0)
935 return false; // candidate does not cut -> no-op, plane not committed
936
937 // The conflict region K (the killed triangles) must be a disk whose fan corners inherit the
938 // killed corners' orientation. The signs above give that for every generic cut; on
939 // (near-)cospherical input the round-off-decided vertices can break it, and the topology
940 // decides instead: first per horizon edge (inconsistentFan, inside the collection), then — if
941 // K is still not a disk — by regrowing it from the outermost vertex (growDisk).
942 if constexpr (!TrackAdj) {
943 // ---- Lean path: collect the horizon by findSharing, then retriangulate. For each killed
944 // triangle edge whose sharing triangle survives, the new plane gets a triangle (x, y, pi).
945 // Collect first, then mutate.
946 unsigned char nA[MAXT], nB[MAXT];
947 // (no horizon with something killed <=> every live vertex killed: the cell is empty)
948 int nnew = collectHorizon(kill, pi, nA, nB, nk);
949 if (!overflow && nnew > 0 && !diskHorizon(nk, nnew, nA, nB)) {
950 degenerate = true;
951 nk = growDisk(kill, pi);
952 nnew = collectHorizon(kill, pi, nA, nB, nk);
953 if (!overflow && nnew > 0 && !diskHorizon(nk, nnew, nA, nB))
954 overflow = true; // cannot happen (growDisk builds a disk): report, never publish
955 }
956 if (nnew == 0 && !anyKilled(kill))
957 return false; // every outside vertex lay on the plane: it only touches the cell
958 if (nnew == 0 && allLiveKilled(kill)) {
959 nt = 0; // every live vertex strictly outside: the (power) cell is empty
960 return true;
961 }
962 np = pi + 1; // commit the plane
963 if (nnew == 0)
964 overflow = true; // killed, no horizon, yet a survivor: broken triangulation, never publish
965 if (overflow)
966 return true;
967 // Remove killed triangles.
968 for (int t = 0; t < nt; ++t)
969 if (kill[t])
970 alive[t] = false;
971 // Add the new fan around plane pi.
972 for (int i = 0; i < nnew; ++i) {
973 const int slot = allocTri();
974 if (slot < 0)
975 break;
976 t0[slot] = nA[i];
977 t1[slot] = nB[i];
978 t2[slot] = (unsigned char)pi;
979 alive[slot] = true;
980 computeVertex(slot);
981 }
982 } else {
983 // ---- TrackAdj path: walk the conflict-region boundary as ONE oriented cycle via `adj`
984 // (O(conflict), no findSharing), then stitch the new fan's adjacency in O(1) per edge. ----
985 int hA[MAXT], hB[MAXT], hSurv[MAXT],
986 hBack[MAXT]; // horizon edge {A,B}, survivor across it, slot back
987 int H = walkHorizon(kill, pi, hA, hB, hSurv, hBack, nk);
988 if (!overflow && H > 0 && !(nk <= 2 ? H == nk + 2 : simpleHorizonCycle(kill, hA, H))) {
989 degenerate = true;
990 nk = growDisk(kill, pi);
991 H = walkHorizon(kill, pi, hA, hB, hSurv, hBack, nk);
992 if (!overflow && H > 0 && !(nk <= 2 ? H == nk + 2 : simpleHorizonCycle(kill, hA, H)))
993 overflow = true; // cannot happen (growDisk builds a disk): report, never publish
994 }
995 if (H == 0 && !anyKilled(kill))
996 return false; // every outside vertex lay on the plane: it only touches the cell
997 if (H == 0 && allLiveKilled(kill)) {
998 nt = 0; // every live vertex strictly outside: the (power) cell is empty
999 return true;
1000 }
1001 np = pi + 1; // commit the plane
1002 if (H == 0)
1003 overflow = true; // killed, no horizon, yet a survivor: broken triangulation, never publish
1004 if (overflow)
1005 return true;
1006
1007 // Remove killed triangles (frees their slots for the fan via allocTri).
1008 for (int t = 0; t < nt; ++t)
1009 if (kill[t])
1010 alive[t] = false;
1011 // Allocate the whole fan first (so τ_{i±1} cross-references resolve), then set topology +
1012 // adjacency.
1013 int tau[MAXT];
1014 bool ok = true;
1015 for (int i = 0; i < H && ok; ++i) {
1016 tau[i] = allocTri();
1017 if (tau[i] < 0)
1018 ok = false;
1019 else
1020 alive[tau[i]] = true; // reserve the slot so allocTri doesn't hand it out again
1021 }
1022 if (ok) {
1023 for (int i = 0; i < H; ++i) {
1024 const int s = tau[i];
1025 t0[s] = (unsigned char)hA[i];
1026 t1[s] = (unsigned char)hB[i];
1027 t2[s] = (unsigned char)pi;
1028 alive[s] = true;
1029 computeVertex(s);
1030 adj[s * 3 + 0] = hSurv[i]; // slot 0 = edge {A_i,B_i} -> survivor across the horizon
1031 adj[s * 3 + 1] = tau[(i + 1) % H]; // slot 1 = edge {B_i,P} -> next fan triangle
1032 adj[s * 3 + 2] = tau[(i - 1 + H) % H]; // slot 2 = edge {P,A_i} -> prev fan triangle
1033 adj[hSurv[i] * 3 + hBack[i]] = s; // back-patch the survivor's slot to this fan triangle
1034 }
1035 }
1036 }
1037 return true;
1038 }
1039
1044 KOKKOS_INLINE_FUNCTION int collectHorizon(bool* kill, int pi, unsigned char* nA,
1045 unsigned char* nB, int& nk) {
1046 int nnew = 0;
1047 bool redo = true;
1048 while (redo) {
1049 redo = false;
1050 nnew = 0;
1051 for (int t = 0; t < nt && !redo; ++t) {
1052 if (!kill[t])
1053 continue;
1054 const int vtx[3] = {t0[t], t1[t], t2[t]};
1055 for (int e = 0; e < 3; ++e) {
1056 const int x = vtx[e], y = vtx[(e + 1) % 3];
1057 const int other = findSharing(t, x, y);
1058 if (other >= 0 && !kill[other]) {
1059 if (inconsistentFan(x, y, vtx[(e + 2) % 3], pi)) {
1060 kill[t] = false; // t is ON the plane: keep it
1061 --nk;
1062 degenerate = true;
1063 redo = true;
1064 break;
1065 }
1066 if (nnew < MAXT) {
1067 nA[nnew] = (unsigned char)x;
1068 nB[nnew] = (unsigned char)y;
1069 ++nnew;
1070 } else {
1071 overflow = true;
1072 }
1073 }
1074 }
1075 }
1076 }
1077 return nnew;
1078 }
1079
1086 KOKKOS_INLINE_FUNCTION int walkHorizon(bool* kill, int pi, int* hA, int* hB, int* hSurv,
1087 int* hBack, int& nk) {
1088 if constexpr (TrackAdj) {
1089 int H = 0;
1090 bool redo = true;
1091 while (redo) {
1092 redo = false;
1093 H = 0;
1094 // Start edge: a killed triangle with a surviving neighbour across some slot.
1095 int t0k = -1, e0 = -1;
1096 for (int t = 0; t < nt && t0k < 0; ++t) {
1097 if (!kill[t])
1098 continue;
1099 for (int e = 0; e < 3; ++e) {
1100 const int s = adj[t * 3 + e];
1101 if (s >= 0 && alive[s] && !kill[s]) {
1102 t0k = t;
1103 e0 = e;
1104 break;
1105 }
1106 }
1107 }
1108 if (t0k < 0)
1109 return 0;
1110 const int vstart[3] = {t0[t0k], t1[t0k], t2[t0k]};
1111 int A = vstart[e0], B = vstart[(e0 + 1) % 3];
1112 int tt = t0k, ee = e0;
1113 const int guardMax = 6 * nt + 12;
1114 int guard = 0;
1115 do {
1116 const int C = (t0[tt] != A && t0[tt] != B) ? t0[tt]
1117 : (t1[tt] != A && t1[tt] != B) ? t1[tt]
1118 : t2[tt];
1119 if (inconsistentFan(A, B, C, pi)) {
1120 kill[tt] = false; // tt is ON the plane: keep it, restart the walk
1121 --nk;
1122 degenerate = true;
1123 redo = true;
1124 break;
1125 }
1126 const int surv = adj[tt * 3 + ee];
1127 int back = 0;
1128 for (int f = 0; f < 3; ++f)
1129 if (adj[surv * 3 + f] == tt) {
1130 back = f;
1131 break;
1132 }
1133 if (H < MAXT) {
1134 hA[H] = A;
1135 hB[H] = B;
1136 hSurv[H] = surv;
1137 hBack[H] = back;
1138 ++H;
1139 } else {
1140 overflow = true;
1141 break;
1142 }
1143 // Pivot around vertex B, rotating through killed triangles, until we exit to the next
1144 // survivor.
1145 const int pivot = B;
1146 int rt = tt, arrival = ee, rotGuard = 0;
1147 while (true) {
1148 const int rv[3] = {t0[rt], t1[rt], t2[rt]};
1149 const int lv = (rv[0] == pivot) ? 0
1150 : (rv[1] == pivot) ? 1
1151 : 2; // local index of pivot in rt
1152 const int crossSlot = (arrival == lv) ? (lv + 2) % 3 : lv; // the OTHER B-edge of rt
1153 const int rn = adj[rt * 3 + crossSlot];
1154 if (rn >= 0 && kill[rn]) { // still inside the conflict region: keep rotating around B
1155 int f = (adj[rn * 3 + 0] == rt) ? 0 : (adj[rn * 3 + 1] == rt) ? 1 : 2;
1156 rt = rn;
1157 arrival = f;
1158 if (++rotGuard > guardMax) {
1159 overflow = true;
1160 break;
1161 }
1162 continue;
1163 }
1164 // rn survives -> edge crossSlot of rt is the next horizon edge, oriented out of the
1165 // pivot.
1166 const int u = rv[crossSlot], w = rv[(crossSlot + 1) % 3];
1167 A = pivot;
1168 B = (u == pivot) ? w : u;
1169 tt = rt;
1170 ee = crossSlot;
1171 break;
1172 }
1173 if (overflow)
1174 break;
1175 if (++guard > guardMax) {
1176 overflow = true;
1177 break;
1178 }
1179 } while (!(tt == t0k && ee == e0));
1180 }
1181 return H;
1182 } else {
1183 (void)kill, (void)pi, (void)hA, (void)hB, (void)hSurv, (void)hBack, (void)nk;
1184 return 0;
1185 }
1186 }
1187
1189 KOKKOS_INLINE_FUNCTION int across(int t, int e) const {
1190 if constexpr (TrackAdj) {
1191 return adj[t * 3 + e];
1192 } else {
1193 const int vt[3] = {t0[t], t1[t], t2[t]};
1194 return findSharing(t, vt[e], vt[(e + 1) % 3]);
1195 }
1196 }
1197
1214 KOKKOS_INLINE_FUNCTION int growDisk(bool* kill, int pi) {
1215 bool cand[MAXT];
1216 Real sv[MAXT]; // the cut's slack: > 0 on the candidates (the clip killed only s > 0), else 0
1217 for (int t = 0; t < nt; ++t) {
1218 sv[t] = kill[t] ? n[pi][0] * vx[t] + n[pi][1] * vy[t] + n[pi][2] * vz[t] - nn[pi] : Real(0);
1219 kill[t] = false;
1220 }
1221 unsigned char touch[MAXP];
1222 for (int round = 0; round <= nt; ++round) {
1223 for (int t = 0; t < nt; ++t) {
1224 cand[t] = sv[t] > Real(0);
1225 kill[t] = false;
1226 }
1227 for (int k = 0; k < np; ++k)
1228 touch[k] = 0;
1229 int H = 0; // boundary length of K
1230 int k = 0; // its triangle count (returned)
1231 for (bool first = true;; first = false) {
1232 int pick = -1, pickM = 0;
1233 for (int t = 0; t < nt; ++t) {
1234 if (!cand[t] || (pick >= 0 && sv[t] <= sv[pick]))
1235 continue;
1236 const int ut[3] = {t0[t], t1[t], t2[t]};
1237 int mt = 0, shared = 0;
1238 bool fanOk = true;
1239 for (int e = 0; e < 3; ++e) {
1240 const int o = across(t, e);
1241 if (o >= 0 && kill[o]) {
1242 ++mt;
1243 shared = e;
1244 } else if (!(o >= 0 && cand[o]) &&
1245 inconsistentFan(ut[e], ut[(e + 1) % 3], ut[(e + 2) % 3], pi)) {
1246 fanOk = false; // a survivor's edge: horizon for good (edges to candidates may close)
1247 }
1248 }
1249 const bool disk =
1250 first ? mt == 0 : (mt == 1 && !touch[ut[(shared + 2) % 3]]) || (mt == 2 && H > 3);
1251 if (fanOk && disk) {
1252 pick = t;
1253 pickM = mt;
1254 }
1255 }
1256 if (pick < 0)
1257 break;
1258 ++k;
1259 kill[pick] = true;
1260 cand[pick] = false;
1261 H += 3 - 2 * pickM;
1262 touch[t0[pick]] = touch[t1[pick]] = touch[t2[pick]] = 1;
1263 }
1264 // the deferred edges: K's edges to candidates that never joined
1265 int stay = -1;
1266 for (int t = 0; t < nt && stay < 0; ++t) {
1267 if (!kill[t])
1268 continue;
1269 const int ut[3] = {t0[t], t1[t], t2[t]};
1270 for (int e = 0; e < 3; ++e) {
1271 const int o = across(t, e);
1272 if (o >= 0 && cand[o] && inconsistentFan(ut[e], ut[(e + 1) % 3], ut[(e + 2) % 3], pi)) {
1273 stay = o;
1274 break;
1275 }
1276 }
1277 }
1278 if (stay < 0)
1279 return k;
1280 sv[stay] = Real(0);
1281 }
1282 for (int t = 0; t < nt; ++t) // unreachable: every round drops a candidate
1283 kill[t] = false;
1284 return 0;
1285 }
1286
1287 KOKKOS_INLINE_FUNCTION bool empty() const {
1288 for (int t = 0; t < nt; ++t)
1289 if (alive[t])
1290 return false;
1291 return true;
1292 }
1293
1294 KOKKOS_INLINE_FUNCTION int countFaces() const {
1295 int nf = 0;
1296 for (int k = 0; k < np; ++k) {
1297 bool used = false;
1298 for (int t = 0; t < nt && !used; ++t)
1299 if (alive[t] && (t0[t] == k || t1[t] == k || t2[t] == k))
1300 used = true;
1301 if (used)
1302 ++nf;
1303 }
1304 return nf;
1305 }
1306
1307 static constexpr int MAXFV = 28; // max vertices on one face polygon
1308
1314 KOKKOS_INLINE_FUNCTION int faceOrderedIdx(int k, int out[MAXFV]) const {
1315 int m = 0;
1316 Real fx[MAXFV], fy[MAXFV], fz[MAXFV];
1317 for (int t = 0; t < nt; ++t) {
1318 if (!alive[t])
1319 continue;
1320 if (t0[t] != k && t1[t] != k && t2[t] != k)
1321 continue;
1322 if (m < MAXFV) {
1323 out[m] = t;
1324 fx[m] = vx[t];
1325 fy[m] = vy[t];
1326 fz[m] = vz[t];
1327 ++m;
1328 }
1329 }
1330 if (m < 3)
1331 return m;
1332 const Real nx = n[k][0], ny = n[k][1], nz = n[k][2];
1333 const Real nlen = Kokkos::sqrt(nx * nx + ny * ny + nz * nz);
1334 if (nlen == Real(0))
1335 return 0;
1336 const Real un[3] = {nx / nlen, ny / nlen, nz / nlen};
1337 Real e1[3];
1338 if (Kokkos::fabs(un[0]) <= Kokkos::fabs(un[1]) && Kokkos::fabs(un[0]) <= Kokkos::fabs(un[2])) {
1339 e1[0] = 0;
1340 e1[1] = -un[2];
1341 e1[2] = un[1];
1342 } else if (Kokkos::fabs(un[1]) <= Kokkos::fabs(un[2])) {
1343 e1[0] = -un[2];
1344 e1[1] = 0;
1345 e1[2] = un[0];
1346 } else {
1347 e1[0] = -un[1];
1348 e1[1] = un[0];
1349 e1[2] = 0;
1350 }
1351 const Real e1l = Kokkos::sqrt(e1[0] * e1[0] + e1[1] * e1[1] + e1[2] * e1[2]);
1352 e1[0] /= e1l;
1353 e1[1] /= e1l;
1354 e1[2] /= e1l;
1355 const Real e2[3] = {un[1] * e1[2] - un[2] * e1[1], un[2] * e1[0] - un[0] * e1[2],
1356 un[0] * e1[1] - un[1] * e1[0]};
1357 Real cx = 0, cy = 0, cz = 0;
1358 for (int i = 0; i < m; ++i) {
1359 cx += fx[i];
1360 cy += fy[i];
1361 cz += fz[i];
1362 }
1363 cx /= m;
1364 cy /= m;
1365 cz /= m;
1366 Real ang[MAXFV];
1367 for (int i = 0; i < m; ++i) {
1368 const Real dx = fx[i] - cx, dy = fy[i] - cy, dz = fz[i] - cz;
1369 const Real px = dx * e1[0] + dy * e1[1] + dz * e1[2]; // in-plane coords
1370 const Real py = dx * e2[0] + dy * e2[1] + dz * e2[2];
1371 // diamond pseudo-angle: monotonic in true angle, in [0,4), NO atan2 (transcendental)
1372 const Real s = Kokkos::fabs(px) + Kokkos::fabs(py);
1373 const Real t = (s > Real(0)) ? py / s : Real(0); // [-1,1]
1374 ang[i] = (px < Real(0)) ? (Real(2) - t) : (py < Real(0) ? Real(4) + t : t);
1375 }
1376 for (int i = 1; i < m; ++i) {
1377 const Real ka = ang[i];
1378 const int ki = out[i];
1379 int j = i - 1;
1380 while (j >= 0 && ang[j] > ka) {
1381 ang[j + 1] = ang[j];
1382 out[j + 1] = out[j];
1383 --j;
1384 }
1385 ang[j + 1] = ka;
1386 out[j + 1] = ki;
1387 }
1388 return m;
1389 }
1390
1395 KOKKOS_INLINE_FUNCTION int faceOrdered(int k, Real fx[MAXFV], Real fy[MAXFV],
1396 Real fz[MAXFV]) const {
1397 int idx[MAXFV];
1398 const int m = faceOrderedIdx(k, idx);
1399 for (int i = 0; i < m; ++i) {
1400 fx[i] = vx[idx[i]];
1401 fy[i] = vy[idx[i]];
1402 fz[i] = vz[idx[i]];
1403 }
1404 return m;
1405 }
1406
1408 KOKKOS_INLINE_FUNCTION static void polyAreaVec(const Real fx[], const Real fy[], const Real fz[],
1409 int m, Real A[3]) {
1410 Real ax = 0, ay = 0, az = 0;
1411 for (int i = 0; i < m; ++i) {
1412 const int j = (i + 1 == m) ? 0 : i + 1;
1413 ax += fy[i] * fz[j] - fz[i] * fy[j];
1414 ay += fz[i] * fx[j] - fx[i] * fz[j];
1415 az += fx[i] * fy[j] - fy[i] * fx[j];
1416 }
1417 A[0] = Real(0.5) * ax;
1418 A[1] = Real(0.5) * ay;
1419 A[2] = Real(0.5) * az;
1420 }
1421
1422 static constexpr int MAXSV = 2 * MAXFV;
1423
1433 KOKKOS_INLINE_FUNCTION int sectionPolygon(const Real p0[3], const Real u[3], Real px[MAXSV],
1434 Real py[MAXSV], Real pz[MAXSV]) const {
1435 int m = 0;
1436 for (int i = 0; i < nt; ++i) {
1437 if (!alive[i])
1438 continue;
1439 const Real di = (vx[i] - p0[0]) * u[0] + (vy[i] - p0[1]) * u[1] + (vz[i] - p0[2]) * u[2];
1440 const unsigned char a0 = t0[i], a1 = t1[i], a2 = t2[i];
1441 for (int j = i + 1; j < nt; ++j) {
1442 if (!alive[j])
1443 continue;
1444 const unsigned char b0 = t0[j], b1 = t1[j], b2 = t2[j];
1445 const int common = (a0 == b0) + (a0 == b1) + (a0 == b2) + (a1 == b0) + (a1 == b1) +
1446 (a1 == b2) + (a2 == b0) + (a2 == b1) + (a2 == b2);
1447 if (common != 2)
1448 continue; // not an edge (a primal edge = two triangles sharing exactly two planes)
1449 const Real dj = (vx[j] - p0[0]) * u[0] + (vy[j] - p0[1]) * u[1] + (vz[j] - p0[2]) * u[2];
1450 if ((di > Real(0)) == (dj > Real(0)))
1451 continue; // both endpoints on the same side -> edge doesn't cross the plane
1452 const Real denom = di - dj;
1453 if (denom == Real(0))
1454 continue;
1455 const Real tt = di / denom;
1456 if (m >= MAXSV)
1457 return -1;
1458 px[m] = vx[i] + tt * (vx[j] - vx[i]);
1459 py[m] = vy[i] + tt * (vy[j] - vy[i]);
1460 pz[m] = vz[i] + tt * (vz[j] - vz[i]);
1461 ++m;
1462 }
1463 }
1464 if (m < 3)
1465 return m;
1466 const Real ulen = Kokkos::sqrt(u[0] * u[0] + u[1] * u[1] + u[2] * u[2]);
1467 if (ulen == Real(0))
1468 return 0;
1469 const Real un[3] = {u[0] / ulen, u[1] / ulen, u[2] / ulen};
1470 Real e1[3];
1471 if (Kokkos::fabs(un[0]) <= Kokkos::fabs(un[1]) && Kokkos::fabs(un[0]) <= Kokkos::fabs(un[2])) {
1472 e1[0] = 0;
1473 e1[1] = -un[2];
1474 e1[2] = un[1];
1475 } else if (Kokkos::fabs(un[1]) <= Kokkos::fabs(un[2])) {
1476 e1[0] = -un[2];
1477 e1[1] = 0;
1478 e1[2] = un[0];
1479 } else {
1480 e1[0] = -un[1];
1481 e1[1] = un[0];
1482 e1[2] = 0;
1483 }
1484 const Real e1l = Kokkos::sqrt(e1[0] * e1[0] + e1[1] * e1[1] + e1[2] * e1[2]);
1485 e1[0] /= e1l;
1486 e1[1] /= e1l;
1487 e1[2] /= e1l;
1488 const Real e2[3] = {un[1] * e1[2] - un[2] * e1[1], un[2] * e1[0] - un[0] * e1[2],
1489 un[0] * e1[1] - un[1] * e1[0]};
1490 Real cx = 0, cy = 0, cz = 0;
1491 for (int i = 0; i < m; ++i) {
1492 cx += px[i];
1493 cy += py[i];
1494 cz += pz[i];
1495 }
1496 cx /= m;
1497 cy /= m;
1498 cz /= m;
1499 Real ang[MAXSV];
1500 for (int i = 0; i < m; ++i) {
1501 const Real dx = px[i] - cx, dy = py[i] - cy, dz = pz[i] - cz;
1502 const Real qx = dx * e1[0] + dy * e1[1] + dz * e1[2];
1503 const Real qy = dx * e2[0] + dy * e2[1] + dz * e2[2];
1504 const Real s = Kokkos::fabs(qx) + Kokkos::fabs(qy);
1505 const Real tt = (s > Real(0)) ? qy / s : Real(0);
1506 ang[i] = (qx < Real(0)) ? (Real(2) - tt) : (qy < Real(0) ? Real(4) + tt : tt);
1507 }
1508 for (int i = 1; i < m; ++i) {
1509 Real ka = ang[i], kx = px[i], ky = py[i], kz = pz[i];
1510 int j = i - 1;
1511 while (j >= 0 && ang[j] > ka) {
1512 ang[j + 1] = ang[j];
1513 px[j + 1] = px[j];
1514 py[j + 1] = py[j];
1515 pz[j + 1] = pz[j];
1516 --j;
1517 }
1518 ang[j + 1] = ka;
1519 px[j + 1] = kx;
1520 py[j + 1] = ky;
1521 pz[j + 1] = kz;
1522 }
1523 return m;
1524 }
1525
1528 KOKKOS_INLINE_FUNCTION Real volume() const {
1529 Real vol = 0;
1530 Real fx[MAXFV], fy[MAXFV], fz[MAXFV];
1531 for (int k = 0; k < np; ++k) {
1532 const int m = faceOrdered(k, fx, fy, fz);
1533 if (m < 3)
1534 continue;
1535 Real A[3];
1536 polyAreaVec(fx, fy, fz, m, A);
1537 const Real area = Kokkos::sqrt(A[0] * A[0] + A[1] * A[1] + A[2] * A[2]);
1538 const Real support = signedSupport(k); // signed seed->plane distance
1539 vol += support * area;
1540 }
1541 return vol * (Real(1) / Real(3));
1542 }
1543
1544 // ---- Vertex-local, SORT-FREE geometry (Ray/Sokolov/Lefebvre/Lévy TOG 2018; geogram ConvexCell)
1545 // ---- Plane stored as the outward (non-unit) normal n with half-space {x : n·x ≤ nn},
1546 // nn = σ|n|²; the foot of the perpendicular from the origin onto the plane (the facet point) is
1547 // q = σn, and |n| is the origin→plane distance (σ = −1 only for a power seed outside its cell).
1548 // Role rule: half-space tests, Cramer and orientations read n; any use of a plane as a POINT
1549 // (cone apex, centroid, moment arm) reads q; |n|² normalisations read |nn|. Volume by the
1550 // divergence theorem, coning every boundary flag (facet q_i, edge foot f, vertex v) to the
1551 // origin: each tetra (0,q_i,f,v) is LOCAL to one vertex + its 3 planes, and the signed sum is
1552 // exact even when feet fall outside their faces or the origin outside the cell. No vertex
1553 // ordering, no adjacency — a pure per-vertex scatter.
1554
1557 KOKKOS_INLINE_FUNCTION void planeN(int k, Real out[3]) const {
1558 out[0] = n[k][0];
1559 out[1] = n[k][1];
1560 out[2] = n[k][2];
1561 }
1562 KOKKOS_INLINE_FUNCTION static void xprod(const Real a[3], const Real b[3], Real o[3]) {
1563 o[0] = a[1] * b[2] - a[2] * b[1];
1564 o[1] = a[2] * b[0] - a[0] * b[2];
1565 o[2] = a[0] * b[1] - a[1] * b[0];
1566 }
1567 KOKKOS_INLINE_FUNCTION static Real dot3(const Real a[3], const Real b[3]) {
1568 return a[0] * b[0] + a[1] * b[1] + a[2] * b[2];
1569 }
1570 KOKKOS_INLINE_FUNCTION static Real det3(const Real a[3], const Real b[3], const Real c[3]) {
1571 Real bc[3];
1572 xprod(b, c, bc);
1573 return dot3(a, bc);
1574 }
1576 KOKKOS_INLINE_FUNCTION static void edgeFoot(const Real v[3], const Real ck[3], Real f[3]) {
1577 const Real c2 = dot3(ck, ck), s = (c2 > Real(0)) ? dot3(v, ck) / c2 : Real(0);
1578 f[0] = v[0] - s * ck[0];
1579 f[1] = v[1] - s * ck[1];
1580 f[2] = v[2] - s * ck[2];
1581 }
1582
1585 KOKKOS_INLINE_FUNCTION Real volumePerVertex() const {
1586 Real vol = 0;
1587 for (int t = 0; t < nt; ++t) {
1588 if (!alive[t])
1589 continue;
1590 Real n1[3], n2[3], n3[3];
1591 planeN(t0[t], n1);
1592 planeN(t1[t], n2);
1593 planeN(t2[t], n3);
1594 Real q1[3], q2[3], q3[3]; // foot points (cone apexes)
1595 footPoint(t0[t], q1);
1596 footPoint(t1[t], q2);
1597 footPoint(t2[t], q3);
1598 Real c1[3], c2[3], c3[3];
1599 xprod(n2, n3, c1);
1600 xprod(n3, n1, c2);
1601 xprod(n1, n2, c3);
1602 Real D = dot3(n1, c1);
1603 if (D < Real(0)) // canonical order D>0: swap (n2,n3), (c2,c3), (q2,q3); v unchanged
1604 for (int a = 0; a < 3; ++a) {
1605 Real tmp = n2[a];
1606 n2[a] = n3[a];
1607 n3[a] = tmp;
1608 tmp = c2[a];
1609 c2[a] = c3[a];
1610 c3[a] = tmp;
1611 tmp = q2[a];
1612 q2[a] = q3[a];
1613 q3[a] = tmp;
1614 }
1615 const Real v[3] = {vx[t], vy[t], vz[t]};
1616 Real f12[3], f23[3], f31[3];
1617 edgeFoot(v, c3, f12);
1618 edgeFoot(v, c1, f23);
1619 edgeFoot(v, c2, f31);
1620 Real e[3], d = 0;
1621 e[0] = q1[0] - q2[0];
1622 e[1] = q1[1] - q2[1];
1623 e[2] = q1[2] - q2[2];
1624 d += det3(e, f12, v);
1625 e[0] = q2[0] - q3[0];
1626 e[1] = q2[1] - q3[1];
1627 e[2] = q2[2] - q3[2];
1628 d += det3(e, f23, v);
1629 e[0] = q3[0] - q1[0];
1630 e[1] = q3[1] - q1[1];
1631 e[2] = q3[2] - q1[2];
1632 d += det3(e, f31, v);
1633 vol += d;
1634 }
1635 return vol * (Real(1) / Real(6));
1636 }
1637
1641 KOKKOS_INLINE_FUNCTION void facetAreasPerVertex(Real* area) const {
1642 for (int t = 0; t < nt; ++t) {
1643 if (!alive[t])
1644 continue;
1645 int k1 = t0[t], k2 = t1[t], k3 = t2[t];
1646 Real n1[3], n2[3], n3[3];
1647 planeN(k1, n1);
1648 planeN(k2, n2);
1649 planeN(k3, n3);
1650 Real c1[3], c2[3], c3[3];
1651 xprod(n2, n3, c1);
1652 xprod(n3, n1, c2);
1653 xprod(n1, n2, c3);
1654 Real D = dot3(n1, c1);
1655 if (D < Real(0)) { // canonical: swap planes 2,3 (normals, cross, AND indices)
1656 for (int a = 0; a < 3; ++a) {
1657 Real tm = n2[a];
1658 n2[a] = n3[a];
1659 n3[a] = tm;
1660 tm = c2[a];
1661 c2[a] = c3[a];
1662 c3[a] = tm;
1663 }
1664 int tk = k2;
1665 k2 = k3;
1666 k3 = tk;
1667 }
1668 const Real v[3] = {vx[t], vy[t], vz[t]};
1669 Real f12[3], f23[3], f31[3];
1670 edgeFoot(v, c3, f12);
1671 edgeFoot(v, c1, f23);
1672 edgeFoot(v, c2, f31);
1673 Real g[3];
1674 g[0] = f12[0] - f31[0];
1675 g[1] = f12[1] - f31[1];
1676 g[2] = f12[2] - f31[2];
1677 area[k1] += det3(n1, g, v) / (Real(2) * Kokkos::sqrt(dot3(n1, n1)));
1678 g[0] = f23[0] - f12[0];
1679 g[1] = f23[1] - f12[1];
1680 g[2] = f23[2] - f12[2];
1681 area[k2] += det3(n2, g, v) / (Real(2) * Kokkos::sqrt(dot3(n2, n2)));
1682 g[0] = f31[0] - f23[0];
1683 g[1] = f31[1] - f23[1];
1684 g[2] = f31[2] - f23[2];
1685 area[k3] += det3(n3, g, v) / (Real(2) * Kokkos::sqrt(dot3(n3, n3)));
1686 }
1687 }
1688
1693 KOKKOS_INLINE_FUNCTION static void scatterFacetMoment(int ki, const Real ni[3], const Real qi[3],
1694 const Real ff[3], const Real fl[3],
1695 const Real v[3], Real* area, Real* mx,
1696 Real* my, Real* mz) {
1697 const Real inv2n = Real(0.5) / Kokkos::sqrt(dot3(ni, ni));
1698 const Real sa = det3(ni, ff, v) * inv2n; // triangle (ni, ff, v)
1699 const Real sb = -det3(ni, fl, v) * inv2n; // triangle (ni, v, fl)
1700 area[ki] += sa + sb;
1701 mx[ki] += sa * (qi[0] + ff[0] + v[0]) / Real(3) + sb * (qi[0] + v[0] + fl[0]) / Real(3);
1702 my[ki] += sa * (qi[1] + ff[1] + v[1]) / Real(3) + sb * (qi[1] + v[1] + fl[1]) / Real(3);
1703 mz[ki] += sa * (qi[2] + ff[2] + v[2]) / Real(3) + sb * (qi[2] + v[2] + fl[2]) / Real(3);
1704 }
1705
1710 KOKKOS_INLINE_FUNCTION void facetMomentsPerVertex(Real* area, Real* mx, Real* my,
1711 Real* mz) const {
1712 for (int t = 0; t < nt; ++t) {
1713 if (!alive[t])
1714 continue;
1715 int k1 = t0[t], k2 = t1[t], k3 = t2[t];
1716 Real n1[3], n2[3], n3[3];
1717 planeN(k1, n1);
1718 planeN(k2, n2);
1719 planeN(k3, n3);
1720 Real q1[3], q2[3], q3[3]; // foot points (cone apexes, centroids)
1721 footPoint(k1, q1);
1722 footPoint(k2, q2);
1723 footPoint(k3, q3);
1724 Real c1[3], c2[3], c3[3];
1725 xprod(n2, n3, c1);
1726 xprod(n3, n1, c2);
1727 xprod(n1, n2, c3);
1728 if (dot3(n1, c1) < Real(0)) { // canonical: swap planes 2,3 (normals, cross, indices)
1729 for (int a = 0; a < 3; ++a) {
1730 Real tm = n2[a];
1731 n2[a] = n3[a];
1732 n3[a] = tm;
1733 tm = c2[a];
1734 c2[a] = c3[a];
1735 c3[a] = tm;
1736 tm = q2[a];
1737 q2[a] = q3[a];
1738 q3[a] = tm;
1739 }
1740 int tk = k2;
1741 k2 = k3;
1742 k3 = tk;
1743 }
1744 const Real v[3] = {vx[t], vy[t], vz[t]};
1745 Real f12[3], f23[3], f31[3];
1746 edgeFoot(v, c3, f12);
1747 edgeFoot(v, c1, f23);
1748 edgeFoot(v, c2, f31);
1749 scatterFacetMoment(k1, n1, q1, f12, f31, v, area, mx, my, mz);
1750 scatterFacetMoment(k2, n2, q2, f23, f12, v, area, mx, my, mz);
1751 scatterFacetMoment(k3, n3, q3, f31, f23, v, area, mx, my, mz);
1752 }
1753 }
1754
1761 KOKKOS_INLINE_FUNCTION static void scatterGradRaw(int ki, const Real ni[3], const Real qi[3],
1762 const Real ff[3], const Real fl[3],
1763 const Real v[3], Real* dgx, Real* dgy,
1764 Real* dgz) {
1765 const Real sa = det3(ni, ff, v); // raw signed area of triangle (ni, ff, v)
1766 const Real sb = -det3(ni, fl, v); // raw signed area of triangle (ni, v, fl)
1767 const Real da = sa + sb;
1768 const Real momx = sa * (qi[0] + ff[0] + v[0]) / Real(3) + sb * (qi[0] + v[0] + fl[0]) / Real(3);
1769 const Real momy = sa * (qi[1] + ff[1] + v[1]) / Real(3) + sb * (qi[1] + v[1] + fl[1]) / Real(3);
1770 const Real momz = sa * (qi[2] + ff[2] + v[2]) / Real(3) + sb * (qi[2] + v[2] + fl[2]) / Real(3);
1771 dgx[ki] += Real(2) * da * qi[0] - momx; // 2·da·n_k − mom (numerator piece)
1772 dgy[ki] += Real(2) * da * qi[1] - momy;
1773 dgz[ki] += Real(2) * da * qi[2] - momz;
1774 }
1775
1785 KOKKOS_INLINE_FUNCTION void geomVolumeGrad(Real& vol, Real* dgx, Real* dgy, Real* dgz) const {
1786 Real V = 0;
1787 for (int t = 0; t < nt; ++t) {
1788 if (!alive[t])
1789 continue;
1790 int k1 = t0[t], k2 = t1[t], k3 = t2[t];
1791 Real n1[3], n2[3], n3[3];
1792 planeN(k1, n1);
1793 planeN(k2, n2);
1794 planeN(k3, n3);
1795 Real q1[3], q2[3], q3[3]; // foot points (cone apexes, centroids)
1796 footPoint(k1, q1);
1797 footPoint(k2, q2);
1798 footPoint(k3, q3);
1799 Real c1[3], c2[3], c3[3];
1800 xprod(n2, n3, c1);
1801 xprod(n3, n1, c2);
1802 xprod(n1, n2, c3);
1803 if (dot3(n1, c1) < Real(0)) { // canonical D>0: swap planes 2,3 (normals, cross, indices)
1804 for (int a = 0; a < 3; ++a) {
1805 Real tm = n2[a];
1806 n2[a] = n3[a];
1807 n3[a] = tm;
1808 tm = c2[a];
1809 c2[a] = c3[a];
1810 c3[a] = tm;
1811 tm = q2[a];
1812 q2[a] = q3[a];
1813 q3[a] = tm;
1814 }
1815 int tk = k2;
1816 k2 = k3;
1817 k3 = tk;
1818 }
1819 const Real v[3] = {vx[t], vy[t], vz[t]};
1820 Real f12[3], f23[3], f31[3];
1821 edgeFoot(v, c3, f12);
1822 edgeFoot(v, c1, f23);
1823 edgeFoot(v, c2, f31);
1824 Real e[3];
1825 e[0] = q1[0] - q2[0];
1826 e[1] = q1[1] - q2[1];
1827 e[2] = q1[2] - q2[2];
1828 V += det3(e, f12, v);
1829 e[0] = q2[0] - q3[0];
1830 e[1] = q2[1] - q3[1];
1831 e[2] = q2[2] - q3[2];
1832 V += det3(e, f23, v);
1833 e[0] = q3[0] - q1[0];
1834 e[1] = q3[1] - q1[1];
1835 e[2] = q3[2] - q1[2];
1836 V += det3(e, f31, v);
1837 scatterGradRaw(k1, n1, q1, f12, f31, v, dgx, dgy, dgz);
1838 scatterGradRaw(k2, n2, q2, f23, f12, v, dgx, dgy, dgz);
1839 scatterGradRaw(k3, n3, q3, f31, f23, v, dgx, dgy, dgz);
1840 }
1841 for (int k = 0; k < np; ++k) { // per-facet fold, sqrt-free: numerator/(2·nn)
1842 const Real inv = (nn[k] != Real(0)) ? Real(1) / (Real(2) * nn[k]) : Real(0); // signed
1843 dgx[k] *= inv;
1844 dgy[k] *= inv;
1845 dgz[k] *= inv;
1846 }
1847 vol = V * (Real(1) / Real(6));
1848 }
1849
1854 KOKKOS_INLINE_FUNCTION static void scatterAreaMomentRaw(int ki, const Real ni[3],
1855 const Real qi[3], const Real ff[3],
1856 const Real fl[3], const Real v[3],
1857 Real* sa_acc, Real* smx, Real* smy,
1858 Real* smz) {
1859 const Real sa = det3(ni, ff, v); // raw signed area of triangle (ni, ff, v)
1860 const Real sb = -det3(ni, fl, v); // raw signed area of triangle (ni, v, fl)
1861 sa_acc[ki] += sa + sb;
1862 smx[ki] += sa * (qi[0] + ff[0] + v[0]) / Real(3) + sb * (qi[0] + v[0] + fl[0]) / Real(3);
1863 smy[ki] += sa * (qi[1] + ff[1] + v[1]) / Real(3) + sb * (qi[1] + v[1] + fl[1]) / Real(3);
1864 smz[ki] += sa * (qi[2] + ff[2] + v[2]) / Real(3) + sb * (qi[2] + v[2] + fl[2]) / Real(3);
1865 }
1866
1876 KOKKOS_INLINE_FUNCTION void geomVolumeArea(Real& vol, Real* avx, Real* avy, Real* avz, Real* dgx,
1877 Real* dgy, Real* dgz) const {
1878 Real V = 0;
1879 for (int t = 0; t < nt; ++t) {
1880 if (!alive[t])
1881 continue;
1882 int k1 = t0[t], k2 = t1[t], k3 = t2[t];
1883 Real n1[3], n2[3], n3[3];
1884 planeN(k1, n1);
1885 planeN(k2, n2);
1886 planeN(k3, n3);
1887 Real q1[3], q2[3], q3[3]; // foot points (cone apexes, centroids)
1888 footPoint(k1, q1);
1889 footPoint(k2, q2);
1890 footPoint(k3, q3);
1891 Real c1[3], c2[3], c3[3];
1892 xprod(n2, n3, c1);
1893 xprod(n3, n1, c2);
1894 xprod(n1, n2, c3);
1895 if (dot3(n1, c1) < Real(0)) { // canonical D>0: swap planes 2,3 (normals, cross, indices)
1896 for (int a = 0; a < 3; ++a) {
1897 Real tm = n2[a];
1898 n2[a] = n3[a];
1899 n3[a] = tm;
1900 tm = c2[a];
1901 c2[a] = c3[a];
1902 c3[a] = tm;
1903 tm = q2[a];
1904 q2[a] = q3[a];
1905 q3[a] = tm;
1906 }
1907 int tk = k2;
1908 k2 = k3;
1909 k3 = tk;
1910 }
1911 const Real v[3] = {vx[t], vy[t], vz[t]};
1912 Real f12[3], f23[3], f31[3];
1913 edgeFoot(v, c3, f12);
1914 edgeFoot(v, c1, f23);
1915 edgeFoot(v, c2, f31);
1916 Real e[3];
1917 e[0] = q1[0] - q2[0];
1918 e[1] = q1[1] - q2[1];
1919 e[2] = q1[2] - q2[2];
1920 V += det3(e, f12, v);
1921 e[0] = q2[0] - q3[0];
1922 e[1] = q2[1] - q3[1];
1923 e[2] = q2[2] - q3[2];
1924 V += det3(e, f23, v);
1925 e[0] = q3[0] - q1[0];
1926 e[1] = q3[1] - q1[1];
1927 e[2] = q3[2] - q1[2];
1928 V += det3(e, f31, v);
1929 scatterAreaMomentRaw(k1, n1, q1, f12, f31, v, avx, dgx, dgy,
1930 dgz); // avx <- S_a, (dgx,dgy,dgz) <- S_m
1931 scatterAreaMomentRaw(k2, n2, q2, f23, f12, v, avx, dgx, dgy, dgz);
1932 scatterAreaMomentRaw(k3, n3, q3, f31, f23, v, avx, dgx, dgy, dgz);
1933 }
1934 for (int k = 0; k < np; ++k) { // per-facet fold, sqrt-free
1935 const Real inv = (nn[k] != Real(0)) ? Real(1) / (Real(2) * nn[k]) : Real(0); // signed
1936 const Real inva = (nn[k] != Real(0)) ? Real(1) / (Real(2) * Kokkos::fabs(nn[k])) : Real(0);
1937 Real qk[3];
1938 footPoint(k, qk);
1939 const Real sa = avx[k]; // raw area S_a (accumulated here during the scatter)
1940 const Real smx = dgx[k], smy = dgy[k], smz = dgz[k];
1941 dgx[k] = (Real(2) * sa * qk[0] - smx) * inv; // dV/dn_k
1942 dgy[k] = (Real(2) * sa * qk[1] - smy) * inv;
1943 dgz[k] = (Real(2) * sa * qk[2] - smz) * inv;
1944 avx[k] = sa * n[k][0] * inva; // outward area-vector A_k·n_k/|n_k|
1945 avy[k] = sa * n[k][1] * inva;
1946 avz[k] = sa * n[k][2] * inva;
1947 }
1948 vol = V * (Real(1) / Real(6));
1949 }
1950
1953 KOKKOS_INLINE_FUNCTION static void edgeCrossAdj(int j, int p, int q, const Real Nn[3][3],
1954 const Real w[3], Real out[3]) {
1955 if (j == p) {
1956 xprod(Nn[q], w, out);
1957 } else if (j == q) {
1958 Real tm[3];
1959 xprod(Nn[p], w, tm);
1960 out[0] = -tm[0];
1961 out[1] = -tm[1];
1962 out[2] = -tm[2];
1963 } else {
1964 out[0] = out[1] = out[2] = Real(0);
1965 }
1966 }
1967
1979 KOKKOS_INLINE_FUNCTION void geomVolumeAreaGrad(int t, int pl[3], Real contrib[3],
1980 Real grad[3][3][3]) const {
1981 // planes + canonical swap (matches facetAreasPerVertex), reusing the cached vertex — nothing
1982 // recomputed. Every loop is VOR_UNROLL'd so the small local arrays scalarize into registers on
1983 // GPU (no local-memory spill from runtime indices); on CPU the macro is empty.
1984 int kk[3] = {t0[t], t1[t], t2[t]};
1985 Real Nn[3][3];
1986 VOR_UNROLL for (int a = 0; a < 3; ++a) {
1987 Nn[a][0] = n[kk[a]][0];
1988 Nn[a][1] = n[kk[a]][1];
1989 Nn[a][2] = n[kk[a]][2];
1990 }
1991 {
1992 Real c01[3];
1993 xprod(Nn[1], Nn[2], c01);
1994 if (dot3(Nn[0], c01) < Real(0)) {
1995 VOR_UNROLL for (int d = 0; d < 3; ++d) {
1996 Real tm = Nn[1][d];
1997 Nn[1][d] = Nn[2][d];
1998 Nn[2][d] = tm;
1999 }
2000 int tk = kk[1];
2001 kk[1] = kk[2];
2002 kk[2] = tk;
2003 }
2004 }
2005 pl[0] = kk[0];
2006 pl[1] = kk[1];
2007 pl[2] = kk[2];
2008 const Real v[3] = {vx[t], vy[t], vz[t]};
2009 Real nn3[3], u[3][3]; // u[a] = 2 q_a − v (q the foot point; = n_a for σ = +1)
2010 VOR_UNROLL for (int a = 0; a < 3; ++a) {
2011 nn3[a] = dot3(Nn[a], Nn[a]); // |n_a|² (= |nn|)
2012 Real qa[3];
2013 footPoint(kk[a], qa);
2014 VOR_UNROLL for (int d = 0; d < 3; ++d) u[a][d] = Real(2) * qa[d] - v[d];
2015 }
2016 // edge directions e[m] = N[eP]×N[eQ] for edges (0,1),(1,2),(2,0); cofactor of plane j is c_j =
2017 // e[(j+1)%3]
2018 const int eP[3] = {0, 1, 2}, eQ[3] = {1, 2, 0};
2019 Real e[3][3], ecc[3], es[3], F[3][3];
2020 VOR_UNROLL for (int m = 0; m < 3; ++m) {
2021 xprod(Nn[eP[m]], Nn[eQ[m]], e[m]);
2022 ecc[m] = dot3(e[m], e[m]);
2023 const Real evc = dot3(v, e[m]);
2024 es[m] = (ecc[m] > Real(0)) ? evc / ecc[m] : Real(0);
2025 VOR_UNROLL for (int d = 0; d < 3; ++d) F[m][d] =
2026 v[d] - es[m] * e[m][d]; // foot of v on edge line m
2027 }
2028 const Real D = dot3(Nn[0], e[1]); // e[1] = N1×N2 = cofactor c_0; D = det(n0,n1,n2) > 0
2029 Real g[3][3], den[3];
2030 VOR_UNROLL for (int i = 0; i < 3; ++i) { // facet i: g_i = F[i] − F[(i+2)%3]
2031 const int mb = (i + 2) % 3;
2032 VOR_UNROLL for (int d = 0; d < 3; ++d) g[i][d] = F[i][d] - F[mb][d];
2033 const Real Ni = det3(Nn[i], g[i], v);
2034 den[i] = Real(2) * Kokkos::sqrt(nn3[i]);
2035 contrib[i] = (den[i] > Real(0)) ? Ni / den[i] : Real(0);
2036 }
2037 VOR_UNROLL for (int i = 0; i < 3; ++i) {
2038 Real wi[3];
2039 xprod(v, Nn[i], wi); // w_i = v×n_i (∂det3(n_i,·,v)/∂g)
2040 Real nixg[3];
2041 xprod(Nn[i], g[i], nixg); // n_i×g_i (∂det3(n_i,g_i,·)/∂v)
2042 const int mb = (i + 2) % 3;
2043 VOR_UNROLL for (int j = 0; j < 3; ++j) {
2044 const Real* cj = e[(j + 1) % 3]; // cofactor of plane j
2045 Real gN[3] = {0, 0, 0};
2046 if (i == j) {
2047 Real gxv[3];
2048 xprod(g[i], v, gxv);
2049 VOR_UNROLL for (int d = 0; d < 3; ++d) gN[d] += gxv[d];
2050 } // ∂/∂n_i
2051 const Real alpha = dot3(nixg, cj) / D; // ∂/∂v (rank-1)
2052 VOR_UNROLL for (int d = 0; d < 3; ++d) gN[d] += alpha * u[j][d];
2053 // ∂/∂g = (∂F[i]/∂n_j − ∂F[(i+2)%3]/∂n_j)^T w_i — the two foot adjoints
2054 VOR_UNROLL for (int s = 0; s < 2; ++s) {
2055 const int m = (s == 0) ? i : mb;
2056 const Real sgn = (s == 0) ? Real(1) : Real(-1);
2057 const int p = eP[m], q = eQ[m];
2058 const Real cjw = dot3(cj, wi); // for vAdj
2059 const Real cje = dot3(cj, e[m]); // for dS
2060 const Real emw = dot3(e[m], wi);
2061 Real eaW[3], eaV[3], eaE[3];
2062 edgeCrossAdj(j, p, q, Nn, wi, eaW);
2063 edgeCrossAdj(j, p, q, Nn, v, eaV);
2064 edgeCrossAdj(j, p, q, Nn, e[m], eaE);
2065 const Real inv_ecc = (ecc[m] > Real(0)) ? Real(1) / ecc[m] : Real(0);
2066 Real dS[3]; // ∂s_m/∂n_j
2067 VOR_UNROLL for (int d = 0; d < 3; ++d) dS[d] =
2068 ((cje / D) * u[j][d] + eaV[d]) * inv_ecc - (es[m] * inv_ecc) * Real(2) * eaE[d];
2069 // (∂F[m]/∂n_j)^T w_i = (c_j·w_i/D) u_j − (e_m·w_i) dS − s_m (∂e_m/∂n_j)^T w_i
2070 VOR_UNROLL for (int d = 0; d < 3; ++d) gN[d] +=
2071 sgn * ((cjw / D) * u[j][d] - emw * dS[d] - es[m] * eaW[d]);
2072 }
2073 const Real invden = (den[i] > Real(0)) ? Real(1) / den[i] : Real(0);
2074 VOR_UNROLL for (int d = 0; d < 3; ++d) grad[i][j][d] = gN[d] * invden;
2075 if (i == j) { // 1/(2|n_i|) factor: −A_i n_i/nn_i
2076 const Real f = (nn3[i] > Real(0)) ? contrib[i] / nn3[i] : Real(0);
2077 VOR_UNROLL for (int d = 0; d < 3; ++d) grad[i][j][d] -= f * Nn[i][d];
2078 }
2079 }
2080 }
2081 // ∂/∂n_j → ∂/∂q_j (n_j = σ_j q_j): the outputs are derivatives w.r.t. the foot point.
2082 VOR_UNROLL for (int j = 0; j < 3; ++j) {
2083 const Real sg = sigmaOf(pl[j]);
2084 VOR_UNROLL for (int i = 0; i < 3; ++i) VOR_UNROLL for (int d = 0; d < 3; ++d) grad[i][j][d] *=
2085 sg;
2086 }
2087 }
2088
2093 KOKKOS_INLINE_FUNCTION void geomVolumeAreaGradAD(int t, int pl[3], Real contrib[3],
2094 Real grad[3][3][3]) const {
2095 using D = detail::Dual<Real, 9>;
2096 int k1 = t0[t], k2 = t1[t], k3 = t2[t];
2097 Real rn1[3] = {n[k1][0], n[k1][1], n[k1][2]};
2098 Real rn2[3] = {n[k2][0], n[k2][1], n[k2][2]};
2099 Real rn3[3] = {n[k3][0], n[k3][1], n[k3][2]};
2100 { // canonical D>0 swap on the real values (same as facetAreasPerVertex) so contributions land
2101 // right
2102 Real cc[3];
2103 xprod(rn2, rn3, cc);
2104 if (dot3(rn1, cc) < Real(0)) {
2105 for (int a = 0; a < 3; ++a) {
2106 Real tm = rn2[a];
2107 rn2[a] = rn3[a];
2108 rn3[a] = tm;
2109 }
2110 int tk = k2;
2111 k2 = k3;
2112 k3 = tk;
2113 }
2114 }
2115 pl[0] = k1;
2116 pl[1] = k2;
2117 pl[2] = k3;
2118 D n1[3], n2[3], n3[3];
2119 for (int a = 0; a < 3; ++a) { // seed: n1 <- slots 0..2, n2 <- 3..5, n3 <- 6..8
2120 n1[a] = detail::dseed<Real, 9>(rn1[a], a);
2121 n2[a] = detail::dseed<Real, 9>(rn2[a], 3 + a);
2122 n3[a] = detail::dseed<Real, 9>(rn3[a], 6 + a);
2123 }
2124 const D nn1 = detail::ddot<Real, 9>(n1, n1), nn2 = detail::ddot<Real, 9>(n2, n2),
2125 nn3 = detail::ddot<Real, 9>(n3, n3);
2126 D c1[3], c2[3], c3[3];
2127 detail::dcross<Real, 9>(n2, n3, c1);
2128 detail::dcross<Real, 9>(n3, n1, c2);
2129 detail::dcross<Real, 9>(n1, n2, c3);
2130 const D Dd = detail::ddot<Real, 9>(n1, c1); // det
2131 // signed offsets σ|n|² in the vertex solve ONLY; the area denominators keep |n| = sqrt(n·n)
2132 const D s1 = detail::dnum<Real, 9>(sigmaOf(k1)), s2 = detail::dnum<Real, 9>(sigmaOf(k2)),
2134 D v[3];
2135 for (int a = 0; a < 3; ++a)
2136 v[a] = (s1 * nn1 * c1[a] + s2 * nn2 * c2[a] + s3 * nn3 * c3[a]) / Dd; // Cramer: n·v = σ|n|²
2137 D f12[3], f23[3], f31[3];
2138 detail::dedgeFoot<Real, 9>(v, c3, f12);
2139 detail::dedgeFoot<Real, 9>(v, c1, f23);
2140 detail::dedgeFoot<Real, 9>(v, c2, f31);
2141 const D two = detail::dnum<Real, 9>(Real(2));
2142 D g[3], aa[3];
2143 for (int a = 0; a < 3; ++a)
2144 g[a] = f12[a] - f31[a];
2145 aa[0] = detail::ddet3<Real, 9>(n1, g, v) / (two * detail::dsqrt<Real, 9>(nn1));
2146 for (int a = 0; a < 3; ++a)
2147 g[a] = f23[a] - f12[a];
2148 aa[1] = detail::ddet3<Real, 9>(n2, g, v) / (two * detail::dsqrt<Real, 9>(nn2));
2149 for (int a = 0; a < 3; ++a)
2150 g[a] = f31[a] - f23[a];
2151 aa[2] = detail::ddet3<Real, 9>(n3, g, v) / (two * detail::dsqrt<Real, 9>(nn3));
2152 for (int i = 0; i < 3; ++i) {
2153 contrib[i] = aa[i].v;
2154 for (int j = 0; j < 3; ++j)
2155 for (int c = 0; c < 3; ++c)
2156 grad[i][j][c] = aa[i].d[3 * j + c] * sigmaOf(pl[j]); // ∂/∂q_j
2157 }
2158 }
2159
2168 KOKKOS_INLINE_FUNCTION void geometryPerVertex(Real& vol, Real* area, Real* mx, Real* my,
2169 Real* mz) const {
2170 Real V = 0;
2171 for (int t = 0; t < nt; ++t) {
2172 if (!alive[t])
2173 continue;
2174 int k1 = t0[t], k2 = t1[t], k3 = t2[t];
2175 Real n1[3], n2[3], n3[3];
2176 planeN(k1, n1);
2177 planeN(k2, n2);
2178 planeN(k3, n3);
2179 Real q1[3], q2[3], q3[3]; // foot points (cone apexes, centroids)
2180 footPoint(k1, q1);
2181 footPoint(k2, q2);
2182 footPoint(k3, q3);
2183 Real c1[3], c2[3], c3[3];
2184 xprod(n2, n3, c1);
2185 xprod(n3, n1, c2);
2186 xprod(n1, n2, c3);
2187 if (dot3(n1, c1) < Real(0)) { // canonical D>0: swap planes 2,3 (normals, cross, indices)
2188 for (int a = 0; a < 3; ++a) {
2189 Real tm = n2[a];
2190 n2[a] = n3[a];
2191 n3[a] = tm;
2192 tm = c2[a];
2193 c2[a] = c3[a];
2194 c3[a] = tm;
2195 tm = q2[a];
2196 q2[a] = q3[a];
2197 q3[a] = tm;
2198 }
2199 int tk = k2;
2200 k2 = k3;
2201 k3 = tk;
2202 }
2203 const Real v[3] = {vx[t], vy[t], vz[t]};
2204 Real f12[3], f23[3], f31[3];
2205 edgeFoot(v, c3, f12);
2206 edgeFoot(v, c1, f23);
2207 edgeFoot(v, c2, f31);
2208 Real e[3];
2209 e[0] = q1[0] - q2[0];
2210 e[1] = q1[1] - q2[1];
2211 e[2] = q1[2] - q2[2];
2212 V += det3(e, f12, v);
2213 e[0] = q2[0] - q3[0];
2214 e[1] = q2[1] - q3[1];
2215 e[2] = q2[2] - q3[2];
2216 V += det3(e, f23, v);
2217 e[0] = q3[0] - q1[0];
2218 e[1] = q3[1] - q1[1];
2219 e[2] = q3[2] - q1[2];
2220 V += det3(e, f31, v);
2221 scatterFacetMoment(k1, n1, q1, f12, f31, v, area, mx, my, mz);
2222 scatterFacetMoment(k2, n2, q2, f23, f12, v, area, mx, my, mz);
2223 scatterFacetMoment(k3, n3, q3, f31, f23, v, area, mx, my, mz);
2224 }
2225 vol = V * (Real(1) / Real(6));
2226 }
2227
2235 KOKKOS_INLINE_FUNCTION bool facetGeometry(int k, Real areaVec[3], Real dv[3],
2236 Real conn[3]) const {
2237 const Real r[3] = {Real(2) * n[k][0], Real(2) * n[k][1],
2238 Real(2) * n[k][2]}; // connector r = 2·foot
2239 if (!facetGeometryFrom(k, r, areaVec, dv))
2240 return false;
2241 conn[0] = r[0];
2242 conn[1] = r[1];
2243 conn[2] = r[2];
2244 return true;
2245 }
2246
2258 KOKKOS_INLINE_FUNCTION bool facetGeometryFrom(int k, const Real r[3], Real areaVec[3],
2259 Real dv[3]) const {
2260 Real fx[MAXFV], fy[MAXFV], fz[MAXFV];
2261 const int m = faceOrdered(k, fx, fy, fz);
2262 if (m < 3)
2263 return false;
2264 Real A[3];
2265 polyAreaVec(fx, fy, fz, m, A);
2266 if (A[0] * r[0] + A[1] * r[1] + A[2] * r[2] < Real(0)) { // orient outward (toward neighbour)
2267 A[0] = -A[0];
2268 A[1] = -A[1];
2269 A[2] = -A[2];
2270 }
2271 const Real area = Kokkos::sqrt(A[0] * A[0] + A[1] * A[1] + A[2] * A[2]);
2272 // area-weighted centroid (fan-triangulate from v0)
2273 Real cx = 0, cy = 0, cz = 0, asum = 0;
2274 for (int i = 1; i + 1 < m; ++i) {
2275 const Real ux = fx[i] - fx[0], uy = fy[i] - fy[0], uz = fz[i] - fz[0];
2276 const Real wx = fx[i + 1] - fx[0], wy = fy[i + 1] - fy[0], wz = fz[i + 1] - fz[0];
2277 const Real tx = uy * wz - uz * wy, ty = uz * wx - ux * wz, tz = ux * wy - uy * wx;
2278 const Real at = Real(0.5) * Kokkos::sqrt(tx * tx + ty * ty + tz * tz);
2279 cx += at * (fx[0] + fx[i] + fx[i + 1]);
2280 cy += at * (fy[0] + fy[i] + fy[i + 1]);
2281 cz += at * (fz[0] + fz[i] + fz[i + 1]);
2282 asum += at;
2283 }
2284 const Real inv = asum > Real(0) ? Real(1) / (Real(3) * asum) : Real(0);
2285 cx *= inv;
2286 cy *= inv;
2287 cz *= inv;
2288 const Real s = area / Kokkos::sqrt(r[0] * r[0] + r[1] * r[1] + r[2] * r[2]);
2289 areaVec[0] = A[0];
2290 areaVec[1] = A[1];
2291 areaVec[2] = A[2];
2292 dv[0] = s * (r[0] - cx);
2293 dv[1] = s * (r[1] - cy);
2294 dv[2] = s * (r[2] - cz);
2295 return true;
2296 }
2297};
2298
2306template <class Real, int MAXP, int MAXT, bool TrackAdj, class Policy = Voronoi>
2308 const Real L[3], const Real* relx, const Real* rely,
2309 const Real* relz, const int* ids, int nNbr,
2310 Real wSelf = Real(0), const Real* weights = nullptr) {
2311 c.initBox(L[0], L[1], L[2]);
2312 for (int i = 0; i < nNbr; ++i) {
2313 const Real n[3] = {relx[i], rely[i], relz[i]};
2314 Real wNbr = Real(0);
2315 if constexpr (Policy::kHasWeightDof)
2316 wNbr = weights[i];
2317 const Real off = Policy::template offsetFromRel<Real>(n, wSelf, wNbr);
2318 if constexpr (!Policy::kHasWeightDof) {
2319 if (!(off < Real(2) * c.maxVertexRsq()))
2320 break; // security radius (bisector certificate; Voronoi only)
2321 }
2322 c.clip(n, off, ids[i]);
2323 if (c.overflow)
2324 return;
2325 }
2326}
2327
2328} // namespace peclet::voro
2329
2330#endif // PECLET_VORO_CONVEX_CELL_HPP
#define VOR_UNROLL
Definition convex_cell.hpp:38
KOKKOS_INLINE_FUNCTION Dual< Real, K > dnum(Real c)
Definition convex_cell.hpp:53
KOKKOS_INLINE_FUNCTION void dedgeFoot(const Dual< Real, K > v[3], const Dual< Real, K > c[3], Dual< Real, K > f[3])
Definition convex_cell.hpp:127
KOKKOS_INLINE_FUNCTION Dual< Real, K > ddet3(const Dual< Real, K > a[3], const Dual< Real, K > b[3], const Dual< Real, K > c[3])
Definition convex_cell.hpp:120
KOKKOS_INLINE_FUNCTION Dual< Real, K > operator-(const Dual< Real, K > &a, const Dual< Real, K > &b)
Definition convex_cell.hpp:75
KOKKOS_INLINE_FUNCTION Dual< Real, K > operator*(const Dual< Real, K > &a, const Dual< Real, K > &b)
Definition convex_cell.hpp:83
KOKKOS_INLINE_FUNCTION Dual< Real, K > operator+(const Dual< Real, K > &a, const Dual< Real, K > &b)
Definition convex_cell.hpp:67
KOKKOS_INLINE_FUNCTION Dual< Real, K > dsqrt(const Dual< Real, K > &a)
Definition convex_cell.hpp:100
KOKKOS_INLINE_FUNCTION Dual< Real, K > dseed(Real c, int s)
Definition convex_cell.hpp:61
KOKKOS_INLINE_FUNCTION Dual< Real, K > ddot(const Dual< Real, K > a[3], const Dual< Real, K > b[3])
Definition convex_cell.hpp:109
KOKKOS_INLINE_FUNCTION void dcross(const Dual< Real, K > a[3], const Dual< Real, K > b[3], Dual< Real, K > o[3])
Definition convex_cell.hpp:113
KOKKOS_INLINE_FUNCTION Dual< Real, K > operator/(const Dual< Real, K > &a, const Dual< Real, K > &b)
Definition convex_cell.hpp:91
Device-native Voronoi tessellation, geometry and finite-volume physics.
Definition convex_cell.hpp:41
KOKKOS_INLINE_FUNCTION void buildConvexCell(ConvexCell< Real, MAXP, MAXT, TrackAdj > &c, const Real L[3], const Real *relx, const Real *rely, const Real *relz, const int *ids, int nNbr, Real wSelf=Real(0), const Real *weights=nullptr)
Definition convex_cell.hpp:2307
The named engine defaults of peclet.voro — one header-level home for every capacity and default the l...
Definition convex_cell.hpp:140
KOKKOS_INLINE_FUNCTION bool facetGeometryFrom(int k, const Real r[3], Real areaVec[3], Real dv[3]) const
Definition convex_cell.hpp:2258
KOKKOS_INLINE_FUNCTION Real volumePerVertex() const
Definition convex_cell.hpp:1585
static constexpr int MAXFV
Definition convex_cell.hpp:1307
KOKKOS_INLINE_FUNCTION int across(int t, int e) const
Neighbour of triangle t across its edge slot e (slot convention of adj).
Definition convex_cell.hpp:1189
KOKKOS_INLINE_FUNCTION int findSharing(int self, int x, int y) const
Definition convex_cell.hpp:694
Kokkos::Array< int,(TrackAdj ? MAXT :0) *3 > adj
Definition convex_cell.hpp:178
KOKKOS_INLINE_FUNCTION int compactPlanes()
Definition convex_cell.hpp:633
KOKKOS_INLINE_FUNCTION bool anyKilled(const bool *kill) const
Definition convex_cell.hpp:788
KOKKOS_INLINE_FUNCTION Real planeGap(const Real pdir[3], Real d) const
Definition convex_cell.hpp:611
KOKKOS_INLINE_FUNCTION bool isLocallyConvexPartners(const unsigned char *poke4, Real tol, int *outPartners, int maxPartners, int &nPartners) const
Definition convex_cell.hpp:537
static KOKKOS_INLINE_FUNCTION void scatterAreaMomentRaw(int ki, const Real ni[3], const Real qi[3], const Real ff[3], const Real fl[3], const Real v[3], Real *sa_acc, Real *smx, Real *smy, Real *smz)
Definition convex_cell.hpp:1854
KOKKOS_INLINE_FUNCTION int faceOrderedIdx(int k, int out[MAXFV]) const
Definition convex_cell.hpp:1314
KOKKOS_INLINE_FUNCTION int sectionPolygon(const Real p0[3], const Real u[3], Real px[MAXSV], Real py[MAXSV], Real pz[MAXSV]) const
Definition convex_cell.hpp:1433
KOKKOS_INLINE_FUNCTION void facetMomentsPerVertex(Real *area, Real *mx, Real *my, Real *mz) const
Definition convex_cell.hpp:1710
KOKKOS_INLINE_FUNCTION void setHalfSpace(int k, const Real p[3], Real d)
Definition convex_cell.hpp:217
KOKKOS_INLINE_FUNCTION int oppositePlane(int s, int x, int y) const
Definition convex_cell.hpp:461
unsigned char t1[MAXT]
Definition convex_cell.hpp:156
KOKKOS_INLINE_FUNCTION void facetAreasPerVertex(Real *area) const
Definition convex_cell.hpp:1641
KOKKOS_INLINE_FUNCTION int growDisk(bool *kill, int pi)
Definition convex_cell.hpp:1214
static KOKKOS_INLINE_FUNCTION void edgeFoot(const Real v[3], const Real ck[3], Real f[3])
foot of the perpendicular from v onto the edge line of direction ck: v − (v·ck/ck·ck) ck.
Definition convex_cell.hpp:1576
KOKKOS_INLINE_FUNCTION void footPoint(int k, Real out[3]) const
Foot of the perpendicular from the seed onto plane k: q_k = σ_k n_k (exact negation).
Definition convex_cell.hpp:244
static KOKKOS_INLINE_FUNCTION void scatterGradRaw(int ki, const Real ni[3], const Real qi[3], const Real ff[3], const Real fl[3], const Real v[3], Real *dgx, Real *dgy, Real *dgz)
Definition convex_cell.hpp:1761
KOKKOS_INLINE_FUNCTION bool simpleHorizonCycle(const bool *kill, const int *hA, int H) const
Definition convex_cell.hpp:850
KOKKOS_INLINE_FUNCTION Real faceMoment2(int k) const
Definition convex_cell.hpp:578
KOKKOS_INLINE_FUNCTION Real sigmaOf(int k) const
σ_k = sign(nn_k) as ±1 (+1 for nn_k = 0).
Definition convex_cell.hpp:256
static KOKKOS_INLINE_FUNCTION Real dot3(const Real a[3], const Real b[3])
Definition convex_cell.hpp:1567
KOKKOS_INLINE_FUNCTION void initBoxPlanes(Real L0, Real L1, Real L2)
Definition convex_cell.hpp:306
static constexpr bool kTrackAdj
Definition convex_cell.hpp:144
KOKKOS_INLINE_FUNCTION void initBox(Real L0, Real L1, Real L2)
Definition convex_cell.hpp:182
static constexpr int kMaxT
dual-triangle (vertex) capacity
Definition convex_cell.hpp:143
bool overflow
set if MAXP/MAXT exceeded -> cell invalid, caller falls back
Definition convex_cell.hpp:160
int nt
triangle high-water mark
Definition convex_cell.hpp:159
KOKKOS_INLINE_FUNCTION bool allLiveKilled(const bool *kill) const
Definition convex_cell.hpp:797
Real nn[MAXP]
signed offset σ·|n|²: the half-space test is n·x <= nn
Definition convex_cell.hpp:153
Real n[MAXP][3]
Definition convex_cell.hpp:152
static constexpr Real kFanSine2
Definition convex_cell.hpp:759
static KOKKOS_INLINE_FUNCTION void polyAreaVec(const Real fx[], const Real fy[], const Real fz[], int m, Real A[3])
Polygon area vector (0.5 Σ vi × v(i+1)) of an ordered face.
Definition convex_cell.hpp:1408
static KOKKOS_INLINE_FUNCTION void scatterFacetMoment(int ki, const Real ni[3], const Real qi[3], const Real ff[3], const Real fl[3], const Real v[3], Real *area, Real *mx, Real *my, Real *mz)
Definition convex_cell.hpp:1693
KOKKOS_INLINE_FUNCTION bool isLocallyConvex(const unsigned char *poke4, Real tol) const
Definition convex_cell.hpp:512
KOKKOS_INLINE_FUNCTION int walkHorizon(bool *kill, int pi, int *hA, int *hB, int *hSurv, int *hBack, int &nk)
Definition convex_cell.hpp:1086
KOKKOS_INLINE_FUNCTION void rebuildAdjacency()
Definition convex_cell.hpp:709
KOKKOS_INLINE_FUNCTION int faceOrdered(int k, Real fx[MAXFV], Real fy[MAXFV], Real fz[MAXFV]) const
Definition convex_cell.hpp:1395
Real vy[MAXT]
Definition convex_cell.hpp:157
KOKKOS_INLINE_FUNCTION bool isSelfConsistentPartners(Real tol, int *outPartners, int maxPartners, int &nPartners) const
Definition convex_cell.hpp:424
KOKKOS_INLINE_FUNCTION bool simpleHorizonEdges(const unsigned char *nA, const unsigned char *nB, int H) const
Definition convex_cell.hpp:816
KOKKOS_INLINE_FUNCTION void geomVolumeAreaGrad(int t, int pl[3], Real contrib[3], Real grad[3][3][3]) const
Definition convex_cell.hpp:1979
KOKKOS_INLINE_FUNCTION bool empty() const
Definition convex_cell.hpp:1287
int pnbr[MAXP]
neighbour seed id per plane (<0 => bounding box)
Definition convex_cell.hpp:154
static constexpr int MAXSV
max vertices on a cross-section polygon
Definition convex_cell.hpp:1422
KOKKOS_INLINE_FUNCTION void geomVolumeGrad(Real &vol, Real *dgx, Real *dgy, Real *dgz) const
Definition convex_cell.hpp:1785
KOKKOS_INLINE_FUNCTION Real maxVertexRsq() const
Largest squared dual-vertex radius over live triangles (drives the security radius).
Definition convex_cell.hpp:676
static KOKKOS_INLINE_FUNCTION void edgeCrossAdj(int j, int p, int q, const Real Nn[3][3], const Real w[3], Real out[3])
Definition convex_cell.hpp:1953
KOKKOS_INLINE_FUNCTION bool isSelfConsistent(Real tol) const
Definition convex_cell.hpp:384
KOKKOS_INLINE_FUNCTION bool checkAdjacencyInvariant() const
Definition convex_cell.hpp:725
Real vx[MAXT]
Definition convex_cell.hpp:157
KOKKOS_INLINE_FUNCTION void geometryPerVertex(Real &vol, Real *area, Real *mx, Real *my, Real *mz) const
Definition convex_cell.hpp:2168
KOKKOS_INLINE_FUNCTION int countFaces() const
Definition convex_cell.hpp:1294
KOKKOS_INLINE_FUNCTION bool diskHorizon(int k, int H, const unsigned char *nA, const unsigned char *nB) const
Definition convex_cell.hpp:807
unsigned char t2[MAXT]
triangle = triple of plane indices
Definition convex_cell.hpp:156
KOKKOS_INLINE_FUNCTION void geomVolumeArea(Real &vol, Real *avx, Real *avy, Real *avz, Real *dgx, Real *dgy, Real *dgz) const
Definition convex_cell.hpp:1876
KOKKOS_INLINE_FUNCTION void computeVertex(int t)
Definition convex_cell.hpp:284
int np
number of planes
Definition convex_cell.hpp:155
KOKKOS_INLINE_FUNCTION bool clip(const Real pdir[3], Real d, int nbr, Real *gapOut=nullptr)
Definition convex_cell.hpp:895
static constexpr int kMaxP
plane capacity (consumers size scratch by these)
Definition convex_cell.hpp:142
KOKKOS_INLINE_FUNCTION Real volume() const
Definition convex_cell.hpp:1528
KOKKOS_INLINE_FUNCTION int collectHorizon(bool *kill, int pi, unsigned char *nA, unsigned char *nB, int &nk)
Definition convex_cell.hpp:1044
KOKKOS_INLINE_FUNCTION void geomVolumeAreaGradAD(int t, int pl[3], Real contrib[3], Real grad[3][3][3]) const
Definition convex_cell.hpp:2093
bool degenerate
Definition convex_cell.hpp:161
KOKKOS_INLINE_FUNCTION void planeN(int k, Real out[3]) const
Definition convex_cell.hpp:1557
KOKKOS_INLINE_FUNCTION Real planeDistance(int k) const
Seed->plane distance |n_k|.
Definition convex_cell.hpp:258
unsigned char t0[MAXT]
Definition convex_cell.hpp:156
KOKKOS_INLINE_FUNCTION void reevalGeometry(Real sx, Real sy, Real sz, const Real *pos, Real L, Real wSelf=Real(0), const Real *weight=nullptr)
Definition convex_cell.hpp:335
KOKKOS_INLINE_FUNCTION int allocTri()
Definition convex_cell.hpp:875
KOKKOS_INLINE_FUNCTION bool anyLiveNegOffset() const
Definition convex_cell.hpp:268
KOKKOS_INLINE_FUNCTION void computePoke4(unsigned char *out) const
Definition convex_cell.hpp:474
bool alive[MAXT]
triangle live flag
Definition convex_cell.hpp:158
KOKKOS_INLINE_FUNCTION bool inconsistentFan(int x, int y, int z, int p) const
Definition convex_cell.hpp:777
KOKKOS_INLINE_FUNCTION Real signedSupport(int k) const
Signed support h_k: the seed's signed distance to plane k, positive inside.
Definition convex_cell.hpp:262
KOKKOS_INLINE_FUNCTION bool facetGeometry(int k, Real areaVec[3], Real dv[3], Real conn[3]) const
Definition convex_cell.hpp:2235
Real vz[MAXT]
cached dual-vertex position per triangle
Definition convex_cell.hpp:157
static KOKKOS_INLINE_FUNCTION Real det3(const Real a[3], const Real b[3], const Real c[3])
Definition convex_cell.hpp:1570
static KOKKOS_INLINE_FUNCTION void xprod(const Real a[3], const Real b[3], Real o[3])
Definition convex_cell.hpp:1562
Definition convex_cell.hpp:48
Real d[K]
Definition convex_cell.hpp:50
Real v
Definition convex_cell.hpp:49