SolidCraft: an open-source parametric 3D CAD application built in pure Rust. Desktop, web, and automation through MCP.
Two-distance chamfers of free-form rims: a test on the SMLib kernel (#4332) (#4333)
Alexander Puzyniaee02080a25e0
diff --git a/crates/kernel/src/chamfer_sides_curved_tests.rs b/crates/kernel/src/chamfer_sides_curved_tests.rsindex 6486162d53..176e7a135a 100644--- a/crates/kernel/src/chamfer_sides_curved_tests.rs+++ b/crates/kernel/src/chamfer_sides_curved_tests.rs@@ -5,7 +5,8 @@use std::f64::consts::PI;-use solidcore_record::{Loop2, Plane, Region2, Vec2, Vec3};+use solidcore_record::{Loop2, Plane, Region2, Seg2, Vec2, Vec3};+use solidcraft_geom::Seg2Geom;use crate::build::{box_solid, extrude, extrude_tapered};use crate::{Body, BoolOp, ChamferSide, boolean, chamfer, chamfer_sides};@@ -238,3 +239,46 @@ fn rounded_plate_top_loop() {let out = chamfer_sides(&b, &picks, 2.0, ChamferSide::Distance(2.0), false).unwrap();assert!(rel(volume(&out), volume(&equal)) < 1e-9);}++/// A smooth oval of four cubic Béziers (no lines or arcs) round the origin, 40 × 28, extruded 10.+fn bezier_oval() -> (Body, f64) {+ let (a, b, k) = (20.0, 14.0, 0.5523);+ let q = |x: f64, y: f64| Vec2::new(x, y);+ let segs = vec![+ Seg2::Cubic { p0: q(a, 0.0), p1: q(a, b * k), p2: q(a * k, b), p3: q(0.0, b) },+ Seg2::Cubic { p0: q(0.0, b), p1: q(-a * k, b), p2: q(-a, b * k), p3: q(-a, 0.0) },+ Seg2::Cubic { p0: q(-a, 0.0), p1: q(-a, -b * k), p2: q(-a * k, -b), p3: q(0.0, -b) },+ Seg2::Cubic { p0: q(0.0, -b), p1: q(a * k, -b), p2: q(a, -b * k), p3: q(a, 0.0) },+ ];+ let perimeter = segs.iter().map(Seg2::length).sum();+ let region = Region2 { outer: Loop2 { segs }, holes: vec![] };+ (extrude(&Plane::XY, &[region], 0.0, 10.0).unwrap().pop().unwrap(), perimeter)+}++/// The volume a chamfer `across` the top and `down` the wall takes off a convex outline of length+/// `perimeter` extruded square: at depth h the wall moves in by w = across·(1 − h/down), and the+/// strip between a convex loop and its offset by w has area perimeter·w − π w².+fn rim_volume(perimeter: f64, across: f64, down: f64) -> f64 {+ perimeter * across * down / 2.0 - PI * across * across * down / 3.0+}++#[test]+fn spline_outline_rim_by_two_distances() {+ let (oval, perimeter) = bezier_oval();+ let base = volume(&oval);+ // The whole rim, as a pick takes it: the four Béziers' tangent chain.+ let rim = crate::tangent_chain(&oval, &[Vec3::new(14.6, 10.2, 10.0)]).unwrap();+ assert_eq!(rim.len(), 4);+ // 1 across the top, 7 down the wall; flipped, 1 down the wall and 7 across the top.+ for (flip, across, down) in [(false, 1.0, 7.0), (true, 7.0, 1.0)] {+ let out = chamfer_sides(&oval, &rim, 1.0, ChamferSide::Distance(7.0), flip).unwrap();+ sound(&out);+ let expected = base - rim_volume(perimeter, across, down);+ assert!(rel(volume(&out), expected) < 1e-4, "flip {flip}: {} vs {expected}", volume(&out));+ }+ // By an angle: 2 across the top at 60° from it, 2·tan 60° down the square wall.+ let out = chamfer_sides(&oval, &rim, 2.0, ChamferSide::Angle(60f64.to_radians()), false).unwrap();+ sound(&out);+ let expected = base - rim_volume(perimeter, 2.0, 2.0 * 60f64.to_radians().tan());+ assert!(rel(volume(&out), expected) < 1e-4, "{} vs {expected}", volume(&out));+}