Your browser sucks, JavaScript disabled! Gunnar's console needs JavaScript and WebAssembly. Git over SSH on port 2222 does not.

nordisk/facett

facett-map3d/tests/ground_roads_proof.rs

//! **Invisible-roads oracle (headless, CPU painter).** The Garmin navigator's #1
//! issue (`.nornir/osm-3d-gaps.md` §F): the CPU painter used to back-face-cull the
//! flat, +Y ground-plane road/route ribbons, so roads were DATA-proven on the mesh
//! but never pixel-drawn from an above-ground camera. A flat up-facing ribbon has no
//! "back" from the map's always-above camera — its screen-winding sign merely flips
//! with the viewing hemisphere — so [`shade_mesh_faces`] now keeps strongly
//! up-facing faces (`ny > GROUND_FACE_UP_NY`) regardless of screen winding.
//!
//! This proves it as DATA on the shaded-face list with no GPU and no golden: adding
//! a road to a town yields MORE visible ground-plane faces from BOTH an oblique and
//! an overhead (Top) camera. RED-when-culled: if the ground-plane cull regresses, the
//! road contributes zero visible faces and every assertion below fails.
//!
//! Run: `cargo test -p facett-map3d --test ground_roads_proof [--features testmatrix]`.

use facett_core::Theme;
use facett_map::Way;
use facett_map3d::{Map3D, ViewPreset};

fn building(id: i64, ring: &[(f64, f64)]) -> Way {
    let tags = vec![("building".to_string(), "yes".to_string())];
    Way::from_lonlat(id, ring, &tags, &Theme::default()).expect("a building way")
}

/// The deterministic 5×5 Sigtuna-ish town (the same footprints the golden uses).
fn town() -> Vec<Way> {
    let mut ways = Vec::new();
    let mut id = 1;
    for r in 0..5 {
        for c in 0..5 {
            let bx = 17.70 + c as f64 * 0.0015;
            let by = 59.61 + r as f64 * 0.0015;
            ways.push(building(
                id,
                &[(bx, by), (bx + 0.0008, by), (bx + 0.0008, by + 0.0008), (bx, by + 0.0008), (bx, by)],
            ));
            id += 1;
        }
    }
    ways
}

/// A primary road threading through the town (a flat ground-plane ribbon).
fn road() -> Way {
    let tags = vec![("highway".to_string(), "primary".to_string())];
    Way::from_lonlat(999, &[(17.700, 59.6105), (17.706, 59.6105), (17.706, 59.616)], &tags, &Theme::default())
        .expect("a road way")
}

/// Count the visible shaded faces `Map3D` draws from `preset`, framed on the town.
fn visible_faces(ways: &[Way], preset: ViewPreset) -> usize {
    let mut m = Map3D::from_ways("proof", ways, 0.22);
    m.spin = false;
    let tgt = m.render_camera().target;
    m.cam.target = tgt;
    m.cam.goal_target = tgt;
    m.cam.apply_preset(preset);
    m.cam.distance = 0.9;
    m.cam.goal_distance = 0.9;
    m.cam.settle();
    m.shade_faces(egui::pos2(300.0, 300.0), 300.0, egui::Color32::BLACK).len()
}

#[test]
fn ground_plane_roads_draw_from_above() {
    let mut town_with_road = town();
    town_with_road.push(road());

    for preset in [ViewPreset::Oblique, ViewPreset::Top] {
        let town_only = visible_faces(&town(), preset);
        let with_road = visible_faces(&town_with_road, preset);
        let contributed = with_road as i64 - town_only as i64;
        eprintln!("[{preset:?}] town={town_only} town+road={with_road} road_contributed={contributed}");
        // RED-when-culled: the flat ground-plane road ribbon draws real pixels from an
        // above-ground camera. Was 0 (fully culled) before the §F fix.
        assert!(
            contributed > 0,
            "[{preset:?}] the ground-plane road ribbon must draw visible faces from above \
             (got {contributed}); a 0 here is the invisible-roads regression (osm-3d-gaps §F)"
        );
    }

    #[cfg(feature = "testmatrix")]
    {
        let mut twr = town();
        twr.push(road());
        let top_town = visible_faces(&town(), ViewPreset::Top);
        let top_road = visible_faces(&twr, ViewPreset::Top);
        facett_core::testmatrix::emit(
            "facett-map3d",
            "ground_plane_roads_draw_from_above",
            top_road > top_town,
            &format!("top_town_faces={top_town} top_town+road_faces={top_road}"),
        );
    }
}