Modern-Apps.git
Publicgit clone https://git.maproj.org/Modern-Apps.gitModern-Apps.git / scripts / maps / tile_build / tests / geom_part2.rs
geom_part2.rs356 lines · 14.7 KiBRaw
| 1 | #[cfg(test)] |
| 2 | mod tests { |
| 3 | use tile_build::geom::*; |
| 4 | use tile_build::mvt::DEFAULT_EXTENT; |
| 5 | |
| 6 | #[test] |
| 7 | fn projection_anchors_are_right() { |
| 8 | // z0: the whole world is one tile, and 0,0 sits at its centre. |
| 9 | let (x, y) = project(0.0, 0.0, 0); |
| 10 | assert!((x - 0.5).abs() < 1e-9, "lon 0 -> x 0.5, got {x}"); |
| 11 | assert!((y - 0.5).abs() < 1e-9, "lat 0 -> y 0.5, got {y}"); |
| 12 | let (x, _) = project(-180.0, 0.0, 0); |
| 13 | assert!(x.abs() < 1e-9); |
| 14 | let (_, y) = project(0.0, 85.051_128_78, 0); |
| 15 | assert!(y.abs() < 1e-6, "Mercator top -> y 0, got {y}"); |
| 16 | // A pole must clamp rather than diverge. |
| 17 | let (_, y) = project(0.0, 90.0, 4); |
| 18 | assert!(y.is_finite(), "lat 90 must clamp, got {y}"); |
| 19 | // San Francisco at z11 is a well-known tile. |
| 20 | let (fx, fy) = project(-122.4194, 37.7749, 11); |
| 21 | assert_eq!((fx.floor() as u64, fy.floor() as u64), (327, 791)); |
| 22 | } |
| 23 | |
| 24 | #[test] |
| 25 | fn scaling_puts_a_point_inside_its_own_tile() { |
| 26 | let extent = DEFAULT_EXTENT; |
| 27 | let (wx, wy) = project_scaled(-122.4194, 37.7749, 11, extent); |
| 28 | let (tx, ty) = (327u64, 791u64); |
| 29 | let local = (wx - tx as f64 * extent as f64, wy - ty as f64 * extent as f64); |
| 30 | assert!( |
| 31 | (0.0..=extent as f64).contains(&local.0) && (0.0..=extent as f64).contains(&local.1), |
| 32 | "{local:?} must be inside [0,{extent}]" |
| 33 | ); |
| 34 | } |
| 35 | |
| 36 | #[test] |
| 37 | fn buffer_scales_with_the_extent() { |
| 38 | assert_eq!(buffer_for(DEFAULT_EXTENT), DEFAULT_BUFFER); |
| 39 | assert_eq!(buffer_for(8192), DEFAULT_BUFFER * 2.0); |
| 40 | assert_eq!(buffer_for(2048), DEFAULT_BUFFER / 2.0); |
| 41 | } |
| 42 | |
| 43 | #[test] |
| 44 | fn bounds_cover_every_part_and_ignore_non_finite() { |
| 45 | let g = |
| 46 | Geometry::Lines(vec![vec![(0.0, 0.0), (10.0, 5.0)], vec![(-3.0, 20.0), (4.0, 1.0)]]); |
| 47 | assert_eq!(bounds(&g), Some(Rect { min_x: -3.0, min_y: 0.0, max_x: 10.0, max_y: 20.0 })); |
| 48 | assert_eq!(bounds(&Geometry::<Pt>::Points(vec![])), None); |
| 49 | |
| 50 | // A NaN vertex is skipped rather than poisoning the whole box. |
| 51 | let g = Geometry::Points(vec![(1.0, 2.0), (f64::NAN, 0.0), (3.0, 4.0)]); |
| 52 | assert_eq!(bounds(&g), Some(Rect { min_x: 1.0, min_y: 2.0, max_x: 3.0, max_y: 4.0 })); |
| 53 | assert_eq!(bounds(&Geometry::Points(vec![(f64::NAN, f64::NAN)])), None); |
| 54 | } |
| 55 | |
| 56 | #[test] |
| 57 | fn a_tile_range_covers_exactly_the_tiles_touched() { |
| 58 | let e = 4096u32; |
| 59 | // Entirely inside tile (1,1). |
| 60 | let b = Rect { |
| 61 | min_x: 1.5 * e as f64, |
| 62 | min_y: 1.5 * e as f64, |
| 63 | max_x: 1.6 * e as f64, |
| 64 | max_y: 1.6 * e as f64, |
| 65 | }; |
| 66 | assert_eq!(tile_range(&b, 4, e, 0.0), Some(TileRange { x0: 1, y0: 1, x1: 1, y1: 1 })); |
| 67 | // Straddling into (2,2). |
| 68 | let b = Rect { |
| 69 | min_x: 1.5 * e as f64, |
| 70 | min_y: 1.5 * e as f64, |
| 71 | max_x: 2.5 * e as f64, |
| 72 | max_y: 2.5 * e as f64, |
| 73 | }; |
| 74 | assert_eq!(tile_range(&b, 4, e, 0.0), Some(TileRange { x0: 1, y0: 1, x1: 2, y1: 2 })); |
| 75 | } |
| 76 | |
| 77 | #[test] |
| 78 | fn the_pad_pulls_in_the_neighbouring_tile() { |
| 79 | let e = 4096u32; |
| 80 | // Two units inside tile 1's left edge. With no pad it touches only tile 1; |
| 81 | // with a 5-unit pad it also reaches into tile 0's buffer, which is the |
| 82 | // whole reason the buffer exists. |
| 83 | let x = e as f64 + 2.0; |
| 84 | let b = Rect { min_x: x, min_y: x, max_x: x, max_y: x }; |
| 85 | assert_eq!(tile_range(&b, 4, e, 0.0), Some(TileRange { x0: 1, y0: 1, x1: 1, y1: 1 })); |
| 86 | assert_eq!(tile_range(&b, 4, e, 5.0), Some(TileRange { x0: 0, y0: 0, x1: 1, y1: 1 })); |
| 87 | } |
| 88 | |
| 89 | #[test] |
| 90 | fn a_tile_range_clamps_to_the_grid_and_rejects_the_wholly_outside() { |
| 91 | let e = 4096u32; |
| 92 | let n = 1u64 << 4; |
| 93 | // Overhanging both ends is clamped, not wrapped. |
| 94 | let b = Rect { |
| 95 | min_x: -100.0, |
| 96 | min_y: -100.0, |
| 97 | max_x: (n as f64 + 3.0) * e as f64, |
| 98 | max_y: (n as f64 + 3.0) * e as f64, |
| 99 | }; |
| 100 | assert_eq!( |
| 101 | tile_range(&b, 4, e, 0.0), |
| 102 | Some(TileRange { x0: 0, y0: 0, x1: n - 1, y1: n - 1 }) |
| 103 | ); |
| 104 | // Wholly off the grid. |
| 105 | let b = |
| 106 | Rect { min_x: -10.0 * e as f64, min_y: 0.0, max_x: -9.0 * e as f64, max_y: e as f64 }; |
| 107 | assert_eq!(tile_range(&b, 4, e, 0.0), None); |
| 108 | // NaN in, None out. |
| 109 | let b = Rect { min_x: f64::NAN, min_y: 0.0, max_x: 1.0, max_y: 1.0 }; |
| 110 | assert_eq!(tile_range(&b, 4, e, 0.0), None); |
| 111 | } |
| 112 | |
| 113 | /// The whole point of `tiles_touched`: a long diagonal must not claim the empty |
| 114 | /// corners of its bounding box. This is the case that made `transit_lines` at |
| 115 | /// planet scale take hours. |
| 116 | #[test] |
| 117 | fn a_diagonal_line_skips_the_corners_of_its_bounding_box() { |
| 118 | let extent = 4096u32; |
| 119 | let e = extent as f64; |
| 120 | // A staircase across a 40 x 40 tile square, one vertex per tile step, so the |
| 121 | // line genuinely passes through ~40 tiles out of 1600. |
| 122 | let pts: Vec<Pt> = (0..=40).map(|i| (i as f64 * e, i as f64 * e)).collect(); |
| 123 | let g = Geometry::Lines(vec![pts]); |
| 124 | |
| 125 | let mut touched = Vec::new(); |
| 126 | tiles_touched(&g, 8, extent, 0.0, &mut touched); |
| 127 | |
| 128 | let bbox = tile_range(&bounds(&g).unwrap(), 8, extent, 0.0).unwrap(); |
| 129 | assert_eq!(bbox.len(), 41 * 41, "the box really is that big"); |
| 130 | // Every vertex sits exactly on a tile corner here, which is the worst case: |
| 131 | // each segment's box spans 2x2 tiles rather than 1. Even so the walk is an |
| 132 | // order of magnitude below the box, and that ratio is what grows with zoom. |
| 133 | assert!( |
| 134 | (touched.len() as u64) * 10 < bbox.len(), |
| 135 | "walked {} tiles, more than a tenth of the box's {}", |
| 136 | touched.len(), |
| 137 | bbox.len() |
| 138 | ); |
| 139 | |
| 140 | // Every tile on the diagonal is present... |
| 141 | for i in 0..=40u64 { |
| 142 | assert!(touched.contains(&(i, i)), "tile ({i},{i}) is on the line"); |
| 143 | } |
| 144 | // ...and the far corners, which the line never approaches, are not. |
| 145 | assert!(!touched.contains(&(0, 40)), "bottom-left corner is empty"); |
| 146 | assert!(!touched.contains(&(40, 0)), "top-right corner is empty"); |
| 147 | } |
| 148 | |
| 149 | /// Safety property: it may over-include, never under-include. Everything it lists |
| 150 | /// is inside the feature's bounding box, and every point ON the line lands in a |
| 151 | /// tile it listed. |
| 152 | /// |
| 153 | /// Sampling the line rather than asserting each segment's whole box, because |
| 154 | /// bisection deliberately produces a tighter set than those boxes — that is the |
| 155 | /// optimisation. What must not change is that no part of the line is unreachable. |
| 156 | #[test] |
| 157 | fn tiles_touched_covers_every_point_on_the_line_and_nothing_outside_the_box() { |
| 158 | let extent = 4096u32; |
| 159 | let e = extent as f64; |
| 160 | let pts = vec![(0.5 * e, 0.5 * e), (3.5 * e, 1.5 * e), (2.0 * e, 6.0 * e)]; |
| 161 | let g = Geometry::Lines(vec![pts.clone()]); |
| 162 | |
| 163 | let mut touched = Vec::new(); |
| 164 | tiles_touched(&g, 8, extent, 0.0, &mut touched); |
| 165 | |
| 166 | let bbox = tile_range(&bounds(&g).unwrap(), 8, extent, 0.0).unwrap(); |
| 167 | let inside: Vec<(u64, u64)> = bbox.iter().collect(); |
| 168 | for t in &touched { |
| 169 | assert!(inside.contains(t), "{t:?} is outside the bounding box"); |
| 170 | } |
| 171 | |
| 172 | // Walk each segment finely; every sample's tile must have been listed. |
| 173 | for seg in pts.windows(2) { |
| 174 | let steps = 2000; |
| 175 | for i in 0..=steps { |
| 176 | let t = i as f64 / steps as f64; |
| 177 | let x = seg[0].0 + (seg[1].0 - seg[0].0) * t; |
| 178 | let y = seg[0].1 + (seg[1].1 - seg[0].1) * t; |
| 179 | let tile = ((x / e).floor() as u64, (y / e).floor() as u64); |
| 180 | assert!( |
| 181 | touched.contains(&tile), |
| 182 | "point ({x}, {y}) is in tile {tile:?}, which was not listed" |
| 183 | ); |
| 184 | } |
| 185 | } |
| 186 | } |
| 187 | |
| 188 | /// Bisection is what makes the result independent of vertex density: a 4-vertex |
| 189 | /// line across a continent must walk about as many tiles as a 400-vertex one along |
| 190 | /// the same path, rather than filling its segment boxes. |
| 191 | #[test] |
| 192 | fn a_sparse_line_walks_as_tightly_as_a_dense_one() { |
| 193 | let extent = 4096u32; |
| 194 | let e = extent as f64; |
| 195 | let end = 40.0; |
| 196 | |
| 197 | let sparse = Geometry::Lines(vec![vec![(0.0, 0.0), (end * e, end * e)]]); |
| 198 | let dense: Vec<Pt> = (0..=400) |
| 199 | .map(|i| { |
| 200 | let t = i as f64 / 400.0; |
| 201 | (end * e * t, end * e * t) |
| 202 | }) |
| 203 | .collect(); |
| 204 | let dense = Geometry::Lines(vec![dense]); |
| 205 | |
| 206 | let (mut a, mut b) = (Vec::new(), Vec::new()); |
| 207 | tiles_touched(&sparse, 8, extent, 0.0, &mut a); |
| 208 | tiles_touched(&dense, 8, extent, 0.0, &mut b); |
| 209 | |
| 210 | let bbox = tile_range(&bounds(&sparse).unwrap(), 8, extent, 0.0).unwrap(); |
| 211 | assert!( |
| 212 | (a.len() as u64) * 10 < bbox.len(), |
| 213 | "sparse walked {} of the box's {}", |
| 214 | a.len(), |
| 215 | bbox.len() |
| 216 | ); |
| 217 | // Within a small factor of each other, not orders apart. |
| 218 | assert!( |
| 219 | a.len() < b.len() * 3 && b.len() < a.len() * 3, |
| 220 | "sparse {} vs dense {} should be comparable", |
| 221 | a.len(), |
| 222 | b.len() |
| 223 | ); |
| 224 | } |
| 225 | |
| 226 | /// A polygon covers its interior, so its footprint stays the whole ring box -- |
| 227 | /// per-segment boxes would drop every tile strictly inside it. |
| 228 | #[test] |
| 229 | fn a_polygon_still_claims_its_interior_tiles() { |
| 230 | let extent = 4096u32; |
| 231 | let e = extent as f64; |
| 232 | let ring = vec![ |
| 233 | (0.5 * e, 0.5 * e), |
| 234 | (5.5 * e, 0.5 * e), |
| 235 | (5.5 * e, 5.5 * e), |
| 236 | (0.5 * e, 5.5 * e), |
| 237 | (0.5 * e, 0.5 * e), |
| 238 | ]; |
| 239 | let g = Geometry::Polygons(vec![vec![ring]]); |
| 240 | |
| 241 | let mut touched = Vec::new(); |
| 242 | tiles_touched(&g, 8, extent, 0.0, &mut touched); |
| 243 | |
| 244 | // (3,3) is strictly inside and touches no edge. |
| 245 | assert!(touched.contains(&(3, 3)), "interior tile must be filled"); |
| 246 | assert_eq!(touched.len(), 36, "the whole 6x6 footprint"); |
| 247 | } |
| 248 | |
| 249 | /// Adjacent segments share tiles; a duplicate would encode the geometry twice. |
| 250 | #[test] |
| 251 | fn tiles_touched_deduplicates_and_sorts() { |
| 252 | let extent = 4096u32; |
| 253 | let e = extent as f64; |
| 254 | // Many short segments all inside one tile. |
| 255 | let pts: Vec<Pt> = (0..20).map(|i| (0.1 * e + i as f64, 0.1 * e)).collect(); |
| 256 | let mut touched = Vec::new(); |
| 257 | tiles_touched(&Geometry::Lines(vec![pts]), 8, extent, 0.0, &mut touched); |
| 258 | assert_eq!(touched, vec![(0, 0)]); |
| 259 | |
| 260 | let mut sorted = touched.clone(); |
| 261 | sorted.sort_unstable(); |
| 262 | assert_eq!(touched, sorted, "output is sorted for a deterministic pass"); |
| 263 | } |
| 264 | |
| 265 | #[test] |
| 266 | fn tile_range_iterates_row_major() { |
| 267 | let r = TileRange { x0: 2, y0: 5, x1: 3, y1: 6 }; |
| 268 | assert_eq!(r.len(), 4); |
| 269 | assert_eq!(r.iter().collect::<Vec<_>>(), vec![(2, 5), (3, 5), (2, 6), (3, 6)]); |
| 270 | } |
| 271 | |
| 272 | #[test] |
| 273 | fn a_tile_rect_is_the_tile_plus_its_buffer() { |
| 274 | let r = tile_rect(2, 3, 4096, 5.0); |
| 275 | assert_eq!( |
| 276 | r, |
| 277 | Rect { |
| 278 | min_x: 8192.0 - 5.0, |
| 279 | min_y: 12288.0 - 5.0, |
| 280 | max_x: 12288.0 + 5.0, |
| 281 | max_y: 16384.0 + 5.0 |
| 282 | } |
| 283 | ); |
| 284 | assert!(r.contains((8192.0, 12288.0))); |
| 285 | assert!(r.contains((8192.0 - 4.0, 12288.0 - 4.0)), "inside the buffer"); |
| 286 | assert!(!r.contains((8192.0 - 6.0, 12288.0)), "beyond the buffer"); |
| 287 | } |
| 288 | |
| 289 | #[test] |
| 290 | fn to_tile_translates_then_rounds() { |
| 291 | // A vertex at world (4096.4, 8191.6) is (0.4, 4095.6) inside tile (1,1), |
| 292 | // which rounds to (0, 4096). |
| 293 | let g = Geometry::Lines(vec![vec![(4096.4, 8191.6), (5000.0, 9000.0)]]); |
| 294 | let IntGeometry::Lines(lines) = to_tile(&g, 1, 1, 4096) else { |
| 295 | panic!("lines in, lines out") |
| 296 | }; |
| 297 | assert_eq!(lines[0][0], (0, 4096)); |
| 298 | assert_eq!(lines[0][1], (904, 4904)); |
| 299 | } |
| 300 | |
| 301 | #[test] |
| 302 | fn quantisation_drops_the_duplicates_it_creates() { |
| 303 | // Three vertices within a rounding unit of each other collapse to one. |
| 304 | let g = Geometry::Lines(vec![vec![(10.1, 10.1), (10.2, 10.3), (9.9, 10.0), (20.0, 20.0)]]); |
| 305 | let IntGeometry::Lines(lines) = quantize(&g) else { panic!() }; |
| 306 | assert_eq!(lines[0], vec![(10, 10), (20, 20)]); |
| 307 | |
| 308 | // Non-adjacent repeats are kept: a line that doubles back is a real shape. |
| 309 | let g = Geometry::Lines(vec![vec![(0.0, 0.0), (5.0, 0.0), (0.0, 0.0)]]); |
| 310 | let IntGeometry::Lines(lines) = quantize(&g) else { panic!() }; |
| 311 | assert_eq!(lines[0], vec![(0, 0), (5, 0), (0, 0)]); |
| 312 | } |
| 313 | |
| 314 | #[test] |
| 315 | fn quantisation_keeps_a_rings_closing_vertex() { |
| 316 | // ClosePath is applied by the encoder, not here, so the ring stays closed |
| 317 | // through simplification -- which needs the closure to measure against. |
| 318 | let ring = vec![(0.0, 0.0), (4.0, 0.0), (4.0, 4.0), (0.0, 4.0), (0.0, 0.0)]; |
| 319 | let IntGeometry::Polygons(polys) = quantize(&Geometry::Polygons(vec![vec![ring]])) else { |
| 320 | panic!() |
| 321 | }; |
| 322 | assert_eq!(polys[0][0].len(), 5); |
| 323 | assert_eq!(polys[0][0].first(), polys[0][0].last()); |
| 324 | } |
| 325 | |
| 326 | #[test] |
| 327 | fn a_non_finite_vertex_saturates_rather_than_wrapping() { |
| 328 | // `f64::NAN as i32` is 0 in Rust and `1e300 as i32` is the saturated bound, |
| 329 | // but both are silent. Keeping the saturation explicit means an out-of-range |
| 330 | // vertex stays obviously out of range instead of landing somewhere |
| 331 | // plausible; a NaN has no meaningful bound, so it becomes the origin. |
| 332 | let g = Geometry::Points(vec![(f64::NAN, 1e300), (-1e300, 5.0)]); |
| 333 | let IntGeometry::Points(pts) = quantize(&g) else { panic!() }; |
| 334 | assert_eq!(pts, vec![(0, i32::MAX), (i32::MIN, 5)]); |
| 335 | } |
| 336 | |
| 337 | #[test] |
| 338 | fn emptiness_is_judged_per_geometry_kind() { |
| 339 | assert!(IntGeometry::Points(vec![]).is_empty()); |
| 340 | assert!(!IntGeometry::Points(vec![(0, 0)]).is_empty()); |
| 341 | assert!(IntGeometry::Lines(vec![vec![(0, 0)]]).is_empty()); |
| 342 | assert!(!IntGeometry::Lines(vec![vec![(0, 0), (1, 1)]]).is_empty()); |
| 343 | // Three distinct vertices are the least that can enclose an area. |
| 344 | assert!(IntGeometry::Polygons(vec![vec![vec![(0, 0), (1, 0)]]]).is_empty()); |
| 345 | assert!(!IntGeometry::Polygons(vec![vec![vec![(0, 0), (1, 0), (1, 1)]]]).is_empty()); |
| 346 | } |
| 347 | |
| 348 | #[test] |
| 349 | fn rects_intersect_symmetrically_and_touch_counts() { |
| 350 | let a = Rect { min_x: 0.0, min_y: 0.0, max_x: 10.0, max_y: 10.0 }; |
| 351 | let b = Rect { min_x: 10.0, min_y: 10.0, max_x: 20.0, max_y: 20.0 }; |
| 352 | let c = Rect { min_x: 11.0, min_y: 0.0, max_x: 20.0, max_y: 10.0 }; |
| 353 | assert!(a.intersects(&b) && b.intersects(&a), "touching corners overlap"); |
| 354 | assert!(!a.intersects(&c) && !c.intersects(&a)); |
| 355 | } |
| 356 | } |