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

nordisk/facett

facett-graph3d/tests/node_ink_budget.rs

//! **A cloud's node discs must not out-cover the pane they are drawn on.**
//!
//! `facett-graph3d` sized every node disc at `3.0 + (z + 1.0) * 2.5` — 3 to 8 px — no
//! matter how many there were. A 40-node call graph and korp's 89 130-node Sweden road
//! network drew the same disc, and at the large end that is an eleven-fold
//! over-coverage: 89 130 × π × 5.5² ≈ 8.5 Mpx² of ink on a 948 × 796 pane of 754 608 px.
//! The GPU lane is emissive and accumulates, so the world shot came out a solid white
//! silhouette (MEASURED oden 2026-09-03: 11.6 % of the canvas above 235/255 on every
//! channel, `~/scratch/ge-lane-proof/world-shot/sweden-3d-speed-z-flat.png`).
//!
//! The budget is a SHRINK that is exactly `1.0` while the discs already fit, so nothing
//! that fits ever moves — which is what these guards are for.

use facett_graph3d::{ink_shrink, NODE_INK_BUDGET};

const BASE_R: f32 = 5.5;

fn ink(n: usize, r: f32) -> f32 {
    n as f32 * std::f32::consts::PI * r * r
}

#[test]
fn a_cloud_that_already_fits_is_not_touched() {
    // The sizes every existing golden and snapshot is drawn at.
    for n in [0, 1, 2, 40, 200, 1_000, 2_000] {
        assert_eq!(ink_shrink(n, 948.0, 796.0, BASE_R), 1.0, "n = {n} fits and must not shrink");
    }
    // Degenerate inputs are a no-op, never a divide.
    assert_eq!(ink_shrink(100, 0.0, 796.0, BASE_R), 1.0);
    assert_eq!(ink_shrink(100, 948.0, 796.0, 0.0), 1.0);
}

#[test]
fn a_crowd_is_shrunk_to_the_budget_and_no_further() {
    let (w, h) = (948.0_f32, 796.0_f32);
    let n = 89_130;
    let s = ink_shrink(n, w, h, BASE_R);
    assert!(s < 1.0, "89 130 discs do not fit a 948 x 796 pane");
    // Exactly at the budget: the shrunk ink is the budget, not less.
    let after = ink(n, BASE_R * s) / (w * h);
    assert!(
        (after - NODE_INK_BUDGET).abs() < 1e-3,
        "shrunk coverage is {after:.4}, the budget is {NODE_INK_BUDGET}"
    );
    // Before it was eleven times the pane.
    let before = ink(n, BASE_R) / (w * h);
    assert!(before > 10.0, "the unshrunk cloud covered {before:.1}x the pane");
}

#[test]
fn the_shrink_is_monotone_and_scales_as_the_square_root() {
    let (w, h) = (948.0_f32, 796.0_f32);
    let mut last = 1.0_f32;
    for n in [4_000, 8_000, 20_000, 50_000, 100_000] {
        let s = ink_shrink(n, w, h, BASE_R);
        assert!(s <= last, "a bigger cloud never gets bigger discs ({n})");
        last = s;
    }
    // Four times the nodes, half the radius — the area law, not a fudge.
    let a = ink_shrink(25_000, w, h, BASE_R);
    let b = ink_shrink(100_000, w, h, BASE_R);
    assert!((a / b - 2.0).abs() < 1e-3, "4x the nodes must halve the radius: {a} vs {b}");
    // A bigger pane holds more before shrinking.
    assert!(ink_shrink(20_000, 1896.0, 1592.0, BASE_R) > ink_shrink(20_000, w, h, BASE_R));
}