rust_3d/
convex_hull_2d.rs

1/*
2Copyright 2017 Martin Buck
3
4Permission is hereby granted, free of charge, to any person obtaining a copy
5of this software and associated documentation files (the "Software"),
6to deal in the Software without restriction, including without limitation the
7rights to use, copy, modify, merge, publish, distribute, sublicense,
8and/or sell copies of the Software, and to permit persons to whom the Software
9is furnished to do so, subject to the following conditions:
10
11The above copyright notice and this permission notice shall
12be included all copies or substantial portions of the Software.
13
14THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
17IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
18DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
19TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE
20OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
21*/
22
23//! Convex hull algorithm returning a Vec of the hull where the points are ordered according to the hull
24//! Using Andrew's monotone chain convex hull algorithm https://en.wikibooks.org/wiki/Algorithm_Implementation/Geometry/Convex_hull/Monotone_chain
25
26use crate::*;
27
28//------------------------------------------------------------------------------
29
30/// Convex hull algorithm returning a Vec of the hull where the points are ordered according to the hull
31/// Using Andrew's monotone chain convex hull algorithm https://en.wikibooks.org/wiki/Algorithm_Implementation/Geometry/Convex_hull/Monotone_chain
32pub fn convex_hull_2d<RA, P>(ra: &RA) -> Vec<P>
33where
34    RA: IsRandomAccessible<P>,
35    P: IsBuildable2D + Clone,
36{
37    let n = ra.len();
38
39    let mut sorted = PointCloud2D::new();
40    sorted.append_ra(ra);
41    sorted.sort_x();
42    let sorted = sorted;
43
44    let mut lower = Vec::<P>::new();
45    for i in 0..n {
46        while lower.len() >= 2
47            && ccw(&lower[lower.len() - 2], &lower[lower.len() - 1], &sorted[i]) <= 0.0
48        {
49            lower.pop().unwrap(); //safe, since only called if len > 0
50        }
51        lower.push(sorted[i].clone());
52    }
53
54    let mut upper = Vec::<P>::new();
55    for i in (0..n).rev() {
56        while upper.len() >= 2
57            && ccw(&upper[upper.len() - 2], &upper[upper.len() - 1], &sorted[i]) <= 0.0
58        {
59            upper.pop().unwrap(); //safe, since only called if len > 0
60        }
61        upper.push(sorted[i].clone());
62    }
63
64    if lower.len() > 0 {
65        lower.pop().unwrap();
66    } //safe, since len > 0
67    if upper.len() > 0 {
68        upper.pop().unwrap();
69    } //safe, since len > 0
70
71    let mut result = Vec::<P>::new();
72    result.extend(lower);
73    result.extend(upper);
74    result
75}
76
77//------------------------------------------------------------------------------
78
79fn ccw<P1, P2, P3>(p1: &P1, p2: &P2, p3: &P3) -> f64
80where
81    P1: Is2D,
82    P2: Is2D,
83    P3: Is2D,
84{
85    (p2.x() - p1.x()) * (p3.y() - p1.y()) - (p2.y() - p1.y()) * (p3.x() - p1.x())
86}