Skip to content

Commit

Permalink
Merge pull request #603 from JuliaReach/mforets/559
Browse files Browse the repository at this point in the history
#559 - Tohrep, tovrep for concrete polytopes
  • Loading branch information
mforets authored Sep 3, 2018
2 parents ce2cb0d + af64c2a commit 4b20264
Show file tree
Hide file tree
Showing 3 changed files with 98 additions and 3 deletions.
44 changes: 43 additions & 1 deletion src/HPolytope.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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}
Expand Down Expand Up @@ -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()
43 changes: 42 additions & 1 deletion src/VPolytope.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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}
Expand Down Expand Up @@ -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()
14 changes: 13 additions & 1 deletion test/unit_Polytope.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
# -----
Expand Down Expand Up @@ -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

0 comments on commit 4b20264

Please sign in to comment.