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
use crate::{
    CoordFloat, EuclideanLength, Line, LineInterpolatePoint, LineString, MultiLineString,
    MultiPolygon, Point, Polygon, Rect, Triangle,
};

/// Return a new linear geometry containing both existing and new interpolated coordinates with
/// a maximum distance of `max_distance` between them.
///
/// Note: `max_distance` must be greater than 0.
///
/// # Examples
/// ```
/// use geo::{coord, Line, LineString};
/// use geo::Densify;
///
/// let line: Line<f64> = Line::new(coord! {x: 0.0, y: 6.0}, coord! {x: 1.0, y: 8.0});
/// let correct: LineString<f64> = vec![[0.0, 6.0], [0.5, 7.0], [1.0, 8.0]].into();
/// let max_dist = 2.0;
/// let densified = line.densify(max_dist);
/// assert_eq!(densified, correct);
///```
pub trait Densify<F: CoordFloat> {
    type Output;

    fn densify(&self, max_distance: F) -> Self::Output;
}

// Helper for densification trait
fn densify_line<T: CoordFloat>(line: Line<T>, container: &mut Vec<Point<T>>, max_distance: T) {
    assert!(max_distance > T::zero());
    container.push(line.start_point());
    let num_segments = (line.euclidean_length() / max_distance)
        .ceil()
        .to_u64()
        .unwrap();
    // distance "unit" for this line segment
    let frac = T::one() / T::from(num_segments).unwrap();
    for segment_idx in 1..num_segments {
        let ratio = frac * T::from(segment_idx).unwrap();
        let interpolated_point = line
            .line_interpolate_point(ratio)
            .expect("ratio should be between 0..1");
        container.push(interpolated_point);
    }
}

impl<T> Densify<T> for MultiPolygon<T>
where
    T: CoordFloat,
    Line<T>: EuclideanLength<T>,
    LineString<T>: EuclideanLength<T>,
{
    type Output = MultiPolygon<T>;

    fn densify(&self, max_distance: T) -> Self::Output {
        MultiPolygon::new(
            self.iter()
                .map(|polygon| polygon.densify(max_distance))
                .collect(),
        )
    }
}

impl<T> Densify<T> for Polygon<T>
where
    T: CoordFloat,
    Line<T>: EuclideanLength<T>,
    LineString<T>: EuclideanLength<T>,
{
    type Output = Polygon<T>;

    fn densify(&self, max_distance: T) -> Self::Output {
        let densified_exterior = self.exterior().densify(max_distance);
        let densified_interiors = self
            .interiors()
            .iter()
            .map(|ring| ring.densify(max_distance))
            .collect();
        Polygon::new(densified_exterior, densified_interiors)
    }
}

impl<T> Densify<T> for MultiLineString<T>
where
    T: CoordFloat,
    Line<T>: EuclideanLength<T>,
    LineString<T>: EuclideanLength<T>,
{
    type Output = MultiLineString<T>;

    fn densify(&self, max_distance: T) -> Self::Output {
        MultiLineString::new(
            self.iter()
                .map(|linestring| linestring.densify(max_distance))
                .collect(),
        )
    }
}

impl<T> Densify<T> for LineString<T>
where
    T: CoordFloat,
    Line<T>: EuclideanLength<T>,
    LineString<T>: EuclideanLength<T>,
{
    type Output = LineString<T>;

    fn densify(&self, max_distance: T) -> Self::Output {
        if self.0.is_empty() {
            return LineString::new(vec![]);
        }

        let mut new_line = vec![];

        self.lines()
            .for_each(|line| densify_line(line, &mut new_line, max_distance));
        // we're done, push the last coordinate on to finish
        new_line.push(self.points().last().unwrap());
        LineString::from(new_line)
    }
}

impl<T> Densify<T> for Line<T>
where
    T: CoordFloat,
    Line<T>: EuclideanLength<T>,
    LineString<T>: EuclideanLength<T>,
{
    type Output = LineString<T>;

    fn densify(&self, max_distance: T) -> Self::Output {
        let mut new_line = vec![];
        densify_line(*self, &mut new_line, max_distance);
        // we're done, push the last coordinate on to finish
        new_line.push(self.end_point());
        LineString::from(new_line)
    }
}

