Skip to content

Commit 83e3987

Browse files
committed
fix: update melkman to remove collinear points
1 parent 5c1cdcd commit 83e3987

File tree

1 file changed

+21
-5
lines changed

1 file changed

+21
-5
lines changed

src/algorithms/melkman.rs

Lines changed: 21 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,25 +18,28 @@ impl<T> SecondIndex<T> for VecDeque<T> {
1818
}
1919

2020
fn melkman<T: GeoNum>(poly: &Polygon<T>) -> Vec<(usize, Coord<T>)> {
21-
let mut poly_iter = poly.exterior_coords_iter().enumerate();
21+
let mut poly_iter = poly
22+
.exterior_coords_iter()
23+
.take(poly.exterior().0.len() - 1)
24+
.enumerate();
2225
let x = poly_iter.next().unwrap();
2326
let y = poly_iter.next().unwrap();
2427
let mut hull = VecDeque::from([y, x, y]);
2528

2629
for (index, v) in poly_iter {
2730
if matches!(
2831
T::Ker::orient2d(v, hull.front().unwrap().1, hull.front_less().unwrap().1),
29-
Orientation::CounterClockwise
32+
Orientation::CounterClockwise | Orientation::Collinear
3033
) || matches!(
3134
T::Ker::orient2d(v, hull.back().unwrap().1, hull.back_less().unwrap().1),
32-
Orientation::Clockwise
35+
Orientation::Clockwise | Orientation::Collinear
3336
) {
34-
while let Orientation::CounterClockwise =
37+
while let Orientation::CounterClockwise | Orientation::Collinear =
3538
T::Ker::orient2d(v, hull.front().unwrap().1, hull.front_less().unwrap().1)
3639
{
3740
hull.pop_front();
3841
}
39-
while let Orientation::Clockwise =
42+
while let Orientation::Clockwise | Orientation::Collinear =
4043
T::Ker::orient2d(v, hull.back().unwrap().1, hull.back_less().unwrap().1)
4144
{
4245
hull.pop_back();
@@ -77,4 +80,17 @@ mod test {
7780
let correct = vec![4, 0, 1, 3, 4];
7881
assert_eq!(hull, correct);
7982
}
83+
84+
#[test]
85+
fn colinear_test() {
86+
let poly = polygon![
87+
(x: 0.0, y: 0.0),
88+
(x: 0.0, y: 1.0),
89+
(x: 0.5, y: 0.5),
90+
(x: 1.0, y: 0.0),
91+
];
92+
let hull = poly.hull_indices();
93+
let correct = vec![3, 0, 1, 3];
94+
assert_eq!(hull, correct);
95+
}
8096
}

0 commit comments

Comments
 (0)