Skip to content
This repository was archived by the owner on Nov 22, 2023. It is now read-only.
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 10 additions & 1 deletion src/baseutils.jl
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,19 @@ Return copy of x with ith entry ommited.
Expr(:tuple, args...)
end

"""
deleteat{N,T,i}(x::SVector{N,T}, ::Type{Val{i}})
"""
@generated function deleteat(x::SVector{N,T}, ::Type{Val{i}}) where {N,T,i}
(1 <= i <= N) || throw(MethodError(drop_index, (x,Val{i})))
args = [:(x[$j]) for j in deleteat!([1:N...], i)]
Expr(:call, SVector{N-1,T}, args...)
end

"""
deleteat{N,T}(x::NTuple{N,T}, i::Int)
"""
@generated deleteat(x::NTuple{N,T}, i::Int) where {N,T} = quote
@generated deleteat(x::Union{NTuple{N,T},SVector{N,T}}, i::Int) where {N,T} = quote
# would be nice to eliminate boundscheck
(1 <= i <= N) || throw(BoundsError(x,i))
@nif $(N) d->(i==d) d-> deleteat(x, Val{d})
Expand Down
6 changes: 3 additions & 3 deletions src/convexhulls.jl
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ Base.copy(fl::FG) where {FG <: AFG} = FG(copy(vertices(fl)))
push(fl::AFG, pt) = push!(copy(fl), pt)

vertices(x::AbstractFlexibleGeometry) = x.vertices
vertices(s::Simplex) = Tuple(s)
vertices(s::Simplex) = SVector(s)

standard_cube_vertices(::Type{Val{1}}) = [Vec(0), Vec(1)]
_vcat(v1,v2) = Vec(Tuple(v1)..., Tuple(v2)...)
Expand Down Expand Up @@ -57,11 +57,11 @@ end

vertices(c::HyperCube) = vertices(convert(HyperRectangle, c))

vertexmat(s) = hcat(vertices(s)...)
vertexmat(s) = hcat(Tuple(vertices(s))...)
vertexmatrix(s::AbstractConvexHull) = Matrix(vertexmat(s))::Matrix{numtype(s)}

(::Type{F})(g::Union{AbstractSimplex, AFG, GeometryPrimitive}) where {F <: AFG} = F(vertices(g))
(::Type{F})(v::NTuple) where {F <: AFG} = F(collect(v))
(::Type{F})(v::Union{NTuple,StaticArray}) where {F <: AFG} = F(collect(v))
Base.convert(::Type{F}, s::Simplex) where {F <: AFG} = F(s)

(::Type{R})(c::HyperCube) where {R <: HyperRectangle} = R(origin(c), widths(c))
Expand Down
3 changes: 3 additions & 0 deletions src/simplices.jl
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@
function (::Type{S})(sv::StaticVector) where S <: Simplex
Simplex{1, typeof(sv)}((sv,))
end
function (::Type{S})(sv::StaticVector{N,T}) where {S <: Simplex, N, T<:StaticVector}
Simplex{length(sv),eltype(sv)}((sv...,))
end

@inline function Simplex{S, T}(x) where {S, T}
Simplex{S, T}(ntuple(i-> T(x), Val(S)))
Expand Down
17 changes: 17 additions & 0 deletions test/simplices.jl
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
using StaticArrays: @SVector

@testset "example simplices" begin
@testset "2d simplex in 2d" begin
s = Simplex((Vec(1.0,0.0), Vec(0.0,1.0), Vec(0.0,0.0)))
Expand Down Expand Up @@ -67,4 +69,19 @@
@test contains(s, Vec(0.2, 0.2, 0.2))
@test !(contains(s, Vec(0.1, 0.0999, 0.1)))
end

@testset "Simplex SVector interaction" begin
# SVector of StaticVectors is interpreted as multiple vertices
s0 = Simplex((Vec(1.0,0.0,0.0), Vec(0.0,1.0,0.0), Vec(0.0,0.0,1.0), Vec(0.0,0.0,0.0)))
s1 = Simplex(@SVector [Vec(1.0,0.0,0.0), Vec(0.0,1.0,0.0), Vec(0.0,0.0,1.0), Vec(0.0,0.0,0.0)])

@test length(s0) == 4
@test length(s0[1]) == 3
@test s0 === s1

# SVector of Numbers is interpreted as single vertex
s3 = Simplex(@SVector[1.0, 0.0, 1.0])
@test length(s3) == 1
@test length(s3[1]) == 3
end
end