Skip to content

Commit

Permalink
#58 - outsourced common functions for HPolygonal
Browse files Browse the repository at this point in the history
  • Loading branch information
schillic committed Dec 14, 2017
1 parent 62b01bb commit ae28a22
Show file tree
Hide file tree
Showing 3 changed files with 107 additions and 216 deletions.
118 changes: 0 additions & 118 deletions src/HPolygon.jl
Original file line number Diff line number Diff line change
Expand Up @@ -35,49 +35,6 @@ 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
- `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
96 changes: 0 additions & 96 deletions src/HPolygonOpt.jl
Original file line number Diff line number Diff line change
Expand Up @@ -59,48 +59,6 @@ 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
### 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
109 changes: 107 additions & 2 deletions src/HPolygonal.jl
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ Abstract type for polygons in H-representation (i.e., constraints).
### Notes
Every concrete `HPolygonal` must define the following functions:
- `TODO()`
Every concrete `HPolygonal` must have the following fields:
- `constraints_list::Vector{LinearConstraint{N}}` -- the constraints
```jldoctest
subtypes(HPolygonal)
Expand All @@ -18,3 +18,108 @@ subtypes(HPolygonal)
```
"""
abstract type HPolygonal{N<:Real} <: Polygonal{N} end


"""
addconstraint!(P::HPolygonal{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
- `P` -- polygon in constraint representation
- `constraint` -- linear constraint to add
### Output
Nothing.
"""
function addconstraint!(P::HPolygonal{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


"""
∈(x::AbstractVector{N}, P::HPolygonal{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::HPolygonal{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::HPolygonal)::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::HPolygonal)::VPolygon
return VPolygon(vertices_list(P))
end


"""
vertices_list(P::HPolygonal{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::HPolygonal{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

0 comments on commit ae28a22

Please sign in to comment.