SolidCraft: an open-source parametric 3D CAD application built in pure Rust. Desktop, web, and automation through MCP.
Wave 331/509: Tests: a face's flat area may differ from its surface's by the sails its boundary chords bridge (and 3 more)
jyviko7067258444d2
diff --git a/crates/kernel/src/splitface.rs b/crates/kernel/src/splitface.rsindex f39dc005f7..219c813a2c 100644--- a/crates/kernel/src/splitface.rs+++ b/crates/kernel/src/splitface.rs@@ -632,9 +632,14 @@ mod tests {assert!(n > c.face_count());// The pieces stay apart; the cylinder's incidental seam pieces still join as before.assert_eq!(round_trip_faces(&cs), round_trip_faces(&c) + 1);- // Without the record the export joins the top's pieces again (as it does for pieces a- // boolean left).- assert_eq!(round_trip_faces(&s.clone().with_split_lines(None)), 6);+ // Without the record (no operation drops it: only Split Face's own removal, which+ // rebuilds without the split) the pieces are incidental: the file has what the body+ // has, and the next operation joins them as it joins any pieces on one surface (SMLib's+ // booleans at their end, the local modifiers through the heal).+ let unrecorded = s.clone().with_split_lines(None);+ assert_eq!(round_trip_faces(&unrecorded), unrecorded.face_count());+ let joined = crate::boolean_record::healed(&unrecorded).unwrap();+ assert_eq!((joined.face_count(), round_trip_faces(&joined)), (6, 6));// A split after a split keeps both lines.let pl2 = Plane::new(Vec3::new(0.0, 12.0, 0.0), Vec3::X, Vec3::Z).unwrap();let tops: Vec<usize> =@@ -656,12 +661,13 @@ mod tests {let m = measure(&s).unwrap();// (Measured on meshes: curved faces agree to about 1e-4.)assert!(rel(m.volume, m0.volume) < 2e-4 && rel(m.area, m0.area) < 2e-4, "{} {} {} {}", m.volume, m0.volume, m.area, m0.area);- // The half side face splits at z = 5: a quarter and three quarters of its area.+ // The side (one face round the whole turn) splits at z = 5: a quarter and three quarters+ // of its area.let mut sides: Vec<f64> = s.faces(0.01).unwrap().iter().filter(|f| f.plane_normal.is_none()).map(|f| f.area).collect();sides.sort_by(f64::total_cmp);- let half = std::f64::consts::PI * 10.0 * 20.0;- assert_eq!(sides.len(), 3);- assert!(rel(sides[0], half / 4.0) < 1e-3 && rel(sides[1], half * 0.75) < 1e-3 && rel(sides[2], half) < 1e-3, "{sides:?}");+ let whole = std::f64::consts::TAU * 10.0 * 20.0;+ assert_eq!(sides.len(), 2);+ assert!(rel(sides[0], whole / 4.0) < 1e-3 && rel(sides[1], whole * 0.75) < 1e-3, "{sides:?}");assert!(s.face_count() > c.face_count());// A smaller cylinder's side splits the top in a ring and a disc.let pin = cylinder(Vec3::ZERO, Vec3::Z, 4.0, 40.0).unwrap();diff --git a/crates/kernel/src/tess/mod.rs b/crates/kernel/src/tess/mod.rsindex d956b2adb2..3695369c0d 100644--- a/crates/kernel/src/tess/mod.rs+++ b/crates/kernel/src/tess/mod.rs@@ -356,7 +356,14 @@ fn boundary(}let n = uvs.len();let pole = |q: &[f64; 2]| (0..2).any(|d| pole_along(*q, d));- if let Some(k) = (0..n).find(|&k| uvs.get(k).is_some_and(|q| !pole(q)) && uvs.get((k + n - 1) % n).is_some_and(|q| !pole(q))) {+ let off = |k: usize| uvs.get(k).is_some_and(|q| !pole(q));+ // A node off the poles whose predecessor is off them too; failing that (a whole sphere's+ // seam in two segments: pole, equator, pole), one whose predecessor is a pole on the same+ // edge, so the step into it is the edge's own and the step into that pole, from the+ // edge before, is in the loop where it is unwrapped like any other.+ let same_edge = |k: usize| segs.get((k + n - 1) % n).zip(segs.get(k)).is_some_and(|(a, b)| a.0 == b.0);+ let start = (0..n).find(|&k| off(k) && off((k + n - 1) % n)).or_else(|| (0..n).find(|&k| off(k) && same_edge(k)));+ if let Some(k) = start {uvs.rotate_left(k);pts.rotate_left(k);segs.rotate_left(k);diff --git a/crates/kernel/src/tess/tests.rs b/crates/kernel/src/tess/tests.rsindex 07f35669e5..9d60e2bcfe 100644--- a/crates/kernel/src/tess/tests.rs+++ b/crates/kernel/src/tess/tests.rs@@ -470,11 +470,16 @@ fn iso_groove(p: f64, d: f64, wall: f64, internal: bool) -> [(f64, f64); 4] {}/// Each face's triangles, in 3D, cover as much area as its surface over the triangles' own-/// parameters, within 5% (a coarse tolerance on a small curved face, a few percent): nothing-/// zigzags across the face, which multiplies its area. The surface's area by the midpoint rule-/// on sixteen pieces of each parameter triangle.+/// parameters, within 5% (a coarse tolerance on a small curved face, a few percent) and the+/// sails along its boundary: nothing zigzags across the face, which multiplies its area. The+/// surface's area by the midpoint rule on sixteen pieces of each parameter triangle. A face's+/// boundary sides are its edges' chords, each up to the chord tolerance `tol` off its edge, and+/// between a chord of length L and its edge lies a sail of surface of at most 2/3 L tol (a+/// parabolic segment) that flat triangles bridge rather than follow: a face no wider than its+/// chords sag (the thread's crest band where it runs out through the block's end, 0.06 mm sag+/// at 0.1) takes up to that much more or less area however it is triangulated inside.#[cfg(feature = "smlib")]-fn assert_faces_lie_flat(name: &str, plan: &solidcraft_geom::tess::TessPlan) {+fn assert_faces_lie_flat(name: &str, plan: &solidcraft_geom::tess::TessPlan, tol: f64) {for fp in &plan.faces {let Some(s) = fp.surface.and_then(|i| plan.surfaces.get(i as usize)) else { continue };let at = |i: u32| {@@ -504,7 +509,21 @@ fn assert_faces_lie_flat(name: &str, plan: &solidcraft_geom::tess::TessPlan) {}}}- assert!((flat - surface).abs() <= 0.05 * surface, "{name}: face {} has {flat} mm² of triangles over {surface} mm² of surface", fp.face);+ // The boundary: the sides of exactly one triangle.+ let mut sides: HashMap<(u32, u32), u32> = HashMap::new();+ for t in &fp.triangles {+ for k in 0..3 {+ let (a, b) = (t[k], t[(k + 1) % 3]);+ *sides.entry((a.min(b), a.max(b))).or_default() += 1;+ }+ }+ let boundary: f64 = sides.iter().filter(|(_, n)| **n == 1).map(|((a, b), _)| at(*a).dist(at(*b))).sum();+ let sails = 2.0 / 3.0 * boundary * tol;+ assert!(+ (flat - surface).abs() <= 0.05 * surface + sails,+ "{name}: face {} has {flat} mm² of triangles over {surface} mm² of surface (its boundary's sails {sails})",+ fp.face+ );}}@@ -534,7 +553,7 @@ fn a_modelled_thread_in_a_bore_meshes_inside_its_block() {let m = closed_display_mesh("thread in a bore", &body, tol);let chord = max_chord(&body, &m);assert!(chord <= tol * 1.05, "thread in a bore at {tol}: chord {chord}");- assert_faces_lie_flat("thread in a bore", &ok(body.mesh_plan(&crate::MeshTolerance { chord: tol, angle: Some(crate::DISPLAY_ANGLE) })));+ assert_faces_lie_flat("thread in a bore", &ok(body.mesh_plan(&crate::MeshTolerance { chord: tol, angle: Some(crate::DISPLAY_ANGLE) })), tol);}}@@ -560,7 +579,7 @@ fn a_thread_root_band_running_out_through_an_end_face_lies_on_its_surface() {let chord = max_chord(&body, &m);assert!(chord <= tol * 1.05, "thread run out at {tol}: chord {chord}");assert!(m.triangles.len() < 60_000, "thread run out at {tol}: {} triangles", m.triangles.len());- assert_faces_lie_flat("thread run out", &ok(body.mesh_plan(&crate::MeshTolerance { chord: tol, angle: Some(crate::DISPLAY_ANGLE) })));+ assert_faces_lie_flat("thread run out", &ok(body.mesh_plan(&crate::MeshTolerance { chord: tol, angle: Some(crate::DISPLAY_ANGLE) })), tol);}}@@ -869,3 +888,23 @@ fn a_loop_corner_within_its_tolerance_of_a_pole_is_a_pole() {let Err(e) = plan(&pyramid_past_a_near_pole(1e-8), 0.01, None) else { panic!("no pole without the tolerance") };assert!(e.to_string().contains("winds around the surface"), "{e}");}++/// A whole sphere as one face (its seam the only edge) in a body many times its size: at the+/// body's chord the seam gets two segments, pole to equator to pole, and every node off the poles+/// follows a pole. The loop used to start at a pole and close across the other, which unwrapped+/// to a whole turn: "boundary winds around the surface", and the sphere was left out (a sealed+/// void r 2 in a 60 mm block). It meshes closed.+#[test]+fn a_small_sphere_void_in_a_large_block_meshes() {+ let block = ok(crate::box_solid(Vec3::new(-30.0, -30.0, 0.0), Vec3::new(30.0, 30.0, 60.0)));+ let ball = ok(crate::sphere(Vec3::new(0.0, 0.0, 30.0), 2.0));+ let hollow = cut(&block, &ball);+ let tol = hollow.size() * 0.01;+ let (m, stats) = match plan(&record(&hollow), tol, None) {+ Ok((p, stats)) => (p.evaluate(), stats),+ Err(e) => panic!("plan: {e}"),+ };+ assert!(stats.unmeshed.is_empty(), "{:?}", stats.unmeshed.iter().map(|u| &u.reason).collect::<Vec<_>>());+ assert_watertight(&m, "block with a sphere void");+ assert_eq!(hollow.unmeshed_faces(), 0);+}