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
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
use types::{CoordinateType, LineString, Point};
pub(crate) fn twice_signed_ring_area<T>(linestring: &LineString<T>) -> T where T: CoordinateType {
if linestring.0.is_empty() || linestring.0.len() == 1 {
return T::zero();
}
let mut tmp = T::zero();
for line in linestring.lines() {
tmp = tmp + (line.start.x() * line.end.y() - line.end.x() * line.start.y());
}
tmp
}
#[derive(PartialEq, Clone, Debug, Eq)]
pub enum WindingOrder {
Clockwise,
CounterClockwise,
}
pub trait Winding<T> where T: CoordinateType
{
fn winding_order(&self) -> Option<WindingOrder>;
fn is_cw(&self) -> bool {
self.winding_order() == Some(WindingOrder::Clockwise)
}
fn is_ccw(&self) -> bool {
self.winding_order() == Some(WindingOrder::CounterClockwise)
}
fn points_cw<'a>(&'a self) -> Box<Iterator<Item=&'a Point<T>> + 'a>;
fn points_ccw<'a>(&'a self) -> Box<Iterator<Item=&'a Point<T>> + 'a>;
fn make_cw_winding(&mut self);
fn make_ccw_winding(&mut self);
fn clone_to_winding_order(&self, winding_order: WindingOrder) -> Self where Self: Sized+Clone {
let mut new: Self = self.clone();
new.make_winding_order(winding_order);
new
}
fn make_winding_order(&mut self, winding_order: WindingOrder) {
match winding_order {
WindingOrder::Clockwise => self.make_cw_winding(),
WindingOrder::CounterClockwise => self.make_ccw_winding(),
}
}
}
impl<T> Winding<T> for LineString<T>
where T: CoordinateType
{
fn winding_order(&self) -> Option<WindingOrder> {
let shoelace = twice_signed_ring_area(self);
if shoelace < T::zero() {
Some(WindingOrder::Clockwise)
} else if shoelace > T::zero() {
Some(WindingOrder::CounterClockwise)
} else if shoelace == T::zero() {
None
} else {
unreachable!()
}
}
fn points_cw<'a>(&'a self) -> Box<Iterator<Item=&'a Point<T>> + 'a> {
match self.winding_order() {
Some(WindingOrder::CounterClockwise) => Box::new(self.0.iter().rev()),
_ => Box::new(self.0.iter()),
}
}
fn points_ccw<'a>(&'a self) -> Box<Iterator<Item=&'a Point<T>> + 'a> {
match self.winding_order() {
Some(WindingOrder::Clockwise) => Box::new(self.0.iter().rev()),
_ => Box::new(self.0.iter()),
}
}
fn make_cw_winding(&mut self) {
match self.winding_order() {
Some(WindingOrder::CounterClockwise) => {
self.0.reverse();
},
_ => {},
}
}
fn make_ccw_winding(&mut self) {
match self.winding_order() {
Some(WindingOrder::Clockwise) => {
self.0.reverse();
},
_ => {}
}
}
}
#[cfg(test)]
mod test {
use super::*;
#[test]
fn winding_order() {
let a = Point::new(0., 0.);
let b = Point::new(2., 0.);
let c = Point::new(1., 2.);
let cw_line = LineString(vec![a, c, b, a].clone());
let ccw_line = LineString(vec![a, b, c, a].clone());
assert_eq!(cw_line.winding_order(), Some(WindingOrder::Clockwise));
assert_eq!(cw_line.is_cw(), true);
assert_eq!(cw_line.is_ccw(), false);
assert_eq!(ccw_line.winding_order(), Some(WindingOrder::CounterClockwise));
assert_eq!(ccw_line.is_cw(), false);
assert_eq!(ccw_line.is_ccw(), true);
let cw_points1: Vec<_> = cw_line.points_cw().cloned().collect();
assert_eq!(cw_points1.len(), 4);
assert_eq!(cw_points1[0], a);
assert_eq!(cw_points1[1], c);
assert_eq!(cw_points1[2], b);
assert_eq!(cw_points1[3], a);
let ccw_points1: Vec<_> = cw_line.points_ccw().cloned().collect();
assert_eq!(ccw_points1.len(), 4);
assert_eq!(ccw_points1[0], a);
assert_eq!(ccw_points1[1], b);
assert_eq!(ccw_points1[2], c);
assert_eq!(ccw_points1[3], a);
assert_ne!(cw_points1, ccw_points1);
let cw_points2: Vec<_> = ccw_line.points_cw().cloned().collect();
let ccw_points2: Vec<_> = ccw_line.points_ccw().cloned().collect();
assert_eq!(cw_points2, cw_points2);
assert_eq!(ccw_points2, ccw_points2);
let mut new_line1 = ccw_line.clone();
new_line1.make_cw_winding();
assert_eq!(new_line1.winding_order(), Some(WindingOrder::Clockwise));
assert_eq!(new_line1, cw_line);
assert_ne!(new_line1, ccw_line);
let mut new_line2 = cw_line.clone();
new_line2.make_ccw_winding();
assert_eq!(new_line2.winding_order(), Some(WindingOrder::CounterClockwise));
assert_ne!(new_line2, cw_line);
assert_eq!(new_line2, ccw_line);
}
}