24#ifndef PECLET_VORO_CONVEX_CELL_HPP
25#define PECLET_VORO_CONVEX_CELL_HPP
27#include <Kokkos_Core.hpp>
35#if defined(__CUDA_ARCH__) || defined(__HIP_DEVICE_COMPILE__)
36#define VOR_UNROLL _Pragma("unroll")
47template <
class Real,
int K>
52template <
class Real,
int K>
56 for (
int i = 0; i < K; ++i)
60template <
class Real,
int K>
66template <
class Real,
int K>
70 for (
int i = 0;
i <
K; ++
i)
74template <
class Real,
int K>
78 for (
int i = 0;
i <
K; ++
i)
82template <
class Real,
int K>
86 for (
int i = 0;
i <
K; ++
i)
87 r.
d[
i] = a.
d[
i] * b.
v + a.
v * b.
d[
i];
90template <
class Real,
int K>
95 for (
int i = 0;
i <
K; ++
i)
99template <
class Real,
int K>
102 r.
v = Kokkos::sqrt(a.
v);
104 for (
int i = 0;
i <
K; ++
i)
108template <
class Real,
int K>
110 return a[0] * b[0] + a[1] * b[1] + a[2] * b[2];
112template <
class Real,
int K>
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];
119template <
class Real,
int K>
126template <
class Real,
int K>
130 f[0] = v[0] - s * c[0];
131 f[1] = v[1] - s * c[1];
132 f[2] = v[2] - s * c[2];
139template <
class Real,
int MAXP = kMaxPlanes,
int MAXT = kMaxTriangles,
bool TrackAdj = false>
141 static_assert(MAXP <= 255,
"plane index must fit in unsigned char");
156 unsigned char t0[MAXT],
t1[MAXT],
t2[MAXT];
178 Kokkos::Array<int, (TrackAdj ? MAXT : 0) * 3>
adj;
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];
189 nn[k] = h[ax] * h[ax];
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);
208 if constexpr (TrackAdj)
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);
224 nn[k] =
n[k][0] *
n[k][0] +
n[k][1] *
n[k][1] +
n[k][2] *
n[k][2];
228 d = Kokkos::Experimental::epsilon_v<Real> * l2;
230 const Real a = d / l2;
234 nn[k] =
n[k][0] *
n[k][0] +
n[k][1] *
n[k][1] +
n[k][2] *
n[k][2];
236 const Real a = (-d) / l2;
240 nn[k] = -(
n[k][0] *
n[k][0] +
n[k][1] *
n[k][1] +
n[k][2] *
n[k][2]);
244 KOKKOS_INLINE_FUNCTION
void footPoint(
int k, Real out[3])
const {
245 if (
nn[k] < Real(0)) {
256 KOKKOS_INLINE_FUNCTION Real
sigmaOf(
int k)
const {
return nn[k] < Real(0) ? Real(-1) : Real(1); }
259 return Kokkos::sqrt(Kokkos::fabs(
nn[k]));
263 return nn[k] >= Real(0) ? Kokkos::sqrt(
nn[k]) : -Kokkos::sqrt(-
nn[k]);
270 for (
int k = 0; k <
np; ++k)
271 anyNeg = anyNeg ||
nn[k] < Real(0);
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)))
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;
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];
313 nn[k] = h[ax] * h[ax];
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) {
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) {
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);
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)))
362 const Real hx = Real(0.5) * rx, hy = Real(0.5) * ry, hz = Real(0.5) * rz;
366 nn[k] = hx * hx + hy * hy + hz * hz;
369 for (
int t = 0; t <
nt; ++t)
391 for (
int t = 0; t <
nt; ++t) {
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)
398 const Real s =
n[k][0] * v[0] +
n[k][1] * v[1] +
n[k][2] * v[2] -
nn[k];
402 (
nn[k] != Real(0)) ? Real(1) / Kokkos::sqrt(Kokkos::fabs(
nn[k])) : Real(0);
403 maxd = Kokkos::max(maxd, s * invlen);
425 int& nPartners)
const {
427 bool consistent =
true;
428 for (
int t = 0; t <
nt; ++t) {
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)
435 const Real s =
n[k][0] * v[0] +
n[k][1] * v[1] +
n[k][2] * v[2] -
nn[k];
438 (
nn[k] != Real(0)) ? Real(1) / Kokkos::sqrt(Kokkos::fabs(
nn[k])) : Real(0);
439 if (s * invlen > tol) {
441 const int part =
pnbr[k];
444 for (
int q = 0; q < nPartners; ++q)
445 if (outPartners[q] == part) {
449 if (!seen && nPartners < maxPartners)
450 outPartners[nPartners++] = part;
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;
475 static_assert(TrackAdj,
"computePoke4 requires TrackAdj");
476 for (
int t = 0; t <
nt; ++t) {
478 out[t * 4 + 0] = out[t * 4 + 1] = out[t * 4 + 2] = out[t * 4 + 3] = 0xff;
481 const int vt[3] = {
t0[t],
t1[t],
t2[t]};
483 for (
int e = 0; e < 3; ++e) {
484 const int s =
adj[t * 3 + e];
486 out[t * 4 + e] = (
unsigned char)(eo[e] & 0xff);
488 const Real v[3] = {
vx[t],
vy[t],
vz[t]};
489 Real best = Real(3.4e38);
491 for (
int k = 0; k <
np; ++k) {
492 if (k == vt[0] || k == vt[1] || k == vt[2])
494 if (k == eo[0] || k == eo[1] || k == eo[2])
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) {
502 out[t * 4 + 3] = (
unsigned char)(g & 0xff);
512 KOKKOS_INLINE_FUNCTION
bool isLocallyConvex(
const unsigned char* poke4, Real tol)
const {
514 for (
int t = 0; t <
nt; ++t) {
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];
522 const Real sl =
n[k][0] * v[0] +
n[k][1] * v[1] +
n[k][2] * v[2] -
nn[k];
525 (
nn[k] != Real(0)) ? Real(1) / Kokkos::sqrt(Kokkos::fabs(
nn[k])) : Real(0);
526 maxd = Kokkos::max(maxd, sl * invlen);
538 int* outPartners,
int maxPartners,
539 int& nPartners)
const {
541 bool consistent =
true;
542 for (
int t = 0; t <
nt; ++t) {
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];
550 const Real sl =
n[k][0] * v[0] +
n[k][1] * v[1] +
n[k][2] * v[2] -
nn[k];
553 (
nn[k] != Real(0)) ? Real(1) / Kokkos::sqrt(Kokkos::fabs(
nn[k])) : Real(0);
554 if (sl * invlen > tol) {
556 const int part =
pnbr[k];
559 for (
int q = 0; q < nPartners; ++q)
560 if (outPartners[q] == part) {
564 if (!seen && nPartners < maxPartners)
565 outPartners[nPartners++] = part;
586 const Real ux =
n[k][0] / nl, uy =
n[k][1] / nl, uz =
n[k][2] / nl;
589 const Real nx = q[0], ny = q[1], nz = q[2];
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);
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];
615 Real m = Real(-1e30);
616 for (
int t = 0; t <
nt; ++t) {
619 const Real s = pdir[0] *
vx[t] + pdir[1] *
vy[t] + pdir[2] *
vz[t] - d;
623 return m / Kokkos::sqrt(l2);
634 unsigned char used[MAXP];
637 for (
int k = 0; k <
np; ++k)
638 used[k] = (k < 6) ? 1 : 0;
639 for (
int t = 0; t <
nt; ++t)
645 unsigned char map[MAXP];
647 for (
int k = 0; k <
np; ++k) {
652 map[k] = (
unsigned char)m;
662 const int removed =
np - m;
664 for (
int t = 0; t <
nt; ++t)
678 for (
int t = 0; t <
nt; ++t) {
681 const Real r =
vx[t] *
vx[t] +
vy[t] *
vy[t] +
vz[t] *
vz[t];
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])
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);
710 if constexpr (TrackAdj) {
711 for (
int t = 0; t <
nt; ++t) {
714 const int vt[3] = {
t0[t],
t1[t],
t2[t]};
715 for (
int e = 0; e < 3; ++e)
726 if constexpr (!TrackAdj) {
729 for (
int t = 0; t <
nt; ++t) {
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])
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);
744 for (
int f = 0; f < 3; ++f)
745 if (
adj[s * 3 + f] == t)
759 static constexpr Real
kFanSine2 =
sizeof(Real) ==
sizeof(
float) ? Real(6.0e-10) : Real(1.4e-21);
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]};
782 const Real dp =
n[p][0] * d[0] +
n[p][1] * d[1] +
n[p][2] * d[2];
783 const Real dz =
n[z][0] * d[0] +
n[z][1] * d[1] +
n[z][2] * d[2];
784 return !(dp * dz > Real(0)) ||
785 dp * dp <=
kFanSine2 * Kokkos::fabs(
nn[x]) * Kokkos::fabs(
nn[y]) * Kokkos::fabs(
nn[p]);
788 KOKKOS_INLINE_FUNCTION
bool anyKilled(
const bool* kill)
const {
789 for (
int t = 0; t <
nt; ++t)
798 for (
int t = 0; t <
nt; ++t)
799 if (
alive[t] && !kill[t])
807 KOKKOS_INLINE_FUNCTION
bool diskHorizon(
int k,
int H,
const unsigned char* nA,
808 const unsigned char* nB)
const {
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];
829 e1[p] = (
unsigned short)(i + 1);
831 e2[p] = (
unsigned short)(i + 1);
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)
841 cur = (nA[nx] == cur) ? nB[nx] : nA[nx];
851 if constexpr (TrackAdj) {
854 unsigned char seen[MAXP];
855 for (
int i = 0; i < H; ++i)
857 for (
int i = 0; i < H; ++i)
861 for (
int t = 0; t <
nt; ++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;
876 for (
int s = 0; s <
nt; ++s)
895 KOKKOS_INLINE_FUNCTION
bool clip(
const Real pdir[3], Real d,
int nbr, Real* gapOut =
nullptr) {
907 *gapOut = Real(-1e30);
917 Real smax = Real(-1e30);
918 for (
int t = 0; t <
nt; ++t) {
922 const Real s =
n[pi][0] *
vx[t] +
n[pi][1] *
vy[t] +
n[pi][2] *
vz[t] -
nn[pi];
932 *gapOut = nl > Real(0) ? smax / nl : Real(-1e30);
942 if constexpr (!TrackAdj) {
946 unsigned char nA[MAXT], nB[MAXT];
968 for (
int t = 0; t <
nt; ++t)
972 for (
int i = 0; i < nnew; ++i) {
978 t2[slot] = (
unsigned char)pi;
985 int hA[MAXT], hB[MAXT], hSurv[MAXT],
987 int H =
walkHorizon(kill, pi, hA, hB, hSurv, hBack, nk);
991 H =
walkHorizon(kill, pi, hA, hB, hSurv, hBack, nk);
1008 for (
int t = 0; t <
nt; ++t)
1015 for (
int i = 0; i < H && ok; ++i) {
1020 alive[tau[i]] =
true;
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;
1030 adj[s * 3 + 0] = hSurv[i];
1031 adj[s * 3 + 1] = tau[(i + 1) % H];
1032 adj[s * 3 + 2] = tau[(i - 1 + H) % H];
1033 adj[hSurv[i] * 3 + hBack[i]] = s;
1045 unsigned char* nB,
int& nk) {
1051 for (
int t = 0; t <
nt && !redo; ++t) {
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];
1058 if (other >= 0 && !kill[other]) {
1067 nA[nnew] = (
unsigned char)x;
1068 nB[nnew] = (
unsigned char)y;
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) {
1095 int t0k = -1, e0 = -1;
1096 for (
int t = 0; t <
nt && t0k < 0; ++t) {
1099 for (
int e = 0; e < 3; ++e) {
1100 const int s =
adj[t * 3 + e];
1101 if (s >= 0 &&
alive[s] && !kill[s]) {
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;
1116 const int C = (
t0[tt] != A &&
t0[tt] != B) ?
t0[tt]
1117 : (
t1[tt] != A &&
t1[tt] != B) ?
t1[tt]
1126 const int surv =
adj[tt * 3 + ee];
1128 for (
int f = 0; f < 3; ++f)
1129 if (
adj[surv * 3 + f] == tt) {
1145 const int pivot = B;
1146 int rt = tt, arrival = ee, rotGuard = 0;
1148 const int rv[3] = {
t0[rt],
t1[rt],
t2[rt]};
1149 const int lv = (rv[0] == pivot) ? 0
1150 : (rv[1] == pivot) ? 1
1152 const int crossSlot = (arrival == lv) ? (lv + 2) % 3 : lv;
1153 const int rn =
adj[rt * 3 + crossSlot];
1154 if (rn >= 0 && kill[rn]) {
1155 int f = (
adj[rn * 3 + 0] == rt) ? 0 : (
adj[rn * 3 + 1] == rt) ? 1 : 2;
1158 if (++rotGuard > guardMax) {
1166 const int u = rv[crossSlot], w = rv[(crossSlot + 1) % 3];
1168 B = (u == pivot) ? w : u;
1175 if (++guard > guardMax) {
1179 }
while (!(tt == t0k && ee == e0));
1183 (void)kill, (
void)pi, (void)hA, (
void)hB, (void)hSurv, (
void)hBack, (void)nk;
1189 KOKKOS_INLINE_FUNCTION
int across(
int t,
int e)
const {
1190 if constexpr (TrackAdj) {
1191 return adj[t * 3 + e];
1193 const int vt[3] = {
t0[t],
t1[t],
t2[t]};
1214 KOKKOS_INLINE_FUNCTION
int growDisk(
bool* kill,
int pi) {
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);
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);
1227 for (
int k = 0; k <
np; ++k)
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]))
1236 const int ut[3] = {
t0[t],
t1[t],
t2[t]};
1237 int mt = 0, shared = 0;
1239 for (
int e = 0; e < 3; ++e) {
1240 const int o =
across(t, e);
1241 if (o >= 0 && kill[o]) {
1244 }
else if (!(o >= 0 && cand[o]) &&
1250 first ? mt == 0 : (mt == 1 && !touch[ut[(shared + 2) % 3]]) || (mt == 2 && H > 3);
1251 if (fanOk && disk) {
1262 touch[
t0[pick]] = touch[
t1[pick]] = touch[
t2[pick]] = 1;
1266 for (
int t = 0; t <
nt && stay < 0; ++t) {
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)) {
1282 for (
int t = 0; t <
nt; ++t)
1287 KOKKOS_INLINE_FUNCTION
bool empty()
const {
1288 for (
int t = 0; t <
nt; ++t)
1296 for (
int k = 0; k <
np; ++k) {
1298 for (
int t = 0; t <
nt && !used; ++t)
1299 if (
alive[t] && (
t0[t] == k ||
t1[t] == k ||
t2[t] == k))
1317 for (
int t = 0; t <
nt; ++t) {
1320 if (
t0[t] != k &&
t1[t] != k &&
t2[t] != k)
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))
1336 const Real un[3] = {nx / nlen, ny / nlen, nz / nlen};
1338 if (Kokkos::fabs(un[0]) <= Kokkos::fabs(un[1]) && Kokkos::fabs(un[0]) <= Kokkos::fabs(un[2])) {
1342 }
else if (Kokkos::fabs(un[1]) <= Kokkos::fabs(un[2])) {
1351 const Real e1l = Kokkos::sqrt(e1[0] * e1[0] + e1[1] * e1[1] + e1[2] * e1[2]);
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) {
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];
1370 const Real py = dx * e2[0] + dy * e2[1] + dz * e2[2];
1372 const Real s = Kokkos::fabs(px) + Kokkos::fabs(py);
1373 const Real t = (s > Real(0)) ? py / s : Real(0);
1374 ang[i] = (px < Real(0)) ? (Real(2) - t) : (py < Real(0) ? Real(4) + t : t);
1376 for (
int i = 1; i < m; ++i) {
1377 const Real ka = ang[i];
1378 const int ki = out[i];
1380 while (j >= 0 && ang[j] > ka) {
1381 ang[j + 1] = ang[j];
1382 out[j + 1] = out[j];
1396 Real fz[
MAXFV])
const {
1399 for (
int i = 0; i < m; ++i) {
1408 KOKKOS_INLINE_FUNCTION
static void polyAreaVec(
const Real fx[],
const Real fy[],
const Real fz[],
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];
1417 A[0] = Real(0.5) * ax;
1418 A[1] = Real(0.5) * ay;
1419 A[2] = Real(0.5) * az;
1436 for (
int i = 0; i <
nt; ++i) {
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) {
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);
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)))
1452 const Real denom = di - dj;
1453 if (denom == Real(0))
1455 const Real tt = di / denom;
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]);
1466 const Real ulen = Kokkos::sqrt(u[0] * u[0] + u[1] * u[1] + u[2] * u[2]);
1467 if (ulen == Real(0))
1469 const Real un[3] = {u[0] / ulen, u[1] / ulen, u[2] / ulen};
1471 if (Kokkos::fabs(un[0]) <= Kokkos::fabs(un[1]) && Kokkos::fabs(un[0]) <= Kokkos::fabs(un[2])) {
1475 }
else if (Kokkos::fabs(un[1]) <= Kokkos::fabs(un[2])) {
1484 const Real e1l = Kokkos::sqrt(e1[0] * e1[0] + e1[1] * e1[1] + e1[2] * e1[2]);
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) {
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);
1508 for (
int i = 1; i < m; ++i) {
1509 Real ka = ang[i], kx = px[i], ky = py[i], kz = pz[i];
1511 while (j >= 0 && ang[j] > ka) {
1512 ang[j + 1] = ang[j];
1531 for (
int k = 0; k <
np; ++k) {
1537 const Real area = Kokkos::sqrt(A[0] * A[0] + A[1] * A[1] + A[2] * A[2]);
1539 vol += support * area;
1541 return vol * (Real(1) / Real(3));
1557 KOKKOS_INLINE_FUNCTION
void planeN(
int k, Real out[3])
const {
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];
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];
1570 KOKKOS_INLINE_FUNCTION
static Real
det3(
const Real a[3],
const Real b[3],
const Real c[3]) {
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];
1587 for (
int t = 0; t <
nt; ++t) {
1590 Real n1[3], n2[3], n3[3];
1594 Real q1[3], q2[3], q3[3];
1598 Real c1[3], c2[3], c3[3];
1602 Real D =
dot3(n1, c1);
1604 for (
int a = 0; a < 3; ++a) {
1615 const Real v[3] = {
vx[t],
vy[t],
vz[t]};
1616 Real f12[3], f23[3], f31[3];
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);
1635 return vol * (Real(1) / Real(6));
1642 for (
int t = 0; t <
nt; ++t) {
1645 int k1 =
t0[t], k2 =
t1[t], k3 =
t2[t];
1646 Real n1[3], n2[3], n3[3];
1650 Real c1[3], c2[3], c3[3];
1654 Real D =
dot3(n1, c1);
1656 for (
int a = 0; a < 3; ++a) {
1668 const Real v[3] = {
vx[t],
vy[t],
vz[t]};
1669 Real f12[3], f23[3], f31[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)));
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;
1699 const Real sb = -
det3(ni, fl, v) * inv2n;
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);
1712 for (
int t = 0; t <
nt; ++t) {
1715 int k1 =
t0[t], k2 =
t1[t], k3 =
t2[t];
1716 Real n1[3], n2[3], n3[3];
1720 Real q1[3], q2[3], q3[3];
1724 Real c1[3], c2[3], c3[3];
1728 if (
dot3(n1, c1) < Real(0)) {
1729 for (
int a = 0; a < 3; ++a) {
1744 const Real v[3] = {
vx[t],
vy[t],
vz[t]};
1745 Real f12[3], f23[3], f31[3];
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,
1765 const Real sa =
det3(ni, ff, v);
1766 const Real sb = -
det3(ni, fl, v);
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;
1772 dgy[ki] += Real(2) * da * qi[1] - momy;
1773 dgz[ki] += Real(2) * da * qi[2] - momz;
1785 KOKKOS_INLINE_FUNCTION
void geomVolumeGrad(Real& vol, Real* dgx, Real* dgy, Real* dgz)
const {
1787 for (
int t = 0; t <
nt; ++t) {
1790 int k1 =
t0[t], k2 =
t1[t], k3 =
t2[t];
1791 Real n1[3], n2[3], n3[3];
1795 Real q1[3], q2[3], q3[3];
1799 Real c1[3], c2[3], c3[3];
1803 if (
dot3(n1, c1) < Real(0)) {
1804 for (
int a = 0; a < 3; ++a) {
1819 const Real v[3] = {
vx[t],
vy[t],
vz[t]};
1820 Real f12[3], f23[3], f31[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);
1841 for (
int k = 0; k <
np; ++k) {
1842 const Real inv = (
nn[k] != Real(0)) ? Real(1) / (Real(2) *
nn[k]) : Real(0);
1847 vol = V * (Real(1) / Real(6));
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,
1859 const Real sa =
det3(ni, ff, v);
1860 const Real sb = -
det3(ni, fl, v);
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);
1876 KOKKOS_INLINE_FUNCTION
void geomVolumeArea(Real& vol, Real* avx, Real* avy, Real* avz, Real* dgx,
1877 Real* dgy, Real* dgz)
const {
1879 for (
int t = 0; t <
nt; ++t) {
1882 int k1 =
t0[t], k2 =
t1[t], k3 =
t2[t];
1883 Real n1[3], n2[3], n3[3];
1887 Real q1[3], q2[3], q3[3];
1891 Real c1[3], c2[3], c3[3];
1895 if (
dot3(n1, c1) < Real(0)) {
1896 for (
int a = 0; a < 3; ++a) {
1911 const Real v[3] = {
vx[t],
vy[t],
vz[t]};
1912 Real f12[3], f23[3], f31[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);
1934 for (
int k = 0; k <
np; ++k) {
1935 const Real inv = (
nn[k] != Real(0)) ? Real(1) / (Real(2) *
nn[k]) : Real(0);
1936 const Real inva = (
nn[k] != Real(0)) ? Real(1) / (Real(2) * Kokkos::fabs(
nn[k])) : Real(0);
1939 const Real sa = avx[k];
1940 const Real smx = dgx[k], smy = dgy[k], smz = dgz[k];
1941 dgx[k] = (Real(2) * sa * qk[0] - smx) * inv;
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;
1945 avy[k] = sa *
n[k][1] * inva;
1946 avz[k] = sa *
n[k][2] * inva;
1948 vol = V * (Real(1) / Real(6));
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]) {
1956 xprod(Nn[q], w, out);
1957 }
else if (j == q) {
1959 xprod(Nn[p], w, tm);
1964 out[0] = out[1] = out[2] = Real(0);
1980 Real grad[3][3][3])
const {
1984 int kk[3] = {
t0[t],
t1[t],
t2[t]};
1987 Nn[a][0] =
n[kk[a]][0];
1988 Nn[a][1] =
n[kk[a]][1];
1989 Nn[a][2] =
n[kk[a]][2];
1993 xprod(Nn[1], Nn[2], c01);
1994 if (
dot3(Nn[0], c01) < Real(0)) {
1997 Nn[1][d] = Nn[2][d];
2008 const Real v[3] = {
vx[t],
vy[t],
vz[t]};
2009 Real nn3[3], u[3][3];
2011 nn3[a] =
dot3(Nn[a], Nn[a]);
2014 VOR_UNROLL for (
int d = 0; d < 3; ++d) u[a][d] = Real(2) * qa[d] - v[d];
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];
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];
2028 const Real D =
dot3(Nn[0], e[1]);
2029 Real g[3][3], den[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);
2039 xprod(v, Nn[i], wi);
2041 xprod(Nn[i], g[i], nixg);
2042 const int mb = (i + 2) % 3;
2044 const Real* cj = e[(j + 1) % 3];
2045 Real gN[3] = {0, 0, 0};
2048 xprod(g[i], v, gxv);
2049 VOR_UNROLL for (
int d = 0; d < 3; ++d) gN[d] += gxv[d];
2051 const Real alpha =
dot3(nixg, cj) / D;
2052 VOR_UNROLL for (
int d = 0; d < 3; ++d) gN[d] += alpha * u[j][d];
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);
2059 const Real cje =
dot3(cj, e[m]);
2060 const Real emw =
dot3(e[m], wi);
2061 Real eaW[3], eaV[3], eaE[3];
2065 const Real inv_ecc = (ecc[m] > Real(0)) ? Real(1) / ecc[m] : Real(0);
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];
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]);
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;
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];
2083 const Real sg =
sigmaOf(pl[j]);
2094 Real grad[3][3][3])
const {
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]};
2103 xprod(rn2, rn3, cc);
2104 if (
dot3(rn1, cc) < Real(0)) {
2105 for (
int a = 0; a < 3; ++a) {
2118 D n1[3], n2[3], n3[3];
2119 for (
int a = 0; a < 3; ++a) {
2126 D c1[3], c2[3], c3[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;
2137 D f12[3], f23[3], f31[3];
2143 for (
int a = 0; a < 3; ++a)
2144 g[a] = f12[a] - f31[a];
2146 for (
int a = 0; a < 3; ++a)
2147 g[a] = f23[a] - f12[a];
2149 for (
int a = 0; a < 3; ++a)
2150 g[a] = f31[a] - f23[a];
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]);
2171 for (
int t = 0; t <
nt; ++t) {
2174 int k1 =
t0[t], k2 =
t1[t], k3 =
t2[t];
2175 Real n1[3], n2[3], n3[3];
2179 Real q1[3], q2[3], q3[3];
2183 Real c1[3], c2[3], c3[3];
2187 if (
dot3(n1, c1) < Real(0)) {
2188 for (
int a = 0; a < 3; ++a) {
2203 const Real v[3] = {
vx[t],
vy[t],
vz[t]};
2204 Real f12[3], f23[3], f31[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);
2225 vol = V * (Real(1) / Real(6));
2236 Real conn[3])
const {
2237 const Real r[3] = {Real(2) *
n[k][0], Real(2) *
n[k][1],
2266 if (A[0] * r[0] + A[1] * r[1] + A[2] * r[2] < Real(0)) {
2271 const Real area = Kokkos::sqrt(A[0] * A[0] + A[1] * A[1] + A[2] * A[2]);
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]);
2284 const Real inv = asum > Real(0) ? Real(1) / (Real(3) * asum) : Real(0);
2288 const Real s = area / Kokkos::sqrt(r[0] * r[0] + r[1] * r[1] + r[2] * r[2]);
2292 dv[0] = s * (r[0] - cx);
2293 dv[1] = s * (r[1] - cy);
2294 dv[2] = s * (r[2] - cz);
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) {
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)
2317 const Real off = Policy::template offsetFromRel<Real>(n, wSelf, wNbr);
2318 if constexpr (!Policy::kHasWeightDof) {
2322 c.
clip(n, off, ids[i]);
#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