1use num_traits::NumCast;
2
3use crate::shape::Point;
4use crate::base::Vec2;
5use crate::traits::{BoundingBox, Cast, CoordType, MapPoint, TryBoundingBox};
6pub use crate::base::Angle;
7pub use crate::base::{Contains, Side};
8
9use super::Rect;
10
11#[derive(Clone, Copy, PartialEq, Eq, Hash, Debug)]
13pub struct Edge<T> {
14 pub start: Point<T>,
15 pub end: Point<T>,
16}
17
18impl<T: Copy> Edge<T> {
19 pub fn new(start: impl Into<Point<T>>, end: impl Into<Point<T>>) -> Self {
20 Self {
21 start: start.into(),
22 end: end.into(),
23 }
24 }
25
26 pub fn reversed(&self) -> Self {
28 Self {
29 start: self.end,
30 end: self.start,
31 }
32 }
33}
34
35impl<T: PartialEq> Edge<T> {
36 pub fn is_degenerate(&self) -> bool {
39 self.start == self.end
40 }
41
42 pub fn is_rectilinear(&self) -> bool {
43 !self.is_degenerate() && (self.start.x == self.end.x || self.start.y == self.end.y)
44 }
45
46 pub fn is_horizontal(&self) -> bool {
47 !self.is_degenerate() && self.start.y == self.end.y
48 }
49
50 pub fn is_vertical(&self) -> bool {
51 !self.is_degenerate() && self.start.x == self.end.x
52 }
53}
54
55impl<T: CoordType> Edge<T> {
56 pub fn vec2(&self) -> Vec2<T> {
58 self.end - self.start
59 }
60
61 pub fn side_of(&self, point: Point<T>) -> Side {
70 assert!(!self.is_degenerate());
71
72 let a = self.vec2();
73 let b = point - self.start;
74
75 if a.x.is_zero() {
77 let side = if point.x < self.start.x {
78 Side::Left
79 } else if point.x > self.start.x {
80 Side::Right
81 } else {
82 Side::Center
83 };
84
85 let side = if self.start.y < self.end.y {
87 side
88 } else {
89 side.other()
90 };
91
92 return side;
93 } else if a.y.is_zero() {
94 let side = if point.y < self.start.y {
95 Side::Right
96 } else if point.y > self.start.y {
97 Side::Left
98 } else {
99 Side::Center
100 };
101
102 let side = if self.start.x < self.end.x {
104 side
105 } else {
106 side.other()
107 };
108
109 return side;
110 }
111
112 let area = b.cross_prod(a);
113
114 if area.is_zero() {
115 Side::Center
116 } else if area < T::zero() {
117 Side::Left
118 } else {
119 Side::Right
120 }
121 }
122
123 pub fn contains_point(&self, point: Point<T>) -> Contains {
126 if self.start == point || self.end == point {
127 Contains::OnBounds
128 } else if self.is_degenerate() {
129 Contains::No
130 } else {
131 let a = self.start - point;
132 let b = self.end - point;
133 if a.cross_prod(b).is_zero() && a.dot(b) <= T::zero() {
136 Contains::WithinBounds
137 } else {
138 Contains::No
139 }
140 }
141 }
142
143 pub fn line_contains_point(&self, point: Point<T>) -> bool {
145 if self.is_degenerate() {
146 self.start == point
147 } else {
148 let l = self.vec2();
149 let b = point - self.start;
150
151 l.cross_prod(b).is_zero()
152 }
153 }
154
155 pub fn is_parallel(&self, other: &Self) -> bool {
157 if self.is_degenerate() || other.is_degenerate() {
158 false
159 } else {
160 let a = self.vec2();
161 let b = other.vec2();
162
163 if a.x.is_zero() {
164 b.x.is_zero()
166 } else if a.y.is_zero() {
167 b.y.is_zero()
169 } else if b.x.is_zero() || b.y.is_zero() {
170 false
171 } else {
172 a.cross_prod(b).is_zero()
173 }
174 }
175 }
176
177 pub fn is_collinear(&self, other: &Self) -> bool {
179 if self.is_degenerate() || other.is_degenerate() {
180 false
181 } else {
182 let v = self.vec2();
183 let a = other.start - self.start;
184 let b = other.end - self.start;
185
186 if self.start.x == self.end.x {
187 other.start.x == self.start.x && other.end.x == self.start.x
190 } else if self.start.y == self.end.y {
191 other.start.y == self.start.y && other.end.y == self.start.y
193 } else if other.start.x == other.end.x {
194 self.start.x == other.start.x && self.end.x == other.start.x
195 } else if other.start.y == other.end.y {
196 self.start.y == other.start.y && self.end.y == other.start.y
198 } else {
199 v.cross_prod(a).is_zero() && v.cross_prod(b).is_zero()
200 }
201 }
202 }
203
204 pub fn is_coincident(&self, other: &Self) -> bool {
208 let self_start_in_other = other.contains_point(self.start).inclusive_bounds();
212 let self_end_in_other = other.contains_point(self.end).inclusive_bounds();
213 let other_start_in_self = self.contains_point(other.start).inclusive_bounds();
214 let other_end_in_self = self.contains_point(other.end).inclusive_bounds();
215
216 let share_more_than_one_point = self.end != other.start
217 && self.start != other.end
218 && (self_start_in_other || other_start_in_self)
219 && (other_end_in_self || self_end_in_other);
220
221 debug_assert!(if share_more_than_one_point {
223 self.is_parallel(other)
224 } else {
225 true
226 });
227
228 let oriented_the_same_way = self.vec2().dot(other.vec2()) > T::zero();
229
230 share_more_than_one_point && oriented_the_same_way
231 }
232
233 pub fn is_parallel_approx(&self, other: &Self, epsilon_squared: T) -> bool {
237 if self.is_degenerate() || other.is_degenerate() {
238 false
239 } else {
240 let d1 = self.vec2();
241 let d2 = other.vec2();
242
243 let len1_sqr = d1.norm2_squared();
244 let len2_sqr = d2.norm2_squared();
245
246 let cross = d1.cross_prod(d2);
247 let cross_sqr = cross * cross;
248
249 cross_sqr <= len1_sqr * len2_sqr * epsilon_squared
251 }
252 }
253
254 pub fn is_collinear_approx(&self, other: &Self, epsilon_squared: T) -> bool {
257 if self.is_degenerate() || other.is_degenerate() {
258 false
259 } else {
260 let d1 = self.vec2();
261 let d2 = other.vec2();
262
263 let len1_sqr = d1.norm2_squared();
264 let len2_sqr = d2.norm2_squared();
265
266 let cross = d1.cross_prod(d2);
267 let cross_sqr = cross * cross;
268
269 let approx_parallel = cross_sqr <= len1_sqr * len2_sqr * epsilon_squared;
270
271 if approx_parallel {
272 let e = other.start - self.start;
273 let len_e_sqrt = e.norm2_squared();
274 let cross = e.cross_prod(d1);
275 let cross_sqr = cross * cross;
276 cross_sqr <= len1_sqr * len_e_sqrt * epsilon_squared
277 } else {
278 false
279 }
280 }
281 }
282
283 pub fn lines_intersect_approx(&self, other: &Self, epsilon_squared: T) -> bool {
286 !self.is_parallel_approx(other, epsilon_squared)
287 || self.is_collinear_approx(other, epsilon_squared)
288 }
289
290 pub fn crossed_by_line(&self, other: &Self) -> Contains {
296 let side1 = other.side_of(self.start);
298
299 if side1 == Side::Center {
300 Contains::OnBounds
301 } else {
302 let side2 = other.side_of(self.end);
303
304 if side2 == Side::Center {
305 Contains::OnBounds
306 } else if side1 == side2 {
307 Contains::No
308 } else {
309 Contains::WithinBounds
310 }
311 }
312 }
313
314 pub fn lines_intersect(&self, other: &Self) -> bool {
317 !self.is_parallel(other) || self.is_collinear(other)
318 }
319}
320
321impl<T: Copy> From<Edge<T>> for (Point<T>, Point<T>) {
322 fn from(e: Edge<T>) -> Self {
323 (e.start, e.end)
324 }
325}
326
327impl<T: Copy> From<&Edge<T>> for (Point<T>, Point<T>) {
328 fn from(e: &Edge<T>) -> Self {
329 (e.start, e.end)
330 }
331}
332
333impl<T: CoordType> From<(Point<T>, Point<T>)> for Edge<T> {
334 fn from(points: (Point<T>, Point<T>)) -> Self {
335 Edge::new(points.0, points.1)
336 }
337}
338
339impl<T: CoordType> From<[Point<T>; 2]> for Edge<T> {
340 fn from(points: [Point<T>; 2]) -> Self {
341 Edge::new(points[0], points[1])
342 }
343}
344
345impl<T: Copy> MapPoint<T> for Edge<T> {
346 fn transform<F>(&self, transformate: F) -> Self
347 where
348 F: Fn(Point<T>) -> Point<T>
349 {
350 Self {
351 start: transformate(self.start),
352 end: transformate(self.end)
353 }
354 }
355}
356
357impl<T: Copy + PartialOrd> BoundingBox<T> for Edge<T> {
358 fn bounding_box(&self) -> Rect<T> {
359 Rect::new(self.start, self.end)
360 }
361}
362
363impl<T: Copy + PartialOrd> TryBoundingBox<T> for Edge<T> {
364 fn try_bounding_box(&self) -> Option<Rect<T>> {
365 Some(self.bounding_box())
366 }
367}
368
369impl<Src: Copy + NumCast, Dst: Copy + NumCast> Cast<Src, Dst> for Edge<Src> {
370 type Output = Edge<Dst>;
371
372 fn try_cast(&self) -> Option<Self::Output> {
373 match (self.start.try_cast(), self.end.try_cast()) {
374 (Some(s), Some(e)) => Some(Edge::new(s, e)),
375 _ => None,
376 }
377 }
378}
379
380
381
382#[cfg(test)]
383mod tests {
384 extern crate rand;
385
386 use super::*;
387 use crate::shape::Edge;
388 use crate::shape::Point;
389
390 #[test]
391 fn test_is_parallel() {
392 let e1 = Edge::new((1, 2), (3, 4));
393 let e2 = Edge::new((1 - 10, 2 - 20), (5 - 10, 6 - 20));
394 let e3 = Edge::new((1 - 10, 2 - 20), (5 - 10, 6 - 20 + 1));
395
396 assert!(e1.is_parallel(&e2));
397 assert!(!e1.is_parallel(&e3));
398
399 assert!(e1.is_parallel_approx(&e2, 0));
400 assert!(!e1.is_parallel_approx(&e3, 0));
401
402 assert!(e1.is_parallel_approx(&e3, 1));
403 }
404
405 #[test]
406 fn test_is_collinear() {
407 let e1 = Edge::new((0, 0), (1, 2));
408 let e2 = Edge::new((10, 20), (100, 200));
409 assert!(e1.is_collinear(&e2));
410 assert!(e2.is_collinear(&e1));
411 assert!(e1.is_collinear_approx(&e2, 0));
412 assert!(e2.is_collinear_approx(&e1, 0));
413
414 let e1 = Edge::new((0i64, 0), (1, 2));
416 let e2 = Edge::new((10, 20), (1000, 2001));
417 assert!(!e1.is_collinear(&e2));
418 assert!(!e2.is_collinear(&e1));
419 assert!(!e1.is_collinear_approx(&e2, 0));
420 assert!(!e2.is_collinear_approx(&e1, 0));
421
422 assert!(e1.is_collinear_approx(&e2, 1));
423 assert!(e2.is_collinear_approx(&e1, 1));
424 }
425
426 #[test]
427 fn test_line_contains_point() {
428 let e1 = Edge::new((1, 2), (5, 6));
429 let p0 = Point::new(1, 2);
430 let p1 = Point::new(3, 4);
431 let p2 = Point::new(5, 6);
432 let p3 = Point::new(6, 7);
433 let p4 = Point::new(0, 1);
434 let p5 = Point::new(0, 0);
435
436 assert!(e1.line_contains_point(p0));
437 assert!(e1.line_contains_point(p1));
438 assert!(e1.line_contains_point(p2));
439 assert!(e1.line_contains_point(p3));
440 assert!(e1.line_contains_point(p4));
441 assert!(!e1.line_contains_point(p5));
442
443 let e1 = Edge::new((0, 0), (1, 1));
444 assert!(e1.line_contains_point(Point::new(2, 2)));
445 assert!(e1.line_contains_point(Point::new(-1, -1)));
446 }
447
448 #[test]
449 fn test_contains() {
450 let e1 = Edge::new((1, 2), (5, 6));
451 let p0 = Point::new(1, 2);
452 let p1 = Point::new(3, 4);
453 let p2 = Point::new(5, 6);
454 let p3 = Point::new(0, 0);
455 let p4 = Point::new(6, 7);
456
457 assert!(e1.contains_point(p0).inclusive_bounds());
458 assert!(e1.contains_point(p1).inclusive_bounds());
459 assert!(e1.contains_point(p2).inclusive_bounds());
460 assert!(!e1.contains_point(p3).inclusive_bounds());
461 assert!(!e1.contains_point(p4).inclusive_bounds());
462
463 let e1 = Edge::new((0, 0), (1, 1));
464 let p0 = Point::new(2, 2);
465 assert!(!e1.contains_point(p0).inclusive_bounds());
466 }
467
468 #[test]
469 fn test_side_of() {
470 let e1 = Edge::new((1, 0), (4, 4));
471 let p1 = Point::new(-10, 0);
472 let p2 = Point::new(10, -10);
473 let p3 = Point::new(1, 0);
474
475 assert_eq!(e1.side_of(p1), Side::Left);
476 assert_eq!(e1.side_of(p2), Side::Right);
477 assert_eq!(e1.side_of(p3), Side::Center);
478 }
479
480 #[test]
481 fn test_crossed_by() {
482 let e1 = Edge::new((1, 0), (4, 4));
483 let e2 = Edge::new((1 + 1, 0), (4 + 1, 4));
484 let e3 = Edge::new((1, 0), (4, 5));
485 let e4 = Edge::new((2, -2), (0, 0));
486
487 assert!(e1.crossed_by_line(&e1).inclusive_bounds());
489
490 assert!(e1.is_parallel(&e2));
492 assert!(!e1.crossed_by_line(&e2).inclusive_bounds());
493
494 assert!(e1.crossed_by_line(&e3).inclusive_bounds());
496
497 assert!(!e1.crossed_by_line(&e4).inclusive_bounds());
499 assert!(e4.crossed_by_line(&e1).inclusive_bounds());
500 }
501
502 #[test]
503 fn test_intersect() {
504 let e1 = Edge::new((0, 0), (4, 4));
505 let e3 = Edge::new((0, -1), (-1, 1));
506
507 assert!(!e1.crossed_by_line(&e3).inclusive_bounds());
508 assert!(e3.crossed_by_line(&e1).inclusive_bounds());
509
510 let e1 = Edge::new((0, 0), (8, 8));
512 let e2 = Edge::new((4, 0), (3, 1));
513 assert!(!e1.is_rectilinear());
514 assert!(!e2.is_rectilinear());
515 assert!(e1.bounding_box().touches(&e2.bounding_box()));
516 assert!(e1.lines_intersect(&e2));
517 }
518
519 #[test]
520 fn test_coincident() {
521 let e1 = Edge::new((0, 0), (2, 2));
522 let e2 = Edge::new((1, 1), (3, 3));
523 assert!(e1.is_coincident(&e2));
524 assert!(e2.is_coincident(&e1));
525
526 let e1 = Edge::new((0, 0), (3, 3));
527 let e2 = Edge::new((1, 1), (2, 2));
528 assert!(e1.is_coincident(&e2));
529 assert!(e2.is_coincident(&e1));
530
531 let e1 = Edge::new((0, 0), (1, 1));
532 let e2 = Edge::new((1, 1), (2, 2));
533 assert!(!e1.is_coincident(&e2));
534 assert!(!e2.is_coincident(&e1));
535 }
536}