diff --git a/src/HPolytope.jl b/src/HPolytope.jl index 72efdd8538..d1a4f68fc1 100644 --- a/src/HPolytope.jl +++ b/src/HPolytope.jl @@ -213,7 +213,7 @@ import Polyhedra:polyhedron, SimpleHRepresentation, SimpleHRepresentation, hcartesianproduct, points -export intersection, convex_hull, cartesian_product, vertices_list +export intersection, convex_hull, cartesian_product, vertices_list, tovrep, tohrep # HPolytope from an HRep function HPolytope(P::HRep{N, T}, backend=CDDLib.CDDLibrary()) where {N, T} @@ -370,5 +370,47 @@ function vertices_list(P::HPolytope{N}; prunefunc(P) return collect(points(P)) end + +""" + tovrep(P::HPolytope; backend=CDDLib.CDDLibrary()) + +Transform a polytope in H-representation to a polytope in V-representation. + +### Input + +- `P` -- polytope in constraint representation +- `backend` -- (optional, default: `CDDLib.CDDLibrary()`) the polyhedral + computations backend, + see [Polyhedra's documentation](https://juliapolyhedra.github.io/Polyhedra.jl/latest/installation.html#Getting-Libraries-1) + for further information + +### Output + +The `VPolytope` which is the vertex representation of the given polytope +in constraint representation. +""" +function tovrep(P::HPolytope; backend=CDDLib.CDDLibrary()) + P = polyhedron(P, backend) + return VPolytope(P) +end + +""" + tohrep(P::HPolytope) + +Return a constraint representation of the given polytope in constraint +representation (no-op). + +### Input + +- `P` -- polytope in constraint representation + +### Output + +The same polytope instance. +""" +function tohrep(P::HPolytope) + return P +end + end # quote end # function load_polyhedra_hpolytope() diff --git a/src/VPolytope.jl b/src/VPolytope.jl index f8b51d5680..aee02c8405 100644 --- a/src/VPolytope.jl +++ b/src/VPolytope.jl @@ -129,7 +129,7 @@ import Polyhedra:polyhedron, SimpleHRepresentation, SimpleVRepresentation, convexhull, hcartesianproduct -export intersection, convex_hull, cartesian_product, vertices_list +export intersection, convex_hull, cartesian_product, vertices_list, tohrep, tovrep # VPolytope from a VRep function VPolytope(P::VRep{N, T}, backend=CDDLib.CDDLibrary()) where {N, T} @@ -237,5 +237,46 @@ function cartesian_product(P1::VPolytope, P2::VPolytope; backend=CDDLib.CDDLibra return VPolytope(Pcp) end +""" + tohrep(P::VPolytope; backend=CDDLib.CDDLibrary()) + +Transform a polytope in V-representation to a polytope in H-representation. + +### Input + +- `P` -- polytope in vertex representation +- `backend` -- (optional, default: `CDDLib.CDDLibrary()`) the polyhedral + computations backend, + see [Polyhedra's documentation](https://juliapolyhedra.github.io/Polyhedra.jl/latest/installation.html#Getting-Libraries-1) + for further information + +### Output + +The `HPolytope` which is the constraint representation of the given polytope +in vertex representation. +""" +function tohrep(P::VPolytope; backend=CDDLib.CDDLibrary()) + P = polyhedron(P, backend) + return HPolytope(P) +end + +""" + tovrep(P::VPolytope) + +Return a vertex representation of the given polytope in vertex +representation (no-op). + +### Input + +- `P` -- polytope in vertex representation + +### Output + +The same polytope instance. +""" +function tovrep(P::VPolytope) + return P +end + end # quote end # function load_polyhedra_vpolytope() diff --git a/test/unit_Polytope.jl b/test/unit_Polytope.jl index d86bb42a88..de6fc5e543 100644 --- a/test/unit_Polytope.jl +++ b/test/unit_Polytope.jl @@ -72,7 +72,7 @@ for N in [Float64, Rational{Int}, Float32] d = N[1, 0] @test_throws ErrorException σ(d, p, algorithm="xyz") if test_suite_polyhedra - @test_throws MethodError σ(d, p) # not implemented yet + @test σ(d, p) == N[1.0, 0.0] else @test_throws AssertionError σ(d, p) end @@ -128,6 +128,13 @@ if test_suite_polyhedra vl = vertices_list(p) @test length(vl) == 2 && N[0] ∈ vl && [1] ∈ vl + # tovrep from HPolytope + A = [N(0) N(-1); N(-1) N(0); N(1) N(1)] + b = N[-0.25, -0.25, -0] + P = HPolytope(A, b) + @test tovrep(P) isa VPolytope + @test tohrep(P) isa HPolytope # test no-op + # ----- # V-rep # ----- @@ -160,5 +167,10 @@ if test_suite_polyhedra cp = cartesian_product(p1, p2) vl = vertices_list(cp) @test length(vl) == 2 && N[0, 0, 2] ∈ vl && N[1, 1, 2] ∈ vl + + # tohrep from VPolytope + P = VPolytope([v1, v2, v3, v4, v5]) + @test tohrep(P) isa HPolytope + @test tovrep(P) isa VPolytope # no-op end end