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
use std::slice::SliceIndex;
use record::{Point, PointM, PointZ};
pub trait HasXY {
fn x(&self) -> f64;
fn y(&self) -> f64;
}
pub(crate) trait HasMutXY {
fn x_mut(&mut self) -> &mut f64;
fn y_mut(&mut self) -> &mut f64;
}
pub(crate) trait HasM {
fn m(&self) -> f64;
fn m_mut(&mut self) -> &mut f64;
}
pub trait MultipointShape<PointType> {
fn point<I: SliceIndex<[PointType]>>(
&self,
index: I,
) -> Option<&<I as SliceIndex<[PointType]>>::Output>;
fn points(&self) -> &[PointType];
}
pub trait MultipartShape<PointType>: MultipointShape<PointType> {
fn parts_indices(&self) -> &[i32];
fn part(&self, index: usize) -> Option<&[PointType]> {
let parts = self.parts_indices();
if parts.len() < 2 {
Some(self.points())
} else {
let first_index = *parts.get(index)? as usize;
let last_index = if index == parts.len() - 1 {
self.points().len()
} else {
*parts.get(index + 1)? as usize
};
self.points().get(first_index..last_index)
}
}
fn parts(&self) -> PartIterator<PointType, Self> {
PartIterator {
phantom: std::marker::PhantomData,
shape: &self,
current_part: 0,
}
}
}
pub struct PartIterator<'a, PointType, Shape: 'a + MultipartShape<PointType> + ?Sized> {
phantom: std::marker::PhantomData<PointType>,
shape: &'a Shape,
current_part: usize,
}
impl<'a, PointType, Shape> Iterator for PartIterator<'a, PointType, Shape>
where
PointType: 'a,
Shape: 'a + MultipartShape<PointType>,
{
type Item = &'a [PointType];
fn next(&mut self) -> Option<Self::Item> {
if self.current_part >= self.shape.parts_indices().len() {
None
} else {
self.current_part += 1;
self.shape.part(self.current_part - 1)
}
}
}
macro_rules! impl_has_xy_for {
($PointType:ty) => {
impl HasXY for $PointType {
fn x(&self) -> f64 {
self.x
}
fn y(&self) -> f64 {
self.y
}
}
};
}
macro_rules! impl_has_mut_xy_for {
($PointType:ty) => {
impl HasMutXY for $PointType {
fn x_mut(&mut self) -> &mut f64 {
&mut self.x
}
fn y_mut(&mut self) -> &mut f64 {
&mut self.y
}
}
};
}
macro_rules! impl_has_m_for {
($PointType:ty) => {
impl HasM for $PointType {
fn m(&self) -> f64 {
self.m
}
fn m_mut(&mut self) -> &mut f64 {
&mut self.m
}
}
};
}
impl_has_xy_for!(Point);
impl_has_xy_for!(PointM);
impl_has_xy_for!(PointZ);
impl_has_mut_xy_for!(Point);
impl_has_mut_xy_for!(PointM);
impl_has_mut_xy_for!(PointZ);
impl_has_m_for!(PointM);
impl_has_m_for!(PointZ);