Modern-Apps.git
Publicgit clone https://git.maproj.org/Modern-Apps.gitModern-Apps.git / scripts / maps / mamaps_build / src / rings_part1.rs
rings_part1.rs355 lines · 16.0 KiBRaw
| 1 | #[cfg(test)] |
| 2 | mod tests { |
| 3 | use super::*; |
| 4 | use tilecodec::mamaps::body::{Feature, GEOM_LINE, GEOM_POLYGON}; |
| 5 | use tilecodec::mamaps::dict; |
| 6 | |
| 7 | /// A layer holding one polygon feature with the given rings, all labelled by position. |
| 8 | fn layer_of(rings: &[Vec<(i16, i16)>]) -> Layer { |
| 9 | let mut layer = Layer::new(dict::LAYER_LANDTYPE); |
| 10 | layer.features.push(Feature { |
| 11 | kind: dict::NONE, |
| 12 | kind_detail: dict::NONE, |
| 13 | geom_type: GEOM_POLYGON, |
| 14 | flags: 0, |
| 15 | name_idx: tilecodec::mamaps::body::NAME_NONE, |
| 16 | parts_offset: 0, |
| 17 | part_count: rings.len() as u32, |
| 18 | transit_color: 0, |
| 19 | transit_ordinal: 0, |
| 20 | transit_lanes: 0, |
| 21 | transit_taper: 0, |
| 22 | lane_count: 0, |
| 23 | }); |
| 24 | for (index, ring) in rings.iter().enumerate() { |
| 25 | layer.parts.push(Part { |
| 26 | coord_start: layer.coords.len() as u32, |
| 27 | point_count: ring.len() as u32, |
| 28 | winding: if index == 0 { WINDING_OUTER } else { WINDING_HOLE }, |
| 29 | }); |
| 30 | layer.coords.extend_from_slice(ring); |
| 31 | } |
| 32 | layer |
| 33 | } |
| 34 | |
| 35 | /// Positive signed area, which is what this module calls an exterior's winding. |
| 36 | /// |
| 37 | /// Tile coordinates put y downward, so a positive shoelace area looks clockwise on screen. The |
| 38 | /// name follows the formula rather than the screen, because the formula is what the code and |
| 39 | /// `tess::fill` both use. |
| 40 | fn square(x: i16, y: i16, size: i16) -> Vec<(i16, i16)> { |
| 41 | vec![(x, y), (x + size, y), (x + size, y + size), (x, y + size), (x, y)] |
| 42 | } |
| 43 | |
| 44 | fn reversed(ring: &[(i16, i16)]) -> Vec<(i16, i16)> { |
| 45 | let mut out = ring.to_vec(); |
| 46 | out.reverse(); |
| 47 | out |
| 48 | } |
| 49 | |
| 50 | /// **The bug that reached a real build.** A degenerate exterior makes `normalise` drop the |
| 51 | /// feature, and the id table is indexed by feature position — so dropping one without dropping |
| 52 | /// its id shifts every id after it onto the wrong feature. The encoder catches the length |
| 53 | /// mismatch, but only when the casualty happens to be the last feature does it stay a length |
| 54 | /// mismatch rather than a silent misattribution. |
| 55 | #[test] |
| 56 | fn dropping_a_degenerate_feature_drops_its_id_too() { |
| 57 | // Three features: a good square, a collapsed exterior, and another good square. |
| 58 | let mut layer = layer_of(&[square(0, 0, 100)]); |
| 59 | let mut push = |ring: Vec<(i16, i16)>| { |
| 60 | layer.features.push(Feature { |
| 61 | kind: dict::NONE, |
| 62 | kind_detail: dict::NONE, |
| 63 | geom_type: GEOM_POLYGON, |
| 64 | flags: 0, |
| 65 | name_idx: tilecodec::mamaps::body::NAME_NONE, |
| 66 | parts_offset: layer.parts.len() as u32, |
| 67 | part_count: 1, |
| 68 | transit_color: 0, |
| 69 | transit_ordinal: 0, |
| 70 | transit_lanes: 0, |
| 71 | transit_taper: 0, |
| 72 | lane_count: 0, |
| 73 | }); |
| 74 | layer.parts.push(Part { |
| 75 | coord_start: layer.coords.len() as u32, |
| 76 | point_count: ring.len() as u32, |
| 77 | winding: WINDING_OUTER, |
| 78 | }); |
| 79 | layer.coords.extend_from_slice(&ring); |
| 80 | }; |
| 81 | // Zero area: every point the same, so there is no exterior left after normalising. |
| 82 | push(vec![(5, 5), (5, 5), (5, 5), (5, 5)]); |
| 83 | push(square(200, 200, 100)); |
| 84 | |
| 85 | let mut ids = vec![11u64, 22, 33]; |
| 86 | normalise_with_ids(&mut layer, Some(&mut ids), None, None); |
| 87 | |
| 88 | assert_eq!(layer.features.len(), 2, "the collapsed exterior is dropped"); |
| 89 | assert_eq!(ids, vec![11, 33], "and its id goes with it, not the one after it"); |
| 90 | } |
| 91 | |
| 92 | #[test] |
| 93 | fn an_exterior_wound_the_wrong_way_is_reversed_not_relabelled() { |
| 94 | let mut layer = layer_of(&[reversed(&square(0, 0, 100))]); |
| 95 | let stats = normalise(&mut layer); |
| 96 | assert_eq!(stats.rewound, 1); |
| 97 | assert!(check(&layer).is_empty(), "{:?}", check(&layer)); |
| 98 | // The coordinates changed, not just the label: the winding field states a fact about them. |
| 99 | assert!(signed_area(layer.points(&layer.parts[0])) > 0.0); |
| 100 | } |
| 101 | |
| 102 | #[test] |
| 103 | fn a_hole_wound_the_wrong_way_is_reversed() { |
| 104 | let mut layer = layer_of(&[square(0, 0, 100), square(20, 20, 20)]); |
| 105 | let stats = normalise(&mut layer); |
| 106 | // The hole was given counter-clockwise, which is an exterior's winding. |
| 107 | assert_eq!(stats.rewound, 1); |
| 108 | assert_eq!(stats.holes_dropped, 0); |
| 109 | assert!(check(&layer).is_empty(), "{:?}", check(&layer)); |
| 110 | assert!(signed_area(layer.points(&layer.parts[1])) < 0.0, "the hole is clockwise"); |
| 111 | } |
| 112 | |
| 113 | /// **The four pathologies the published z0 tile exhibits.** Each is dropped and counted rather |
| 114 | /// than clipped, because a botched boolean operation is worse than a missing lake. |
| 115 | #[test] |
| 116 | fn a_hole_outside_straddling_or_overlapping_is_dropped_and_counted() { |
| 117 | // Wholly outside. |
| 118 | let mut layer = layer_of(&[square(0, 0, 100), square(500, 500, 20)]); |
| 119 | let stats = normalise(&mut layer); |
| 120 | assert_eq!(stats.holes_dropped, 1); |
| 121 | assert_eq!(layer.features[0].part_count, 1, "only the exterior is left"); |
| 122 | |
| 123 | // Straddling the exterior. |
| 124 | let mut layer = layer_of(&[square(0, 0, 100), square(90, 90, 40)]); |
| 125 | assert_eq!(normalise(&mut layer).holes_dropped, 1); |
| 126 | |
| 127 | // Overlapping another hole. |
| 128 | let mut layer = |
| 129 | layer_of(&[square(0, 0, 200), square(20, 20, 60), square(50, 50, 60)]); |
| 130 | let stats = normalise(&mut layer); |
| 131 | assert_eq!(stats.holes_dropped, 1, "the first hole is kept, the overlapping one is not"); |
| 132 | assert_eq!(layer.features[0].part_count, 2); |
| 133 | assert!(check(&layer).is_empty(), "{:?}", check(&layer)); |
| 134 | } |
| 135 | |
| 136 | /// **The zoom-in island flooding.** A hole clipped onto the tile edge shares exact |
| 137 | /// vertices with its exterior's clipped edge there. The boundary counts as inside, so |
| 138 | /// the hole is kept — reading it as outside dropped real islands on fine tiles while |
| 139 | /// the coarse tile (hole fully inside) stayed pixel-perfect. |
| 140 | #[test] |
| 141 | fn a_hole_touching_its_exterior_is_kept() { |
| 142 | // Left and right edges of the hole sit exactly on the exterior's edges: the shape |
| 143 | // the tile clipper emits when both rings reach the same tile edge. |
| 144 | let exterior = vec![(0i16, 0i16), (100, 0), (100, 100), (0, 100), (0, 0)]; |
| 145 | let touching = vec![(0i16, 40i16), (100, 40), (100, 60), (0, 60), (0, 40)]; |
| 146 | let mut layer = layer_of(&[exterior, touching]); |
| 147 | let stats = normalise(&mut layer); |
| 148 | assert_eq!(stats.holes_dropped, 0, "an edge-touching hole is not a straddle"); |
| 149 | assert_eq!(layer.features[0].part_count, 2); |
| 150 | assert!(check(&layer).is_empty(), "{:?}", check(&layer)); |
| 151 | } |
| 152 | |
| 153 | /// The complement: a hole with a vertex genuinely outside the exterior still goes. |
| 154 | /// One vertex at 130 against an exterior ending at 100 is outside, not on the edge, |
| 155 | /// so no boundary rule rescues it. |
| 156 | #[test] |
| 157 | fn a_hole_crossing_its_exterior_by_one_vertex_is_still_dropped() { |
| 158 | let exterior = vec![(0i16, 0i16), (100, 0), (100, 100), (0, 100), (0, 0)]; |
| 159 | let crossing = vec![(40i16, 30i16), (40, 70), (70, 70), (130, 50), (70, 30), (40, 30)]; |
| 160 | let mut layer = layer_of(&[exterior, crossing]); |
| 161 | let stats = normalise(&mut layer); |
| 162 | assert_eq!(stats.holes_dropped, 1); |
| 163 | assert_eq!(layer.features[0].part_count, 1); |
| 164 | } |
| 165 | |
| 166 | #[test] |
| 167 | fn a_zero_area_ring_goes_and_takes_nothing_with_it() { |
| 168 | // A degenerate hole. |
| 169 | let mut layer = |
| 170 | layer_of(&[square(0, 0, 100), vec![(20, 20), (40, 20), (20, 20), (20, 20)]]); |
| 171 | let stats = normalise(&mut layer); |
| 172 | assert_eq!(stats.zero_area, 1); |
| 173 | assert_eq!(layer.features[0].part_count, 1); |
| 174 | |
| 175 | // A degenerate exterior takes its holes with it: there is nothing for them to be in. |
| 176 | let mut layer = layer_of(&[vec![(0, 0), (10, 0), (0, 0), (0, 0)], square(2, 2, 2)]); |
| 177 | let stats = normalise(&mut layer); |
| 178 | assert_eq!(stats.zero_area, 1); |
| 179 | assert!(layer.features.is_empty(), "the feature draws nothing, so it is not carried"); |
| 180 | } |
| 181 | |
| 182 | /// Nested lakes: a hole inside a hole. The inner one is land again, and dropping it is the |
| 183 | /// conservative answer — it paints as water rather than as a wedge of nothing. |
| 184 | #[test] |
| 185 | fn a_lake_nested_inside_another_hole_is_dropped() { |
| 186 | let mut layer = |
| 187 | layer_of(&[square(0, 0, 400), square(50, 50, 200), square(100, 100, 50)]); |
| 188 | let stats = normalise(&mut layer); |
| 189 | assert_eq!(stats.holes_dropped, 1); |
| 190 | assert!(check(&layer).is_empty(), "{:?}", check(&layer)); |
| 191 | } |
| 192 | |
| 193 | /// An already-valid polygon must come out untouched, or every rebuild would churn. |
| 194 | #[test] |
| 195 | fn a_valid_polygon_is_left_exactly_alone() { |
| 196 | let rings = [square(0, 0, 200), reversed(&square(20, 20, 40))]; |
| 197 | let mut layer = layer_of(&rings); |
| 198 | let before = (layer.parts.clone(), layer.coords.clone()); |
| 199 | let stats = normalise(&mut layer); |
| 200 | assert!(stats.clean(), "{stats:?}"); |
| 201 | assert_eq!((layer.parts, layer.coords), before); |
| 202 | } |
| 203 | |
| 204 | /// Winding means nothing on an open path, so a line's parts pass through untouched. |
| 205 | #[test] |
| 206 | fn a_line_layer_is_not_rewound() { |
| 207 | let mut layer = Layer::new(dict::LAYER_ROADS); |
| 208 | layer.features.push(Feature { |
| 209 | kind: dict::NONE, |
| 210 | kind_detail: dict::NONE, |
| 211 | geom_type: GEOM_LINE, |
| 212 | flags: 0, |
| 213 | name_idx: tilecodec::mamaps::body::NAME_NONE, |
| 214 | parts_offset: 0, |
| 215 | part_count: 1, |
| 216 | transit_color: 0, |
| 217 | transit_ordinal: 0, |
| 218 | transit_lanes: 0, |
| 219 | transit_taper: 0, |
| 220 | lane_count: 0, |
| 221 | }); |
| 222 | layer.parts.push(Part { coord_start: 0, point_count: 3, winding: WINDING_OUTER }); |
| 223 | layer.coords = vec![(0, 0), (50, 0), (50, 50)]; |
| 224 | let before = (layer.parts.clone(), layer.coords.clone()); |
| 225 | let stats = normalise(&mut layer); |
| 226 | assert!(stats.clean()); |
| 227 | assert_eq!((layer.parts, layer.coords), before); |
| 228 | } |
| 229 | |
| 230 | /// The check has to be able to fail, or it proves nothing. |
| 231 | #[test] |
| 232 | fn the_check_reports_each_invariant_it_can_see_broken() { |
| 233 | let mut layer = layer_of(&[reversed(&square(0, 0, 100))]); |
| 234 | assert!(!check(&layer).is_empty(), "a clockwise exterior"); |
| 235 | |
| 236 | layer = layer_of(&[square(0, 0, 100), square(20, 20, 20)]); |
| 237 | let problems = check(&layer); |
| 238 | assert!( |
| 239 | problems.iter().any(|p| p.contains("not clockwise")), |
| 240 | "a counter-clockwise hole: {problems:?}", |
| 241 | ); |
| 242 | |
| 243 | layer = layer_of(&[square(0, 0, 100), reversed(&square(500, 500, 20))]); |
| 244 | let problems = check(&layer); |
| 245 | assert!( |
| 246 | problems.iter().any(|p| p.contains("not inside")), |
| 247 | "a hole outside its exterior: {problems:?}", |
| 248 | ); |
| 249 | } |
| 250 | |
| 251 | /// The arena has to stay exactly tiled by the parts after a ring is dropped, or the encoder |
| 252 | /// refuses the body — which is the check that makes this safe to run over a whole build. |
| 253 | #[test] |
| 254 | fn the_arena_stays_tiled_by_its_parts_after_a_drop() { |
| 255 | let mut layer = |
| 256 | layer_of(&[square(0, 0, 100), square(500, 500, 20), square(20, 20, 20)]); |
| 257 | normalise(&mut layer); |
| 258 | let mut at = 0u32; |
| 259 | for part in &layer.parts { |
| 260 | assert_eq!(part.coord_start, at); |
| 261 | at += part.point_count; |
| 262 | } |
| 263 | assert_eq!(at as usize, layer.coords.len(), "no orphaned coordinates"); |
| 264 | let body = tilecodec::mamaps::body::Body { |
| 265 | extent: 4096, |
| 266 | layers: vec![layer], |
| 267 | names: Vec::new(), |
| 268 | ids: Vec::new(), |
| 269 | turn_lanes: Vec::new(), |
| 270 | buildings: Vec::new(), |
| 271 | heightmap: None, carriageways: Vec::new(), convention: None, region_links: Vec::new(), |
| 272 | }; |
| 273 | // The encoder's own contiguity check, which is the real proof. |
| 274 | assert!(tilecodec::mamaps::body::serialize(&body).is_ok()); |
| 275 | } |
| 276 | |
| 277 | /// **Verification item 6, over a whole build rather than a synthetic case.** Every polygon of |
| 278 | /// every tile has to satisfy all five invariants after the tiler runs, because that is exactly |
| 279 | /// what the archive's `FLAG_RINGS_VALIDATED` claims and what the renderer will skip a repair |
| 280 | /// pass on the strength of. |
| 281 | #[test] |
| 282 | fn every_polygon_of_a_real_build_is_valid() { |
| 283 | use crate::schema::Class; |
| 284 | use tilecodec::mamaps::body::Body; |
| 285 | use tilecodec::mamaps::dict; |
| 286 | |
| 287 | // Shapes chosen to exercise the pathologies: a lake with a hole, a hole outside its |
| 288 | // exterior, and a shape large enough to be clipped across several tiles at deep zoom. |
| 289 | let ring = |x: f64, y: f64, size: f64| { |
| 290 | vec![(x, y), (x + size, y), (x + size, y + size), (x, y + size), (x, y)] |
| 291 | }; |
| 292 | let features = vec![ |
| 293 | crate::extract::Feature { |
| 294 | class: Class::area(dict::LAYER_LANDTYPE, crate::schema::kind("lake"), 0), |
| 295 | geometry: tile_build::geom::Geometry::Polygons(vec![vec![ |
| 296 | ring(-120.5, 35.0, 1.0), |
| 297 | // A hole, wound the same way as its exterior, which stage C has to reverse. |
| 298 | ring(-120.2, 35.2, 0.3), |
| 299 | ]]), |
| 300 | name: None, id: tilecodec::mamaps::body::ID_NONE, transit_color: 0, transit_ordinal: 0, transit_lanes: 0, transit_taper: 0, lane_count: 0, |
| 301 | turn_fwd: Vec::new(), |
| 302 | turn_bwd: Vec::new(), |
| 303 | carriageway: tilecodec::mamaps::body::Carriageway::default(), |
| 304 | building: None, |
| 305 | }, |
| 306 | crate::extract::Feature { |
| 307 | class: Class::area(dict::LAYER_LANDTYPE, crate::schema::kind("water"), 0), |
| 308 | geometry: tile_build::geom::Geometry::Polygons(vec![vec![ |
| 309 | ring(-119.0, 36.0, 0.5), |
| 310 | // A hole nowhere near its exterior, which stage C has to drop. |
| 311 | ring(-100.0, 20.0, 0.1), |
| 312 | ]]), |
| 313 | name: None, id: tilecodec::mamaps::body::ID_NONE, transit_color: 0, transit_ordinal: 0, transit_lanes: 0, transit_taper: 0, lane_count: 0, |
| 314 | turn_fwd: Vec::new(), |
| 315 | turn_bwd: Vec::new(), |
| 316 | carriageway: tilecodec::mamaps::body::Carriageway::default(), |
| 317 | building: None, |
| 318 | }, |
| 319 | ]; |
| 320 | let settings = crate::tiler::Settings { |
| 321 | build_id: 1, |
| 322 | scratch: std::env::temp_dir() |
| 323 | .join(format!("mamaps_rings_{}.tilechunks", std::process::id())), |
| 324 | force_chunk_spill_file: false, |
| 325 | dem: crate::dem::Dem::from_grids(14, 17, Vec::new()), |
| 326 | region_links: std::collections::HashMap::new(), |
| 327 | country_zooms: std::collections::HashMap::new(), |
| 328 | }; |
| 329 | let store = crate::store::Store::of(&features).expect("spill"); |
| 330 | let (bytes, stats) = crate::tiler::build(&store, &settings).expect("build"); |
| 331 | |
| 332 | // The archive claims validity, so every tile in it had better be valid. |
| 333 | let header = tilecodec::mamaps::Header::parse(&bytes).expect("header"); |
| 334 | assert!(header.rings_validated(), "the build claims validated rings"); |
| 335 | let mut checked = 0usize; |
| 336 | for (id, _, body) in tilecodec::mamaps::read::read_all(&bytes).expect("read") { |
| 337 | let body = Body::parse(&body).expect("parse"); |
| 338 | for layer in &body.layers { |
| 339 | let problems = check(layer); |
| 340 | assert!(problems.is_empty(), "tile {id}: {problems:?}"); |
| 341 | checked += layer.features.len(); |
| 342 | } |
| 343 | } |
| 344 | assert!(checked > 0, "the build produced no polygons to check"); |
| 345 | // And stage C really had something to do, or this proves nothing. |
| 346 | let corrected: crate::rings::Stats = |
| 347 | stats.iter().fold(crate::rings::Stats::default(), |mut acc, z| { |
| 348 | acc.add(z.rings); |
| 349 | acc |
| 350 | }); |
| 351 | assert!(!corrected.clean(), "stage C corrected nothing: {corrected:?}"); |
| 352 | } |
| 353 | |
| 354 | include!("rings_part1_s1.rs"); |
| 355 | } |