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

* claim: #8639

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

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

Two free-form curves that lie on each other along a stretch (a DXF that
carries an outline twice, with the same straight sides and different
corners) are cut into sampled pieces whose ends the graph joins into one
node within 1e-4 mm, but the pieces keep their own ends, a few millionths
apart. Only analytic edges were closed at those joints, so the sliver
loops between the two curves stayed open by up to 1e-5 mm, more than the
1e-6 a solid needs. The region around them took those loops as holes:
extruding it failed with "profile loop is not closed" and it drew no
fill when picked, so it looked unselectable.

Close a line's joint in each finished loop: line to line at their middle,
a line onto any other segment's end. On the reporter's DXF the band
between the outer two rings now extrudes (110.5 mm2 by 2 mm = 220.9 mm3),
and the zero-area slivers no longer form profiles (221 -> 216).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

---------

Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
Logan Night
2026-10-29 17:11:01 +00:00
committed by GitHub
parent cc047a29c1
commit d0b357d532
2 changed files with 73 additions and 1 deletions
+33 -1
View File
@@ -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()));
+40
View File
@@ -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));
}
}
}