/
LineSegment.jl
351 lines (241 loc) · 8.3 KB
/
LineSegment.jl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
import Base: rand,
∈
export LineSegment,
halfspace_left, halfspace_right,
constraints_list
"""
LineSegment{N<:Real} <: AbstractCentrallySymmetricPolytope{N}
Type that represents a line segment in 2D between two points ``p`` and ``q``.
### Fields
- `p` -- first point
- `q` -- second point
### Examples
A line segment along the ``x = y`` diagonal:
```jldoctest linesegment_constructor
julia> s = LineSegment([0., 0], [1., 1.])
LineSegment{Float64}([0.0, 0.0], [1.0, 1.0])
julia> dim(s)
2
```
Use `plot(s)` to plot the extreme points of `s` and the line segment joining
them. Membership test is computed with ∈ (`in`):
```jldoctest linesegment_constructor
julia> [0., 0] ∈ s && [.25, .25] ∈ s && [1., 1] ∈ s && !([.5, .25] ∈ s)
true
```
We can check the intersection with another line segment, and optionally compute
a witness (which is just the common point in this case):
```jldoctest linesegment_constructor
julia> sn = LineSegment([1., 0], [0., 1.])
LineSegment{Float64}([1.0, 0.0], [0.0, 1.0])
julia> isempty(s ∩ sn)
false
julia> is_intersection_empty(s, sn, true)
(false, [0.5, 0.5])
```
"""
struct LineSegment{N<:Real} <: AbstractCentrallySymmetricPolytope{N}
p::AbstractVector{N}
q::AbstractVector{N}
# default constructor with length constraint
function LineSegment{N}(p::AbstractVector{N},
q::AbstractVector{N}) where {N<:Real}
@assert length(p) == length(q) == 2 "points for line segments must " *
"be two-dimensional"
return new{N}(p, q)
end
end
# convenience constructor without type parameter
LineSegment(p::AbstractVector{N}, q::AbstractVector{N}) where {N<:Real} =
LineSegment{N}(p, q)
# --- LazySet interface functions ---
"""
dim(L::LineSegment)::Int
Return the ambient dimension of a line segment.
### Input
- `L` -- line segment
### Output
The ambient dimension of the line segment, which is 2.
"""
function dim(L::LineSegment)::Int
return 2
end
"""
σ(d::AbstractVector{N}, L::LineSegment{N}) where {N<:Real}
Return the support vector of a line segment in a given direction.
### Input
- `d` -- direction
- `L` -- line segment
### Output
The support vector in the given direction.
### Algorithm
If the angle between the vector ``q - p`` and ``d`` is bigger than 90° and less
than 270° (measured in counter-clockwise order), the result is ``p``, otherwise
it is ``q``.
If the angle is exactly 90° or 270°, or if the direction has norm zero, this
implementation returns ``q``.
"""
function σ(d::AbstractVector{N}, L::LineSegment{N}) where {N<:Real}
return sign(dot(L.q - L.p, d)) >= 0 ? L.q : L.p
end
"""
an_element(L::LineSegment{N}) where {N<:Real}
Return some element of a line segment.
### Input
- `L` -- line segment
### Output
The first vertex of the line segment.
"""
function an_element(L::LineSegment{N}) where {N<:Real}
return L.p
end
"""
∈(x::AbstractVector{N}, L::LineSegment{N})::Bool where {N<:Real}
Check whether a given point is contained in a line segment.
### Input
- `x` -- point/vector
- `L` -- line segment
### Output
`true` iff ``x ∈ L``.
### Algorithm
Let ``L = (p, q)`` be the line segment with extremes ``p`` and ``q``, and let
``x`` be the given point.
1. A necessary condition for ``x ∈ (p, q)`` is that the three points are aligned,
thus their cross product should be zero.
2. It remains to check that ``x`` belongs to the box approximation of ``L``.
This amounts to comparing each coordinate with those of the extremes ``p``
and ``q``.
### Notes
The algorithm is inspired from [here](https://stackoverflow.com/a/328110).
"""
function ∈(x::AbstractVector{N}, L::LineSegment{N})::Bool where {N<:Real}
@assert length(x) == dim(L)
# check if the point is on the line through the line segment
if (x[2] - L.p[2]) * (L.q[1] - L.p[1]) -
(x[1] - L.p[1]) * (L.q[2] - L.p[2]) != 0
return false
end
# check if the point is inside the box approximation of the line segment
return min(L.p[1], L.q[1]) <= x[1] <= max(L.p[1], L.q[1]) &&
min(L.p[2], L.q[2]) <= x[2] <= max(L.p[2], L.q[2])
end
# --- AbstractCentrallySymmetric interface functions ---
"""
center(L::LineSegment{N})::Vector{N} where {N<:Real}
Return the center of a line segment.
### Input
- `L` -- line segment
### Output
The center of the line segment.
"""
function center(L::LineSegment{N})::Vector{N} where {N<:Real}
return L.p + (L.q - L.p) / 2
end
# --- AbstractPolytope interface functions ---
"""
vertices_list(L::LineSegment{N}
)::Vector{<:AbstractVector{N}} where {N<:Real}
Return the list of vertices of a line segment.
### Input
- `L` -- line segment
### Output
The list of end points of the line segment.
"""
function vertices_list(L::LineSegment{N}
)::Vector{<:AbstractVector{N}} where {N<:Real}
return [L.p, L.q]
end
# --- LazySet interface functions ---
"""
rand(::Type{LineSegment}; [N]::Type{<:Real}=Float64, [dim]::Int=2,
[rng]::AbstractRNG=GLOBAL_RNG, [seed]::Union{Int, Nothing}=nothing
)::LineSegment{N}
Create a random line segment.
### Input
- `LineSegment` -- type for dispatch
- `N` -- (optional, default: `Float64`) numeric type
- `dim` -- (optional, default: 2) dimension
- `rng` -- (optional, default: `GLOBAL_RNG`) random number generator
- `seed` -- (optional, default: `nothing`) seed for reseeding
### Output
A random line segment.
### Algorithm
All numbers are normally distributed with mean 0 and standard deviation 1.
"""
function rand(::Type{LineSegment};
N::Type{<:Real}=Float64,
dim::Int=2,
rng::AbstractRNG=GLOBAL_RNG,
seed::Union{Int, Nothing}=nothing
)::LineSegment{N}
@assert dim == 2 "cannot create a random LineSegment of dimension $dim"
rng = reseed(rng, seed)
p = randn(rng, N, dim)
q = randn(rng, N, dim)
return LineSegment(p, q)
end
# --- LineSegment functions ---
"""
halfspace_left(L::LineSegment)
Return a half-space describing the 'left' of a two-dimensional line segment
through two points.
### Input
- `L` -- line segment
### Output
The half-space whose boundary goes through the two points `p` and `q` and which
describes the left-hand side of the directed line segment `pq`.
"""
halfspace_left(L::LineSegment) = halfspace_left(L.p, L.q)
"""
halfspace_right(L::LineSegment)
Return a half-space describing the 'right' of a two-dimensional line segment
through two points.
### Input
- `L` -- line segment
### Output
The half-space whose boundary goes through the two points `p` and `q` and which
describes the right-hand side of the directed line segment `pq`.
"""
halfspace_right(L::LineSegment) = halfspace_right(L.p, L.q)
"""
constraints_list(L::LineSegment{N})::Vector{LinearConstraint{N}} where {N<:Real}
Return the list of constraints defining a line segment in 2D.
### Input
- `L` -- line segment
### Output
A vector of constraints that define the line segment.
### Algorithm
``L`` is defined by 4 constraints. In this algorithm, the first two constraints
are returned by ``halfspace_right`` and ``halfspace_left``, and the other two
are obtained by considering the vector normal to the line segment that passes
through each opposite vertex.
### Notes
This function returns a vector of halfspaces. It does not return equality
constraints.
"""
function constraints_list(L::LineSegment{N})::Vector{LinearConstraint{N}} where {N<:Real}
clist = Vector{LinearConstraint{N}}(undef, 4)
clist[1] = halfspace_left(L)
clist[2] = halfspace_right(L)
p, q = L.p, L.q
d = [(p[2]-q[2]), (q[1]-p[1])]
clist[3] = halfspace_right(p, p + d)
clist[4] = halfspace_left(q, q + d)
return clist
end
"""
translate(L::LineSegment{N}, v::AbstractVector{N}) where {N<:Real}
Translate (i.e., shift) a line segment by a given vector.
### Input
- `L` -- line segment
- `v` -- translation vector
### Output
A translated line segment.
### Algorithm
We add the vector to both defining points of the line segment.
"""
function translate(L::LineSegment{N}, v::AbstractVector{N}) where {N<:Real}
@assert length(v) == dim(L) "cannot translate a $(dim(L))-dimensional " *
"set by a $(length(v))-dimensional vector"
return LineSegment(L.p + v, L.q + v)
end