Mesh bodies: measure their size once, when made (#4662) (#4663)

`Body::size` walked a mesh body's vertices on every call, and it is asked
for every frame. A mesh body's triangles never change after `mesh_body` or
`mesh_transformed` makes it, so both now measure it then (`TriMesh::span`)
and `size` returns it like a B-rep body's. `ModelBody::mesh` returns a mesh
it already has before working out the display tolerance.

Co-authored-by: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
This commit is contained in:
Jackson Oursland
2026-10-22 18:51:40 -07:00
committed by GitHub
parent 0db2abbeca
commit b0048a6104
4 changed files with 30 additions and 5 deletions
+3
View File
@@ -120,6 +120,9 @@ impl ModelBody {
// finding their surface, picks naming their face, sketch planes), and a different
// triangulation moved those and changed what designs build. A GPU viewport draws the
// plan ([`ModelBody::display_plan`]).
if let Some(mesh) = self.mesh.get() {
return mesh.clone();
}
match self.mesh_placement.as_deref() {
Some(placed) => self.mesh.get_or_init(|| placed.mesh()).clone(),
None => placed_mesh::reference_mesh(&self.body, &self.mesh, self.display_tol()),
+3 -3
View File
@@ -1094,10 +1094,10 @@ impl Body {
}
/// Characteristic size (bounding box diagonal) for tolerances. [`Body::new`] measured it
/// while it read the vertices anyway, so the answer is cached; a body built by hand carries
/// 0 and is walked here once, and mesh bodies always measure their triangles.
/// while it read the vertices anyway, and a mesh body when it was made, so the answer is
/// cached; a body built by hand carries 0 and is walked here.
pub fn size(&self) -> f64 {
if self.mesh.is_none() && self.size > 0.0 {
if self.size > 0.0 {
return self.size;
}
let mut b = solidcraft_geom::Aabb3::EMPTY;
+13 -2
View File
@@ -41,6 +41,17 @@ impl TriMesh {
}
/// Distinct undirected triangle sides.
/// The diagonal of the box around the vertices: a mesh body's size ([`Body::size`]),
/// measured once when the body is made. 0 when it has none.
pub(crate) fn span(&self) -> f64 {
let mut b = solidcraft_geom::Aabb3::EMPTY;
for p in &self.positions {
b.add(*p);
}
let d = b.diagonal();
if d.is_finite() && d > 0.0 { d } else { 0.0 }
}
pub(crate) fn edge_count(&self) -> usize {
let mut set = std::collections::HashSet::new();
for t in &self.triangles {
@@ -103,7 +114,7 @@ pub fn mesh_body(positions: &[Vec3], triangles: &[[u32; 3]]) -> Result<Body> {
}
Ok(Body {
solid: Arc::new(crate::body::Solid::new_unchecked(Vec::new())),
size: 0.0,
size: out.span(),
boundary_tol: None,
volume: Some(std::sync::Arc::new(std::sync::Mutex::new(None))),
coarse_missing: None,
@@ -203,7 +214,7 @@ impl Body {
}
Some(Body {
solid: self.solid.clone(),
size: 0.0,
size: out.span(),
boundary_tol: None,
volume: Some(std::sync::Arc::new(std::sync::Mutex::new(None))),
coarse_missing: None,
+11
View File
@@ -1434,6 +1434,17 @@ fn mesh_bodies_move_measure_and_refuse_solid_ops() {
assert!((measure(&moved).unwrap().volume - 1000.0).abs() < 1e-9);
let mirrored = crate::transform_matrix(&b, [[-1.0, 0.0, 0.0, 0.0], [0.0, 1.0, 0.0, 0.0], [0.0, 0.0, 1.0, 0.0], [0.0, 0.0, 0.0, 1.0]]).unwrap();
assert!((measure(&mirrored).unwrap().volume - 1000.0).abs() < 1e-9);
// Their size (the diagonal of the box around them) is measured once, when made: a large
// scan's size is asked for every frame.
for body in [&b, &moved, &mirrored] {
assert!(body.size > 0.0, "measured when made");
let walked = body.mesh.as_ref().unwrap().positions.iter().fold(solidcraft_geom::Aabb3::EMPTY, |mut a, p| {
a.add(*p);
a
});
assert!((body.size() - walked.diagonal()).abs() < 1e-9, "{} vs {}", body.size(), walked.diagonal());
}
assert!((b.size() - 300.0f64.sqrt()).abs() < 1e-9);
let solid = box_solid(Vec3::ZERO, Vec3::new(5.0, 5.0, 5.0)).unwrap();
assert!(fillet(&b, &[Vec3::new(5.0, 0.0, 0.0)], 1.0).unwrap_err().to_string().contains("mesh"));
assert!(boolean(&solid, &b, BoolOp::Union).is_err());