Skip to content

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

Sketch: a spline ending on another spline between its fit points closes a selectable profile (#12324) (#12774)

PaddleStrokebe648ef74271
diff --git a/crates/sketch/src/profiles.rs b/crates/sketch/src/profiles.rs
index 1a36c66332..dc77d44c66 100644
--- a/crates/sketch/src/profiles.rs
+++ b/crates/sketch/src/profiles.rs
@@ -1092,6 +1092,18 @@ fn island_loops(sk: &Sketch, curves: &[usize], max_edges: usize) -> Island {
let mut exact: std::collections::HashMap<usize, Vec<Seg2>> = std::collections::HashMap::new();
let mut whole_graph_complete = true;
let mut expected = std::collections::HashMap::<usize, Vec<usize>>::new();
+ // End points of the profile curves, by x: free-form polylines pass through those lying on
+ // them ([`polyline_through`]).
+ let mut curve_ends: Vec<Vec2> = curves
+ .iter()
+ .filter_map(|ci| sk.curves.get(*ci))
+ .filter(|c| !c.construction && !c.centerline && !c.infinite)
+ .filter_map(|c| c.kind.ends())
+ .flat_map(|(a, b)| [sk.point(a), sk.point(b)])
+ .flatten()
+ .filter(|p| p.is_finite())
+ .collect();
+ curve_ends.sort_by(|a, b| a.x.total_cmp(&b.x));
for &ci in curves {
let Some(c) = sk.curves.get(ci) else { continue };
if c.construction || c.centerline || c.infinite {
@@ -1124,7 +1136,7 @@ fn island_loops(sk: &Sketch, curves: &[usize], max_edges: usize) -> Island {
Some(ex) => {
let mut spans = Vec::with_capacity(ex.len());
for (k, seg) in ex.iter().enumerate() {
- let poly = seg.polyline(1e-3);
+ let poly = polyline_through(seg, &curve_ends, tol);
let n = poly.len().saturating_sub(1);
spans.push(n);
for (j, w) in poly.windows(2).enumerate() {
@@ -1607,6 +1619,68 @@ fn island_loops(sk: &Sketch, curves: &[usize], max_edges: usize) -> Island {
Island { loops, incomplete }
}
+/// Chord tolerance of the polylines free-form curves take part in the profile graph as.
+const SAMPLE_TOL: f64 = 1e-3;
+
+/// The polyline of a free-form segment, through every point of `ends` (sorted by x) lying on
+/// the segment within `tol`, away from its own ends. The chords stray from the curve by up to
+/// [`SAMPLE_TOL`], far more than `tol`: a curve ending on the segment between two samples, on
+/// the side away from the chord, ended short of the polyline and never met it, so the regions
+/// it closes were not found (#12324). Each such end is a sample of the polyline instead.
+fn polyline_through(seg: &Seg2, ends: &[Vec2], tol: f64) -> Vec<Vec2> {
+ let poly = seg.polyline(SAMPLE_TOL);
+ let bounds = poly
+ .iter()
+ .fold(Aabb2::EMPTY, |mut b, p| {
+ b.add(*p);
+ b
+ })
+ .inflated(SAMPLE_TOL + tol);
+ if !(bounds.min.is_finite() && bounds.max.is_finite()) {
+ return poly;
+ }
+ let from = ends.partition_point(|p| p.x < bounds.min.x);
+ let mut on: Vec<(f64, Vec2)> = Vec::new();
+ for &e in ends.get(from..).unwrap_or(&[]) {
+ if e.x > bounds.max.x {
+ break;
+ }
+ if e.y < bounds.min.y || e.y > bounds.max.y {
+ continue;
+ }
+ let s = seg.closest_param(e);
+ let q = seg.point_at(s);
+ if (1e-9..1.0 - 1e-9).contains(&s) && q.dist(e) <= tol && q.dist(seg.start()) > tol && q.dist(seg.end()) > tol {
+ on.push((s, e));
+ }
+ }
+ if on.is_empty() {
+ return poly;
+ }
+ on.sort_by(|a, b| a.0.total_cmp(&b.0));
+ on.dedup_by(|a, b| a.1.dist(b.1) <= tol);
+ // Split the segment at each end in turn and sample the pieces; the end itself joins them,
+ // so the curve ending there meets the polyline at exactly its end point.
+ let mut out: Vec<Vec2> = Vec::new();
+ let mut rest = *seg;
+ for (s, e) in on {
+ let u = rest.closest_param(seg.point_at(s));
+ if !(1e-9..1.0 - 1e-9).contains(&u) {
+ continue;
+ }
+ let (left, right) = rest.split_at(u);
+ let piece = left.polyline(SAMPLE_TOL);
+ if let Some((_, init)) = piece.split_last() {
+ out.extend(init.iter().skip(usize::from(!out.is_empty())));
+ }
+ out.push(e);
+ rest = right;
+ }
+ let piece = rest.polyline(SAMPLE_TOL);
+ out.extend(piece.iter().skip(usize::from(!out.is_empty())));
+ out
+}
+
/// Nest the loops of every island into profiles: a loop inside a loop of another connected
/// component (from any island) becomes a hole of the innermost one containing it.
fn assemble(sk: &Sketch, islands: &[Arc<Island>]) -> Vec<Profile> {
@@ -2194,6 +2268,45 @@ mod tests {
assert!(profiles.iter().any(|p| p.hole_curves.iter().any(|h| *h == [disc.clone()])));
}
+ /// #12324: a fit spline ending on a closed fit spline between two of the samples its polyline
+ /// takes, and bulging out of it, closes a region with it. The chords run up to the sampling
+ /// tolerance inside the curve, so the bulge's ends, on the curve itself, ended short of them
+ /// and the region was not found.
+ #[test]
+ fn a_spline_ending_on_a_spline_between_its_samples_closes_a_region() {
+ let mut sk = Sketch::new();
+ let ring = [(20.0, 0.0), (10.0, 17.0), (-10.0, 17.0), (-20.0, 0.0), (-10.0, -17.0), (10.0, -17.0)];
+ let mut pts: Vec<usize> = ring.iter().map(|(x, y)| sk.add_point(Vec2::new(*x, *y), None).unwrap()).collect();
+ pts.push(pts[0]);
+ let outline = sk.add_curve(CurveKind::Spline { pts, control: false, degree: 3, periodic: true, nurbs: None }, None).unwrap();
+ let pieces = sk.exact_pieces(outline);
+ assert!(pieces.len() >= 2, "{pieces:?}");
+ // The point of a piece furthest out from the middle chord of its polyline.
+ let off_chord = |seg: &Seg2| {
+ let poly = seg.polyline(SAMPLE_TOL);
+ let k = (poly.len() / 2).saturating_sub(1);
+ let (p, q) = (poly[k], poly[k + 1]);
+ let e = seg.point_at(seg.closest_param((p + q) * 0.5));
+ let gap = (e - p).cross(q - p).abs() / p.dist(q);
+ assert!(gap > 1e-4, "the end lies {gap} off the polyline, as in the report");
+ e
+ };
+ let (start, end) = (off_chord(&pieces[0]), off_chord(&pieces[1]));
+ // Bulging out past the fit point the two pieces share.
+ let corner = pieces[0].end() * 1.15;
+ let bulge: Vec<usize> = [start, corner, end].iter().map(|p| sk.add_point(*p, None).unwrap()).collect();
+ let bulge = sk.add_curve(CurveKind::Spline { pts: bulge, control: false, degree: 3, periodic: false, nurbs: None }, None).unwrap();
+ islands_match_whole(&sk);
+ let profiles = find_profiles(&sk);
+ assert_eq!(profiles.len(), 2, "the inside and the bulge: {profiles:?}");
+ let (outline, bulge) = (sk.curves[outline].id.clone(), sk.curves[bulge].id.clone());
+ let lens = profiles.iter().find(|p| p.outer_curves.contains(&bulge)).expect("the bulge closes a region");
+ assert!(lens.outer_curves.contains(&outline), "{:?}", lens.outer_curves);
+ let inside = profiles.iter().find(|p| !p.outer_curves.contains(&bulge)).unwrap();
+ assert!(lens.area > 0.1 && lens.area < inside.area * 0.2, "{} of {}", lens.area, inside.area);
+ assert!(lens.region.outer.joints().all(|(a, b)| a.end().dist(b.start()) < 1e-6), "{:?}", lens.region.outer.segs);
+ }
+
/// #6896: past the edge limit, the old single search dropped profiles silently. The limit
/// now holds per island, so many small islands are all found, and an island too large says
/// so.