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
use std::cmp::Ordering;

use geo::{
    kernels::{HasKernel, Kernel, Orientation},
    line_intersection::LineIntersection,
    Coordinate, GeoFloat, GeoNum, Line,
};

use crate::events::SweepPoint;

/// Either a line segment or a point.
///
/// The coordinates are ordered (see [`SweepPoint`]) and a line
/// segment must have distinct points (use the `Point` variant if the
/// coordinates are the equal).
#[derive(Debug, Clone, Copy)]
pub enum LineOrPoint<T: GeoNum> {
    Point(SweepPoint<T>),
    Line(SweepPoint<T>, SweepPoint<T>),
}

/// Convert from a [`Line`] ensuring end point ordering.
impl<T: GeoNum> From<Line<T>> for LineOrPoint<T> {
    fn from(l: Line<T>) -> Self {
        let start: SweepPoint<T> = l.start.into();
        let end = l.end.into();
        match start.cmp(&end) {
            Ordering::Less => LineOrPoint::Line(start, end),
            Ordering::Equal => LineOrPoint::Point(start),
            Ordering::Greater => LineOrPoint::Line(end, start),
        }
    }
}

/// Convert from a [`Coordinate`]
impl<T: GeoNum> From<Coordinate<T>> for LineOrPoint<T> {
    fn from(c: Coordinate<T>) -> Self {
        LineOrPoint::Point(c.into())
    }
}

impl<T: GeoNum> LineOrPoint<T> {
    /// Checks if the variant is a line.
    #[inline]
    pub fn is_line(&self) -> bool {
        matches!(self, LineOrPoint::Line(_, _))
    }

    /// Return a [`Line`] if it is one, otherwise `None`.
    #[inline]
    pub fn line(&self) -> Line<T> {
        match self {
            LineOrPoint::Line(p, q) => Line::new(p.coord(), q.coord()),
            LineOrPoint::Point(p) => Line::new(p.coord(), p.coord()),
        }
    }

    /// Returns the lexicographic first coordinate of the geometry.
    pub(crate) fn first(&self) -> SweepPoint<T> {
        *match self {
            LineOrPoint::Point(p) => p,
            LineOrPoint::Line(p, _) => p,
        }
    }
}
impl<T: GeoFloat> LineOrPoint<T> {
    /// Intersect a line with self and return a point, a overlapping segment or `None`.
    ///
    /// The `other` argument must be a line variant (panics otherwise).
    pub fn intersect_line(&self, other: &Self) -> Option<Self> {
        assert!(other.is_line(), "tried to intersect with a point variant!");

        let line = other.line();
        match *self {
            LineOrPoint::Point(p) => {
                if <T as HasKernel>::Ker::orient2d(line.start, p.coord(), line.end)
                    == Orientation::Collinear
                {
                    let ls = line.start.into();
                    let le = line.end.into();
                    if p >= ls && p <= le {
                        Some(*self)
                    } else {
                        None
                    }
                } else {
                    None
                }
            }
            LineOrPoint::Line(p, q) => {
                use geo::algorithm::line_intersection::line_intersection;
                line_intersection(Line::new(p.coord(), q.coord()), line).map(|l| match l {
                    LineIntersection::SinglePoint { intersection, .. } => intersection.into(),
                    LineIntersection::Collinear { intersection } => intersection.into(),
                })
            }
        }
    }

    #[cfg(test)]
    pub fn coords_equal(&self, other: &LineOrPoint<T>) -> bool {
        match (self, other) {
            (LineOrPoint::Point(p), LineOrPoint::Point(q)) => p == q,
            (LineOrPoint::Line(p1, q1), LineOrPoint::Line(p2, q2)) => p1 == p2 && q1 == q2,
            _ => false,
        }
    }
}

/// Equality based on ordering defined for segments as per algorithm.
impl<T: GeoNum> PartialEq for LineOrPoint<T> {
    #[inline]
    fn eq(&self, other: &Self) -> bool {
        self.partial_cmp(other) == Some(Ordering::Equal)
    }
}

/// Ordering defined for segments as per algorithm.
///
/// Requires the following conditions:
///
/// 1. If comparing two lines, the both left ends must be strictly
/// smaller than both right ends.
///
/// 2. A point is treated as a infinitesimal small vertical segment
/// centered at its coordinates.
impl<T: GeoNum> PartialOrd for LineOrPoint<T> {
    fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
        match (self, other) {
            (LineOrPoint::Point(p), LineOrPoint::Point(q)) => {
                if p == q {
                    Some(Ordering::Equal)
                } else {
                    // Unequal points do not satisfy pre-condition and
                    // can't be ordered.
                    None
                }
            }
            (LineOrPoint::Point(_), LineOrPoint::Line(_, _)) => {
                other.partial_cmp(self).map(Ordering::reverse)
            }
            (LineOrPoint::Line(p, q), LineOrPoint::Point(r)) => {
                if r > q || p > r {
                    return None;
                }
                Some(
                    orientation_as_ordering(T::Ker::orient2d(p.coord(), q.coord(), r.coord()))
                        .then(Ordering::Greater),
                )
            }
            (LineOrPoint::Line(p1, q1), LineOrPoint::Line(p2, q2)) => {
                if p1 > p2 {
                    return other.partial_cmp(self).map(Ordering::reverse);
                }
                if p1 >= q2 || p2 >= q2 {
                    return None;
                }

                // Assertion: p1 <= p2
                // Assertion: pi < q_j
                Some(
                    orientation_as_ordering(T::Ker::orient2d(p1.coord(), q1.coord(), p2.coord()))
                        .then_with(|| {
                            orientation_as_ordering(T::Ker::orient2d(
                                p1.coord(),
                                q1.coord(),
                                q2.coord(),
                            ))
                        }),
                )
            }
        }
    }
}

/// Helper to convert orientation-2d into an ordering
#[inline]
fn orientation_as_ordering(orientation: Orientation) -> Ordering {
    match orientation {
        Orientation::CounterClockwise => Ordering::Less,
        Orientation::Clockwise => Ordering::Greater,
        Orientation::Collinear => Ordering::Equal,
    }
}