Modern Apps git
git clone https://git.maproj.org/Modern-Apps.git
translate ▾

Modern-Apps.git / scripts / maps / tile_build / tests / simplify_extra.rs

simplify_extra.rs341 lines · 15.0 KiBRaw
1#[cfg(test)]
2mod tests {
3 use tile_build::geom::{Geometry, Pt, SigPt, Vertex, ALWAYS};
4 use tile_build::simplify::*;
5
6 /// Annotate a path and hand back the vertices a tolerance keeps, as plain pairs.
7 fn thin(pts: &[Pt], tolerance: f64) -> Vec<Pt> {
8 let mut g = line_of(pts);
9 annotate(&mut g);
10 let Geometry::Lines(out) = filter(&g, tolerance) else { panic!() };
11 match out.first() {
12 Some(l) => l.iter().map(|v| v.xy()).collect(),
13 None => Vec::new(),
14 }
15 }
16
17 fn line_of(pts: &[Pt]) -> Geometry<SigPt> {
18 Geometry::Lines(vec![pts.iter().map(|&(x, y)| SigPt::new(x, y)).collect()])
19 }
20
21 fn ring_of(pts: &[Pt]) -> Vec<SigPt> {
22 pts.iter().map(|&(x, y)| SigPt::new(x, y)).collect()
23 }
24
25 /// Annotate one ring on its own and filter it, as the polygon path would.
26 fn thin_ring(pts: &[Pt], tolerance: f64) -> Option<Vec<Pt>> {
27 let mut ring = ring_of(pts);
28 annotate_path(&mut ring);
29 filter_ring(&ring, tolerance * tolerance).map(|r| r.iter().map(|v| v.xy()).collect())
30 }
31
32 #[test]
33 fn a_collinear_run_collapses_to_its_endpoints() {
34 let line = [(0.0, 0.0), (10.0, 0.0), (20.0, 0.0), (30.0, 0.0), (40.0, 0.0)];
35 assert_eq!(thin(&line, 1.0), vec![(0.0, 0.0), (40.0, 0.0)]);
36 }
37
38 #[test]
39 fn a_deviation_above_tolerance_is_kept_and_below_is_dropped() {
40 // The middle vertex sits 5 units off the chord.
41 let line = [(0.0, 0.0), (50.0, 5.0), (100.0, 0.0)];
42 assert_eq!(thin(&line, 1.0), line.to_vec(), "5 > 1, so it stays");
43 assert_eq!(thin(&line, 10.0), vec![(0.0, 0.0), (100.0, 0.0)], "5 < 10, so it goes");
44 }
45
46 #[test]
47 fn endpoints_are_never_removed() {
48 // Even a line whose interior is entirely within tolerance keeps its ends.
49 let line = [(0.0, 0.0), (1.0, 0.0), (2.0, 0.0), (100.0, 0.0)];
50 let out = thin(&line, 50.0);
51 assert_eq!(out.first(), Some(&(0.0, 0.0)));
52 assert_eq!(out.last(), Some(&(100.0, 0.0)));
53 assert_eq!(out.len(), 2);
54 }
55
56 #[test]
57 fn thinning_is_monotonic_in_the_tolerance() {
58 // A zigzag of decreasing amplitude: a larger tolerance must never keep more
59 // vertices than a smaller one.
60 let line: Vec<Pt> = (0..40)
61 .map(|i| {
62 let a = (40 - i) as f64;
63 ((i * 10) as f64, if i % 2 == 0 { a } else { -a })
64 })
65 .collect();
66 let mut prev = usize::MAX;
67 for tol in [0.5, 1.0, 2.0, 4.0, 8.0, 16.0, 32.0, 64.0] {
68 let n = thin(&line, tol).len();
69 assert!(n <= prev, "tolerance {tol} kept {n} vertices, more than the {prev} before it");
70 prev = n;
71 }
72 // And the extremes behave.
73 assert_eq!(thin(&line, 0.0), line, "no tolerance, no change");
74 assert_eq!(thin(&line, 1e9).len(), 2, "everything but the ends");
75 }
76
77 #[test]
78 fn thinning_never_adds_a_vertex_or_reorders_them() {
79 let line: Vec<Pt> = (0..50).map(|i| ((i * 7) as f64, ((i * i) % 23) as f64)).collect();
80 for tol in [1.0, 3.0, 9.0] {
81 let out = thin(&line, tol);
82 assert!(out.len() <= line.len());
83 // The output is a subsequence of the input.
84 let mut it = line.iter();
85 for p in &out {
86 assert!(it.any(|q| q == p), "{p:?} is not in the input, or is out of order");
87 }
88 }
89 }
90
91 /// The property the whole design exists for. Two rings that share an edge --
92 /// which is what a clip against a tile boundary produces -- must agree on it
93 /// vertex for vertex after filtering, whatever else happens to either ring.
94 #[test]
95 fn a_vertex_shared_between_two_rings_is_kept_or_dropped_in_both() {
96 use tile_build::clip::clip_polygon;
97 use tile_build::geom::Rect;
98
99 let rect = Rect { min_x: 0.0, min_y: 0.0, max_x: 100.0, max_y: 100.0 };
100 // An exterior and a hole that both hang out over the eastern boundary, so
101 // the clip gives them a shared run of vertices along x = 100.
102 let exterior: Vec<Pt> =
103 vec![(10.0, 10.0), (150.0, 10.0), (150.0, 90.0), (10.0, 90.0), (10.0, 10.0)];
104 let hole: Vec<Pt> =
105 vec![(40.0, 30.0), (150.0, 30.0), (150.0, 70.0), (40.0, 70.0), (40.0, 30.0)];
106 let mut g = Geometry::Polygons(vec![vec![ring_of(&exterior), ring_of(&hole)]]);
107 annotate(&mut g);
108 let Geometry::Polygons(polys) = &g else { panic!() };
109 let clipped = clip_polygon(&polys[0], &rect);
110 assert_eq!(clipped.len(), 2, "exterior and hole both survive the clip");
111
112 // Every vertex the clip put on the eastern boundary appears in both rings.
113 let on_edge = |rings: &Vec<Vec<SigPt>>| -> Vec<Pt> {
114 rings.iter().flatten().map(|v| v.xy()).filter(|(x, _)| *x == 100.0).collect()
115 };
116 for tol in [0.5, 1.0, 8.0, 64.0] {
117 let g = Geometry::Polygons(vec![clipped.clone()]);
118 let Geometry::Polygons(out) = filter(&g, tol) else { panic!() };
119 let kept = on_edge(&out[0]);
120 let before = on_edge(&vec![clipped[0].clone(), clipped[1].clone()]);
121 assert_eq!(
122 kept, before,
123 "tolerance {tol} moved a boundary vertex: {kept:?} vs {before:?}"
124 );
125 }
126 }
127
128 /// Clip-introduced vertices carry `ALWAYS`, so no tolerance can remove them --
129 /// which is the only thing holding two clipped rings to the same boundary edge.
130 #[test]
131 fn boundary_vertices_survive_the_coarsest_tolerance() {
132 use tile_build::clip::clip_ring;
133 use tile_build::geom::Rect;
134
135 let rect = Rect { min_x: 0.0, min_y: 0.0, max_x: 10.0, max_y: 10.0 };
136 let mut ring =
137 ring_of(&[(-5.0, -5.0), (15.0, -5.0), (15.0, 15.0), (-5.0, 15.0), (-5.0, -5.0)]);
138 annotate_path(&mut ring);
139 let clipped = clip_ring(&ring, &rect);
140 // Every surviving vertex is a crossing the clipper invented.
141 assert!(clipped.iter().all(|v| v.sig == ALWAYS), "{clipped:?}");
142 let out = filter_ring(&clipped, 1e18).expect("nothing to drop");
143 assert_eq!(out.len(), clipped.len());
144 }
145
146 #[test]
147 fn a_closed_ring_stays_closed() {
148 // A square with a redundant midpoint on each side.
149 let ring = [
150 (0.0, 0.0),
151 (50.0, 0.0),
152 (100.0, 0.0),
153 (100.0, 50.0),
154 (100.0, 100.0),
155 (50.0, 100.0),
156 (0.0, 100.0),
157 (0.0, 50.0),
158 (0.0, 0.0),
159 ];
160 let out = thin_ring(&ring, 1.0).expect("a square survives");
161 assert_eq!(out.first(), out.last(), "closure preserved");
162 assert_eq!(out.len(), 5, "four corners plus the close: {out:?}");
163 }
164
165 #[test]
166 fn an_unclosed_ring_stays_unclosed() {
167 let ring = [(0.0, 0.0), (50.0, 0.0), (100.0, 0.0), (100.0, 100.0), (0.0, 100.0)];
168 let out = thin_ring(&ring, 1.0).unwrap();
169 assert_ne!(out.first(), out.last());
170 assert_eq!(out.len(), 4, "the collinear midpoint goes: {out:?}");
171 }
172
173 #[test]
174 fn a_ring_is_dropped_rather_than_collapsed_below_four_vertices() {
175 // A very thin sliver: at a large tolerance every vertex but the two ends is
176 // sub-tolerance, which leaves 2 and encloses no area. Drop it instead.
177 let ring = [(0.0, 0.0), (500.0, 1.0), (1000.0, 0.0), (500.0, -1.0), (0.0, 0.0)];
178 assert_eq!(thin_ring(&ring, 1000.0), None, "a collapsed ring is dropped");
179 // At a tolerance it can actually survive, it does.
180 assert!(thin_ring(&ring, 0.5).is_some());
181 }
182
183 #[test]
184 fn a_ring_that_is_already_too_small_is_rejected() {
185 assert_eq!(thin_ring(&[], 1.0), None);
186 assert_eq!(thin_ring(&[(0.0, 0.0)], 1.0), None);
187 assert_eq!(thin_ring(&[(0.0, 0.0), (1.0, 1.0)], 1.0), None);
188 // Closed, but only two distinct vertices.
189 assert_eq!(thin_ring(&[(0.0, 0.0), (1.0, 1.0), (0.0, 0.0)], 1.0), None);
190 // The minimum viable closed ring, at zero tolerance.
191 let tri = [(0.0, 0.0), (10.0, 0.0), (0.0, 10.0), (0.0, 0.0)];
192 assert!(thin_ring(&tri, 0.0).is_some());
193 }
194
195 #[test]
196 fn a_ring_at_the_floor_is_dropped_only_when_it_is_below_tolerance() {
197 // Four vertices is the floor, but filtering can still remove the two
198 // interior ones: whether the ring survives is a question about its size,
199 // not its vertex count.
200 let ring = [(0.0, 0.0), (1000.0, 0.0), (0.0, 1000.0), (0.0, 0.0)];
201 assert_eq!(
202 thin_ring(&ring, 1.0),
203 Some(ring.to_vec()),
204 "1000 units across, 1 unit tolerance"
205 );
206 assert_eq!(thin_ring(&ring, 1e9), None, "sub-tolerance, so dropped not collapsed");
207 }
208
209 #[test]
210 fn the_geometry_entry_point_drops_lines_that_fall_below_two_vertices() {
211 let mut g = Geometry::Lines(vec![
212 ring_of(&[(0.0, 0.0), (10.0, 0.0), (20.0, 0.0)]),
213 ring_of(&[(5.0, 5.0)]),
214 ]);
215 annotate(&mut g);
216 let Geometry::Lines(out) = filter(&g, 1.0) else { panic!() };
217 assert_eq!(out.len(), 1);
218 assert_eq!(
219 out[0].iter().map(|v| v.xy()).collect::<Vec<_>>(),
220 vec![(0.0, 0.0), (20.0, 0.0)]
221 );
222 }
223
224 #[test]
225 fn losing_the_exterior_ring_drops_the_whole_polygon() {
226 // A 1000x2 sliver and a 100000-unit square, at a 1000-unit tolerance: the
227 // sliver is entirely sub-tolerance and the square is nowhere near it.
228 let sliver = [(0.0, 0.0), (500.0, 1.0), (1000.0, 0.0), (500.0, -1.0), (0.0, 0.0)];
229 let fat =
230 [(0.0, 0.0), (100_000.0, 0.0), (100_000.0, 100_000.0), (0.0, 100_000.0), (0.0, 0.0)];
231 // Exterior collapses: the polygon goes, hole and all.
232 let mut g = Geometry::Polygons(vec![vec![ring_of(&sliver), ring_of(&fat)]]);
233 annotate(&mut g);
234 let Geometry::Polygons(out) = filter(&g, 1000.0) else { panic!() };
235 assert!(out.is_empty(), "{out:?}");
236 // A hole collapsing leaves the exterior intact.
237 let mut g = Geometry::Polygons(vec![vec![ring_of(&fat), ring_of(&sliver)]]);
238 annotate(&mut g);
239 let Geometry::Polygons(out) = filter(&g, 1000.0) else { panic!() };
240 assert_eq!(out.len(), 1);
241 assert_eq!(out[0].len(), 1, "the hole was dropped, the exterior kept");
242 }
243
244 #[test]
245 fn points_pass_through_untouched() {
246 // Thinning points is the drop policy's job, not simplification's.
247 let mut g = Geometry::Points(ring_of(&[(0.0, 0.0), (1.0, 1.0), (2.0, 2.0)]));
248 annotate(&mut g);
249 assert_eq!(filter(&g, 1e9), g);
250 }
251
252 #[test]
253 fn a_zero_tolerance_is_the_identity() {
254 let mut g = line_of(&[(0.0, 0.0), (1.0, 0.0), (2.0, 0.0), (3.0, 0.0)]);
255 annotate(&mut g);
256 assert_eq!(filter(&g, 0.0), g);
257 assert_eq!(filter(&g, -1.0), g, "a negative tolerance is not a licence");
258 }
259
260 #[test]
261 fn the_tolerance_policy_spares_the_deepest_zoom() {
262 assert_eq!(tolerance_for(14, 14, 1.0), 0.0, "max zoom keeps full detail");
263 assert_eq!(tolerance_for(15, 14, 1.0), 0.0, "and so does anything past it");
264 assert_eq!(tolerance_for(13, 14, 1.0), DEFAULT_TOLERANCE);
265 assert_eq!(tolerance_for(6, 14, 4.0), DEFAULT_TOLERANCE * 4.0);
266 }
267
268 #[test]
269 fn the_distance_is_to_the_segment_not_the_line_it_lies_on() {
270 let p = |x: f64, y: f64| SigPt::new(x, y);
271 // Beyond `b`: the perpendicular to the infinite line would say 0.
272 assert_eq!(seg_dist_sq(p(20.0, 0.0), p(0.0, 0.0), p(10.0, 0.0)), 100.0);
273 assert_eq!(seg_dist_sq(p(-5.0, 0.0), p(0.0, 0.0), p(10.0, 0.0)), 25.0);
274 // Between them, it is the perpendicular.
275 assert_eq!(seg_dist_sq(p(5.0, 3.0), p(0.0, 0.0), p(10.0, 0.0)), 9.0);
276 assert_eq!(seg_dist_sq(p(5.0, 0.0), p(0.0, 0.0), p(10.0, 0.0)), 0.0);
277 // A degenerate chord, which is what a closed ring's first pass measures
278 // against, is point-to-point.
279 assert_eq!(seg_dist_sq(p(5.0, 5.0), p(0.0, 0.0), p(0.0, 0.0)), 50.0);
280 }
281
282 #[test]
283 fn significance_is_scale_invariant() {
284 // The same shape at two scales must rank its vertices the same way, which is
285 // what lets one annotation pass serve every zoom.
286 let shape: Vec<Pt> =
287 vec![(0.0, 0.0), (10.0, 3.0), (20.0, 1.0), (30.0, 12.0), (40.0, 2.0), (50.0, 0.0)];
288 let mut small = line_of(&shape);
289 let mut big = line_of(&shape.iter().map(|(x, y)| (x * 8.0, y * 8.0)).collect::<Vec<_>>());
290 annotate(&mut small);
291 annotate(&mut big);
292 let sig = |g: &Geometry<SigPt>| -> Vec<f64> {
293 let Geometry::Lines(l) = g else { panic!() };
294 l[0].iter().map(|v| v.sig).collect()
295 };
296 for (a, b) in sig(&small).iter().zip(sig(&big)) {
297 if a.is_finite() {
298 assert!((b - a * 64.0).abs() < 1e-9, "{a} scaled to {b}, not {}", a * 64.0);
299 } else {
300 assert!(b.is_infinite());
301 }
302 }
303 // And a threshold scaled with them keeps the same vertices.
304 let kept = |g: &Geometry<SigPt>, tol: f64| -> usize {
305 let Geometry::Lines(l) = filter(g, tol) else { panic!() };
306 l[0].len()
307 };
308 for tol in [0.5, 1.0, 2.0, 4.0] {
309 assert_eq!(kept(&small, tol), kept(&big, tol * 8.0), "at tolerance {tol}");
310 }
311 }
312
313 /// A coincident shore and its water edge must thin to the SAME vertices, or the
314 /// two drift apart and the renderer paints earth over sea between them. Both rings
315 /// carry the same boundary crossings (ALWAYS), which the filter must respect.
316 #[test]
317 fn coincident_rings_thin_to_the_same_vertices() {
318 use tile_build::clip::clip_ring;
319 use tile_build::geom::Rect;
320 // A land edge and a water edge sharing the segment x=0..20 at y=0.
321 let rect = Rect { min_x: 0.0, min_y: -10.0, max_x: 20.0, max_y: 10.0 };
322 let land = [(0.0, -10.0), (20.0, -10.0), (20.0, 0.0), (0.0, 0.0), (0.0, -10.0)];
323 let water = [(0.0, 10.0), (20.0, 10.0), (20.0, 0.0), (0.0, 0.0), (0.0, 10.0)];
324 let land_ring: Vec<SigPt> =
325 clip_ring(&land.iter().map(|&(x, y)| SigPt::new(x, y)).collect::<Vec<_>>(), &rect);
326 let water_ring: Vec<SigPt> =
327 clip_ring(&water.iter().map(|&(x, y)| SigPt::new(x, y)).collect::<Vec<_>>(), &rect);
328 let mut land_g = Geometry::Polygons(vec![vec![land_ring]]);
329 let mut water_g = Geometry::Polygons(vec![vec![water_ring]]);
330 annotate(&mut land_g);
331 annotate(&mut water_g);
332 let Geometry::Polygons(land_out) = filter(&land_g, 5.0) else { panic!() };
333 let Geometry::Polygons(water_out) = filter(&water_g, 5.0) else { panic!() };
334 // The shared edge y=0 survives identically in both: same vertices, so no
335 // sliver opens between them at any zoom.
336 let edge_of = |rings: &[Vec<Vec<SigPt>>]| -> Vec<Pt> {
337 rings[0][0].iter().filter(|v| v.y == 0.0).map(|v| v.xy()).collect()
338 };
339 assert_eq!(edge_of(&land_out), edge_of(&water_out), "shared edge diverged");
340 }
341}