From 514a92aaacd394d07a109f49de996588c67e9e94 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Beno=C3=AEt=20Legat?= Date: Fri, 19 Jun 2020 13:57:01 +0200 Subject: [PATCH] Pass solver to removehrendundancy --- Project.toml | 8 ++-- src/polyhedron.jl | 110 ++++++++++++++++++++++------------------------ 2 files changed, 57 insertions(+), 61 deletions(-) diff --git a/Project.toml b/Project.toml index 465840e..7c286ad 100644 --- a/Project.toml +++ b/Project.toml @@ -1,7 +1,7 @@ name = "QHull" uuid = "a8468747-bd6f-53ef-9e5c-744dbc5c59e7" repo = "https://github.com/JuliaPolyhedra/QHull.jl.git" -version = "0.2.0" +version = "0.2.1" [deps] Polyhedra = "67491407-f73d-577b-9b50-8179a7c68029" @@ -9,7 +9,7 @@ PyCall = "438e738f-606a-5dbb-bf0a-cddfbfd45ab0" StaticArrays = "90137ffa-7385-5640-81b9-e52037218182" [compat] -Polyhedra = "0.6" +Polyhedra = "0.6.5" PyCall = "1.91" StaticArrays = "0.12" julia = "1" @@ -19,8 +19,10 @@ Combinatorics = "861a8166-3701-5b0c-9a16-15d98fcdc6aa" GLPK = "60bf3e95-4087-53dc-ae20-288a0d20c6a6" GeometryTypes = "4d00f742-c7ba-57c2-abde-4428a4b178cb" JuMP = "4076af6c-e467-56ae-b986-b466b2749572" +LinearAlgebra = "37e2e46d-f89d-539d-b4ee-838fcccc9c8e" RecipesBase = "3cdcf5f2-1ef4-517c-9805-6587b60abb01" +SparseArrays = "2f01184e-e22b-5df5-ae63-d93ebab69eaf" Test = "8dfed614-e22c-5e08-85e1-65c5234f0b40" [targets] -test = ["Combinatorics", "GLPK", "Test", "JuMP", "GeometryTypes", "RecipesBase"] +test = ["Combinatorics", "GLPK", "GeometryTypes", "JuMP", "LinearAlgebra", "RecipesBase", "SparseArrays", "Test"] diff --git a/src/polyhedron.jl b/src/polyhedron.jl index 5df0a41..8ef46d6 100644 --- a/src/polyhedron.jl +++ b/src/polyhedron.jl @@ -18,8 +18,8 @@ function Polyhedra.similar_library(l::Library, d::Polyhedra.FullDim, ::Type{Floa end mutable struct Polyhedron <: Polyhedra.Polyhedron{Float64} - ine::Union{Nothing, Polyhedra.MixedMatHRep{Float64, Matrix{Float64}}} - ext::Union{Nothing, Polyhedra.MixedMatVRep{Float64, Matrix{Float64}}} + hrep::Union{Nothing, Polyhedra.MixedMatHRep{Float64, Matrix{Float64}}} + vrep::Union{Nothing, Polyhedra.MixedMatVRep{Float64, Matrix{Float64}}} nohredundancy::Bool novredundancy::Bool solver @@ -37,7 +37,7 @@ mutable struct Polyhedron <: Polyhedra.Polyhedron{Float64} end end -Polyhedra.FullDim(p::Polyhedron) = Polyhedra.FullDim_rep(p.ine, p.ext) +Polyhedra.FullDim(p::Polyhedron) = Polyhedra.FullDim_rep(p.hrep, p.vrep) Polyhedra.library(p::Polyhedron) = Library(p.solver) Polyhedra.similar_type(::Type{<:Polyhedron}, d::Polyhedra.FullDim, T::Type) = Polyhedra.default_type(d, T) function Polyhedra.similar_type(::Type{<:Polyhedron}, d::Polyhedra.FullDim, ::Type{Float64}) @@ -58,24 +58,25 @@ Polyhedra.vvectortype(::Union{Polyhedron, Type{Polyhedron}}) = Polyhedra.vvector epsz = 1e-8 function qhull(p::Polyhedron, rep=:Auto) - if rep == :V || (rep == :Auto && (p.ext !== nothing)) - p.ext, ine, p.area, p.volume = qhull(getext(p), p.solver) - if p.ine === nothing + if rep == :V || (rep == :Auto && (p.vrep !== nothing)) + p.vrep, ine, p.area, p.volume = qhull(Polyhedra.vrep(p), p.solver) + if p.hrep === nothing # Otherwise, it is not interesting as it may have redundancy - p.ine = ine + p.hrep = ine end else @assert rep == :H || rep == :Auto - p.ine, ext = qhull(getine(p), p.solver) - if p.ext === nothing - p.ext = ext + p.hrep, ext = qhull(Polyhedra.hrep(p), p.solver) + if p.vrep === nothing + p.vrep = ext end end p.nohredundancy = true p.novredundancy = true + return end -function qhull(h::Polyhedra.MixedMatVRep{T}, solver=nothing) where T +function qhull(h::Polyhedra.MixedMatVRep{T}, solver) where T if Polyhedra.hasrays(h) error("Rays are not supported.") end @@ -90,11 +91,11 @@ function qhull(h::Polyhedra.MixedMatVRep{T}, solver=nothing) where T # The same facet may be listed several times in case the facet is not # simplicial, e.g. a quad facet is listed twice as it is made of two # triangles. After `removeduplicates`, there should be no redundancy. - hnored = Polyhedra.removeduplicates(h) + hnored = Polyhedra.removeduplicates(h, Polyhedra.OppositeMockOptimizer) vnored, hnored, ch.area, ch.volume end -function qhull(h::Polyhedra.MixedMatHRep{T}, solver=nothing) where {T<:Real} +function qhull(h::Polyhedra.MixedMatHRep{T}, solver) where {T<:Real} linset = h.linset if !isempty(linset) error("Equalities are not supported.") @@ -162,22 +163,9 @@ function qhull(h::Polyhedra.MixedMatHRep{T}, solver=nothing) where {T<:Real} hnored, vnored end -function getine(p::Polyhedron) - if p.ine === nothing - qhull(p) - end - p.ine -end -function getext(p::Polyhedron) - if p.ext === nothing - qhull(p) - end - p.ext -end - function clearfield!(p::Polyhedron) - p.ine = nothing - p.ext = nothing + p.hrep = nothing + p.vrep = nothing p.nohredundancy = false p.novredundancy = false end @@ -195,41 +183,15 @@ Polyhedron(d::Polyhedra.FullDim, ps::Polyhedra.PointIt, ls::Polyhedra.LineIt, rs function Base.copy(p::Polyhedron) ine = nothing - if p.ine !== nothing - ine = copy(p.ine) + if p.hrep !== nothing + ine = copy(p.hrep) end ext = nothing - if p.ext !== nothing - ext = copy(p.ext) + if p.vrep !== nothing + ext = copy(p.vrep) end Polyhedron(ine, ext, p.nohredundancy, p.novredundancy, p.solver) end -function Polyhedra.hrepiscomputed(p::Polyhedron) - p.ine !== nothing -end -function Polyhedra.hrep(p::Polyhedron) - getine(p) -end -function Polyhedra.vrepiscomputed(p::Polyhedron) - p.ext !== nothing -end -function Polyhedra.vrep(p::Polyhedron) - getext(p) -end -function Polyhedra.sethrep!(p::Polyhedron, h::Polyhedra.HRepresentation) - p.ine = h -end -function Polyhedra.setvrep!(p::Polyhedron, v::Polyhedra.VRepresentation) - p.ext = v -end -function Polyhedra.resethrep!(p::Polyhedron, h::Polyhedra.HRepresentation) - clearfield!(p) - p.ine = h -end -function Polyhedra.resetvrep!(p::Polyhedron, v::Polyhedra.VRepresentation) - clearfield!(p) - p.ext = v -end function Polyhedra.removehredundancy!(p::Polyhedron) if !p.nohredundancy qhull(p, :H) @@ -247,3 +209,35 @@ function Polyhedra.volume(p::Polyhedron) end return p.volume end + +Polyhedra.hrepiscomputed(p::Polyhedron) = p.hrep !== nothing +Polyhedra.computehrep!(p::Polyhedron) = qhull(p, :V) +function Polyhedra.hrep(p::Polyhedron) + if !Polyhedra.hrepiscomputed(p) + Polyhedra.computehrep!(p) + end + return p.hrep +end +Polyhedra.vrepiscomputed(p::Polyhedron) = p.vrep !== nothing +Polyhedra.computevrep!(p::Polyhedron) = qhull(p, :H) +function Polyhedra.vrep(p::Polyhedron) + if !Polyhedra.vrepiscomputed(p) + Polyhedra.computevrep!(p) + end + return p.vrep +end + +function Polyhedra.sethrep!(p::Polyhedron, h::Polyhedra.HRepresentation) + p.hrep = h +end +function Polyhedra.setvrep!(p::Polyhedron, v::Polyhedra.VRepresentation) + p.vrep = v +end +function Polyhedra.resethrep!(p::Polyhedron, h::Polyhedra.HRepresentation) + clearfield!(p) + p.hrep = h +end +function Polyhedra.resetvrep!(p::Polyhedron, v::Polyhedra.VRepresentation) + clearfield!(p) + p.vrep = v +end