Skip to content

Commit b93e7c4

Browse files
eliascarvjuliohm
andauthored
Avoid allocations in vertex of PolyArea (#1135)
* Avoid allocations in 'vertex' of 'PolyArea' * Update 'vertextest' * Update src/geometries/polytopes/polyarea.jl Co-authored-by: Júlio Hoffimann <[email protected]> * Update 'vertices' of 'PolyArea' --------- Co-authored-by: Júlio Hoffimann <[email protected]>
1 parent 82c995f commit b93e7c4

File tree

4 files changed

+35
-5
lines changed

4 files changed

+35
-5
lines changed

src/geometries/polytopes/polyarea.jl

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,19 @@ PolyArea(outer...) = PolyArea(collect(outer))
3131
Base.isapprox(p₁::PolyArea, p₂::PolyArea; atol=atol(lentype(p₁)), kwargs...) =
3232
length(p₁.rings) == length(p₂.rings) && all(isapprox(r₁, r₂; atol, kwargs...) for (r₁, r₂) in zip(p₁.rings, p₂.rings))
3333

34-
vertices(p::PolyArea) = mapreduce(vertices, vcat, p.rings)
34+
function vertex(p::PolyArea, ind)
35+
offset = 0
36+
for r in p.rings
37+
nverts = nvertices(r)
38+
if ind offset + nverts
39+
return vertex(r, ind - offset)
40+
end
41+
offset += nverts
42+
end
43+
throw(BoundsError(p, ind))
44+
end
45+
46+
vertices(p::PolyArea) = collect(eachvertex(p))
3547

3648
nvertices(p::PolyArea) = mapreduce(nvertices, +, p.rings)
3749

test/multigeoms.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -112,5 +112,5 @@
112112
multi3 = Multi([poly3, poly4])
113113
vertextest(multi1)
114114
vertextest(multi2)
115-
vertextest(multi3, bytes=3100)
115+
vertextest(multi3)
116116
end

test/polytopes.jl

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -652,9 +652,27 @@ end
652652
@test centroid(poly) == cart(0.5, 0.5)
653653

654654
# single vertex access
655-
poly = PolyArea(cart.([(0, 0), (1, 0), (1, 1), (0, 1)]))
655+
outer = cart.([(0, 0), (1, 0), (1, 1), (0, 1)])
656+
hole1 = cart.([(0.2, 0.2), (0.4, 0.2), (0.4, 0.4), (0.2, 0.4)])
657+
hole2 = cart.([(0.6, 0.2), (0.8, 0.2), (0.8, 0.4), (0.6, 0.4)])
658+
poly = PolyArea([outer, hole1, hole2])
656659
@test vertex(poly, 1) == cart(0, 0)
660+
@test vertex(poly, 2) == cart(1, 0)
661+
@test vertex(poly, 3) == cart(1, 1)
657662
@test vertex(poly, 4) == cart(0, 1)
663+
@test vertex(poly, 5) == cart(0.2, 0.2)
664+
@test vertex(poly, 6) == cart(0.4, 0.2)
665+
@test vertex(poly, 7) == cart(0.4, 0.4)
666+
@test vertex(poly, 8) == cart(0.2, 0.4)
667+
@test vertex(poly, 9) == cart(0.6, 0.2)
668+
@test vertex(poly, 10) == cart(0.8, 0.2)
669+
@test vertex(poly, 11) == cart(0.8, 0.4)
670+
@test vertex(poly, 12) == cart(0.6, 0.4)
671+
@test_throws BoundsError vertex(poly, 13)
672+
# type stability
673+
@inferred vertex(poly, 4)
674+
@inferred vertex(poly, 8)
675+
@inferred vertex(poly, 12)
658676

659677
# point in polygonal area
660678
outer = cart.([(0, 0), (1, 0), (1, 1), (0, 1)])

test/testutils.jl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -183,9 +183,9 @@ function eachvertexalloc(g)
183183
end
184184
end
185185

186-
function vertextest(g; bytes=0)
186+
function vertextest(g)
187187
@test collect(eachvertex(g)) == vertices(g)
188-
@test eachvertexalloc(g) bytes
188+
@test eachvertexalloc(g) == 0
189189
# type stability
190190
@test isconcretetype(eltype(vertices(g)))
191191
@inferred vertices(g)

0 commit comments

Comments
 (0)