Modern Apps git

← Modern-Apps.git

library:map: gate the fill repair pass on validated rings

Commit
f44f710437e8a8c70b9d66c2840f358a0a9995b5
Author
vayun-mathur <[email protected]>
When
2026-08-28 01:26 UTC (3 months ago)
Parents
ccbafe5

Diff

library/map/src/main/rust/examples/nodrop_probe.rs
diff --git a/library/map/src/main/rust/examples/nodrop_probe.rs b/library/map/src/main/rust/examples/nodrop_probe.rsindex 0df35cc43f..d7794bab59 100644--- a/library/map/src/main/rust/examples/nodrop_probe.rs+++ b/library/map/src/main/rust/examples/nodrop_probe.rs@@ -36,7 +36,7 @@ use map_renderer::tess::fill; fn production_area(rings: &[Vec<(i32, i32)>], extent: u32) -> f64 {     let mut v = Vec::new();     let mut idx = Vec::new();-    fill::tessellate(rings, extent, &mut v, &mut idx);+    fill::tessellate(rings, extent, false, &mut v, &mut idx);     let mut total = 0.0f64;     for tri in idx.chunks_exact(3) {         let p = |i: u32| {
library/map/src/main/rust/examples/probe_archive.rs
diff --git a/library/map/src/main/rust/examples/probe_archive.rs b/library/map/src/main/rust/examples/probe_archive.rsindex f5939fa4a9..53d39e504d 100644--- a/library/map/src/main/rust/examples/probe_archive.rs+++ b/library/map/src/main/rust/examples/probe_archive.rs@@ -247,7 +247,7 @@ fn main() {                                 let want = even_odd_area(rings, layer.extent);                                 let mut v = Vec::new();                                 let mut idx = Vec::new();-                                fill::tessellate(rings, layer.extent, &mut v, &mut idx);+                                fill::tessellate(rings, layer.extent, false, &mut v, &mut idx);                                 let mut got = 0.0f64;                                 for tri in idx.chunks(3) {                                     let p = |i: u32| {@@ -305,7 +305,7 @@ fn main() {         // renderer now takes a `.mamaps` body. The MVT analysis above stays MVT, because it is         // about the upstream archive's own ring structure.         let (converted, _) = tilecodec::mamaps::from_mvt::from_tile(&decoded).expect("converts");-        let mesh = geometry::build(&converted, &layers, tile.z, tile.x, tile.y);+        let mesh = geometry::build(&converted, &layers, tile.z, tile.x, tile.y, false);         let triangles: usize = mesh.meshes.iter().map(|m| m.indices.len() / 3).sum();          // Per-layer detail, plus how much of the tile each fill actually covers. A layer
library/map/src/main/rust/examples/zoom_sweep.rs
diff --git a/library/map/src/main/rust/examples/zoom_sweep.rs b/library/map/src/main/rust/examples/zoom_sweep.rsindex a783d74c4c..72fe7d8ee0 100644--- a/library/map/src/main/rust/examples/zoom_sweep.rs+++ b/library/map/src/main/rust/examples/zoom_sweep.rs@@ -187,7 +187,7 @@ fn main() {                         for rings in &polygons {                             let mut v = Vec::new();                             let mut idx = Vec::new();-                            fill::tessellate(rings, source.extent, &mut v, &mut idx);+                            fill::tessellate(rings, source.extent, false, &mut v, &mut idx);                             rasterise(&v, &idx, mask);                         }                     }
library/map/src/main/rust/src/bridge.rs
diff --git a/library/map/src/main/rust/src/bridge.rs b/library/map/src/main/rust/src/bridge.rsindex 0431f570a6..0d0a39fb83 100644--- a/library/map/src/main/rust/src/bridge.rs+++ b/library/map/src/main/rust/src/bridge.rs@@ -258,6 +258,8 @@ fn spawn_worker(             // silently gets nothing back.             zoom_range.set(archive.header.min_zoom, archive.header.max_zoom);             let layers = style::layers();+            // Read once from the header rather than per tile: it is a property of the archive.+            let rings_validated = archive.header.rings_validated();              loop {                 // Hold the queue lock only long enough to take one tile, never across the@@ -276,6 +278,7 @@ fn spawn_worker(                         tile.z,                         tile.x,                         tile.y,+                        rings_validated,                     )),                     Ok(None) => TileResult::Absent,                     Err(e) => {
library/map/src/main/rust/src/tess/fill.rs
diff --git a/library/map/src/main/rust/src/tess/fill.rs b/library/map/src/main/rust/src/tess/fill.rsindex b77e9293be..7cf14209b2 100644--- a/library/map/src/main/rust/src/tess/fill.rs+++ b/library/map/src/main/rust/src/tess/fill.rs@@ -42,10 +42,20 @@ pub const FLOATS_PER_VERTEX: usize = 2; const MIN_RING_COORDS: usize = 6;  /// Tessellate one polygon: the exterior ring first, then holes, each flat and-/// **closed** as `decode_polygons` returns them.+/// **closed** as the decoder returns them.+///+/// `validated` says the producer already normalised the rings โ€” exactly one exterior, wound+/// consistently, holes strictly inside it and not overlapping each other. A `.mamaps` archive says+/// so in its header; anything else does not.+///+/// **The repair pass is kept either way.** Deleting it would turn a generator bug into an on-device+/// artifact, or an earcut hang, with no diagnostic โ€” and in a debug build it runs regardless and+/// asserts it found nothing to fix, so the claim is continuously verified on the device rather than+/// taken on trust. pub fn tessellate(     rings: &[Vec<(i32, i32)>],     extent: u32,+    validated: bool,     vertices: &mut Vec<f32>,     indices: &mut Vec<u32>, ) {@@ -72,7 +82,20 @@ pub fn tessellate(     // unreferenced vertices behind โ€” the z0 ocean drops enough of them to matter. `offsets`     // maps a ring to where its vertices land, so a group's triangles can be mapped back     // after being renumbered for its own earcut call.-    let groups = polygons(&open);+    let groups = if validated {+        // One group: the exterior and every hole, in the order the producer stated them. Which is+        // what `polygons` would work out anyway, at the cost of an O(holes x outers) containment+        // scan per feature per frame.+        let trusted = vec![(0..open.len()).collect::<Vec<usize>>()];+        debug_assert_eq!(+            polygons(&open),+            trusted,+            "an archive claimed validated rings and the repair pass disagreed",+        );+        trusted+    } else {+        polygons(&open)+    };     let mut offsets = vec![usize::MAX; open.len()];     let mut emitted: Vec<usize> = Vec::with_capacity(open.len());     let mut vertex_count = 0usize;@@ -345,7 +368,7 @@ mod tests {     fn a_closed_ring_loses_its_repeated_vertex() {         let mut v = Vec::new();         let mut idx = Vec::new();-        tessellate(&[square(4096)], 4096, &mut v, &mut idx);+        tessellate(&[square(4096)], 4096, false, &mut v, &mut idx);         assert_eq!(v.len() / FLOATS_PER_VERTEX, 4, "the closing vertex is not a vertex");         assert_eq!(idx.len(), 6);         // The whole tile, so area 1 in tile-normalised units.@@ -355,7 +378,7 @@ mod tests {     #[test]     fn positions_are_tile_normalised() {         let mut v = Vec::new();-        tessellate(&[square(4096)], 4096, &mut v, &mut Vec::new());+        tessellate(&[square(4096)], 4096, false, &mut v, &mut Vec::new());         for f in &v {             assert!(*f >= 0.0 && *f <= 1.0, "{f} is not tile-normalised");         }@@ -496,7 +519,7 @@ mod tests {                 checked += 1;                 let mut v = Vec::new();                 let mut idx = Vec::new();-                tessellate(rings, water.extent, &mut v, &mut idx);+                tessellate(rings, water.extent, false, &mut v, &mut idx);                 let want = expected_area(rings, water.extent);                 let got = covered_area(&v, &idx); @@ -545,7 +568,7 @@ mod tests {          let mut v = Vec::new();         let mut idx = Vec::new();-        tessellate(&rings, SIZE as u32, &mut v, &mut idx);+        tessellate(&rings, SIZE as u32, false, &mut v, &mut idx);          let want = expected_area(&rings, SIZE as u32);         let got = covered_area(&v, &idx);@@ -569,7 +592,7 @@ mod tests {          let mut v = Vec::new();         let mut idx = Vec::new();-        tessellate(&rings, 1000, &mut v, &mut idx);+        tessellate(&rings, 1000, false, &mut v, &mut idx);          // The tile minus the continent, plus the lake back: 1 - 0.36 + 0.04.         let want = expected_area(&rings, 1000);@@ -593,7 +616,7 @@ mod tests {          let mut v = Vec::new();         let mut idx = Vec::new();-        tessellate(&vec![outer, a, b], 1000, &mut v, &mut idx);+        tessellate(&vec![outer, a, b], 1000, false, &mut v, &mut idx);          // One 400x400 hole of a 1000x1000 tile cut out, and only one.         assert!(@@ -620,7 +643,7 @@ mod tests {          let mut v = Vec::new();         let mut idx = Vec::new();-        tessellate(&rings, 1000, &mut v, &mut idx);+        tessellate(&rings, 1000, false, &mut v, &mut idx);          let want = expected_area(&rings, 1000);         assert!((want - 0.8).abs() < 1e-6, "the even-odd region is {want}");@@ -650,7 +673,7 @@ mod tests {          let mut v = Vec::new();         let mut idx = Vec::new();-        tessellate(&vec![outer, crossing], 1000, &mut v, &mut idx);+        tessellate(&vec![outer, crossing], 1000, false, &mut v, &mut idx);          // Dropped, so the fill is exactly the exterior. Keeping it covers 0.8965 instead.         assert!(@@ -671,7 +694,7 @@ mod tests {         for (name, extent, rings) in &polygons {             let mut v = Vec::new();             let mut idx = Vec::new();-            tessellate(rings, *extent, &mut v, &mut idx);+            tessellate(rings, *extent, false, &mut v, &mut idx);             let want = expected_area(rings, *extent);             let got = covered_area(&v, &idx);             // Generous: exact only up to f32 vertex precision.@@ -705,7 +728,7 @@ mod tests {          let mut v = Vec::new();         let mut idx = Vec::new();-        tessellate(&rings, 4096, &mut v, &mut idx);+        tessellate(&rings, 4096, false, &mut v, &mut idx);         let want = expected_area(&rings, 4096);         let got = covered_area(&v, &idx);         assert!(@@ -720,7 +743,7 @@ mod tests {         let hole = vec![(40, 40), (40, 60), (60, 60), (60, 40), (40, 40)];         let mut v = Vec::new();         let mut idx = Vec::new();-        tessellate(&[outer, hole], 100, &mut v, &mut idx);+        tessellate(&[outer, hole], 100, false, &mut v, &mut idx);         // 100x100 minus 20x20, over an extent of 100. Vertices are `f32`, so 0.4 and         // 0.6 are not exact and the tolerance is set by that rather than by the maths.         assert!(@@ -739,6 +762,7 @@ mod tests {         tessellate(             &[vec![(0, 0), (5, 0), (0, 0)], vec![(40, 40), (40, 60), (60, 60), (60, 40), (40, 40)]],             100,+            false,             &mut v,             &mut idx,         );@@ -750,7 +774,7 @@ mod tests {     fn a_degenerate_hole_leaves_the_exterior() {         let mut v = Vec::new();         let mut idx = Vec::new();-        tessellate(&[square(100), vec![(5, 5), (6, 5)]], 100, &mut v, &mut idx);+        tessellate(&[square(100), vec![(5, 5), (6, 5)]], 100, false, &mut v, &mut idx);         assert!((covered_area(&v, &idx) - 1.0).abs() < 1e-6, "the exterior still fills");     } @@ -758,7 +782,7 @@ mod tests {     fn empty_input_emits_nothing() {         let mut v = Vec::new();         let mut idx = Vec::new();-        tessellate(&[], 4096, &mut v, &mut idx);+        tessellate(&[], 4096, false, &mut v, &mut idx);         assert!(v.is_empty() && idx.is_empty());     } @@ -766,10 +790,35 @@ mod tests {     fn a_second_polygon_indices_are_based_at_its_own_vertices() {         let mut v = Vec::new();         let mut idx = Vec::new();-        tessellate(&[square(100)], 100, &mut v, &mut idx);+        tessellate(&[square(100)], 100, false, &mut v, &mut idx);         let first = idx.len();-        tessellate(&[square(100)], 100, &mut v, &mut idx);+        tessellate(&[square(100)], 100, false, &mut v, &mut idx);         assert!(idx[first..].iter().all(|&i| i >= 4), "the second polygon rebases: {:?}", &idx[first..]);         assert_eq!(v.len() / FLOATS_PER_VERTEX, 8);     }+    /// **The gate, and the reason the repair pass is kept.** On a polygon that is already valid the+    /// two paths have to agree exactly: trusting the producer is only a saving if it is not also a+    /// change. Anything else means the gate is a behaviour switch rather than an optimisation.+    #[test]+    fn trusting_a_validated_polygon_matches_repairing_it() {+        // Valid: one exterior, one hole wound the other way, strictly inside it.+        let valid = vec![+            vec![(0, 0), (400, 0), (400, 400), (0, 400), (0, 0)],+            vec![(100, 100), (100, 200), (200, 200), (200, 100), (100, 100)],+        ];+        let mut trusted = (Vec::new(), Vec::new());+        let mut repaired = (Vec::new(), Vec::new());+        tessellate(&valid, 4096, true, &mut trusted.0, &mut trusted.1);+        tessellate(&valid, 4096, false, &mut repaired.0, &mut repaired.1);+        assert_eq!(trusted, repaired, "the gate changed the output on a valid polygon");+        assert!(!trusted.1.is_empty(), "and it drew something");++        // And the hole is really cut out, rather than both paths agreeing on a solid square.+        let mut whole = (Vec::new(), Vec::new());+        tessellate(&[valid[0].clone()], 4096, true, &mut whole.0, &mut whole.1);+        assert!(+            covered_area(&trusted.0, &trusted.1) < covered_area(&whole.0, &whole.1),+            "the hole was not cut out",+        );+    } }
library/map/src/main/rust/src/tile/geometry.rs
diff --git a/library/map/src/main/rust/src/tile/geometry.rs b/library/map/src/main/rust/src/tile/geometry.rsindex bfc0bba7e1..eeed85a1cc 100644--- a/library/map/src/main/rust/src/tile/geometry.rs+++ b/library/map/src/main/rust/src/tile/geometry.rs@@ -45,7 +45,14 @@ pub struct TileMesh { /// So the gate here is the *widest* it could need to be, and the renderer decides what is /// actually visible against the camera's own zoom every frame. Layers outside the window /// are still skipped, which keeps this bounded: a z5 tile does not tessellate buildings.-pub fn build(tile: &Body, layers: &[Layer], z: u8, x: u32, y: u32) -> TileMesh {+pub fn build(+    tile: &Body,+    layers: &[Layer],+    z: u8,+    x: u32,+    y: u32,+    rings_validated: bool,+) -> TileMesh {     let mut meshes = Vec::with_capacity(layers.len());     let deepest = z.saturating_add(ANCESTOR_DEPTH);     let extent = tile.extent as u32;@@ -76,7 +83,7 @@ pub fn build(tile: &Body, layers: &[Layer], z: u8, x: u32, y: u32) -> TileMesh {                     // ring group rather than making every consumer regroup them.                     let rings: Vec<Vec<(i32, i32)>> =                         parts.iter().map(|part| widen(source.points(part))).collect();-                    fill::tessellate(&rings, extent, &mut vertices, &mut indices);+                    fill::tessellate(&rings, extent, rings_validated, &mut vertices, &mut indices);                 }                 LayerKind::Line => {                     // Polygons contribute their outlines too: a lake's shoreline and an@@ -146,7 +153,7 @@ mod tests {         // The published tile carries one `earth` polygon, one `roads` LineString of         // kind = major_road, and two `water` polygons.         let layers = style::layers();-        let mesh = build(&real(), &layers, 11, 339, 770);+        let mesh = build(&real(), &layers, 11, 339, 770, false);          assert!(mesh_for(&mesh, &layers, "earth").is_some(), "the earth polygon tessellates");         assert!(mesh_for(&mesh, &layers, "water").is_some(), "both water polygons tessellate");@@ -178,7 +185,7 @@ mod tests {          // z6 is within reach of z9 (6 + 4 = 10), so the road is tessellated ready for the         // camera to descend onto it. The old tile-zoom gate dropped it here.-        let reaching = build(&real(), &layers, 6, 339, 770);+        let reaching = build(&real(), &layers, 6, 339, 770, false);         assert!(             mesh_for(&reaching, &layers, "roads-major").is_some(),             "a z6 ancestor must carry the roads it will stand in for at z9",@@ -186,7 +193,7 @@ mod tests {          // z4 is not (4 + 4 = 8 < 9), so the window stays bounded and this is not simply         // tessellating everything at every zoom.-        let out_of_reach = build(&real(), &layers, 4, 339, 770);+        let out_of_reach = build(&real(), &layers, 4, 339, 770, false);         assert!(             mesh_for(&out_of_reach, &layers, "roads-major").is_none(),             "the window must stay bounded, or every tile pays for every layer",@@ -196,7 +203,7 @@ mod tests {     #[test]     fn every_mesh_is_well_formed() {         let layers = style::layers();-        let mesh = build(&real(), &layers, 11, 339, 770);+        let mesh = build(&real(), &layers, 11, 339, 770, false);         assert!(!mesh.meshes.is_empty());         for m in &mesh.meshes {             let id = &layers[m.layer_index].id;@@ -226,7 +233,7 @@ mod tests {         // pixels for the dash pattern. A violation of any of those renders a recognisable         // map that is badly wrong, which is exactly the failure this pins down.         let layers = style::layers();-        let mesh = build(&real(), &layers, 11, 339, 770);+        let mesh = build(&real(), &layers, 11, 339, 770, false);         assert!(!mesh.meshes.is_empty());          let mut worst_pos = 0.0f32;@@ -280,7 +287,7 @@ mod tests {         // The clip transform assumes 0..1 within the tile. Clipped geometry overspills         // the edges a little, which is why the bound is generous rather than exact.         let layers = style::layers();-        let mesh = build(&real(), &layers, 11, 339, 770);+        let mesh = build(&real(), &layers, 11, 339, 770, false);         for m in &mesh.meshes {             let stride = match m.kind {                 LayerKind::Fill => fill::FLOATS_PER_VERTEX,@@ -296,7 +303,7 @@ mod tests {     #[test]     fn a_layer_outside_its_zoom_range_is_skipped() {         let layers = style::layers();-        let low = build(&real(), &layers, 11, 339, 770);+        let low = build(&real(), &layers, 11, 339, 770, false);         // buildings is min_zoom 14, and roads-minor is 13; the tile's road is a         // major_road anyway.         assert!(mesh_for(&low, &layers, "roads-minor").is_none());@@ -306,7 +313,7 @@ mod tests {     #[test]     fn a_casing_produces_twice_the_vertices_of_a_plain_stroke() {         let layers = style::layers();-        let mesh = build(&real(), &layers, 11, 339, 770);+        let mesh = build(&real(), &layers, 11, 339, 770, false);         let plain = mesh_for(&mesh, &layers, "roads-major").expect("plain");         let casing = mesh_for(&mesh, &layers, "roads-major-casing").expect("casing");         assert_eq!(@@ -320,7 +327,7 @@ mod tests {     #[test]     fn an_empty_tile_produces_no_meshes() {         let layers = style::layers();-        let mesh = build(&Body::new(4096), &layers, 11, 0, 0);+        let mesh = build(&Body::new(4096), &layers, 11, 0, 0, false);         assert!(mesh.meshes.is_empty());     } @@ -344,7 +351,7 @@ mod tests {             max_zoom: 22,             authored: "water".to_string(),         }];-        let mesh = build(&real(), &outline, 11, 339, 770);+        let mesh = build(&real(), &outline, 11, 339, 770, false);         assert_eq!(mesh.meshes.len(), 1, "the water polygons' outlines stroke");         assert!(!mesh.meshes[0].indices.is_empty());     }@@ -385,7 +392,7 @@ mod tests {          let layers = style::layers();         let body = archive.tile(11, 339, 770).expect("read").expect("present");-        let mesh = build(&body, layers, 11, 339, 770);+        let mesh = build(&body, layers, 11, 339, 770, archive.header.rings_validated());         // The same layers the fixture produces when tessellated directly, so nothing was lost         // between the encoder and the reader.         for id in ["earth", "water", "roads-major", "roads-major-casing"] {