1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
//! Volume, centre of mass and the inertia tensor of the solid the triangles
//! bound — computed on the surface, with no volume mesh and no round trip
//! through a physics crate.
//!
//! Ticket: R-083, pre-registered as `P-83`.
//!
//! # The construction
//!
//! Hartmann & Ewougsi Tekeu, *Gauss divergence theorem for the calculation of
//! the mass and area moment of inertia tensors*, Acta Mechanica 236 (2025),
//! [`10.1007/s00707-025-04419-1`](https://doi.org/10.1007/s00707-025-04419-1),
//! open access. Their equation numbers are cited below; this module is their
//! §5.1 with nothing added.
//!
//! The mass properties of a solid `Ω` are volume integrals, and the obvious way
//! to get them is to fill `Ω` with tetrahedra or voxels and sum. This crate has
//! already produced `∂Ω`, so filling it again is paying twice for geometry it is
//! holding. Substituting the position vector into the divergence theorem removes
//! the volume entirely. With `div x = 3` (their Eq. 5–6),
//!
//! ```text
//! V = ⅓ ∮ x·n dA
//! ```
//!
//! with `div(x ⊗ x) = 4x` (Eq. 8, 12),
//!
//! ```text
//! r = 1/(4V) ∮ (x·n) x dA
//! ```
//!
//! and — the part they call novel — with `div((x·x)x) = 5(x·x)` and
//! `grad((x·x)x) = 2 x⊗x + (x·x)I` (Eq. 17–21), the whole inertia tensor about
//! the origin:
//!
//! ```text
//! Θ₀ = 3/10 (∮ (x·x)(x·n) dA) I − ½ ∮ (x·x) x ⊗ n dA
//! ```
//!
//! # The trap, which is the reason [`MassProperties::asymmetry`] is public
//!
//! `Θ₀` is symmetric by definition, so the second surface integral must satisfy
//! their Eq. (22), `∮(x·x) x⊗n dA = ∮(x·x) n⊗x dA`. The paper's own words:
//! *"which holds in the exact case but might be violated by discretization
//! schemes"*, and their §6.1 measures it violated on a triangulated cylinder —
//! `Θˣᶻ ≠ Θᶻˣ` — with the remedy `Θ = ½(Θ + Θᵀ)` (Eq. 66).
//!
//! The mechanism is visible in the sum: each triangle contributes the rank-one
//! `G ⊗ N`, which is antisymmetric-in-part and only cancels against the rest of
//! the surface. So the asymmetry is a *global cancellation residual*, and it
//! reports on the surface rather than on the arithmetic — see the field's docs.
//!
//! # One quadrature kernel, not ten
//!
//! On a triangle `(X₁, X₂, X₃)` with `N = (X₂ − X₁) × (X₃ − X₁)`, the outward
//! `n dA` is the constant `N` times the parametric area element, so each
//! integral above is a polynomial in the linear shape functions
//! `n₁ = 1 − ξ − η`, `n₂ = ξ`, `n₃ = η` over the unit triangle (their Eq. 44),
//! and `∫ n₁^a n₂^b n₃^c dξ dη = a!b!c!/(a+b+c+2)!`. Their Table 1 is the cubic
//! case of that, and it collects — writing `S = ΣXₐ` and `Q = Σ Xₐ·Xₐ` — into
//!
//! ```text
//! 120·∫ (x·x) x dξdη = (S·S)S + Q·S + 2·Σₐ(S·Xₐ)Xₐ + 2·Σₐ(Xₐ·Xₐ)Xₐ
//! ```
//!
//! which is this module's one quadrature kernel. `‖N‖` cancels between `dA` and
//! the unit normal, so
//! **no square root is evaluated anywhere in this module** and the triangle's
//! area is never formed.
//!
//! # Cost
//!
//! One pass, no allocation, no scratch buffer — the accumulator is fourteen
//! scalars. There is nothing for a caller to provide and nothing to reuse, which
//! is why this module has no `&mut` output parameter while the extractors all
//! do.
use crate;
use crate::;
/// What a closed triangle mesh weighs, where it balances, and how it spins.
///
/// Unit density throughout: multiply [`volume`](Self::volume) by a density to
/// get mass, and either tensor by the same density to get the physical inertia.
/// Density is not a parameter because a uniform one is a scalar multiple of
/// every field here, and taking it would let a caller believe the module
/// supports a varying one.
///
/// # Units
///
/// With positions in metres: `volume` is m³, `center_of_mass` is m, both tensors
/// are m⁵ — inertia per unit density — and [`asymmetry`](Self::asymmetry) is m⁵
/// so that it is directly comparable against the tensor entries beside it.
/// Mass properties of the solid bounded by `triangles`.
///
/// `triangles` indexes `positions`, and the winding is the crate's:
/// counter-clockwise viewed from outside the solid, so `(X₂ − X₁) × (X₃ − X₁)`
/// points away from the material. The paper makes the same demand — *"a
/// fundamental problem with triangulation is the numbering of the nodes, which
/// results in a normal pointing outward from the body"* (§5.1). A consistently
/// *inward*-wound mesh yields a negative volume and is rejected rather than
/// silently flipped: the sign is the caller's contract, and repairing it here
/// would make an inside-out mesh indistinguishable from a correct one.
///
/// The mesh must be closed. Nothing here can check that cheaply — closedness is
/// an edge-adjacency property and this is a single pass over triangles — so the
/// module reports [`MassProperties::asymmetry`] instead, which rises off the
/// round-off floor exactly when the surface leaks. Use
/// [`validate_indexed`](crate::validate::validate_indexed) when a definite
/// answer is wanted.
///
/// Triangles are consumed in slice order and nothing is sorted, so the result is
/// a deterministic function of the input order. Float addition is not
/// associative; a caller that reorders its triangle list will get a different
/// last bit.
///
/// # Errors
///
/// [`Error::IndexOutOfRange`] if a triangle names a vertex `positions` does not
/// have. [`Error::MassPropertiesUndefined`] if the surface encloses no positive
/// finite volume, or if a moment overflowed to a non-finite value — in either
/// case the centre of mass is a division by something that is not a volume, and
/// the crate reports that rather than returning one.
///
/// # Example
///
/// ```
/// use isomesh::mass::mass_properties;
///
/// // The unit cube [0,1]³, twelve triangles, wound outward.
/// let positions = [
/// [0.0f64, 0.0, 0.0], [1.0, 0.0, 0.0], [1.0, 1.0, 0.0], [0.0, 1.0, 0.0],
/// [0.0, 0.0, 1.0], [1.0, 0.0, 1.0], [1.0, 1.0, 1.0], [0.0, 1.0, 1.0],
/// ];
/// let triangles = [
/// [0u32, 2, 1], [0, 3, 2], [4, 5, 6], [4, 6, 7],
/// [0, 1, 5], [0, 5, 4], [1, 2, 6], [1, 6, 5],
/// [2, 3, 7], [2, 7, 6], [3, 0, 4], [3, 4, 7],
/// ];
///
/// let props = mass_properties(&positions, &triangles)?;
/// assert!((props.volume - 1.0).abs() < 1e-15);
/// assert!((props.center_of_mass[0] - 0.5).abs() < 1e-15);
/// // A unit cube about its centre: I = 1/6 on the diagonal, 0 off it.
/// assert!((props.inertia[0][0] - 1.0 / 6.0).abs() < 1e-15);
/// assert!(props.inertia[0][1].abs() < 1e-15);
/// # Ok::<(), isomesh::Error>(())
/// ```
/// `120 · ∫ (x·x) x dξ dη` over the unit triangle, for `x` interpolated
/// linearly from the three corners.
///
/// The paper's Eq. (48) with its Table 1 folded in: a cubic product of
/// barycentrics integrates to `1/20` when all three indices coincide, `1/60`
/// when exactly two do and `1/120` otherwise, which is
/// `(1 + [a=b] + [b=c] + [a=c] + 2[a=b=c])/120`. Summing that against
/// `(Xₐ·X_b)X_c` gives the five terms below, so the triple sum over 27 shape
/// products never has to be written out.
///
/// `s` is `X₁ + X₂ + X₃`, passed in because the caller already formed it for the
/// volume.
///
/// The `1/120` is deferred to the caller, so that the trace term's `3/10` and
/// the outer term's `½` are each applied in a single division at the end.
///
/// The two per-corner contractions are formed once rather than once per axis —
/// this is the innermost arithmetic of the module and the naive nesting does
/// eighteen dot products where six suffice.