From c96a843d2b5301676650f8a569d39097f94bf1cc Mon Sep 17 00:00:00 2001 From: Object905 Date: Fri, 13 Nov 2026 16:00:48 +0300 Subject: [PATCH] bound endpoint snapping and round axis-aligned crossings A rounded crossing near an endpoint was snapped to it, splitting the other segment there without checking how far that moved the segment. With integer inputs, segments could be bent by a unit or more, and boolean results could lie outside their operands (e.g. a box minus a sliver returned a triangle outside the box). - Snap only to endpoints within half a pixel diagonal of the line of the segment being split; otherwise use the rounded crossing. - Round crossings on axis-aligned segments to nearest instead of truncating, keeping the truncated result on ties. Add regression tests and update expectations that relied on the bent segments, after checking them against an f64 reference. Co-Authored-By: Claude Opus 5.5 --- iOverlay/src/split/cross_solver.rs | 79 +- iOverlay/src/string/slice.rs | 25 +- iOverlay/tests/boolean/test_129.json | 178 ++- iOverlay/tests/boolean/test_130.json | 533 ++----- iOverlay/tests/boolean/test_131.json | 74 +- iOverlay/tests/boolean/test_132.json | 72 +- iOverlay/tests/boolean/test_133.json | 152 +- iOverlay/tests/boolean/test_96.json | 2008 ++++--------------------- iOverlay/tests/fragment_tests.rs | 3 +- iOverlay/tests/issue_91_tests.rs | 68 +- iOverlay/tests/slice_tests.rs | 5 +- iOverlay/tests/snap_rounding_tests.rs | 96 ++ 12 files changed, 955 insertions(+), 2338 deletions(-) create mode 100644 iOverlay/tests/snap_rounding_tests.rs diff --git a/iOverlay/src/split/cross_solver.rs b/iOverlay/src/split/cross_solver.rs index 2a0f3f5b..a4783653 100644 --- a/iOverlay/src/split/cross_solver.rs +++ b/iOverlay/src/split/cross_solver.rs @@ -192,7 +192,7 @@ impl CrossSolver { if r0 <= r1 { let p = if ra0 < rb0 { target.a } else { target.b }; // ignore if it's a clean point - if Triangle::is_not_line(other.a, p, other.b) { + if Triangle::is_not_line(other.a, p, other.b) && Self::is_within_rounding(other, p) { return Some(CrossResult { point: p, cross_type: CrossType::TargetEnd, @@ -203,7 +203,7 @@ impl CrossSolver { let p = if ra1 < rb1 { other.a } else { other.b }; // ignore if it's a clean point - if Triangle::is_not_line(target.a, p, target.b) { + if Triangle::is_not_line(target.a, p, target.b) && Self::is_within_rounding(target, p) { return Some(CrossResult { point: p, cross_type: CrossType::OtherEnd, @@ -220,6 +220,33 @@ impl CrossSolver { }) } + /// Whether splitting `segment` at `p` moves it no further than rounding a crossing on it + /// can: the distance from `p` to the segment's line is at most half a pixel diagonal. + /// + /// Snapping a crossing to an endpoint of one segment splits the other segment at that + /// endpoint. Without this bound, the other segment can be bent by more than a unit, e.g. + /// out of the polygon it bounds. + #[inline] + fn is_within_rounding(segment: &XSegment, p: IntPoint) -> bool { + let v = segment.b - segment.a; + let cross = v.cross_product(p - segment.a).to_f64(); + let sqr_length = segment.a.sqr_distance(segment.b).to_f64(); + 2.0 * cross * cross <= sqr_length + } + + /// Divides, rounding to the nearest integer, with ties toward zero. + /// + /// Truncating would move a crossing on an axis-aligned segment by up to a unit. Rounding + /// ties toward zero, i.e. toward the segment's first endpoint, keeps the truncated result + /// whenever it is one of the two nearest integers. + #[inline] + fn divide_round_half_toward_zero(numerator: I::Wide, divisor: I::Wide) -> I::Wide { + let n = numerator.unsigned_abs(); + let d = divisor.unsigned_abs(); + let q = (n + ((d - ::UInt::ONE) >> 1)) / d; + numerator.signum() * divisor.signum() * I::Wide::from_uint(q) + } + fn cross_point(target: &XSegment, other: &XSegment) -> IntPoint { // edges are not parallel // Input coordinates follow the N-bit range documented in core::integer. @@ -282,11 +309,11 @@ impl CrossSolver { if a1x == I::Wide::ZERO { // dxB is not zero because it will be parallel case and it's impossible x0 = I::Wide::ZERO; - y0 = xy_b / dx_b; + y0 = Self::divide_round_half_toward_zero(xy_b, dx_b); } else if a1y == I::Wide::ZERO { // dyB is not zero because it will be parallel case and it's impossible y0 = I::Wide::ZERO; - x0 = -xy_b / dy_b; + x0 = Self::divide_round_half_toward_zero(-xy_b, dy_b); } else { // divider let div = a1y * dx_b - a1x * dy_b; @@ -325,16 +352,45 @@ mod tests { #[test] fn snapping_compares_squared_distance_directly_with_the_threshold() { - let target = XSegment::new(IntPoint::new(0, 0), IntPoint::new(5, 5)); - let other = XSegment::new(IntPoint::new(0, 3), IntPoint::new(5, -2)); - // The rounded crossing is (2, 2). Its nearest endpoint is (0, 3), + let target = XSegment::new(IntPoint::new(0, 0), IntPoint::new(10, 3)); + let other = XSegment::new(IntPoint::new(1, 0), IntPoint::new(5, 2)); + // The rounded crossing is (3, 1). Its nearest endpoint is (5, 2), // at squared distance 5. A threshold of 4 must not be squared again. let crossing = CrossSolver::cross(&target, &other, 4).unwrap(); assert!(matches!(crossing.cross_type, CrossType::Pure)); - assert_eq!(crossing.point, IntPoint::new(2, 2)); + assert_eq!(crossing.point, IntPoint::new(3, 1)); let snapped = CrossSolver::cross(&target, &other, 5).unwrap(); assert!(matches!(snapped.cross_type, CrossType::OtherEnd)); - assert_eq!(snapped.point, other.a); + assert_eq!(snapped.point, other.b); + } + + #[test] + fn snapping_does_not_move_a_segment_beyond_rounding() { + // The crossing (1, 2 1/11) rounds to (1, 2), a unit from the endpoint (0, 2) of the + // target. Snapping to (0, 2) would bend the vertical segment a unit off its line. + let target = XSegment::new(IntPoint::new(0, 2), IntPoint::new(11, 3)); + let other = XSegment::new(IntPoint::new(1, 1), IntPoint::new(1, 4)); + for radius_squared in [1, 2, 4, 16] { + let crossing = CrossSolver::cross(&target, &other, radius_squared).unwrap(); + assert!(matches!(crossing.cross_type, CrossType::Pure)); + assert_eq!(crossing.point, IntPoint::new(1, 2)); + } + } + + #[test] + fn axis_aligned_crossings_are_rounded() { + // The crossing is (-1.25, -1); truncating relative to the first endpoint gave (-2, -1). + let horizontal = XSegment::new(IntPoint::new(-2, -1), IntPoint::new(-1, -1)); + let other = XSegment::new(IntPoint::new(-2, 2), IntPoint::new(-1, -2)); + assert_eq!( + CrossSolver::cross_point(&horizontal, &other), + IntPoint::new(-1, -1) + ); + + // The same, transposed. + let vertical = XSegment::new(IntPoint::new(-1, -2), IntPoint::new(-1, -1)); + let other = XSegment::new(IntPoint::new(-2, -2), IntPoint::new(2, 1)); + assert_eq!(CrossSolver::cross_point(&vertical, &other), IntPoint::new(-1, -1)); } #[test] @@ -376,9 +432,10 @@ mod tests { b: point(hi, b1), }; let result = CrossSolver::::cross_point(&a, &b); - // For a horizontal target x is truncated, not rounded. + // For a horizontal target x is rounded with ties toward a0. let expected_x = if a0 == a1 { - lo as i128 + (hi as i128 - lo as i128) * n.abs() / d.abs() + let product = (hi as i128 - lo as i128) * n.abs(); + lo as i128 + (product + (d.abs() - 1) / 2) / d.abs() } else { expected_x }; diff --git a/iOverlay/src/string/slice.rs b/iOverlay/src/string/slice.rs index 1e882625..a344a9ba 100644 --- a/iOverlay/src/string/slice.rs +++ b/iOverlay/src/string/slice.rs @@ -318,10 +318,27 @@ mod tests { FillRule::NonZero, ); - assert_eq!(shapes.len(), 3); - assert_eq!(shapes[0][0].len(), 5); - assert_eq!(shapes[1][0].len(), 3); - assert_eq!(shapes[2][0].len(), 3); + // The line crosses the edges at (0.5, 2) and (1.5, 2), which round to (0, 2) and (1, 2). + // The top-left part, a triangle half a unit wide, collapses onto the left edge. The + // crossing isn't snapped to the vertex (1, 1), a unit away from the line. + assert_eq!( + shapes, + vec![ + vec![vec![ + IntPoint::new(2, 0), + IntPoint::new(2, 2), + IntPoint::new(1, 2), + IntPoint::new(1, 1), + IntPoint::new(0, 2), + IntPoint::new(0, 0), + ]], + vec![vec![ + IntPoint::new(1, 2), + IntPoint::new(2, 2), + IntPoint::new(2, 3) + ]], + ] + ); } #[test] diff --git a/iOverlay/tests/boolean/test_129.json b/iOverlay/tests/boolean/test_129.json index e9ef3b23..d20e6071 100644 --- a/iOverlay/tests/boolean/test_129.json +++ b/iOverlay/tests/boolean/test_129.json @@ -28,10 +28,54 @@ -2, 0 ], + [ + 0, + 0 + ], + [ + 0, + -1 + ] + ] + ], + [ + [ + [ + -1, + -3 + ], + [ + -1, + -2 + ], + [ + 0, + -1 + ], + [ + 0, + -2 + ], [ 1, + -1 + ], + [ + 0, + -1 + ], + [ + 1, + 0 + ], + [ + 0, 0 ], + [ + 1, + 2 + ], [ 1, 3 @@ -43,14 +87,6 @@ [ 2, -3 - ], - [ - -1, - -3 - ], - [ - -1, - -2 ] ] ] @@ -61,12 +97,24 @@ [ [ [ - -2, + -1, + -3 + ], + [ + -1, + -2 + ], + [ + 0, + -1 + ], + [ + 0, 0 ], [ 1, - -1 + 2 ], [ 1, @@ -79,14 +127,6 @@ [ 2, -3 - ], - [ - -1, - -3 - ], - [ - -1, - -2 ] ] ] @@ -101,11 +141,11 @@ 0 ], [ - 1, + 0, 0 ], [ - 1, + 0, -1 ] ] @@ -117,13 +157,41 @@ [ [ [ - -2, - 0 + -1, + -3 + ], + [ + -1, + -2 + ], + [ + 0, + -1 + ], + [ + 0, + -2 ], [ 1, -1 ], + [ + 0, + -1 + ], + [ + 1, + 0 + ], + [ + 0, + 0 + ], + [ + 1, + 2 + ], [ 1, 3 @@ -135,14 +203,6 @@ [ 2, -3 - ], - [ - -1, - -3 - ], - [ - -1, - -2 ] ] ] @@ -150,6 +210,38 @@ ], "intersect": [ [ + [ + [ + [ + 0, + -2 + ], + [ + 0, + -1 + ], + [ + 1, + -1 + ] + ] + ], + [ + [ + [ + 0, + -1 + ], + [ + 0, + 0 + ], + [ + 1, + 0 + ] + ] + ] ] ], "subject": [ @@ -164,6 +256,22 @@ 1, 0 ], + [ + 0, + -1 + ] + ] + ], + [ + [ + [ + 0, + -2 + ], + [ + 0, + -1 + ], [ 1, -1 @@ -205,9 +313,13 @@ 0 ], [ - 1, + 0, 0 ], + [ + 1, + 2 + ], [ 1, 3 @@ -227,6 +339,10 @@ [ -1, -2 + ], + [ + 0, + -1 ] ] ] diff --git a/iOverlay/tests/boolean/test_130.json b/iOverlay/tests/boolean/test_130.json index 6bca5363..8a2613d4 100644 --- a/iOverlay/tests/boolean/test_130.json +++ b/iOverlay/tests/boolean/test_130.json @@ -1,72 +1,5 @@ { "union" : [ - [ - [ - [ - [-5, 5], [3, 2], [5, 0], [3, -1], [2, -2], [2, -3], [1, -2], [0, 0] - ] - ], - [ - [ - [-2, -3], [1, -2], [-1, -3] - ] - ], - [ - [ - [2, -3], [3, -1], [5, -5] - ] - ] - ], - [ - [ - [ - [ - -5, - 5 - ], - [ - 3, - 2 - ], - [ - 5, - 0 - ], - [ - 3, - -1 - ], - [ - 2, - -3 - ], - [ - 1, - -2 - ], - [ - 0, - 0 - ] - ] - ], - [ - [ - [ - -2, - -3 - ], - [ - 1, - -2 - ], - [ - -1, - -3 - ] - ] - ] - ], [ [ [ @@ -78,6 +11,10 @@ 3, 2 ], + [ + 3, + 1 + ], [ 5, 0 @@ -86,104 +23,82 @@ 3, -1 ], + [ + 2, + -1 + ], + [ + 2, + -2 + ], + [ + 5, + -5 + ], [ 2, -3 ], + [ + 1, + -2 + ], [ 0, 0 ] ] + ], + [ + [ + [ + -2, + -3 + ], + [ + 1, + -2 + ], + [ + -1, + -3 + ] + ] ] ] ], "fillRule" : 1, "subject" : [ - [ - [ - [ - [ - -2, - -3 - ], - [ - 1, - -2 - ], - [ - -1, - -3 - ] - ] - ], - [ - [ - [ - 0, - 2 - ], - [ - 3, - 2 - ], - [ - 5, - 0 - ], - [ - 3, - -1 - ] - ] - ], - [ - [ - [ - 2, - -2 - ], - [ - 2, - -1 - ], - [ - 3, - -1 - ] - ] - ] - ], [ [ [ [ - 0, - 2 - ], - [ - 3, - 2 + -2, + -3 ], [ - 5, - 0 + 1, + -2 ], [ - 3, - -1 + -1, + -3 ] ] ], [ [ [ - 2, - -3 + 0, + 2 ], [ - 2, - -1 + 3, + 1 + ], + [ + 5, + 0 ], [ 3, @@ -194,24 +109,6 @@ ] ], "intersect" : [ - [ - [ - [ - [ - 0, - 2 - ], - [ - 3, - 2 - ], - [ - 3, - -1 - ] - ] - ] - ], [ [ [ @@ -221,7 +118,7 @@ ], [ 3, - 2 + 1 ], [ 3, @@ -256,94 +153,6 @@ ] ], "xor" : [ - [ - [ - [ - [-5, 5], [3, 2], [0, 2], [3, -1], [2, -2], [2, -3], [1, -2], [0, 0] - ] - ], - [ - [ - [-2, -3], [1, -2], [-1, -3] - ] - ], - [ - [ - [2, -3], [3, -1], [5, -5] - ] - ], - [ - [ - [3, -1], [3, 2], [5, 0] - ] - ] - ], - [ - [ - [ - [ - -5, - 5 - ], - [ - 3, - 2 - ], - [ - 0, - 2 - ], - [ - 3, - -1 - ], - [ - 2, - -3 - ], - [ - 1, - -2 - ], - [ - 0, - 0 - ] - ] - ], - [ - [ - [ - -2, - -3 - ], - [ - 1, - -2 - ], - [ - -1, - -3 - ] - ] - ], - [ - [ - [ - 3, - -1 - ], - [ - 3, - 2 - ], - [ - 5, - 0 - ] - ] - ] - ], [ [ [ @@ -355,6 +164,10 @@ 3, 2 ], + [ + 3, + 1 + ], [ 0, 2 @@ -363,16 +176,48 @@ 3, -1 ], + [ + 2, + -1 + ], + [ + 2, + -2 + ], + [ + 5, + -5 + ], [ 2, -3 ], + [ + 1, + -2 + ], [ 0, 0 ] ] ], + [ + [ + [ + -2, + -3 + ], + [ + 1, + -2 + ], + [ + -1, + -3 + ] + ] + ], [ [ [ @@ -381,7 +226,7 @@ ], [ 3, - 2 + 1 ], [ 5, @@ -392,64 +237,6 @@ ] ], "inverseDifference" : [ - [ - [ - [ - [-5, 5], [3, 2], [0, 2], [3, -1], [2, -1], [2, -3], [1, -2], [0, 0] - ] - ], - [ - [ - [2, -3], [3, -1], [5, -5] - ] - ] - ], - [ - [ - [ - [ - -5, - 5 - ], - [ - 3, - 2 - ], - [ - 0, - 2 - ], - [ - 3, - -1 - ], - [ - 2, - -1 - ], - [ - 2, - -2 - ], - [ - 3, - -1 - ], - [ - 2, - -3 - ], - [ - 1, - -2 - ], - [ - 0, - 0 - ] - ] - ] - ], [ [ [ @@ -461,6 +248,10 @@ 3, 2 ], + [ + 3, + 1 + ], [ 0, 2 @@ -473,10 +264,22 @@ 2, -1 ], + [ + 2, + -2 + ], + [ + 5, + -5 + ], [ 2, -3 ], + [ + 1, + -2 + ], [ 0, 0 @@ -486,60 +289,6 @@ ] ], "clip" : [ - [ - [ - [ - [-5, 5], [3, 2], [3, -1], [2, -1], [2, -3], [1, -2], [0, 0] - ] - ], - [ - [ - [2, -3], [3, -1], [5, -5] - ] - ] - ], - [ - [ - [ - [ - -5, - 5 - ], - [ - 3, - 2 - ], - [ - 3, - -1 - ], - [ - 2, - -1 - ], - [ - 2, - -2 - ], - [ - 3, - -1 - ], - [ - 2, - -3 - ], - [ - 1, - -2 - ], - [ - 0, - 0 - ] - ] - ] - ], [ [ [ @@ -559,10 +308,22 @@ 2, -1 ], + [ + 2, + -2 + ], + [ + 5, + -5 + ], [ 2, -3 ], + [ + 1, + -2 + ], [ 0, 0 @@ -572,70 +333,20 @@ ] ], "difference" : [ - [ - [ - [ - [ - -2, - -3 - ], - [ - 1, - -2 - ], - [ - -1, - -3 - ] - ] - ], - [ - [ - [ - 2, - -2 - ], - [ - 2, - -1 - ], - [ - 3, - -1 - ] - ] - ], - [ - [ - [ - 3, - -1 - ], - [ - 3, - 2 - ], - [ - 5, - 0 - ] - ] - ] - ], [ [ [ [ - 2, + -2, -3 ], [ - 2, - -1 + 1, + -2 ], [ - 3, - -1 + -1, + -3 ] ] ], @@ -647,7 +358,7 @@ ], [ 3, - 2 + 1 ], [ 5, diff --git a/iOverlay/tests/boolean/test_131.json b/iOverlay/tests/boolean/test_131.json index 9f30ac79..35c5cde8 100644 --- a/iOverlay/tests/boolean/test_131.json +++ b/iOverlay/tests/boolean/test_131.json @@ -28,20 +28,20 @@ 5 ], [ - 4, + 3, 4 ], [ - 2, - 3 + 4, + 4 ], [ - 5, - 2 + 3, + 3 ], [ 2, - 0 + 3 ], [ 2, @@ -68,6 +68,22 @@ -1 ] ] + ], + [ + [ + [ + 2, + 0 + ], + [ + 3, + 3 + ], + [ + 5, + 2 + ] + ] ] ] ], @@ -107,6 +123,10 @@ 2, 3 ], + [ + 3, + 3 + ], [ 5, 2 @@ -172,10 +192,18 @@ -1, 5 ], + [ + 3, + 4 + ], [ 4, 4 ], + [ + 3, + 3 + ], [ 2, 3 @@ -237,7 +265,7 @@ 0 ], [ - 2, + 3, 3 ], [ @@ -265,6 +293,22 @@ -2 ] ] + ], + [ + [ + [ + 2, + 0 + ], + [ + 2, + 3 + ], + [ + 3, + 3 + ] + ] ] ] ], @@ -276,14 +320,22 @@ -1, 5 ], + [ + 3, + 4 + ], [ 4, 4 ], [ - 2, + 3, 3 ], + [ + 2, + 0 + ], [ 2, -1 @@ -320,12 +372,16 @@ -1, 5 ], + [ + 3, + 4 + ], [ 4, 4 ], [ - 2, + 3, 3 ], [ diff --git a/iOverlay/tests/boolean/test_132.json b/iOverlay/tests/boolean/test_132.json index 4afbd098..885be006 100644 --- a/iOverlay/tests/boolean/test_132.json +++ b/iOverlay/tests/boolean/test_132.json @@ -73,7 +73,7 @@ 2 ], [ - 1, + 0, 3 ], [ @@ -97,6 +97,22 @@ -3 ] ] + ], + [ + [ + [ + 0, + 3 + ], + [ + 1, + 5 + ], + [ + 1, + 3 + ] + ] ] ] ], @@ -125,7 +141,7 @@ 2 ], [ - 1, + 0, 3 ], [ @@ -149,6 +165,22 @@ -3 ] ] + ], + [ + [ + [ + 0, + 3 + ], + [ + 1, + 5 + ], + [ + 1, + 3 + ] + ] ] ] ], @@ -161,7 +193,7 @@ 2 ], [ - 1, + 0, 3 ], [ @@ -185,6 +217,22 @@ -3 ] ] + ], + [ + [ + [ + 0, + 3 + ], + [ + 1, + 5 + ], + [ + 1, + 3 + ] + ] ] ] ], @@ -242,7 +290,7 @@ 2 ], [ - 1, + 0, 3 ], [ @@ -266,6 +314,22 @@ -3 ] ] + ], + [ + [ + [ + 0, + 3 + ], + [ + 1, + 5 + ], + [ + 1, + 3 + ] + ] ] ] ] diff --git a/iOverlay/tests/boolean/test_133.json b/iOverlay/tests/boolean/test_133.json index cff13f5b..c0d6fa37 100644 --- a/iOverlay/tests/boolean/test_133.json +++ b/iOverlay/tests/boolean/test_133.json @@ -6,32 +6,6 @@ ] ], "subject" : [ - [ - [ - [ - [ - 0, - 2 - ], - [ - 4, - 3 - ], - [ - 4, - -1 - ], - [ - 3, - -3 - ], - [ - 3, - 0 - ] - ] - ] - ], [ [ [ @@ -39,6 +13,10 @@ 0, 2 ], + [ + 3, + 3 + ], [ 4, 3 @@ -46,6 +24,14 @@ [ 4, -1 + ], + [ + 3, + -3 + ], + [ + 3, + 0 ] ] ] @@ -81,32 +67,6 @@ ] ], "union" : [ - [ - [ - [ - [ - 0, - 2 - ], - [ - 4, - 3 - ], - [ - 4, - -1 - ], - [ - 3, - -3 - ], - [ - 3, - 0 - ] - ] - ] - ], [ [ [ @@ -114,6 +74,10 @@ 0, 2 ], + [ + 3, + 3 + ], [ 4, 3 @@ -121,38 +85,20 @@ [ 4, -1 + ], + [ + 3, + -3 + ], + [ + 3, + 0 ] ] ] ] ], "xor" : [ - [ - [ - [ - [ - 0, - 2 - ], - [ - 4, - 3 - ], - [ - 4, - -1 - ], - [ - 3, - -3 - ], - [ - 3, - 0 - ] - ] - ] - ], [ [ [ @@ -160,6 +106,10 @@ 0, 2 ], + [ + 3, + 3 + ], [ 4, 3 @@ -167,38 +117,20 @@ [ 4, -1 + ], + [ + 3, + -3 + ], + [ + 3, + 0 ] ] ] ] ], "difference" : [ - [ - [ - [ - [ - 0, - 2 - ], - [ - 4, - 3 - ], - [ - 4, - -1 - ], - [ - 3, - -3 - ], - [ - 3, - 0 - ] - ] - ] - ], [ [ [ @@ -206,6 +138,10 @@ 0, 2 ], + [ + 3, + 3 + ], [ 4, 3 @@ -213,6 +149,14 @@ [ 4, -1 + ], + [ + 3, + -3 + ], + [ + 3, + 0 ] ] ] diff --git a/iOverlay/tests/boolean/test_96.json b/iOverlay/tests/boolean/test_96.json index 2239da1a..0a559b38 100644 --- a/iOverlay/tests/boolean/test_96.json +++ b/iOverlay/tests/boolean/test_96.json @@ -60,116 +60,6 @@ ] ], "subject" : [ - [ - [ - [ - [ - -1136, - -48 - ], - [ - -940, - 404 - ], - [ - -922, - 444 - ], - [ - -746, - 858 - ], - [ - -270, - 987 - ], - [ - -228, - 999 - ], - [ - -226, - 999 - ], - [ - 205, - 1118 - ], - [ - 603, - 827 - ], - [ - 638, - 802 - ], - [ - 640, - 800 - ], - [ - 1002, - 537 - ], - [ - 1023, - 44 - ], - [ - 1025, - 1 - ], - [ - 1025, - -1 - ], - [ - 1045, - -449 - ], - [ - 672, - -772 - ], - [ - 640, - -800 - ], - [ - 638, - -802 - ], - [ - 300, - -1097 - ], - [ - -184, - -1007 - ], - [ - -226, - -999 - ], - [ - -229, - -999 - ], - [ - -670, - -919 - ], - [ - -902, - -483 - ], - [ - -922, - -444 - ] - ] - ] - ], [ [ [ @@ -182,110 +72,12 @@ 404 ], [ - -922, + -923, 444 ], - [ - -746, - 858 - ], - [ - -270, - 987 - ], - [ - -227, - 998 - ], - [ - 205, - 1118 - ], - [ - 603, - 827 - ], - [ - 638, - 802 - ], - [ - 640, - 800 - ], - [ - 1002, - 537 - ], - [ - 1023, - 44 - ], - [ - 1024, - 0 - ], - [ - 1045, - -449 - ], - [ - 672, - -772 - ], - [ - 640, - -800 - ], - [ - 638, - -802 - ], - [ - 300, - -1097 - ], - [ - -184, - -1007 - ], - [ - -226, - -1000 - ], - [ - -229, - -999 - ], - [ - -670, - -919 - ], - [ - -902, - -483 - ], - [ - -922, - -444 - ] - ] - ] - ], - [ - [ - [ - [ - -1136, - -48 - ], - [ - -940, - 404 - ], [ -922, - 444 + 445 ], [ -746, @@ -296,8 +88,12 @@ 987 ], [ - -227, - 998 + -228, + 999 + ], + [ + -226, + 999 ], [ 205, @@ -324,8 +120,12 @@ 44 ], [ - 1024, - 0 + 1025, + 1 + ], + [ + 1025, + -1 ], [ 1045, @@ -352,8 +152,8 @@ -1007 ], [ - -227, - -998 + -226, + -999 ], [ -229, @@ -369,6 +169,10 @@ ], [ -922, + -445 + ], + [ + -923, -444 ] ] @@ -376,256 +180,6 @@ ] ], "xor" : [ - [ - [ - [ - [ - -2048, - 0 - ], - [ - -922, - 444 - ], - [ - -940, - 404 - ], - [ - -1136, - -48 - ], - [ - -922, - -444 - ] - ] - ], - [ - [ - [ - -1276, - -1601 - ], - [ - -922, - -444 - ], - [ - -902, - -483 - ], - [ - -670, - -919 - ], - [ - -229, - -999 - ] - ] - ], - [ - [ - [ - -1276, - 1601 - ], - [ - -228, - 999 - ], - [ - -270, - 987 - ], - [ - -746, - 858 - ], - [ - -922, - 444 - ] - ] - ], - [ - [ - [ - -229, - -999 - ], - [ - -227, - -998 - ], - [ - -226, - -999 - ] - ] - ], - [ - [ - [ - -228, - 999 - ], - [ - -226, - 999 - ], - [ - -227, - 998 - ] - ] - ], - [ - [ - [ - -226, - -999 - ], - [ - -184, - -1007 - ], - [ - 300, - -1097 - ], - [ - 638, - -802 - ], - [ - 455, - -1996 - ] - ] - ], - [ - [ - [ - -226, - 999 - ], - [ - 455, - 1996 - ], - [ - 638, - 802 - ], - [ - 603, - 827 - ], - [ - 205, - 1118 - ] - ] - ], - [ - [ - [ - 638, - -802 - ], - [ - 638, - -800 - ], - [ - 640, - -800 - ] - ] - ], - [ - [ - [ - 638, - 800 - ], - [ - 638, - 802 - ], - [ - 640, - 800 - ] - ] - ], - [ - [ - [ - 640, - -800 - ], - [ - 672, - -772 - ], - [ - 1045, - -449 - ], - [ - 1025, - -1 - ], - [ - 1845, - -888 - ] - ] - ], - [ - [ - [ - 640, - 800 - ], - [ - 1845, - 888 - ], - [ - 1025, - 1 - ], - [ - 1023, - 44 - ], - [ - 1002, - 537 - ] - ] - ], - [ - [ - [ - 1024, - 0 - ], - [ - 1025, - 1 - ], - [ - 1025, - -1 - ] - ] - ] - ], [ [ [ @@ -634,7 +188,7 @@ 0 ], [ - -922, + -923, 444 ], [ @@ -646,7 +200,7 @@ -48 ], [ - -922, + -923, -444 ] ] @@ -659,7 +213,7 @@ ], [ -922, - -444 + -445 ], [ -902, @@ -682,8 +236,8 @@ 1601 ], [ - -227, - 998 + -228, + 999 ], [ -270, @@ -693,6 +247,38 @@ -746, 858 ], + [ + -922, + 445 + ] + ] + ], + [ + [ + [ + -923, + -444 + ], + [ + -922, + -444 + ], + [ + -922, + -445 + ] + ] + ], + [ + [ + [ + -923, + 444 + ], + [ + -922, + 445 + ], [ -922, 444 @@ -711,31 +297,23 @@ ], [ -226, - -1000 + -999 ] ] ], [ [ [ - -227, - 998 - ], - [ - 455, - 1996 - ], - [ - 638, - 802 + -228, + 999 ], [ - 603, - 827 + -226, + 999 ], [ - 205, - 1118 + -227, + 998 ] ] ], @@ -743,7 +321,7 @@ [ [ -226, - -1000 + -999 ], [ -184, @@ -763,6 +341,30 @@ ] ] ], + [ + [ + [ + -226, + 999 + ], + [ + 455, + 1996 + ], + [ + 638, + 802 + ], + [ + 603, + 827 + ], + [ + 205, + 1118 + ] + ] + ], [ [ [ @@ -810,8 +412,8 @@ -449 ], [ - 1024, - 0 + 1025, + -1 ], [ 1845, @@ -830,8 +432,8 @@ 888 ], [ - 1024, - 0 + 1025, + 1 ], [ 1023, @@ -842,372 +444,72 @@ 537 ] ] - ] - ], - [ + ], [ [ [ - -2048, + 1024, 0 ], [ - -922, - 444 + 1025, + 1 ], [ - -940, - 404 - ], + 1025, + -1 + ] + ] + ] + ] + ], + "intersect" : [ + [ + [ + [ [ -1136, -48 ], [ - -922, - -444 - ] - ] - ], - [ - [ + -940, + 404 + ], [ - -1276, - -1601 + -923, + 444 ], [ -922, - -444 + 444 ], [ - -902, - -483 + -922, + 445 ], [ - -670, - -919 + -746, + 858 ], [ - -229, - -999 - ] - ] - ], - [ - [ + -270, + 987 + ], [ - -1276, - 1601 + -228, + 999 ], [ -227, 998 ], [ - -270, - 987 + -226, + 999 ], [ - -746, - 858 - ], - [ - -922, - 444 - ] - ] - ], - [ - [ - [ - -227, - -998 - ], - [ - -184, - -1007 - ], - [ - 300, - -1097 - ], - [ - 638, - -802 - ], - [ - 455, - -1996 - ] - ] - ], - [ - [ - [ - -227, - 998 - ], - [ - 455, - 1996 - ], - [ - 638, - 802 - ], - [ - 603, - 827 - ], - [ - 205, - 1118 - ] - ] - ], - [ - [ - [ - 638, - -802 - ], - [ - 638, - -800 - ], - [ - 640, - -800 - ] - ] - ], - [ - [ - [ - 638, - 800 - ], - [ - 638, - 802 - ], - [ - 640, - 800 - ] - ] - ], - [ - [ - [ - 640, - -800 - ], - [ - 672, - -772 - ], - [ - 1045, - -449 - ], - [ - 1024, - 0 - ], - [ - 1845, - -888 - ] - ] - ], - [ - [ - [ - 640, - 800 - ], - [ - 1845, - 888 - ], - [ - 1024, - 0 - ], - [ - 1023, - 44 - ], - [ - 1002, - 537 - ] - ] - ] - ] - ], - "intersect" : [ - [ - [ - [ - [ - -1136, - -48 - ], - [ - -940, - 404 - ], - [ - -922, - 444 - ], - [ - -746, - 858 - ], - [ - -270, - 987 - ], - [ - -228, - 999 - ], - [ - -227, - 998 - ], - [ - -226, - 999 - ], - [ - 205, - 1118 - ], - [ - 603, - 827 - ], - [ - 638, - 802 - ], - [ - 638, - 800 - ], - [ - 640, - 800 - ], - [ - 1002, - 537 - ], - [ - 1023, - 44 - ], - [ - 1025, - 1 - ], - [ - 1024, - 0 - ], - [ - 1025, - -1 - ], - [ - 1045, - -449 - ], - [ - 672, - -772 - ], - [ - 640, - -800 - ], - [ - 638, - -800 - ], - [ - 638, - -802 - ], - [ - 300, - -1097 - ], - [ - -184, - -1007 - ], - [ - -226, - -999 - ], - [ - -227, - -998 - ], - [ - -229, - -999 - ], - [ - -670, - -919 - ], - [ - -902, - -483 - ], - [ - -922, - -444 - ] - ] - ] - ], - [ - [ - [ - [ - -1136, - -48 - ], - [ - -940, - 404 - ], - [ - -922, - 444 - ], - [ - -746, - 858 - ], - [ - -270, - 987 - ], - [ - -227, - 998 - ], - [ - 205, - 1118 + 205, + 1118 ], [ 603, @@ -1233,10 +535,18 @@ 1023, 44 ], + [ + 1025, + 1 + ], [ 1024, 0 ], + [ + 1025, + -1 + ], [ 1045, -449 @@ -1267,7 +577,7 @@ ], [ -226, - -1000 + -999 ], [ -227, @@ -1287,116 +597,14 @@ ], [ -922, - -444 - ] - ] - ] - ], - [ - [ - [ - [ - -1136, - -48 - ], - [ - -940, - 404 + -445 ], [ -922, - 444 - ], - [ - -746, - 858 - ], - [ - -270, - 987 - ], - [ - -227, - 998 - ], - [ - 205, - 1118 - ], - [ - 603, - 827 - ], - [ - 638, - 802 - ], - [ - 638, - 800 - ], - [ - 640, - 800 - ], - [ - 1002, - 537 - ], - [ - 1023, - 44 - ], - [ - 1024, - 0 - ], - [ - 1045, - -449 - ], - [ - 672, - -772 - ], - [ - 640, - -800 - ], - [ - 638, - -800 - ], - [ - 638, - -802 - ], - [ - 300, - -1097 - ], - [ - -184, - -1007 - ], - [ - -227, - -998 - ], - [ - -229, - -999 - ], - [ - -670, - -919 - ], - [ - -902, - -483 + -444 ], [ - -922, + -923, -444 ] ] @@ -1465,346 +673,6 @@ ], "fillRule" : 0, "inverseDifference" : [ - [ - [ - [ - [ - -2048, - 0 - ], - [ - -922, - 444 - ], - [ - -940, - 404 - ], - [ - -1136, - -48 - ], - [ - -922, - -444 - ] - ] - ], - [ - [ - [ - -1276, - -1601 - ], - [ - -922, - -444 - ], - [ - -902, - -483 - ], - [ - -670, - -919 - ], - [ - -229, - -999 - ] - ] - ], - [ - [ - [ - -1276, - 1601 - ], - [ - -228, - 999 - ], - [ - -270, - 987 - ], - [ - -746, - 858 - ], - [ - -922, - 444 - ] - ] - ], - [ - [ - [ - -226, - -999 - ], - [ - -184, - -1007 - ], - [ - 300, - -1097 - ], - [ - 638, - -802 - ], - [ - 455, - -1996 - ] - ] - ], - [ - [ - [ - -226, - 999 - ], - [ - 455, - 1996 - ], - [ - 638, - 802 - ], - [ - 603, - 827 - ], - [ - 205, - 1118 - ] - ] - ], - [ - [ - [ - 640, - -800 - ], - [ - 672, - -772 - ], - [ - 1045, - -449 - ], - [ - 1025, - -1 - ], - [ - 1845, - -888 - ] - ] - ], - [ - [ - [ - 640, - 800 - ], - [ - 1845, - 888 - ], - [ - 1025, - 1 - ], - [ - 1023, - 44 - ], - [ - 1002, - 537 - ] - ] - ] - ], - [ - [ - [ - [ - -2048, - 0 - ], - [ - -922, - 444 - ], - [ - -940, - 404 - ], - [ - -1136, - -48 - ], - [ - -922, - -444 - ] - ] - ], - [ - [ - [ - -1276, - -1601 - ], - [ - -922, - -444 - ], - [ - -902, - -483 - ], - [ - -670, - -919 - ], - [ - -229, - -999 - ] - ] - ], - [ - [ - [ - -1276, - 1601 - ], - [ - -227, - 998 - ], - [ - -270, - 987 - ], - [ - -746, - 858 - ], - [ - -922, - 444 - ] - ] - ], - [ - [ - [ - -227, - -998 - ], - [ - -184, - -1007 - ], - [ - 300, - -1097 - ], - [ - 638, - -802 - ], - [ - 455, - -1996 - ] - ] - ], - [ - [ - [ - -227, - 998 - ], - [ - 455, - 1996 - ], - [ - 638, - 802 - ], - [ - 603, - 827 - ], - [ - 205, - 1118 - ] - ] - ], - [ - [ - [ - 640, - -800 - ], - [ - 672, - -772 - ], - [ - 1045, - -449 - ], - [ - 1024, - 0 - ], - [ - 1845, - -888 - ] - ] - ], - [ - [ - [ - 640, - 800 - ], - [ - 1845, - 888 - ], - [ - 1024, - 0 - ], - [ - 1023, - 44 - ], - [ - 1002, - 537 - ] - ] - ] - ], [ [ [ @@ -1813,7 +681,7 @@ 0 ], [ - -922, + -923, 444 ], [ @@ -1825,7 +693,7 @@ -48 ], [ - -922, + -923, -444 ] ] @@ -1838,7 +706,7 @@ ], [ -922, - -444 + -445 ], [ -902, @@ -1861,44 +729,20 @@ 1601 ], [ - -227, - 998 + -228, + 999 ], [ -270, 987 ], [ - -746, - 858 - ], - [ - -922, - 444 - ] - ] - ], - [ - [ - [ - -227, - 998 - ], - [ - 455, - 1996 - ], - [ - 638, - 802 - ], - [ - 603, - 827 + -746, + 858 ], [ - 205, - 1118 + -922, + 445 ] ] ], @@ -1906,7 +750,7 @@ [ [ -226, - -1000 + -999 ], [ -184, @@ -1926,6 +770,30 @@ ] ] ], + [ + [ + [ + -226, + 999 + ], + [ + 455, + 1996 + ], + [ + 638, + 802 + ], + [ + 603, + 827 + ], + [ + 205, + 1118 + ] + ] + ], [ [ [ @@ -1941,8 +809,8 @@ -449 ], [ - 1024, - 0 + 1025, + -1 ], [ 1845, @@ -1961,8 +829,8 @@ 888 ], [ - 1024, - 0 + 1025, + 1 ], [ 1023, @@ -1977,108 +845,6 @@ ] ], "clip" : [ - [ - [ - [ - [ - -2048, - 0 - ], - [ - -922, - 444 - ], - [ - -1276, - 1601 - ], - [ - -228, - 999 - ], - [ - -227, - 998 - ], - [ - -226, - 999 - ], - [ - 455, - 1996 - ], - [ - 638, - 802 - ], - [ - 638, - 800 - ], - [ - 640, - 800 - ], - [ - 1845, - 888 - ], - [ - 1025, - 1 - ], - [ - 1024, - 0 - ], - [ - 1025, - -1 - ], - [ - 1845, - -888 - ], - [ - 640, - -800 - ], - [ - 638, - -800 - ], - [ - 638, - -802 - ], - [ - 455, - -1996 - ], - [ - -226, - -999 - ], - [ - -227, - -998 - ], - [ - -229, - -999 - ], - [ - -1276, - -1601 - ], - [ - -922, - -444 - ] - ] - ] - ], [ [ [ @@ -2087,99 +853,33 @@ 0 ], [ - -922, + -923, 444 ], - [ - -1276, - 1601 - ], - [ - -227, - 998 - ], - [ - 455, - 1996 - ], - [ - 638, - 802 - ], - [ - 638, - 800 - ], - [ - 640, - 800 - ], - [ - 1845, - 888 - ], - [ - 1024, - 0 - ], - [ - 1845, - -888 - ], - [ - 640, - -800 - ], - [ - 638, - -800 - ], - [ - 638, - -802 - ], - [ - 455, - -1996 - ], - [ - -227, - -998 - ], - [ - -229, - -999 - ], - [ - -1276, - -1601 - ], [ -922, - -444 - ] - ] - ] - ], - [ - [ - [ - [ - -2048, - 0 + 444 ], [ -922, - 444 + 445 ], [ -1276, 1601 ], + [ + -228, + 999 + ], [ -227, 998 ], + [ + -226, + 999 + ], [ 455, 1996 @@ -2200,10 +900,18 @@ 1845, 888 ], + [ + 1025, + 1 + ], [ 1024, 0 ], + [ + 1025, + -1 + ], [ 1845, -888 @@ -2226,7 +934,7 @@ ], [ -226, - -1000 + -999 ], [ -227, @@ -2242,6 +950,14 @@ ], [ -922, + -445 + ], + [ + -922, + -444 + ], + [ + -923, -444 ] ] @@ -2249,88 +965,6 @@ ] ], "union" : [ - [ - [ - [ - [ - -2048, - 0 - ], - [ - -922, - 444 - ], - [ - -1276, - 1601 - ], - [ - -228, - 999 - ], - [ - -226, - 999 - ], - [ - 455, - 1996 - ], - [ - 638, - 802 - ], - [ - 640, - 800 - ], - [ - 1845, - 888 - ], - [ - 1025, - 1 - ], - [ - 1025, - -1 - ], - [ - 1845, - -888 - ], - [ - 640, - -800 - ], - [ - 638, - -802 - ], - [ - 455, - -1996 - ], - [ - -226, - -999 - ], - [ - -229, - -999 - ], - [ - -1276, - -1601 - ], - [ - -922, - -444 - ] - ] - ] - ], [ [ [ @@ -2339,16 +973,24 @@ 0 ], [ - -922, + -923, 444 ], + [ + -922, + 445 + ], [ -1276, 1601 ], [ - -227, - 998 + -228, + 999 + ], + [ + -226, + 999 ], [ 455, @@ -2367,8 +1009,12 @@ 888 ], [ - 1024, - 0 + 1025, + 1 + ], + [ + 1025, + -1 ], [ 1845, @@ -2388,7 +1034,7 @@ ], [ -226, - -1000 + -999 ], [ -229, @@ -2400,170 +1046,50 @@ ], [ -922, + -445 + ], + [ + -923, -444 ] ] ] - ], + ] + ], + "difference" : [ [ [ [ [ - -2048, - 0 + -923, + -444 ], [ -922, - 444 - ], - [ - -1276, - 1601 - ], - [ - -227, - 998 - ], - [ - 455, - 1996 - ], - [ - 638, - 802 - ], - [ - 640, - 800 - ], - [ - 1845, - 888 - ], - [ - 1024, - 0 - ], - [ - 1845, - -888 - ], - [ - 640, - -800 - ], - [ - 638, - -802 - ], - [ - 455, - -1996 + -444 ], [ - -227, - -998 - ], + -922, + -445 + ] + ] + ], + [ + [ [ - -229, - -999 + -923, + 444 ], [ - -1276, - -1601 + -922, + 445 ], [ -922, - -444 + 444 ] ] - ] - ] - ], - "difference" : [ - [ - [ - [ - [ - -229, - -999 - ], - [ - -227, - -998 - ], - [ - -226, - -999 - ] - ] - ], - [ - [ - [ - -228, - 999 - ], - [ - -226, - 999 - ], - [ - -227, - 998 - ] - ] - ], - [ - [ - [ - 638, - -802 - ], - [ - 638, - -800 - ], - [ - 640, - -800 - ] - ] - ], - [ - [ - [ - 638, - 800 - ], - [ - 638, - 802 - ], - [ - 640, - 800 - ] - ] - ], - [ - [ - [ - 1024, - 0 - ], - [ - 1025, - 1 - ], - [ - 1025, - -1 - ] - ] - ] - ], - [ + ], [ [ [ @@ -2576,23 +1102,23 @@ ], [ -226, - -1000 + -999 ] ] ], [ [ [ - 638, - -802 + -228, + 999 ], [ - 638, - -800 + -226, + 999 ], [ - 640, - -800 + -227, + 998 ] ] ], @@ -2600,49 +1126,47 @@ [ [ 638, - 800 + -802 ], [ 638, - 802 + -800 ], [ 640, - 800 + -800 ] ] - ] - ], - [ + ], [ [ [ 638, - -802 + 800 ], [ 638, - -800 + 802 ], [ 640, - -800 + 800 ] ] ], [ [ [ - 638, - 800 + 1024, + 0 ], [ - 638, - 802 + 1025, + 1 ], [ - 640, - 800 + 1025, + -1 ] ] ] diff --git a/iOverlay/tests/fragment_tests.rs b/iOverlay/tests/fragment_tests.rs index f3357eb1..7340a99e 100644 --- a/iOverlay/tests/fragment_tests.rs +++ b/iOverlay/tests/fragment_tests.rs @@ -44,8 +44,9 @@ mod tests { }) .collect(); + // The exact even-odd area is 2.8 (area2 = 5.6); rounding the crossings to integers gives 6. for (strategy, shapes, area2) in &results { - assert_eq!(*area2, 7, "{strategy:?} lost part of the filled region"); + assert_eq!(*area2, 6, "{strategy:?} lost part of the filled region"); assert_eq!(shapes, &results[0].1, "{strategy:?} differs from List"); } } diff --git a/iOverlay/tests/issue_91_tests.rs b/iOverlay/tests/issue_91_tests.rs index 3ecff254..3459cad5 100644 --- a/iOverlay/tests/issue_91_tests.rs +++ b/iOverlay/tests/issue_91_tests.rs @@ -132,25 +132,55 @@ fn check_owner_case(ogc: bool, preserve_collinear: bool, vectors: bool) { overlay.overlay(OverlayRule::Union, FillRule::NonZero) }; assert_eq!(shapes.len(), squares + 2); - assert_hole_owner( - &shapes, - &[[-4, 3], [-4, 4], [-3, 2]], - &[ - [-6, 5], - [-10, 5], - [-4, 2], - [-5, 1], - [-2, 1], - [-2, 3], - [0, 4], - [-2, 4], - [-3, 3], - [-3, 5], - [-4, 5], - [-4, 6], - [-7, 6], - ], - ); + // The hole touches the outer boundary at (-3, 4), where input edges cross exactly. + // OGC output keeps it as a hole of the containing shape; ordinary output merges it + // into the outer contour, which passes through (-3, 4) twice. + let outer_with_pinch = [ + [-6, 5], + [-10, 5], + [-4, 2], + [-5, 1], + [-2, 1], + [-2, 3], + [0, 4], + [-3, 4], + [-3, 2], + [-4, 3], + [-4, 4], + [-3, 4], + [-3, 5], + [-4, 5], + [-4, 6], + [-7, 6], + ]; + if ogc { + assert_hole_owner( + &shapes, + &[[-4, 3], [-4, 4], [-3, 4], [-3, 2]], + &[ + [-6, 5], + [-10, 5], + [-4, 2], + [-5, 1], + [-2, 1], + [-2, 3], + [0, 4], + [-3, 4], + [-3, 5], + [-4, 5], + [-4, 6], + [-7, 6], + ], + ); + } else { + assert_eq!(shapes.iter().map(|s| s.len() - 1).sum::(), 0); + let outer = canonical(&contour(&outer_with_pinch)); + assert_eq!( + shapes.iter().filter(|s| canonical(&s[0]) == outer).count(), + 1, + "{shapes:?}" + ); + } } } } diff --git a/iOverlay/tests/slice_tests.rs b/iOverlay/tests/slice_tests.rs index f3914235..b51ccc39 100644 --- a/iOverlay/tests/slice_tests.rs +++ b/iOverlay/tests/slice_tests.rs @@ -322,10 +322,11 @@ mod tests { let result = path.slice_by_lines(&lines, FillRule::NonZero); - assert_eq!(result.len(), 3); + // The horizontal line cuts the triangle in two, at (-0.75, -1), which rounds to + // (-1, -1). The vertical line only touches the triangle at the vertex (-1, -2). + assert_eq!(result.len(), 2); assert_eq!(result[0].len(), 1); assert_eq!(result[1].len(), 1); - assert_eq!(result[2].len(), 1); } #[test] diff --git a/iOverlay/tests/snap_rounding_tests.rs b/iOverlay/tests/snap_rounding_tests.rs new file mode 100644 index 00000000..97f86125 --- /dev/null +++ b/iOverlay/tests/snap_rounding_tests.rs @@ -0,0 +1,96 @@ +//! Rounded crossings must not move segments further than rounding does. +//! +//! A crossing that rounds next to a segment endpoint is snapped to that endpoint, and the other +//! segment is split there. With integer inputs, that used to bend the other segment by up to a +//! unit or more, e.g. outside the polygon it bounds. + +use i_float::int::point::IntPoint; +use i_overlay::core::fill_rule::FillRule; +use i_overlay::core::overlay::{IntOverlayOptions, Overlay}; +use i_overlay::core::overlay_rule::OverlayRule; +use i_overlay::core::solver::Solver; +use i_shape::int::shape::IntShapes; + +const SOLVERS: [Solver; 4] = [Solver::LIST, Solver::TREE, Solver::FRAG, Solver::AUTO]; + +fn contour(points: &[[i32; 2]]) -> Vec { + points.iter().map(|&[x, y]| IntPoint::new(x, y)).collect() +} + +/// Shapes as sorted contours, each starting at its smallest point, for order-independent +/// comparison. +fn normalized(shapes: &IntShapes) -> Vec>> { + let mut shapes: Vec>> = shapes + .iter() + .map(|shape| { + shape + .iter() + .map(|path| { + let mut path = path.clone(); + let start = path.iter().enumerate().min_by_key(|(_, p)| **p).unwrap().0; + path.rotate_left(start); + path + }) + .collect() + }) + .collect(); + shapes.sort(); + shapes +} + +fn overlay( + subj: &[Vec], + clip: &[Vec], + rule: OverlayRule, + solver: Solver, +) -> IntShapes { + Overlay::from_subj_and_clip_custom(&subj.to_vec(), &clip.to_vec(), IntOverlayOptions::ogc(), solver) + .overlay(rule, FillRule::EvenOdd) +} + +#[test] +fn box_minus_collapsed_sliver_stays_inside_the_box() { + // The sliver's top edge crosses the box at (1, 2 1/11) and (2, 2 2/11), which round onto its + // bottom edge, so the sliver collapses over the box. Snapping these crossings to the sliver's + // vertex (0, 2) and the box's corner (2, 1) produced a triangle outside the box. + let boxes = [contour(&[[1, 1], [2, 1], [2, 4], [1, 4]])]; + let sliver = [contour(&[[0, 2], [11, 2], [11, 3]])]; + for solver in SOLVERS { + let difference = overlay(&boxes, &sliver, OverlayRule::Difference, solver); + assert_eq!( + normalized(&difference), + vec![vec![contour(&[[1, 1], [2, 1], [2, 4], [1, 4]])]], + "{:?}", + solver.strategy + ); + let intersection = overlay(&boxes, &sliver, OverlayRule::Intersect, solver); + assert!(intersection.is_empty(), "{:?}: {intersection:?}", solver.strategy); + } +} + +#[test] +fn boxes_minus_sliver_match_jts_snap_rounding() { + // JTS TestOverlayAAPrec.xml, "AA - sliver triangle with multiple intersecting boxes", + // difference with a fixed precision model of scale 1. + let boxes: Vec<_> = (0..5) + .map(|i| { + let x = 1 + 2 * i; + contour(&[[x, 1], [x + 1, 1], [x + 1, 4], [x, 4]]) + }) + .collect(); + let sliver = [contour(&[[0, 2], [11, 2], [11, 3]])]; + let expected = vec![ + vec![contour(&[[1, 1], [2, 1], [2, 4], [1, 4]])], + vec![contour(&[[3, 1], [4, 1], [4, 4], [3, 4]])], + vec![contour(&[[5, 1], [6, 1], [6, 2], [5, 2]])], + vec![contour(&[[5, 2], [6, 3], [6, 4], [5, 4]])], + vec![contour(&[[7, 1], [8, 1], [8, 2], [7, 2]])], + vec![contour(&[[7, 3], [8, 3], [8, 4], [7, 4]])], + vec![contour(&[[9, 1], [10, 1], [10, 2], [9, 2]])], + vec![contour(&[[9, 3], [10, 3], [10, 4], [9, 4]])], + ]; + for solver in SOLVERS { + let difference = overlay(&boxes, &sliver, OverlayRule::Difference, solver); + assert_eq!(normalized(&difference), expected, "{:?}", solver.strategy); + } +}