diff --git a/crates/pathops/src/pathfinder.rs b/crates/pathops/src/pathfinder.rs index 6f99a3d917..1ec916e500 100644 --- a/crates/pathops/src/pathfinder.rs +++ b/crates/pathops/src/pathfinder.rs @@ -971,6 +971,48 @@ fn along(a: &[PathSeg], b: &[PathSeg], tol: f64) -> bool { covers(a, b) && covers(b, a) } +/// [`along`] with each piece checked at points no farther apart along its control polygon than +/// `tol`, at most 64 to a piece, so curves that meet at the middles and ends of their pieces but +/// part between them are not one. `a` and `b` run opposite ways. Each piece's samples include its +/// middle. Chains whose pieces pair up with control points within `tol` of each other are one +/// without sampling: each point of such a piece is within `tol` of the point of its pair at the same +/// parameter. +fn along_closely(a: &[PathSeg], b: &[PathSeg], tol: f64) -> bool { + let within = |p: Point, q: Point| p.distance(q) <= tol; + let paired = a.len() == b.len() + && a.iter().zip(b.iter().rev()).all(|(p, q)| match (p, q.reverse()) { + (PathSeg::Line(x), PathSeg::Line(y)) => within(x.p0, y.p0) && within(x.p1, y.p1), + (PathSeg::Quad(x), PathSeg::Quad(y)) => within(x.p0, y.p0) && within(x.p1, y.p1) && within(x.p2, y.p2), + (PathSeg::Cubic(x), PathSeg::Cubic(y)) => within(x.p0, y.p0) && within(x.p1, y.p1) && within(x.p2, y.p2) && within(x.p3, y.p3), + _ => false, + }); + if paired { + return true; + } + let samples = |s: &PathSeg| { + let len = match s { + PathSeg::Line(l) => l.p0.distance(l.p1), + PathSeg::Quad(q) => q.p0.distance(q.p1) + q.p1.distance(q.p2), + PathSeg::Cubic(c) => c.p0.distance(c.p1) + c.p1.distance(c.p2) + c.p2.distance(c.p3), + }; + // An even count puts a sample at the middle of the piece. + let n = if tol > 0.0 { ((len / tol / 2.0).ceil().clamp(1.0, 32.0) as usize) * 2 } else { 64 }; + let s = *s; + (1..=n).map(move |k| s.eval(k as f64 / n as f64)) + }; + let covers = |from: &[PathSeg], to: &[PathSeg]| { + // Each point's search goes back along `to` from the piece the point before it was near. + let mut at = to.len(); + from.iter().flat_map(samples).all(|p| { + let mut order = to.iter().enumerate().take(at).rev().chain(to.iter().enumerate().skip(at).rev()); + let Some((i, _)) = order.find(|(_, t)| t.nearest(p, 1e-12).distance_sq.sqrt() <= tol) else { return false }; + at = i + 1; + true + }) + }; + covers(a, b) && covers(b, a) +} + /// Pathfinder Outline: split every shape's boundary at junctions of the arrangement. Junctions /// closer together than `reach` are one, and coincident edges within ten times it are one edge. /// `reach` is its value at [`DEFAULT_PRECISION`] times `precision / DEFAULT_PRECISION`, a factor kept @@ -1057,8 +1099,15 @@ fn outline_edges(arr: &Arrangement, shapes: &[Shape], precision: f64) -> Vec, u64)> = Vec::new(); let mut sigs: Vec<(Point, Point, Point)> = Vec::new(); + let mut paths: Vec> = Vec::new(); + let as_path = |c: &[Seg]| -> Vec { + c.iter().map(|s| if s.line { PathSeg::Line(Line::new(s.c.p0, s.c.p3)) } else { PathSeg::Cubic(s.c) }).collect() + }; + let reversed = |c: &[PathSeg]| -> Vec { c.iter().rev().map(|s| s.reverse()).collect() }; let sig = |c: &[Seg]| -> (Point, Point, Point) { let a = c[0].c.p0; let b = c[c.len() - 1].c.p3; @@ -1071,6 +1120,7 @@ fn outline_edges(arr: &Arrangement, shapes: &[Shape], precision: f64) -> Vec> = HashMap::new(); for (chain, key) in chains { let (a, b, m) = sig(&chain); + let path = as_path(&chain); let (cx, cy) = cell(m); let found = (cx - 1..=cx + 1) .flat_map(|x| (cy - 1..=cy + 1).map(move |y| (x, y))) @@ -1079,7 +1129,21 @@ fn outline_edges(arr: &Arrangement, shapes: &[Shape], precision: f64) -> Vec p.end().distance(f.end()) <= p.end().distance(l.start()), + _ => true, + }); + if runs_same { along_closely(&path, &reversed(other), d) } else { along_closely(&path, other, d) } }) .min(); match found { @@ -1087,6 +1151,7 @@ fn outline_edges(arr: &Arrangement, shapes: &[Shape], precision: f64) -> Vec { grid.entry((cx, cy)).or_default().push(out.len()); sigs.push((a, b, m)); + paths.push(path); out.push((chain, key)); } } @@ -1714,4 +1779,41 @@ mod tests { assert_eq!(merged.len(), 40); assert!(merged.iter().enumerate().all(|(k, s)| s.key == k as u64)); } + + /// A curve with the ends of a line that passes within 0.25 pt of the line's middle, and whose + /// own middle is within 0.25 pt of the line, but which runs about 23 pt past the line's start: + /// the middles and ends of the two pieces match, and the two are still not one edge. + #[test] + fn along_closely_checks_curves_between_the_middles_of_their_pieces() { + let line = [PathSeg::Line(Line::new((100.0, 0.0), (0.0, 0.0)))]; + let curve = [PathSeg::Cubic(kurbo::CubicBez::new((0.0, 0.0), (-54.56, 0.4537), (198.85, 0.0963), (100.0, 0.0)))]; + assert!(along(&line, &curve, 0.25), "the middles and ends match"); + assert!(!along_closely(&line, &curve, 0.25)); + assert!(!along_closely(&curve, &line, 0.25)); + let same = [PathSeg::Line(Line::new((0.0, 0.0), (100.0, 0.0)))]; + assert!(along_closely(&line, &same, 0.25)); + } + + /// A curve 1.05 pt from a line at its middle and under 1 pt from it at its thirds: with a + /// control polygon under three times the tolerance long, the samples still take in the middle. + #[test] + fn along_closely_samples_the_middle_of_a_short_piece() { + let line = [PathSeg::Line(Line::new((0.15, 0.0), (0.0, 0.0)))]; + let curve = [PathSeg::Cubic(kurbo::CubicBez::new((0.0, 0.0), (0.05, 1.4), (0.1, 1.4), (0.15, 0.0)))]; + assert!(!along_closely(&curve, &line, 1.0)); + } + + /// Pieces whose control points pair up within the tolerance are one edge, copies included. + #[test] + fn along_closely_takes_pieces_whose_control_points_pair_up() { + let a = [ + PathSeg::Cubic(kurbo::CubicBez::new((0.0, 0.0), (10.0, 20.0), (30.0, 20.0), (40.0, 0.0))), + PathSeg::Line(Line::new((40.0, 0.0), (80.0, 0.0))), + ]; + let reversed: Vec = a.iter().rev().map(|s| s.reverse()).collect(); + assert!(along_closely(&a, &reversed, 0.01)); + let shifted: Vec = reversed.iter().map(|s| kurbo::Affine::translate((0.0, 0.2)) * *s).collect(); + assert!(along_closely(&a, &shifted, 0.25)); + assert!(!along_closely(&a, &shifted, 0.1)); + } } diff --git a/crates/pathops/tests/it/ops.rs b/crates/pathops/tests/it/ops.rs index 9780f71420..def782ffc0 100644 --- a/crates/pathops/tests/it/ops.rs +++ b/crates/pathops/tests/it/ops.rs @@ -254,6 +254,25 @@ fn pf_outline_joins_junctions_closer_than_the_precision() { assert!(close(length(&coarse), length(&at_default), 1e-6), "{} against {}", length(&coarse), length(&at_default)); } +/// Two boundaries that are close only at their start, end and middle stay two edges at 10 pt: an +/// ellipse 0.005 pt inside a circle at its rightmost and leftmost points, and 30 pt inside it at +/// the top and bottom. Two copies of one boundary are still one edge at that precision. +#[test] +fn pf_outline_keeps_boundaries_that_differ_between_their_ends_at_coarse_precision() { + let ellipse = |rx: f64, ry: f64| PathData::from_bezpath(&kurbo::Ellipse::new((100.0, 100.0), (rx, ry), 0.0).to_path(1e-4)); + let s = vec![shape(ellipse(50.0, 50.0), 1), shape(ellipse(49.995, 20.0), 2)]; + let length = |r: &[Shape]| r.iter().map(|s| s.path.length()).sum::(); + let expected = s[0].path.length() + s[1].path.length(); + for precision in [DEFAULT_PRECISION, 10.0] { + let r = pathfinder_with(PathfinderOp::Outline, &s, precision); + assert_eq!(r.len(), 2, "{precision} pt: {r:?}"); + assert!(close(length(&r), expected, 1e-6), "{precision} pt: {} against {expected}", length(&r)); + } + let copies = vec![shape(ellipse(50.0, 50.0), 1), shape(ellipse(50.0, 50.0), 2)]; + let r = pathfinder_with(PathfinderOp::Outline, &copies, 10.0); + assert!(close(length(&r), copies[0].path.length(), 1e-6), "{r:?}"); +} + #[test] fn regions_and_region_at() { let s = stack();