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
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286

use crate::data::{Point, Polygon, PolygonConvex};
use crate::{Error, Orientation, PolygonScalar, TotalOrd};

// https://en.wikipedia.org/wiki/Graham_scan

// Doesn't allocate.
// Properties:
//    No panics.
//    All Ok results are valid convex polygons.
//    No points are outside the resulting convex polygon.
/// Convex hull of a set of points.
///
/// [Graham scan][wiki] algorithm for finding the smallest convex polygon which
/// contains all the given points.
///
/// # Errors
/// Will return an error iff the input set contains less than three distinct points.
///
/// # Properties
/// * No points from the input set will be outside the returned convex polygon.
/// * All vertices in the convex polygon are from the input set.
///
/// # Time complexity
/// $O(n \log n)$
///
/// # Examples
///
/// rust
/// # pub fn main() {
/// # use rgeometry::algorithms::convex_hull;
/// # use rgeometry::data::Point;
/// # use rgeometry::Error;
/// let empty_set: Vec<Point<i32,2>> = vec![];
/// assert_eq!(
///   convex_hull(empty_set).err(),
///   Some(Error::InsufficientVertices))
/// # }
/// 
///
/// rust
/// # pub fn main() {
/// # use rgeometry::algorithms::convex_hull;
/// # use rgeometry::data::Point;
/// # use rgeometry::Error;
/// let dups = vec![Point::new([0,0])].repeat(3);
/// assert_eq!(
///   convex_hull(dups).err(),
///   Some(Error::InsufficientVertices))
/// # }
/// 
///
/// no_run
/// # pub fn main() {
/// #   use rgeometry::algorithms::convex_hull;
/// #   use rgeometry_wasm::playground::*;
/// #
/// #   static START: std::sync::Once = std::sync::Once::new();
/// #   START.call_once(|| on_mousemove(|_event| main()));
/// #
/// #   clear_screen();
/// #   set_viewport(2., 2.);
/// #
/// #   let pts = with_points(7);
/// #   let points = pts.clone();
/// if let Ok(convex) = convex_hull(points) {
///   render_polygon(&convex);
/// #   context().set_fill_style(&"grey".into());
/// #   context().fill();
/// }
/// #   for pt in &pts {
/// #     render_point(&pt);
/// #   }
/// # }
/// 
///
/// <iframe src="https://web.rgeometry.org/wasm/gist/eac484cd855d001815d23a053919b5ca"></iframe>
///
/// [wiki]: https://en.wikipedia.org/wiki/Graham_scan
pub fn convex_hull<T>(mut pts: Vec<Point<T>>) -> Result<PolygonConvex<T>, Error>
where
T: PolygonScalar,
{
let smallest = &smallest_point(&pts)?;

pts.sort_unstable_by(|a, b| {
smallest
.ccw_cmp_around(a, b)
.then_with(|| (a.y_coord(), a.x_coord()).total_cmp(&(b.y_coord(), b.x_coord())))
// .then_with(|| smallest.cmp_distance_to(a, b))
});
if pts.len() < 3 {
return Err(Error::InsufficientVertices);
}
debug_assert_eq!(&pts[0], smallest);
let mut write_idx = 1;
// Drop points that are co-linear with our origin.
{
let origin = pts[write_idx - 1].clone();
if Point::orient(&origin, &pts[write_idx], &pts[read_idx]) == Orientation::CoLinear {
} else {
break;
}
}
}
// Filter out points until all consecutive points are oriented counter-clockwise.
let p2 = &pts[write_idx];
let p3 = &pts[write_idx - 1];
match Point::orient(p3, p2, p1) {
Orientation::CounterClockWise => {
write_idx += 1;
}
Orientation::ClockWise | Orientation::CoLinear => {
write_idx -= 1;
}
}
}
pts.truncate(write_idx + 1);
if pts.len() < 3 {
return Err(Error::InsufficientVertices);
}
Ok(PolygonConvex::new_unchecked(Polygon::new_unchecked(pts)))
}

// Find the smallest point and remove it from the vector
// O(n)
fn smallest_point<T>(pts: &[Point<T>]) -> Result<Point<T>, Error>
where
T: PolygonScalar,
{
Ok(
pts
.iter()
.min_by(|a, b| TotalOrd::total_cmp(&(a.y_coord(), a.x_coord()), &(b.y_coord(), b.x_coord())))
.ok_or(Error::InsufficientVertices)?
.clone(),
)
}

