Skip to content

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

Wave 468/509: Robustness tests: lip_after_boss and hole_as_deep_as_the_plate live once, in the engine's tests.rs with exact volumes (and 3 more)

jyvikof60f71d878ad
diff --git a/crates/engine/src/tests.rs b/crates/engine/src/tests.rs
index e27f337824..598d39e507 100644
--- a/crates/engine/src/tests.rs
+++ b/crates/engine/src/tests.rs
@@ -391,7 +391,6 @@ mod flush_join_loft;
mod gltf_import;
mod import_component;
mod large_step_import;
-mod loft_cut;
mod loft_ring;
mod loft_spacing;
mod mirror_placed;
@@ -3116,9 +3115,13 @@ fn lip_after_boss() {
run(&mut s, "plastic.boss", json!({"position": [70, 40, 2], "diameter": 7, "height": 12, "hole_diameter": 2.5}));
let v0 = volume(&mut s);
run(&mut s, "plastic.lip", json!({"face": [1, 25, 30], "width": 1, "height": 2}));
- let v = volume(&mut s);
- assert!(v > v0 + 1.0, "{v0} {v}");
- // Bosses up to the rim, with ribs, against a wall; lips, grooves and outside rims.
+ // The band inside the rim, 1 wide and 2 high: (78 × 48 − 76 × 46) × 2; the bosses stop
+ // at z = 14, well under it.
+ let inside = (78.0 * 48.0 - 76.0 * 46.0) * 2.0;
+ assert!(rel(volume(&mut s) - v0, inside) < 1e-6, "{}", volume(&mut s) - v0);
+ // Bosses up to the rim, with ribs, against a wall; lips, grooves and outside rims. None
+ // reaches the rim's bands, so each lip adds, and the groove (no gap) takes, its band: the
+ // inside one (78 × 48 − 76 × 46) × 2, the outside one (80 × 50 − 78 × 48) × 2.
let bosses = [
json!({"position": [15, 15, 2], "diameter": 8, "height": 28, "hole_diameter": 3}),
// Ribs out to the inner walls (15 − 4 − 9 = 2), and short of them.
@@ -3127,13 +3130,14 @@ fn lip_after_boss() {
json!({"position": [5.5, 25, 2], "diameter": 7, "height": 12, "hole_diameter": 2.5}),
json!({"position": [40, 25, 2], "diameter": 8, "height": 20, "draft": "1 deg", "fillet": 1}),
];
+ let outside = (80.0 * 50.0 - 78.0 * 48.0) * 2.0;
let lips = [
- json!({"face": [1, 25, 30], "width": 1, "height": 2}),
- json!({"face": [1, 25, 30], "width": 1, "height": 2, "type": "groove"}),
- json!({"face": [1, 25, 30], "width": 1, "height": 2, "side": "outside"}),
+ (json!({"face": [1, 25, 30], "width": 1, "height": 2}), inside),
+ (json!({"face": [1, 25, 30], "width": 1, "height": 2, "type": "groove"}), -inside),
+ (json!({"face": [1, 25, 30], "width": 1, "height": 2, "side": "outside"}), outside),
];
for (i, b) in bosses.iter().enumerate() {
- for (j, l) in lips.iter().enumerate() {
+ for (j, (l, band)) in lips.iter().enumerate() {
let mut s = Session::default();
run(&mut s, "solid.box", json!({"length": 80, "width": 50, "height": 30}));
run(&mut s, "solid.shell", json!({"faces": [[40, 25, 30]], "thickness": 2}));
@@ -3142,7 +3146,7 @@ fn lip_after_boss() {
let v0 = volume(&mut s);
let r = s.execute("plastic.lip", l);
assert!(r.is_ok(), "boss {i} lip {j}: {r:?}");
- assert!((volume(&mut s) - v0).abs() > 1.0, "boss {i} lip {j}");
+ assert!(rel(volume(&mut s) - v0, *band) < 1e-6, "boss {i} lip {j}: {} vs {band}", volume(&mut s) - v0);
}
}
}
@@ -3168,16 +3172,24 @@ fn hole_as_deep_as_the_plate() {
let mut s = Session::default();
run(&mut s, "solid.box", json!({"length": 50, "width": 25, "height": 10}));
let v0 = volume(&mut s);
+ // The depth is to the drill point's shoulder: the bore, radius 3, runs the plate's 10 and the
+ // point is in the air below it.
run(&mut s, "solid.hole", json!({"position": [25, 12.5, 10], "diameter": 6, "depth": 10, "type": "drilled"}));
let v = volume(&mut s);
- assert!(v < v0 - PI * 9.0 * 9.0 && v > v0 - PI * 9.0 * 10.0 - 1.0, "{v0} {v}");
- // Other sizes and places, the defaults, and a tapped hole.
- for (i, p) in [
- json!({"position": [10, 10, 10], "diameter": 5, "depth": 10, "type": "drilled"}),
- json!({"position": [40, 15, 10], "diameter": 5, "depth": 10}),
- json!({"position": [12, 6, 10], "diameter": 8.5, "depth": 10}),
- json!({"position": [30, 7, 10], "thread": "M6", "depth": 10}),
- json!({"position": [20, 18, 10], "diameter": 4, "depth": 10, "tip_angle": 90}),
+ assert!((v0 - v - PI * 9.0 * 10.0).abs() < 4e-7 * v0, "{v0} {v}");
+ // Other sizes and places, the defaults, and a tapped hole: each a through bore of its drill,
+ // 10 long. The 8.5 at (12, 6) overlaps the 5 at (10, 10) (√20 apart, under 2.5 + 4.25), so
+ // it takes its bore less the lens they share, 10 long.
+ let (r, q, d) = (2.5f64, 4.25f64, 20f64.sqrt());
+ let lens = r * r * ((d * d + r * r - q * q) / (2.0 * d * r)).acos() + q * q * ((d * d + q * q - r * r) / (2.0 * d * q)).acos()
+ - 0.5 * ((-d + r + q) * (d + r - q) * (d - r + q) * (d + r + q)).sqrt();
+ for (i, (p, cut)) in [
+ (json!({"position": [10, 10, 10], "diameter": 5, "depth": 10, "type": "drilled"}), PI * 6.25 * 10.0),
+ (json!({"position": [40, 15, 10], "diameter": 5, "depth": 10}), PI * 6.25 * 10.0),
+ (json!({"position": [12, 6, 10], "diameter": 8.5, "depth": 10}), (PI * q * q - lens) * 10.0),
+ // M6 × 1: the tap maker's 5.0 drill.
+ (json!({"position": [30, 7, 10], "thread": "M6", "depth": 10}), PI * 6.25 * 10.0),
+ (json!({"position": [20, 18, 10], "diameter": 4, "depth": 10, "tip_angle": 90}), PI * 4.0 * 10.0),
]
.into_iter()
.enumerate()
@@ -3185,7 +3197,8 @@ fn hole_as_deep_as_the_plate() {
let before = volume(&mut s);
let r = s.execute("solid.hole", &p);
assert!(r.is_ok(), "{i} {p}: {r:?}");
- assert!(volume(&mut s) < before - 1.0, "{i} {p}");
+ let v = volume(&mut s);
+ assert!((before - v - cut).abs() < 4e-7 * before, "{i} {p}: {} vs {cut}", before - v);
}
}
diff --git a/crates/engine/src/tests/loft_cut.rs b/crates/engine/src/tests/loft_cut.rs
deleted file mode 100644
index 7bb5dcf300..0000000000
--- a/crates/engine/src/tests/loft_cut.rs
+++ /dev/null
@@ -1,53 +0,0 @@
-//! A hollow elbow: a tube lofted from round sections along an arc, minus a narrower tube lofted the
-//! same way (and running 5 mm past both ends, so the cut opens the ends).
-//!
-//! The boolean gave up with "no result" for some section counts and not for their neighbours: the
-//! points of the intersection curves lie on the seam between the two halves of a loft's circle,
-//! where truck's search for their parameters (from the previous point's) failed.
-
-use std::f64::consts::PI;
-
-use super::*;
-
-/// `sections` circles of radius `radius` centred on an arc of radius `path` turning `angle`
-/// (radians), in planes normal to the arc, lofted into the body `body`; `over` extends the
-/// sections past both ends along the tangent.
-fn tube(s: &mut Session, body: &str, radius: f64, path: f64, angle: f64, sections: usize, over: f64) {
- let mut stations: Vec<[f64; 4]> = (0..sections)
- .map(|i| {
- let t = angle * i as f64 / (sections - 1) as f64;
- [path * t.cos(), path * t.sin(), -t.sin(), t.cos()]
- })
- .collect();
- if over > 0.0 {
- let (a, b) = (stations[0], stations[sections - 1]);
- stations.insert(0, [a[0] - over * a[2], a[1] - over * a[3], a[2], a[3]]);
- stations.push([b[0] + over * b[2], b[1] + over * b[3], b[2], b[3]]);
- }
- let mut list = Vec::new();
- for (i, [x, y, tx, ty]) in stations.iter().enumerate() {
- let name = format!("{body}{i}");
- run(s, "sketch.create", json!({"plane": {"origin": [x, y, 0], "x_dir": [0, 0, 1], "y_dir": [ty, -tx, 0]}, "name": name}));
- run(s, "sketch.circle.center", json!({"center": [0, 0], "radius": radius}));
- run(s, "sketch.finish", json!({}));
- list.push(json!({"sketch": name}));
- }
- run(s, "solid.loft", json!({"sections": list, "operation": "new", "name": format!("Loft{body}"), "body_name": body}));
-}
-
-#[test]
-fn a_hollow_elbow_cut_from_two_lofted_tubes() {
- let (path, outer, inner) = (100.0, 30.0, 18.0);
- // Each of these gave up in truck's `parameter boundary` step.
- for (degrees, sections) in [(60.0, 5), (60.0, 7), (90.0, 10), (120.0, 7), (120.0, 13)] {
- let angle = degrees * PI / 180.0;
- let mut s = Session::default();
- tube(&mut s, "Outer", outer, path, angle, sections, 0.0);
- tube(&mut s, "Inner", inner, path, angle, sections, 5.0);
- run(&mut s, "solid.combine", json!({"target": "Outer", "tools": ["Inner"], "operation": "cut", "name": "Hollow"}));
- assert_eq!(s.model.state().bodies.len(), 1, "{degrees} degrees, {sections} sections");
- let want = PI * (outer * outer - inner * inner) * path * angle;
- let got = volume(&mut s);
- assert!(rel(got, want) < 3e-3, "{degrees} degrees, {sections} sections: {got} vs {want}");
- }
-}
diff --git a/crates/engine/src/tests/robustness.rs b/crates/engine/src/tests/robustness.rs
index 4f126969e1..7c5a229624 100644
--- a/crates/engine/src/tests/robustness.rs
+++ b/crates/engine/src/tests/robustness.rs
@@ -5,7 +5,7 @@ use std::f64::consts::PI;
use serde_json::{Value, json};
-use super::{run, volume};
+use super::run;
use crate::Session;
use crate::fuzz;
@@ -366,74 +366,6 @@ fn fixed_seeds_run_clean() {
const FIXED_SEEDS: &[u64] = &[3, 5, 11, 16, 21, 29, 33, 35];
-/// A drilled hole as deep as the plate: the drill point breaks through the bottom.
-#[test]
-fn hole_as_deep_as_the_plate() {
- let mut s = Session::default();
- run(&mut s, "solid.box", json!({"length": 50, "width": 25, "height": 10}));
- let v0 = volume(&mut s);
- run(&mut s, "solid.hole", json!({"position": [25, 12.5, 10], "diameter": 6, "depth": 10, "type": "drilled"}));
- let v = volume(&mut s);
- assert!(v < v0 - PI * 9.0 * 9.0 && v > v0 - PI * 9.0 * 10.0 - 1.0, "{v0} {v}");
- // Other sizes and places, the defaults, and a tapped hole.
- for (i, p) in [
- json!({"position": [10, 10, 10], "diameter": 5, "depth": 10, "type": "drilled"}),
- json!({"position": [40, 15, 10], "diameter": 5, "depth": 10}),
- json!({"position": [12, 6, 10], "diameter": 8.5, "depth": 10}),
- json!({"position": [30, 7, 10], "thread": "M6", "depth": 10}),
- json!({"position": [20, 18, 10], "diameter": 4, "depth": 10, "tip_angle": 90}),
- ]
- .into_iter()
- .enumerate()
- {
- let before = volume(&mut s);
- let r = s.execute("solid.hole", &p);
- assert!(r.is_ok(), "{i} {p}: {r:?}");
- assert!(volume(&mut s) < before - 1.0, "{i} {p}");
- }
-}
-
-/// An enclosure built in the usual order: shell, bosses, then the lip on the rim.
-#[test]
-fn lip_after_boss() {
- let mut s = Session::default();
- run(&mut s, "solid.box", json!({"length": 80, "width": 50, "height": 30}));
- run(&mut s, "solid.shell", json!({"faces": [[40, 25, 30]], "thickness": 2}));
- run(&mut s, "plastic.boss", json!({"position": [10, 10, 2], "diameter": 7, "height": 12, "hole_diameter": 2.5}));
- run(&mut s, "plastic.boss", json!({"position": [70, 40, 2], "diameter": 7, "height": 12, "hole_diameter": 2.5}));
- let v0 = volume(&mut s);
- run(&mut s, "plastic.lip", json!({"face": [1, 25, 30], "width": 1, "height": 2}));
- let v = volume(&mut s);
- assert!(v > v0 + 1.0, "{v0} {v}");
- // Bosses up to the rim, with ribs, against a wall; lips, grooves and outside rims.
- let bosses = [
- json!({"position": [15, 15, 2], "diameter": 8, "height": 28, "hole_diameter": 3}),
- // Ribs out to the inner walls (15 − 4 − 9 = 2), and short of them.
- json!({"position": [15, 15, 2], "diameter": 8, "height": 12, "ribs": 4, "rib_thickness": 1.5, "rib_length": 9}),
- json!({"position": [15, 15, 2], "diameter": 8, "height": 12, "ribs": 4, "rib_thickness": 1.5, "rib_length": 6}),
- json!({"position": [5.5, 25, 2], "diameter": 7, "height": 12, "hole_diameter": 2.5}),
- json!({"position": [40, 25, 2], "diameter": 8, "height": 20, "draft": "1 deg", "fillet": 1}),
- ];
- let lips = [
- json!({"face": [1, 25, 30], "width": 1, "height": 2}),
- json!({"face": [1, 25, 30], "width": 1, "height": 2, "type": "groove"}),
- json!({"face": [1, 25, 30], "width": 1, "height": 2, "side": "outside"}),
- ];
- for (i, b) in bosses.iter().enumerate() {
- for (j, l) in lips.iter().enumerate() {
- let mut s = Session::default();
- run(&mut s, "solid.box", json!({"length": 80, "width": 50, "height": 30}));
- run(&mut s, "solid.shell", json!({"faces": [[40, 25, 30]], "thickness": 2}));
- let r = s.execute("plastic.boss", b);
- assert!(r.is_ok(), "boss {i}: {r:?}");
- let v0 = volume(&mut s);
- let r = s.execute("plastic.lip", l);
- assert!(r.is_ok(), "boss {i} lip {j}: {r:?}");
- assert!((volume(&mut s) - v0).abs() > 1.0, "boss {i} lip {j}");
- }
- }
-}
-
/// A lip, a groove and an outside lip on the rim of a rounded, shelled enclosure (#29: the
/// lip's walls continue the shell's walls, which a boolean join couldn't do).
#[test]
diff --git a/crates/kernel/src/issue_1080_tests.rs b/crates/kernel/src/issue_1080_tests.rs
index 702a0495cf..290e07ae71 100644
--- a/crates/kernel/src/issue_1080_tests.rs
+++ b/crates/kernel/src/issue_1080_tests.rs
@@ -92,7 +92,11 @@ fn chamfers_crossing_each_other_are_refused() {
// Below half the width both build.
let b = crate::chamfer(&rib, &edges, 1.5).unwrap();
assert!((b.volume_at(0.01).unwrap() - (3200.0 - 2.0 * 0.5 * 1.5 * 1.5 * 40.0)).abs() < 1e-6);
- assert!(crate::fillet(&rib, &edges, 1.5).is_ok());
+ // Two rounds of radius 1.5 along 40: each removes (1 − π/4)·1.5² of section.
+ let b = crate::fillet(&rib, &edges, 1.5).unwrap();
+ let want = 3200.0 - 2.0 * (1.0 - std::f64::consts::FRAC_PI_4) * 2.25 * 40.0;
+ let v = crate::measure(&b).unwrap().volume;
+ assert!((v - want).abs() < 1e-6 * want, "{v} vs {want}");
}
/// Edges meeting at a corner mitre: their chamfers overlap there and are not refused for it.
@@ -100,6 +104,14 @@ fn chamfers_crossing_each_other_are_refused() {
fn chamfers_meeting_at_a_corner_still_build() {
let b = box_solid(Vec3::new(0.0, 0.0, 0.0), Vec3::new(20.0, 20.0, 20.0)).unwrap();
let edges = [Vec3::new(10.0, 0.0, 20.0), Vec3::new(20.0, 10.0, 20.0), Vec3::new(10.0, 20.0, 20.0), Vec3::new(0.0, 10.0, 20.0)];
- assert!(crate::chamfer(&b, &edges, 5.0).is_ok());
- assert!(crate::fillet(&b, &edges, 5.0).is_ok());
+ // The chamfers leave the 20 x 20 x 15 block under a frustum from 20 x 20 to 10 x 10, 5 high:
+ // 6000 + (5/3)(400 + 100 + √(400·100)).
+ let v = crate::measure(&crate::chamfer(&b, &edges, 5.0).unwrap()).unwrap().volume;
+ let want = 6000.0 + 5.0 / 3.0 * (400.0 + 100.0 + 200.0);
+ assert!((v - want).abs() < 1e-6 * want, "{v} vs {want}");
+ // The rounds: (1 − π/4)·25 of section along each edge's 10 mm between the corners, and in each
+ // 5 mm corner cube the two cylinders' intersection is kept, ∫₀⁵ (25 − w²) dw = 250/3 of 125.
+ let v = crate::measure(&crate::fillet(&b, &edges, 5.0).unwrap()).unwrap().volume;
+ let want = 8000.0 - 4.0 * (1.0 - std::f64::consts::FRAC_PI_4) * 25.0 * 10.0 - 4.0 * (125.0 - 250.0 / 3.0);
+ assert!((v - want).abs() < 1e-6 * want, "{v} vs {want}");
}