Skip to content

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

Wave 408/509: Kernel tests: a swallowed tip's volume is the drafted wedge's, every wall at the taper (and 3 more)

jyviko1bffc3cc786e
diff --git a/crates/engine/src/cmd/surface.rs b/crates/engine/src/cmd/surface.rs
index d7626c7768..7d9235688c 100644
--- a/crates/engine/src/cmd/surface.rs
+++ b/crates/engine/src/cmd/surface.rs
@@ -1954,17 +1954,25 @@ mod tests {
(numbers[0], numbers[1])
}
- /// Surface Fair on the fitted B-spline face of an offset sphere: the face is smoothed in
- /// place, its edges do not move at all, the reply carries the energy before and after, undo,
- /// redo and a cold reopen keep it, and hostile inputs are refused.
+ /// Surface Fair on the fitted B-spline face of a patch on a rim that is not planar (a box's
+ /// top cut away by a sideways cylinder): the face is smoothed in place, its edges do not move
+ /// at all, the reply carries the energy before and after, undo, redo and a cold reopen keep
+ /// it, and hostile inputs are refused.
#[test]
fn fairing_a_b_spline_face_keeps_its_edges_and_reports_its_energy() {
let mut s = Session::default();
- run(&mut s, "solid.sphere", json!({"radius": 20, "body_name": "Ball"}));
- // A sphere is neither a plane nor a cylinder, so its offset is a fitted B-spline face.
- run(&mut s, "surface.offset", json!({"faces": [[0, 0, 20]], "body": "Ball", "distance": 2, "body_name": "Shell"}));
+ run(&mut s, "solid.box", json!({"length": 28, "width": 28, "height": 20, "corner": [-14, -14, 0], "body_name": "Saddle"}));
+ run(
+ &mut s,
+ "solid.cylinder",
+ json!({"radius": 40, "height": 120, "base": [0, -60, 50], "axis": "Y", "operation": "cut", "targets": ["Saddle"]}),
+ );
+ let z = |x: f64| 50.0 - (1600.0 - x * x).sqrt();
+ let rim = json!([[14, 0, z(14.0)], [0, 14, z(0.0)], [-14, 0, z(-14.0)], [0, -14, z(0.0)]]);
+ // The rim is not planar, so its patch is a fitted B-spline face.
+ run(&mut s, "surface.patch", json!({"edges": rim, "body": "Saddle", "body_name": "Shell"}));
let st = s.world_state().unwrap();
- assert!(st.body("Shell").unwrap().body.is_surface(), "an offset sphere face is a sheet");
+ assert!(st.body("Shell").unwrap().body.is_surface(), "a patch on a rim is a sheet");
let before: Vec<solidcraft_geom::Vec3> = st.body("Shell").unwrap().body.edges(0.1).unwrap().iter().flat_map(|e| e.points.clone()).collect();
assert!(!before.is_empty());
let area_before = area(&s, "Shell");
diff --git a/crates/kernel/src/tests.rs b/crates/kernel/src/tests.rs
index 06ab45c6a7..7e8be3d44c 100644
--- a/crates/kernel/src/tests.rs
+++ b/crates/kernel/src/tests.rs
@@ -668,28 +668,47 @@ fn tapered_extrude_of_cubics_is_close_to_the_true_offset() {
assert!(rel(m.volume, v) < 5e-4, "{} vs {v}", m.volume);
}
-/// A wedge whose tip is a flat line far shorter than the taper moves its sides in: the tip line
-/// goes, the two long sides meet, and its side is the flat triangle up to there. The solid is a
-/// prismatoid, its volume h/6 · (A₀ + 4·A_mid + A₁).
+/// A wedge whose tip is a flat line far shorter than the taper moves its sides in: every wall,
+/// the tip's too, leans by the taper, so the tip's wall is a triangle that closes where the two
+/// long sides meet across it (z* ≈ 0.126 of h = 2), and above that the long sides meet each
+/// other along a ridge. The section at height z is the profile's lines moved in by z·tan(a): a
+/// quadrilateral below z*, a triangle above, each with an area quadratic in z, so the volume is
+/// exact as Simpson's rule on each of the two pieces.
#[test]
fn tapered_extrude_swallows_a_short_tip() {
let (h, a) = (2.0, 5f64.to_radians());
- let d = h * a.tan();
let pts = [Vec2::new(0.0, -2.0), Vec2::new(20.0, -0.01), Vec2::new(20.0, 0.01), Vec2::new(0.0, 2.0)];
let b = extrude_tapered(&Plane::XY, &Region2 { outer: Loop2::polygon(&pts), holes: vec![] }, h, 1.0, -a, crate::DraftCorner::default()).unwrap();
- // The top: the three long lines moved in by d (to their left, the loop running
- // counter-clockwise), meeting each other.
- let moved = |p: Vec2, q: Vec2| {
- let t = (q - p).normalized().unwrap();
- (p + t.perp() * d, t)
+ // Side i (from pts[i] to the next) moved in by t (to its left, the loop running
+ // counter-clockwise), as a point and its direction.
+ let moved = |i: usize, t: f64| {
+ let (p, q) = (pts[i], pts[(i + 1) % pts.len()]);
+ let u = (q - p).normalized().unwrap();
+ (p + u.perp() * t, u)
};
let meet = |(p0, t0): (Vec2, Vec2), (p1, t1): (Vec2, Vec2)| p0 + t0 * ((p1 - p0).cross(t1) / t0.cross(t1));
- let (low, high, left) = (moved(pts[0], pts[1]), moved(pts[2], pts[3]), moved(pts[3], pts[0]));
- let top = [meet(left, low), meet(low, high), meet(high, left)];
- let area = |q: &[Vec2]| solidcraft_geom::Ring::from(q.to_vec()).signed_area();
- // Halfway up, each side edge's midpoint (the two tip corners both run to the top's tip).
- let mid = [pts[0].lerp(top[0], 0.5), pts[1].lerp(top[1], 0.5), pts[2].lerp(top[1], 0.5), pts[3].lerp(top[2], 0.5)];
- let v = h / 6.0 * (area(&pts) + 4.0 * area(&mid) + area(&top));
+ // Where the long sides meet, measured off the moved tip line (into the solid positive): it
+ // runs linearly with t, and its root is the offset at which the tip's wall closes.
+ let past_tip = |t: f64| {
+ let (q, u) = moved(1, t);
+ (meet(moved(0, t), moved(2, t)) - q).cross(u)
+ };
+ let d = h * a.tan();
+ let closes = d * past_tip(0.0) / (past_tip(0.0) - past_tip(d));
+ let z_closes = closes / a.tan();
+ assert!(z_closes > 0.0 && z_closes < h, "the tip's wall closes partway up, at {z_closes}");
+ let section = |z: f64| -> Vec<Vec2> {
+ let t = z * a.tan();
+ if t <= closes {
+ (0..4).map(|i| meet(moved((i + 3) % 4, t), moved(i, t))).collect()
+ } else {
+ let (low, high, left) = (moved(0, t), moved(2, t), moved(3, t));
+ vec![meet(left, low), meet(low, high), meet(high, left)]
+ }
+ };
+ let area = |z: f64| solidcraft_geom::Ring::from(section(z)).signed_area();
+ let simpson = |z0: f64, z1: f64| (z1 - z0) / 6.0 * (area(z0) + 4.0 * area(0.5 * (z0 + z1)) + area(z1));
+ let v = simpson(0.0, z_closes) + simpson(z_closes, h);
let m = measure(&b).unwrap();
assert!(rel(m.volume, v) < 1e-6, "{} vs {v}", m.volume);
assert_eq!(m.merged.faces, 6, "bottom, top, three sides and the tip's triangle");
@@ -1474,25 +1493,44 @@ fn shell_non_convex() {
assert!(rel(v, outer - cavity) < 1e-6, "{v} vs {}", outer - cavity);
}
-/// A basin: a slab with a tapered pocket and a 4 mm front rim, shelled from below. A wall
-/// thicker than the rim (with the pocket's slope) would turn the top face inside out: refused,
-/// not a body with wrong mass properties.
+/// A basin: a 100 × 60 × 30 slab with a tapered pocket (60 × 46 at the top, 4 mm from the front,
+/// 40 × 20 at its floor 20 down) shelled from below, each wall moved in by t with sharp edges.
+/// The cavity is the slab shrunk by t (open at z = 0) less the pocket grown by t, each sloped side
+/// moved out along its normal; the volume, slab − pocket − cavity, is a cubic in z integrated by
+/// hand. From t ≈ 2.70 the front side's offset passes the front wall's below the 4 mm rim, and
+/// the rim stays solid: the cavity's front stops at y = t (from z = (28 − (1 + √1.64) t)/0.8 up).
#[test]
-fn shell_thicker_than_a_rim_is_refused() {
+fn shell_of_a_basin_past_its_rim_keeps_the_rim_solid() {
let slab = box_solid(Vec3::ZERO, Vec3::new(100.0, 60.0, 30.0)).unwrap();
let rect = |x0: f64, y0: f64, x1: f64, y1: f64| Loop2::polygon(&[Vec2::new(x0, y0), Vec2::new(x1, y0), Vec2::new(x1, y1), Vec2::new(x0, y1)]);
let at = |z: f64| Plane::new(Vec3::new(0.0, 0.0, z), Vec3::X, Vec3::Y).unwrap();
let pocket = loft(&[(at(30.0), rect(20.0, 4.0, 80.0, 50.0)), (at(10.0), rect(30.0, 20.0, 70.0, 40.0))]).unwrap();
let basin = boolean(&slab, &pocket, BoolOp::Cut).unwrap().unwrap();
let open = [Vec3::new(50.0, 30.0, 0.0)];
- let e = shell(&basin, &open, 6.0).unwrap_err().to_string();
- assert!(e.contains("rim or ledge"), "{e}");
- let (whole, thin) = (measure(&basin).unwrap(), measure(&shell(&basin, &open, 1.0).unwrap()).unwrap());
- assert!(thin.volume > 0.0 && thin.volume < whole.volume, "{} vs {}", thin.volume, whole.volume);
- let (lo, hi) = (thin.bbox.min, thin.bbox.max);
- let c = thin.centroid;
- assert!(c.x > lo.x && c.x < hi.x && c.y > lo.y && c.y < hi.y && c.z > lo.z && c.z < hi.z, "{c:?}");
- assert!((c.x - 50.0).abs() < 1e-3, "{c:?}");
+ // The pocket grown by t at height z: x from 35 − z/2 − t√5/2 to 65 + z/2 + t√5/2, y from
+ // 28 − 0.8 z − t√1.64 (or y = t, where that is less) to 35 + z/2 + t√5/2, for
+ // z = 10 − t … 30 − t; its sides stay inside the shrunk slab in x and at the back for t ≤ 6.
+ let (r5, r164) = (5f64.sqrt(), 1.64f64.sqrt());
+ let integral = |c: [f64; 3], lo: f64, hi: f64| c[0] * (hi - lo) + c[1] * (hi * hi - lo * lo) / 2.0 + c[2] * (hi.powi(3) - lo.powi(3)) / 3.0;
+ // (w0 + z)(h0 + h1 z) as coefficients in z.
+ let product = |w0: f64, h0: f64, h1: f64| [w0 * h0, w0 * h1 + h0, h1];
+ let grown = |t: f64| {
+ let (lo, hi, w0) = (10.0 - t, 30.0 - t, 30.0 + r5 * t);
+ let full = product(w0, 7.0 + t * (r5 / 2.0 + r164), 1.3);
+ let z0 = ((28.0 - r164 * t - t) / 0.8).clamp(lo, hi);
+ integral(full, lo, z0) + integral(product(w0, 35.0 + (r5 / 2.0 - 1.0) * t, 0.5), z0, hi)
+ };
+ let volume = |t: f64| 100.0 * 60.0 * 30.0 - grown(0.0) - (100.0 - 2.0 * t) * (60.0 - 2.0 * t) * (30.0 - t) + grown(t);
+ let whole = measure(&basin).unwrap();
+ assert!(rel(whole.volume, 180_000.0 - grown(0.0)) < 1e-9, "{}", whole.volume);
+ for (t, want) in [(1.0, 17_105.624_041_5), (4.0, 63_560.249_228_4), (6.0, 90_113.920_210_1)] {
+ assert!(rel(volume(t), want) < 1e-12, "the closed form at {t}: {}", volume(t));
+ let m = measure(&shell(&basin, &open, t).unwrap()).unwrap();
+ assert!(rel(m.volume, want) < 1e-6, "t {t}: {} vs {want}", m.volume);
+ let (lo, hi, c) = (m.bbox.min, m.bbox.max, m.centroid);
+ assert!(c.x > lo.x && c.x < hi.x && c.y > lo.y && c.y < hi.y && c.z > lo.z && c.z < hi.z, "{c:?}");
+ assert!((c.x - 50.0).abs() < 1e-3, "{c:?}");
+ }
}
#[test]
diff --git a/crates/smlib/SMLIB_PREBUILT b/crates/smlib/SMLIB_PREBUILT
index 6760c936ee..6e6ca87dd3 100644
--- a/crates/smlib/SMLIB_PREBUILT
+++ b/crates/smlib/SMLIB_PREBUILT
@@ -1,10 +1,10 @@
# Rust target, SHA-256 of smlib-<target>.tar.gz in the release prebuilt-<SMLIB_REV> of
# tangency-labs/smlib. Written by scripts/fetch-smlib.sh --pin; never edited by hand.
-aarch64-apple-darwin 0cc77e26edf71fdd25881bc2b27a37d7eae6e4983939ce7769e47cdd88b7d142
-aarch64-pc-windows-msvc f6802ecb6d21bad7c8e23a0371aaa4d79cac9d75f271f00900067a1777353b39
-aarch64-unknown-linux-gnu 0c9b9f8fe3522e1baab52d04f8f003da94b73b7837c60d8ec4d7bd8b34d32889
-i686-pc-windows-msvc 1a430d114c38010eefbbd0b89cd060d36abc5262dbfafbbb08f09fefc1258c30
-wasm32-unknown-unknown 53f03772528182d2cc9398eaaebcae1c643ca6d2ff5553b19db69e713a84579e
-x86_64-apple-darwin 26cfc2cd85d049159893972c2dbdcdf08ad4f88e57d54a9e23146e86d4d3b8ca
-x86_64-pc-windows-msvc ec9b082bb11a00b25c967a3f4cf8f497d5e69cf6a7f009ecff387d41697f162f
-x86_64-unknown-linux-gnu d75936dadf5553e5d3e45b9b8136323285b1ee040fcaf1d6e2df6754c99e02c6
+aarch64-apple-darwin 449768fb27dddbc2613bba876e96a762fd4dc36f8ce43e498679a98c3a9c03f1
+aarch64-pc-windows-msvc 1ce7c9e59d390c855c8d73688340fb747ae6e00fe969065ba646980735abc447
+aarch64-unknown-linux-gnu 014ba0b4734de8fb8665b58394624a68dced523b09a804f2258a3808cfcda7b2
+i686-pc-windows-msvc 38ff5885eaba1f0681eb2be777b16fdfdb986ca66c0496c4f4235ec3dc045e84
+wasm32-unknown-unknown 7227d3b5f208122d39dfab0b9573efc4037d0ea9fd0bfec6f91dba27a9434fe0
+x86_64-apple-darwin cde31baadd1a807f6a58b3ab17815a939940e2bf82ee032c3a526ecf6d0a41d4
+x86_64-pc-windows-msvc c0d02f0e0e59c977fd134d872e049d7eb3e027be4ea9c888418fea0cbabd675c
+x86_64-unknown-linux-gnu edc4a1e08d3263bafbcd44dbd243a7815ca4d535dc308a89b1844126c0bea613
diff --git a/crates/smlib/SMLIB_REV b/crates/smlib/SMLIB_REV
index 27d149b0bc..be726d954d 100644
--- a/crates/smlib/SMLIB_REV
+++ b/crates/smlib/SMLIB_REV
@@ -1 +1 @@
-1ccd461266aa36470d29a5e2634cf6c622830dc8
+985faaeed0311d5eaf5e6f091f62ac55c382597c