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

Introducing iteration on vertices #1133

Open
wants to merge 10 commits into
base: master
Choose a base branch
from
1 change: 1 addition & 0 deletions src/Meshes.jl
Original file line number Diff line number Diff line change
Expand Up @@ -559,6 +559,7 @@ export
measurematrix,
adjacencymatrix,
atol,
eachvertex,

# visualization
viz,
Expand Down
9 changes: 8 additions & 1 deletion src/domains/meshes.jl
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ function vertex end

Return the vertices of the `mesh`.
"""
vertices(m::Mesh) = [vertex(m, ind) for ind in 1:nvertices(m)]
vertices(m::Mesh) = collect(eachvertex(m))

"""
nvertices(mesh)
Expand All @@ -32,6 +32,13 @@ Return the number of vertices of the `mesh`.
"""
nvertices(m::Mesh) = nvertices(topology(m))

"""
eachvertex(mesh)

Return an iterator for the vertices of the `mesh`.
"""
eachvertex(m::Mesh) = (vertex(m, ind) for ind in 1:nvertices(m))

"""
faces(mesh, rank)

Expand Down
6 changes: 4 additions & 2 deletions src/geometries/multigeom.jl
Original file line number Diff line number Diff line change
Expand Up @@ -48,12 +48,14 @@ Base.isapprox(m₁::Multi, m₂::Multi; atol=atol(lentype(m₁)), kwargs...) =
# POLYTOPE
# ---------

vertex(m::MultiPolytope, ind) = vertices(m)[ind]
vertex(m::MultiPolytope, ind) = first(Base.Iterators.drop(eachvertex(m), ind - 1)) # nth(itr, n)

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

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

eachvertex(m::MultiPolytope) = (v for g in m.geoms for v in vertices(g))

Base.unique(m::MultiPolytope) = unique!(deepcopy(m))

function Base.unique!(m::MultiPolytope)
Expand Down
7 changes: 7 additions & 0 deletions src/geometries/polytopes.jl
Original file line number Diff line number Diff line change
Expand Up @@ -251,6 +251,13 @@ Return the number of vertices in the `polytope`.
"""
nvertices(p::Polytope) = nvertices(typeof(p))

"""
eachvertex(polytope)

Return an iterator for the vertices in the `polytope`
"""
eachvertex(p::Polytope) = (v for v in vertices(p))

"""
unique(polytope)

Expand Down
3 changes: 3 additions & 0 deletions test/meshes.jl
Original file line number Diff line number Diff line change
Expand Up @@ -789,6 +789,7 @@ end
@test crs(mesh) <: Cartesian{NoDatum}
@test Meshes.lentype(mesh) == ℳ
@test vertices(mesh) == points
@test collect(eachvertex(mesh)) == points
@test collect(faces(mesh, 2)) == triangles
@test collect(elements(mesh)) == triangles
@test nelements(mesh) == 4
Expand Down Expand Up @@ -873,13 +874,15 @@ end
mesh₂ = SimpleMesh(cart.([(1, 0), (1, 1), (0, 1)]), connect.([(1, 2, 3)]))
mesh = merge(mesh₁, mesh₂)
@test vertices(mesh) == [vertices(mesh₁); vertices(mesh₂)]
@test collect(eachvertex(mesh)) == vertices(mesh)
@test collect(elements(topology(mesh))) == connect.([(1, 2, 3), (4, 5, 6)])

# merge operation with 3D geometries
mesh₁ = SimpleMesh(cart.([(0, 0, 0), (1, 0, 0), (0, 1, 0), (0, 0, 1)]), connect.([(1, 2, 3, 4)], Tetrahedron))
mesh₂ = SimpleMesh(cart.([(1, 0, 0), (1, 1, 0), (0, 1, 0), (1, 1, 1)]), connect.([(1, 2, 3, 4)], Tetrahedron))
mesh = merge(mesh₁, mesh₂)
@test vertices(mesh) == [vertices(mesh₁); vertices(mesh₂)]
@test collect(eachvertex(mesh)) == vertices(mesh)
@test collect(elements(topology(mesh))) == connect.([(1, 2, 3, 4), (5, 6, 7, 8)], Tetrahedron)

# convert any mesh to SimpleMesh
Expand Down
2 changes: 2 additions & 0 deletions test/multigeoms.jl
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
@test vertex(multi, 1) == vertex(poly, 1)
@test vertices(multi) == [vertices(poly); vertices(poly)]
@test nvertices(multi) == nvertices(poly) + nvertices(poly)
@test collect(eachvertex(multi)) == [vertices(poly); vertices(poly)]
@test boundary(multi) == merge(boundary(poly), boundary(poly))
@test rings(multi) == [rings(poly); rings(poly)]

Expand All @@ -20,6 +21,7 @@
multi = Multi([poly1, poly2])
@test vertices(multi) == [vertices(poly1); vertices(poly2)]
@test nvertices(multi) == nvertices(poly1) + nvertices(poly2)
@test collect(eachvertex(multi)) == [vertices(poly1); vertices(poly2)]
@test area(multi) == area(poly1) + area(poly2)
@test perimeter(multi) == perimeter(poly1) + perimeter(poly2)
@test centroid(multi) == cart(1, 1)
Expand Down
7 changes: 7 additions & 0 deletions test/polytopes.jl
Original file line number Diff line number Diff line change
Expand Up @@ -302,6 +302,13 @@ end
@test vertices(Ngon{3}(pts...)) == verts
@test vertices(Ngon{3}(tups...)) == verts

@test collect(eachvertex(Ngon(pts))) == verts
@test @allocated begin
for v in eachvertex(Ngon(pts))
# ...
end
end 0

NGONS = [Triangle, Quadrangle, Pentagon, Hexagon, Heptagon, Octagon, Nonagon, Decagon]
NVERT = 3:10
for (i, NGON) in enumerate(NGONS)
Expand Down
Loading