Skip to content

Commit 82c995f

Browse files
ghyatzojuliohmeliascarv
authored
Introducing iteration on vertices (#1133)
* initial draft for vertices iteration * added small (temporary) comments for better initial digestion, will remove later * refactor into `eachvertex` * fix: typo in the export list * fix: Multi -> MultiPolytope, no importing of base methods * revert to simple generators. Rewrite some function in term of the iterator instead. * add tests, first part * fix: change test * typo Co-authored-by: Júlio Hoffimann <[email protected]> * fix: use first instead of getindex, * revive the custom iterator * add typecheck to avoid stack overflows. * better tests * fix tests * revert Multi constructor * Update src/geometries/multigeom.jl * Update src/geometries/multigeom.jl * Cleanup * *removed double definition of the `eachvertex` function *eltype returns the specific point based on manifold and tranformation. *added missing `IteratorEltype` trait * * improved allocation testing * small cleanup * Update src/geometries/multigeom.jl Co-authored-by: Elias Carvalho <[email protected]> * Update src/geometries/multigeom.jl Co-authored-by: Elias Carvalho <[email protected]> * Update test/meshes.jl Co-authored-by: Elias Carvalho <[email protected]> * Update test/meshes.jl Co-authored-by: Elias Carvalho <[email protected]> * Update test/meshes.jl Co-authored-by: Elias Carvalho <[email protected]> * Update test/meshes.jl Co-authored-by: Elias Carvalho <[email protected]> * Update test/testutils.jl Co-authored-by: Elias Carvalho <[email protected]> * Update test/polytopes.jl Co-authored-by: Elias Carvalho <[email protected]> * Update test/polytopes.jl Co-authored-by: Elias Carvalho <[email protected]> * Update test/polytopes.jl Co-authored-by: Elias Carvalho <[email protected]> * Update test/multigeoms.jl Co-authored-by: Elias Carvalho <[email protected]> * Update test/meshes.jl Co-authored-by: Elias Carvalho <[email protected]> * Apply suggestions from code review * Add more tests * Update tests * Simplify tests * Simplify tests * Apply suggestions * Update test/testutils.jl Co-authored-by: Júlio Hoffimann <[email protected]> * Apply suggestions * Fix tests --------- Co-authored-by: Júlio Hoffimann <[email protected]> Co-authored-by: Elias Carvalho <[email protected]> Co-authored-by: Elias Carvalho <[email protected]>
1 parent 729c46a commit 82c995f

File tree

8 files changed

+92
-4
lines changed

8 files changed

+92
-4
lines changed

src/Meshes.jl

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -221,6 +221,7 @@ export
221221
vertex,
222222
vertices,
223223
nvertices,
224+
eachvertex,
224225
rings,
225226
segments,
226227
angles,
@@ -313,6 +314,7 @@ export
313314
vertex,
314315
vertices,
315316
nvertices,
317+
eachvertex,
316318
element,
317319
elements,
318320
nelements,

src/domains/meshes.jl

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ function vertex end
2323
2424
Return the vertices of the `mesh`.
2525
"""
26-
vertices(m::Mesh) = [vertex(m, ind) for ind in 1:nvertices(m)]
26+
vertices(m::Mesh) = collect(eachvertex(m))
2727

2828
"""
2929
nvertices(mesh)
@@ -32,6 +32,13 @@ Return the number of vertices of the `mesh`.
3232
"""
3333
nvertices(m::Mesh) = nvertices(topology(m))
3434

35+
"""
36+
eachvertex(mesh)
37+
38+
Return an iterator for the vertices of the `mesh`.
39+
"""
40+
eachvertex(m::Mesh) = (vertex(m, i) for i in 1:nvertices(m))
41+
3542
"""
3643
faces(mesh, rank)
3744

src/geometries/multigeom.jl

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,12 +48,14 @@ Base.isapprox(m₁::Multi, m₂::Multi; atol=atol(lentype(m₁)), kwargs...) =
4848
# POLYTOPE
4949
# ---------
5050

51-
vertex(m::MultiPolytope, ind) = vertices(m)[ind]
51+
vertex(m::MultiPolytope, ind) = first(Iterators.drop(eachvertex(m), ind - 1))
5252

53-
vertices(m::MultiPolytope) = [vertex for geom in m.geoms for vertex in vertices(geom)]
53+
vertices(m::MultiPolytope) = collect(eachvertex(m))
5454

5555
nvertices(m::MultiPolytope) = sum(nvertices, m.geoms)
5656

57+
eachvertex(m::MultiPolytope) = (v for g in m.geoms for v in eachvertex(g))
58+
5759
Base.unique(m::MultiPolytope) = unique!(deepcopy(m))
5860

5961
function Base.unique!(m::MultiPolytope)

src/geometries/polytopes.jl

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -247,10 +247,17 @@ vertices(p::Polytope) = p.vertices
247247
"""
248248
nvertices(polytope)
249249
250-
Return the number of vertices in the `polytope`.
250+
Return the number of vertices of the `polytope`.
251251
"""
252252
nvertices(p::Polytope) = nvertices(typeof(p))
253253

254+
"""
255+
eachvertex(polytope)
256+
257+
Return an iterator for the vertices of the `polytope`.
258+
"""
259+
eachvertex(p::Polytope) = (vertex(p, i) for i in 1:nvertices(p))
260+
254261
"""
255262
unique(polytope)
256263

test/meshes.jl

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,10 @@
127127
@test minimum(sub) == Point(Polar(T(1), T(2)))
128128
@test maximum(sub) == Point(Polar(T(4), T(7)))
129129

130+
# vertex iteration
131+
grid = RegularGrid((10, 10), cart(0, 0), T.((1, 1)))
132+
vertextest(grid)
133+
130134
# type stability
131135
grid = RegularGrid((10, 20), Point(Polar(T(0), T(0))), T.((1, 1)))
132136
@inferred vertex(grid, (1, 1))
@@ -501,6 +505,12 @@ end
501505
@test topology(rg) == topology(cg)
502506
@test vertices(rg) == vertices(cg)
503507

508+
# vertex iteration
509+
x = range(zero(T), stop=one(T), length=6)
510+
y = T[0.0, 0.1, 0.3, 0.7, 0.9, 1.0]
511+
grid = RectilinearGrid(x, y)
512+
vertextest(grid)
513+
504514
# type stability
505515
x = range(zero(T), stop=one(T), length=6) * u"mm"
506516
y = T[0.0, 0.1, 0.3, 0.7, 0.9, 1.0] * u"cm"
@@ -682,6 +692,12 @@ end
682692
@test topology(sg) == topology(rg)
683693
@test vertices(sg) == vertices(rg)
684694

695+
# vertex iteration
696+
X = repeat(range(zero(T), stop=one(T), length=6), 1, 6)
697+
Y = repeat(T[0.0, 0.1, 0.3, 0.7, 0.9, 1.0]', 6, 1)
698+
grid = StructuredGrid(X, Y)
699+
vertextest(grid)
700+
685701
# type stability
686702
X = repeat(range(zero(T), stop=one(T), length=6), 1, 6) * u"mm"
687703
Y = repeat(T[0.0, 0.1, 0.3, 0.7, 0.9, 1.0]', 6, 1) * u"cm"
@@ -929,6 +945,12 @@ end
929945
@test vertex(mesh, 4) == points[4]
930946
@test vertex(mesh, 5) == points[5]
931947

948+
# vertex iteration
949+
points = cart.([(0, 0), (1, 0), (0, 1), (1, 1), (0.5, 0.5)])
950+
connec = connect.([(1, 2, 5), (2, 4, 5), (4, 3, 5), (3, 1, 5)], Triangle)
951+
mesh = SimpleMesh(points, connec)
952+
vertextest(mesh)
953+
932954
points = cart.([(0, 0), (1, 0), (0, 1), (1, 1), (0.5, 0.5)])
933955
connec = connect.([(1, 2, 5), (2, 4, 5), (4, 3, 5), (3, 1, 5)], Triangle)
934956
mesh = SimpleMesh(points, connec)
@@ -988,6 +1010,14 @@ end
9881010
trans2 = Translate(T(-10), T(-10))
9891011
@test TransformedMesh(TransformedMesh(grid, trans1), trans2) == TransformedMesh(grid, trans1 trans2)
9901012

1013+
# vertex iteration
1014+
trans = Identity()
1015+
points = latlon.([(0, 0), (0, 1), (1, 0), (1, 1), (0.5, 0.5)])
1016+
connec = connect.([(1, 2, 5), (2, 4, 5), (4, 3, 5), (3, 1, 5)], Triangle)
1017+
mesh = SimpleMesh(points, connec)
1018+
tmesh = TransformedMesh(mesh, trans)
1019+
vertextest(tmesh)
1020+
9911021
# transforms that change the Manifold and/or CRS
9921022
points = latlon.([(0, 0), (0, 1), (1, 0), (1, 1), (0.5, 0.5)])
9931023
connec = connect.([(1, 2, 5), (2, 4, 5), (4, 3, 5), (3, 1, 5)], Triangle)

test/multigeoms.jl

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,4 +97,20 @@
9797
poly2 = PolyArea(merc.([(1, 1), (2, 1), (2, 2), (1, 2)]))
9898
multi = Multi([poly1, poly2])
9999
@test crs(centroid(multi)) === crs(multi)
100+
101+
# vertex iteration
102+
ring1 = Ring(cart.([(0, 0), (1, 0), (1, 1), (0, 1)]))
103+
ring2 = Ring(cart.([(0, 0), (2, 0), (2, 2), (0, 2)]))
104+
ring3 = Ring(cart.([(0.2, 0.2), (0.4, 0.2), (0.4, 0.4), (0.2, 0.4)]))
105+
ring4 = Ring(cart.([(0.6, 0.2), (0.8, 0.2), (0.8, 0.4), (0.6, 0.4)]))
106+
poly1 = PolyArea(ring1)
107+
poly2 = PolyArea(ring2)
108+
poly3 = PolyArea([ring1, ring3])
109+
poly4 = PolyArea([ring2, ring4])
110+
multi1 = Multi([ring1, ring2, ring3, ring4])
111+
multi2 = Multi([poly1, poly2])
112+
multi3 = Multi([poly3, poly4])
113+
vertextest(multi1)
114+
vertextest(multi2)
115+
vertextest(multi3, bytes=3100)
100116
end

test/polytopes.jl

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@
4242
s = Segment(cart(0, 0), cart(1, 1))
4343
equaltest(s)
4444
isapproxtest(s)
45+
vertextest(s)
4546

4647
s = Segment(Point(1.0, 1.0, 1.0, 1.0), Point(2.0, 2.0, 2.0, 2.0))
4748
@test all(Point(x, x, x, x) s for x in 1:0.01:2)
@@ -118,10 +119,12 @@ end
118119
c = Rope(cart(0, 0), cart(1, 0), cart(0, 1))
119120
equaltest(c)
120121
isapproxtest(c)
122+
vertextest(c)
121123

122124
c = Ring(cart(0, 0), cart(1, 0), cart(0, 1))
123125
equaltest(c)
124126
isapproxtest(c)
127+
vertextest(c)
125128

126129
# circular equality
127130
c1 = Ring(cart.([(1, 1), (2, 2), (3, 3)]))
@@ -352,6 +355,7 @@ end
352355
t = Triangle(cart(0, 0), cart(1, 0), cart(0, 1))
353356
equaltest(t)
354357
isapproxtest(t)
358+
vertextest(t)
355359

356360
t = Triangle(cart(0, 0), cart(1, 0), cart(0, 1))
357361
@test perimeter(t) T(1 + 1 + 2) * u"m"
@@ -466,6 +470,7 @@ end
466470
q = Quadrangle(cart(0, 0), cart(1, 0), cart(1, 1), cart(0, 1))
467471
equaltest(q)
468472
isapproxtest(q)
473+
vertextest(q)
469474

470475
q = Quadrangle(cart(0, 0), cart(1, 0), cart(1, 1), cart(0, 1))
471476
@test_throws DomainError((T(1.2), T(1.2)), "q(u, v) is not defined for u, v outside [0, 1]².") q(T(1.2), T(1.2))
@@ -537,6 +542,7 @@ end
537542
p = PolyArea(cart(0, 0), cart(1, 0), cart(0, 1))
538543
equaltest(p)
539544
isapproxtest(p)
545+
vertextest(p)
540546

541547
# COMMAND USED TO GENERATE TEST FILES (VARY --seed = 1, 2, ..., 5)
542548
# rpg --cluster 30 --algo 2opt --format line --seed 1 --output poly1
@@ -721,6 +727,7 @@ end
721727
t = Tetrahedron(cart(0, 0, 0), cart(1, 0, 0), cart(0, 1, 0), cart(0, 0, 1))
722728
equaltest(t)
723729
isapproxtest(t)
730+
vertextest(t)
724731

725732
# CRS propagation
726733
c1 = Cartesian{WGS84Latest}(T(0), T(0), T(0))
@@ -786,6 +793,7 @@ end
786793
)
787794
equaltest(h)
788795
isapproxtest(h)
796+
vertextest(t)
789797

790798
h = Hexahedron(
791799
cart(0, 0, 0),
@@ -904,6 +912,7 @@ end
904912
@test m[5] isa Triangle
905913
equaltest(p)
906914
isapproxtest(p)
915+
vertextest(p)
907916

908917
p = Pyramid(cart(0, 0, 0), cart(1, 0, 0), cart(1, 1, 0), cart(0, 1, 0), cart(0, 0, 1))
909918
@test sprint(show, p) == "Pyramid((x: 0.0 m, y: 0.0 m, z: 0.0 m), ..., (x: 0.0 m, y: 0.0 m, z: 1.0 m))"
@@ -942,6 +951,7 @@ end
942951
@test m[5] isa Quadrangle
943952
equaltest(w)
944953
isapproxtest(w)
954+
vertextest(w)
945955

946956
w = Wedge(cart(0, 0, 0), cart(1, 0, 0), cart(0, 1, 0), cart(0, 0, 1), cart(1, 0, 1), cart(0, 1, 1))
947957
@test sprint(show, w) == "Wedge((x: 0.0 m, y: 0.0 m, z: 0.0 m), ..., (x: 0.0 m, y: 1.0 m, z: 1.0 m))"

test/testutils.jl

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -176,3 +176,17 @@ function _isapproxtest(g::Geometry, ::Val{3})
176176
@test isapprox(g, Translate(0u"m", τ32, 0u"m")(g32), atol=1.1τ32)
177177
@test isapprox(g, Translate(0u"m", 0u"m", τ32)(g32), atol=1.1τ32)
178178
end
179+
180+
function eachvertexalloc(g)
181+
iterate(eachvertex(g)) # precompile run
182+
@allocated for _ in eachvertex(g)
183+
end
184+
end
185+
186+
function vertextest(g; bytes=0)
187+
@test collect(eachvertex(g)) == vertices(g)
188+
@test eachvertexalloc(g) bytes
189+
# type stability
190+
@test isconcretetype(eltype(vertices(g)))
191+
@inferred vertices(g)
192+
end

0 commit comments

Comments
 (0)