From ae28a2281394f9a16fa956b2e236cdb31917ad19 Mon Sep 17 00:00:00 2001 From: schillic Date: Thu, 14 Dec 2017 12:00:26 +0100 Subject: [PATCH] #58 - outsourced common functions for HPolygonal --- src/HPolygon.jl | 118 --------------------------------------------- src/HPolygonOpt.jl | 96 ------------------------------------ src/HPolygonal.jl | 109 ++++++++++++++++++++++++++++++++++++++++- 3 files changed, 107 insertions(+), 216 deletions(-) diff --git a/src/HPolygon.jl b/src/HPolygon.jl index d20ed631a2..0c6d3650c4 100644 --- a/src/HPolygon.jl +++ b/src/HPolygon.jl @@ -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} @@ -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 diff --git a/src/HPolygonOpt.jl b/src/HPolygonOpt.jl index 8706920ec5..d91b3b602e 100644 --- a/src/HPolygonOpt.jl +++ b/src/HPolygonOpt.jl @@ -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} @@ -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 diff --git a/src/HPolygonal.jl b/src/HPolygonal.jl index a55280c7f1..dbf51eb08e 100644 --- a/src/HPolygonal.jl +++ b/src/HPolygonal.jl @@ -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) @@ -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