From 0d07cee4686ebc02ffb5bb3d22b8f4ae71171d08 Mon Sep 17 00:00:00 2001 From: schillic Date: Tue, 1 Aug 2023 22:30:34 +0200 Subject: [PATCH] remove Line2D(::HalfSpace) --- src/ConcreteOperations/intersection.jl | 10 +++++++++- src/Interfaces/AbstractHPolygon.jl | 9 ++++----- src/Sets/HPolygon.jl | 6 ++---- src/Sets/HPolygonOpt.jl | 15 +++++---------- src/Sets/Line2D.jl | 3 --- 5 files changed, 20 insertions(+), 23 deletions(-) diff --git a/src/ConcreteOperations/intersection.jl b/src/ConcreteOperations/intersection.jl index f0a0d591a8..787109143c 100644 --- a/src/ConcreteOperations/intersection.jl +++ b/src/ConcreteOperations/intersection.jl @@ -88,10 +88,15 @@ Line2D{Float64, Vector{Float64}}([1.0, 1.0], 1.0) ``` """ function intersection(L1::Line2D, L2::Line2D) + _intersection_line2d(L1, L2) +end + +# this method can also be called with `HalfSpace` arguments +function _intersection_line2d(L1, L2) det = right_turn(L1.a, L2.a) if isapproxzero(det) if isapprox(L1.b, L2.b) # lines are identical - return L1 + return _to_Line2D(L1) else N = promote_type(eltype(L1), eltype(L2)) return EmptySet{N}(dim(L1)) # lines are disjoint @@ -105,6 +110,9 @@ function intersection(L1::Line2D, L2::Line2D) end end +_to_Line2D(L::Line2D) = L +_to_Line2D(H::HalfSpace) = Line2D(H.a, H.b) + """ intersection(LS::LineSegment, L2::Line2D) diff --git a/src/Interfaces/AbstractHPolygon.jl b/src/Interfaces/AbstractHPolygon.jl index d5fc71715e..3424b60f19 100644 --- a/src/Interfaces/AbstractHPolygon.jl +++ b/src/Interfaces/AbstractHPolygon.jl @@ -158,14 +158,14 @@ function vertices_list(P::AbstractHPolygon{N}; return points end @inbounds for i in 1:(n - 1) - cap = intersection(Line2D(P.constraints[i]), Line2D(P.constraints[i + 1])) + cap = _intersection_line2d(P.constraints[i], P.constraints[i + 1]) if cap isa EmptySet return Vector{Vector{N}}() else points[i] = element(cap) end end - cap = intersection(Line2D(P.constraints[n]), Line2D(P.constraints[1])) + cap = _intersection_line2d(P.constraints[n], P.constraints[1]) if cap isa EmptySet return Vector{Vector{N}}() else @@ -217,8 +217,7 @@ of the constraints). """ function an_element(P::AbstractHPolygon) @assert length(P.constraints) >= 2 "polygon has less than two constraints" - return element(intersection(Line2D(P.constraints[1]), - Line2D(P.constraints[2]))) + return element(_intersection_line2d(P.constraints[1], P.constraints[2])) end """ @@ -358,7 +357,7 @@ function isredundant(cmid::HalfSpace, cright::HalfSpace, cleft::HalfSpace) # not the same direction => constraint is not redundant return false end - cap = intersection(Line2D(cright), Line2D(cleft)) + cap = _intersection_line2d(cright, cleft) @assert cap isa Singleton return cap ⊆ cmid end diff --git a/src/Sets/HPolygon.jl b/src/Sets/HPolygon.jl index fb6261b483..cb76cc376d 100644 --- a/src/Sets/HPolygon.jl +++ b/src/Sets/HPolygon.jl @@ -141,11 +141,9 @@ function σ(d::AbstractVector, P::HPolygon; if k == 1 || k == n + 1 # corner cases: wrap-around in constraints list - return element(intersection(Line2D(P.constraints[1]), - Line2D(P.constraints[n]))) + return element(_intersection_line2d(P.constraints[1], P.constraints[n])) else - return element(intersection(Line2D(P.constraints[k]), - Line2D(P.constraints[k - 1]))) + return element(_intersection_line2d(P.constraints[k], P.constraints[k - 1])) end end diff --git a/src/Sets/HPolygonOpt.jl b/src/Sets/HPolygonOpt.jl index 3b84d8278c..3e5e21e243 100644 --- a/src/Sets/HPolygonOpt.jl +++ b/src/Sets/HPolygonOpt.jl @@ -148,8 +148,7 @@ function σ(d::AbstractVector, P::HPolygonOpt; if (k == 0) P.ind = n # corner case: wrap-around in constraints list - return element(intersection(Line2D(P.constraints[n]), - Line2D(P.constraints[1]))) + return element(_intersection_line2d(P.constraints[n], P.constraints[1])) else P.ind = k end @@ -162,26 +161,22 @@ function σ(d::AbstractVector, P::HPolygonOpt; if (k == n + 1) P.ind = n # corner case: wrap-around in constraints list - return element(intersection(Line2D(P.constraints[n]), - Line2D(P.constraints[1]))) + return element(_intersection_line2d(P.constraints[n], P.constraints[1])) else P.ind = k - 1 end end - return element(intersection(Line2D(P.constraints[P.ind]), - Line2D(P.constraints[P.ind + 1]))) + return element(_intersection_line2d(P.constraints[P.ind], P.constraints[P.ind + 1])) else # binary search k = binary_search_constraints(d, P.constraints; start_index=P.ind) if k == 1 || k == n + 1 P.ind = 1 # corner cases: wrap-around in constraints list - return element(intersection(Line2D(P.constraints[n]), - Line2D(P.constraints[1]))) + return element(_intersection_line2d(P.constraints[n], P.constraints[1])) else P.ind = k - return element(intersection(Line2D(P.constraints[k - 1]), - Line2D(P.constraints[k]))) + return element(_intersection_line2d(P.constraints[k - 1], P.constraints[k])) end end end diff --git a/src/Sets/Line2D.jl b/src/Sets/Line2D.jl index 7f1a15e027..58571e57c0 100644 --- a/src/Sets/Line2D.jl +++ b/src/Sets/Line2D.jl @@ -79,9 +79,6 @@ end isoperationtype(::Type{<:Line2D}) = false -# constructor from a HalfSpace -Line2D(c::HalfSpace) = Line2D(c.a, c.b) - """ constraints_list(L::Line2D)