Skip to content

Commit

Permalink
HPolygon/HPolytope conversion
Browse files Browse the repository at this point in the history
  • Loading branch information
schillic committed Feb 8, 2018
1 parent ab5d2c1 commit 9082de6
Show file tree
Hide file tree
Showing 7 changed files with 57 additions and 21 deletions.
5 changes: 5 additions & 0 deletions src/HPolygon.jl
Original file line number Diff line number Diff line change
Expand Up @@ -22,15 +22,20 @@ Use `addconstraint!` to iteratively add the edges in a sorted way.
-- default constructor
- `HPolygon()`
-- constructor with no constraints
- `HPolygon(S::LazySet)` -- constructor from another set
"""
struct HPolygon{N<:Real} <: AbstractHPolygon{N}
constraints::Vector{LinearConstraint{N}}
end
# constructor for an HPolygon with no constraints
HPolygon{N}() where {N<:Real} = HPolygon{N}(Vector{N}(0))

# constructor for an HPolygon with no constraints of type Float64
HPolygon() = HPolygon{Float64}()

# conversion constructor
HPolygon(S::LazySet) = convert(HPolygon, S)


# --- LazySet interface functions ---

Expand Down
24 changes: 8 additions & 16 deletions src/HPolygonOpt.jl
Original file line number Diff line number Diff line change
Expand Up @@ -26,34 +26,26 @@ The default constructor assumes that the given list of edges is sorted.
It *does not perform* any sorting.
Use `addconstraint!` to iteratively add the edges in a sorted way.
- `HPolygonOpt(constraints::Vector{LinearConstraint{<:Real}}, ind::Int)`
-- default constructor
- `HPolygonOpt(constraints::Vector{LinearConstraint{<:Real}})`
-- constructor without index
- `HPolygonOpt(H::HPolygon{<:Real})`
-- constructor from an HPolygon
- `HPolygonOpt(constraints::Vector{LinearConstraint{<:Real}}, [ind]::Int)`
-- default constructor with optional index
- `HPolygonOpt(S::LazySet)` -- constructor from another set
"""
mutable struct HPolygonOpt{N<:Real} <: AbstractHPolygon{N}
constraints::Vector{LinearConstraint{N}}
ind::Int

# default constructor
HPolygonOpt{N}(constraints::Vector{LinearConstraint{N}},
ind::Int) where {N<:Real} =
ind::Int=1) where {N<:Real} =
new{N}(constraints, ind)
end
# type-less convenience constructor
# type-less convenience constructor with optional index
HPolygonOpt(constraints::Vector{LinearConstraint{N}},
ind::Int) where {N<:Real} =
ind::Int=1) where {N<:Real} =
HPolygonOpt{N}(constraints, ind)

# type-less convenience constructor without index
HPolygonOpt(constraints::Vector{LinearConstraint{N}}) where {N<:Real} =
HPolygonOpt{N}(constraints, 1)

# constructor from an HPolygon
HPolygonOpt(H::HPolygon{N}) where {N<:Real} =
HPolygonOpt{N}(H.constraints, 1)
# conversion constructor
HPolygonOpt(S::LazySet) = convert(HPolygonOpt, S)


# --- LazySet interface functions ---
Expand Down
4 changes: 2 additions & 2 deletions src/HPolytope.jl
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,8 @@ HPolytope{N}() where {N<:Real} = HPolytope{N}(Vector{N}(0))
# constructor for a HPolytope with no constraints of type Float64
HPolytope() = HPolytope{Float64}()

# constructor from a polygon in H-representation
HPolytope(P::HPolygon) = HPolytope(P.constraints)
# conversion constructor
HPolytope(S::LazySet) = convert(HPolytope, S)

# constructor for a HPolytope from a simple H-representation
function HPolytope(A::Matrix{N}, b::Vector{N}) where {N<:Real}
Expand Down
5 changes: 5 additions & 0 deletions src/LazySets.jl
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,11 @@ include("LinearMap.jl")
include("MinkowskiSum.jl")
include("SymmetricIntervalHull.jl")

# =============================
# Conversions between set types
# =============================
include("convert.jl")

# =======================================
# Algorithms for check operations on sets
# =======================================
Expand Down
22 changes: 22 additions & 0 deletions src/convert.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
#= conversion between set types =#

# from polygon in H-representation to polygon in H-representation
function convert(::Type{HPOLYGON1},
P::HPOLYGON2) where {HPOLYGON1<:AbstractHPolygon,
HPOLYGON2<:AbstractHPolygon}
return HPOLYGON1(P.constraints)
end

# from polygon in H-representation to polytope in H-representation
function convert(::Type{HPolytope}, P::AbstractHPolygon)
return HPolytope(P.constraints)
end

# from polytope in H-representation to polygon in H-representation
function convert(::Type{HPOLYGON},
P::HPolytope) where {HPOLYGON<:AbstractHPolygon}
if dim(P) != 2
error("polytope must be 2D for conversion")
end
return HPOLYGON(P.constraints)
end
2 changes: 1 addition & 1 deletion test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ include("to_N.jl")
@time @testset "LazySets.BallInf" begin include("unit_BallInf.jl") end
@time @testset "LazySets.Hyperrectangle" begin include("unit_Hyperrectangle.jl") end
@time @testset "LazySets.Polygon" begin include("unit_Polygon.jl") end
@time @testset "LazySets.HPolytope" begin include("unit_Polytope.jl") end
@time @testset "LazySets.Polytope" begin include("unit_Polytope.jl") end
@time @testset "LazySets.Zonotope" begin include("unit_Zonotope.jl") end
@time @testset "LazySets.ZeroSet" begin include("unit_ZeroSet.jl") end
@time @testset "LazySets.EmptySet" begin include("unit_EmptySet.jl") end
Expand Down
16 changes: 14 additions & 2 deletions test/unit_Polygon.jl
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,21 @@ for N in [Float64, Float32, Rational{Int}]
@test p.constraints[3] == c3
@test p.constraints[4] == c4

# Optimized polygon
# conversion to optimized polygon
po = HPolygonOpt(p)
# conversion back
HPolygon(po)
# conversion from HPolytope
polytope = HPolytope{N}()
addconstraint!(polytope, c1)
addconstraint!(polytope, c2)
addconstraint!(polytope, c3)
addconstraint!(polytope, c4)
HPolygon(polytope)
HPolygonOpt(polytope)
# conversion to HPolytope
HPolytope(p)
HPolytope(po)

# HPolygon/HPolygonOpt tests
for p in [p, po]
Expand Down Expand Up @@ -122,5 +135,4 @@ for N in [Float64, Float32, Rational{Int}]
@test vi <= v[j]
end
end

end

0 comments on commit 9082de6

Please sign in to comment.