Skip to content

Commit

Permalink
intersection of Interval and HalfSpace/Hyperplane
Browse files Browse the repository at this point in the history
  • Loading branch information
schillic committed May 13, 2019
1 parent d4a5e34 commit d519420
Show file tree
Hide file tree
Showing 3 changed files with 75 additions and 1 deletion.
1 change: 1 addition & 0 deletions docs/src/lib/binary_functions.md
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ intersection(::AbstractHyperrectangle{N}, ::AbstractHyperrectangle{N}) where {N<
intersection(::Interval{N}, ::Interval{N}) where {N<:Real}
intersection(::Interval{N}, ::HalfSpace{N}) where {N<:Real}
intersection(::Interval{N}, ::Hyperplane{N}) where {N<:Real}
intersection(::Interval{N}, ::LazySet{N}) where {N<:Real}
intersection(::AbstractHPolygon{N}, ::AbstractHPolygon{N}, ::Bool=true) where {N<:Real}
intersection(::AbstractPolyhedron{N}, ::AbstractPolyhedron{N}) where {N<:Real}
intersection(::Union{VPolytope{N}, VPolygon{N}}, ::Union{VPolytope{N}, VPolygon{N}}) where {N<:Real}
Expand Down
70 changes: 69 additions & 1 deletion src/concrete_intersection.jl
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,7 @@ end

"""
intersection(x::Interval{N}, y::Interval{N}
)::Union{Interval{N}, EmptySet{N}} where {N<:Real}
)::Union{Interval{N}, EmptySet{N}} where {N<:Real}
Return the intersection of two intervals.
Expand Down Expand Up @@ -293,6 +293,60 @@ function intersection(hp::Hyperplane{N}, X::Interval{N}
return intersection(X, hp)
end

"""
intersection(X::Interval{N}, Y::LazySet{N}
)::Union{Interval{N}, Singleton{N}, EmptySet{N}} where {N<:Real}
Compute the intersection of an interval and a convex set.
### Input
- `X` -- interval
- `Y` -- convex set
### Output
If the sets do not intersect, the result is the empty set.
Otherwise the result is the interval that describes the intersection, which may
be of type `Singleton` if the intersection is very small.
"""
function intersection(X::Interval{N}, Y::LazySet{N}
)::Union{Interval{N}, Singleton{N}, EmptySet{N}} where {N<:Real}
lower = max(min(X), -ρ(N[-1], Y))
upper = min(max(X), ρ(N[1], Y))
if _isapprox(lower, upper)
return Singleton([lower])
elseif lower < upper
return Interval(lower, upper)
else
return EmptySet{N}()
end
end

# symmetric method
function intersection(Y::LazySet{N}, X::Interval{N}
)::Union{Singleton{N}, EmptySet{N}} where {N<:Real}
return intersection(X, Y)
end

# disambiguation
function intersection(X::Interval{N}, H::AbstractHyperrectangle{N}
)::Union{Interval{N}, Singleton{N}, EmptySet{N}} where {N<:Real}
return invoke(intersection, Tuple{Interval{N}, LazySet{N}}, X, H)
end
function intersection(H::AbstractHyperrectangle{N}, X::Interval{N}
)::Union{Interval{N}, Singleton{N}, EmptySet{N}} where {N<:Real}
return invoke(intersection, Tuple{Interval{N}, LazySet{N}}, X, H)
end
function intersection(X::Interval{N}, S::AbstractSingleton{N}
)::Union{Singleton{N}, EmptySet{N}} where {N<:Real}
return invoke(intersection, Tuple{typeof(S), LazySet{N}}, S, X)
end
function intersection(S::AbstractSingleton{N}, X::Interval{N}
)::Union{Singleton{N}, EmptySet{N}} where {N<:Real}
return invoke(intersection, Tuple{typeof(S), LazySet{N}}, S, X)
end

"""
intersection(P1::AbstractHPolygon{N},
P2::AbstractHPolygon{N}
Expand Down Expand Up @@ -500,6 +554,14 @@ function intersection(P::AbstractPolyhedron{N},
)::Union{Singleton{N}, EmptySet{N}} where {N<:Real}
return invoke(intersection, Tuple{typeof(S), LazySet{N}}, S, P)
end
function intersection(X::Interval{N}, P::AbstractPolyhedron{N}
)::Union{Interval{N}, Singleton{N}, EmptySet{N}} where {N<:Real}
return invoke(intersection, Tuple{Interval{N}, LazySet{N}}, X, P)
end
function intersection(P::AbstractPolyhedron{N}, X::Interval{N}
)::Union{Interval{N}, Singleton{N}, EmptySet{N}} where {N<:Real}
return invoke(intersection, Tuple{Interval{N}, LazySet{N}}, X, P)
end

"""
intersection(P1::Union{VPolytope{N}, VPolygon{N}},
Expand Down Expand Up @@ -670,6 +732,12 @@ end
function intersection(S::AbstractSingleton{N}, U::Universe{N}) where {N<:Real}
return S
end
function intersection(X::Interval{N}, U::Universe{N}) where {N<:Real}
return X
end
function intersection(U::Universe{N}, X::Interval{N}) where {N<:Real}
return X
end

"""
intersection(P::AbstractPolyhedron{N}, rm::ResetMap{N}) where {N<:Real}
Expand Down
5 changes: 5 additions & 0 deletions test/unit_Interval.jl
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,11 @@ for N in [Float64, Float32, Rational{Int}]
@test intersection(i, hp) == Singleton(N[1.5])
hp = Hyperplane(N[-1], N(-3))
@test intersection(i, hp) == EmptySet{N}()
# other intersections
Y = Ball1(N[2], N(0.5))
@test intersection(i, Y) == Interval(N(1.5), N(2))
Y = ConvexHull(Singleton(N[-5]), Singleton(N[-1]))
@test intersection(i, Y) == EmptySet{N}()

# disjointness check
@test !isdisjoint(A, B)
Expand Down

0 comments on commit d519420

Please sign in to comment.