Skip to content

Commit

Permalink
Use SVector in Polytope instead of NTuple (#1130)
Browse files Browse the repository at this point in the history
* Use 'SVector' in 'Polytope' instead of 'NTuple'

* Update constructors

* Fix constructors

* Fix tests
  • Loading branch information
eliascarv authored Nov 12, 2024
1 parent cbd8f42 commit 243d3f0
Show file tree
Hide file tree
Showing 7 changed files with 20 additions and 16 deletions.
5 changes: 3 additions & 2 deletions src/geometries/polytopes.jl
Original file line number Diff line number Diff line change
Expand Up @@ -32,20 +32,21 @@ macro polytope(type, K, N)
structexpr = if K == 3
quote
struct $type{C<:CRS,Mₚ<:Manifold} <: Polytope{$K,𝔼{3},C}
vertices::NTuple{$N,Point{Mₚ,C}}
vertices::SVector{$N,Point{Mₚ,C}}
end
end
else
quote
struct $type{M<:Manifold,C<:CRS} <: Polytope{$K,M,C}
vertices::NTuple{$N,Point{M,C}}
vertices::SVector{$N,Point{M,C}}
end
end
end

expr = quote
$Base.@__doc__ $structexpr

$type(vertices::NTuple{$N,P}) where {P<:Point} = $type(SVector(vertices))
$type(vertices::Vararg{Tuple,$N}) = $type(Point.(vertices))
$type(vertices::Vararg{P,$N}) where {P<:Point} = $type(vertices)
end
Expand Down
8 changes: 5 additions & 3 deletions src/geometries/polytopes/ngon.jl
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ are `Triangle` (N=3), `Quadrangle` (N=4), `Pentagon` (N=5), etc.
`Heptagon`, `Octagon`, `Nonagon`, `Decagon`.
"""
struct Ngon{N,M<:Manifold,C<:CRS} <: Polygon{M,C}
vertices::NTuple{N,Point{M,C}}
vertices::SVector{N,Point{M,C}}
function Ngon{N,M,C}(vertices) where {N,M<:Manifold,C<:CRS}
if N < 3
throw(ArgumentError("the number of vertices must be greater than or equal to 3"))
Expand All @@ -30,11 +30,13 @@ struct Ngon{N,M<:Manifold,C<:CRS} <: Polygon{M,C}
end
end

Ngon{N}(vertices::NTuple{N,Point{M,C}}) where {N,M<:Manifold,C<:CRS} = Ngon{N,M,C}(vertices)
Ngon{N}(vertices::SVector{N,Point{M,C}}) where {N,M<:Manifold,C<:CRS} = Ngon{N,M,C}(vertices)
Ngon{N}(vertices::NTuple{N,P}) where {N,P<:Point} = Ngon{N}(SVector(vertices))
Ngon{N}(vertices::Vararg{P,N}) where {N,P<:Point} = Ngon{N}(vertices)
Ngon{N}(vertices::Vararg{Tuple,N}) where {N} = Ngon{N}(Point.(vertices))

Ngon(vertices::NTuple{N,Point{M,C}}) where {N,M<:Manifold,C<:CRS} = Ngon{N,M,C}(vertices)
Ngon(vertices::SVector{N,Point{M,C}}) where {N,M<:Manifold,C<:CRS} = Ngon{N,M,C}(vertices)
Ngon(vertices::NTuple{N,P}) where {N,P<:Point} = Ngon(SVector(vertices))
Ngon(vertices::P...) where {P<:Point} = Ngon(vertices)
Ngon(vertices::Tuple...) = Ngon(Point.(vertices))

Expand Down
2 changes: 1 addition & 1 deletion src/transforms.jl
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ applycoord(t::CoordinateTransform, g::TransformedGeometry) = TransformedGeometry
applycoord(t::CoordinateTransform, m::TransformedMesh) = TransformedMesh(m, t)

# special treatment for lists of geometries
applycoord(t::CoordinateTransform, g::NTuple{<:Any,<:Geometry}) = map(gᵢ -> applycoord(t, gᵢ), g)
applycoord(t::CoordinateTransform, g::StaticVector{<:Any,<:Geometry}) = map(gᵢ -> applycoord(t, gᵢ), g)
applycoord(t::CoordinateTransform, g::AbstractVector{<:Geometry}) = [applycoord(t, gᵢ) for gᵢ in g]
applycoord(t::CoordinateTransform, g::CircularVector{<:Geometry}) = CircularVector([applycoord(t, gᵢ) for gᵢ in g])

Expand Down
4 changes: 2 additions & 2 deletions test/hulls.jl
Original file line number Diff line number Diff line change
Expand Up @@ -90,11 +90,11 @@
# degenerate cases
points = [cart(0, 0), cart(1, 0), cart(2, 0)]
chull = hull(points, method)
@test vertices(chull) == (cart(0, 0), cart(2, 0))
@test vertices(chull) == SVector(cart(0, 0), cart(2, 0))

points = [cart(0, 0), cart(1, 0), cart(2, 0), cart(10, 0), cart(100, 0)]
chull = hull(points, method)
@test vertices(chull) == (cart(0, 0), cart(100, 0))
@test vertices(chull) == SVector(cart(0, 0), cart(100, 0))

# partially collinear
points = [
Expand Down
2 changes: 1 addition & 1 deletion test/meshes.jl
Original file line number Diff line number Diff line change
Expand Up @@ -244,7 +244,7 @@ end
@test spacing(grid) == (T(5) * u"m", T(5) * u"m", T(5) * u"m")
@test nelements(grid) == 20 * 10 * 5
@test eltype(grid) <: Hexahedron
@test vertices(grid[1]) == (
@test vertices(grid[1]) == SVector(
cart(0, 0, 0),
cart(5, 0, 0),
cart(5, 5, 0),
Expand Down
13 changes: 7 additions & 6 deletions test/polytopes.jl
Original file line number Diff line number Diff line change
Expand Up @@ -293,13 +293,14 @@ end
@testitem "Ngons" setup = [Setup] begin
pts = (cart(0, 0), cart(1, 0), cart(0, 1))
tups = (T.((0, 0)), T.((1, 0)), T.((0, 1)))
verts = SVector(pts)
@test paramdim(Ngon) == 2
@test vertices(Ngon(pts)) == pts
@test vertices(Ngon(pts...)) == pts
@test vertices(Ngon(tups...)) == pts
@test vertices(Ngon{3}(pts)) == pts
@test vertices(Ngon{3}(pts...)) == pts
@test vertices(Ngon{3}(tups...)) == pts
@test vertices(Ngon(pts)) == verts
@test vertices(Ngon(pts...)) == verts
@test vertices(Ngon(tups...)) == verts
@test vertices(Ngon{3}(pts)) == verts
@test vertices(Ngon{3}(pts...)) == verts
@test vertices(Ngon{3}(tups...)) == verts

NGONS = [Triangle, Quadrangle, Pentagon, Hexagon, Heptagon, Octagon, Nonagon, Decagon]
NVERT = 3:10
Expand Down
2 changes: 1 addition & 1 deletion test/testutils.jl
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ withprecision(T, v::Vec) = numconvert.(T, v)
withprecision(T, p::Point) = Meshes.withcrs(p, withprecision(T, to(p)))
withprecision(T, len::Meshes.Len) = numconvert(T, len)
withprecision(T, lens::NTuple{Dim,Meshes.Len}) where {Dim} = numconvert.(T, lens)
withprecision(T, geoms::NTuple{Dim,<:Geometry}) where {Dim} = withprecision.(T, geoms)
withprecision(T, geoms::StaticVector{Dim,<:Geometry}) where {Dim} = withprecision.(T, geoms)
withprecision(T, geoms::AbstractVector{<:Geometry}) = [withprecision(T, g) for g in geoms]
withprecision(T, geoms::CircularVector{<:Geometry}) = CircularVector([withprecision(T, g) for g in geoms])
@generated function withprecision(T, g::G) where {G<:Meshes.GeometryOrDomain}
Expand Down

0 comments on commit 243d3f0

Please sign in to comment.