|
1 |
| -use crate::{Coordinate, GeoNum, LineString, MultiLineString, MultiPoint, MultiPolygon, Polygon}; |
| 1 | +use crate::geometry::{Coordinate, LineString, Polygon}; |
| 2 | +use crate::GeoNum; |
2 | 3 |
|
3 | 4 | /// Returns the convex hull of a Polygon. The hull is always oriented counter-clockwise.
|
4 | 5 | ///
|
@@ -36,65 +37,23 @@ use crate::{Coordinate, GeoNum, LineString, MultiLineString, MultiPoint, MultiPo
|
36 | 37 | /// let res = poly.convex_hull();
|
37 | 38 | /// assert_eq!(res.exterior(), &correct_hull);
|
38 | 39 | /// ```
|
39 |
| -pub trait ConvexHull { |
| 40 | +pub trait ConvexHull<'a, T> { |
40 | 41 | type Scalar: GeoNum;
|
41 |
| - fn convex_hull(&self) -> Polygon<Self::Scalar>; |
| 42 | + fn convex_hull(&'a self) -> Polygon<Self::Scalar>; |
42 | 43 | }
|
43 | 44 |
|
44 |
| -impl<T> ConvexHull for Polygon<T> |
45 |
| -where |
46 |
| - T: GeoNum, |
47 |
| -{ |
48 |
| - type Scalar = T; |
49 |
| - fn convex_hull(&self) -> Polygon<T> { |
50 |
| - Polygon::new(quick_hull(&mut self.exterior().0.clone()), vec![]) |
51 |
| - } |
52 |
| -} |
53 |
| - |
54 |
| -impl<T> ConvexHull for MultiPolygon<T> |
55 |
| -where |
56 |
| - T: GeoNum, |
57 |
| -{ |
58 |
| - type Scalar = T; |
59 |
| - fn convex_hull(&self) -> Polygon<T> { |
60 |
| - let mut aggregated: Vec<_> = self |
61 |
| - .0 |
62 |
| - .iter() |
63 |
| - .flat_map(|elem| elem.exterior().0.iter().copied()) |
64 |
| - .collect(); |
65 |
| - Polygon::new(quick_hull(&mut aggregated), vec![]) |
66 |
| - } |
67 |
| -} |
| 45 | +use crate::algorithm::CoordsIter; |
68 | 46 |
|
69 |
| -impl<T> ConvexHull for LineString<T> |
| 47 | +impl<'a, T, G> ConvexHull<'a, T> for G |
70 | 48 | where
|
71 | 49 | T: GeoNum,
|
| 50 | + G: CoordsIter<'a, Scalar = T>, |
72 | 51 | {
|
73 | 52 | type Scalar = T;
|
74 |
| - fn convex_hull(&self) -> Polygon<T> { |
75 |
| - Polygon::new(quick_hull(&mut self.0.clone()), vec![]) |
76 |
| - } |
77 |
| -} |
78 | 53 |
|
79 |
| -impl<T> ConvexHull for MultiLineString<T> |
80 |
| -where |
81 |
| - T: GeoNum, |
82 |
| -{ |
83 |
| - type Scalar = T; |
84 |
| - fn convex_hull(&self) -> Polygon<T> { |
85 |
| - let mut aggregated: Vec<_> = self.iter().flat_map(|elem| elem.clone().0).collect(); |
86 |
| - Polygon::new(quick_hull(&mut aggregated), vec![]) |
87 |
| - } |
88 |
| -} |
89 |
| - |
90 |
| -impl<T> ConvexHull for MultiPoint<T> |
91 |
| -where |
92 |
| - T: GeoNum, |
93 |
| -{ |
94 |
| - type Scalar = T; |
95 |
| - fn convex_hull(&self) -> Polygon<T> { |
96 |
| - let mut aggregated: Vec<_> = self.iter().map(|p| p.0).collect(); |
97 |
| - Polygon::new(quick_hull(&mut aggregated), vec![]) |
| 54 | + fn convex_hull(&'a self) -> Polygon<T> { |
| 55 | + let mut exterior: Vec<_> = self.exterior_coords_iter().collect(); |
| 56 | + Polygon::new(quick_hull(&mut exterior), vec![]) |
98 | 57 | }
|
99 | 58 | }
|
100 | 59 |
|
|
0 commit comments