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
use geo::{Coordinate, GeoNum};
use std::cmp::Ordering;
#[derive(Debug, Clone)]
pub(crate) struct Event<T: GeoNum> {
pub point: SweepPoint<T>,
pub ty: EventType,
pub segment_key: usize,
}
impl<T: GeoNum> PartialEq for Event<T> {
fn eq(&self, other: &Self) -> bool {
self.point == other.point && self.ty == other.ty
}
}
impl<T: GeoNum> Eq for Event<T> {}
impl<T: GeoNum> PartialOrd for Event<T> {
fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
Some(self.cmp(other))
}
}
impl<T: GeoNum> Ord for Event<T> {
fn cmp(&self, other: &Self) -> Ordering {
self.point
.cmp(&other.point)
.then_with(|| self.ty.cmp(&other.ty))
.reverse()
}
}
#[derive(Debug, PartialOrd, Ord, PartialEq, Eq, Clone, Copy)]
pub(crate) enum EventType {
PointLeft,
LineRight,
LineLeft,
PointRight,
}
#[derive(PartialEq, Clone, Copy)]
pub struct SweepPoint<T: GeoNum>(Coordinate<T>);
impl<T: GeoNum> std::fmt::Debug for SweepPoint<T> {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.debug_tuple("Pt")
.field(&self.0.x)
.field(&self.0.y)
.finish()
}
}
impl<T: GeoNum> PartialOrd for SweepPoint<T> {
fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
match self.0.x.partial_cmp(&other.0.x) {
Some(Ordering::Equal) => self.0.y.partial_cmp(&other.0.y),
o => o,
}
}
}
impl<T: GeoNum> Ord for SweepPoint<T> {
fn cmp(&self, other: &Self) -> Ordering {
self.partial_cmp(other).unwrap()
}
}
impl<T: GeoNum> Eq for SweepPoint<T> {}
impl<T: GeoNum> SweepPoint<T> {
pub fn coord(&self) -> Coordinate<T> {
self.0
}
}
impl<T: GeoNum> From<Coordinate<T>> for SweepPoint<T> {
fn from(pt: Coordinate<T>) -> Self {
SweepPoint(pt)
}
}
impl<T: GeoNum> From<(T, T)> for SweepPoint<T> {
fn from(coords: (T, T)) -> Self {
Coordinate::from(coords).into()
}
}
#[cfg(test)]
mod tests {
use std::iter::from_fn;
use super::*;
#[test]
fn test_sweep_point_ordering() {
let p1 = SweepPoint::from(Coordinate { x: 0., y: 0. });
let p2 = SweepPoint::from(Coordinate { x: 1., y: 0. });
let p3 = SweepPoint::from(Coordinate { x: 1., y: 1. });
let p4 = SweepPoint::from(Coordinate { x: 1., y: 1. });
assert!(p1 < p2);
assert!(p1 < p3);
assert!(p2 < p3);
assert!(p3 <= p4);
}
#[test]
fn test_event_ordering() {
let e1 = Event {
point: SweepPoint::from(Coordinate { x: 0., y: 0. }),
ty: EventType::LineLeft,
segment_key: 0,
};
let e2 = Event {
point: SweepPoint::from(Coordinate { x: 1., y: 0. }),
ty: EventType::LineRight,
segment_key: 1,
};
let e3 = Event {
point: SweepPoint::from(Coordinate { x: 1., y: 0. }),
ty: EventType::LineLeft,
segment_key: 2,
};
let e4 = Event {
point: SweepPoint::from(Coordinate { x: 1., y: 1. }),
ty: EventType::LineRight,
segment_key: 3,
};
use std::collections::BinaryHeap;
let mut heap = BinaryHeap::new();
heap.push(e1);
heap.push(e2);
heap.push(e3);
heap.push(e4);
let order: Vec<_> = from_fn(|| heap.pop()).map(|e| e.segment_key).collect();
assert_eq!(order, vec![0, 1, 2, 3]);
}
}