From 0648ab54b960c5cab32811ecb79fa77da3f37b23 Mon Sep 17 00:00:00 2001 From: dotCooCoo Date: Fri, 4 Dec 2026 18:22:50 -0800 Subject: [PATCH] DXF import: warn when a closed spline edge starts at the curve's crossing An edge whose vertex lies where a closed spline crosses itself may bound only one lobe of the curve. The import still draws the whole curve, now with the warning for edges on self-crossing curves. --- crates/cad/src/import/acis.rs | 60 ++++++++++++++------------- crates/cad/src/import/tests_region.rs | 18 ++++++++ 2 files changed, 49 insertions(+), 29 deletions(-) diff --git a/crates/cad/src/import/acis.rs b/crates/cad/src/import/acis.rs index af12f517be..581e168625 100644 --- a/crates/cad/src/import/acis.rs +++ b/crates/cad/src/import/acis.rs @@ -680,39 +680,41 @@ fn spline(c: &Rec, from: P3, to: P3, closed: bool, back: bool, xf: &Xf) -> Optio let segs: Vec = full.segments().collect(); let (first, last) = (segs.first()?.start(), segs.last()?.end()); let ring = first.distance(last) <= 1e-9 * (1.0 + first.to_vec2().hypot()); + let mut lengths = vec![0.0]; + for s in &segs { + lengths.push(lengths.last()? + s.arclen(1e-9)); + } + let total = *lengths.last()?; + let at = |p: P3| { + let p = flat(xf.point(p)); + let near: Vec<_> = segs.iter().enumerate().map(|(i, s)| (s.nearest(p, 1e-9), i)).collect(); + let (n, i) = near.iter().min_by(|a, b| a.0.distance_sq.total_cmp(&b.0.distance_sq))?; + let along = |i: usize, t: f64| lengths[i] + segs[i].subsegment(0.0..t).arclen(1e-9); + let tolerance = 1e-6 * (1.0 + p.x.hypot(p.y)); + let mut locations: Vec<_> = + near.iter().filter(|(n, i)| !endpoints_apart(p, segs[*i].eval(n.t))).map(|(n, i)| (*i as f64 + n.t, along(*i, n.t))).collect(); + for (i, s) in segs.iter().enumerate() { + if let PathSeg::Cubic(cubic) = s + && let Some((a, b)) = cubic_crossing(*cubic) + && !endpoints_apart(p, cubic.eval(a)) + { + locations.extend([a, b].map(|t| (i as f64 + t, along(i, t)))); + } + } + // Nearby ends at a segment joint, or at a closed curve's seam, are one location. + locations.sort_by(|a, b| a.0.total_cmp(&b.0)); + locations.dedup_by(|a, b| (a.1 - b.1).abs() <= 4.0 * tolerance); + if ring && locations.len() > 1 && (total - (locations.last()?.1 - locations.first()?.1)).abs() <= 4.0 * tolerance { + locations.pop(); + } + Some((*i as f64 + n.t, locations)) + }; let mut ambiguous = false; let part = if closed && ring { + let (_, locations) = at(from)?; + ambiguous = locations.len() > 1; full } else { - let mut lengths = vec![0.0]; - for s in &segs { - lengths.push(lengths.last()? + s.arclen(1e-9)); - } - let total = *lengths.last()?; - let at = |p: P3| { - let p = flat(xf.point(p)); - let near: Vec<_> = segs.iter().enumerate().map(|(i, s)| (s.nearest(p, 1e-9), i)).collect(); - let (n, i) = near.iter().min_by(|a, b| a.0.distance_sq.total_cmp(&b.0.distance_sq))?; - let along = |i: usize, t: f64| lengths[i] + segs[i].subsegment(0.0..t).arclen(1e-9); - let tolerance = 1e-6 * (1.0 + p.x.hypot(p.y)); - let mut locations: Vec<_> = - near.iter().filter(|(n, i)| !endpoints_apart(p, segs[*i].eval(n.t))).map(|(n, i)| (*i as f64 + n.t, along(*i, n.t))).collect(); - for (i, s) in segs.iter().enumerate() { - if let PathSeg::Cubic(cubic) = s - && let Some((a, b)) = cubic_crossing(*cubic) - && !endpoints_apart(p, cubic.eval(a)) - { - locations.extend([a, b].map(|t| (i as f64 + t, along(i, t)))); - } - } - // Nearby ends at a segment joint, or at a closed curve's seam, are one location. - locations.sort_by(|a, b| a.0.total_cmp(&b.0)); - locations.dedup_by(|a, b| (a.1 - b.1).abs() <= 4.0 * tolerance); - if ring && locations.len() > 1 && (total - (locations.last()?.1 - locations.first()?.1)).abs() <= 4.0 * tolerance { - locations.pop(); - } - Some((*i as f64 + n.t, locations)) - }; let bounds = if closed { let (_, locations) = at(from)?; if let [a, b] = locations.as_slice() { diff --git a/crates/cad/src/import/tests_region.rs b/crates/cad/src/import/tests_region.rs index 0925729add..18167d1fe9 100644 --- a/crates/cad/src/import/tests_region.rs +++ b/crates/cad/src/import/tests_region.rs @@ -441,6 +441,24 @@ fn a_closed_spline_edge_keeps_the_loop_at_one_cubic_spans_crossing() { } } +#[test] +fn a_closed_spline_ring_warns_only_when_its_vertex_is_at_the_crossing() { + // The first and third spans cross (0, 0) halfway along each; the seam is (-100, -100). + let curve = "intcurve-curve $-1 -1 $-1 forward { exactcur 0 nubs 1 closed 5 0 2 1 1 2 1 3 1 4 2 -100 -100 0 100 100 0 -100 100 0 100 -100 0 -100 -100 0 } I I"; + for a in [[0.0, 0.0], [-100.0, -100.0]] { + for back in [false, true] { + let edge = E { a, b: a, curve: Curve::Raw(curve.into()), flip: false, against: back }; + let r = import(&drawing("AC1015", ®ion_text(&sat(&[vec![edge]], None), false), &[]), &points()).unwrap(); + if a == [0.0, 0.0] { + assert_eq!(r.warnings, ["region edges on curves that cross themselves may come in along the wrong part of the curve"]); + } else { + assert!(r.warnings.is_empty(), "{:?}", r.warnings); + } + check(&r.document, &[[0.0, 50.0], [0.0, -50.0]], &[[75.0, 0.0], [-75.0, 0.0]]); + } + } +} + #[test] fn spline_joints_and_a_closed_curves_seam_do_not_warn() { for (curve, a) in [