Skip to content

Commit

Permalink
#58 - finished Polygonal
Browse files Browse the repository at this point in the history
  • Loading branch information
schillic committed Dec 14, 2017
1 parent c8bd161 commit 5a74a8c
Show file tree
Hide file tree
Showing 4 changed files with 90 additions and 51 deletions.
22 changes: 22 additions & 0 deletions src/PointSymmetric_Polytopic.jl
Original file line number Diff line number Diff line change
Expand Up @@ -26,3 +26,25 @@ subtypes(PointSymmetric_Polytopic)
```
"""
abstract type PointSymmetric_Polytopic{N<:Real} <: PointSymmetric{N} end


# --- common Polytopic functions (copy-pasted) ---


"""
singleton_list(P::PointSymmetric_Polytopic{N})::Vector{Singleton{N}} where {N<:Real}
Return the vertices of a polytopic as a list of singletons.
### Input
- `P` -- a polytopic set
### Output
List containing a singleton for each vertex.
"""
function singleton_list(P::PointSymmetric_Polytopic{N}
)::Vector{Singleton{N}} where {N<:Real}
return [Singleton(vi) for vi in P.vertices_list]
end
3 changes: 3 additions & 0 deletions src/Polygonal.jl
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,9 @@ subtypes(Polygonal)
abstract type Polygonal{N<:Real} <: Polytopic{N} end


# --- LazySet interface functions ---


"""
dim(P::Polygonal)::Int
Expand Down
24 changes: 23 additions & 1 deletion src/Polytopic.jl
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
export Polytopic,
vertices_list
vertices_list,
singleton_list

"""
Polytopic{N<:Real} <: LazySet
Expand All @@ -21,3 +22,24 @@ subtypes(Polytopic)
```
"""
abstract type Polytopic{N<:Real} <: LazySet end


# --- common Polytopic functions ---


"""
singleton_list(P::Polytopic{N})::Vector{Singleton{N}} where {N<:Real}
Return the vertices of a polytopic as a list of singletons.
### Input
- `P` -- a polytopic set
### Output
List containing a singleton for each vertex.
"""
function singleton_list(P::Polytopic{N})::Vector{Singleton{N}} where {N<:Real}
return [Singleton(vi) for vi in P.vertices_list]
end
92 changes: 42 additions & 50 deletions src/VPolygon.jl
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
import Base: <=,
import Base:

export VPolygon,
vertices_list,
singleton_list
export VPolygon

"""
VPolygon{N<:Real} <: Polygonal{N}
Expand Down Expand Up @@ -39,47 +37,9 @@ struct VPolygon{N<:Real} <: Polygonal{N}
end
end

"""
σ(d::AbstractVector{<:Real}, P::VPolygon{N})::Vector{N} where {N<:Real}
Return the support vector of a polygon in a given direction.
### 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

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.
# --- Polygonal interface functions ---

### 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 σ(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

"""
tovrep(P::VPolygon{N})::VPolygon{N} where {N<:Real}
Expand Down Expand Up @@ -115,6 +75,10 @@ function tohrep(P::VPolygon{N})::HPolygonal{N} where {N<:Real}
error("this function is not implemented yet, see issue #5")
end


# --- Polytopic interface functions ---


"""
vertices_list(P::VPolygon{N})::Vector{Vector{N}} where {N<:Real}
Expand All @@ -132,22 +96,50 @@ 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 5a74a8c

Please sign in to comment.