Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Share convert code between HPoly #3624

Merged
merged 1 commit into from
Jul 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading