diff --git a/src/geometries/polytopes/polyarea.jl b/src/geometries/polytopes/polyarea.jl index 16c912b90..4980b9f91 100644 --- a/src/geometries/polytopes/polyarea.jl +++ b/src/geometries/polytopes/polyarea.jl @@ -31,7 +31,19 @@ PolyArea(outer...) = PolyArea(collect(outer)) Base.isapprox(p₁::PolyArea, p₂::PolyArea; atol=atol(lentype(p₁)), kwargs...) = length(p₁.rings) == length(p₂.rings) && all(isapprox(r₁, r₂; atol, kwargs...) for (r₁, r₂) in zip(p₁.rings, p₂.rings)) -vertices(p::PolyArea) = mapreduce(vertices, vcat, p.rings) +function vertex(p::PolyArea, ind) + offset = 0 + for r in p.rings + nverts = nvertices(r) + if ind ≤ offset + nverts + return vertex(r, ind - offset) + end + offset += nverts + end + throw(BoundsError(p, ind)) +end + +vertices(p::PolyArea) = collect(eachvertex(p)) nvertices(p::PolyArea) = mapreduce(nvertices, +, p.rings) diff --git a/test/multigeoms.jl b/test/multigeoms.jl index 668582690..5b1dddd6b 100644 --- a/test/multigeoms.jl +++ b/test/multigeoms.jl @@ -112,5 +112,5 @@ multi3 = Multi([poly3, poly4]) vertextest(multi1) vertextest(multi2) - vertextest(multi3, bytes=3100) + vertextest(multi3) end diff --git a/test/polytopes.jl b/test/polytopes.jl index 2f3f26ea8..2a50b8baa 100644 --- a/test/polytopes.jl +++ b/test/polytopes.jl @@ -652,9 +652,27 @@ end @test centroid(poly) == cart(0.5, 0.5) # single vertex access - poly = PolyArea(cart.([(0, 0), (1, 0), (1, 1), (0, 1)])) + outer = cart.([(0, 0), (1, 0), (1, 1), (0, 1)]) + hole1 = cart.([(0.2, 0.2), (0.4, 0.2), (0.4, 0.4), (0.2, 0.4)]) + hole2 = cart.([(0.6, 0.2), (0.8, 0.2), (0.8, 0.4), (0.6, 0.4)]) + poly = PolyArea([outer, hole1, hole2]) @test vertex(poly, 1) == cart(0, 0) + @test vertex(poly, 2) == cart(1, 0) + @test vertex(poly, 3) == cart(1, 1) @test vertex(poly, 4) == cart(0, 1) + @test vertex(poly, 5) == cart(0.2, 0.2) + @test vertex(poly, 6) == cart(0.4, 0.2) + @test vertex(poly, 7) == cart(0.4, 0.4) + @test vertex(poly, 8) == cart(0.2, 0.4) + @test vertex(poly, 9) == cart(0.6, 0.2) + @test vertex(poly, 10) == cart(0.8, 0.2) + @test vertex(poly, 11) == cart(0.8, 0.4) + @test vertex(poly, 12) == cart(0.6, 0.4) + @test_throws BoundsError vertex(poly, 13) + # type stability + @inferred vertex(poly, 4) + @inferred vertex(poly, 8) + @inferred vertex(poly, 12) # point in polygonal area outer = cart.([(0, 0), (1, 0), (1, 1), (0, 1)]) diff --git a/test/testutils.jl b/test/testutils.jl index 9fbcf8f90..26c233dcf 100644 --- a/test/testutils.jl +++ b/test/testutils.jl @@ -183,9 +183,9 @@ function eachvertexalloc(g) end end -function vertextest(g; bytes=0) +function vertextest(g) @test collect(eachvertex(g)) == vertices(g) - @test eachvertexalloc(g) ≤ bytes + @test eachvertexalloc(g) == 0 # type stability @test isconcretetype(eltype(vertices(g))) @inferred vertices(g)