Skip to content

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

Wave 403/509: Kernel tests: a circle is one closed arc, a round wall one face (and 1 more)

jyviko20dde1a67a05
diff --git a/crates/kernel/src/issue_1038_tests.rs b/crates/kernel/src/issue_1038_tests.rs
index b8d22f48dd..5f944df39f 100644
--- a/crates/kernel/src/issue_1038_tests.rs
+++ b/crates/kernel/src/issue_1038_tests.rs
@@ -34,9 +34,9 @@ fn intersection_curves(body: &Body) -> Vec<Curve> {
fn a_small_hole_in_a_large_spline_face_cuts_cleanly() {
let (block, cut) = holed();
assert!(cut.validity().is_empty(), "{:?}", cut.validity());
- // The hole adds its wall (two half cylinders) and splits nothing: the bottom and the wave
- // each gain a loop.
- assert_eq!(cut.face_count(), block.face_count() + 2);
+ // The hole adds its wall (one cylinder, closed along its seam) and splits nothing: the
+ // bottom and the wave each gain a loop.
+ assert_eq!(cut.face_count(), block.face_count() + 1);
let curves = intersection_curves(&cut);
assert!(!curves.is_empty(), "the hole's rim on the wave is an intersection edge");
// The wave is within a few tenths of a millimetre of flat across a Ø1 hole, so the hole
diff --git a/crates/kernel/src/mesh_convert.rs b/crates/kernel/src/mesh_convert.rs
index e5cf84141e..2ddbed5392 100644
--- a/crates/kernel/src/mesh_convert.rs
+++ b/crates/kernel/src/mesh_convert.rs
@@ -478,7 +478,8 @@ mod tests {
})
.collect();
assert_eq!(fit_loop(&points, 0.001, 1e-6).segs.len(), 16);
- assert_eq!(fit_loop(&points, 0.2, 1e-6).segs.len(), 2);
+ // A circle is one closed arc.
+ assert_eq!(fit_loop(&points, 0.2, 1e-6).segs.len(), 1);
}
#[test]
@@ -549,7 +550,8 @@ mod tests {
.collect();
let inner = Loop2::polygon(&points);
let fitted = fit_loop(&points, 0.05, 1e-6);
- assert_eq!(fitted.segs.len(), 2);
+ // Fitted as a circle: one closed arc.
+ assert_eq!(fitted.segs.len(), 1);
assert!(certify_nesting(&[outer.clone(), inner], &[outer, fitted], 1e-6, 3.).is_err());
}
diff --git a/crates/kernel/src/native/smlib_import_tests.rs b/crates/kernel/src/native/smlib_import_tests.rs
index d1f558543b..c71e1732dd 100644
--- a/crates/kernel/src/native/smlib_import_tests.rs
+++ b/crates/kernel/src/native/smlib_import_tests.rs
@@ -62,13 +62,15 @@ fn smlib_results_go_back_into_smlib() {
}
/// Extruded circles go into SMLib as its cylinder, so a boolean with one intersects a plane with
-/// a cylinder, not with a spline.
+/// a cylinder, not with a spline. The record holds the wall as the cylinder it is (one face,
+/// closed along its seam).
#[test]
fn extruded_circles_go_as_cylinders() {
use solidcraft_smlib::flat;
let body = cylinder(v(0., 50., -60.), v(0., 0., 1.), 40.0, 120.0).unwrap();
let record = super::brep_record(&body).unwrap();
- assert!(record.faces.iter().any(|f| matches!(f.surface, solidcraft_geom::brep::Surface::RationalSpline { .. })));
+ let walls = record.faces.iter().filter(|f| matches!(f.surface, solidcraft_geom::brep::Surface::Cylinder { .. })).count();
+ assert_eq!((walls, record.faces.len()), (1, 3));
let arrays = solidcraft_smlib::import::arrays(&record, true).unwrap();
let kinds = &arrays.ints[&flat::FACE_SURFACE_TYPE];
assert!(kinds.iter().all(|k| *k == i64::from(flat::SURFACE_PLANE) || *k == i64::from(flat::SURFACE_CYLINDER)), "{kinds:?}");
diff --git a/crates/kernel/src/sew.rs b/crates/kernel/src/sew.rs
index 8ebad8631e..1be782c750 100644
--- a/crates/kernel/src/sew.rs
+++ b/crates/kernel/src/sew.rs
@@ -493,13 +493,19 @@ fn folded_pcurve(label: &b::Surface, form: Option<&b::Surface>, curve: &b::Curve
}
}
breaks.push((t1, *angles.last()?));
- // A crossing within rounding of another break (or of an end) is that break.
+ let size = points.iter().fold(1.0f64, |m, h| [h[0], h[1], h[2]].iter().fold(m, |m, c| m.max((c / h[3]).abs())));
+ // A crossing within rounding of another break (or of an end), or nearer to it on the edge
+ // than the pcurve lifts onto the edge (`FOLD_TOL`), is that break; the end never goes into
+ // the start. An edge starting 3e-12 of its parameter short of a quarter had pieces 1.6e-12
+ // long, whose knots SMLib took as one, past a multiplicity of the degree + 1.
let close = 1e-12 * (t1 - t0).abs().max(1.0);
+ let lifts = |a: f64, b: f64| matches!((edge.point(a), edge.point(b)), (Some(p), Some(q)) if p.dist(q) <= FOLD_TOL * size);
let mut kept: Vec<(f64, f64)> = Vec::with_capacity(breaks.len());
for (i, b) in breaks.iter().enumerate() {
let is_end = i + 1 == breaks.len();
+ let both_ends = is_end && kept.len() == 1;
match kept.last_mut() {
- Some(last) if (b.0 - last.0).abs() <= close => {
+ Some(last) if (b.0 - last.0).abs() <= close || (!both_ends && lifts(last.0, b.0)) => {
if is_end {
*last = *b;
}
@@ -535,7 +541,6 @@ fn folded_pcurve(label: &b::Surface, form: Option<&b::Surface>, curve: &b::Curve
};
Some([0, 1, 2, 3].map(|k| [u[k], h[k]]))
};
- let size = points.iter().fold(1.0f64, |m, h| [h[0], h[1], h[2]].iter().fold(m, |m, c| m.max((c / h[3]).abs())));
let mut pieces = 1usize;
while pieces * breaks.len() <= FOLD_PIECES {
let mut knots2 = vec![t0; 8];
diff --git a/crates/kernel/src/tests.rs b/crates/kernel/src/tests.rs
index d3387b1b39..41b32ae2b6 100644
--- a/crates/kernel/src/tests.rs
+++ b/crates/kernel/src/tests.rs
@@ -2327,7 +2327,11 @@ fn tight_bend_counts() {
];
let b = sweep(&Plane::XY, &region, &path).unwrap();
let m = measure(&b).unwrap().merged;
- assert_eq!((m.faces, m.edges, m.vertices), (5, 6, 4), "{m:?}");
+ // Two caps and a face per path segment. The circle starts (one closed arc) at (5, 0), which
+ // runs up the first straight to (5, 0, 20) on the bend's axis: the turn leaves it there, so
+ // the bend has no seam, and the second straight's seam runs on from it to (25, 0, 20). Edges:
+ // the four circles and the two straights' seams; vertices: the seams' ends.
+ assert_eq!((m.faces, m.edges, m.vertices), (5, 6, 3), "{m:?}");
}
#[test]
@@ -2752,3 +2756,4 @@ fn a_box_cut_round_a_small_hole_of_a_long_plate_keeps_the_hole() {
}
}
}
+