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

* Clearance: check a shared volume against the bodies and their union

An intersection can come out well formed and still wrong: one of a
mirrored pair of imported parts shared three times the volume of its
twin. The shared volume is now compared with vol(a) + vol(b) - vol(a ∪ b),
on the same (cut down) bodies; when they differ by more than 1 % (plus the
bodies' own measuring error), the pair is reported unchecked with both
numbers. Where the union gives no answer, the intersection stands.

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>

* Clearance: let the union cross-check allow for the bodies' measuring error

vol(a) + vol(b) - vol(a ∪ b) is a difference of measured volumes and
carries the error of all three, so correct small overlaps on large bodies
came back unchecked (0.96 mm³ where the union implied -0.75). measure
keeps a face's fine tessellation when it is within 1 % of one with a
5 times coarser chord, so a measured volume can be off by up to 0.25 %:
the allowance is now 1 % of the larger of the two answers plus 0.25 % of
the three volumes. Past it, a union that implies nothing (within that
noise of zero, or below it) while the shared part is at most a tenth of
the smaller body cannot tell, and the shared part stands. A shared part
of 412 mm³ where bodies of a few thousand imply 141 is still caught.

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>

* Clearance: tighten the union check's measuring allowance to 1e-4

The old note cited measure's 1 % gate, which compares face areas, not
volumes, and its 0.25 % was about 100 times the disagreement the bodies'
measures actually show. The allowance is now justified from volumes: a
curved face's chord shortfall cancels between a body and the union except
where the chord changes, which stays near 2e-5 of the three volumes; 1e-4
leaves five times that. A 40 mm roller pressed 0.2 mm into a 2 m plate
exercises it: with no allowance the pair is left unchecked (7.52 mm³
measured against 8.16 implied), with it the overlap is reported.

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>

---------

Co-authored-by: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
Co-authored-by: bherbruck <brennen.herbruck@gmail.com>
This commit is contained in:
NOWN-DEV
2026-10-19 03:16:37 +02:00
committed by GitHub
parent 7ad5130cd2
commit c177adfceb
2 changed files with 115 additions and 1 deletions
+44 -1
View File
@@ -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 {
+71
View File
@@ -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}");
}