36#ifndef PECLET_CORE_GEOM_BODY_PROPERTIES_HPP
37#define PECLET_CORE_GEOM_BODY_PROPERTIES_HPP
65template <
class Real,
class Phi>
68 const int NS = nseg < 2 ? 2 : (nseg > 64 ? 64 : nseg);
69 auto at = [&](
Real s) {
return phi(
Vec3<Real>{p0.
x + s * d.
x, p0.
y + s * d.
y, p0.
z + s * d.
z}); };
71 for (
int it = 0; it < 40; ++it) {
73 const Real fm = at(m);
74 if ((fm <
Real(0)) == (fa <
Real(0))) {
81 return Real(0.5) * (a + b);
83 m0 = m1 = m2 =
Real(0);
84 auto accumulate = [&](
Real a,
Real b) {
86 m1 +=
Real(0.5) * (b * b - a * a);
87 m2 += (b * b * b - a * a * a) /
Real(3);
91 for (
int i = 1; i <= NS; ++i) {
94 if ((f <
Real(0)) != (fPrev <
Real(0))) {
95 const Real r = root(sPrev, s, fPrev);
96 if (fPrev <
Real(0)) {
97 accumulate(openAt, r);
106 if (openAt >=
Real(0))
107 accumulate(openAt,
Real(1));
115 for (
int i = 0; i < 3; ++i)
116 for (
int j = 0; j < 3; ++j) {
118 V[i][j] = (i == j) ?
Real(1) :
Real(0);
120 for (
int sweep = 0; sweep < 50; ++sweep) {
121 Real off = std::fabs(a[0][1]) + std::fabs(a[0][2]) + std::fabs(a[1][2]);
124 for (
int p = 0; p < 2; ++p)
125 for (
int q = p + 1; q < 3; ++q) {
126 if (a[p][q] ==
Real(0))
128 const Real theta = (a[q][q] - a[p][p]) / (
Real(2) * a[p][q]);
130 (std::fabs(theta) + std::sqrt(theta * theta +
Real(1)));
131 const Real c =
Real(1) / std::sqrt(t * t +
Real(1)), s = t * c;
132 for (
int k = 0; k < 3; ++k) {
133 const Real akp = a[k][p], akq = a[k][q];
134 a[k][p] = c * akp - s * akq;
135 a[k][q] = s * akp + c * akq;
137 for (
int k = 0; k < 3; ++k) {
138 const Real apk = a[p][k], aqk = a[q][k];
139 a[p][k] = c * apk - s * aqk;
140 a[q][k] = s * apk + c * aqk;
142 for (
int k = 0; k < 3; ++k) {
143 const Real vkp = V[k][p], vkq = V[k][q];
144 V[k][p] = c * vkp - s * vkq;
145 V[k][q] = s * vkp + c * vkq;
149 for (
int i = 0; i < 3; ++i)
152 for (
int i = 0; i < 2; ++i)
153 for (
int j = i + 1; j < 3; ++j)
155 std::swap(w[i], w[j]);
156 for (
int k = 0; k < 3; ++k)
157 std::swap(V[k][i], V[k][j]);
165 for (
int pick = 0; pick < k; ++pick) {
167 for (
int r = 0; r < 3; ++r) {
169 for (
int u = 0; u < ns; ++u)
170 used = used || (sel[u] == r);
171 if (!used && (best < 0 || rn[r] > rn[best]))
176 for (
int u = 0; u < ns; ++u)
177 for (
int v = u + 1; v < ns; ++v)
179 std::swap(sel[u], sel[v]);
187 for (
int r = 0; r < k; ++r)
188 for (
int c = 0; c < k; ++c)
189 M[r][c] = V[sel[c]][i + r];
192 for (
int r = 0; r < k; ++r)
193 for (
int c = 0; c < k; ++c)
194 for (
int t = 0; t < k; ++t)
195 MtM[r][c] += M[t][r] * M[t][c];
196 for (
int r = k; r < 3; ++r)
198 Real ew[3], EV[3][3];
201 for (
int r = 0; r < 3; ++r)
202 for (
int c = 0; c < 3; ++c)
203 for (
int t = 0; t < 3; ++t)
204 inv[r][c] += EV[r][t] * EV[c][t] / std::sqrt(std::max(ew[t],
Real(1e-30)));
206 for (
int r = 0; r < k; ++r)
207 for (
int c = 0; c < k; ++c)
208 for (
int t = 0; t < k; ++t)
209 U[r][c] += M[r][t] * inv[t][c];
212 for (
int r = 0; r < 3; ++r)
213 for (
int c = 0; c < k; ++c) {
215 for (
int t = 0; t < k; ++t)
216 NV[r][c] += V[r][i + t] * U[t][c];
218 for (
int r = 0; r < 3; ++r)
219 for (
int c = 0; c < k; ++c)
220 V[r][i + c] = NV[r][c];
228 const Real scale = std::max(std::max(std::fabs(w[0]), std::fabs(w[2])),
Real(1e-30));
232 while (j < 3 && (w[j] - w[j - 1]) <= relTol *
scale)
238 for (
int r = 0; r < 3; ++r) {
240 for (
int c = i; c < j; ++c)
241 rn[r] += V[r][c] * V[r][c];
255 const Real tr = R[0][0] + R[1][1] + R[2][2];
258 q.
w =
Real(0.25) * s;
259 q.
x = (R[2][1] - R[1][2]) / s;
260 q.
y = (R[0][2] - R[2][0]) / s;
261 q.
z = (R[1][0] - R[0][1]) / s;
262 }
else if (R[0][0] > R[1][1] && R[0][0] > R[2][2]) {
263 Real s = std::sqrt(
Real(1) + R[0][0] - R[1][1] - R[2][2]) *
Real(2);
264 q.
w = (R[2][1] - R[1][2]) / s;
265 q.
x =
Real(0.25) * s;
266 q.
y = (R[0][1] + R[1][0]) / s;
267 q.
z = (R[0][2] + R[2][0]) / s;
268 }
else if (R[1][1] > R[2][2]) {
269 Real s = std::sqrt(
Real(1) + R[1][1] - R[0][0] - R[2][2]) *
Real(2);
270 q.
w = (R[0][2] - R[2][0]) / s;
271 q.
x = (R[0][1] + R[1][0]) / s;
272 q.
y =
Real(0.25) * s;
273 q.
z = (R[1][2] + R[2][1]) / s;
275 Real s = std::sqrt(
Real(1) + R[2][2] - R[0][0] - R[1][1]) *
Real(2);
276 q.
w = (R[1][0] - R[0][1]) / s;
277 q.
x = (R[0][2] + R[2][0]) / s;
278 q.
y = (R[1][2] + R[2][1]) / s;
279 q.
z =
Real(0.25) * s;
291 bd::resolveDegenerateFrame(w, R);
293 const Real det = R[0][0] * (R[1][1] * R[2][2] - R[1][2] * R[2][1]) -
294 R[0][1] * (R[1][0] * R[2][2] - R[1][2] * R[2][0]) +
295 R[0][2] * (R[1][0] * R[2][1] - R[1][1] * R[2][0]);
297 for (
int r = 0; r < 3; ++r)
299 for (
int k = 0; k < 3; ++k)
301 for (
int r = 0; r < 3; ++r)
302 for (
int cc = 0; cc < 3; ++cc)
304 bp.
quat = bd::quatFromRotation(R);
309template <
class Real,
class Phi>
312 return phi(
Vec3<Real>{o[0] + a * H[0], o[1] + b * H[1], o[2] + c * H[2]});
318 auto ab = [](
Real v) {
return v <
Real(0) ? -v : v; };
320 if (ab(g1) >= ab(g0) && ab(g1) >= ab(g2))
322 else if (ab(g2) >= ab(g0) && ab(g2) >= ab(g1))
330template <
class Real,
class Phi>
335 const int t1 = (hA + 1) % 3, t2 = (hA + 2) % 3;
336 const Real area = H[t1] * H[t2];
338 for (
int qi = 0; qi < nq; ++qi) {
341 for (
int qj = 0; qj < nq; ++qj) {
344 Real p0[3] = {o[0], o[1], o[2]};
345 p0[t1] += ti * H[t1];
346 p0[t2] += tj * H[t2];
347 Real d[3] = {0, 0, 0};
354 const Real W = wi * wj * area;
355 const Real x0 = p0[hA], h = H[hA];
356 const Real L0 = h * m0;
357 const Real L1 = h * (x0 * m0 + h * m1);
358 const Real L2 = h * (x0 * x0 * m0 +
Real(2) * x0 * h * m1 + h * h * m2);
359 const Real tw[3] = {p0[0], p0[1], p0[2]};
362 M[t1] += W * tw[t1] * L0;
363 M[t2] += W * tw[t2] * L0;
365 S[t1][t1] += W * tw[t1] * tw[t1] * L0;
366 S[t2][t2] += W * tw[t2] * tw[t2] * L0;
367 S[t1][t2] += W * tw[t1] * tw[t2] * L0;
368 S[hA][t1] += W * tw[t1] * L1;
369 S[hA][t2] += W * tw[t2] * L1;
384template <
class Real,
class Phi>
386 int order = 5,
int nseg = 8,
Real density =
Real(1)) {
388 if (!(hi.
x > lo.
x && hi.
y > lo.
y && hi.
z > lo.
z))
389 throw std::invalid_argument(
"bodyProperties: empty box");
391 throw std::invalid_argument(
"bodyProperties: n must be >= 1");
392 const Real H[3] = {(hi.
x - lo.
x) / n, (hi.
y - lo.
y) / n, (hi.
z - lo.
z) / n};
393 const Real LO[3] = {lo.
x, lo.
y, lo.
z};
397 Real V = 0, M[3] = {0, 0, 0}, S[3][3] = {};
399 for (
int ci = 0; ci < n; ++ci)
400 for (
int cj = 0; cj < n; ++cj)
401 for (
int ck = 0; ck < n; ++ck) {
402 const Real o[3] = {LO[0] + ci * H[0], LO[1] + cj * H[1], LO[2] + ck * H[2]};
403 bd::cellMoments(phi, o, H, order, nq, nseg, V, M, S);
406 S[1][0] = S[0][1] += S[1][0];
407 S[2][0] = S[0][2] += S[2][0];
408 S[2][1] = S[1][2] += S[2][1];
412 bp.
mass = density * V;
414 throw std::runtime_error(
415 "bodyProperties: the field encloses no volume in this box -- check the sign convention "
416 "(negative inside) and that the box contains the solid");
421 for (
int a = 0; a < 3; ++a)
422 for (
int b = 0; b < 3; ++b)
423 cs[a][b] = S[a][b] - V * c[a] * c[b];
424 const Real trace = cs[0][0] + cs[1][1] + cs[2][2];
425 for (
int a = 0; a < 3; ++a)
426 for (
int b = 0; b < 3; ++b)
427 bp.
inertia[a][b] = density * ((a == b ? trace :
Real(0)) - cs[a][b]);
429 bd::principalFrame(bp);
void largestProjections(const Real rn[3], int k, int sel[3])
The k input axes with the largest projections rn into a degenerate cluster, in index order.
void interiorMoments(const Phi &phi, Vec3< Real > p0, Vec3< Real > d, int nseg, Real &m0, Real &m1, Real &m2)
Moments of the NEGATIVE set {phi < 0} along p0 + s*d, s in [0,1], in the PARAMETER s: m0 = ∫ 1 ds,...
void cellMoments(const Phi &phi, const Real o[3], const Real H[3], int order, int nq, int nseg, Real &V, Real(&M)[3], Real(&S)[3][3])
One cell's contribution to the raw moments (V, M, S): a tensor Gauss rule (nq = gaussCount(order) nod...
void principalFrame(BodyProperties< Real > &bp)
Principal moments and axes of bp.inertia into bp.principal / rotation / quat: degenerate clusters res...
void rotateOntoAxes(Real V[3][3], int i, int k, const int sel[3])
Rotate the cluster columns i..i+k-1 of V onto the input axes sel (orthogonal Procrustes): M = V_sub^T...
void eigSym3(const Real A[3][3], Real w[3], Real V[3][3])
Jacobi eigensolver for a symmetric 3x3: A = V diag(w) V^T, V orthonormal columns.
Quat< Real > quatFromRotation(const Real R[3][3])
Rotation matrix (columns orthonormal, det +1) -> quaternion (x, y, z, w), Shepperd's method.
int heightAxis(const Phi &phi, const Real o[3], const Real H[3])
Height axis of a cell (origin o, size H): the axis of the strongest central difference at the cell ce...
void resolveDegenerateFrame(Real w[3], Real V[3][3], Real relTol=Real(1e-2))
Within each cluster of (near-)equal eigenvalues, rotate the eigenbasis onto the input axes with the l...
PECLET_HD void gaussLegendre01(int n, int i, Real &x, Real &w)
PECLET_HD int gaussCount(int n)
BodyProperties< Real > bodyProperties(const Phi &phi, Vec3< Real > lo, Vec3< Real > hi, int n=32, int order=5, int nseg=8, Real density=Real(1))
Mass properties of {phi < 0} over the box [lo, hi], composite implicit quadrature: an n^3 cell grid; ...
double Real
Default host floating type. Device kernels may use float; conversions happen at the boundary.
PECLET_HD Vec3< Real > scale(Vec3< Real > a, Real s)
Quaternion, {x, y, z, w} — dem's F4 ordering (dem_portable.hpp), NOT {w, x, y, z}.
Real-templated POD 3-vector — the geometry layer's point type on host and device.
Real mass
density * volume
Vec3< Real > com
centre of mass, input frame
Real principal[3]
principal moments; principal[k] pairs with column k of R
Real inertia[3][3]
full tensor about the COM, input frame, at the given density
Quat< Real > quat
the same rotation, p_input = com + rotate(quat, p_body)
Real rotation[3][3]
columns = principal axes in the input frame