Skip to content

Commit

Permalink
#58 - modified AbstractPolygon types
Browse files Browse the repository at this point in the history
  • Loading branch information
schillic committed Dec 19, 2017
1 parent d84cba2 commit 0237986
Showing 1 changed file with 57 additions and 47 deletions.
104 changes: 57 additions & 47 deletions src/VPolygon.jl
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
import Base: <=,
import Base:

export VPolygon,
vertices_list,
singleton_list
export VPolygon

"""
VPolygon{N<:Real} <: LazySet
VPolygon{N<:Real} <: AbstractPolygon{N}
Type that represents a polygon by its vertices.
Expand All @@ -24,7 +22,7 @@ the convex hull.
apply_convex_hull::Bool=true,
algorithm::String="monotone_chain")`
"""
struct VPolygon{N<:Real} <: LazySet
struct VPolygon{N<:Real} <: AbstractPolygon{N}
vertices_list::Vector{Vector{N}}

# default constructor that applies a convex hull algorithm
Expand All @@ -39,64 +37,47 @@ struct VPolygon{N<:Real} <: LazySet
end
end


# --- AbstractPolygon interface functions ---


"""
dim(P::VPolygon)::Int
tovrep(P::VPolygon{N})::VPolygon{N} where {N<:Real}
Return the dimension of a polygon in vertex representation.
Build a vertex representation of the given polygon.
### Input
- `P` -- polygon in vertex representation
### Output
The ambient dimension of the polygon.
The identity, i.e., the same polygon instance.
"""
function dim(P::VPolygon)::Int
return 2
function tovrep(P::VPolygon{N})::VPolygon{N} where {N<:Real}
return P
end

"""
σ(d::AbstractVector{<:Real}, P::VPolygon{N})::Vector{N} where {N<:Real}
tohrep(P::VPolygon{N})::AbstractHPolygon{N} where {N<:Real}
Return the support vector of a polygon in a given direction.
Build a constraint representation of the given polygon.
### Input
- `d` -- direction
- `P` -- polygon in vertex representation
### Output
The support vector in the given direction.
If the direction has norm zero, the first vertex is returned.
### Algorithm
The same polygon but in constraint representation, an `AbstractHPolygon`.
"""
function tohrep(P::VPolygon{N})::AbstractHPolygon{N} where {N<:Real}
error("this function is not implemented yet, see issue #5")
end

This implementation performs a brute-force search, comparing the projection of
each vector along the given direction.
It runs in ``O(n)`` where ``n`` is the number of vertices.

### Notes
# --- AbstractPolytope interface functions ---

For arbitrary points without structure this is the best one can do.
However, a more efficient approach can be used if the vertices of the polygon
have been sorted in counter-clockwise fashion.
In that case a binary search algorithm can be used that runs in ``O(\\log n)``.
See issue [#40](https://github.com/JuliaReach/LazySets.jl/issues/40).
"""
function σ(d::AbstractVector{<:Real}, P::VPolygon{N})::Vector{N} where {N<:Real}
if isempty(P.vertices_list)
error("this polygon is empty")
end
i_max = 1
@inbounds for i in 2:length(P.vertices_list)
if dot(d, P.vertices_list[i] - P.vertices_list[i_max]) > zero(N)
i_max = i
end
end
return P.vertices_list[i_max]
end

"""
vertices_list(P::VPolygon{N})::Vector{Vector{N}} where {N<:Real}
Expand All @@ -115,22 +96,51 @@ function vertices_list(P::VPolygon{N})::Vector{Vector{N}} where {N<:Real}
return P.vertices_list
end


# --- LazySet interface functions ---


"""
singleton_list(P::VPolygon{N})::Vector{Singleton{N}} where {N<:Real}
σ(d::AbstractVector{<:Real}, P::VPolygon{N})::Vector{N} where {N<:Real}
Return the vertices of a convex polygon in vertex representation as a list of
singletons.
Return the support vector of a polygon in a given direction.
### Input
- `P` -- a polygon vertex representation
- `d` -- direction
- `P` -- polygon in vertex representation
### Output
List containing a singleton for each vertex.
The support vector in the given direction.
If the direction has norm zero, the first vertex is returned.
### Algorithm
This implementation performs a brute-force search, comparing the projection of
each vector along the given direction.
It runs in ``O(n)`` where ``n`` is the number of vertices.
### Notes
For arbitrary points without structure this is the best one can do.
However, a more efficient approach can be used if the vertices of the polygon
have been sorted in counter-clockwise fashion.
In that case a binary search algorithm can be used that runs in ``O(\\log n)``.
See issue [#40](https://github.com/JuliaReach/LazySets.jl/issues/40).
"""
function singleton_list(P::VPolygon{N})::Vector{Singleton{N}} where {N<:Real}
return [Singleton(vi) for vi in P.vertices_list]
function σ(d::AbstractVector{<:Real},
P::VPolygon{N})::Vector{N} where {N<:Real}
if isempty(P.vertices_list)
error("this polygon is empty")
end
i_max = 1
@inbounds for i in 2:length(P.vertices_list)
if dot(d, P.vertices_list[i] - P.vertices_list[i_max]) > zero(N)
i_max = i
end
end
return P.vertices_list[i_max]
end

"""
Expand Down

0 comments on commit 0237986

Please sign in to comment.