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
use super::EndPoint;
use super::ILineSegment;
use super::LineSegment;
use super::LineSegmentView;
use super::Point;
use crate::{Intersects, PolygonScalar, TotalOrd};

///////////////////////////////////////////////////////////////////////////////
// DirectedEdge_

#[derive(Debug, Clone, Copy, Eq, PartialEq, Ord, PartialOrd)]
// Directed edge from A to B, including A and excluding B.
pub struct DirectedEdge_<T: TotalOrd, const N: usize> {
  pub src: Point<T, N>,
  pub dst: Point<T, N>,
}

// DirectedEdge_ -> LineSegment
impl<T: TotalOrd, const N: usize> From<DirectedEdge_<T, N>> for LineSegment<T, N> {
  fn from(edge: DirectedEdge_<T, N>) -> LineSegment<T, N> {
    LineSegment::new(EndPoint::Inclusive(edge.src), EndPoint::Exclusive(edge.dst))
  }
}

// DirectedEdge_ -> LineSegmentView
impl<'a, T: TotalOrd, const N: usize> From<&'a DirectedEdge_<T, N>> for LineSegmentView<'a, T, N> {
  fn from(edge: &'a DirectedEdge_<T, N>) -> LineSegmentView<'a, T, N> {
    LineSegmentView::new(
      EndPoint::Inclusive(&edge.src),
      EndPoint::Exclusive(&edge.dst),
    )
  }
}

impl<'a, T> Intersects for &'a DirectedEdge_<T, 2>
where
  T: PolygonScalar,
{
  type Result = ILineSegment<'a, T>;
  fn intersect(self, other: &'a DirectedEdge_<T, 2>) -> Option<Self::Result> {
    LineSegmentView::from(self).intersect(LineSegmentView::from(other))
  }
}

///////////////////////////////////////////////////////////////////////////////
// DirectedEdge

#[derive(Debug, Eq, PartialEq, Ord, PartialOrd)]
// Directed edge from A to B, including A and excluding B.
pub struct DirectedEdge<'a, T: TotalOrd, const N: usize = 2> {
  pub src: &'a Point<T, N>,
  pub dst: &'a Point<T, N>,
}

impl<'a, T: TotalOrd, const N: usize> Copy for DirectedEdge<'a, T, N> {}
impl<'a, T: TotalOrd, const N: usize> Clone for DirectedEdge<'a, T, N> {
  fn clone(&self) -> Self {
    DirectedEdge {
      src: self.src,
      dst: self.dst,
    }
  }
}

impl<'a, T: TotalOrd> DirectedEdge<'a, T, 2> {
  pub fn contains(self, pt: &Point<T, 2>) -> bool
  where
    T: PolygonScalar,
  {
    LineSegmentView::from(self).contains(pt)
  }
}

impl<'a, T: TotalOrd, const N: usize> From<DirectedEdge<'a, T, N>> for LineSegmentView<'a, T, N> {
  fn from(edge: DirectedEdge<'a, T, N>) -> LineSegmentView<'a, T, N> {
    LineSegmentView::new(EndPoint::Inclusive(edge.src), EndPoint::Exclusive(edge.dst))
  }
}

impl<'a, T: TotalOrd, const N: usize> From<&DirectedEdge<'a, T, N>> for LineSegmentView<'a, T, N> {
  fn from(edge: &DirectedEdge<'a, T, N>) -> LineSegmentView<'a, T, N> {
    LineSegmentView::new(EndPoint::Inclusive(edge.src), EndPoint::Exclusive(edge.dst))
  }
}

impl<'a, T> Intersects for DirectedEdge<'a, T, 2>
where
  T: PolygonScalar,
{
  type Result = ILineSegment<'a, T>;
  fn intersect(self, other: DirectedEdge<'a, T, 2>) -> Option<Self::Result> {
    LineSegmentView::from(self).intersect(LineSegmentView::from(other))
  }
}