Skip to content

Commit

Permalink
#58 - modified AbstractHPolygon types
Browse files Browse the repository at this point in the history
  • Loading branch information
schillic committed Dec 19, 2017
1 parent 0237986 commit f6113d7
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 224 deletions.
128 changes: 5 additions & 123 deletions src/HPolygon.jl
Original file line number Diff line number Diff line change
@@ -1,13 +1,9 @@
import Base: <=,
import Base.<=

export HPolygon,
addconstraint!,
,
tovrep,
vertices_list
export HPolygon

"""
HPolygon{N<:Real} <: LazySet
HPolygon{N<:Real} <: AbstractHPolygon{N}
Type that represents a convex polygon in constraint representation whose edges
are sorted in counter-clockwise fashion with respect to their normal directions.
Expand All @@ -27,56 +23,17 @@ Use `addconstraint!` to iteratively add the edges in a sorted way.
- `HPolygon()`
-- constructor with no constraints
"""
struct HPolygon{N<:Real} <: LazySet
struct HPolygon{N<:Real} <: AbstractHPolygon{N}
constraints_list::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}()

"""
addconstraint!(P::HPolygon{N}, constraint::LinearConstraint{N})::Void where {N<:Real}
Add a linear constraint to a polygon in constraint representation, keeping the
constraints sorted by their normal directions.

### Input
# --- LazySet interface functions ---

- `P` -- polygon
- `constraint` -- linear constraint to add
### Output
Nothing.
"""
function addconstraint!(P::HPolygon{N},
constraint::LinearConstraint{N})::Void where {N<:Real}
i = length(P.constraints_list)
while i > 0 && constraint.a <= P.constraints_list[i].a
i -= 1
end
# here P.constraints_list[i] < constraint
insert!(P.constraints_list, i+1, constraint)
return nothing
end

"""
dim(P::HPolygon)::Int
Return the dimension of a polygon.
### Input
- `P` -- polygon in constraint representation
### Output
The ambient dimension of the polygon.
"""
function dim(P::HPolygon)::Int
return 2
end

