Skip to content

Commit

Permalink
Merge pull request #3624 from JuliaReach/schillic/convert_HPoly
Browse files Browse the repository at this point in the history
Share `convert` code between `HPoly`
  • Loading branch information
schillic authored Jul 26, 2024
2 parents 39176b2 + 1bafde6 commit 6028ccb
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 17 deletions.
2 changes: 1 addition & 1 deletion docs/src/lib/sets/HPolyhedron.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ HPolyhedron

```@docs
convert(::Type{HPolyhedron}, ::LazySet)
convert(::Type{HPolyhedron}, ::HRep{N}) where {N}
convert(::Type{HPolyhedron}, ::HRep)
```

## Operations
Expand Down
18 changes: 4 additions & 14 deletions src/Sets/HPolyhedron/convert.jl
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,10 @@ end
function load_Polyhedra_convert_HPolyhedron()
return quote
using .Polyhedra: HRep
using ..HPolytopeModule: _convert_HPoly

"""
convert(::Type{HPolyhedron}, P::HRep{N}) where {N}
convert(::Type{HPolyhedron}, P::HRep)
Convert an `HRep` polyhedron from `Polyhedra.jl` to a polyhedron in constraint
representation .
Expand All @@ -51,19 +52,8 @@ function load_Polyhedra_convert_HPolyhedron()
An `HPolyhedron`.
"""
function convert(::Type{HPolyhedron}, P::HRep{N}) where {N}
VN = Polyhedra.hvectortype(P)
constraints = Vector{HalfSpace{N,VN}}()
for hi in Polyhedra.allhalfspaces(P)
a, b = hi.a, hi.β
if isapproxzero(norm(a))
@assert b >= zero(N) "the half-space is inconsistent since it has a zero " *
"normal direction but the constraint is negative"
continue
end
push!(constraints, HalfSpace(a, b))
end
return HPolyhedron(constraints)
function convert(::Type{HPolyhedron}, P::HRep)
return _convert_HPoly(HPolyhedron, P)
end
end
end # load_Polyhedra_convert_HPolyhedron
8 changes: 6 additions & 2 deletions src/Sets/HPolytope/convert.jl
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,11 @@ function load_Polyhedra_convert_HPolytope()
return quote
using .Polyhedra: HRep

function convert(::Type{HPolytope}, P::HRep{N}) where {N}
function convert(::Type{HPolytope}, P::HRep)
return _convert_HPoly(HPolytope, P)
end

function _convert_HPoly(T, P::HRep{N}) where {N}
VT = Polyhedra.hvectortype(P)
constraints = Vector{HalfSpace{N,VT}}()
for hi in Polyhedra.allhalfspaces(P)
Expand All @@ -48,7 +52,7 @@ function load_Polyhedra_convert_HPolytope()
end
push!(constraints, HalfSpace(a, b))
end
return HPolytope(constraints)
return T(constraints)
end
end
end # load_Polyhedra_convert_HPolytope

0 comments on commit 6028ccb

Please sign in to comment.