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
use crate::CoordinateType;
use crate::point::Point;
use crate::polygon::Polygon;
pub use crate::traits::{DoubledOrientedArea, BoundingBox, MapPointwise, WindingNumber};
use std::iter::FromIterator;
use crate::traits::{TryBoundingBox, TryIntoBoundingBox};
use crate::prelude::Rect;
#[derive(Default, Clone, Debug, Hash)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
pub struct MultiPolygon<T> {
pub polygons: Vec<Polygon<T>>
}
impl<T> MultiPolygon<T> {
pub fn new() -> Self {
Self {
polygons: vec![]
}
}
pub fn from_polygons(polygons: Vec<Polygon<T>>) -> Self {
MultiPolygon {
polygons
}
}
pub fn len(&self) -> usize {
self.polygons.len()
}
pub fn insert(&mut self, polygon: Polygon<T>) {
self.polygons.push(polygon)
}
}
impl<T> WindingNumber<T> for MultiPolygon<T>
where T: CoordinateType {
fn winding_number(&self, point: Point<T>) -> isize {
self.polygons.iter()
.map(|p| p.winding_number(point))
.sum()
}
}
impl<T> MapPointwise<T> for MultiPolygon<T>
where T: CoordinateType {
fn transform<F: Fn(Point<T>) -> Point<T>>(&self, tf: F) -> Self {
MultiPolygon::from_polygons(
self.polygons.iter()
.map(|p| p.transform(&tf))
.collect()
)
}
}
impl<T, IP: Into<Polygon<T>>> From<IP> for MultiPolygon<T> {
fn from(x: IP) -> Self {
MultiPolygon::from_polygons(vec![x.into()])
}
}
impl<T> From<Vec<Polygon<T>>> for MultiPolygon<T> {
fn from(polygons: Vec<Polygon<T>>) -> Self {
MultiPolygon {
polygons
}
}
}
impl<T, IP: Into<Polygon<T>>> FromIterator<IP> for MultiPolygon<T> {
fn from_iter<I: IntoIterator<Item=IP>>(iter: I) -> Self {
MultiPolygon::from_polygons(
iter.into_iter()
.map(|p| p.into()).collect()
)
}
}
impl<T> IntoIterator for MultiPolygon<T> {
type Item = Polygon<T>;
type IntoIter = ::std::vec::IntoIter<Polygon<T>>;
fn into_iter(self) -> Self::IntoIter {
self.polygons.into_iter()
}
}
impl<T: CoordinateType> TryBoundingBox<T> for MultiPolygon<T> {
fn try_bounding_box(&self) -> Option<Rect<T>> {
self.polygons.iter() .try_into_bounding_box()
}
}