impl<T> Densify<T> for Triangle<T>
where
    T: CoordFloat,
    Line<T>: EuclideanLength<T>,
    LineString<T>: EuclideanLength<T>,
{
    type Output = Polygon<T>;

    fn densify(&self, max_distance: T) -> Self::Output {
        self.to_polygon().densify(max_distance)
    }
}

impl<T> Densify<T> for Rect<T>
where
    T: CoordFloat,
    Line<T>: EuclideanLength<T>,
    LineString<T>: EuclideanLength<T>,
{
    type Output = Polygon<T>;

    fn densify(&self, max_distance: T) -> Self::Output {
        self.to_polygon().densify(max_distance)
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::{coord, Coord};

    #[test]
    fn test_polygon_densify() {
        let linestring: LineString<f64> =
            vec![[-5.0, 0.0], [0.0, 5.0], [5.0, 0.0], [-5.0, 0.0]].into();
        let interior: LineString<f64> =
            vec![[-3.0, 0.0], [0.0, 3.0], [3.0, 0.0], [-3.0, 0.0]].into();
        let polygon = Polygon::new(linestring, vec![interior]);
        let correct_ext: LineString<f64> = LineString(vec![
            Coord { x: -5.0, y: 0.0 },
            Coord { x: -3.75, y: 1.25 },
            Coord { x: -2.5, y: 2.5 },
            Coord { x: -1.25, y: 3.75 },
            Coord { x: 0.0, y: 5.0 },
            Coord { x: 1.25, y: 3.75 },
            Coord { x: 2.5, y: 2.5 },
            Coord { x: 3.75, y: 1.25 },
            Coord { x: 5.0, y: 0.0 },
            Coord { x: 3.0, y: 0.0 },
            Coord { x: 1.0, y: 0.0 },
            Coord {
                x: -1.0000000000000009,
                y: 0.0,
            },
            Coord { x: -3.0, y: 0.0 },
            Coord { x: -5.0, y: 0.0 },
        ]);
        let correct_int: LineString<f64> = LineString(vec![
            Coord { x: -3.0, y: 0.0 },
            Coord { x: -2.0, y: 1.0 },
            Coord { x: -1.0, y: 2.0 },
            Coord { x: 0.0, y: 3.0 },
            Coord { x: 1.0, y: 2.0 },
            Coord { x: 2.0, y: 1.0 },
            Coord { x: 3.0, y: 0.0 },
            Coord { x: 1.0, y: 0.0 },
            Coord { x: -1.0, y: 0.0 },
            Coord { x: -3.0, y: 0.0 },
        ]);
        let correct_polygon = Polygon::new(correct_ext, vec![correct_int]);
        let max_dist = 2.0;
        let densified = polygon.densify(max_dist);
        assert_eq!(densified, correct_polygon);
    }

    #[test]
    fn test_empty_linestring_densify() {
        let linestring = LineString::<f64>::new(vec![]);
        let max_dist = 2.0;
        let densified = linestring.densify(max_dist);
        assert!(densified.0.is_empty());
    }

    #[test]
    fn test_linestring_densify() {
        let linestring: LineString<f64> =
            vec![[-1.0, 0.0], [0.0, 0.0], [0.0, 6.0], [1.0, 8.0]].into();
        let correct: LineString<f64> = vec![
            [-1.0, 0.0],
            [0.0, 0.0],
            [0.0, 2.0],
            [0.0, 4.0],
            [0.0, 6.0],
            [0.5, 7.0],
            [1.0, 8.0],
        ]
        .into();
        let max_dist = 2.0;
        let densified = linestring.densify(max_dist);
        assert_eq!(densified, correct);
    }

    #[test]
    fn test_line_densify() {
        let line: Line<f64> = Line::new(coord! {x: 0.0, y: 6.0}, coord! {x: 1.0, y: 8.0});
        let correct: LineString<f64> = vec![[0.0, 6.0], [0.5, 7.0], [1.0, 8.0]].into();
        let max_dist = 2.0;
        let densified = line.densify(max_dist);
        assert_eq!(densified, correct);
    }
}