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_never_outrises_the_roofs.rs

//! **The land must never float above the rooftops — at ANY scene scale.**
//!
//! `GROUND_LIFT` is an absolute design-unit constant; a building's rise is
//! `height_m · du_per_m`, i.e. metric and therefore a function of how much ground the
//! scene spans. Two unrelated quantities decide which one is taller, so the answer
//! changes with the viewport — and past a few hundred metres of span the LAND WINS and
//! swallows the buildings. That is the recurring *"the ground plane has risen and partly
//! engulfs the buildings"* report.
//!
//! MEASURED on the real Sweden extract at central Stockholm (korp's own served viewport,
//! 2026-08-02): span 55 688 m, 641 buildings, median rise **2.05e-4** design units
//! against a ground-cover cap at **1.30e-3** — the land floats **6.3×** over the median
//! rooftop, and the tallest building in the scene does not clear the road ribbons.
//!
//! This asserts the ORDER of the extruded MESH's y coordinates (applied output, not a
//! knob round-trip): every flat ground-cover cap and every road ribbon vertex must sit
//! strictly BELOW the lowest building roof, over a sweep of real-world scene spans from
//! a 300 m block to a 200 km region.

use facett_map::Way;
use facett_map3d::extrude;

mod common;

// The fixture is shared with `region_span_buildings_rise`, which asks the same question
// from the other side (does the RISE threshold still mean a metre at region span?). Two
// copies of "a 12 m building at 18.07 E" is exactly how the two halves of a scale bug
// come to be measured against two different scales — LAW 5.
use common::town;

/// `(lowest building roof y, highest ground-layer y)` from the EXTRUDED MESH.
///
/// The two sets are separated by the mesh's own structure, not by a guessed threshold:
/// a flat cover cap / road ribbon is a single-sided horizontal patch whose vertices all
/// share one y, while a risen prism carries a bottom rim at `y == 0` and a top cap
/// above it. So classify a triangle by its own vertices: a horizontal triangle whose y
/// lies in the ground band is ground; the maximum y over the whole mesh that belongs to
/// a prism cap is the roofline. We take the simple, robust route: `roof_min` is the
/// smallest positive cap y among the building rises the extruder reports, and
/// `ground_max` is the largest y over every triangle that is NOT part of a prism —
/// derived by extruding the SAME scene twice, once with the buildings and once without,
/// so the ground set is exactly the difference.
fn roof_and_ground(span_m: f64) -> (f32, f32) {
    let all = town(span_m);
    let ground_only: Vec<Way> = all.iter().filter(|w| w.kind != "building").cloned().collect();

    let ground_mesh = extrude::extrude_ways(&ground_only, 0.18);
    let ground_max = ground_mesh.positions.iter().fold(f32::MIN, |a, p| a.max(p[1]));

    // The building roofline, from the extruder's OWN height stats over the full scene
    // (the same `building_design_height` the mesh caps are built from).
    let stats = extrude::building_height_stats(&all, 0.18);
    (stats.min, ground_max)
}

/// **RED-when-broken.** Over a sweep of real scene spans, the highest ground-layer
/// vertex must stay strictly under the LOWEST building roof. Before the ground band was
/// put on the buildings' own metric scale this failed from ~1 km of span upward.
#[test]
fn no_ground_layer_ever_reaches_the_lowest_roof() {
    let mut worst = (0.0f64, 0.0f32);
    for span_m in [300.0f64, 1_000.0, 3_000.0, 10_000.0, 55_688.0, 200_000.0] {
        let (roof_min, ground_max) = roof_and_ground(span_m);
        assert!(roof_min > 0.0, "span {span_m} m: the scene must actually have risen buildings");
        assert!(ground_max > 0.0, "span {span_m} m: the ground layers must clear the terrain plane");
        let ratio = f64::from(ground_max / roof_min);
        if ratio > worst.1 as f64 {
            worst = (span_m, ratio as f32);
        }
        assert!(
            ground_max < roof_min,
            "span {span_m} m: the ground floats OVER the lowest roof — ground_max {ground_max:.3e} \
             vs roof_min {roof_min:.3e} ({ratio:.2}× the roof). The band and the building heights \
             are on two different scales.",
        );
    }
    // NON-VACUITY: the sweep must have produced a real, non-degenerate margin — and the
    // ground must still CLEAR the terrain (a band collapsed to zero would pass the
    // assert above while re-introducing the z-fight the band exists to stop).
    assert!(worst.1 > 0.0, "the sweep measured nothing");
    assert!(
        worst.1 < 1.0,
        "worst span {} m sat at {:.2}× the roof",
        worst.0,
        worst.1
    );
}

/// The band itself: metric where the scene has a world scale, the legacy absolute band
/// where it does not, and never outside `[MIN_GROUND_BAND, GROUND_LIFT]`.
///
/// The `None` arm is what keeps every synthetic fixture (the rabbit, the unit tests)
/// byte-for-byte identical — a scene with no metres has no metric band to compute.
#[test]
fn the_band_is_metric_where_there_is_a_metre_and_legacy_where_there_is_not() {
    assert_eq!(
        extrude::ground_band(None),
        extrude::GROUND_LIFT,
        "no world scale ⇒ the legacy absolute band, so synthetic scenes do not move",
    );
    // A 3 km town: 0.4 m of kerb, well inside the clamp.
    let dpm_town = extrude::DESIGN_FILL / 3_000.0;
    let band_town = extrude::ground_band(Some(dpm_town));
    assert!(
        (band_town - extrude::GROUND_BAND_METRES * dpm_town).abs() < 1e-12,
        "inside the clamp the band IS the metric conversion: {band_town:.3e}",
    );
    assert!(band_town < extrude::GROUND_LIFT, "a town's band is under the legacy constant");
    // A 200 m block: the metric band would exceed the legacy one → clamped down to it.
    let dpm_block = extrude::DESIGN_FILL / 200.0;
    assert_eq!(
        extrude::ground_band(Some(dpm_block)),
        extrude::GROUND_LIFT,
        "the band never grows past the historical constant",
    );
    // A whole region: the depth-safety floor holds.
    let dpm_region = extrude::DESIGN_FILL / 1_000_000.0;
    assert_eq!(
        extrude::ground_band(Some(dpm_region)),
        extrude::MIN_GROUND_BAND,
        "the band never collapses below depth safety",
    );
    // …and the ORDERING inside the band survives the rescale: cover strictly under road.
    let r = [(0.1f32, 0.2f32), (0.3, 0.2), (0.3, 0.4), (0.1, 0.4)];
    for band in [extrude::GROUND_LIFT, band_town, extrude::MIN_GROUND_BAND] {
        let cover = extrude::layer_lift_in(&r, extrude::GroundLayer::Cover, band);
        let road = extrude::layer_lift_in(&r, extrude::GroundLayer::Road, band);
        assert!(cover > 0.0, "cover clears the terrain at band {band:.3e}");
        assert!(road > cover, "a road still wins over the cover it crosses at band {band:.3e}");
        assert!(road <= band * 1.22, "the road stays inside its band at {band:.3e}");
    }
}