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/label_gate_reads_the_altitude.rs

//! **The label ladder and the LOD tiers must read the SAME altitude** — lane ST,
//! gfx3d-S4.
//!
//! `Scene3d::label_shows_at` turns a design-unit distance into metres through
//! `lod::altitude_metres`, whose own parameter is named `distance_to_surface_du`. The
//! tier ladder feeds it `cam.distance_to_surface()` (`lib.rs:1471`, `lib.rs:4423`). The
//! street-label painter fed it `cam.distance` — the raw **orbit radius** — and
//! `label_shows_at`'s doc says it uses *"the same lever the altitude tiers use"*, which
//! was the thing that was not true.
//!
//! On a tilted camera those two are different numbers. The radius is the hypotenuse and
//! the surface distance is its vertical leg, so at low elevation the radius is
//! arbitrarily larger — and the painter therefore believed it was much higher than the
//! tier ladder did, and hid street labels the ladder had already switched on.
//!
//! Run: `cargo test -p facett-map3d --test label_gate_reads_the_altitude`

use facett_map3d::camera::OrbitCamera;

/// **The two distances diverge, and by how much.** The premise of the whole row: if
/// `distance` and `distance_to_surface()` agreed there would be nothing to fix, so the
/// gap is measured here rather than asserted about.
#[test]
fn the_orbit_radius_and_the_surface_distance_are_different_numbers() {
    let mut cam = OrbitCamera { distance: 1000.0, ..Default::default() };

    // Straight down: the radius IS the vertical drop (up to the ground-height lookup).
    cam.elevation = std::f32::consts::FRAC_PI_2;
    let top_down = cam.distance_to_surface();
    assert!(
        (top_down - cam.distance).abs() / cam.distance < 0.05,
        "looking straight down the two agree within 5%: radius {} vs surface {top_down}",
        cam.distance
    );

    // Tilted 5° above the horizon — a car-navigator pose, which is exactly when street
    // labels matter. The vertical leg is now a small fraction of the radius.
    cam.elevation = 5.0_f32.to_radians();
    let shallow = cam.distance_to_surface();
    let ratio = cam.distance / shallow.max(1e-6);
    assert!(
        ratio > 5.0,
        "at 5° above the horizon the orbit radius must be many times the surface \
         distance — radius {} vs surface {shallow} (ratio {ratio:.1}×). If this ever \
         fails the two have become the same quantity and this whole row is moot.",
        cam.distance
    );
    eprintln!(
        "[gfx3d-S4] measured at 5° elevation: radius {:.1} du vs surface {shallow:.1} du \
         = {ratio:.1}× apart",
        cam.distance
    );
}

/// **The painter's gate agrees with the tier ladder** — the property the fix installs.
///
/// Both are read off the SAME camera through the scene's own accessors, so this cannot
/// pass by one side having been re-derived here.
#[test]
fn the_label_gate_and_the_tier_ladder_read_one_altitude() {
    let mut scene = facett_map3d::Map3D::synthetic("gfx3d-s4");
    // A tilted, low camera — the pose where the two distances are furthest apart.
    scene.cam.distance = 1000.0;
    scene.cam.elevation = 5.0_f32.to_radians();

    let ladder_m = scene.altitude_m();
    let cam = scene.render_camera();
    let gate_m = facett_map3d::altitude_metres(
        cam.distance_to_surface(),
        scene.metres_per_design_unit(),
    );
    assert!(
        (ladder_m - gate_m).abs() < 1e-3,
        "`altitude_m()` (the tier ladder) reads {ladder_m} m while the surface distance \
         converts to {gate_m} m — they must be one number"
    );

    // …and the WRONG one is measurably different, so the assertion above is not
    // vacuous: feeding the orbit radius gives a different altitude entirely.
    let radius_m =
        facett_map3d::altitude_metres(cam.distance, scene.metres_per_design_unit());
    assert!(
        (radius_m - ladder_m).abs() > 1.0,
        "the orbit radius converts to {radius_m} m against the ladder's {ladder_m} m — \
         if these agreed there would be no defect to guard"
    );
}