Skip to content

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

Sketch profiles: close the joints near-coincident curves leave open (#8639) (#8922)

Logan Nightd0b357d532b4
diff --git a/crates/sketch/src/profiles.rs b/crates/sketch/src/profiles.rs
index db843c19c9..4d35ea7e91 100644
--- a/crates/sketch/src/profiles.rs
+++ b/crates/sketch/src/profiles.rs
@@ -428,6 +428,38 @@ fn close_graph_joints(mut segs: Vec<Seg2>, edge_nodes: &[(usize, usize)], edge_a
segs
}
+/// Close the joints a line leaves open: the graph joins ends within `tol` into one node, but a
+/// free-form curve's polyline pieces keep their own ends, so two near-coincident curves can leave
+/// a loop open by more than the closing tolerance. A line meeting a line moves both ends to their
+/// middle; a line meeting another segment moves its end onto that segment's.
+fn close_line_joints(mut segs: Vec<Seg2>, tol: f64) -> Vec<Seg2> {
+ let n = segs.len();
+ for i in 0..n {
+ let j = (i + 1) % n;
+ let (Some(a), Some(b)) = (segs.get(i).copied(), segs.get(j).copied()) else { continue };
+ let gap = a.end().dist(b.start());
+ if gap <= 1e-12 || gap > tol {
+ continue;
+ }
+ let (a, b) = match (a, b) {
+ (Seg2::Line { .. }, Seg2::Line { .. }) => {
+ let p = (a.end() + b.start()) * 0.5;
+ (with_end(a, p), set_start(b, p))
+ }
+ (Seg2::Line { .. }, _) => (with_end(a, b.start()), b),
+ (_, Seg2::Line { .. }) => (a, set_start(b, a.end())),
+ _ => continue,
+ };
+ if let Some(slot) = segs.get_mut(i) {
+ *slot = a;
+ }
+ if let Some(slot) = segs.get_mut(j) {
+ *slot = b;
+ }
+ }
+ segs
+}
+
fn merge_nearby_endpoints(sk: &Sketch, endpoints: &[(usize, usize)], groups: &mut [usize], max_edges: usize) {
let tol = ENDPOINT_JOIN_TOL;
let cell = |p: Vec2| ((p.x / tol).floor() as i64, (p.y / tol).floor() as i64);
@@ -1178,7 +1210,7 @@ fn find_profiles_bounded(sk: &Sketch, max_edges: usize) -> Vec<Profile> {
continue;
}
let segs = close_graph_joints(segs, &edge_nodes, &edge_analytic, ENDPOINT_JOIN_TOL);
- let lp = Loop2 { segs: restore_exact(segs, &ptags, &exact) };
+ let lp = Loop2 { segs: close_line_joints(restore_exact(segs, &ptags, &exact), ENDPOINT_JOIN_TOL) };
if lp.signed_area() > 1e-9 {
let c = edges.get(start / 2).map(|e| find(&mut comp, e.u)).unwrap_or(0);
loops.push((lp, ids, c, whole_graph_complete.then(|| complete_loop(&coverage_tags, &expected, sk)).flatten()));
diff --git a/crates/sketch/src/tests.rs b/crates/sketch/src/tests.rs
index 3bf233a733..d9347a9396 100644
--- a/crates/sketch/src/tests.rs
+++ b/crates/sketch/src/tests.rs
@@ -1398,3 +1398,43 @@ fn a_fillet_folded_through_flat_is_refused_like_a_fold() {
}
assert!(refused.is_some_and(|s| (7..=9).contains(&s)), "refused at {refused:?}");
}
+
+/// #8639: two rounded rectangles drawn as closed control-point splines with the same straight
+/// sides and different corners (as a DXF can carry an outline twice). Where the sides lie on each
+/// other the two curves' sampled pieces end a few millionths apart; every profile must still close
+/// within the 1e-6 a solid needs, or the region around them can't be extruded or shown.
+#[test]
+fn rounded_rectangles_sharing_their_sides_leave_every_profile_closed() {
+ // Control points of a rounded rectangle 17.6 by 17.6, corner `r`, sides split in thirds.
+ fn rounded(sk: &mut Sketch, r: f64) {
+ let (lo, hi) = (0.0, 17.6);
+ let k = r * 0.448;
+ let mut pts = Vec::new();
+ for (cx, cy, dx, dy) in [(hi, hi, 1.0, 1.0), (hi, lo, 1.0, -1.0), (lo, lo, -1.0, -1.0), (lo, hi, -1.0, 1.0)] {
+ // Into the corner along the side, round it, out along the next side (clockwise).
+ let (a, b) = if dx * dy > 0.0 { ((cx - dx * r, cy), (cx, cy - dy * r)) } else { ((cx, cy - dy * r), (cx - dx * r, cy)) };
+ let (ka, kb) = if dx * dy > 0.0 { ((cx - dx * k, cy), (cx, cy - dy * k)) } else { ((cx, cy - dy * k), (cx - dx * k, cy)) };
+ pts.extend([v(a.0, a.1), v(ka.0, ka.1), v(kb.0, kb.1), v(b.0, b.1)]);
+ // Two more points along the next side, so it is straight.
+ let (nx, ny) = if dx * dy > 0.0 { (cx, cy - dy * (hi - r)) } else { (cx - dx * (hi - r), cy) };
+ let t = |f: f64| v(b.0 + (nx - b.0) * f, b.1 + (ny - b.1) * f);
+ pts.extend([t(1.0 / 3.0), t(2.0 / 3.0)]);
+ }
+ let mut ids: Vec<usize> = pts.iter().map(|p| sk.add_point(*p, None).unwrap()).collect();
+ ids.push(ids[0]);
+ sk.add_curve(CurveKind::Spline { pts: ids, control: true, degree: 3, periodic: false }, None).unwrap();
+ }
+ let mut sk = Sketch::new();
+ rounded(&mut sk, 0.55);
+ rounded(&mut sk, 0.8);
+ sk.add_circle(v(8.8, 8.8), 10.0, None, None).unwrap();
+ let ps = find_profiles(&sk);
+ assert!(!ps.is_empty());
+ for p in &ps {
+ for lp in std::iter::once(&p.region.outer).chain(&p.region.holes) {
+ let n = lp.segs.len();
+ let gap = (0..n).map(|i| lp.segs[i].end().dist(lp.segs[(i + 1) % n].start())).fold(0.0, f64::max);
+ assert!(lp.is_closed(1e-6), "a loop of {:?} is open by {gap:.3e}", crate::distinct_curves(&p.outer_curves));
+ }
+ }
+}