diff --git a/crates/pathops/src/addons/pathtools/crossings.rs b/crates/pathops/src/addons/pathtools/crossings.rs index bcc254aa82..90a0d266ea 100644 --- a/crates/pathops/src/addons/pathtools/crossings.rs +++ b/crates/pathops/src/addons/pathtools/crossings.rs @@ -48,6 +48,42 @@ fn chords_cross(a: &CubicBez, b: &CubicBez) -> Option<(f64, f64)> { (within(s) && within(u)).then(|| (s.clamp(0.0, 1.0), u.clamp(0.0, 1.0))) } +/// `(s, t)` moved to where `a` and `b` cross exactly, by Newton's method on a(s) = b(t), or as it +/// was when that doesn't settle within both segments, closer than it started, and within the +/// distance a piece bent by [`FLAT`] can shift a crossing at their angle there. +fn refined(a: &CubicBez, b: &CubicBez, (s0, t0): (f64, f64)) -> (f64, f64) { + let gap = |s: f64, t: f64| a.eval(s).distance(b.eval(t)); + let start = gap(s0, t0); + if start <= 1e-12 || !start.is_finite() { + return (s0, t0); + } + let (da, db) = (a.deriv(), b.deriv()); + let (mut s, mut t) = (s0, t0); + for _ in 0..8 { + let f = a.eval(s) - b.eval(t); + let (u, v) = (da.eval(s).to_vec2(), db.eval(t).to_vec2()); + // a(s + ds) - b(t + dt) ≈ f + u ds - v dt, which is zero for these steps. + let den = u.cross(v); + let (ds, dt) = (-f.cross(v) / den, u.cross(f) / den); + if !(ds.is_finite() && dt.is_finite()) { + return (s0, t0); + } + (s, t) = (s + ds, t + dt); + if !(-1e-12..=1.0 + 1e-12).contains(&s) || !(-1e-12..=1.0 + 1e-12).contains(&t) { + return (s0, t0); + } + if gap(s, t) <= 1e-12 { + break; + } + } + let (s, t) = (s.clamp(0.0, 1.0), t.clamp(0.0, 1.0)); + let (u, v) = (da.eval(s).to_vec2(), db.eval(t).to_vec2()); + let sin = u.cross(v).abs() / (u.hypot() * v.hypot()); + let reach = 4.0 * FLAT / sin + SAME; + let moved = a.eval(s).distance(a.eval(s0)).max(b.eval(t).distance(b.eval(t0))); + if gap(s, t) < start.min(1e-9) && moved <= reach { (s, t) } else { (s0, t0) } +} + /// Where segments `a` and `b` cross: (t on `a`, t on `b`) pairs, halving both while their control /// boxes meet until each piece is straight. `None` once `budget` steps are spent. fn segment_crossings(a: &CubicBez, b: &CubicBez, budget: &mut usize) -> Option> { @@ -80,6 +116,11 @@ fn segment_crossings(a: &CubicBez, b: &CubicBez, budget: &mut usize) -> Option = kurbo::PathSeg::Cubic(arc.subpaths[0].segment(0)) + .intersect_line(kurbo::Line::new(from, to)) + .iter() + .map(|x| from.lerp(to, x.line_t)) + .collect(); + assert_eq!(exact.len(), 2, "{gap}"); + let on = |p: &PathData, other: &PathData| -> Vec { + crossings_on(p, &[other]).unwrap().iter().map(|q| p.subpaths[0].segment(q.1).eval(q.2)).collect() + }; + let both = crossings(&[&arc, &across], false).unwrap(); + let found = [ + on(&arc, &across), + on(&across, &arc), + both[0].iter().map(|q| arc.subpaths[0].segment(q.1).eval(q.2)).collect(), + both[1].iter().map(|q| across.subpaths[0].segment(q.1).eval(q.2)).collect(), + ]; + for places in found { + assert_eq!(places.len(), 2, "{gap}: {places:?}"); + for x in &exact { + let off = places.iter().map(|p| p.distance(*x)).fold(f64::INFINITY, f64::min); + assert!(off < 1e-6, "{gap}: {off} pt from {x:?}"); + } + } + } +} + #[test] fn insert_at_keeps_the_shape_and_names_the_new_anchors() { let sp = line(&[(0.0, 0.0), (100.0, 0.0), (100.0, 100.0)], false);