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
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
use crate::{ConvexPolygon, Disk, HalfPlane, Plane};
use fenris_traits::Real;
use nalgebra::allocator::Allocator;
use nalgebra::{clamp, DefaultAllocator, DimName, Matrix2, OPoint, OVector, Vector2, U2, U3};
use nalgebra::{Point2, Point3, Scalar};
use numeric_literals::replace_float_literals;
use std::fmt::Debug;
pub type LineSegment3d<T> = LineSegment<T, U3>;
impl<T: Real> LineSegment3d<T> {
#[allow(non_snake_case)]
pub fn closest_point_to_plane_parametric(&self, plane: &Plane<T>) -> T {
let n = plane.normal();
let x0 = plane.point();
let [a, b] = [self.start(), self.end()];
let d = &b.coords - &a.coords;
let y = &x0.coords - &a.coords;
let nTd = n.dot(&d);
let nTy = n.dot(&y);
let t = if nTd.signum() == nTy.signum() {
if nTy.abs() >= nTd.abs() {
T::one()
} else {
nTy / nTd
}
} else {
T::zero()
};
t
}
pub fn closest_point_to_plane(&self, plane: &Plane<T>) -> Point3<T> {
let t = self.closest_point_to_plane_parametric(plane);
self.point_from_parameter(t)
}
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct LineSegment<T, D>
where
T: Scalar,
D: DimName,
DefaultAllocator: Allocator<T, D>,
{
start: OPoint<T, D>,
end: OPoint<T, D>,
}
pub type LineSegment2d<T> = LineSegment<T, U2>;
impl<T, D> LineSegment<T, D>
where
T: Scalar,
D: DimName,
DefaultAllocator: Allocator<T, D>,
{
pub fn from_end_points(start: OPoint<T, D>, end: OPoint<T, D>) -> Self {
Self { start, end }
}
pub fn start(&self) -> &OPoint<T, D> {
&self.start
}
pub fn end(&self) -> &OPoint<T, D> {
&self.end
}
pub fn reverse(&self) -> Self {
Self {
start: self.end.clone(),
end: self.start.clone(),
}
}
}
impl<T, D> LineSegment<T, D>
where
T: Real,
D: DimName,
DefaultAllocator: Allocator<T, D>,
{
pub fn to_line(&self) -> Line<T, D> {
let dir = &self.end - &self.start;
Line::from_point_and_dir(self.start.clone(), dir)
}
pub fn tangent_dir(&self) -> OVector<T, D> {
&self.end().coords - &self.start().coords
}
pub fn length(&self) -> T {
self.tangent_dir().norm()
}
pub fn midpoint(&self) -> OPoint<T, D> {
OPoint::from((&self.start().coords + &self.end().coords) / (T::one() + T::one()))
}
pub fn closest_point_parametric(&self, point: &OPoint<T, D>) -> T {
let t = self.to_line().project_point_parametric(point);
clamp(t, T::zero(), T::one())
}
pub fn closest_point(&self, point: &OPoint<T, D>) -> OPoint<T, D> {
let t = self.closest_point_parametric(point);
self.point_from_parameter(t)
}
pub fn point_from_parameter(&self, t: T) -> OPoint<T, D> {
OPoint::from(&self.start().coords + self.tangent_dir() * t)
}
}
impl<T> LineSegment2d<T>
where
T: Real,
{
pub fn normal_dir(&self) -> Vector2<T> {
let tangent = self.tangent_dir();
Vector2::new(tangent.y, -tangent.x)
}
pub fn intersect_line_parametric(&self, line: &Line2d<T>) -> Option<T> {
self.to_line()
.intersect_line_parametric(line)
.map(|(t1, _)| t1)
}
#[replace_float_literals(T::from_f64(literal).unwrap())]
pub fn intersect_disk_parametric(&self, disk: &Disk<T>) -> Option<[T; 2]> {
let [t1, t2] = self.to_line().intersect_disk_parametric(disk)?;
let t1 = clamp(t1, 0.0, 1.0);
let t2 = clamp(t2, 0.0, 1.0);
Some([t1, t2])
}
#[replace_float_literals(T::from_f64(literal).unwrap())]
pub fn intersect_disk(&self, disk: &Disk<T>) -> Option<Self> {
self.intersect_disk_parametric(disk)
.map(|[t1, t2]| self.segment_from_parameters(&t1, &t2))
}
pub fn segment_from_parameters(&self, t_begin: &T, t_end: &T) -> Self {
let begin = self.point_from_parameter(t_begin.clone());
let end = self.point_from_parameter(t_end.clone());
Self::from_end_points(begin, end)
}
pub fn intersect_segment_parametric(&self, other: &LineSegment2d<T>) -> Option<T> {
let d1 = &self.end - &self.start;
let d2 = &other.end - &other.start;
let line1 = Line2d::from_point_and_dir(self.start.clone(), d1);
let line2 = Line2d::from_point_and_dir(other.start.clone(), d2);
line1
.intersect_line_parametric(&line2)
.and_then(|(t1, t2)| {
if t2 < T::zero() || t2 > T::one() {
None
} else if t1 < T::zero() || t1 > T::one() {
None
} else {
Some(t1)
}
})
}
#[replace_float_literals(T::from_f64(literal).unwrap())]
pub fn intersect_half_plane_parametric(&self, half_plane: &HalfPlane<T>) -> Option<[T; 2]> {
let contains_start = half_plane.contains_point(self.start());
let contains_end = half_plane.contains_point(self.end());
match (contains_start, contains_end) {
(true, true) => Some([0.0, 1.0]),
(false, false) => None,
(true, false) | (false, true) => {
let t_intersect = self
.intersect_line_parametric(&half_plane.surface())
.map(|t| clamp(t, 0.0, 1.0));
let (t_start, t_end);
if contains_start {
t_start = 0.0;
t_end = t_intersect.unwrap_or(1.0);
} else {
t_start = t_intersect.unwrap_or(0.0);
t_end = 1.0;
}
debug_assert!(t_start <= t_end);
Some([t_start, t_end])
}
}
}
#[replace_float_literals(T::from_f64(literal).unwrap())]
pub fn intersect_half_plane(&self, half_plane: &HalfPlane<T>) -> Option<Self> {
self.intersect_half_plane_parametric(half_plane)
.map(|[t1, t2]| self.segment_from_parameters(&t1, &t2))
}
pub fn intersect_polygon(&self, other: &ConvexPolygon<T>) -> Option<LineSegment2d<T>> {
let mut result = self.clone();
for half_plane in other.half_planes() {
result = result.intersect_half_plane(&half_plane)?;
}
Some(result)
}
}
impl<T: Real> LineSegment3d<T> {
pub fn intersect_plane_parametric(&self, plane: &Plane<T>) -> Option<T> {
self.to_line()
.intersect_plane_parametric(plane)
.filter(|t| t >= &T::zero() && t <= &T::one())
}
}
#[derive(Debug, Clone)]
pub struct Line<T, D>
where
T: Scalar,
D: DimName,
DefaultAllocator: Allocator<T, D>,
{
point: OPoint<T, D>,
dir: OVector<T, D>,
}
pub type Line2d<T> = Line<T, U2>;
pub type Line3d<T> = Line<T, U3>;
impl<T, D> Line<T, D>
where
T: Scalar,
D: DimName,
DefaultAllocator: Allocator<T, D>,
{
pub fn from_point_and_dir(point: OPoint<T, D>, dir: OVector<T, D>) -> Self {
Self { point, dir }
}
pub fn point(&self) -> &OPoint<T, D> {
&self.point
}
pub fn dir(&self) -> &OVector<T, D> {
&self.dir
}
}
impl<T, D> Line<T, D>
where
T: Real,
D: DimName,
DefaultAllocator: Allocator<T, D>,
{
pub fn tangent(&self) -> OVector<T, D> {
self.dir.normalize()
}
pub fn from_point_through_point(point: OPoint<T, D>, through: &OPoint<T, D>) -> Self {
let dir = through - &point;
Self::from_point_and_dir(point, dir)
}
pub fn project_point_parametric(&self, point: &OPoint<T, D>) -> T {
let d2 = self.dir.magnitude_squared();
if d2 == T::zero() {
T::zero()
} else {
(point - &self.point).dot(&self.dir) / d2
}
}
pub fn project_point(&self, point: &OPoint<T, D>) -> OPoint<T, D> {
let t = self.project_point_parametric(point);
self.point_from_parameter(t)
}
pub fn point_from_parameter(&self, t: T) -> OPoint<T, D> {
&self.point + &self.dir * t
}
}
impl<T> Line2d<T>
where
T: Real,
{
pub fn intersect(&self, other: &Line2d<T>) -> Option<Point2<T>> {
self.intersect_line_parametric(other)
.map(|(t1, _)| self.point_from_parameter(t1))
}
pub fn intersect_line_parametric(&self, other: &Line2d<T>) -> Option<(T, T)> {
let rhs = &other.point - &self.point;
let matrix = Matrix2::from_columns(&[self.dir, -other.dir]);
matrix
.try_inverse()
.map(|inv| inv * rhs)
.map(|t| (t.x, t.y))
}
pub fn intersect_disk(&self, disk: &Disk<T>) -> Option<LineSegment2d<T>> {
let [t1, t2] = self.intersect_disk_parametric(disk)?;
let p1 = self.point_from_parameter(t1);
let p2 = self.point_from_parameter(t2);
Some(LineSegment2d::from_end_points(p1, p2))
}
#[replace_float_literals(T::from_f64(literal).unwrap())]
pub fn intersect_disk_parametric(&self, disk: &Disk<T>) -> Option<[T; 2]> {
let a = self.point();
let d = self.dir();
let r = disk.radius();
let a0 = a - disk.center();
let alpha = d.dot(&d);
let beta = 2.0 * d.dot(&a0);
let gamma = a0.dot(&a0) - r * r;
let discriminant = beta * beta - 4.0 * alpha * gamma;
if discriminant >= 0.0 {
let disc_sqrt = discriminant.sqrt();
let t1 = (-beta - disc_sqrt) / (2.0 * alpha);
let t2 = (-beta + disc_sqrt) / (2.0 * alpha);
debug_assert!(t1 <= t2);
Some([t1, t2])
} else {
None
}
}
}
impl<T> Line3d<T>
where
T: Real,
{
pub fn intersect_plane_parametric(&self, plane: &Plane<T>) -> Option<T> {
let n = plane.normal();
let d = self.dir();
let b = self.point() - plane.point();
let d_dot_n = d.dot(&n);
(d_dot_n != T::zero()).then(|| -b.dot(&n) / d_dot_n)
}
}