#[cfg(test)]
#[cfg(not(tarpaulin_include))]
mod tests {
use super::*;
use crate::data::PointLocation;
use crate::testing::*;

use claims::assert_ok;
use num_bigint::BigInt;

use proptest::collection::*;
use proptest::prelude::*;
use test_strategy::proptest;

#[test]
fn convex_hull_colinear() {
let points = vec![
Point::new([0, 0]),
Point::new([1, 0]),
Point::new([2, 0]),
Point::new([3, 0]),
Point::new([4, 0]),
Point::new([1, 1]),
];
let poly = convex_hull(points).unwrap();
assert_ok!(poly.validate());
}

#[test]
fn convex_hull_colinear_rev() {
let points = vec![
Point::new([0, 0]),
Point::new([1, 0]),
Point::new([0, 9]),
Point::new([0, 8]),
Point::new([0, 7]),
Point::new([0, 6]),
];
let poly = convex_hull(points).unwrap();
assert_ok!(poly.validate());
}

#[test]
fn convex_hull_dups() {
let points = vec![
Point::new([0, 0]),
Point::new([1, 0]),
Point::new([0, 0]),
Point::new([1, 0]),
Point::new([2, 2]),
Point::new([2, 2]),
Point::new([5, 1]),
Point::new([5, 1]),
];
let poly = convex_hull(points).unwrap();
assert_ok!(poly.validate());
}

#[test]
fn convex_hull_insufficient_dups() {
let points = vec![
Point::new([0, 0]),
Point::new([0, 0]),
Point::new([2, 2]),
Point::new([2, 2]),
Point::new([0, 0]),
Point::new([2, 2]),
];
assert_eq!(convex_hull(points).err(), Some(Error::InsufficientVertices));
}

#[test]
fn convex_hull_invalid() {
let points: Vec<Point<i64>> = vec![
Point { array: [0, 0] },
Point { array: [100, 0] },
Point { array: [50, 1] },
Point { array: [40, 1] },
Point { array: [0, 100] },
];
let points: Vec<Point<BigInt, 2>> = points.into_iter().map(|pt| pt.cast()).collect();
let poly = convex_hull(points).unwrap();
assert_ok!(poly.validate());
}

#[test]
fn unit_1() {
let points: Vec<Point<BigInt>> = vec![
Point::new([0, 0]).into(),
Point::new([-1, 1]).into(),
Point::new([0, 1]).into(),
Point::new([-717193444810564826, 1]).into(),
];
let poly = convex_hull(points).unwrap();
assert_ok!(poly.validate());
}

#[test]
fn unit_2() {
let points: Vec<Point<i8>> = vec![
Point::new([0, 0]),
Point::new([0, -10]),
Point::new([-13, 0]),
];
let poly = convex_hull(points).unwrap();
assert_ok!(poly.validate());
}

#[proptest]
fn convex_hull_prop(#[strategy(vec(any_r(), 0..100))] pts: Vec<Point<BigInt>>) {
if let Ok(poly) = convex_hull(pts.clone()) {
// Prop #1: Results are valid.
prop_assert_eq!(poly.validate().err(), None);
// Prop #2: No points from the input set are outside the polygon.
for pt in pts.iter() {
prop_assert_ne!(poly.locate(pt), PointLocation::Outside)
}
// Prop #3: All vertices are in the input set.
for pt in poly.iter() {
prop_assert!(pts.contains(pt))
}
}
}

#[proptest]
fn convex_hull_prop_i8(#[strategy(vec(any::<Point<i8>>(), 0..100))] pts: Vec<Point<i8>>) {
if let Ok(poly) = convex_hull(pts.clone()) {
// Prop #1: Results are valid.
prop_assert_eq!(poly.validate().err(), None);
// Prop #2: No points from the input set are outside the polygon.
for pt in pts.iter() {
prop_assert_ne!(poly.locate(pt), PointLocation::Outside)
}
// Prop #3: All vertices are in the input set.
for pt in poly.iter() {
prop_assert!(pts.contains(pt))
}
}
}
}