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

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

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