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
use super::{CoordNode, Edge, Label, Quadrant};
use crate::{Coordinate, GeoFloat};
use std::cell::RefCell;
use std::fmt;
#[derive(Clone, Debug)]
pub(crate) struct EdgeEnd<F>
where
F: GeoFloat,
{
label: Label,
key: EdgeEndKey<F>,
}
#[derive(Clone)]
pub(crate) struct EdgeEndKey<F>
where
F: GeoFloat,
{
coord_0: Coordinate<F>,
coord_1: Coordinate<F>,
delta: Coordinate<F>,
quadrant: Option<Quadrant>,
}
impl<F: GeoFloat> fmt::Debug for EdgeEndKey<F> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.debug_struct("EdgeEndKey")
.field(
"coords",
&format!("{:?} -> {:?}", &self.coord_0, &self.coord_1),
)
.field("quadrant", &self.quadrant)
.finish()
}
}
impl<F> EdgeEnd<F>
where
F: GeoFloat,
{
pub fn new(coord_0: Coordinate<F>, coord_1: Coordinate<F>, label: Label) -> EdgeEnd<F> {
let delta = coord_1 - coord_0;
let quadrant = Quadrant::new(delta.x, delta.y);
EdgeEnd {
label,
key: EdgeEndKey {
coord_0,
coord_1,
delta,
quadrant,
},
}
}
pub fn label(&self) -> &Label {
&self.label
}
pub fn label_mut(&mut self) -> &mut Label {
&mut self.label
}
pub fn coordinate(&self) -> &Coordinate<F> {
&self.key.coord_0
}
pub fn key(&self) -> &EdgeEndKey<F> {
&self.key
}
}
impl<F> std::cmp::Eq for EdgeEndKey<F> where F: GeoFloat {}
impl<F> std::cmp::PartialEq for EdgeEndKey<F>
where
F: GeoFloat,
{
fn eq(&self, other: &EdgeEndKey<F>) -> bool {
self.delta == other.delta
}
}
impl<F> std::cmp::PartialOrd for EdgeEndKey<F>
where
F: GeoFloat,
{
fn partial_cmp(&self, other: &EdgeEndKey<F>) -> Option<std::cmp::Ordering> {
Some(self.cmp(other))
}
}
impl<F> std::cmp::Ord for EdgeEndKey<F>
where
F: GeoFloat,
{
fn cmp(&self, other: &EdgeEndKey<F>) -> std::cmp::Ordering {
self.compare_direction(other)
}
}
impl<F> EdgeEndKey<F>
where
F: GeoFloat,
{
pub(crate) fn compare_direction(&self, other: &EdgeEndKey<F>) -> std::cmp::Ordering {
use std::cmp::Ordering;
if self.delta == other.delta {
return Ordering::Equal;
}
match (self.quadrant, other.quadrant) {
(Some(q1), Some(q2)) if q1 > q2 => Ordering::Greater,
(Some(q1), Some(q2)) if q1 < q2 => Ordering::Less,
_ => {
use crate::algorithm::kernels::{Kernel, Orientation};
match F::Ker::orient2d(other.coord_0, other.coord_1, self.coord_1) {
Orientation::Clockwise => Ordering::Less,
Orientation::CounterClockwise => Ordering::Greater,
Orientation::Collinear => Ordering::Equal,
}
}
}
}
}
#[cfg(test)]
mod test {
use super::*;
#[test]
fn test_ord() {
let fake_label = Label::empty_line_or_point();
let edge_end_1 = EdgeEnd::new(
Coordinate::zero(),
Coordinate { x: 1.0, y: 1.0 },
fake_label.clone(),
);
let edge_end_2 = EdgeEnd::new(
Coordinate::zero(),
Coordinate { x: 1.0, y: 1.0 },
fake_label.clone(),
);
assert_eq!(
edge_end_1.key().cmp(&edge_end_2.key()),
std::cmp::Ordering::Equal
);
let edge_end_3 = EdgeEnd::new(
Coordinate::zero(),
Coordinate { x: 1.0, y: -1.0 },
fake_label.clone(),
);
assert_eq!(
edge_end_1.key().cmp(&edge_end_3.key()),
std::cmp::Ordering::Less
);
assert_eq!(
edge_end_3.key().cmp(&edge_end_1.key()),
std::cmp::Ordering::Greater
);
}
}