brep_kernel/props/mass_properties.rs
1use crate::topology::{BrepSolid, EdgeRecord, FaceRecord, ShellRecord};
2use crate::{NurbsCurve, NurbsSurface, Vec3};
3use serde::Serialize;
4
5/// Eight-station Gauss–Legendre on `[-1, 1]`. `pub(crate)` because
6/// `brep/soundness.rs`'s vector-area closure integrates the trims with the
7/// same rule the mass integrals use — one table, not a third copy of it.
8pub(crate) const GAUSS_X: [f64; 8] = [
9 -0.9602898564975363,
10 -0.7966664774136267,
11 -0.525532409916329,
12 -0.18343464249564978,
13 0.18343464249564978,
14 0.525532409916329,
15 0.7966664774136267,
16 0.9602898564975363,
17];
18pub(crate) const GAUSS_W: [f64; 8] = [
19 0.10122853629037669,
20 0.22238103445337445,
21 0.31370664587788727,
22 0.362683783378362,
23 0.362683783378362,
24 0.31370664587788727,
25 0.22238103445337445,
26 0.10122853629037669,
27];
28
29#[derive(Clone, Copy)]
30enum Integrand {
31 Area,
32 Volume,
33 VolumeAbout(Vec3),
34 /// Divergence-theorem first moments: ∫x dV = ∮ (x²/2)·nx dA, etc.
35 MomentX,
36 MomentY,
37 MomentZ,
38 /// Second moments: ∫x² dV = ∮ (x³/3)·nx dA, etc.
39 SecondXX,
40 SecondYY,
41 SecondZZ,
42 /// Products: ∫xy dV = ∮ (x²y/2)·nx dA, ∫xz, ∫yz analogously.
43 ProductXY,
44 ProductXZ,
45 ProductYZ,
46}
47
48/// A bounded, per-solid memo for exact area and volume. Geometry edits are
49/// detected by the face-set content identity; callers never invalidate it.
50/// The contents are private so external code cannot supply a fabricated result.
51#[derive(Clone, Debug, Default)]
52pub struct MassPropertiesCache {
53 entry: std::cell::Cell<Option<(FaceSetIdentity, MassProperties)>>,
54}
55
56#[derive(Clone, Copy, Debug, Serialize)]
57pub struct MassProperties {
58 pub surface_area: f64,
59 pub volume: f64,
60}
61
62#[derive(Clone, Copy, Debug, Serialize)]
63pub struct FullMassProperties {
64 pub surface_area: f64,
65 pub volume: f64,
66 /// Volume centroid (unit density center of mass).
67 pub centroid: Vec3,
68 /// Inertia tensor about the centroid for unit density, row-major
69 /// [[Ixx, Ixy, Ixz], [Ixy, Iyy, Iyz], [Ixz, Iyz, Izz]].
70 pub inertia: [[f64; 3]; 3],
71 /// Principal moments of inertia (eigenvalues of `inertia`) at unit
72 /// density, sorted ASCENDING: `principal_moments[0] <= [1] <= [2]`.
73 /// Golovanov §8.11.
74 pub principal_moments: [f64; 3],
75 /// Principal axes of inertia (eigenvectors of `inertia`), one per row:
76 /// `principal_axes[i]` is the unit axis whose moment is
77 /// `principal_moments[i]`. The three axes are orthonormal and arranged as
78 /// a RIGHT-HANDED frame (determinant +1); the sign of the third axis is
79 /// fixed to enforce handedness. Density-independent.
80 pub principal_axes: [[f64; 3]; 3],
81}
82
83/// Mass properties scaled to a physical density (Golovanov §8.11). The
84/// underlying `FullMassProperties` is a UNIT-density geometric result; here
85/// `mass = density * volume` and every inertia quantity scales linearly with
86/// density (`inertia = density * geometric_inertia`). The centroid and the
87/// principal AXES are density-independent; only the principal MOMENTS and the
88/// mass scale with density.
89#[derive(Clone, Copy, Debug, Serialize)]
90pub struct DensityMassProperties {
91 /// Density used to scale the geometric (unit-density) properties.
92 pub density: f64,
93 /// `mass = density * volume`.
94 pub mass: f64,
95 pub surface_area: f64,
96 pub volume: f64,
97 /// Center of mass (identical to the unit-density centroid).
98 pub centroid: Vec3,
99 /// Centroidal inertia tensor `= density * geometric_inertia`, row-major.
100 pub inertia: [[f64; 3]; 3],
101 /// Principal moments `= density * geometric_principal_moments`, ascending.
102 pub principal_moments: [f64; 3],
103 /// Principal axes (identical to the unit-density axes; density-invariant).
104 pub principal_axes: [[f64; 3]; 3],
105}
106
107impl FullMassProperties {
108 /// Scale the unit-density geometric result to a physical `density`
109 /// (Golovanov §8.11): `mass = density * volume`, `inertia` and the
110 /// principal moments scale by `density`, while the centroid and principal
111 /// axes are unchanged. Panics only on a non-finite density is avoided —
112 /// callers should pass a finite, positive density; a zero density yields a
113 /// massless (all-zero inertia) result, which is well defined.
114 pub fn with_density(&self, density: f64) -> DensityMassProperties {
115 let scale = |matrix: [[f64; 3]; 3]| {
116 let mut out = [[0.0f64; 3]; 3];
117 for row in 0..3 {
118 for column in 0..3 {
119 out[row][column] = density * matrix[row][column];
120 }
121 }
122 out
123 };
124 DensityMassProperties {
125 density,
126 mass: density * self.volume,
127 surface_area: self.surface_area,
128 volume: self.volume,
129 centroid: self.centroid,
130 inertia: scale(self.inertia),
131 principal_moments: [
132 density * self.principal_moments[0],
133 density * self.principal_moments[1],
134 density * self.principal_moments[2],
135 ],
136 principal_axes: self.principal_axes,
137 }
138 }
139}
140
141
142/// Per-call counters for the trimmed-face integrator, printed to stderr when
143/// `BREP_PROFILE` is set (the switch `boolean.<stage>_ms` and `imprint.*`
144/// use). One line per `solid_mass_properties` / `solid_signed_volume` /
145/// `solid_mass_properties_full` call: which route each trimmed face took,
146/// how many quadrature stations the cells, the boundary triangles and the
147/// chord-arc correction cost, and the wall time of each part. Native only in
148/// practice (no env vars in wasm); when the switch is off every counter is a
149/// thread-local flag test.
150#[path = "mass_properties/profile.rs"]
151mod profile;
152pub(crate) use profile::mass_profile;
153pub use profile::{mass_caller, MassCaller};
154
155/// The content identity of the face set an integrator call reads — the
156/// instrument behind the sign-check pairing, and the key a served value is
157/// checked against.
158#[path = "mass_properties/identity.rs"]
159mod identity;
160
161#[path = "mass_properties/integration.rs"]
162mod integration;
163#[path = "mass_properties/measures.rs"]
164mod measures;
165#[path = "mass_properties/polygons.rs"]
166mod polygons;
167/// Which Gauss–Legendre rule reads one span, and where it is cut: the answer
168/// is the span's own WEIGHTS, because a fixed-order rule is exact for a
169/// polynomial integrand and a rational one is not.
170#[path = "mass_properties/rule.rs"]
171mod rule;
172#[path = "mass_properties/solid_props.rs"]
173mod solid_props;
174#[path = "mass_properties/trimmed.rs"]
175mod trimmed;
176#[path = "mass_properties/winding.rs"]
177mod winding;
178
179use integration::*;
180use measures::*;
181use polygons::*;
182use trimmed::*;
183
184pub use integration::parameter_space_area;
185// The two span helpers `brep/soundness.rs`'s vector-area closure shares with
186// the mass integrals: the surface's knot levels, and a pcurve's spans cut
187// wherever it crosses one. A boundary integral read across a knot line is the
188// same 0.3%-off reading `winding_loops_integral` documents.
189pub(crate) use integration::surface_breaks;
190pub(crate) use winding::split_at_surface_breaks;
191pub use measures::{
192 curve_arc_length, edge_arc_length, face_area, face_boundary_length,
193 face_volume_contribution, solid_edge_length_total,
194};
195pub use solid_props::{solid_mass_properties, solid_mass_properties_full, solid_signed_volume};
196pub(crate) use solid_props::shell_signed_volume;
197pub use polygons::trim_polygons;
198pub use identity::{face_set_identity, FaceSetIdentity};