@@ -18,25 +18,28 @@ impl<T> SecondIndex<T> for VecDeque<T> {
18
18
}
19
19
20
20
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 ( ) ;
22
25
let x = poly_iter. next ( ) . unwrap ( ) ;
23
26
let y = poly_iter. next ( ) . unwrap ( ) ;
24
27
let mut hull = VecDeque :: from ( [ y, x, y] ) ;
25
28
26
29
for ( index, v) in poly_iter {
27
30
if matches ! (
28
31
T :: Ker :: orient2d( v, hull. front( ) . unwrap( ) . 1 , hull. front_less( ) . unwrap( ) . 1 ) ,
29
- Orientation :: CounterClockwise
32
+ Orientation :: CounterClockwise | Orientation :: Collinear
30
33
) || matches ! (
31
34
T :: Ker :: orient2d( v, hull. back( ) . unwrap( ) . 1 , hull. back_less( ) . unwrap( ) . 1 ) ,
32
- Orientation :: Clockwise
35
+ Orientation :: Clockwise | Orientation :: Collinear
33
36
) {
34
- while let Orientation :: CounterClockwise =
37
+ while let Orientation :: CounterClockwise | Orientation :: Collinear =
35
38
T :: Ker :: orient2d ( v, hull. front ( ) . unwrap ( ) . 1 , hull. front_less ( ) . unwrap ( ) . 1 )
36
39
{
37
40
hull. pop_front ( ) ;
38
41
}
39
- while let Orientation :: Clockwise =
42
+ while let Orientation :: Clockwise | Orientation :: Collinear =
40
43
T :: Ker :: orient2d ( v, hull. back ( ) . unwrap ( ) . 1 , hull. back_less ( ) . unwrap ( ) . 1 )
41
44
{
42
45
hull. pop_back ( ) ;
@@ -77,4 +80,17 @@ mod test {
77
80
let correct = vec ! [ 4 , 0 , 1 , 3 , 4 ] ;
78
81
assert_eq ! ( hull, correct) ;
79
82
}
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
+ }
80
96
}
0 commit comments