@@ -47,9 +47,9 @@ fn find_extremes<T, F>(func: F, polygon: &Polygon<T>, convex: bool, oriented: bo
47
47
// let mut poly = polygon.convex_hull();
48
48
// let processed = true;
49
49
// }
50
- // if !oriented and processed {
50
+ // if !oriented && processed {
51
51
// poly = poly.orient()
52
- // } else if !oriented and !processed {
52
+ // } else if !oriented && !processed {
53
53
// poly = polygon.orient();
54
54
// } else {
55
55
// poly = polygon;
@@ -82,97 +82,6 @@ fn polymax_naive<T>(u: &Point<T>, poly: &Polygon<T>) -> Result<Point<T>, ()>
82
82
return Ok ( vertices[ max] ) ;
83
83
}
84
84
85
- // ported from a c++ implementation at
86
- // http://geomalgorithms.com/a14-_extreme_pts.html#polyMax_2D()
87
- // original copyright notice:
88
- // Copyright 2002 softSurfer, 2012-2013 Dan Sunday
89
- // This code may be freely used, distributed and modified for any
90
- // purpose providing that this copyright notice is included with it.
91
- // SoftSurfer makes no warranty for this code, and cannot be held
92
- // liable for any real or imagined damage resulting from its use.
93
- // Users of this code must verify correctness for their application.
94
-
95
- // Original implementation:
96
- // Joseph O'Rourke, Computational Geometry in C (2nd Edition),
97
- // Sect 7.9 "Extreme Point of Convex Polygon" (1998)
98
-
99
- // find a convex, counter-clockwise oriented polygon's maximum vertex in a specified direction
100
- // u: a direction vector. We're using a point to represent this, which is a hack tbh
101
- // this should run in O(log n) time.
102
- // This implementation can't calculate minimum x
103
- // see Section 5.1 at http://codeforces.com/blog/entry/48868 for a discussion
104
- fn polymax < T > ( u : & Point < T > , poly : & Polygon < T > ) -> Result < Point < T > , ( ) >
105
- where T : Float
106
- {
107
- let vertices = & poly. exterior . 0 ;
108
- let n = vertices. len ( ) ;
109
- // these are used to divide the vertices slice
110
- // start chain = [0, n] with vertices[n] = vertices[0]
111
- let mut start: usize = 0 ;
112
- let mut end: usize = n;
113
- let mut mid: usize ;
114
-
115
- // edge vectors at vertices[a] and vertices[c]
116
- let mut vec_c: Point < T > ;
117
- let vec_a = vertices[ 1 ] - vertices[ 0 ] ;
118
- // test for "up" direction of vec_a
119
- let mut up_a = up ( u, & vec_a) ;
120
-
121
- // test if vertices[0] is a local maximum
122
- if !up_a && !above ( u, & vertices[ n - 1 ] , & vertices[ 0 ] ) {
123
- return Ok ( vertices[ 0 ] ) ;
124
- }
125
- loop {
126
- mid = ( start + end) / 2 ;
127
- vec_c = vertices[ mid + 1 ] - vertices[ mid] ;
128
- let up_c = up ( u, & vec_c) ;
129
- if !up_c && !above ( u, & vertices[ mid - 1 ] , & vertices[ mid] ) {
130
- // vertices[mid] is a local maximum, thus it is a maximum
131
- return Ok ( vertices[ mid] ) ;
132
- }
133
- // no max yet, so continue with the binary search
134
- // pick one of the two subchains [start, mid] or [mid, end]
135
- if up_a {
136
- // vec_a points up
137
- if !up_c {
138
- // vec_c points down
139
- end = mid; // select [start, mid]
140
- } else {
141
- // vec_c points up
142
- if above ( u, & vertices[ start] , & vertices[ mid] ) {
143
- // vertices[start] is above vertices[mid]
144
- end = mid; // select [start, mid]
145
- } else {
146
- // vertices[start] is below vertices[mid]
147
- start = mid; // select [mid, end]
148
- up_a = up_c;
149
- }
150
- }
151
- } else {
152
- // vec_a points down
153
- if up_c {
154
- // vec_c points up
155
- start = mid; // select [mid, end]
156
- up_a = up_c;
157
- } else {
158
- // vec_c points down
159
- if below ( u, & vertices[ start] , & vertices[ mid] ) {
160
- // vertices[start] is below vertices[mid]
161
- end = mid; // select [start, mid]
162
- } else {
163
- // vertices[start] is above vertices[mid]
164
- start = mid; // select [mid, end]
165
- up_a = up_c;
166
- }
167
- }
168
- }
169
- // something went really badly wrong
170
- if end <= start + 1 {
171
- return Err ( ( ) ) ;
172
- }
173
- }
174
- }
175
-
176
85
pub trait ExtremePoints < T : Float > {
177
86
/// Find the extreme `x` and `y` points of a Polygon
178
87
///
@@ -229,21 +138,6 @@ mod test {
229
138
assert_eq ! ( min_x, correct) ;
230
139
}
231
140
#[ test]
232
- #[ should_panic]
233
- // this test should panic, because the algorithm can't find minimum x
234
- fn test_polygon_extreme_x_fast ( ) {
235
- // a diamond shape
236
- let points_raw = vec ! [ ( 1.0 , 0.0 ) , ( 2.0 , 1.0 ) , ( 1.0 , 2.0 ) , ( 0.0 , 1.0 ) , ( 1.0 , 0.0 ) ] ;
237
- let points = points_raw
238
- . iter ( )
239
- . map ( |e| Point :: new ( e. 0 , e. 1 ) )
240
- . collect :: < Vec < _ > > ( ) ;
241
- let poly1 = Polygon :: new ( LineString ( points) , vec ! [ ] ) ;
242
- let min_x = polymax ( & Point :: new ( -1. , 0. ) , & poly1) . unwrap ( ) ;
243
- let correct = Point :: new ( 0. , 1. ) ;
244
- assert_eq ! ( min_x, correct) ;
245
- }
246
- #[ test]
247
141
fn test_polygon_extreme_wrapper ( ) {
248
142
// a diamond shape with a bump on the top-right edge
249
143
let points_raw =
0 commit comments