-
Notifications
You must be signed in to change notification settings - Fork 32
/
LineSegment.jl
414 lines (290 loc) · 9.29 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
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
export LineSegment,
halfspace_left, halfspace_right
"""
LineSegment{N, VN<:AbstractVector{N}} <: AbstractZonotope{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, Vector{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. If it is desired to remove the endpoints, pass the options
`markershape=:none` and `seriestype=:shape`.
Membership is checked with ∈ (`in`):
```jldoctest linesegment_constructor
julia> [0., 0] ∈ s && [.25, .25] ∈ s && [1., 1] ∈ s && [.5, .25] ∉ s
true
```
We can check whether the intersection with another line segment is empty, and
optionally compute a witness (which is the unique common point in this case):
```jldoctest linesegment_constructor
julia> sn = LineSegment([1., 0], [0., 1.])
LineSegment{Float64, Vector{Float64}}([1.0, 0.0], [0.0, 1.0])
julia> isdisjoint(s, sn)
false
julia> isdisjoint(s, sn, true)
(false, [0.5, 0.5])
```
"""
struct LineSegment{N,VN<:AbstractVector{N}} <: AbstractZonotope{N}
p::VN
q::VN
# default constructor with length constraint
function LineSegment(p::VN, q::VN) where {N,VN<:AbstractVector{N}}
@assert length(p) == length(q) == 2 "points for line segments must " *
"be two-dimensional, but their lengths are $(length(p)) and " *
"$(length(q))"
return new{N,VN}(p, q)
end
end
isoperationtype(::Type{<:LineSegment}) = false
"""
dim(L::LineSegment)
Return the ambient dimension of a 2D line segment.
### Input
- `L` -- 2D line segment
### Output
The ambient dimension of the 2D line segment, which is ``2``.
"""
function dim(L::LineSegment)
return 2
end
"""
σ(d::AbstractVector, L::LineSegment)
Return the support vector of a 2D line segment in a given direction.
### Input
- `d` -- direction
- `L` -- 2D 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, L::LineSegment)
return sign(dot(L.q - L.p, d)) >= 0 ? L.q : L.p
end
"""
ρ(d::AbstractVector, L::LineSegment)
Evaluate the support function of a 2D line segment in a given direction.
### Input
- `d` -- direction
- `L` -- 2D line segment
### Output
Evaluation of the support function in the given direction.
"""
function ρ(d::AbstractVector, L::LineSegment)
return max(dot(L.p, d), dot(L.q, d))
end
"""
an_element(L::LineSegment)
Return some element of a 2D line segment.
### Input
- `L` -- 2D line segment
### Output
The first vertex of the line segment.
"""
function an_element(L::LineSegment)
return L.p
end
"""
∈(x::AbstractVector, L::LineSegment)
Check whether a given point is contained in a 2D line segment.
### Input
- `x` -- point/vector
- `L` -- 2D line segment
### Output
`true` iff ``x ∈ L``.
### Algorithm
Let ``L = (p, q)`` be the line segment with extreme points ``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, L::LineSegment)
@assert length(x) == dim(L) "a vector of length $(length(x)) is " *
"incompatible with a $(dim(L))-dimensional set"
# check if point x is on the line through the line segment (p, q)
p = L.p
q = L.q
if isapproxzero(right_turn(p, q, x))
# check if the point is inside the box approximation of the line segment
return @inbounds (_leq(min(p[1], q[1]), x[1]) &&
_leq(x[1], max(p[1], q[1])) &&
_leq(min(p[2], q[2]), x[2]) &&
_leq(x[2], max(p[2], q[2])))
else
return false
end
end
"""
center(L::LineSegment)
Return the center of a 2D line segment.
### Input
- `L` -- 2D line segment
### Output
The center of the line segment.
"""
function center(L::LineSegment)
return L.p + (L.q - L.p) / 2
end
"""
genmat(L::LineSegment)
Return the generator matrix of a 2D line segment.
### Input
- `L` -- 2D line segment
### Output
A matrix with at most one column representing the generator of `L`.
"""
function genmat(L::LineSegment)
N = eltype(L)
if _isapprox(L.p, L.q)
# degenerate line segment has no generators
return zeros(N, dim(L), 0)
end
return hcat((L.p - L.q) / 2)
end
"""
generators(L::LineSegment)
Return an iterator over the (single) generator of a 2D line segment.
### Input
- `L` -- 2D line segment
### Output
An iterator over the generator of `L`, if any.
"""
function generators(L::LineSegment)
if _isapprox(L.p, L.q)
# degenerate line segment has no generators
N = eltype(L)
return EmptyIterator{Vector{N}}()
end
return SingletonIterator((L.p - L.q) / 2)
end
"""
vertices_list(L::LineSegment)
Return the list of vertices of a 2D line segment.
### Input
- `L` -- 2D line segment
### Output
The list of end points of the line segment.
"""
function vertices_list(L::LineSegment)
return [L.p, L.q]
end
"""
rand(::Type{LineSegment}; [N]::Type{<:Real}=Float64, [dim]::Int=2,
[rng]::AbstractRNG=GLOBAL_RNG, [seed]::Union{Int, Nothing}=nothing)
Create a random 2D 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 2D 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)
@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
"""
halfspace_left(L::LineSegment)
Return a half-space describing the 'left' of a two-dimensional 2D line segment
through two points.
### Input
- `L` -- 2D 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 2D line segment
through two points.
### Input
- `L` -- 2D 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)
Return a list of constraints defining a 2D line segment in 2D.
### Input
- `L` -- 2D line segment
### Output
A vector of constraints defining 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 a vector parallel to the line segment passing
through one of the vertices.
"""
function constraints_list(L::LineSegment)
p, q = L.p, L.q
d = @inbounds [p[2] - q[2], q[1] - p[1]]
return [halfspace_left(L), halfspace_right(L),
halfspace_right(p, p + d), halfspace_left(q, q + d)]
end
"""
translate(L::LineSegment, v::AbstractVector)
Translate (i.e., shift) a 2D line segment by a given vector.
### Input
- `L` -- 2D 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, v::AbstractVector)
@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
"""
ngens(L::LineSegment)
Return the number of generators of a 2D line segment.
### Input
- `L` -- 2D line segment
### Output
The number of generators.
### Algorithm
A line segment has either one generator, or zero generators if it is a
degenerated line segment of length zero.
"""
function ngens(L::LineSegment)
return _isapprox(L.p, L.q) ? 0 : 1
end
function scale!(α::Real, L::LineSegment)
L.p .*= α
L.q .*= α
return L
end