From b0048a610487bfb24a9d8f787b8957cef2d0e25a Mon Sep 17 00:00:00 2001 From: Jackson Oursland Date: Thu, 22 Oct 2026 18:51:40 -0700 Subject: [PATCH] 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) --- crates/doc/src/eval.rs | 3 +++ crates/kernel/src/body.rs | 6 +++--- crates/kernel/src/meshbody.rs | 15 +++++++++++++-- crates/kernel/src/tests.rs | 11 +++++++++++ 4 files changed, 30 insertions(+), 5 deletions(-) diff --git a/crates/doc/src/eval.rs b/crates/doc/src/eval.rs index 4eba4b2dd..22799b98f 100644 --- a/crates/doc/src/eval.rs +++ b/crates/doc/src/eval.rs @@ -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()), diff --git a/crates/kernel/src/body.rs b/crates/kernel/src/body.rs index 07048a28f..81887545f 100644 --- a/crates/kernel/src/body.rs +++ b/crates/kernel/src/body.rs @@ -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; diff --git a/crates/kernel/src/meshbody.rs b/crates/kernel/src/meshbody.rs index 2633313af..1b859f6a6 100644 --- a/crates/kernel/src/meshbody.rs +++ b/crates/kernel/src/meshbody.rs @@ -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 { } 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, diff --git a/crates/kernel/src/tests.rs b/crates/kernel/src/tests.rs index f63183233..66a608e8a 100644 --- a/crates/kernel/src/tests.rs +++ b/crates/kernel/src/tests.rs @@ -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());