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