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