diff --git a/CHANGELOG.md b/CHANGELOG.md index b10f830a6..ad663aac7 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -144,7 +144,10 @@ The format of this changelog is based on - `isapprox` on arrays of `Point`s, and so on `Polygon`s, returns `false` when the arrays differ in size. Previously it compared only the elements both arrays have, so - an empty array was approximately equal to any other. (#300) + an empty array was approximately equal to any other. + - `==` and `isapprox` on `ClippedPolygon`s return `false` when the polygon trees have + different numbers of children at any level, such as an extra disjoint shape or an extra + hole. Previously, extra children on either side were ignored. - Schematic `render!` to a `Cell` or a `SolidModel` with `strict=:no` logs a component whose geometry fails to build and renders it as empty geometry, instead of throwing. (#287) diff --git a/src/polygons.jl b/src/polygons.jl index fc05383f7..b4c33541f 100644 --- a/src/polygons.jl +++ b/src/polygons.jl @@ -437,6 +437,7 @@ ClippedPolygon{T}(x::Clipper.PolyNode{Point}) where {T} = function ==(p1::ClippedPolygon, p2::ClippedPolygon) function children_equal(l, r) # recursively check contours of all children in the tree + length(Clipper.children(l)) == length(Clipper.children(r)) || return false for (x, y) in zip(Clipper.children(l), Clipper.children(r)) (!circularequality(x.contour, y.contour) || !children_equal(x, y)) && return false @@ -449,6 +450,7 @@ end function isapprox(p1::ClippedPolygon, p2::ClippedPolygon; kwargs...) function children_approx(l, r) # recursively check contours of all children in the tree + length(Clipper.children(l)) == length(Clipper.children(r)) || return false for (x, y) in zip(Clipper.children(l), Clipper.children(r)) (!circularapprox(x.contour, y.contour; kwargs...) || !children_approx(x, y)) && return false diff --git a/test/test_clipping.jl b/test/test_clipping.jl index 90ced5064..25ca22add 100644 --- a/test/test_clipping.jl +++ b/test/test_clipping.jl @@ -414,6 +414,23 @@ @test d2 == difference2d(d2, [d1]) @test d2 == difference2d([d2], d1) @test d2 == difference2d([d2], [d1]) + + # Trees with different numbers of children, at the top level or below, are unequal + sq = Rectangle(T(10), T(10)) + a = union2d(sq) + b = union2d([sq, Rectangle(Point(T(20), T(0)), Point(T(30), T(10)))]) + hole = Rectangle(Point(T(2), T(2)), Point(T(8), T(8))) + holed = difference2d(sq, hole) + e = intersect2d(d1, d2) # empty + for (p, q) in ((a, b), (a, holed), (a, e)) + @test p != q + @test q != p + @test !isapprox(p, q) + @test !isapprox(q, p) + end + @test a == union2d(sq) + @test holed ≈ difference2d(sq, hole) + @test e == intersect2d(r1, r2 + Point(T(5), T(5))) end r1 = Rectangle(2, 2)