Skip to content

Commit

Permalink
Implement inplace convexhull! for vector representation (JuliaPolyhed…
Browse files Browse the repository at this point in the history
  • Loading branch information
blegat committed Jan 24, 2021
1 parent b6490a2 commit ac71431
Show file tree
Hide file tree
Showing 2 changed files with 69 additions and 18 deletions.
8 changes: 8 additions & 0 deletions src/vecrep.jl
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,8 @@ similar_type(PT::Type{<:PointsHull}, d::FullDim, ::Type{T}) where {T} = PointsHu

vreptype(::Type{PointsHull{T, AT, D}}) where {T, AT, D} = Hull{T, AT, D}

convexhull!(v::PointsHull, p::AbstractVector) = push!(v.points, p)

@vecrepelem PointsHull Point points

"""
Expand Down Expand Up @@ -225,6 +227,9 @@ FullDim(v::RaysHull) = FullDim(v.lines)
vvectortype(::Type{<:RaysHull{T, AT}}) where {T, AT} = AT
similar_type(PT::Type{<:RaysHull}, d::FullDim, ::Type{T}) where {T} = RaysHull{T, similar_type(vvectortype(PT), d, T), typeof(d)}

convexhull!(v::RaysHull, l::Line) = convexhull!(v.lines, l)
convexhull!(v::RaysHull, r::Ray) = push!(v.rays, r)

@vecrepelem RaysHull Ray rays
@subrepelem RaysHull Line lines

Expand Down Expand Up @@ -267,6 +272,9 @@ similar_type(PT::Type{<:Hull}, d::FullDim, ::Type{T}) where {T} = Hull{T, simila
Hull(v::VRepresentation{T}) where {T} = Hull{T}(v)
Hull{T}(v::VRepresentation) where {T} = convert(Hull{T, similar_type(vvectortype(typeof(v)), T), typeof(FullDim(v))}, v)

convexhull!(v::Hull, p::AbstractVector) = convexhull!(v.points, p)
convexhull!(v::Hull, r::VStruct) = convexhull!(v.rays, r)

@subrepelem Hull Point points
@subrepelem Hull Line rays
@subrepelem Hull Ray rays
Expand Down
79 changes: 61 additions & 18 deletions test/vecrep.jl
Original file line number Diff line number Diff line change
@@ -1,22 +1,65 @@
function convexhull_test()
v = convexhull([1, 0, 0]) + conichull(Ray([0, 1, 0]), Line([0, 0, 1]))
@test npoints(v) == 1
@test nlines(v) == 1
@test nrays(v) == 1
ps = v.points
_ps = ps.points
rs = v.rays
_ls = rs.lines
__ls = _ls.lines
_rs = rs.rays
function _inplace()
@test v.points === ps
@test v.rays === rs
@test v.points.points === _ps
@test v.rays.lines === _ls
@test v.rays.lines.lines === __ls
@test v.rays.rays === _rs
end
convexhull!(v, [2, 0, 0])
_inplace()
@test npoints(v) == 2
@test nlines(v) == 1
@test nrays(v) == 1
convexhull!(v, Ray([0, 2, 0]))
_inplace()
@test npoints(v) == 2
@test nlines(v) == 1
@test nrays(v) == 2
convexhull!(v, Line([1, 0, 0]))
_inplace()
@test npoints(v) == 2
@test nlines(v) == 2
@test nrays(v) == 2
end

function intersect_test()
h = HalfSpace([1, 0], 1) HyperPlane([0, 1], 1)
@test nhyperplanes(h) == 1
@test nhalfspaces(h) == 1
hs = h.halfspaces
hp = h.hyperplanes
_hp = h.hyperplanes.hyperplanes
intersect!(h, HalfSpace([1, 1], 2))
@test h.halfspaces === hs
@test h.hyperplanes === hp
@test h.hyperplanes.hyperplanes === _hp
@test nhyperplanes(h) == 1
@test nhalfspaces(h) == 2
intersect!(h, HyperPlane([1, 1], 3))
@test h.halfspaces === hs
@test h.hyperplanes === hp
@test h.hyperplanes.hyperplanes === _hp
@test nhyperplanes(h) == 2
@test nhalfspaces(h) == 2
end

@testset "VecRep" begin
@testset "Convex hull" begin
convexhull_test()
end
@testset "Intersection" begin
h = HalfSpace([1, 0], 1) HyperPlane([0, 1], 1)
@test nhyperplanes(h) == 1
@test nhalfspaces(h) == 1
hs = h.halfspaces
hp = h.hyperplanes
_hp = h.hyperplanes.hyperplanes
intersect!(h, HalfSpace([1, 1], 2))
@test h.halfspaces === hs
@test h.hyperplanes === hp
@test h.hyperplanes.hyperplanes === _hp
@test nhyperplanes(h) == 1
@test nhalfspaces(h) == 2
intersect!(h, HyperPlane([1, 1], 3))
@test h.halfspaces === hs
@test h.hyperplanes === hp
@test h.hyperplanes.hyperplanes === _hp
@test nhyperplanes(h) == 2
@test nhalfspaces(h) == 2
intersect_test()
end
end

0 comments on commit ac71431

Please sign in to comment.