Skip to content

SolidCraft: an open-source parametric 3D CAD application built in pure Rust. Desktop, web, and automation through MCP.

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

Jackson Ourslandb0048a610487
diff --git a/crates/doc/src/eval.rs b/crates/doc/src/eval.rs
index 4eba4b2dd8..22799b98f0 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 07048a28f9..81887545fe 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 2633313af0..1b859f6a6e 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<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,
diff --git a/crates/kernel/src/tests.rs b/crates/kernel/src/tests.rs
index f63183233d..66a608e8ae 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());