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

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

clip_part1.rs402 lines · 16.4 KiBRaw
1#[cfg(test)]
2mod tests {
3 use tile_build::clip::*; use tile_build::geom::{Pt, Rect, Geometry};
4
5 /// A 10x10 rect at the origin, so every expected coordinate is readable.
6 fn r() -> Rect {
7 Rect { min_x: 0.0, min_y: 0.0, max_x: 10.0, max_y: 10.0 }
8 }
9
10 fn close(a: Pt, b: Pt) -> bool {
11 (a.0 - b.0).abs() < 1e-9 && (a.1 - b.1).abs() < 1e-9
12 }
13
14 fn seg(a: Pt, b: Pt) -> Option<(Pt, Pt)> {
15 clip_segment(a, b, &r())
16 }
17
18 // --- the clip-region matrix -------------------------------------------
19
20 #[test]
21 fn a_segment_wholly_inside_is_untouched() {
22 let (a, b) = ((2.0, 2.0), (8.0, 7.0));
23 let (ca, cb) = seg(a, b).unwrap();
24 assert!(close(ca, a) && close(cb, b));
25 }
26
27 #[test]
28 fn a_segment_wholly_outside_is_rejected_from_every_direction() {
29 // The eight regions around the rect, plus two that straddle an axis
30 // without ever entering (the case a naive "both endpoints outside" test
31 // gets wrong is covered separately below).
32 for (a, b) in [
33 ((-5.0, 5.0), (-1.0, 5.0)), // west
34 ((11.0, 5.0), (20.0, 5.0)), // east
35 ((5.0, -5.0), (5.0, -1.0)), // south
36 ((5.0, 11.0), (5.0, 20.0)), // north
37 ((-5.0, -5.0), (-1.0, -1.0)), // south-west
38 ((11.0, -5.0), (20.0, -1.0)), // south-east
39 ((-5.0, 11.0), (-1.0, 20.0)), // north-west
40 ((11.0, 11.0), (20.0, 20.0)), // north-east
41 // Spans the rect's x range entirely above it.
42 ((-5.0, 15.0), (15.0, 15.0)),
43 // Passes diagonally past the north-west corner without touching: it is
44 // already above the rect by the time it reaches x = 0.
45 ((-5.0, 8.0), (2.0, 15.0)),
46 ] {
47 assert!(seg(a, b).is_none(), "{a:?} -> {b:?} must miss the rect");
48 }
49 }
50
51 #[test]
52 fn a_segment_crossing_one_boundary_is_cut_at_it() {
53 // In from the west.
54 let (ca, cb) = seg((-5.0, 5.0), (5.0, 5.0)).unwrap();
55 assert!(close(ca, (0.0, 5.0)) && close(cb, (5.0, 5.0)));
56 // Out to the east.
57 let (ca, cb) = seg((5.0, 5.0), (15.0, 5.0)).unwrap();
58 assert!(close(ca, (5.0, 5.0)) && close(cb, (10.0, 5.0)));
59 // In from the south.
60 let (ca, cb) = seg((5.0, -5.0), (5.0, 5.0)).unwrap();
61 assert!(close(ca, (5.0, 0.0)) && close(cb, (5.0, 5.0)));
62 // Out to the north.
63 let (ca, cb) = seg((5.0, 5.0), (5.0, 15.0)).unwrap();
64 assert!(close(ca, (5.0, 5.0)) && close(cb, (5.0, 10.0)));
65 }
66
67 #[test]
68 fn a_segment_spanning_the_rect_is_cut_at_both_ends() {
69 let (ca, cb) = seg((-10.0, 5.0), (20.0, 5.0)).unwrap();
70 assert!(close(ca, (0.0, 5.0)) && close(cb, (10.0, 5.0)));
71 // Diagonally corner to corner, both endpoints well outside.
72 let (ca, cb) = seg((-10.0, -10.0), (20.0, 20.0)).unwrap();
73 assert!(close(ca, (0.0, 0.0)) && close(cb, (10.0, 10.0)));
74 }
75
76 #[test]
77 fn a_segment_clipping_a_corner_keeps_only_the_corner_sliver() {
78 // Passes through the north-east corner region, entering and leaving.
79 let (ca, cb) = seg((5.0, 15.0), (15.0, 5.0)).unwrap();
80 assert!(close(ca, (10.0, 10.0)), "{ca:?}");
81 assert!(close(cb, (10.0, 10.0)), "{cb:?}");
82 }
83
84 #[test]
85 fn a_segment_parallel_to_a_boundary_is_handled_without_dividing_by_zero() {
86 // Exactly along the southern edge: inside (the rect is closed).
87 let (ca, cb) = seg((-5.0, 0.0), (15.0, 0.0)).unwrap();
88 assert!(close(ca, (0.0, 0.0)) && close(cb, (10.0, 0.0)));
89 // Parallel but outside.
90 assert!(seg((-5.0, -1.0), (15.0, -1.0)).is_none());
91 // Vertical along the western edge.
92 let (ca, cb) = seg((0.0, -5.0), (0.0, 15.0)).unwrap();
93 assert!(close(ca, (0.0, 0.0)) && close(cb, (0.0, 10.0)));
94 }
95
96 #[test]
97 fn a_degenerate_segment_is_kept_only_if_inside() {
98 let (ca, cb) = seg((5.0, 5.0), (5.0, 5.0)).unwrap();
99 assert!(close(ca, (5.0, 5.0)) && close(cb, (5.0, 5.0)));
100 assert!(seg((-5.0, -5.0), (-5.0, -5.0)).is_none());
101 // Exactly on a corner counts as inside.
102 assert!(seg((0.0, 0.0), (0.0, 0.0)).is_some());
103 }
104
105 #[test]
106 fn a_non_finite_segment_is_rejected_rather_than_producing_nan() {
107 assert!(seg((f64::NAN, 5.0), (5.0, 5.0)).is_none());
108 assert!(seg((5.0, 5.0), (f64::INFINITY, 5.0)).is_none());
109 }
110
111 // --- polylines ---------------------------------------------------------
112
113 #[test]
114 fn a_polyline_inside_survives_as_one_piece() {
115 let line = vec![(1.0, 1.0), (5.0, 5.0), (9.0, 2.0)];
116 let out = clip_line(&line, &r());
117 assert_eq!(out.len(), 1);
118 assert_eq!(out[0], line);
119 }
120
121 #[test]
122 fn a_polyline_leaving_and_re_entering_yields_two_pieces() {
123 // Out to the east and back: one input line, two output lines. Welding them
124 // would draw a straight edge across ground the line never covered.
125 let line = vec![(5.0, 2.0), (15.0, 2.0), (15.0, 8.0), (5.0, 8.0)];
126 let out = clip_line(&line, &r());
127 assert_eq!(out.len(), 2, "{out:?}");
128 assert!(close(out[0][0], (5.0, 2.0)) && close(out[0][1], (10.0, 2.0)));
129 assert!(close(out[1][0], (10.0, 8.0)) && close(out[1][1], (5.0, 8.0)));
130 }
131
132 #[test]
133 fn consecutive_surviving_segments_are_welded_into_one_line() {
134 // Three segments, all inside: the output must be one 4-vertex line, not
135 // three 2-vertex ones.
136 let line = vec![(1.0, 1.0), (3.0, 3.0), (5.0, 2.0), (7.0, 6.0)];
137 let out = clip_line(&line, &r());
138 assert_eq!(out.len(), 1);
139 assert_eq!(out[0].len(), 4);
140 }
141
142 #[test]
143 fn a_polyline_wholly_outside_yields_nothing() {
144 let line = vec![(-5.0, -5.0), (-3.0, -4.0), (-1.0, -6.0)];
145 assert!(clip_line(&line, &r()).is_empty());
146 }
147
148 #[test]
149 fn a_polyline_that_only_grazes_a_corner_yields_a_degenerate_piece_or_nothing() {
150 // A single touching point cannot make a line, so it must not be emitted as
151 // a one-vertex "line" -- that encodes as a MoveTo with no LineTo.
152 let line = vec![(5.0, 15.0), (15.0, 5.0)];
153 for piece in clip_line(&line, &r()) {
154 assert!(piece.len() >= 2, "no one-vertex lines: {piece:?}");
155 }
156 }
157
158 #[test]
159 fn a_dropped_vertex_breaks_the_run_rather_than_bridging_it() {
160 let line = vec![(1.0, 1.0), (3.0, 3.0), (f64::NAN, 0.0), (7.0, 7.0), (9.0, 9.0)];
161 let out = clip_line(&line, &r());
162 assert_eq!(out.len(), 2, "{out:?}");
163 assert_eq!(out[0], vec![(1.0, 1.0), (3.0, 3.0)]);
164 assert_eq!(out[1], vec![(7.0, 7.0), (9.0, 9.0)]);
165 }
166
167 #[test]
168 fn a_single_vertex_line_yields_nothing() {
169 assert!(clip_line(&[(5.0, 5.0)], &r()).is_empty());
170 assert!(clip_line::<Pt>(&[], &r()).is_empty());
171 }
172
173 // --- polygons ----------------------------------------------------------
174
175 fn square(min: f64, max: f64) -> Vec<Pt> {
176 vec![(min, min), (max, min), (max, max), (min, max), (min, min)]
177 }
178
179 fn area(ring: &[Pt]) -> f64 {
180 let n = ring.len();
181 let mut a = 0.0;
182 for i in 0..n {
183 let (x1, y1) = ring[i];
184 let (x2, y2) = ring[(i + 1) % n];
185 a += x1 * y2 - x2 * y1;
186 }
187 (a / 2.0).abs()
188 }
189
190 #[test]
191 fn a_ring_inside_is_untouched_apart_from_its_closure_convention() {
192 let ring = square(2.0, 8.0);
193 let out = clip_ring(&ring, &r());
194 assert_eq!(area(&out), area(&ring));
195 assert_eq!(out.first(), out.last(), "a closed ring stays closed");
196 }
197
198 #[test]
199 fn an_unclosed_ring_stays_unclosed() {
200 let ring = vec![(2.0, 2.0), (8.0, 2.0), (8.0, 8.0), (2.0, 8.0)];
201 let out = clip_ring(&ring, &r());
202 assert_ne!(out.first(), out.last());
203 assert_eq!(out.len(), 4);
204 }
205
206 #[test]
207 fn a_ring_containing_the_rect_becomes_the_rect() {
208 let ring = square(-100.0, 100.0);
209 let out = clip_ring(&ring, &r());
210 assert_eq!(area(&out), 100.0, "the whole 10x10 rect: {out:?}");
211 }
212
213 #[test]
214 fn a_ring_half_outside_keeps_half_its_area() {
215 // A 10x10 square offset 5 east: exactly half of it is in the rect.
216 let ring = vec![(5.0, 0.0), (15.0, 0.0), (15.0, 10.0), (5.0, 10.0), (5.0, 0.0)];
217 let out = clip_ring(&ring, &r());
218 assert_eq!(area(&out), 50.0, "{out:?}");
219 assert_eq!(out.first(), out.last());
220 }
221
222 #[test]
223 fn a_ring_wholly_outside_disappears() {
224 assert!(clip_ring(&square(20.0, 30.0), &r()).is_empty());
225 // Touching the boundary but enclosing no area inside it.
226 assert!(clip_ring(&square(10.0, 20.0), &r()).is_empty());
227 }
228
229 #[test]
230 fn a_degenerate_ring_disappears() {
231 assert!(clip_ring::<Pt>(&[], &r()).is_empty());
232 assert!(clip_ring(&[(1.0, 1.0), (2.0, 2.0)], &r()).is_empty());
233 // A closed ring of two distinct vertices is a line, not an area.
234 assert!(clip_ring(&[(1.0, 1.0), (2.0, 2.0), (1.0, 1.0)], &r()).is_empty());
235 }
236
237 #[test]
238 fn a_hole_survives_a_clip_that_does_not_reach_it() {
239 let rings = vec![square(-5.0, 15.0), square(3.0, 6.0)];
240 let out = clip_polygon(&rings, &r());
241 assert_eq!(out.len(), 2, "exterior plus its hole");
242 assert_eq!(area(&out[0]), 100.0, "the exterior became the rect");
243 assert_eq!(area(&out[1]), 9.0, "the hole is untouched");
244 }
245
246 #[test]
247 fn a_hole_clipped_away_is_dropped_but_the_polygon_stays() {
248 let rings = vec![square(-5.0, 15.0), square(20.0, 25.0)];
249 let out = clip_polygon(&rings, &r());
250 assert_eq!(out.len(), 1, "only the exterior remains");
251 assert_eq!(area(&out[0]), 100.0);
252 }
253
254 /// A hole straddling the tile edge must survive as a hole, not vanish and not
255 /// escape its exterior. The tile clip cuts both rings against the same rect, so
256 /// a hole poking outside is truncated to the rect — but it must still lie inside
257 /// the clipped exterior, or stage C drops it and the island floods.
258 #[test]
259 fn a_hole_straddling_the_tile_edge_stays_inside_its_exterior() {
260 // Exterior covers the rect's west half and beyond; hole crosses the east edge.
261 let exterior = vec![(-5.0, 2.0), (15.0, 2.0), (15.0, 8.0), (-5.0, 8.0), (-5.0, 2.0)];
262 let hole = vec![(5.0, 4.0), (15.0, 4.0), (15.0, 6.0), (5.0, 6.0), (5.0, 4.0)];
263 let out = clip_polygon(&[exterior, hole], &r());
264 assert_eq!(out.len(), 2, "exterior plus its truncated hole: {out:?}");
265 // The hole's clipped area is the 5x2 rect surviving inside: 5..10 x 4..6.
266 assert!((area(&out[1]) - 10.0).abs() < 1e-9, "hole area {} from {out:?}", area(&out[1]));
267 // Every hole vertex is inside (or on) the clipped exterior: the exact
268 // invariant stage C's strictly_inside demands before the boundary rule.
269 for &(x, y) in &out[1] {
270 assert!(
271 x >= 0.0 && x <= 10.0 && y >= 2.0 && y <= 8.0,
272 "hole vertex ({x}, {y}) escaped the clipped exterior"
273 );
274 }
275 }
276
277 #[test]
278 fn losing_the_exterior_ring_drops_the_holes_too() {
279 // A hole with no surrounding area is not a shape. Emitting it would render
280 // as a solid patch of whatever colour the layer paints.
281 let rings = vec![square(20.0, 30.0), square(3.0, 6.0)];
282 assert!(clip_polygon(&rings, &r()).is_empty());
283 }
284
285 #[test]
286 fn a_concave_ring_clips_to_one_ring_with_the_right_total_area() {
287 // A U shape straddling the eastern boundary: the clipped result is two
288 // disjoint prongs, which Sutherland-Hodgman returns as ONE ring joined by a
289 // zero-area sliver along the boundary. The area is still correct, which is
290 // what the renderer cares about. See the module docs.
291 let u = vec![
292 (5.0, 0.0),
293 (15.0, 0.0),
294 (15.0, 3.0),
295 (8.0, 3.0),
296 (8.0, 7.0),
297 (15.0, 7.0),
298 (15.0, 10.0),
299 (5.0, 10.0),
300 (5.0, 0.0),
301 ];
302 let out = clip_ring(&u, &r());
303 assert!(!out.is_empty());
304 // Inside the rect: the left bar (5..10 x 0..10 = 50) minus the notch
305 // (8..10 x 3..7 = 8) = 42.
306 assert!((area(&out) - 42.0).abs() < 1e-9, "area {} from {out:?}", area(&out));
307 }
308
309 // --- the geometry-level entry point -----------------------------------
310
311 #[test]
312 fn clip_geometry_filters_points_whole() {
313 let g = Geometry::Points(vec![(5.0, 5.0), (15.0, 5.0), (0.0, 0.0), (f64::NAN, 1.0)]);
314 let Geometry::Points(out) = clip_geometry(&g, &r()) else { panic!() };
315 assert_eq!(out, vec![(5.0, 5.0), (0.0, 0.0)]);
316 }
317
318 #[test]
319 fn clip_geometry_flattens_partitioned_lines_into_one_list() {
320 // Two input lines, the second of which splits in two: three parts out.
321 let g = Geometry::Lines(vec![
322 vec![(1.0, 1.0), (2.0, 2.0)],
323 vec![(5.0, 2.0), (15.0, 2.0), (15.0, 8.0), (5.0, 8.0)],
324 ]);
325 let Geometry::Lines(out) = clip_geometry(&g, &r()) else { panic!() };
326 assert_eq!(out.len(), 3);
327 }
328
329 #[test]
330 fn clip_geometry_drops_polygons_that_vanish_entirely() {
331 let g = Geometry::Polygons(vec![vec![square(2.0, 8.0)], vec![square(20.0, 30.0)]]);
332 let Geometry::Polygons(out) = clip_geometry(&g, &r()) else { panic!() };
333 assert_eq!(out.len(), 1);
334 }
335
336 #[test]
337 fn clipping_to_a_buffered_tile_rect_keeps_the_overspill() {
338 use tile_build::geom::{tile_rect, DEFAULT_BUFFER};
339 // A line 3 units past tile 0's eastern edge, with a 5-unit buffer: the
340 // overspill survives, which is what stops a seam at the tile join.
341 let rect = tile_rect(0, 0, 4096, DEFAULT_BUFFER);
342 let line = vec![(4090.0, 100.0), (4099.0, 100.0)];
343 let out = clip_line(&line, &rect);
344 assert_eq!(out.len(), 1);
345 assert!(close(out[0][1], (4099.0, 100.0)), "inside the buffer: {out:?}");
346 // Past the buffer, it is cut at buffer's edge.
347 let line = vec![(4090.0, 100.0), (4200.0, 100.0)];
348 let out = clip_line(&line, &rect);
349 assert!(close(out[0][1], (4096.0 + DEFAULT_BUFFER, 100.0)), "{out:?}");
350 }
351
352 // --- significance ------------------------------------------------------
353
354 /// Every vertex the clip interpolates is marked as one no threshold may drop,
355 /// and every vertex that merely passes through keeps whatever it arrived with.
356 /// That is the contract [`tile_build::simplify::filter`] relies on to keep two rings
357 /// sharing a boundary edge on the same vertices.
358 #[test]
359 fn a_crossing_is_marked_unremovable_and_a_pass_through_keeps_its_significance() {
360 use tile_build::geom::{SigPt, ALWAYS};
361
362 let at = |x: f64, y: f64, sig: f64| SigPt { x, y, sig };
363 // In from the west, out to the east: both ends are crossings, the middle
364 // vertex is the source's own and keeps its score.
365 let line = [at(-5.0, 5.0, 7.0), at(5.0, 5.0, 3.0), at(15.0, 5.0, 9.0)];
366 let out = clip_line(&line, &r());
367 assert_eq!(out.len(), 1, "{out:?}");
368 let piece = &out[0];
369 assert_eq!(piece.len(), 3);
370 assert_eq!(piece[0], at(0.0, 5.0, ALWAYS), "the western crossing");
371 assert_eq!(piece[1], at(5.0, 5.0, 3.0), "untouched, score and all");
372 assert_eq!(piece[2], at(10.0, 5.0, ALWAYS), "the eastern crossing");
373
374 // A segment entirely inside is handed back as the very same vertices, not
375 // as recomputed ones: `a + 1.0 * (b - a)` is not always `b`.
376 let inside = [at(2.0, 2.0, 4.0), at(8.0, 7.0, 6.0)];
377 assert_eq!(clip_line(&inside, &r())[0], inside.to_vec());
378 }
379
380 /// A ring re-closed after clipping must close on a **copy** of its first
381 /// surviving vertex. Two ends with different significance is how a filter opens
382 /// a ring: it keeps one and drops the other.
383 #[test]
384 fn a_re_closed_ring_closes_on_a_copy_of_its_first_vertex() {
385 use tile_build::geom::SigPt;
386
387 let at = |x: f64, y: f64, sig: f64| SigPt { x, y, sig };
388 // Half in, half out, so the ring is genuinely rebuilt by the clip.
389 let ring = [
390 at(5.0, 2.0, 1.0),
391 at(15.0, 2.0, 2.0),
392 at(15.0, 8.0, 3.0),
393 at(5.0, 8.0, 4.0),
394 at(5.0, 2.0, 1.0),
395 ];
396 let out = clip_ring(&ring, &r());
397 assert!(out.len() > 3, "{out:?}");
398 assert_eq!(out.first(), out.last(), "closed, on the same vertex exactly");
399 }
400}
401
402