diff --git a/src/PointSymmetric_Polytopic.jl b/src/PointSymmetric_Polytopic.jl index 0eac5eb424..e1698a7e1d 100644 --- a/src/PointSymmetric_Polytopic.jl +++ b/src/PointSymmetric_Polytopic.jl @@ -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 diff --git a/src/Polygonal.jl b/src/Polygonal.jl index b1bf8aedaf..21ee34edc7 100644 --- a/src/Polygonal.jl +++ b/src/Polygonal.jl @@ -23,6 +23,9 @@ subtypes(Polygonal) abstract type Polygonal{N<:Real} <: Polytopic{N} end +# --- LazySet interface functions --- + + """ dim(P::Polygonal)::Int diff --git a/src/Polytopic.jl b/src/Polytopic.jl index a870fbd6fb..6c5068039f 100644 --- a/src/Polytopic.jl +++ b/src/Polytopic.jl @@ -1,5 +1,6 @@ export Polytopic, - vertices_list + vertices_list, + singleton_list """ Polytopic{N<:Real} <: LazySet @@ -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 diff --git a/src/VPolygon.jl b/src/VPolygon.jl index ca71cfb5fd..2aaa5ad88d 100644 --- a/src/VPolygon.jl +++ b/src/VPolygon.jl @@ -1,8 +1,6 @@ -import Base: <=, ∈ +import Base: ∈ -export VPolygon, - vertices_list, - singleton_list +export VPolygon """ VPolygon{N<:Real} <: Polygonal{N} @@ -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} @@ -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} @@ -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 """