"""
σ(d::AbstractVector{<:Real}, P::HPolygon{N})::Vector{N} where {N<:Real}
Expand Down Expand Up @@ -116,78 +73,3 @@ function σ(d::AbstractVector{<:Real}, P::HPolygon{N})::Vector{N} where {N<:Real
Line(P.constraints_list[k-1]))
end
end

"""
∈(x::AbstractVector{N}, P::HPolygon{N})::Bool where {N<:Real}
Check whether a given 2D point is contained in a polygon in constraint
representation.
### Input
- `x` -- two-dimensional point/vector
- `P` -- polygon in constraint representation
### Output
`true` iff ``x ∈ P``.
### Algorithm
This implementation checks if the point lies on the outside of each edge.
"""
function (x::AbstractVector{N}, P::HPolygon{N})::Bool where {N<:Real}
@assert length(x) == 2

for c in P.constraints_list
if dot(c.a, x) > c.b
return false
end
end
return true
end

"""
tovrep(P::HPolygon)::VPolygon
Build a vertex representation of the given polygon.
### Input
- `P` -- polygon in constraint representation
### Output
The same polygon but in vertex representation, a `VPolygon`.
"""
function tovrep(P::HPolygon)::VPolygon
return VPolygon(vertices_list(P))
end

"""
vertices_list(P::HPolygon{N})::Vector{Vector{N}} where {N<:Real}
Return the list of vertices of a polygon in constraint representation.
### Input
- `P` -- polygon in constraint representation
### Output
List of vertices.
"""
function vertices_list(P::HPolygon{N})::Vector{Vector{N}} where {N<:Real}
n = length(P.constraints_list)
points = Vector{Vector{N}}(n)
if n == 0
return points
end
@inbounds for i in 1:n-1
points[i] = intersection(Line(P.constraints_list[i]),
Line(P.constraints_list[i+1]))
end
points[n] = intersection(Line(P.constraints_list[n]),
Line(P.constraints_list[1]))
return points
end
106 changes: 5 additions & 101 deletions src/HPolygonOpt.jl
Original file line number Diff line number Diff line change
@@ -1,13 +1,9 @@
import Base: <=,
import Base.<=

export HPolygonOpt,
addconstraint!,
,
tovrep,
vertices_list
export HPolygonOpt

"""
HPolygonOpt{N<:Real} <: LazySet
HPolygonOpt{N<:Real} <: AbstractHPolygon{N}
Type that represents a convex polygon in constraint representation whose edges
are sorted in counter-clockwise fashion with respect to their normal directions.
Expand Down Expand Up @@ -37,7 +33,7 @@ Use `addconstraint!` to iteratively add the edges in a sorted way.
- `HPolygonOpt(H::HPolygon{<:Real})`
-- constructor from an HPolygon
"""
mutable struct HPolygonOpt{N<:Real} <: LazySet
mutable struct HPolygonOpt{N<:Real} <: AbstractHPolygon{N}
constraints_list::Vector{LinearConstraint{N}}
ind::Int

Expand All @@ -59,47 +55,9 @@ HPolygonOpt(constraints_list::Vector{LinearConstraint{N}}) where {N<:Real} =
HPolygonOpt(H::HPolygon{N}) where {N<:Real} =
HPolygonOpt{N}(H.constraints_list, 1)

"""
addconstraint!(P::HPolygonOpt{N}, constraint::LinearConstraint{N})::Void where {N<:Real}
Add a linear constraint to an optimized polygon in constraint representation,
keeping the constraints sorted by their normal directions.
### Input
- `P` -- optimized polygon
- `constraint` -- linear constraint to add
### Output
Nothing.
"""
function addconstraint!(P::HPolygonOpt{N},
constraint::LinearConstraint{N})::Void where {N<:Real}
i = length(P.constraints_list)
while i > 0 && constraint.a <= P.constraints_list[i].a
i -= 1
end
insert!(P.constraints_list, i+1, constraint)
return nothing
end

"""
dim(P::HPolygonOpt)::Int
Return the dimension of an optimized polygon.

### Input
- `P` -- optimized polygon in constraint representation
# --- LazySet interface functions ---

### Output
The ambient dimension of the optimized polygon.
"""
function dim(P::HPolygonOpt)::Int
return 2
end

"""
σ(d::AbstractVector{<:Real}, P::HPolygonOpt{N})::Vector{N} where {N<:Real}
Expand Down Expand Up @@ -158,57 +116,3 @@ function σ(d::AbstractVector{<:Real},
end
end
end

"""
∈(x::AbstractVector{N}, P::HPolygonOpt{N})::Bool where {N<:Real}
Check whether a given 2D point is contained in an optimized polygon in
constraint representation.
### Input
- `x` -- two-dimensional point/vector
- `P` -- optimized polygon in constraint representation
### Output
`true` iff ``x ∈ P``.
"""
function (x::AbstractVector{N}, P::HPolygonOpt{N})::Bool where {N<:Real}
return (x, HPolygon(P.constraints_list))
end

"""
tovrep(P::HPolygonOpt)::VPolygon
Build a vertex representation of the given optimized polygon.
### Input
- `P` -- optimized polygon in constraint representation
### Output
The same polygon but in vertex representation, a `VPolygon`.
"""
function tovrep(P::HPolygonOpt)::VPolygon
return tovrep(HPolygon(P.constraints_list))
end

"""
vertices_list(P::HPolygonOpt{N})::Vector{Vector{N}} where {N<:Real}
Return the list of vertices of an optimized polygon in constraint
representation.
### Input
- `P` -- an optimized polygon in constraint representation
### Output
List of vertices.
"""
function vertices_list(P::HPolygonOpt{N})::Vector{Vector{N}} where {N<:Real}
return vertices_list(HPolygon(P.constraints_list))
end

0 comments on commit f6113d7

Please sign in to comment.