From a454ad4810b15618cf6efdace73e5e7dbae23143 Mon Sep 17 00:00:00 2001 From: cschen Date: Tue, 12 Nov 2024 23:42:47 +0100 Subject: [PATCH 01/41] initial draft for vertices iteration --- src/Meshes.jl | 8 +++++--- src/iteration.jl | 26 ++++++++++++++++++++++++++ 2 files changed, 31 insertions(+), 3 deletions(-) create mode 100644 src/iteration.jl diff --git a/src/Meshes.jl b/src/Meshes.jl index e7b067e91..5109aecf5 100644 --- a/src/Meshes.jl +++ b/src/Meshes.jl @@ -37,6 +37,7 @@ import Base: sort import Base: ==, ! import Base: +, -, * import Base: <, >, ≤, ≥ +import Base: iterate, length, IteratorSize, eltype import StatsBase: sample import Distances: evaluate import NearestNeighbors: MinkowskiMetric @@ -130,6 +131,7 @@ include("distances.jl") include("supportfun.jl") include("matrices.jl") include("projecting.jl") +include("iteration.jl") # visualization include("viz.jl") @@ -559,9 +561,9 @@ export measurematrix, adjacencymatrix, atol, + verticesiter - # visualization - viz, - viz! +# visualization +viz, viz! end # module diff --git a/src/iteration.jl b/src/iteration.jl new file mode 100644 index 000000000..fdccd0208 --- /dev/null +++ b/src/iteration.jl @@ -0,0 +1,26 @@ +struct VertexItr{T} + el::T +end + +Base.iterate(itr::VertexItr{T}, i=1) where {T<:Polytope} = + (@inline; (i - 1) % UInt < nvertices(itr.el) % UInt ? (@inbounds vertex(itr.el, i), i + 1) : nothing) + +Base.iterate(itr::VertexItr{T}, state=(1, 1)) where {T<:Multi} = begin + ig, ivg = state + ig > length(itr.el.geoms) && return nothing + + is = iterate(itr.el.geoms[ig], ivg) + isnothing(is) && return iterate(itr, (ig + 1, 1)) + + v, ivg = is + return (v, (ig, ivg)) +end + +Base.IteratorSize(itr::VertexItr{T}) where {T<:Polytope} = Base.HasLength() +Base.IteratorSize(itr::VertexItr{T}) where {T<:Multi} = Base.HasLength() + +Base.length(itr::VertexItr{T}) where {T<:Polytope} = nvertices(T) +Base.length(itr::VertexItr{T}) where {T<:Multi} = sum(nvertices, itr.el.geoms) +Base.eltype(itr::VertexItr{T}) where {T} = Point + +verticesiter(e::T) where {T<:Union{Geometry,Multi}} = VertexIter(e) \ No newline at end of file From 9ca4f25b19b15cee8ee2b84415ff2d43b7f8e223 Mon Sep 17 00:00:00 2001 From: cschen Date: Wed, 13 Nov 2024 00:02:35 +0100 Subject: [PATCH 02/41] added small (temporary) comments for better initial digestion, will remove later --- src/iteration.jl | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/src/iteration.jl b/src/iteration.jl index fdccd0208..0c907bb1d 100644 --- a/src/iteration.jl +++ b/src/iteration.jl @@ -6,10 +6,13 @@ Base.iterate(itr::VertexItr{T}, i=1) where {T<:Polytope} = (@inline; (i - 1) % UInt < nvertices(itr.el) % UInt ? (@inbounds vertex(itr.el, i), i + 1) : nothing) Base.iterate(itr::VertexItr{T}, state=(1, 1)) where {T<:Multi} = begin - ig, ivg = state + ig, ivg = state # current geometry index, current vertex index in the current geometry ig > length(itr.el.geoms) && return nothing + # iterate through the current geometry is = iterate(itr.el.geoms[ig], ivg) + + # start next geometry if current one is done isnothing(is) && return iterate(itr, (ig + 1, 1)) v, ivg = is From 7bd8de9586ae43b9579ed67a1f1060f97bfc2515 Mon Sep 17 00:00:00 2001 From: cschen Date: Wed, 13 Nov 2024 12:47:34 +0100 Subject: [PATCH 03/41] refactor into `eachvertex` --- src/Meshes.jl | 2 +- src/iteration.jl | 49 ++++++++++++++++++++++++++++++------------------ 2 files changed, 32 insertions(+), 19 deletions(-) diff --git a/src/Meshes.jl b/src/Meshes.jl index 5109aecf5..913e75f2e 100644 --- a/src/Meshes.jl +++ b/src/Meshes.jl @@ -561,7 +561,7 @@ export measurematrix, adjacencymatrix, atol, - verticesiter + eachvertex # visualization viz, viz! diff --git a/src/iteration.jl b/src/iteration.jl index 0c907bb1d..f05688e4e 100644 --- a/src/iteration.jl +++ b/src/iteration.jl @@ -1,29 +1,42 @@ + struct VertexItr{T} - el::T + el::T end -Base.iterate(itr::VertexItr{T}, i=1) where {T<:Polytope} = - (@inline; (i - 1) % UInt < nvertices(itr.el) % UInt ? (@inbounds vertex(itr.el, i), i + 1) : nothing) +_v_iterate(el::Polytope, i) = + (@inline; (i - 1) % UInt < nvertices(el) % UInt ? (@inbounds vertex(el, i), i + 1) : nothing) + +_v_iterate(el::Mesh, i) = + (@inline; (i - 1) % UInt < nvertices(el) % UInt ? (@inbounds vertex(el, i), i + 1) : nothing) + +Base.iterate(itr::VertexItr{<:Mesh}, i=1) = _v_iterate(itr.el, i) -Base.iterate(itr::VertexItr{T}, state=(1, 1)) where {T<:Multi} = begin - ig, ivg = state # current geometry index, current vertex index in the current geometry - ig > length(itr.el.geoms) && return nothing +Base.iterate(itr::VertexItr{<:Polytope}, i=1) = _v_iterate(itr.el, i) - # iterate through the current geometry - is = iterate(itr.el.geoms[ig], ivg) +Base.iterate(itr::VertexItr{<:Multi}, state=(1, 1)) = begin + ig, ivg = state + ig > length(itr.el.geoms) && return nothing - # start next geometry if current one is done - isnothing(is) && return iterate(itr, (ig + 1, 1)) + is = _v_iterate(itr.el.geoms[ig], ivg) + is === nothing && return Base.iterate(itr, (ig + 1, 1)) - v, ivg = is - return (v, (ig, ivg)) + v, ivg = is + return (v, (ig, ivg)) end -Base.IteratorSize(itr::VertexItr{T}) where {T<:Polytope} = Base.HasLength() -Base.IteratorSize(itr::VertexItr{T}) where {T<:Multi} = Base.HasLength() +Base.length(itr::VertexItr{<:Mesh}) = nvertices(T) +Base.length(itr::VertexItr{<:Polytope}) = nvertices(T) +Base.length(itr::VertexItr{<:Multi}) = sum(nvertices, itr.el.geoms) + +Base.IteratorSize(itr::VertexItr) = Base.HasLength() +Base.eltype(::VertexItr) = Point + +eachvertex(e::T) where {T} = error("Vertex iterator not implemented for type $T") +eachvertex(e::Union{Mesh,Polytope,Multi}) = VertexItr(e) +# eachvertex(e::Multi) = Iterators.flatten(eachvertex.(e.geoms)) + + + + -Base.length(itr::VertexItr{T}) where {T<:Polytope} = nvertices(T) -Base.length(itr::VertexItr{T}) where {T<:Multi} = sum(nvertices, itr.el.geoms) -Base.eltype(itr::VertexItr{T}) where {T} = Point -verticesiter(e::T) where {T<:Union{Geometry,Multi}} = VertexIter(e) \ No newline at end of file From 3275c8e69b408cce3bbf63401e10bad2a841b058 Mon Sep 17 00:00:00 2001 From: cschen Date: Wed, 13 Nov 2024 13:24:00 +0100 Subject: [PATCH 04/41] fix: typo in the export list --- src/Meshes.jl | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/src/Meshes.jl b/src/Meshes.jl index 913e75f2e..9684efd5c 100644 --- a/src/Meshes.jl +++ b/src/Meshes.jl @@ -561,9 +561,10 @@ export measurematrix, adjacencymatrix, atol, - eachvertex + eachvertex, -# visualization -viz, viz! + # visualization + viz, + viz! end # module From 86eab4ed19742784f03f28dad5fbce62925dff9a Mon Sep 17 00:00:00 2001 From: cschen Date: Wed, 13 Nov 2024 14:17:28 +0100 Subject: [PATCH 05/41] fix: Multi -> MultiPolytope, no importing of base methods --- src/Meshes.jl | 1 - src/iteration.jl | 9 +++------ 2 files changed, 3 insertions(+), 7 deletions(-) diff --git a/src/Meshes.jl b/src/Meshes.jl index 9684efd5c..f1c0d21d0 100644 --- a/src/Meshes.jl +++ b/src/Meshes.jl @@ -37,7 +37,6 @@ import Base: sort import Base: ==, ! import Base: +, -, * import Base: <, >, ≤, ≥ -import Base: iterate, length, IteratorSize, eltype import StatsBase: sample import Distances: evaluate import NearestNeighbors: MinkowskiMetric diff --git a/src/iteration.jl b/src/iteration.jl index f05688e4e..b4c6c1fbf 100644 --- a/src/iteration.jl +++ b/src/iteration.jl @@ -13,7 +13,7 @@ Base.iterate(itr::VertexItr{<:Mesh}, i=1) = _v_iterate(itr.el, i) Base.iterate(itr::VertexItr{<:Polytope}, i=1) = _v_iterate(itr.el, i) -Base.iterate(itr::VertexItr{<:Multi}, state=(1, 1)) = begin +Base.iterate(itr::VertexItr{<:MultiPolytope}, state=(1, 1)) = begin ig, ivg = state ig > length(itr.el.geoms) && return nothing @@ -26,16 +26,13 @@ end Base.length(itr::VertexItr{<:Mesh}) = nvertices(T) Base.length(itr::VertexItr{<:Polytope}) = nvertices(T) -Base.length(itr::VertexItr{<:Multi}) = sum(nvertices, itr.el.geoms) +Base.length(itr::VertexItr{<:MultiPolytope}) = sum(nvertices, itr.el.geoms) Base.IteratorSize(itr::VertexItr) = Base.HasLength() Base.eltype(::VertexItr) = Point eachvertex(e::T) where {T} = error("Vertex iterator not implemented for type $T") -eachvertex(e::Union{Mesh,Polytope,Multi}) = VertexItr(e) -# eachvertex(e::Multi) = Iterators.flatten(eachvertex.(e.geoms)) - - +eachvertex(e::Union{Mesh,Polytope,MultiPolytope}) = VertexItr(e) From 9a62f4fa607e8101f2bad50344d45d75d99cef19 Mon Sep 17 00:00:00 2001 From: cschen Date: Wed, 13 Nov 2024 17:07:56 +0100 Subject: [PATCH 06/41] revert to simple generators. Rewrite some function in term of the iterator instead. --- src/Meshes.jl | 1 - src/domains/meshes.jl | 9 ++++++++- src/geometries/multigeom.jl | 6 ++++-- src/geometries/polytopes.jl | 7 +++++++ src/iteration.jl | 39 ------------------------------------- 5 files changed, 19 insertions(+), 43 deletions(-) delete mode 100644 src/iteration.jl diff --git a/src/Meshes.jl b/src/Meshes.jl index f1c0d21d0..436670401 100644 --- a/src/Meshes.jl +++ b/src/Meshes.jl @@ -130,7 +130,6 @@ include("distances.jl") include("supportfun.jl") include("matrices.jl") include("projecting.jl") -include("iteration.jl") # visualization include("viz.jl") diff --git a/src/domains/meshes.jl b/src/domains/meshes.jl index f98cad96b..2891a6238 100644 --- a/src/domains/meshes.jl +++ b/src/domains/meshes.jl @@ -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) @@ -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`. +""" +eachvertex(m::Mesh) = (vertex(m, ind) for ind in 1:nvertices(m)) + """ faces(mesh, rank) diff --git a/src/geometries/multigeom.jl b/src/geometries/multigeom.jl index 451e6f747..0a17e0304 100644 --- a/src/geometries/multigeom.jl +++ b/src/geometries/multigeom.jl @@ -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) -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) diff --git a/src/geometries/polytopes.jl b/src/geometries/polytopes.jl index 3785a6d39..6e0db93f4 100644 --- a/src/geometries/polytopes.jl +++ b/src/geometries/polytopes.jl @@ -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) diff --git a/src/iteration.jl b/src/iteration.jl deleted file mode 100644 index b4c6c1fbf..000000000 --- a/src/iteration.jl +++ /dev/null @@ -1,39 +0,0 @@ - -struct VertexItr{T} - el::T -end - -_v_iterate(el::Polytope, i) = - (@inline; (i - 1) % UInt < nvertices(el) % UInt ? (@inbounds vertex(el, i), i + 1) : nothing) - -_v_iterate(el::Mesh, i) = - (@inline; (i - 1) % UInt < nvertices(el) % UInt ? (@inbounds vertex(el, i), i + 1) : nothing) - -Base.iterate(itr::VertexItr{<:Mesh}, i=1) = _v_iterate(itr.el, i) - -Base.iterate(itr::VertexItr{<:Polytope}, i=1) = _v_iterate(itr.el, i) - -Base.iterate(itr::VertexItr{<:MultiPolytope}, state=(1, 1)) = begin - ig, ivg = state - ig > length(itr.el.geoms) && return nothing - - is = _v_iterate(itr.el.geoms[ig], ivg) - is === nothing && return Base.iterate(itr, (ig + 1, 1)) - - v, ivg = is - return (v, (ig, ivg)) -end - -Base.length(itr::VertexItr{<:Mesh}) = nvertices(T) -Base.length(itr::VertexItr{<:Polytope}) = nvertices(T) -Base.length(itr::VertexItr{<:MultiPolytope}) = sum(nvertices, itr.el.geoms) - -Base.IteratorSize(itr::VertexItr) = Base.HasLength() -Base.eltype(::VertexItr) = Point - -eachvertex(e::T) where {T} = error("Vertex iterator not implemented for type $T") -eachvertex(e::Union{Mesh,Polytope,MultiPolytope}) = VertexItr(e) - - - - From 5e15a2468782f4a643d76240158c7185e5e2f5ec Mon Sep 17 00:00:00 2001 From: cschen Date: Wed, 13 Nov 2024 17:08:20 +0100 Subject: [PATCH 07/41] add tests, first part --- test/meshes.jl | 3 +++ test/multigeoms.jl | 2 ++ test/polytopes.jl | 4 ++++ 3 files changed, 9 insertions(+) diff --git a/test/meshes.jl b/test/meshes.jl index ce1070f8b..2cc2edc35 100644 --- a/test/meshes.jl +++ b/test/meshes.jl @@ -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 @@ -873,6 +874,7 @@ 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 @@ -880,6 +882,7 @@ end 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 diff --git a/test/multigeoms.jl b/test/multigeoms.jl index 0a585fc38..c1007e16b 100644 --- a/test/multigeoms.jl +++ b/test/multigeoms.jl @@ -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)] @@ -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) diff --git a/test/polytopes.jl b/test/polytopes.jl index 8f6a43b26..fb85a55a4 100644 --- a/test/polytopes.jl +++ b/test/polytopes.jl @@ -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 + NGONS = [Triangle, Quadrangle, Pentagon, Hexagon, Heptagon, Octagon, Nonagon, Decagon] NVERT = 3:10 for (i, NGON) in enumerate(NGONS) From 7628b36bf669940589a4c9791633036ab7fee61c Mon Sep 17 00:00:00 2001 From: cschen Date: Wed, 13 Nov 2024 17:30:26 +0100 Subject: [PATCH 08/41] fix: change test --- test/polytopes.jl | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/test/polytopes.jl b/test/polytopes.jl index fb85a55a4..4adcfd83e 100644 --- a/test/polytopes.jl +++ b/test/polytopes.jl @@ -303,8 +303,11 @@ end @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 + @test @allocated begin + for v in eachvertex(Ngon(pts)) + # ... + end + end 0 NGONS = [Triangle, Quadrangle, Pentagon, Hexagon, Heptagon, Octagon, Nonagon, Decagon] NVERT = 3:10 From 33c6c6fbec73b2660d0a65c8f964a7759cd017ca Mon Sep 17 00:00:00 2001 From: cschen Date: Wed, 13 Nov 2024 17:30:22 +0100 Subject: [PATCH 09/41] typo MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: Júlio Hoffimann --- src/domains/meshes.jl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/domains/meshes.jl b/src/domains/meshes.jl index 2891a6238..141a16500 100644 --- a/src/domains/meshes.jl +++ b/src/domains/meshes.jl @@ -35,7 +35,7 @@ nvertices(m::Mesh) = nvertices(topology(m)) """ eachvertex(mesh) -Return an iterator for the vertices in the `mesh`. +Return an iterator for the vertices of the `mesh`. """ eachvertex(m::Mesh) = (vertex(m, ind) for ind in 1:nvertices(m)) From a473e57326ef24f6f2e7467cfb3703ab33a2a18e Mon Sep 17 00:00:00 2001 From: cschen Date: Wed, 13 Nov 2024 17:37:22 +0100 Subject: [PATCH 10/41] fix: use first instead of getindex, --- src/geometries/multigeom.jl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/geometries/multigeom.jl b/src/geometries/multigeom.jl index 0a17e0304..997953b32 100644 --- a/src/geometries/multigeom.jl +++ b/src/geometries/multigeom.jl @@ -48,7 +48,7 @@ Base.isapprox(m₁::Multi, m₂::Multi; atol=atol(lentype(m₁)), kwargs...) = # POLYTOPE # --------- -vertex(m::MultiPolytope, ind) = getindex(Base.iterate(Base.Iterators.drop(eachvertex(m), ind - 1)), 1) # nth(itr, n) +vertex(m::MultiPolytope, ind) = first(Base.Iterators.drop(eachvertex(m), ind - 1)) # nth(itr, n) vertices(m::MultiPolytope) = collect(eachvertex(m)) From 421141caa96a90523c46c3ccc17c08d626cfe9b5 Mon Sep 17 00:00:00 2001 From: cschen Date: Fri, 15 Nov 2024 11:48:42 +0100 Subject: [PATCH 11/41] revive the custom iterator --- src/Meshes.jl | 7 ++++++- src/domains/meshes.jl | 7 ------- src/geometries/multigeom.jl | 2 -- src/geometries/polytopes.jl | 7 ------- src/iteration.jl | 34 ++++++++++++++++++++++++++++++++++ 5 files changed, 40 insertions(+), 17 deletions(-) create mode 100644 src/iteration.jl diff --git a/src/Meshes.jl b/src/Meshes.jl index 436670401..02b3e98b6 100644 --- a/src/Meshes.jl +++ b/src/Meshes.jl @@ -79,6 +79,9 @@ include("domains.jl") # utilities include("utils.jl") +# iteration +include("iteration.jl") + # domain indices include("indices.jl") @@ -330,6 +333,9 @@ export # indices indices, + # iteration + eachvertex, + # partitions Partition, indices, @@ -559,7 +565,6 @@ export measurematrix, adjacencymatrix, atol, - eachvertex, # visualization viz, diff --git a/src/domains/meshes.jl b/src/domains/meshes.jl index 141a16500..e1f68fe9f 100644 --- a/src/domains/meshes.jl +++ b/src/domains/meshes.jl @@ -32,13 +32,6 @@ 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) diff --git a/src/geometries/multigeom.jl b/src/geometries/multigeom.jl index 997953b32..fd1c65f56 100644 --- a/src/geometries/multigeom.jl +++ b/src/geometries/multigeom.jl @@ -54,8 +54,6 @@ 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) diff --git a/src/geometries/polytopes.jl b/src/geometries/polytopes.jl index 6e0db93f4..3785a6d39 100644 --- a/src/geometries/polytopes.jl +++ b/src/geometries/polytopes.jl @@ -251,13 +251,6 @@ 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) diff --git a/src/iteration.jl b/src/iteration.jl new file mode 100644 index 000000000..49dde959c --- /dev/null +++ b/src/iteration.jl @@ -0,0 +1,34 @@ +struct VertexItr{T} + el::T +end + +eachvertex(e::T) where {T} = error("Vertex iterator not implemented for type $T") + +eachvertex(e::Union{Mesh,Polytope,MultiPolytope}) = VertexItr(e) + +_v_iterate(el::Polytope, i) = + (@inline; (i - 1) % UInt < nvertices(el) % UInt ? (@inbounds vertex(el, i), i + 1) : nothing) + +_v_iterate(el::Mesh, i) = (@inline; (i - 1) % UInt < nvertices(el) % UInt ? (@inbounds vertex(el, i), i + 1) : nothing) + +Base.iterate(itr::VertexItr{<:Mesh}, i=1) = _v_iterate(itr.el, i) + +Base.iterate(itr::VertexItr{<:Polytope}, i=1) = _v_iterate(itr.el, i) + +@propagate_inbounds Base.iterate(itr::VertexItr{<:Meshes.MultiPolytope}, state=(1, 1)) = begin + ig, ivg = state + ig > length(itr.el.geoms) && return nothing + + is = _v_iterate(itr.el.geoms[ig], ivg) + is === nothing && return Base.iterate(itr, (ig + 1, 1)) + + v, ivg = is + return (v, (ig, ivg)) +end + +Base.length(itr::VertexItr{<:Mesh}) = nvertices(T) +Base.length(itr::VertexItr{<:Polytope}) = nvertices(T) +Base.length(itr::VertexItr{<:Meshes.MultiPolytope}) = sum(nvertices, itr.el.geoms) + +Base.IteratorSize(itr::VertexItr) = Base.HasLength() +Base.eltype(::VertexItr) = Point From bc985f628fcb9081aa5ab729ad698fa78e27b9e9 Mon Sep 17 00:00:00 2001 From: cschen Date: Sun, 17 Nov 2024 10:43:40 +0100 Subject: [PATCH 12/41] add typecheck to avoid stack overflows. --- src/geometries/multigeom.jl | 7 ++++++- src/iteration.jl | 10 ++++------ 2 files changed, 10 insertions(+), 7 deletions(-) diff --git a/src/geometries/multigeom.jl b/src/geometries/multigeom.jl index fd1c65f56..86777fb50 100644 --- a/src/geometries/multigeom.jl +++ b/src/geometries/multigeom.jl @@ -20,7 +20,12 @@ struct Multi{M<:Manifold,C<:CRS,G<:Geometry{M,C}} <: Geometry{M,C} end # constructor with iterator of geometries -Multi(geoms) = Multi(collect(geoms)) + +Multi(geoms) = begin + all(gT <: Geometry{M,C} where {M<:Manifold,C<:CRS} for gT in typeof.(geoms)) || + throw("All elements used must be subtypes of the same Geometry{<:Manifold, <:CRS}") + Multi(collect(geoms)) +end # type aliases for convenience const MultiPoint{M<:Manifold,C<:CRS} = Multi{M,C,<:Point{M,C}} diff --git a/src/iteration.jl b/src/iteration.jl index 49dde959c..0b6d22582 100644 --- a/src/iteration.jl +++ b/src/iteration.jl @@ -15,20 +15,18 @@ Base.iterate(itr::VertexItr{<:Mesh}, i=1) = _v_iterate(itr.el, i) Base.iterate(itr::VertexItr{<:Polytope}, i=1) = _v_iterate(itr.el, i) -@propagate_inbounds Base.iterate(itr::VertexItr{<:Meshes.MultiPolytope}, state=(1, 1)) = begin +Base.iterate(itr::VertexItr{<:Meshes.MultiPolytope}, state=(1, 1)) = begin ig, ivg = state ig > length(itr.el.geoms) && return nothing - is = _v_iterate(itr.el.geoms[ig], ivg) is === nothing && return Base.iterate(itr, (ig + 1, 1)) - v, ivg = is return (v, (ig, ivg)) end -Base.length(itr::VertexItr{<:Mesh}) = nvertices(T) -Base.length(itr::VertexItr{<:Polytope}) = nvertices(T) +Base.length(itr::VertexItr{<:Mesh}) = nvertices(itr.el) +Base.length(itr::VertexItr{<:Polytope}) = nvertices(itr.el) Base.length(itr::VertexItr{<:Meshes.MultiPolytope}) = sum(nvertices, itr.el.geoms) -Base.IteratorSize(itr::VertexItr) = Base.HasLength() +Base.IteratorSize(::VertexItr) = Base.HasLength() Base.eltype(::VertexItr) = Point From f788fcf00322c6b3263f53b9d0093dd82810ebf3 Mon Sep 17 00:00:00 2001 From: cschen Date: Sun, 17 Nov 2024 11:29:27 +0100 Subject: [PATCH 13/41] better tests --- test/meshes.jl | 15 +++++++++++++++ test/multigeoms.jl | 8 +++++++- test/polytopes.jl | 25 +++++++++++++++++++++---- 3 files changed, 43 insertions(+), 5 deletions(-) diff --git a/test/meshes.jl b/test/meshes.jl index 2cc2edc35..4dab996bc 100644 --- a/test/meshes.jl +++ b/test/meshes.jl @@ -127,6 +127,14 @@ @test minimum(sub) == Point(Polar(T(1), T(2))) @test maximum(sub) == Point(Polar(T(4), T(7))) + # vertex iteration + const rg = RegularGrid(Point(1, 1), Point(2, 2), (1, 1)) + @test collect(eachvertex(rg)) == Point.([(1, 1), (2, 1), (1, 2), (2, 2)]) + @test @allocated begin + for _ in eachvertex(rg) + end + end == 0 + # type stability grid = RegularGrid((10, 20), Point(Polar(T(0), T(0))), T.((1, 1))) @inferred vertex(grid, (1, 1)) @@ -801,6 +809,12 @@ end @test area(mesh) ≈ T(1) * u"m^2" @test extrema(mesh) == (cart(0, 0), cart(1, 1)) + const cmesh = mesh + @test @allocated begin + for _ in eachvertex(cmesh) + end + end == 0 + # test constructors coords = [T.((0, 0)), T.((1, 0)), T.((0, 1)), T.((1, 1)), T.((0.5, 0.5))] connec = connect.([(1, 2, 5), (2, 4, 5), (4, 3, 5), (3, 1, 5)], Triangle) @@ -875,6 +889,7 @@ end mesh = merge(mesh₁, mesh₂) @test vertices(mesh) == [vertices(mesh₁); vertices(mesh₂)] @test collect(eachvertex(mesh)) == vertices(mesh) + @test @allocated iterate(eachvertex(mesh)) == 0 @test collect(elements(topology(mesh))) == connect.([(1, 2, 3), (4, 5, 6)]) # merge operation with 3D geometries diff --git a/test/multigeoms.jl b/test/multigeoms.jl index c1007e16b..722a61e32 100644 --- a/test/multigeoms.jl +++ b/test/multigeoms.jl @@ -10,12 +10,18 @@ @test crs(multi) <: Cartesian{NoDatum} @test Meshes.lentype(multi) == ℳ @test vertex(multi, 1) == vertex(poly, 1) + @test collect(eachvertex(multi)) == [vertices(poly); vertices(poly)] @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)] + const cmulti = multi + @test @allocated begin + for _ in eachvertex(cmulti) + end + end == 0 + poly1 = PolyArea(cart.([(0, 0), (1, 0), (1, 1), (0, 1)])) poly2 = PolyArea(cart.([(1, 1), (2, 1), (2, 2), (1, 2)])) multi = Multi([poly1, poly2]) diff --git a/test/polytopes.jl b/test/polytopes.jl index 4adcfd83e..6de0326fd 100644 --- a/test/polytopes.jl +++ b/test/polytopes.jl @@ -82,6 +82,14 @@ s = Segment(latlon(0, 135), latlon(0, 45)) @test_broken measure(s) ≈ 3C / 4 + # vertex iteration + const s = Segment(Point(1.0, 1.0, 1.0, 1.0), Point(2.0, 2.0, 2.0, 2.0)) + @test collect(eachvertex(s)) == [Point(1.0, 1.0, 1.0, 1.0), Point(2.0, 2.0, 2.0, 2.0)] + @test @allocated begin + for _ in eachindex(s) + end + end == 0 + # parameterization s = Segment(latlon(45, 0), latlon(45, 90)) @test s(T(0)) == latlon(45, 0) @@ -257,6 +265,14 @@ end @test @elapsed(issimple(r)) < 0.02 @test @allocated(issimple(r)) < 950000 + # vertex iteration + const r = Ring(Point.([(0, 0), (1, 0), (1, 1), (0, 1)])) + @test collect(eachvertex(r)) == Point.([(0, 0), (1, 0), (1, 1), (0, 1)]) + @test @allocated begin + for _ in eachvertex(r) + end + end == 0 + # CRS propagation r = Ring(merc.([(0, 0), (1, 0), (1, 1), (0, 1)])) @test crs(centroid(r)) === crs(r) @@ -302,12 +318,13 @@ end @test vertices(Ngon{3}(pts...)) == verts @test vertices(Ngon{3}(tups...)) == verts - @test collect(eachvertex(Ngon(pts))) == verts + # vertex iteration + const ngon = Ngon(pts) + @test collect(eachvertex(ngon)) == verts @test @allocated begin - for v in eachvertex(Ngon(pts)) - # ... + for v in eachvertex(ngon) end - end 0 + end == 0 NGONS = [Triangle, Quadrangle, Pentagon, Hexagon, Heptagon, Octagon, Nonagon, Decagon] NVERT = 3:10 From 8cf1604db320c57987866254bfad9b26c0b19901 Mon Sep 17 00:00:00 2001 From: cschen Date: Sun, 17 Nov 2024 11:45:46 +0100 Subject: [PATCH 14/41] fix tests --- test/meshes.jl | 10 +++++----- test/multigeoms.jl | 4 ++-- test/polytopes.jl | 12 ++++++------ 3 files changed, 13 insertions(+), 13 deletions(-) diff --git a/test/meshes.jl b/test/meshes.jl index 4dab996bc..fcb44fa39 100644 --- a/test/meshes.jl +++ b/test/meshes.jl @@ -130,10 +130,10 @@ # vertex iteration const rg = RegularGrid(Point(1, 1), Point(2, 2), (1, 1)) @test collect(eachvertex(rg)) == Point.([(1, 1), (2, 1), (1, 2), (2, 2)]) - @test @allocated begin + @test @allocated(begin for _ in eachvertex(rg) end - end == 0 + end) == 0 # type stability grid = RegularGrid((10, 20), Point(Polar(T(0), T(0))), T.((1, 1))) @@ -810,10 +810,10 @@ end @test extrema(mesh) == (cart(0, 0), cart(1, 1)) const cmesh = mesh - @test @allocated begin + @test @allocated(begin for _ in eachvertex(cmesh) end - end == 0 + end) == 0 # test constructors coords = [T.((0, 0)), T.((1, 0)), T.((0, 1)), T.((1, 1)), T.((0.5, 0.5))] @@ -889,7 +889,7 @@ end mesh = merge(mesh₁, mesh₂) @test vertices(mesh) == [vertices(mesh₁); vertices(mesh₂)] @test collect(eachvertex(mesh)) == vertices(mesh) - @test @allocated iterate(eachvertex(mesh)) == 0 + @test @allocated(iterate(eachvertex(mesh))) == 0 @test collect(elements(topology(mesh))) == connect.([(1, 2, 3), (4, 5, 6)]) # merge operation with 3D geometries diff --git a/test/multigeoms.jl b/test/multigeoms.jl index 722a61e32..c122277c7 100644 --- a/test/multigeoms.jl +++ b/test/multigeoms.jl @@ -17,10 +17,10 @@ @test rings(multi) == [rings(poly); rings(poly)] const cmulti = multi - @test @allocated begin + @test @allocated(begin for _ in eachvertex(cmulti) end - end == 0 + end) == 0 poly1 = PolyArea(cart.([(0, 0), (1, 0), (1, 1), (0, 1)])) poly2 = PolyArea(cart.([(1, 1), (2, 1), (2, 2), (1, 2)])) diff --git a/test/polytopes.jl b/test/polytopes.jl index 6de0326fd..ca765adcf 100644 --- a/test/polytopes.jl +++ b/test/polytopes.jl @@ -85,10 +85,10 @@ # vertex iteration const s = Segment(Point(1.0, 1.0, 1.0, 1.0), Point(2.0, 2.0, 2.0, 2.0)) @test collect(eachvertex(s)) == [Point(1.0, 1.0, 1.0, 1.0), Point(2.0, 2.0, 2.0, 2.0)] - @test @allocated begin + @test @allocated(begin for _ in eachindex(s) end - end == 0 + end) == 0 # parameterization s = Segment(latlon(45, 0), latlon(45, 90)) @@ -268,10 +268,10 @@ end # vertex iteration const r = Ring(Point.([(0, 0), (1, 0), (1, 1), (0, 1)])) @test collect(eachvertex(r)) == Point.([(0, 0), (1, 0), (1, 1), (0, 1)]) - @test @allocated begin + @test @allocated(begin for _ in eachvertex(r) end - end == 0 + end) == 0 # CRS propagation r = Ring(merc.([(0, 0), (1, 0), (1, 1), (0, 1)])) @@ -321,10 +321,10 @@ end # vertex iteration const ngon = Ngon(pts) @test collect(eachvertex(ngon)) == verts - @test @allocated begin + @test @allocated(begin for v in eachvertex(ngon) end - end == 0 + end) == 0 NGONS = [Triangle, Quadrangle, Pentagon, Hexagon, Heptagon, Octagon, Nonagon, Decagon] NVERT = 3:10 From 1770bf98d7600f3d609dea86c2956f3ee1ef2d01 Mon Sep 17 00:00:00 2001 From: cschen Date: Sun, 17 Nov 2024 13:24:17 +0100 Subject: [PATCH 15/41] revert Multi constructor --- src/geometries/multigeom.jl | 7 +------ 1 file changed, 1 insertion(+), 6 deletions(-) diff --git a/src/geometries/multigeom.jl b/src/geometries/multigeom.jl index 86777fb50..fd1c65f56 100644 --- a/src/geometries/multigeom.jl +++ b/src/geometries/multigeom.jl @@ -20,12 +20,7 @@ struct Multi{M<:Manifold,C<:CRS,G<:Geometry{M,C}} <: Geometry{M,C} end # constructor with iterator of geometries - -Multi(geoms) = begin - all(gT <: Geometry{M,C} where {M<:Manifold,C<:CRS} for gT in typeof.(geoms)) || - throw("All elements used must be subtypes of the same Geometry{<:Manifold, <:CRS}") - Multi(collect(geoms)) -end +Multi(geoms) = Multi(collect(geoms)) # type aliases for convenience const MultiPoint{M<:Manifold,C<:CRS} = Multi{M,C,<:Point{M,C}} From 34e825e9ff7aeb060fa2153b4a16bc0928d117b0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=BAlio=20Hoffimann?= Date: Sun, 17 Nov 2024 11:02:43 -0300 Subject: [PATCH 16/41] Update src/geometries/multigeom.jl --- src/geometries/multigeom.jl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/geometries/multigeom.jl b/src/geometries/multigeom.jl index fd1c65f56..57f46afa7 100644 --- a/src/geometries/multigeom.jl +++ b/src/geometries/multigeom.jl @@ -48,7 +48,7 @@ Base.isapprox(m₁::Multi, m₂::Multi; atol=atol(lentype(m₁)), kwargs...) = # POLYTOPE # --------- -vertex(m::MultiPolytope, ind) = first(Base.Iterators.drop(eachvertex(m), ind - 1)) # nth(itr, n) +vertex(m::MultiPolytope, ind) = first(Base.Iterators.drop(eachvertex(m), ind - 1)) vertices(m::MultiPolytope) = collect(eachvertex(m)) From f716552dbe7ec946a4645e102bd4a83fac600a23 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=BAlio=20Hoffimann?= Date: Sun, 17 Nov 2024 11:03:08 -0300 Subject: [PATCH 17/41] Update src/geometries/multigeom.jl --- src/geometries/multigeom.jl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/geometries/multigeom.jl b/src/geometries/multigeom.jl index 57f46afa7..337b0b348 100644 --- a/src/geometries/multigeom.jl +++ b/src/geometries/multigeom.jl @@ -48,7 +48,7 @@ Base.isapprox(m₁::Multi, m₂::Multi; atol=atol(lentype(m₁)), kwargs...) = # POLYTOPE # --------- -vertex(m::MultiPolytope, ind) = first(Base.Iterators.drop(eachvertex(m), ind - 1)) +vertex(m::MultiPolytope, ind) = first(Iterators.drop(eachvertex(m), ind - 1)) vertices(m::MultiPolytope) = collect(eachvertex(m)) From 95138cbdd6a6d58ca883076aa54c7e0b272b4ec8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=BAlio=20Hoffimann?= Date: Sun, 17 Nov 2024 11:20:14 -0300 Subject: [PATCH 18/41] Cleanup --- src/Meshes.jl | 8 ++------ src/domains/meshes.jl | 7 +++++++ src/geometries/multigeom.jl | 28 ++++++++++++++++++++++++++++ src/geometries/polytopes.jl | 9 ++++++++- src/iteration.jl | 32 -------------------------------- 5 files changed, 45 insertions(+), 39 deletions(-) delete mode 100644 src/iteration.jl diff --git a/src/Meshes.jl b/src/Meshes.jl index 02b3e98b6..cf5b60247 100644 --- a/src/Meshes.jl +++ b/src/Meshes.jl @@ -79,9 +79,6 @@ include("domains.jl") # utilities include("utils.jl") -# iteration -include("iteration.jl") - # domain indices include("indices.jl") @@ -224,6 +221,7 @@ export vertex, vertices, nvertices, + eachvertex, rings, segments, angles, @@ -316,6 +314,7 @@ export vertex, vertices, nvertices, + eachvertex, element, elements, nelements, @@ -333,9 +332,6 @@ export # indices indices, - # iteration - eachvertex, - # partitions Partition, indices, diff --git a/src/domains/meshes.jl b/src/domains/meshes.jl index e1f68fe9f..5b3f73381 100644 --- a/src/domains/meshes.jl +++ b/src/domains/meshes.jl @@ -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, i) for i in 1:nvertices(m)) + """ faces(mesh, rank) diff --git a/src/geometries/multigeom.jl b/src/geometries/multigeom.jl index 337b0b348..462c6d5e2 100644 --- a/src/geometries/multigeom.jl +++ b/src/geometries/multigeom.jl @@ -54,6 +54,8 @@ vertices(m::MultiPolytope) = collect(eachvertex(m)) nvertices(m::MultiPolytope) = sum(nvertices, m.geoms) +eachvertex(m::MultiPolytope) = VertexItr(m) + Base.unique(m::MultiPolytope) = unique!(deepcopy(m)) function Base.unique!(m::MultiPolytope) @@ -89,3 +91,29 @@ function Base.show(io::IO, ::MIME"text/plain", m::Multi) println(io) printelms(io, m.geoms) end + +# ----------------- +# HELPER FUNCTIONS +# ----------------- + +struct VertexItr{T} + el::T +end + +eachvertex(e::MultiPolytope) = VertexItr(e) + +_v_iterate(el::Polytope, i) = + (@inline; (i - 1) % UInt < nvertices(el) % UInt ? (@inbounds vertex(el, i), i + 1) : nothing) + +Base.iterate(itr::VertexItr{<:MultiPolytope}, state=(1, 1)) = begin + ig, ivg = state + ig > length(itr.el.geoms) && return nothing + is = _v_iterate(itr.el.geoms[ig], ivg) + is === nothing && return Base.iterate(itr, (ig + 1, 1)) + v, ivg = is + return (v, (ig, ivg)) +end + +Base.length(itr::VertexItr{<:MultiPolytope}) = sum(nvertices, itr.el.geoms) +Base.IteratorSize(::VertexItr) = Base.HasLength() +Base.eltype(::VertexItr) = Point diff --git a/src/geometries/polytopes.jl b/src/geometries/polytopes.jl index 3785a6d39..ac16288f9 100644 --- a/src/geometries/polytopes.jl +++ b/src/geometries/polytopes.jl @@ -247,10 +247,17 @@ vertices(p::Polytope) = p.vertices """ nvertices(polytope) -Return the number of vertices in the `polytope`. +Return the number of vertices of the `polytope`. """ nvertices(p::Polytope) = nvertices(typeof(p)) +""" + eachvertex(polytope) + +Return an iterator for the vertices of the `polytope`. +""" +eachvertex(p::Polytope) = (vertex(p, i) for i in 1:nvertices(p)) + """ unique(polytope) diff --git a/src/iteration.jl b/src/iteration.jl deleted file mode 100644 index 0b6d22582..000000000 --- a/src/iteration.jl +++ /dev/null @@ -1,32 +0,0 @@ -struct VertexItr{T} - el::T -end - -eachvertex(e::T) where {T} = error("Vertex iterator not implemented for type $T") - -eachvertex(e::Union{Mesh,Polytope,MultiPolytope}) = VertexItr(e) - -_v_iterate(el::Polytope, i) = - (@inline; (i - 1) % UInt < nvertices(el) % UInt ? (@inbounds vertex(el, i), i + 1) : nothing) - -_v_iterate(el::Mesh, i) = (@inline; (i - 1) % UInt < nvertices(el) % UInt ? (@inbounds vertex(el, i), i + 1) : nothing) - -Base.iterate(itr::VertexItr{<:Mesh}, i=1) = _v_iterate(itr.el, i) - -Base.iterate(itr::VertexItr{<:Polytope}, i=1) = _v_iterate(itr.el, i) - -Base.iterate(itr::VertexItr{<:Meshes.MultiPolytope}, state=(1, 1)) = begin - ig, ivg = state - ig > length(itr.el.geoms) && return nothing - is = _v_iterate(itr.el.geoms[ig], ivg) - is === nothing && return Base.iterate(itr, (ig + 1, 1)) - v, ivg = is - return (v, (ig, ivg)) -end - -Base.length(itr::VertexItr{<:Mesh}) = nvertices(itr.el) -Base.length(itr::VertexItr{<:Polytope}) = nvertices(itr.el) -Base.length(itr::VertexItr{<:Meshes.MultiPolytope}) = sum(nvertices, itr.el.geoms) - -Base.IteratorSize(::VertexItr) = Base.HasLength() -Base.eltype(::VertexItr) = Point From 3a455b734a2a7a0b161cd056e5b54d0a0ef6ef3f Mon Sep 17 00:00:00 2001 From: cschen Date: Mon, 18 Nov 2024 16:55:47 +0100 Subject: [PATCH 19/41] *removed double definition of the `eachvertex` function *eltype returns the specific point based on manifold and tranformation. *added missing `IteratorEltype` trait --- src/geometries/multigeom.jl | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/src/geometries/multigeom.jl b/src/geometries/multigeom.jl index 462c6d5e2..fbdd98d30 100644 --- a/src/geometries/multigeom.jl +++ b/src/geometries/multigeom.jl @@ -100,8 +100,6 @@ struct VertexItr{T} el::T end -eachvertex(e::MultiPolytope) = VertexItr(e) - _v_iterate(el::Polytope, i) = (@inline; (i - 1) % UInt < nvertices(el) % UInt ? (@inbounds vertex(el, i), i + 1) : nothing) @@ -116,4 +114,5 @@ end Base.length(itr::VertexItr{<:MultiPolytope}) = sum(nvertices, itr.el.geoms) Base.IteratorSize(::VertexItr) = Base.HasLength() -Base.eltype(::VertexItr) = Point +Base.IteratorEltype(::VertexItr) = Base.HasEltype() +Base.eltype(::VertexItr{<:MultiPolytope{K,M,C}}) where {K,M<:Manifold,C<:CRS} = Point{M,C} \ No newline at end of file From 874016db5ae4c0e949ab1478485a067eb35077d5 Mon Sep 17 00:00:00 2001 From: cschen Date: Mon, 18 Nov 2024 18:05:51 +0100 Subject: [PATCH 20/41] * improved allocation testing * small cleanup --- src/geometries/multigeom.jl | 4 ++-- test/meshes.jl | 14 ++++---------- test/multigeoms.jl | 7 ++----- test/polytopes.jl | 23 +++++++---------------- test/testutils.jl | 8 ++++++++ 5 files changed, 23 insertions(+), 33 deletions(-) diff --git a/src/geometries/multigeom.jl b/src/geometries/multigeom.jl index fbdd98d30..14fa64b05 100644 --- a/src/geometries/multigeom.jl +++ b/src/geometries/multigeom.jl @@ -112,7 +112,7 @@ Base.iterate(itr::VertexItr{<:MultiPolytope}, state=(1, 1)) = begin return (v, (ig, ivg)) end -Base.length(itr::VertexItr{<:MultiPolytope}) = sum(nvertices, itr.el.geoms) Base.IteratorSize(::VertexItr) = Base.HasLength() Base.IteratorEltype(::VertexItr) = Base.HasEltype() -Base.eltype(::VertexItr{<:MultiPolytope{K,M,C}}) where {K,M<:Manifold,C<:CRS} = Point{M,C} \ No newline at end of file +Base.length(itr::VertexItr{<:MultiPolytope}) = sum(nvertices, itr.el.geoms) +Base.eltype(::VertexItr{<:MultiPolytope{K,M,C}}) where {K,M<:Manifold,C<:CRS} = Point{M,C} diff --git a/test/meshes.jl b/test/meshes.jl index fcb44fa39..7f60f740f 100644 --- a/test/meshes.jl +++ b/test/meshes.jl @@ -130,10 +130,7 @@ # vertex iteration const rg = RegularGrid(Point(1, 1), Point(2, 2), (1, 1)) @test collect(eachvertex(rg)) == Point.([(1, 1), (2, 1), (1, 2), (2, 2)]) - @test @allocated(begin - for _ in eachvertex(rg) - end - end) == 0 + @test _iter_alloctest(rg) == 0 # type stability grid = RegularGrid((10, 20), Point(Polar(T(0), T(0))), T.((1, 1))) @@ -809,11 +806,8 @@ end @test area(mesh) ≈ T(1) * u"m^2" @test extrema(mesh) == (cart(0, 0), cart(1, 1)) - const cmesh = mesh - @test @allocated(begin - for _ in eachvertex(cmesh) - end - end) == 0 + const cmesh = SimpleMesh(points, connec) + @test _iter_alloctest(cmesh) == 0 # test constructors coords = [T.((0, 0)), T.((1, 0)), T.((0, 1)), T.((1, 1)), T.((0.5, 0.5))] @@ -889,7 +883,7 @@ end mesh = merge(mesh₁, mesh₂) @test vertices(mesh) == [vertices(mesh₁); vertices(mesh₂)] @test collect(eachvertex(mesh)) == vertices(mesh) - @test @allocated(iterate(eachvertex(mesh))) == 0 + @test _iter_alloctest(mesh) == 0 @test collect(elements(topology(mesh))) == connect.([(1, 2, 3), (4, 5, 6)]) # merge operation with 3D geometries diff --git a/test/multigeoms.jl b/test/multigeoms.jl index c122277c7..308a3c5e0 100644 --- a/test/multigeoms.jl +++ b/test/multigeoms.jl @@ -16,11 +16,8 @@ @test boundary(multi) == merge(boundary(poly), boundary(poly)) @test rings(multi) == [rings(poly); rings(poly)] - const cmulti = multi - @test @allocated(begin - for _ in eachvertex(cmulti) - end - end) == 0 + const cmulti = Multi([poly, poly]) + @test _iter_alloctest(cmulti) == 0 poly1 = PolyArea(cart.([(0, 0), (1, 0), (1, 1), (0, 1)])) poly2 = PolyArea(cart.([(1, 1), (2, 1), (2, 2), (1, 2)])) diff --git a/test/polytopes.jl b/test/polytopes.jl index ca765adcf..81c030628 100644 --- a/test/polytopes.jl +++ b/test/polytopes.jl @@ -83,12 +83,9 @@ @test_broken measure(s) ≈ 3C / 4 # vertex iteration - const s = Segment(Point(1.0, 1.0, 1.0, 1.0), Point(2.0, 2.0, 2.0, 2.0)) - @test collect(eachvertex(s)) == [Point(1.0, 1.0, 1.0, 1.0), Point(2.0, 2.0, 2.0, 2.0)] - @test @allocated(begin - for _ in eachindex(s) - end - end) == 0 + const cseg = Segment(Point(1.0, 1.0, 1.0, 1.0), Point(2.0, 2.0, 2.0, 2.0)) + @test collect(eachvertex(cseg)) == [Point(1.0, 1.0, 1.0, 1.0), Point(2.0, 2.0, 2.0, 2.0)] + @test _iter_alloctest(cseg) == 0 # parameterization s = Segment(latlon(45, 0), latlon(45, 90)) @@ -266,12 +263,9 @@ end @test @allocated(issimple(r)) < 950000 # vertex iteration - const r = Ring(Point.([(0, 0), (1, 0), (1, 1), (0, 1)])) - @test collect(eachvertex(r)) == Point.([(0, 0), (1, 0), (1, 1), (0, 1)]) - @test @allocated(begin - for _ in eachvertex(r) - end - end) == 0 + const cring = Ring(Point.([(0, 0), (1, 0), (1, 1), (0, 1)])) + @test collect(eachvertex(cring)) == Point.([(0, 0), (1, 0), (1, 1), (0, 1)]) + @test _iter_alloctest(cring) == 0 # CRS propagation r = Ring(merc.([(0, 0), (1, 0), (1, 1), (0, 1)])) @@ -321,10 +315,7 @@ end # vertex iteration const ngon = Ngon(pts) @test collect(eachvertex(ngon)) == verts - @test @allocated(begin - for v in eachvertex(ngon) - end - end) == 0 + @test _iter_alloctest(ngon) == 0 NGONS = [Triangle, Quadrangle, Pentagon, Hexagon, Heptagon, Octagon, Nonagon, Decagon] NVERT = 3:10 diff --git a/test/testutils.jl b/test/testutils.jl index d12c2f374..491dd1bda 100644 --- a/test/testutils.jl +++ b/test/testutils.jl @@ -176,3 +176,11 @@ function _isapproxtest(g::Geometry, ::Val{3}) @test isapprox(g, Translate(0u"m", τ32, 0u"m")(g32), atol=1.1τ32) @test isapprox(g, Translate(0u"m", 0u"m", τ32)(g32), atol=1.1τ32) end + +function _iter_alloctest(coll::Union{Geometry,Mesh}) + iterate(eachvertex(coll)) # precompile run + + bytes = @allocated for _ in eachvertex(coll) + end + bytes +end From 14fdf6ee4a47e75cfa462135d18b9304bee67c55 Mon Sep 17 00:00:00 2001 From: cschen Date: Mon, 18 Nov 2024 18:50:05 +0100 Subject: [PATCH 21/41] Update src/geometries/multigeom.jl Co-authored-by: Elias Carvalho <73039601+eliascarv@users.noreply.github.com> --- src/geometries/multigeom.jl | 25 ------------------------- 1 file changed, 25 deletions(-) diff --git a/src/geometries/multigeom.jl b/src/geometries/multigeom.jl index 14fa64b05..ae7dc827b 100644 --- a/src/geometries/multigeom.jl +++ b/src/geometries/multigeom.jl @@ -91,28 +91,3 @@ function Base.show(io::IO, ::MIME"text/plain", m::Multi) println(io) printelms(io, m.geoms) end - -# ----------------- -# HELPER FUNCTIONS -# ----------------- - -struct VertexItr{T} - el::T -end - -_v_iterate(el::Polytope, i) = - (@inline; (i - 1) % UInt < nvertices(el) % UInt ? (@inbounds vertex(el, i), i + 1) : nothing) - -Base.iterate(itr::VertexItr{<:MultiPolytope}, state=(1, 1)) = begin - ig, ivg = state - ig > length(itr.el.geoms) && return nothing - is = _v_iterate(itr.el.geoms[ig], ivg) - is === nothing && return Base.iterate(itr, (ig + 1, 1)) - v, ivg = is - return (v, (ig, ivg)) -end - -Base.IteratorSize(::VertexItr) = Base.HasLength() -Base.IteratorEltype(::VertexItr) = Base.HasEltype() -Base.length(itr::VertexItr{<:MultiPolytope}) = sum(nvertices, itr.el.geoms) -Base.eltype(::VertexItr{<:MultiPolytope{K,M,C}}) where {K,M<:Manifold,C<:CRS} = Point{M,C} From 21b5aa2eb82ba352ccd38c7a6922da7c1c2d73c1 Mon Sep 17 00:00:00 2001 From: cschen Date: Mon, 18 Nov 2024 18:50:35 +0100 Subject: [PATCH 22/41] Update src/geometries/multigeom.jl Co-authored-by: Elias Carvalho <73039601+eliascarv@users.noreply.github.com> --- src/geometries/multigeom.jl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/geometries/multigeom.jl b/src/geometries/multigeom.jl index ae7dc827b..fbbc61037 100644 --- a/src/geometries/multigeom.jl +++ b/src/geometries/multigeom.jl @@ -54,7 +54,7 @@ vertices(m::MultiPolytope) = collect(eachvertex(m)) nvertices(m::MultiPolytope) = sum(nvertices, m.geoms) -eachvertex(m::MultiPolytope) = VertexItr(m) +eachvertex(m::MultiPolytope) = (v for g in m.geoms for v in vertices(g)) Base.unique(m::MultiPolytope) = unique!(deepcopy(m)) From 1739d4bd1a6aac5e3c168129d6c6f47e3a1c360c Mon Sep 17 00:00:00 2001 From: cschen Date: Mon, 18 Nov 2024 18:51:06 +0100 Subject: [PATCH 23/41] Update test/meshes.jl Co-authored-by: Elias Carvalho <73039601+eliascarv@users.noreply.github.com> --- test/meshes.jl | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/test/meshes.jl b/test/meshes.jl index 7f60f740f..affa7ff98 100644 --- a/test/meshes.jl +++ b/test/meshes.jl @@ -128,9 +128,9 @@ @test maximum(sub) == Point(Polar(T(4), T(7))) # vertex iteration - const rg = RegularGrid(Point(1, 1), Point(2, 2), (1, 1)) - @test collect(eachvertex(rg)) == Point.([(1, 1), (2, 1), (1, 2), (2, 2)]) - @test _iter_alloctest(rg) == 0 + grid = RegularGrid(cart(1, 1), cart(2, 2), (1, 1)) + @test collect(eachvertex(grid)) == cart.([(1, 1), (2, 1), (1, 2), (2, 2)]) + @test eachvertexalloc(grid) == 0 # type stability grid = RegularGrid((10, 20), Point(Polar(T(0), T(0))), T.((1, 1))) From ab28a90974c7a1caa265b1b7eab47ef1e8e8afaf Mon Sep 17 00:00:00 2001 From: cschen Date: Mon, 18 Nov 2024 18:51:19 +0100 Subject: [PATCH 24/41] Update test/meshes.jl Co-authored-by: Elias Carvalho <73039601+eliascarv@users.noreply.github.com> --- test/meshes.jl | 1 + 1 file changed, 1 insertion(+) diff --git a/test/meshes.jl b/test/meshes.jl index affa7ff98..49d957bc3 100644 --- a/test/meshes.jl +++ b/test/meshes.jl @@ -795,6 +795,7 @@ end @test Meshes.lentype(mesh) == ℳ @test vertices(mesh) == points @test collect(eachvertex(mesh)) == points + @test eachvertexalloc(mesh) == 0 @test collect(faces(mesh, 2)) == triangles @test collect(elements(mesh)) == triangles @test nelements(mesh) == 4 From d3fc0767802ba37972a57580e80169ea3833a393 Mon Sep 17 00:00:00 2001 From: cschen Date: Mon, 18 Nov 2024 18:51:41 +0100 Subject: [PATCH 25/41] Update test/meshes.jl Co-authored-by: Elias Carvalho <73039601+eliascarv@users.noreply.github.com> --- test/meshes.jl | 3 --- 1 file changed, 3 deletions(-) diff --git a/test/meshes.jl b/test/meshes.jl index 49d957bc3..e5d8801ca 100644 --- a/test/meshes.jl +++ b/test/meshes.jl @@ -807,9 +807,6 @@ end @test area(mesh) ≈ T(1) * u"m^2" @test extrema(mesh) == (cart(0, 0), cart(1, 1)) - const cmesh = SimpleMesh(points, connec) - @test _iter_alloctest(cmesh) == 0 - # test constructors coords = [T.((0, 0)), T.((1, 0)), T.((0, 1)), T.((1, 1)), T.((0.5, 0.5))] connec = connect.([(1, 2, 5), (2, 4, 5), (4, 3, 5), (3, 1, 5)], Triangle) From fe4302bc557000c578883cc205eb4958827c0489 Mon Sep 17 00:00:00 2001 From: cschen Date: Mon, 18 Nov 2024 18:51:53 +0100 Subject: [PATCH 26/41] Update test/meshes.jl Co-authored-by: Elias Carvalho <73039601+eliascarv@users.noreply.github.com> --- test/meshes.jl | 1 + 1 file changed, 1 insertion(+) diff --git a/test/meshes.jl b/test/meshes.jl index e5d8801ca..066213b1d 100644 --- a/test/meshes.jl +++ b/test/meshes.jl @@ -890,6 +890,7 @@ end mesh = merge(mesh₁, mesh₂) @test vertices(mesh) == [vertices(mesh₁); vertices(mesh₂)] @test collect(eachvertex(mesh)) == vertices(mesh) + @test eachvertexalloc(mesh) == 0 @test collect(elements(topology(mesh))) == connect.([(1, 2, 3, 4), (5, 6, 7, 8)], Tetrahedron) # convert any mesh to SimpleMesh From 0d1394b576445b0f7f1cc9b638e91b467d300620 Mon Sep 17 00:00:00 2001 From: cschen Date: Mon, 18 Nov 2024 18:52:07 +0100 Subject: [PATCH 27/41] Update test/testutils.jl Co-authored-by: Elias Carvalho <73039601+eliascarv@users.noreply.github.com> --- test/testutils.jl | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/test/testutils.jl b/test/testutils.jl index 491dd1bda..1573c964c 100644 --- a/test/testutils.jl +++ b/test/testutils.jl @@ -177,10 +177,9 @@ function _isapproxtest(g::Geometry, ::Val{3}) @test isapprox(g, Translate(0u"m", 0u"m", τ32)(g32), atol=1.1τ32) end -function _iter_alloctest(coll::Union{Geometry,Mesh}) - iterate(eachvertex(coll)) # precompile run +function eachvertexalloc(g) + iterate(eachvertex(g)) # precompile run - bytes = @allocated for _ in eachvertex(coll) + @allocated for _ in eachvertex(coll) end - bytes end From 7270456f20198b0747fa41aea4e6d6ba21436bd9 Mon Sep 17 00:00:00 2001 From: cschen Date: Mon, 18 Nov 2024 18:52:19 +0100 Subject: [PATCH 28/41] Update test/polytopes.jl Co-authored-by: Elias Carvalho <73039601+eliascarv@users.noreply.github.com> --- test/polytopes.jl | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/test/polytopes.jl b/test/polytopes.jl index 81c030628..db5909b28 100644 --- a/test/polytopes.jl +++ b/test/polytopes.jl @@ -313,9 +313,9 @@ end @test vertices(Ngon{3}(tups...)) == verts # vertex iteration - const ngon = Ngon(pts) + ngon = Ngon(pts) @test collect(eachvertex(ngon)) == verts - @test _iter_alloctest(ngon) == 0 + @test eachvertexalloc(ngon) == 0 NGONS = [Triangle, Quadrangle, Pentagon, Hexagon, Heptagon, Octagon, Nonagon, Decagon] NVERT = 3:10 From f9cb83dd21e7fc523bd82bb19c4262f69579b9df Mon Sep 17 00:00:00 2001 From: cschen Date: Mon, 18 Nov 2024 18:52:31 +0100 Subject: [PATCH 29/41] Update test/polytopes.jl Co-authored-by: Elias Carvalho <73039601+eliascarv@users.noreply.github.com> --- test/polytopes.jl | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/test/polytopes.jl b/test/polytopes.jl index db5909b28..c3a52b2fd 100644 --- a/test/polytopes.jl +++ b/test/polytopes.jl @@ -263,9 +263,9 @@ end @test @allocated(issimple(r)) < 950000 # vertex iteration - const cring = Ring(Point.([(0, 0), (1, 0), (1, 1), (0, 1)])) - @test collect(eachvertex(cring)) == Point.([(0, 0), (1, 0), (1, 1), (0, 1)]) - @test _iter_alloctest(cring) == 0 + r = Ring(cart.([(0, 0), (1, 0), (1, 1), (0, 1)])) + @test collect(eachvertex(r)) == cart.([(0, 0), (1, 0), (1, 1), (0, 1)]) + @test eachvertexalloc(r) == 0 # CRS propagation r = Ring(merc.([(0, 0), (1, 0), (1, 1), (0, 1)])) From 11678f51497c8c1fd995878027c598af44bf9254 Mon Sep 17 00:00:00 2001 From: cschen Date: Mon, 18 Nov 2024 18:52:41 +0100 Subject: [PATCH 30/41] Update test/polytopes.jl Co-authored-by: Elias Carvalho <73039601+eliascarv@users.noreply.github.com> --- test/polytopes.jl | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/test/polytopes.jl b/test/polytopes.jl index c3a52b2fd..081239d70 100644 --- a/test/polytopes.jl +++ b/test/polytopes.jl @@ -83,9 +83,9 @@ @test_broken measure(s) ≈ 3C / 4 # vertex iteration - const cseg = Segment(Point(1.0, 1.0, 1.0, 1.0), Point(2.0, 2.0, 2.0, 2.0)) - @test collect(eachvertex(cseg)) == [Point(1.0, 1.0, 1.0, 1.0), Point(2.0, 2.0, 2.0, 2.0)] - @test _iter_alloctest(cseg) == 0 + s = Segment(cart(0, 0), cart(1, 1)) + @test collect(eachvertex(s)) == [cart(0, 0), cart(1, 1)] + @test eachvertexalloc(s) == 0 # parameterization s = Segment(latlon(45, 0), latlon(45, 90)) From 780b7b018bb80281f3935f6a58d4b5e832c4f3f0 Mon Sep 17 00:00:00 2001 From: cschen Date: Mon, 18 Nov 2024 18:52:52 +0100 Subject: [PATCH 31/41] Update test/multigeoms.jl Co-authored-by: Elias Carvalho <73039601+eliascarv@users.noreply.github.com> --- test/multigeoms.jl | 1 + 1 file changed, 1 insertion(+) diff --git a/test/multigeoms.jl b/test/multigeoms.jl index 308a3c5e0..3e13b6016 100644 --- a/test/multigeoms.jl +++ b/test/multigeoms.jl @@ -25,6 +25,7 @@ @test vertices(multi) == [vertices(poly1); vertices(poly2)] @test nvertices(multi) == nvertices(poly1) + nvertices(poly2) @test collect(eachvertex(multi)) == [vertices(poly1); vertices(poly2)] + @test eachvertexalloc(multi) == 0 @test area(multi) == area(poly1) + area(poly2) @test perimeter(multi) == perimeter(poly1) + perimeter(poly2) @test centroid(multi) == cart(1, 1) From 23371b47da328a5aaf2c30decc7ede278bdc7815 Mon Sep 17 00:00:00 2001 From: cschen Date: Mon, 18 Nov 2024 18:53:01 +0100 Subject: [PATCH 32/41] Update test/meshes.jl Co-authored-by: Elias Carvalho <73039601+eliascarv@users.noreply.github.com> --- test/meshes.jl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/meshes.jl b/test/meshes.jl index 066213b1d..76b260960 100644 --- a/test/meshes.jl +++ b/test/meshes.jl @@ -881,7 +881,7 @@ end mesh = merge(mesh₁, mesh₂) @test vertices(mesh) == [vertices(mesh₁); vertices(mesh₂)] @test collect(eachvertex(mesh)) == vertices(mesh) - @test _iter_alloctest(mesh) == 0 + @test eachvertexalloc(mesh) == 0 @test collect(elements(topology(mesh))) == connect.([(1, 2, 3), (4, 5, 6)]) # merge operation with 3D geometries From c30d340b9f26d32e7ddede474b9747259863bb74 Mon Sep 17 00:00:00 2001 From: Elias Carvalho <73039601+eliascarv@users.noreply.github.com> Date: Mon, 18 Nov 2024 14:54:46 -0300 Subject: [PATCH 33/41] Apply suggestions from code review --- test/multigeoms.jl | 4 +--- test/testutils.jl | 2 +- 2 files changed, 2 insertions(+), 4 deletions(-) diff --git a/test/multigeoms.jl b/test/multigeoms.jl index 3e13b6016..5673a5ef3 100644 --- a/test/multigeoms.jl +++ b/test/multigeoms.jl @@ -11,14 +11,12 @@ @test Meshes.lentype(multi) == ℳ @test vertex(multi, 1) == vertex(poly, 1) @test collect(eachvertex(multi)) == [vertices(poly); vertices(poly)] + @test eachvertexalloc(multi) == 0 @test vertices(multi) == [vertices(poly); vertices(poly)] @test nvertices(multi) == nvertices(poly) + nvertices(poly) @test boundary(multi) == merge(boundary(poly), boundary(poly)) @test rings(multi) == [rings(poly); rings(poly)] - const cmulti = Multi([poly, poly]) - @test _iter_alloctest(cmulti) == 0 - poly1 = PolyArea(cart.([(0, 0), (1, 0), (1, 1), (0, 1)])) poly2 = PolyArea(cart.([(1, 1), (2, 1), (2, 2), (1, 2)])) multi = Multi([poly1, poly2]) diff --git a/test/testutils.jl b/test/testutils.jl index 1573c964c..bd89935f3 100644 --- a/test/testutils.jl +++ b/test/testutils.jl @@ -180,6 +180,6 @@ end function eachvertexalloc(g) iterate(eachvertex(g)) # precompile run - @allocated for _ in eachvertex(coll) + @allocated for _ in eachvertex(g) end end From e80e15517b7ee04c0371514451ba7eb49840878e Mon Sep 17 00:00:00 2001 From: Elias Carvalho Date: Mon, 18 Nov 2024 15:56:40 -0300 Subject: [PATCH 34/41] Add more tests --- test/meshes.jl | 44 ++++++++++++++++++++---- test/multigeoms.jl | 28 +++++++++++++--- test/polytopes.jl | 83 +++++++++++++++++++++++++++++++++++++++++++--- 3 files changed, 140 insertions(+), 15 deletions(-) diff --git a/test/meshes.jl b/test/meshes.jl index 76b260960..b3772bc9b 100644 --- a/test/meshes.jl +++ b/test/meshes.jl @@ -128,8 +128,8 @@ @test maximum(sub) == Point(Polar(T(4), T(7))) # vertex iteration - grid = RegularGrid(cart(1, 1), cart(2, 2), (1, 1)) - @test collect(eachvertex(grid)) == cart.([(1, 1), (2, 1), (1, 2), (2, 2)]) + grid = RegularGrid(cart(0, 0), cart(1, 1), (1, 1)) + @test collect(eachvertex(grid)) == cart.([(0, 0), (1, 0), (0, 1), (1, 1)]) @test eachvertexalloc(grid) == 0 # type stability @@ -139,6 +139,8 @@ @inferred grid[1:2, 1:2] @inferred Meshes.xyz(grid) @inferred Meshes.XYZ(grid) + @test isconcretetype(eltype(vertices(grid))) + @inferred vertices(grid) # error: dimensions must be positive @test_throws ArgumentError RegularGrid((-10, -10), latlon(0, 0), T.((1, 1))) @@ -506,6 +508,11 @@ end @test topology(rg) == topology(cg) @test vertices(rg) == vertices(cg) + # vertex iteration + grid = RectilinearGrid(T[0, 1], T[0, 1]) + @test collect(eachvertex(grid)) == cart.([(0, 0), (1, 0), (0, 1), (1, 1)]) + @test eachvertexalloc(grid) == 0 + # type stability x = range(zero(T), stop=one(T), length=6) * u"mm" y = T[0.0, 0.1, 0.3, 0.7, 0.9, 1.0] * u"cm" @@ -519,6 +526,8 @@ end @inferred grid[1, 1] @inferred grid[1:2, 1:2] @inferred Meshes.XYZ(grid) + @test isconcretetype(eltype(vertices(grid))) + @inferred vertices(grid) # error: regular spacing on `🌐` requires `LatLon` coordinates x = range(zero(T), stop=one(T), length=6) @@ -687,6 +696,11 @@ end @test topology(sg) == topology(rg) @test vertices(sg) == vertices(rg) + # vertex iteration + grid = StructuredGrid(T[0 0; 1 1], T[0 1; 0 1]) + @test collect(eachvertex(grid)) == cart.([(0, 0), (1, 0), (0, 1), (1, 1)]) + @test eachvertexalloc(grid) == 0 + # type stability X = repeat(range(zero(T), stop=one(T), length=6), 1, 6) * u"mm" Y = repeat(T[0.0, 0.1, 0.3, 0.7, 0.9, 1.0]', 6, 1) * u"cm" @@ -880,8 +894,6 @@ 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 eachvertexalloc(mesh) == 0 @test collect(elements(topology(mesh))) == connect.([(1, 2, 3), (4, 5, 6)]) # merge operation with 3D geometries @@ -889,8 +901,6 @@ end 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 eachvertexalloc(mesh) == 0 @test collect(elements(topology(mesh))) == connect.([(1, 2, 3, 4), (5, 6, 7, 8)], Tetrahedron) # convert any mesh to SimpleMesh @@ -940,6 +950,16 @@ end @test vertex(mesh, 4) == points[4] @test vertex(mesh, 5) == points[5] + # vertex iteration + points = cart.([(0, 0), (1, 0), (0, 1), (1, 1), (0.5, 0.5)]) + connec = connect.([(1, 2, 5), (2, 4, 5), (4, 3, 5), (3, 1, 5)], Triangle) + mesh = SimpleMesh(points, connec) + @test collect(eachvertex(mesh)) == points + @test eachvertexalloc(mesh) == 0 + # type stability + @test isconcretetype(eltype(vertices(mesh))) + @inferred vertices(mesh) + points = cart.([(0, 0), (1, 0), (0, 1), (1, 1), (0.5, 0.5)]) connec = connect.([(1, 2, 5), (2, 4, 5), (4, 3, 5), (3, 1, 5)], Triangle) mesh = SimpleMesh(points, connec) @@ -999,6 +1019,18 @@ end trans2 = Translate(T(-10), T(-10)) @test TransformedMesh(TransformedMesh(grid, trans1), trans2) == TransformedMesh(grid, trans1 → trans2) + # vertex iteration + trans = Identity() + points = latlon.([(0, 0), (0, 1), (1, 0), (1, 1), (0.5, 0.5)]) + connec = connect.([(1, 2, 5), (2, 4, 5), (4, 3, 5), (3, 1, 5)], Triangle) + mesh = SimpleMesh(points, connec) + tmesh = TransformedMesh(mesh, trans) + @test collect(eachvertex(tmesh)) == points + @test eachvertexalloc(tmesh) == 0 + # type stability + @test isconcretetype(eltype(vertices(tmesh))) + @inferred vertices(tmesh) + # transforms that change the Manifold and/or CRS points = latlon.([(0, 0), (0, 1), (1, 0), (1, 1), (0.5, 0.5)]) connec = connect.([(1, 2, 5), (2, 4, 5), (4, 3, 5), (3, 1, 5)], Triangle) diff --git a/test/multigeoms.jl b/test/multigeoms.jl index 5673a5ef3..366e06870 100644 --- a/test/multigeoms.jl +++ b/test/multigeoms.jl @@ -10,8 +10,6 @@ @test crs(multi) <: Cartesian{NoDatum} @test Meshes.lentype(multi) == ℳ @test vertex(multi, 1) == vertex(poly, 1) - @test collect(eachvertex(multi)) == [vertices(poly); vertices(poly)] - @test eachvertexalloc(multi) == 0 @test vertices(multi) == [vertices(poly); vertices(poly)] @test nvertices(multi) == nvertices(poly) + nvertices(poly) @test boundary(multi) == merge(boundary(poly), boundary(poly)) @@ -22,8 +20,6 @@ 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 eachvertexalloc(multi) == 0 @test area(multi) == area(poly1) + area(poly2) @test perimeter(multi) == perimeter(poly1) + perimeter(poly2) @test centroid(multi) == cart(1, 1) @@ -101,4 +97,28 @@ poly2 = PolyArea(merc.([(1, 1), (2, 1), (2, 2), (1, 2)])) multi = Multi([poly1, poly2]) @test crs(centroid(multi)) === crs(multi) + + # vertex iteration + ring1 = Ring(cart.([(0, 0), (1, 0), (1, 1), (0, 1)])) + ring2 = Ring(cart.([(0, 0), (2, 0), (2, 2), (0, 2)])) + ring3 = Ring(cart.([(0.2, 0.2), (0.4, 0.2), (0.4, 0.4), (0.2, 0.4)])) + ring4 = Ring(cart.([(0.6, 0.2), (0.8, 0.2), (0.8, 0.4), (0.6, 0.4)])) + poly1 = PolyArea(ring1) + poly2 = PolyArea(ring2) + poly3 = PolyArea([ring1, ring3]) + poly4 = PolyArea([ring2, ring4]) + multi1 = Multi([ring1, ring2, ring3, ring4]) + multi2 = Multi([poly1, poly2]) + multi3 = Multi([poly3, poly4]) + @test collect(eachvertex(multi1)) == [vertices(ring1); vertices(ring2); vertices(ring3); vertices(ring4)] + @test collect(eachvertex(multi2)) == [vertices(poly1); vertices(poly2)] + @test collect(eachvertex(multi3)) == [vertices(poly3); vertices(poly4)] + @test eachvertexalloc(multi1) == 0 + @test eachvertexalloc(multi2) == 0 + @test eachvertexalloc(multi3) < 400 + # type stability + @test isconcretetype(eltype(vertices(multi1))) + @test isconcretetype(eltype(vertices(multi2))) + @inferred vertices(multi1) + @inferred vertices(multi2) end diff --git a/test/polytopes.jl b/test/polytopes.jl index 081239d70..15aae16a5 100644 --- a/test/polytopes.jl +++ b/test/polytopes.jl @@ -86,6 +86,9 @@ s = Segment(cart(0, 0), cart(1, 1)) @test collect(eachvertex(s)) == [cart(0, 0), cart(1, 1)] @test eachvertexalloc(s) == 0 + # type stability + @test isconcretetype(eltype(vertices(s))) + @inferred vertices(s) # parameterization s = Segment(latlon(45, 0), latlon(45, 90)) @@ -263,9 +266,17 @@ end @test @allocated(issimple(r)) < 950000 # vertex iteration - r = Ring(cart.([(0, 0), (1, 0), (1, 1), (0, 1)])) - @test collect(eachvertex(r)) == cart.([(0, 0), (1, 0), (1, 1), (0, 1)]) - @test eachvertexalloc(r) == 0 + ro = Rope(cart.([(0, 0), (1, 0), (1, 1), (0, 1)])) + ri = Ring(cart.([(0, 0), (1, 0), (1, 1), (0, 1)])) + @test collect(eachvertex(ro)) == cart.([(0, 0), (1, 0), (1, 1), (0, 1)]) + @test collect(eachvertex(ri)) == cart.([(0, 0), (1, 0), (1, 1), (0, 1)]) + @test eachvertexalloc(ro) == 0 + @test eachvertexalloc(ri) == 0 + # type stability + @test isconcretetype(eltype(vertices(ro))) + @test isconcretetype(eltype(vertices(ri))) + @inferred vertices(ro) + @inferred vertices(ri) # CRS propagation r = Ring(merc.([(0, 0), (1, 0), (1, 1), (0, 1)])) @@ -313,9 +324,12 @@ end @test vertices(Ngon{3}(tups...)) == verts # vertex iteration - ngon = Ngon(pts) - @test collect(eachvertex(ngon)) == verts + ngon = Ngon(cart(0, 0), cart(1, 0), cart(0, 1)) + @test collect(eachvertex(ngon)) == [cart(0, 0), cart(1, 0), cart(0, 1)] @test eachvertexalloc(ngon) == 0 + # type stability + @test isconcretetype(eltype(vertices(ngon))) + @inferred vertices(ngon) NGONS = [Triangle, Quadrangle, Pentagon, Hexagon, Heptagon, Octagon, Nonagon, Decagon] NVERT = 3:10 @@ -553,6 +567,14 @@ end equaltest(p) isapproxtest(p) + # vertex iteration + p = PolyArea(cart(0, 0), cart(1, 0), cart(0, 1)) + @test collect(eachvertex(p)) == [cart(0, 0), cart(1, 0), cart(0, 1)] + @test eachvertexalloc(p) == 0 + # type stability + @test isconcretetype(eltype(vertices(p))) + @inferred vertices(p) + # COMMAND USED TO GENERATE TEST FILES (VARY --seed = 1, 2, ..., 5) # rpg --cluster 30 --algo 2opt --format line --seed 1 --output poly1 fnames = ["poly$i.line" for i in 1:5] @@ -737,6 +759,14 @@ end equaltest(t) isapproxtest(t) + # vertex iteration + t = Tetrahedron(cart(0, 0, 0), cart(1, 0, 0), cart(0, 1, 0), cart(0, 0, 1)) + @test collect(eachvertex(t)) == [cart(0, 0, 0), cart(1, 0, 0), cart(0, 1, 0), cart(0, 0, 1)] + @test eachvertexalloc(t) == 0 + # type stability + @test isconcretetype(eltype(vertices(t))) + @inferred vertices(t) + # CRS propagation c1 = Cartesian{WGS84Latest}(T(0), T(0), T(0)) c2 = Cartesian{WGS84Latest}(T(1), T(0), T(0)) @@ -802,6 +832,32 @@ end equaltest(h) isapproxtest(h) + # vertex iteration + h = Hexahedron( + cart(0, 0, 0), + cart(1, 0, 0), + cart(1, 1, 0), + cart(0, 1, 0), + cart(0, 0, 1), + cart(1, 0, 1), + cart(1, 1, 1), + cart(0, 1, 1) + ) + @test collect(eachvertex(h)) == [ + cart(0, 0, 0), + cart(1, 0, 0), + cart(1, 1, 0), + cart(0, 1, 0), + cart(0, 0, 1), + cart(1, 0, 1), + cart(1, 1, 1), + cart(0, 1, 1) + ] + @test eachvertexalloc(h) == 0 + # type stability + @test isconcretetype(eltype(vertices(h))) + @inferred vertices(h) + h = Hexahedron( cart(0, 0, 0), cart(1, 0, 0), @@ -920,6 +976,14 @@ end equaltest(p) isapproxtest(p) + # vertex iteration + p = Pyramid(cart(0, 0, 0), cart(1, 0, 0), cart(1, 1, 0), cart(0, 1, 0), cart(0, 0, 1)) + @test collect(eachvertex(p)) == [cart(0, 0, 0), cart(1, 0, 0), cart(1, 1, 0), cart(0, 1, 0), cart(0, 0, 1)] + @test eachvertexalloc(p) == 0 + # type stability + @test isconcretetype(eltype(vertices(p))) + @inferred vertices(p) + p = Pyramid(cart(0, 0, 0), cart(1, 0, 0), cart(1, 1, 0), cart(0, 1, 0), cart(0, 0, 1)) @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))" if T === Float32 @@ -958,6 +1022,15 @@ end equaltest(w) isapproxtest(w) + # vertex iteration + 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)) + @test collect(eachvertex(w)) == + [cart(0, 0, 0), cart(1, 0, 0), cart(0, 1, 0), cart(0, 0, 1), cart(1, 0, 1), cart(0, 1, 1)] + @test eachvertexalloc(w) == 0 + # type stability + @test isconcretetype(eltype(vertices(w))) + @inferred vertices(w) + 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)) @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))" if T === Float32 From 7058cff83e20a4f0c0d23c03c3f0b9c383b9dc73 Mon Sep 17 00:00:00 2001 From: Elias Carvalho Date: Mon, 18 Nov 2024 16:01:14 -0300 Subject: [PATCH 35/41] Update tests --- test/meshes.jl | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/test/meshes.jl b/test/meshes.jl index b3772bc9b..30495834c 100644 --- a/test/meshes.jl +++ b/test/meshes.jl @@ -713,6 +713,8 @@ end @inferred vertex(grid, (1, 1)) @inferred grid[1, 1] @inferred grid[1:2, 1:2] + @test isconcretetype(eltype(vertices(grid))) + @inferred vertices(grid) # error: regular spacing on `🌐` requires `LatLon` coordinates X = repeat(range(zero(T), stop=one(T), length=6), 1, 6, 6) @@ -808,8 +810,6 @@ end @test crs(mesh) <: Cartesian{NoDatum} @test Meshes.lentype(mesh) == ℳ @test vertices(mesh) == points - @test collect(eachvertex(mesh)) == points - @test eachvertexalloc(mesh) == 0 @test collect(faces(mesh, 2)) == triangles @test collect(elements(mesh)) == triangles @test nelements(mesh) == 4 From 0dd41fa4edb9d1388b750ca5a48f5cff031c8f68 Mon Sep 17 00:00:00 2001 From: Elias Carvalho Date: Mon, 18 Nov 2024 16:08:24 -0300 Subject: [PATCH 36/41] Simplify tests --- test/meshes.jl | 20 ++++++++++++-------- test/multigeoms.jl | 6 +++--- test/polytopes.jl | 28 +++++++++------------------- 3 files changed, 24 insertions(+), 30 deletions(-) diff --git a/test/meshes.jl b/test/meshes.jl index 30495834c..0916510a8 100644 --- a/test/meshes.jl +++ b/test/meshes.jl @@ -128,8 +128,8 @@ @test maximum(sub) == Point(Polar(T(4), T(7))) # vertex iteration - grid = RegularGrid(cart(0, 0), cart(1, 1), (1, 1)) - @test collect(eachvertex(grid)) == cart.([(0, 0), (1, 0), (0, 1), (1, 1)]) + grid = RegularGrid((10, 10), cart(0, 0), T.((1, 1))) + @test collect(eachvertex(grid)) == vertices(grid) @test eachvertexalloc(grid) == 0 # type stability @@ -509,8 +509,10 @@ end @test vertices(rg) == vertices(cg) # vertex iteration - grid = RectilinearGrid(T[0, 1], T[0, 1]) - @test collect(eachvertex(grid)) == cart.([(0, 0), (1, 0), (0, 1), (1, 1)]) + x = range(zero(T), stop=one(T), length=6) + y = T[0.0, 0.1, 0.3, 0.7, 0.9, 1.0] + grid = RectilinearGrid(x, y) + @test collect(eachvertex(grid)) == vertices(grid) @test eachvertexalloc(grid) == 0 # type stability @@ -697,8 +699,10 @@ end @test vertices(sg) == vertices(rg) # vertex iteration - grid = StructuredGrid(T[0 0; 1 1], T[0 1; 0 1]) - @test collect(eachvertex(grid)) == cart.([(0, 0), (1, 0), (0, 1), (1, 1)]) + X = repeat(range(zero(T), stop=one(T), length=6), 1, 6) + Y = repeat(T[0.0, 0.1, 0.3, 0.7, 0.9, 1.0]', 6, 1) + grid = StructuredGrid(X, Y) + @test collect(eachvertex(grid)) == vertices(grid) @test eachvertexalloc(grid) == 0 # type stability @@ -954,7 +958,7 @@ end points = cart.([(0, 0), (1, 0), (0, 1), (1, 1), (0.5, 0.5)]) connec = connect.([(1, 2, 5), (2, 4, 5), (4, 3, 5), (3, 1, 5)], Triangle) mesh = SimpleMesh(points, connec) - @test collect(eachvertex(mesh)) == points + @test collect(eachvertex(mesh)) == vertices(mesh) @test eachvertexalloc(mesh) == 0 # type stability @test isconcretetype(eltype(vertices(mesh))) @@ -1025,7 +1029,7 @@ end connec = connect.([(1, 2, 5), (2, 4, 5), (4, 3, 5), (3, 1, 5)], Triangle) mesh = SimpleMesh(points, connec) tmesh = TransformedMesh(mesh, trans) - @test collect(eachvertex(tmesh)) == points + @test collect(eachvertex(tmesh)) == vertices(tmesh) @test eachvertexalloc(tmesh) == 0 # type stability @test isconcretetype(eltype(vertices(tmesh))) diff --git a/test/multigeoms.jl b/test/multigeoms.jl index 366e06870..92f7ec790 100644 --- a/test/multigeoms.jl +++ b/test/multigeoms.jl @@ -110,9 +110,9 @@ multi1 = Multi([ring1, ring2, ring3, ring4]) multi2 = Multi([poly1, poly2]) multi3 = Multi([poly3, poly4]) - @test collect(eachvertex(multi1)) == [vertices(ring1); vertices(ring2); vertices(ring3); vertices(ring4)] - @test collect(eachvertex(multi2)) == [vertices(poly1); vertices(poly2)] - @test collect(eachvertex(multi3)) == [vertices(poly3); vertices(poly4)] + @test collect(eachvertex(multi1)) == vertices(multi1) + @test collect(eachvertex(multi2)) == vertices(multi2) + @test collect(eachvertex(multi3)) == vertices(multi3) @test eachvertexalloc(multi1) == 0 @test eachvertexalloc(multi2) == 0 @test eachvertexalloc(multi3) < 400 diff --git a/test/polytopes.jl b/test/polytopes.jl index 15aae16a5..8e2baefee 100644 --- a/test/polytopes.jl +++ b/test/polytopes.jl @@ -84,7 +84,7 @@ # vertex iteration s = Segment(cart(0, 0), cart(1, 1)) - @test collect(eachvertex(s)) == [cart(0, 0), cart(1, 1)] + @test collect(eachvertex(s)) == vertices(s) @test eachvertexalloc(s) == 0 # type stability @test isconcretetype(eltype(vertices(s))) @@ -268,8 +268,8 @@ end # vertex iteration ro = Rope(cart.([(0, 0), (1, 0), (1, 1), (0, 1)])) ri = Ring(cart.([(0, 0), (1, 0), (1, 1), (0, 1)])) - @test collect(eachvertex(ro)) == cart.([(0, 0), (1, 0), (1, 1), (0, 1)]) - @test collect(eachvertex(ri)) == cart.([(0, 0), (1, 0), (1, 1), (0, 1)]) + @test collect(eachvertex(ro)) == vertices(ro) + @test collect(eachvertex(ri)) == vertices(ro) @test eachvertexalloc(ro) == 0 @test eachvertexalloc(ri) == 0 # type stability @@ -325,7 +325,7 @@ end # vertex iteration ngon = Ngon(cart(0, 0), cart(1, 0), cart(0, 1)) - @test collect(eachvertex(ngon)) == [cart(0, 0), cart(1, 0), cart(0, 1)] + @test collect(eachvertex(ngon)) == vertices(ngon) @test eachvertexalloc(ngon) == 0 # type stability @test isconcretetype(eltype(vertices(ngon))) @@ -569,7 +569,7 @@ end # vertex iteration p = PolyArea(cart(0, 0), cart(1, 0), cart(0, 1)) - @test collect(eachvertex(p)) == [cart(0, 0), cart(1, 0), cart(0, 1)] + @test collect(eachvertex(p)) == vertices(p) @test eachvertexalloc(p) == 0 # type stability @test isconcretetype(eltype(vertices(p))) @@ -761,7 +761,7 @@ end # vertex iteration t = Tetrahedron(cart(0, 0, 0), cart(1, 0, 0), cart(0, 1, 0), cart(0, 0, 1)) - @test collect(eachvertex(t)) == [cart(0, 0, 0), cart(1, 0, 0), cart(0, 1, 0), cart(0, 0, 1)] + @test collect(eachvertex(t)) == vertices(t) @test eachvertexalloc(t) == 0 # type stability @test isconcretetype(eltype(vertices(t))) @@ -843,16 +843,7 @@ end cart(1, 1, 1), cart(0, 1, 1) ) - @test collect(eachvertex(h)) == [ - cart(0, 0, 0), - cart(1, 0, 0), - cart(1, 1, 0), - cart(0, 1, 0), - cart(0, 0, 1), - cart(1, 0, 1), - cart(1, 1, 1), - cart(0, 1, 1) - ] + @test collect(eachvertex(h)) == vertices(h) @test eachvertexalloc(h) == 0 # type stability @test isconcretetype(eltype(vertices(h))) @@ -978,7 +969,7 @@ end # vertex iteration p = Pyramid(cart(0, 0, 0), cart(1, 0, 0), cart(1, 1, 0), cart(0, 1, 0), cart(0, 0, 1)) - @test collect(eachvertex(p)) == [cart(0, 0, 0), cart(1, 0, 0), cart(1, 1, 0), cart(0, 1, 0), cart(0, 0, 1)] + @test collect(eachvertex(p)) == vertices(p) @test eachvertexalloc(p) == 0 # type stability @test isconcretetype(eltype(vertices(p))) @@ -1024,8 +1015,7 @@ end # vertex iteration 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)) - @test collect(eachvertex(w)) == - [cart(0, 0, 0), cart(1, 0, 0), cart(0, 1, 0), cart(0, 0, 1), cart(1, 0, 1), cart(0, 1, 1)] + @test collect(eachvertex(w)) == vertices(w) @test eachvertexalloc(w) == 0 # type stability @test isconcretetype(eltype(vertices(w))) From 15fd7aa1295a4b2bb94053f4eec1c1739ad697d2 Mon Sep 17 00:00:00 2001 From: Elias Carvalho Date: Mon, 18 Nov 2024 16:23:55 -0300 Subject: [PATCH 37/41] Simplify tests --- test/meshes.jl | 27 +++----------- test/multigeoms.jl | 14 ++------ test/polytopes.jl | 88 ++++++---------------------------------------- test/testutils.jl | 8 +++++ 4 files changed, 26 insertions(+), 111 deletions(-) diff --git a/test/meshes.jl b/test/meshes.jl index 0916510a8..3ab6862a3 100644 --- a/test/meshes.jl +++ b/test/meshes.jl @@ -129,8 +129,7 @@ # vertex iteration grid = RegularGrid((10, 10), cart(0, 0), T.((1, 1))) - @test collect(eachvertex(grid)) == vertices(grid) - @test eachvertexalloc(grid) == 0 + eachvertextest(grid) # type stability grid = RegularGrid((10, 20), Point(Polar(T(0), T(0))), T.((1, 1))) @@ -139,8 +138,6 @@ @inferred grid[1:2, 1:2] @inferred Meshes.xyz(grid) @inferred Meshes.XYZ(grid) - @test isconcretetype(eltype(vertices(grid))) - @inferred vertices(grid) # error: dimensions must be positive @test_throws ArgumentError RegularGrid((-10, -10), latlon(0, 0), T.((1, 1))) @@ -512,8 +509,7 @@ end x = range(zero(T), stop=one(T), length=6) y = T[0.0, 0.1, 0.3, 0.7, 0.9, 1.0] grid = RectilinearGrid(x, y) - @test collect(eachvertex(grid)) == vertices(grid) - @test eachvertexalloc(grid) == 0 + eachvertextest(grid) # type stability x = range(zero(T), stop=one(T), length=6) * u"mm" @@ -528,8 +524,6 @@ end @inferred grid[1, 1] @inferred grid[1:2, 1:2] @inferred Meshes.XYZ(grid) - @test isconcretetype(eltype(vertices(grid))) - @inferred vertices(grid) # error: regular spacing on `🌐` requires `LatLon` coordinates x = range(zero(T), stop=one(T), length=6) @@ -702,8 +696,7 @@ end X = repeat(range(zero(T), stop=one(T), length=6), 1, 6) Y = repeat(T[0.0, 0.1, 0.3, 0.7, 0.9, 1.0]', 6, 1) grid = StructuredGrid(X, Y) - @test collect(eachvertex(grid)) == vertices(grid) - @test eachvertexalloc(grid) == 0 + eachvertextest(grid) # type stability X = repeat(range(zero(T), stop=one(T), length=6), 1, 6) * u"mm" @@ -717,8 +710,6 @@ end @inferred vertex(grid, (1, 1)) @inferred grid[1, 1] @inferred grid[1:2, 1:2] - @test isconcretetype(eltype(vertices(grid))) - @inferred vertices(grid) # error: regular spacing on `🌐` requires `LatLon` coordinates X = repeat(range(zero(T), stop=one(T), length=6), 1, 6, 6) @@ -958,11 +949,7 @@ end points = cart.([(0, 0), (1, 0), (0, 1), (1, 1), (0.5, 0.5)]) connec = connect.([(1, 2, 5), (2, 4, 5), (4, 3, 5), (3, 1, 5)], Triangle) mesh = SimpleMesh(points, connec) - @test collect(eachvertex(mesh)) == vertices(mesh) - @test eachvertexalloc(mesh) == 0 - # type stability - @test isconcretetype(eltype(vertices(mesh))) - @inferred vertices(mesh) + eachvertextest(mesh) points = cart.([(0, 0), (1, 0), (0, 1), (1, 1), (0.5, 0.5)]) connec = connect.([(1, 2, 5), (2, 4, 5), (4, 3, 5), (3, 1, 5)], Triangle) @@ -1029,11 +1016,7 @@ end connec = connect.([(1, 2, 5), (2, 4, 5), (4, 3, 5), (3, 1, 5)], Triangle) mesh = SimpleMesh(points, connec) tmesh = TransformedMesh(mesh, trans) - @test collect(eachvertex(tmesh)) == vertices(tmesh) - @test eachvertexalloc(tmesh) == 0 - # type stability - @test isconcretetype(eltype(vertices(tmesh))) - @inferred vertices(tmesh) + eachvertextest(tmesh) # transforms that change the Manifold and/or CRS points = latlon.([(0, 0), (0, 1), (1, 0), (1, 1), (0.5, 0.5)]) diff --git a/test/multigeoms.jl b/test/multigeoms.jl index 92f7ec790..86f039a74 100644 --- a/test/multigeoms.jl +++ b/test/multigeoms.jl @@ -110,15 +110,7 @@ multi1 = Multi([ring1, ring2, ring3, ring4]) multi2 = Multi([poly1, poly2]) multi3 = Multi([poly3, poly4]) - @test collect(eachvertex(multi1)) == vertices(multi1) - @test collect(eachvertex(multi2)) == vertices(multi2) - @test collect(eachvertex(multi3)) == vertices(multi3) - @test eachvertexalloc(multi1) == 0 - @test eachvertexalloc(multi2) == 0 - @test eachvertexalloc(multi3) < 400 - # type stability - @test isconcretetype(eltype(vertices(multi1))) - @test isconcretetype(eltype(vertices(multi2))) - @inferred vertices(multi1) - @inferred vertices(multi2) + eachvertextest(multi1) + eachvertextest(multi2) + eachvertextest(multi3, alloccheck=(<(400))) end diff --git a/test/polytopes.jl b/test/polytopes.jl index 8e2baefee..762638627 100644 --- a/test/polytopes.jl +++ b/test/polytopes.jl @@ -42,6 +42,7 @@ s = Segment(cart(0, 0), cart(1, 1)) equaltest(s) isapproxtest(s) + eachvertextest(s) s = Segment(Point(1.0, 1.0, 1.0, 1.0), Point(2.0, 2.0, 2.0, 2.0)) @test all(Point(x, x, x, x) ∈ s for x in 1:0.01:2) @@ -82,14 +83,6 @@ s = Segment(latlon(0, 135), latlon(0, 45)) @test_broken measure(s) ≈ 3C / 4 - # vertex iteration - s = Segment(cart(0, 0), cart(1, 1)) - @test collect(eachvertex(s)) == vertices(s) - @test eachvertexalloc(s) == 0 - # type stability - @test isconcretetype(eltype(vertices(s))) - @inferred vertices(s) - # parameterization s = Segment(latlon(45, 0), latlon(45, 90)) @test s(T(0)) == latlon(45, 0) @@ -126,10 +119,12 @@ end c = Rope(cart(0, 0), cart(1, 0), cart(0, 1)) equaltest(c) isapproxtest(c) + eachvertextest(c) c = Ring(cart(0, 0), cart(1, 0), cart(0, 1)) equaltest(c) isapproxtest(c) + eachvertextest(c) # circular equality c1 = Ring(cart.([(1, 1), (2, 2), (3, 3)])) @@ -265,19 +260,6 @@ end @test @elapsed(issimple(r)) < 0.02 @test @allocated(issimple(r)) < 950000 - # vertex iteration - ro = Rope(cart.([(0, 0), (1, 0), (1, 1), (0, 1)])) - ri = Ring(cart.([(0, 0), (1, 0), (1, 1), (0, 1)])) - @test collect(eachvertex(ro)) == vertices(ro) - @test collect(eachvertex(ri)) == vertices(ro) - @test eachvertexalloc(ro) == 0 - @test eachvertexalloc(ri) == 0 - # type stability - @test isconcretetype(eltype(vertices(ro))) - @test isconcretetype(eltype(vertices(ri))) - @inferred vertices(ro) - @inferred vertices(ri) - # CRS propagation r = Ring(merc.([(0, 0), (1, 0), (1, 1), (0, 1)])) @test crs(centroid(r)) === crs(r) @@ -323,14 +305,6 @@ end @test vertices(Ngon{3}(pts...)) == verts @test vertices(Ngon{3}(tups...)) == verts - # vertex iteration - ngon = Ngon(cart(0, 0), cart(1, 0), cart(0, 1)) - @test collect(eachvertex(ngon)) == vertices(ngon) - @test eachvertexalloc(ngon) == 0 - # type stability - @test isconcretetype(eltype(vertices(ngon))) - @inferred vertices(ngon) - NGONS = [Triangle, Quadrangle, Pentagon, Hexagon, Heptagon, Octagon, Nonagon, Decagon] NVERT = 3:10 for (i, NGON) in enumerate(NGONS) @@ -381,6 +355,7 @@ end t = Triangle(cart(0, 0), cart(1, 0), cart(0, 1)) equaltest(t) isapproxtest(t) + eachvertextest(t) t = Triangle(cart(0, 0), cart(1, 0), cart(0, 1)) @test perimeter(t) ≈ T(1 + 1 + √2) * u"m" @@ -495,6 +470,7 @@ end q = Quadrangle(cart(0, 0), cart(1, 0), cart(1, 1), cart(0, 1)) equaltest(q) isapproxtest(q) + eachvertextest(q) q = Quadrangle(cart(0, 0), cart(1, 0), cart(1, 1), cart(0, 1)) @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)) @@ -566,14 +542,7 @@ end p = PolyArea(cart(0, 0), cart(1, 0), cart(0, 1)) equaltest(p) isapproxtest(p) - - # vertex iteration - p = PolyArea(cart(0, 0), cart(1, 0), cart(0, 1)) - @test collect(eachvertex(p)) == vertices(p) - @test eachvertexalloc(p) == 0 - # type stability - @test isconcretetype(eltype(vertices(p))) - @inferred vertices(p) + eachvertextest(p) # COMMAND USED TO GENERATE TEST FILES (VARY --seed = 1, 2, ..., 5) # rpg --cluster 30 --algo 2opt --format line --seed 1 --output poly1 @@ -758,14 +727,7 @@ end t = Tetrahedron(cart(0, 0, 0), cart(1, 0, 0), cart(0, 1, 0), cart(0, 0, 1)) equaltest(t) isapproxtest(t) - - # vertex iteration - t = Tetrahedron(cart(0, 0, 0), cart(1, 0, 0), cart(0, 1, 0), cart(0, 0, 1)) - @test collect(eachvertex(t)) == vertices(t) - @test eachvertexalloc(t) == 0 - # type stability - @test isconcretetype(eltype(vertices(t))) - @inferred vertices(t) + eachvertextest(t) # CRS propagation c1 = Cartesian{WGS84Latest}(T(0), T(0), T(0)) @@ -831,23 +793,7 @@ end ) equaltest(h) isapproxtest(h) - - # vertex iteration - h = Hexahedron( - cart(0, 0, 0), - cart(1, 0, 0), - cart(1, 1, 0), - cart(0, 1, 0), - cart(0, 0, 1), - cart(1, 0, 1), - cart(1, 1, 1), - cart(0, 1, 1) - ) - @test collect(eachvertex(h)) == vertices(h) - @test eachvertexalloc(h) == 0 - # type stability - @test isconcretetype(eltype(vertices(h))) - @inferred vertices(h) + eachvertextest(t) h = Hexahedron( cart(0, 0, 0), @@ -966,14 +912,7 @@ end @test m[5] isa Triangle equaltest(p) isapproxtest(p) - - # vertex iteration - p = Pyramid(cart(0, 0, 0), cart(1, 0, 0), cart(1, 1, 0), cart(0, 1, 0), cart(0, 0, 1)) - @test collect(eachvertex(p)) == vertices(p) - @test eachvertexalloc(p) == 0 - # type stability - @test isconcretetype(eltype(vertices(p))) - @inferred vertices(p) + eachvertextest(p) p = Pyramid(cart(0, 0, 0), cart(1, 0, 0), cart(1, 1, 0), cart(0, 1, 0), cart(0, 0, 1)) @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))" @@ -1012,14 +951,7 @@ end @test m[5] isa Quadrangle equaltest(w) isapproxtest(w) - - # vertex iteration - 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)) - @test collect(eachvertex(w)) == vertices(w) - @test eachvertexalloc(w) == 0 - # type stability - @test isconcretetype(eltype(vertices(w))) - @inferred vertices(w) + eachvertextest(w) 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)) @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))" diff --git a/test/testutils.jl b/test/testutils.jl index bd89935f3..f96740152 100644 --- a/test/testutils.jl +++ b/test/testutils.jl @@ -183,3 +183,11 @@ function eachvertexalloc(g) @allocated for _ in eachvertex(g) end end + +function eachvertextest(g; alloccheck=(==(0))) + @test collect(eachvertex(g)) == vertices(g) + @test alloccheck(eachvertexalloc(g)) + # type stability + @test isconcretetype(eltype(vertices(g))) + @inferred vertices(g) +end From 5d96dcf0c897a813b9ee79ed1fbce2b7261dd198 Mon Sep 17 00:00:00 2001 From: Elias Carvalho Date: Mon, 18 Nov 2024 16:32:09 -0300 Subject: [PATCH 38/41] Apply suggestions --- test/meshes.jl | 10 +++++----- test/multigeoms.jl | 6 +++--- test/polytopes.jl | 20 ++++++++++---------- test/testutils.jl | 4 ++-- 4 files changed, 20 insertions(+), 20 deletions(-) diff --git a/test/meshes.jl b/test/meshes.jl index 3ab6862a3..031354233 100644 --- a/test/meshes.jl +++ b/test/meshes.jl @@ -129,7 +129,7 @@ # vertex iteration grid = RegularGrid((10, 10), cart(0, 0), T.((1, 1))) - eachvertextest(grid) + vertextest(grid) # type stability grid = RegularGrid((10, 20), Point(Polar(T(0), T(0))), T.((1, 1))) @@ -509,7 +509,7 @@ end x = range(zero(T), stop=one(T), length=6) y = T[0.0, 0.1, 0.3, 0.7, 0.9, 1.0] grid = RectilinearGrid(x, y) - eachvertextest(grid) + vertextest(grid) # type stability x = range(zero(T), stop=one(T), length=6) * u"mm" @@ -696,7 +696,7 @@ end X = repeat(range(zero(T), stop=one(T), length=6), 1, 6) Y = repeat(T[0.0, 0.1, 0.3, 0.7, 0.9, 1.0]', 6, 1) grid = StructuredGrid(X, Y) - eachvertextest(grid) + vertextest(grid) # type stability X = repeat(range(zero(T), stop=one(T), length=6), 1, 6) * u"mm" @@ -949,7 +949,7 @@ end points = cart.([(0, 0), (1, 0), (0, 1), (1, 1), (0.5, 0.5)]) connec = connect.([(1, 2, 5), (2, 4, 5), (4, 3, 5), (3, 1, 5)], Triangle) mesh = SimpleMesh(points, connec) - eachvertextest(mesh) + vertextest(mesh) points = cart.([(0, 0), (1, 0), (0, 1), (1, 1), (0.5, 0.5)]) connec = connect.([(1, 2, 5), (2, 4, 5), (4, 3, 5), (3, 1, 5)], Triangle) @@ -1016,7 +1016,7 @@ end connec = connect.([(1, 2, 5), (2, 4, 5), (4, 3, 5), (3, 1, 5)], Triangle) mesh = SimpleMesh(points, connec) tmesh = TransformedMesh(mesh, trans) - eachvertextest(tmesh) + vertextest(tmesh) # transforms that change the Manifold and/or CRS points = latlon.([(0, 0), (0, 1), (1, 0), (1, 1), (0.5, 0.5)]) diff --git a/test/multigeoms.jl b/test/multigeoms.jl index 86f039a74..7e5a89887 100644 --- a/test/multigeoms.jl +++ b/test/multigeoms.jl @@ -110,7 +110,7 @@ multi1 = Multi([ring1, ring2, ring3, ring4]) multi2 = Multi([poly1, poly2]) multi3 = Multi([poly3, poly4]) - eachvertextest(multi1) - eachvertextest(multi2) - eachvertextest(multi3, alloccheck=(<(400))) + vertextest(multi1) + vertextest(multi2) + vertextest(multi3, bytes=400) end diff --git a/test/polytopes.jl b/test/polytopes.jl index 762638627..2f3f26ea8 100644 --- a/test/polytopes.jl +++ b/test/polytopes.jl @@ -42,7 +42,7 @@ s = Segment(cart(0, 0), cart(1, 1)) equaltest(s) isapproxtest(s) - eachvertextest(s) + vertextest(s) s = Segment(Point(1.0, 1.0, 1.0, 1.0), Point(2.0, 2.0, 2.0, 2.0)) @test all(Point(x, x, x, x) ∈ s for x in 1:0.01:2) @@ -119,12 +119,12 @@ end c = Rope(cart(0, 0), cart(1, 0), cart(0, 1)) equaltest(c) isapproxtest(c) - eachvertextest(c) + vertextest(c) c = Ring(cart(0, 0), cart(1, 0), cart(0, 1)) equaltest(c) isapproxtest(c) - eachvertextest(c) + vertextest(c) # circular equality c1 = Ring(cart.([(1, 1), (2, 2), (3, 3)])) @@ -355,7 +355,7 @@ end t = Triangle(cart(0, 0), cart(1, 0), cart(0, 1)) equaltest(t) isapproxtest(t) - eachvertextest(t) + vertextest(t) t = Triangle(cart(0, 0), cart(1, 0), cart(0, 1)) @test perimeter(t) ≈ T(1 + 1 + √2) * u"m" @@ -470,7 +470,7 @@ end q = Quadrangle(cart(0, 0), cart(1, 0), cart(1, 1), cart(0, 1)) equaltest(q) isapproxtest(q) - eachvertextest(q) + vertextest(q) q = Quadrangle(cart(0, 0), cart(1, 0), cart(1, 1), cart(0, 1)) @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)) @@ -542,7 +542,7 @@ end p = PolyArea(cart(0, 0), cart(1, 0), cart(0, 1)) equaltest(p) isapproxtest(p) - eachvertextest(p) + vertextest(p) # COMMAND USED TO GENERATE TEST FILES (VARY --seed = 1, 2, ..., 5) # rpg --cluster 30 --algo 2opt --format line --seed 1 --output poly1 @@ -727,7 +727,7 @@ end t = Tetrahedron(cart(0, 0, 0), cart(1, 0, 0), cart(0, 1, 0), cart(0, 0, 1)) equaltest(t) isapproxtest(t) - eachvertextest(t) + vertextest(t) # CRS propagation c1 = Cartesian{WGS84Latest}(T(0), T(0), T(0)) @@ -793,7 +793,7 @@ end ) equaltest(h) isapproxtest(h) - eachvertextest(t) + vertextest(t) h = Hexahedron( cart(0, 0, 0), @@ -912,7 +912,7 @@ end @test m[5] isa Triangle equaltest(p) isapproxtest(p) - eachvertextest(p) + vertextest(p) p = Pyramid(cart(0, 0, 0), cart(1, 0, 0), cart(1, 1, 0), cart(0, 1, 0), cart(0, 0, 1)) @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))" @@ -951,7 +951,7 @@ end @test m[5] isa Quadrangle equaltest(w) isapproxtest(w) - eachvertextest(w) + vertextest(w) 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)) @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))" diff --git a/test/testutils.jl b/test/testutils.jl index f96740152..1285e4e5d 100644 --- a/test/testutils.jl +++ b/test/testutils.jl @@ -184,9 +184,9 @@ function eachvertexalloc(g) end end -function eachvertextest(g; alloccheck=(==(0))) +function vertextest(g; bytes=0) @test collect(eachvertex(g)) == vertices(g) - @test alloccheck(eachvertexalloc(g)) + @test eachvertexalloc(g) ≤ bytes # type stability @test isconcretetype(eltype(vertices(g))) @inferred vertices(g) From 700c46b99fbd8b267764f7e29707e1c5ace775a1 Mon Sep 17 00:00:00 2001 From: Elias Carvalho <73039601+eliascarv@users.noreply.github.com> Date: Mon, 18 Nov 2024 16:32:31 -0300 Subject: [PATCH 39/41] Update test/testutils.jl MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: Júlio Hoffimann --- test/testutils.jl | 1 - 1 file changed, 1 deletion(-) diff --git a/test/testutils.jl b/test/testutils.jl index 1285e4e5d..9fbcf8f90 100644 --- a/test/testutils.jl +++ b/test/testutils.jl @@ -179,7 +179,6 @@ end function eachvertexalloc(g) iterate(eachvertex(g)) # precompile run - @allocated for _ in eachvertex(g) end end From 7218ba08dc39e217b142c80e83c6e8d370362ab3 Mon Sep 17 00:00:00 2001 From: Elias Carvalho Date: Mon, 18 Nov 2024 16:43:01 -0300 Subject: [PATCH 40/41] Apply suggestions --- src/geometries/multigeom.jl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/geometries/multigeom.jl b/src/geometries/multigeom.jl index fbbc61037..19696ae0a 100644 --- a/src/geometries/multigeom.jl +++ b/src/geometries/multigeom.jl @@ -54,7 +54,7 @@ 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)) +eachvertex(m::MultiPolytope) = (v for g in m.geoms for v in eachvertex(g)) Base.unique(m::MultiPolytope) = unique!(deepcopy(m)) From 6c5e330e241a352a975564812defc9573c3919ba Mon Sep 17 00:00:00 2001 From: Elias Carvalho Date: Mon, 18 Nov 2024 16:53:17 -0300 Subject: [PATCH 41/41] Fix tests --- test/multigeoms.jl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/multigeoms.jl b/test/multigeoms.jl index 7e5a89887..668582690 100644 --- a/test/multigeoms.jl +++ b/test/multigeoms.jl @@ -112,5 +112,5 @@ multi3 = Multi([poly3, poly4]) vertextest(multi1) vertextest(multi2) - vertextest(multi3, bytes=400) + vertextest(multi3, bytes=3100) end