Skip to content

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

Clearance: check a shared volume against the union, allowing for the bodies' measuring error (#2090)

NOWN-DEVc177adfceb8d
diff --git a/crates/engine/src/cmd/clearance.rs b/crates/engine/src/cmd/clearance.rs
index 1752d56efe..14356d0c3f 100644
--- a/crates/engine/src/cmd/clearance.rs
+++ b/crates/engine/src/cmd/clearance.rs
@@ -298,7 +298,14 @@ fn measure_pair(
));
return out;
}
- Ok(m) => out.overlap = Some((m.volume, m.area, m.bbox, m.centroid)),
+ Ok(m) => {
+ let (wa, wb) = (ca.as_ref().unwrap_or(&ba.body), cb.as_ref().unwrap_or(&bb.body));
+ if let Some(reason) = union_disagrees(wa, wb, m.volume, region.center()) {
+ out.unchecked = Some(reason);
+ return out;
+ }
+ out.overlap = Some((m.volume, m.area, m.bbox, m.centroid));
+ }
Err(e) => {
out.unchecked = Some(e.to_string());
return out;
@@ -375,6 +382,42 @@ fn measure_near_origin(body: &solidcraft_kernel::Body, at: solidcraft_geom::Vec3
Ok(m)
}
+/// Does the shared volume of `a` and `b` disagree with the one their union implies,
+/// vol(a) + vol(b) − vol(a ∪ b)? The intersection can come out well formed and still wrong (a
+/// mirrored pair read three times its volume). `None` when they agree, or when the union gives no
+/// answer to compare with.
+fn union_disagrees(a: &solidcraft_kernel::Body, b: &solidcraft_kernel::Body, shared: f64, at: solidcraft_geom::Vec3) -> Option<String> {
+ let (union, report) = solidcraft_kernel::capture_boolean_diagnostics(|| solidcraft_kernel::boolean(a, b, solidcraft_kernel::BoolOp::Union));
+ let union = union.ok().flatten().filter(|_| report.unverified().is_none())?;
+ let volume = |x: &solidcraft_kernel::Body| measure_near_origin(x, at).ok().map(|m| m.volume).filter(|v| v.is_finite());
+ contradicted(shared, volume(a)?, volume(b)?, volume(&union)?)
+}
+
+/// How far vol(a) + vol(b) − vol(a ∪ b) can stray from the shared volume by measuring alone,
+/// as a share of vol(a) + vol(b) + vol(a ∪ b). [`solidcraft_kernel::measure`] facets a curved
+/// face with chords of up to 3e-4 of its radius where the radius sets them, which leaves the
+/// volume it encloses short by up to (4/3) × 3e-4 = 4e-4. That shortfall is the same for a face
+/// measured on its own body and in the union, and cancels in the difference, except where the
+/// chord changes with the body measured (it also follows the body's size). Measured, that
+/// change stays near 2e-5 of the three volumes: 2.1e-5 for a 40 mm roller pressed 0.2 mm into a
+/// 2 m plate (`a_roller_pressed_into_a_large_plate_is_reported`), 2e-5 and 1e-6 in the examples
+/// of `the_union_check_allows_for_the_bodies_measuring_error`. This allows five times that.
+const VOLUME_NOISE: f64 = 1e-4;
+
+/// [`union_disagrees`] on measured volumes: `shared` against vol(a) + vol(b) − vol(a ∪ b).
+/// That difference of large numbers carries the measuring error of all three, so a small overlap
+/// on large bodies can come out near zero or below it: then the union cannot tell, and a shared
+/// part small next to both bodies stands. A shared part the union clearly contradicts (412 mm³
+/// where the bodies of a few thousand and their union imply 141) does not.
+fn contradicted(shared: f64, va: f64, vb: f64, vu: f64) -> Option<String> {
+ let implied = va + vb - vu;
+ let noise = VOLUME_NOISE * (va + vb + vu) + 1e-6;
+ let allowed = 0.01 * shared.max(implied.abs()) + noise;
+ let unresolved = implied <= noise && shared <= 0.1 * va.min(vb);
+ ((implied - shared).abs() > allowed && !unresolved)
+ .then(|| format!("the shared part measures {shared:.4} mm³ but the bodies and their union imply {implied:.4} mm³, so neither is reported"))
+}
+
/// Is a measured shared part well formed? A part whose shell came out inside out or open can
/// carry a volume with its centre outside its own box, or more volume than its box holds.
fn plausible(volume: f64, centre: solidcraft_geom::Vec3, bbox: Aabb3) -> bool {
diff --git a/crates/engine/src/cmd/clearance_tests.rs b/crates/engine/src/cmd/clearance_tests.rs
index 5f1876e7b8..57c25f9ed6 100644
--- a/crates/engine/src/cmd/clearance_tests.rs
+++ b/crates/engine/src/cmd/clearance_tests.rs
@@ -251,3 +251,74 @@ fn a_small_gap_past_a_corner_is_not_contact() {
assert!((f(&r["nearest"][a][0]["distance_mm"]) - want).abs() < 1e-9, "{a} {b}: {r}");
}
}
+
+/// The shared volume is checked against the bodies and their union: two 10 mm cubes sharing a
+/// 3 mm corner cube agree at 27 mm³, and a shared part read as three times that (as a mirrored
+/// pair once was) does not.
+#[test]
+fn a_shared_volume_the_union_contradicts_is_caught() {
+ use solidcraft_geom::Vec3;
+ let a = solidcraft_kernel::box_solid(Vec3::new(0.0, 0.0, 0.0), Vec3::new(10.0, 10.0, 10.0)).unwrap_or_else(|e| panic!("{e}"));
+ let b = solidcraft_kernel::box_solid(Vec3::new(7.0, 7.0, 7.0), Vec3::new(17.0, 17.0, 17.0)).unwrap_or_else(|e| panic!("{e}"));
+ let at = Vec3::new(8.5, 8.5, 8.5);
+ assert_eq!(super::union_disagrees(&a, &b, 27.0, at), None);
+ assert!(super::union_disagrees(&a, &b, 81.0, at).is_some());
+}
+
+/// The check works on measured volumes, and vol(a) + vol(b) − vol(a ∪ b) carries the error of
+/// all three: a small overlap on large bodies can come out near zero, or below it. A union that
+/// cannot resolve the shared part leaves it standing; one that clearly contradicts it does not.
+#[test]
+fn the_union_check_allows_for_the_bodies_measuring_error() {
+ use super::contradicted;
+ // 0.96 mm³ shared where the union of 40 000 and 300 mm³ implies -0.75: noise, not a contradiction.
+ assert_eq!(contradicted(0.96, 40_000.0, 300.0, 40_300.75), None);
+ // 5.92 mm³ of a small body in a large sheet, with the union 0.4 mm³ off the bodies' sum.
+ assert_eq!(contradicted(5.92, 250_000.0, 80.0, 250_074.48), None);
+ // Past the noise, but the union implies nothing at all while the shared part is small next
+ // to both bodies: the union cannot tell, and the shared part stands.
+ assert_eq!(contradicted(6.0, 400.0, 200.0, 600.5), None);
+ // 412 mm³ shared where bodies of a few thousand and their union imply 141: caught.
+ assert!(contradicted(411.98, 3_200.0, 3_150.0, 6_350.0 - 141.18).is_some());
+ // A large shared part the union says is not there at all: caught too.
+ assert!(contradicted(900.0, 3_200.0, 3_150.0, 6_350.0).is_some());
+}
+
+/// A small box pressed 0.6 mm into a 2000 × 1000 × 1 mm plate shares 3 × 3 × 0.6 = 5.4 mm³: an
+/// overlap, not an unchecked pair.
+#[test]
+fn a_small_overlap_on_a_large_plate_is_reported() {
+ let mut s = Session::default();
+ run(&mut s, "solid.box", json!({"length": 2000, "width": 1000, "height": 1, "corner": [0, 0, 0], "operation": "new", "body_name": "Plate"}));
+ run(&mut s, "solid.box", json!({"length": 3, "width": 3, "height": 3, "corner": [700, 400, 0.4], "operation": "new", "body_name": "Box"}));
+ let r = run(&mut s, "inspect.clearance", json!({}));
+ assert_eq!(r["unchecked"].as_array().map(Vec::len), Some(0), "{r}");
+ let overlaps = r["overlaps"].as_array().cloned().unwrap_or_default();
+ assert_eq!(overlaps.len(), 1, "{r}");
+ assert_eq!(pair(&overlaps[0]), ["Box", "Plate"]);
+ assert!((f(&overlaps[0]["volume_mm3"]) - 5.4).abs() < 1e-6, "{r}");
+}
+
+/// A 40 mm roller lying on a 2000 × 1000 × 3 mm plate, pressed 0.2 mm into it, shares the
+/// circular segment along its 10 mm (7.53 mm³). Its curved wall is faceted differently on its
+/// own and in the union, so vol(a) + vol(b) − vol(a ∪ b) comes out 0.64 mm³ over the shared
+/// volume (2e-5 of the three volumes, past the 1 % of the shared part): the measuring allowance
+/// keeps it an overlap, not an unchecked pair.
+#[test]
+fn a_roller_pressed_into_a_large_plate_is_reported() {
+ let (r, depth, length) = (20.0_f64, 0.2_f64, 10.0);
+ let mut s = Session::default();
+ run(&mut s, "solid.box", json!({"length": 2000, "width": 1000, "height": 3, "corner": [0, 0, 0], "operation": "new", "body_name": "Plate"}));
+ run(
+ &mut s,
+ "solid.cylinder",
+ json!({"radius": r, "height": length, "base": [100, 50, 3.0 + r - depth], "axis": [1, 0, 0], "operation": "new", "body_name": "Roller"}),
+ );
+ let res = run(&mut s, "inspect.clearance", json!({}));
+ assert_eq!(res["unchecked"].as_array().map(Vec::len), Some(0), "{res}");
+ let overlaps = res["overlaps"].as_array().cloned().unwrap_or_default();
+ assert_eq!(overlaps.len(), 1, "{res}");
+ assert_eq!(pair(&overlaps[0]), ["Plate", "Roller"]);
+ let want = (r * r * ((r - depth) / r).acos() - (r - depth) * (r * r - (r - depth) * (r - depth)).sqrt()) * length;
+ assert!((f(&overlaps[0]["volume_mm3"]) - want).abs() < 0.01 * want, "{want} {res}");
+}