Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Avoid allocations in vertex of PolyArea #1135

Merged
merged 4 commits into from
Nov 18, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 13 additions & 1 deletion src/geometries/polytopes/polyarea.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down
2 changes: 1 addition & 1 deletion test/multigeoms.jl
Original file line number Diff line number Diff line change
Expand Up @@ -112,5 +112,5 @@
multi3 = Multi([poly3, poly4])
vertextest(multi1)
vertextest(multi2)
vertextest(multi3, bytes=3100)
vertextest(multi3)
end
20 changes: 19 additions & 1 deletion test/polytopes.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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)])
Expand Down
4 changes: 2 additions & 2 deletions test/testutils.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
Loading