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

Merged
merged 41 commits into from
Nov 18, 2024
Merged
Show file tree
Hide file tree
Changes from 7 commits
Commits
Show all changes
41 commits
Select commit Hold shift + click to select a range
a454ad4
initial draft for vertices iteration
ghyatzo Nov 12, 2024
9ca4f25
added small (temporary) comments for better initial digestion, will r…
ghyatzo Nov 12, 2024
7bd8de9
refactor into `eachvertex`
ghyatzo Nov 13, 2024
3275c8e
fix: typo in the export list
ghyatzo Nov 13, 2024
86eab4e
fix: Multi -> MultiPolytope, no importing of base methods
ghyatzo Nov 13, 2024
9a62f4f
revert to simple generators. Rewrite some function in term of the ite…
ghyatzo Nov 13, 2024
5e15a24
add tests, first part
ghyatzo Nov 13, 2024
7628b36
fix: change test
ghyatzo Nov 13, 2024
33c6c6f
typo
ghyatzo Nov 13, 2024
a473e57
fix: use first instead of getindex,
ghyatzo Nov 13, 2024
421141c
revive the custom iterator
ghyatzo Nov 15, 2024
bc985f6
add typecheck to avoid stack overflows.
ghyatzo Nov 17, 2024
f788fcf
better tests
ghyatzo Nov 17, 2024
8cf1604
fix tests
ghyatzo Nov 17, 2024
1770bf9
revert Multi constructor
ghyatzo Nov 17, 2024
34e825e
Update src/geometries/multigeom.jl
juliohm Nov 17, 2024
f716552
Update src/geometries/multigeom.jl
juliohm Nov 17, 2024
95138cb
Cleanup
juliohm Nov 17, 2024
3a455b7
*removed double definition of the `eachvertex` function
ghyatzo Nov 18, 2024
874016d
* improved allocation testing
ghyatzo Nov 18, 2024
14fdf6e
Update src/geometries/multigeom.jl
ghyatzo Nov 18, 2024
21b5aa2
Update src/geometries/multigeom.jl
ghyatzo Nov 18, 2024
1739d4b
Update test/meshes.jl
ghyatzo Nov 18, 2024
ab28a90
Update test/meshes.jl
ghyatzo Nov 18, 2024
d3fc076
Update test/meshes.jl
ghyatzo Nov 18, 2024
fe4302b
Update test/meshes.jl
ghyatzo Nov 18, 2024
0d1394b
Update test/testutils.jl
ghyatzo Nov 18, 2024
7270456
Update test/polytopes.jl
ghyatzo Nov 18, 2024
f9cb83d
Update test/polytopes.jl
ghyatzo Nov 18, 2024
11678f5
Update test/polytopes.jl
ghyatzo Nov 18, 2024
780b7b0
Update test/multigeoms.jl
ghyatzo Nov 18, 2024
23371b4
Update test/meshes.jl
ghyatzo Nov 18, 2024
c30d340
Apply suggestions from code review
eliascarv Nov 18, 2024
e80e155
Add more tests
eliascarv Nov 18, 2024
7058cff
Update tests
eliascarv Nov 18, 2024
0dd41fa
Simplify tests
eliascarv Nov 18, 2024
15fd7aa
Simplify tests
eliascarv Nov 18, 2024
5d96dcf
Apply suggestions
eliascarv Nov 18, 2024
700c46b
Update test/testutils.jl
eliascarv Nov 18, 2024
7218ba0
Apply suggestions
eliascarv Nov 18, 2024
6c5e330
Fix tests
eliascarv Nov 18, 2024
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
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 in the `mesh`.
ghyatzo marked this conversation as resolved.
Show resolved Hide resolved
"""
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) = getindex(Base.iterate(Base.Iterators.drop(eachvertex(m), ind - 1)), 1) # nth(itr, n)
juliohm marked this conversation as resolved.
Show resolved Hide resolved

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
ghyatzo marked this conversation as resolved.
Show resolved Hide resolved
@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)
ghyatzo marked this conversation as resolved.
Show resolved Hide resolved
@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)]
ghyatzo marked this conversation as resolved.
Show resolved Hide resolved
@test area(multi) == area(poly1) + area(poly2)
@test perimeter(multi) == perimeter(poly1) + perimeter(poly2)
@test centroid(multi) == cart(1, 1)
Expand Down
4 changes: 4 additions & 0 deletions test/polytopes.jl
Original file line number Diff line number Diff line change
Expand Up @@ -302,6 +302,10 @@ end
@test vertices(Ngon{3}(pts...)) == verts
@test vertices(Ngon{3}(tups...)) == verts

@test collect(eachvertex(Ngon(pts))) == verts
@test @allocated eachvertex(Ngon(pts)) == 0
@test @allocated Base.iterate(eachvertex(Ngon(pts))) == 0
juliohm marked this conversation as resolved.
Show resolved Hide resolved

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