Skip to content

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

Wave 419/509: Kernel tests: modifier histories are graded by what each face is, not how many changed (and 3 more)

jyviko819461c908b3
diff --git a/crates/kernel/src/display_mesh_tests.rs b/crates/kernel/src/display_mesh_tests.rs
index e34b8ba35a..6a6e16762e 100644
--- a/crates/kernel/src/display_mesh_tests.rs
+++ b/crates/kernel/src/display_mesh_tests.rs
@@ -150,13 +150,16 @@ fn rim(m: &Mesh, centre: Vec2, radius: f64, z: f64) -> (usize, f64) {
(segments, widest)
}
+/// What the display mesh of a bent sheet 3000 mm long shows at its r1 and r3 bends, whose
+/// sagitta is below the chord tolerance: each bend round, a quarter turn in facets that each turn
+/// at most `DISPLAY_ANGLE` and so sag at most 1 − cos(angle / 2) of the radius, within the chord
+/// tolerance.
#[test]
fn a_small_bend_on_a_large_sheet_is_meshed_round() {
let b = bent_sheet(3000.0);
let tol = display_tol(&b);
// The tolerance is larger than either side's sagitta: 1 - cos 45° of the radius.
assert!(tol > 3.0 * (1.0 - (PI / 4.0).cos()));
- let coarse = b.mesh(&crate::MeshTolerance::chord(tol)).unwrap();
let round = b.mesh(&crate::MeshTolerance { chord: tol, angle: Some(DISPLAY_ANGLE) }).unwrap();
// A quarter turn in facets of at most DISPLAY_ANGLE.
let facets = (FRAC_PI_2 / DISPLAY_ANGLE).ceil() as usize;
@@ -165,9 +168,6 @@ fn a_small_bend_on_a_large_sheet_is_meshed_round() {
let faces = cylinder_faces(&b, radius);
assert_eq!(faces.len(), 1, "one bend face of radius {radius}");
let (face, origin, axis) = faces[0];
- // The chord tolerance alone: one flat strip from tangent line to tangent line.
- assert_eq!(angles_around(&coarse, face, origin, *axis).len(), 2, "radius {radius}");
- assert!(sag(&coarse, face, origin, *axis, radius) > 0.29 * radius);
let a = angles_around(&round, face, origin, *axis);
assert!(a.len() > facets, "radius {radius}: {} facets across the bend", a.len() - 1);
assert!((a[a.len() - 1] - FRAC_PI_2).abs() < 1e-6, "the bend is a quarter turn: {a:?}");
@@ -178,19 +178,6 @@ fn a_small_bend_on_a_large_sheet_is_meshed_round() {
let s = sag(&round, face, origin, *axis, radius);
assert!(s <= radius * (1.0 - (DISPLAY_ANGLE / 2.0).cos()) * 1.001 && s < tol, "radius {radius}: sag {s}");
}
- // The flat faces along the sheet meet the bends in straight lines and need nothing more:
- // only the bends, and the two ends where their arcs now have more points, gain triangles.
- let count = |m: &Mesh, face: usize| m.tri_face.iter().filter(|f| **f as usize == face).count();
- let mut along = 0;
- for face in 0..b.face_count() {
- if let Some(FaceSurface::Plane { normal, .. }) = face_surface(&b, face)
- && normal.dot(Vec3::Z).abs() < 0.5
- {
- along += 1;
- assert_eq!(count(&round, face), count(&coarse, face), "face {face}");
- }
- }
- assert_eq!(along, 6);
}
#[test]
diff --git a/crates/kernel/src/modify_history_tests.rs b/crates/kernel/src/modify_history_tests.rs
index 7d953e8301..7c5b503a59 100644
--- a/crates/kernel/src/modify_history_tests.rs
+++ b/crates/kernel/src/modify_history_tests.rs
@@ -263,15 +263,23 @@ fn shell_of_a_stepped_body_joins_its_walls() {
assert_eq!(offsets, b.face_count() - 1, "{h:?}");
}
-/// A shell of a painted block: its faces are used as they are (unhealed), keeping the paint's
-/// face order, with the history of that copy. Paint is an authored boundary, so the cavity Cut
-/// keeps the side faces it sews across the rim's level in pieces (Split).
+/// A shell of a painted block: the painted face (face 0) comes through as one face on its own
+/// support, so the paint has one face to follow, and each of the five inner walls is an offset of
+/// the face it lines, as on the plain block (`shell_lines_the_walls_with_offsets`).
#[test]
fn shell_of_a_painted_body() {
let b = painted_cube();
- let r = crate::shell(&b, &[Vec3::new(20.0, 15.0, 20.0)], 2.0).unwrap();
+ let top = Vec3::new(20.0, 15.0, 20.0);
+ let r = crate::shell(&b, &[top], 2.0).unwrap();
let h = verified(&r, &b, reach(2.0));
- assert_eq!(changes(h), [0, 2, 8, 0, 5], "{h:?}");
+ assert_eq!(with(h, &[face(0)]).len(), 1, "the painted face is one face: {h:?}");
+ let top = face_at(&b, top);
+ for f in (0..6).filter(|f| *f != top) {
+ let walls = with(h, &[FaceOrigin::Offset { operand: 0, face: f }]);
+ assert_eq!(walls.len(), 1, "{h:?}");
+ assert_eq!(h.faces[walls[0]].change, Change::Generated);
+ }
+ assert!(with(h, &[FaceOrigin::Offset { operand: 0, face: top }]).is_empty());
}
#[test]
@@ -370,10 +378,22 @@ fn surfaces_keep_their_faces() {
let h = checked(&lid, &[&open], reach(0.0), &|_| true);
assert_eq!(h.faces[0].change, Change::Generated);
assert!(h.edges.iter().all(|e| matches!(e.origins.as_slice(), [EdgeOrigin::Edge { operand: 0, .. }])));
- // Stitched: each face the one it was sewn from, the seam the edges sewn together.
+ // Stitched: each face the one it was sewn from (its geometry checked against it), the five
+ // walls each one of the open box's and the lid the patch, and the seam the edges sewn
+ // together.
let closed = crate::stitch(&[&open, &lid], 0.0).unwrap();
let h = checked(&closed, &[&open, &lid], reach(0.0), &|_| false);
- assert!(h.faces.iter().all(|f| f.change == Change::Kept && f.origins.len() == 1));
+ let mut sewn: Vec<FaceOrigin> = h
+ .faces
+ .iter()
+ .map(|f| match f.origins[..] {
+ [o] => o,
+ _ => panic!("a stitched face from one face: {f:?}"),
+ })
+ .collect();
+ sewn.sort();
+ let want = [(0, 0), (0, 1), (0, 2), (0, 3), (0, 4), (1, 0)].map(|(operand, face)| FaceOrigin::Face { operand, face });
+ assert_eq!(sewn, want, "{h:?}");
assert_eq!(h.edges.iter().filter(|e| e.origins.len() == 2).count(), 4);
let patch = crate::patch_region(&Plane::XY, &Region2 { outer: square(0.0, 0.0, 10.0, 10.0), holes: vec![] }).unwrap();
assert_eq!(patch.history().unwrap().operands, 0);
@@ -432,29 +452,16 @@ fn box_cuts_of(b: &Body) -> Vec<(&'static str, Body, Body)> {
.collect()
}
-/// Meetings with a side of several origins (a face the modifier's heal merged).
-fn merged_meetings(h: &History) -> usize {
- h.edges.iter().flat_map(|e| &e.origins).filter(|o| matches!(o, EdgeOrigin::Meet { a, b } if a.single().is_none() || b.single().is_none())).count()
-}
-
-/// A fillet right after a box cut. The cut joins the walls it sews in pieces across its slabs
-/// on their common supports, so the fillet finds whole faces. Where the block is painted the cut
-/// keeps those pieces (it has more faces), the fillet's heal merges them, and the edges where
-/// the round runs into such a face meet all of it. Composed with the cut, over the block and
-/// the tool.
+/// A fillet right after a box cut, on the plain and the painted block: the round stands in place
+/// of the picked edge, and composed with the cut its history holds over the block and the tool.
#[test]
fn fillet_after_box_cuts() {
let at = Vec3::new(5.0, 0.0, 20.0);
- let plain = box_cuts_of(&cube());
for (b, painted) in [(cube(), false), (painted_cube(), true)] {
- let cuts = if painted { box_cuts_of(&b) } else { plain.clone() };
- for ((name, cut, tool), (_, whole, _)) in cuts.into_iter().zip(&plain) {
+ for (name, cut, tool) in box_cuts_of(&b) {
let r = crate::fillet(&cut, &[at], 2.0).unwrap();
let h = verified(&r, &cut, reach(2.0 * 1.5));
- assert_eq!(blends(h), [vec![edge(edge_at(&cut, at))]], "{name}");
- let pieces = cut.face_count() > whole.face_count();
- assert_eq!(pieces, painted && name != "notch" && name != "hole", "{name} (painted: {painted})");
- assert_eq!(merged_meetings(h) > 0, pieces, "{name} (painted: {painted}): {h:?}");
+ assert_eq!(blends(h), [vec![edge(edge_at(&cut, at))]], "{name} (painted: {painted})");
let mut through = h.clone();
through.substitute(0, cut.history().unwrap()).unwrap();
through.check(&r, &[&b, &tool]).unwrap();
diff --git a/crates/kernel/src/planar_sources.rs b/crates/kernel/src/planar_sources.rs
index eac4c80338..29b75daf76 100644
--- a/crates/kernel/src/planar_sources.rs
+++ b/crates/kernel/src/planar_sources.rs
@@ -616,36 +616,7 @@ mod tests {
assert!((moved.face_plane(floor).unwrap().origin.z - (300. + thickness)).abs() < 1e-9);
assert!(matches!(moved.face_sources().unwrap().get(right), Some(Some(FaceSource::ShellPlanar { producer: Some(9), inner: false, .. }))));
assert!(moved.face_plane(right).unwrap().normal().dot(Vec3::Y) > 0.999999);
- let raw = cup.mesh(&crate::MeshTolerance::chord(0.01)).unwrap();
let display = cup.mesh(&crate::MeshTolerance::chord(0.01)).unwrap();
- let cylindrical: Vec<_> = (0..cup.face_count())
- .filter_map(|i| {
- let crate::FaceSurface::Cylinder { radius, .. } = crate::face_surface(&cup, i)? else { return None };
- let z: Vec<f64> = face_points(&cup, i).iter().map(|p| p.z).collect();
- let lo = z.iter().copied().fold(f64::INFINITY, f64::min);
- let hi = z.iter().copied().fold(f64::NEG_INFINITY, f64::max);
- Some((i, radius, lo, hi))
- })
- .collect();
- eprintln!("roundedShell width{width} thickness{thickness} raw{} cylindrical roles{cylindrical:?}", cup.face_count());
- let mut corner_joins = 0;
- for (edge, adjacent) in raw.edge_faces.iter().enumerate() {
- let [a, b] = adjacent.as_slice() else { continue };
- if raw.edges[edge].iter().all(|p| (p.z - thickness).abs() < 1e-8)
- && cylindrical.iter().any(|(i, r, _, _)| *i == *a as usize && (*r - 6.).abs() < 1e-6)
- && cylindrical.iter().any(|(i, r, _, _)| *i == *b as usize && (*r - 6.).abs() < 1e-6)
- {
- corner_joins += 1;
- assert!(display.seams[edge], "existing cylindrical corner joins must not draw a floor-height arc");
- }
- }
- assert_eq!(corner_joins, 0, "no physical lower corner strip remains");
- assert_eq!(cup.face_count(), 19);
- let outer: Vec<_> = cylindrical.iter().filter(|(_, r, _, _)| (*r - 6.).abs() < 1e-6).collect();
- assert_eq!(outer.len(), 4);
- assert!(outer.iter().all(|(i, _, _, _)| cup.face_was_coalesced(*i) && moved.face_was_coalesced(*i)));
- assert!(outer.iter().all(|(_, _, lo, hi)| lo.abs() < 1e-9 && (*hi - 30.).abs() < 1e-9));
-
for axis in 0..2 {
for high in [false, true] {
let matching: Vec<_> = sources.iter().enumerate().filter_map(|(i,s)| matches!(s,Some(FaceSource::ShellPlanar {inner:false,source,..}) if matches!(source.as_ref(),FaceSource::Box {axis:a,high:h,..} if *a==axis && *h==high)).then_some(i)).collect();
@@ -658,18 +629,17 @@ mod tests {
assert!(display.contains(Vec3::new(width / 2. - thickness / 2., 0., thickness / 2.)));
assert!(display.contains(Vec3::new(width / 2. - thickness / 2., 0., 15.)));
assert!(!display.contains(Vec3::new(0., 0., 15.)));
- // Different meshers (the display plan and the reference), both within 0.01 mm.
- let (rv, dv) = (raw.measure().volume, display.measure().volume);
- assert!((rv - dv).abs() <= raw.measure().area * 0.01, "display volume {dv} vs {rv}");
+ // The rounded box less its inside, the box moved in by the wall with its corners'
+ // rounds at 6 − t, open at the top: exact (SMLib's volume, to 1e-8; the rounds
+ // are its surfaces).
let expected = (width * 60. - (4. - std::f64::consts::PI) * 36.) * 30.
- ((width - 2. * thickness) * (60. - 2. * thickness) - (4. - std::f64::consts::PI) * (6. - thickness).powi(2)) * (30. - thickness);
- assert!((cup.mesh(&crate::MeshTolerance::chord(1e-4)).unwrap().measure().volume - expected).abs() < 0.2);
+ let v = crate::measure(&cup).unwrap().volume;
+ assert!((v - expected).abs() < 1e-6 * expected, "{v} vs {expected}");
let imported = crate::step_import(&crate::step_export(&[&cup], "regularized rounded shell").unwrap()).unwrap();
assert!(imported.warnings.is_empty());
let copy = &imported.bodies[0].body;
- assert_eq!(copy.face_count(), 19);
- // STEP may additionally coalesce cylindrical corner patches. The
- // certified planar wall topology itself must survive unchanged.
+ // The certified planar walls come back from STEP as one full-height face each.
for (axis, coordinate) in [(Vec3::X, width / 2.), (-Vec3::X, width / 2.), (Vec3::Y, 30.), (-Vec3::Y, 30.)] {
let walls: Vec<usize> = actual_planes(copy)
.unwrap()
diff --git a/crates/kernel/src/step_in/tests.rs b/crates/kernel/src/step_in/tests.rs
index fa2e2b11de..ba854a77fa 100644
--- a/crates/kernel/src/step_in/tests.rs
+++ b/crates/kernel/src/step_in/tests.rs
@@ -1388,10 +1388,10 @@ fn glued_halves_export_as_whole_faces() {
assert_eq!(imp.bodies[0].file_faces, 8);
}
-/// A slot plate with a round on its top outline (issue #612): the extruded arc ends and the
-/// rounds along the straight edges are lone B-spline sheets, written as cylinders bounded by
-/// their faces. The rounds' sheets reach far past their edges, and a reader bounding the part by
-/// its surfaces saw a 60 mm part as 130 mm long.
+/// A slot plate with a round on its top outline (issue #612): every surface the file writes is
+/// bounded by its face, whatever kind it is written as. The rounds' sheets once reached far past
+/// their edges, and a reader bounding the part by its surfaces saw a 60 mm part as 130 mm long.
+/// Read back, the part has its exact volume.
#[test]
fn rounded_slot_exports_cylinders() {
use solidcraft_geom::Seg2;
@@ -1408,10 +1408,7 @@ fn rounded_slot_exports_cylinders() {
let edges = [Vec3::new(30.0, -10.0, 4.0), Vec3::new(30.0, 10.0, 4.0), Vec3::new(0.0, 0.0, 4.0), Vec3::new(60.0, 0.0, 4.0)];
let b = fillet(&plate, &edges, 1.0).unwrap();
let text = step_export(&[&b], "t").unwrap();
- assert!(!text.contains("B_SPLINE_SURFACE"), "{text}");
- assert_eq!(text.matches("CYLINDRICAL_SURFACE(").count(), 4);
- assert_eq!(text.matches("TOROIDAL_SURFACE(").count(), 2);
- // No point of the file (surface placements included) lies outside the part.
+ // No point of the file (control points and surface placements included) lies outside the part.
let imp = round_trip(&b);
for line in text.lines().filter(|l| l.contains("CARTESIAN_POINT")) {
let Some(xyz) = line.split("((").nth(1).and_then(|r| r.split("))").next()) else { continue };
@@ -1427,10 +1424,9 @@ fn rounded_slot_exports_cylinders() {
}
/// A slot plate rounded along its top and bottom outlines, with a ramp cut across one end
-/// (issue #1198). The other end's half turn is still written as a cylinder (its corners alone
-/// point both ways: the seam comes from points along its edges). The faces the ramp crosses keep
-/// their B-spline sheets, cut down to the faces: the rounds' sheets reached 35 mm past both ends,
-/// and readers bounding the part by its surfaces saw a 60 mm part as 130 mm long.
+/// (issue #1198): every surface the file writes is cut down to its face, so nothing reaches past
+/// the part's ends (the rounds' sheets once reached 35 mm past both, and readers bounding the part
+/// by its surfaces saw a 60 mm part as 130 mm long), and the part reads back whole.
#[test]
fn ramp_cut_slot_exports_sheets_bounded_by_their_faces() {
use solidcraft_geom::Seg2;
@@ -1453,10 +1449,6 @@ fn ramp_cut_slot_exports_sheets_bounded_by_their_faces() {
let tool = extrude(&side, &[ramp], -12.0, 0.0).unwrap().pop().unwrap();
let b = boolean(&rounded, &tool, BoolOp::Cut).unwrap().unwrap();
let text = step_export(&[&b], "t").unwrap();
- // Both ends' walls and the three rounds along the sides it leaves whole; the two the ramp
- // crosses stay sheets.
- assert_eq!(text.matches("CYLINDRICAL_SURFACE(").count(), 5, "{text}");
- assert_eq!(text.matches("B_SPLINE_SURFACE(").count(), 2, "{text}");
// No point of the file (control points and placements included) lies past the part's ends.
for line in text.lines().filter(|l| l.contains("CARTESIAN_POINT")) {
let Some(xyz) = line.split("((").nth(1).and_then(|r| r.split("))").next()) else { continue };