Skip to content

Commit

Permalink
remove Line2D(::HalfSpace)
Browse files Browse the repository at this point in the history
  • Loading branch information
schillic committed Aug 2, 2023
1 parent c31680f commit 0d07cee
Show file tree
Hide file tree
Showing 5 changed files with 20 additions and 23 deletions.
10 changes: 9 additions & 1 deletion src/ConcreteOperations/intersection.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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)
Expand Down
9 changes: 4 additions & 5 deletions src/Interfaces/AbstractHPolygon.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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

"""
Expand Down Expand Up @@ -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
Expand Down
6 changes: 2 additions & 4 deletions src/Sets/HPolygon.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
15 changes: 5 additions & 10 deletions src/Sets/HPolygonOpt.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down
3 changes: 0 additions & 3 deletions src/Sets/Line2D.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down

0 comments on commit 0d07cee

Please sign in to comment.