geolith-cli 0.3.1

Rust vector tile generator — OSM + Overture Maps → protomaps PMTiles
Documentation
#![allow(clippy::unwrap_used)] // tests and benches may unwrap (AGENTS.md).

//! Integration tests for the Gaussian splat pipeline.
//!
//! Writes binary 3DGS PLY fixtures in the layout the INRIA reference trainer,
//! gsplat and Nerfstudio `splatfacto` emit, then drives
//! [`geolith_cli::run_splat_tiles`] end-to-end and asserts on the artifacts a
//! 3D Tiles renderer consumes: `tileset.json`, `tiles/*.glb` and `scene.spz`.

use std::io::{Read, Write};
use std::path::Path;

use geolith_cli::cli::SplatTilesArgs;

const MINIMAL_PROPERTIES: [&str; 14] = [
    "x", "y", "z", "f_dc_0", "f_dc_1", "f_dc_2", "opacity", "scale_0", "scale_1", "scale_2",
    "rot_0", "rot_1", "rot_2", "rot_3",
];

/// SPZ packet magic, `NGSP` in little-endian byte order.
const SPZ_MAGIC: u32 = 0x5053_474e;

fn write_ply(vertex_count: usize) -> tempfile::NamedTempFile {
    let mut file = tempfile::NamedTempFile::new().expect("temp file");
    let mut header = String::from("ply\nformat binary_little_endian 1.0\n");
    header.push_str(&format!("element vertex {vertex_count}\n"));
    for property in MINIMAL_PROPERTIES {
        header.push_str(&format!("property float {property}\n"));
    }
    header.push_str("end_header\n");
    file.write_all(header.as_bytes()).expect("header");

    for index in 0..vertex_count {
        let offset = index as f32;
        let vertex = [
            offset,
            offset * 0.5,
            offset * 0.25,
            0.4,
            0.5,
            0.6,
            1.1,
            -2.0,
            -2.1,
            -2.2,
            1.0,
            0.0,
            0.0,
            0.0,
        ];
        for value in vertex {
            file.write_all(&value.to_le_bytes()).expect("vertex");
        }
    }
    file.flush().expect("flush");
    file
}

fn splat_args(ply: &Path, output: &Path) -> SplatTilesArgs {
    SplatTilesArgs {
        ply: ply.to_path_buf(),
        output: output.to_path_buf(),
        longitude: 7.9797,
        latitude: 45.9756,
        altitude: 2250.0,
        max_splats_per_tile: 4,
        max_depth: 2,
        root_geometric_error: 64.0,
        antialiased: false,
        emit_scene_spz: true,
        crop_percentile: 0.0,
        scale: 1.0,
        coverage_boost_scale: 0.0,
    }
}

fn gunzip(bytes: &[u8]) -> Vec<u8> {
    let mut decoder = flate2::read::GzDecoder::new(bytes);
    let mut raw = Vec::new();
    decoder.read_to_end(&mut raw).expect("gunzip");
    raw
}

#[test]
fn writes_a_tileset_with_glb_tiles_and_scene_spz() {
    let ply = write_ply(32);
    let dir = tempfile::tempdir().expect("tempdir");
    let output = dir.path().join("tileset");

    geolith_cli::run_splat_tiles(&splat_args(ply.path(), &output)).expect("splat pipeline runs");

    let tileset: serde_json::Value =
        serde_json::from_slice(&std::fs::read(output.join("tileset.json")).expect("json"))
            .expect("valid tileset json");
    assert_eq!(tileset["asset"]["version"], "1.1");
    assert_eq!(tileset["root"]["refine"], "REPLACE");
    assert_eq!(
        tileset["root"]["transform"]
            .as_array()
            .expect("transform")
            .len(),
        16,
        "the root carries a 4x4 ENU-to-ECEF matrix"
    );

    let root_glb = std::fs::read(output.join("tiles/0.glb")).expect("root tile");
    assert_eq!(&root_glb[..4], b"glTF", "tiles are binary glTF");

    let scene = std::fs::read(output.join("scene.spz")).expect("scene.spz");
    assert_eq!(
        &scene[..2],
        &[0x1f, 0x8b],
        "scene.spz is a gzip stream per the SPZ spec"
    );
}

#[test]
fn every_declared_tile_has_a_glb_on_disk() {
    let ply = write_ply(64);
    let dir = tempfile::tempdir().expect("tempdir");
    let output = dir.path().join("tileset");

    geolith_cli::run_splat_tiles(&splat_args(ply.path(), &output)).expect("splat pipeline runs");

    let written = std::fs::read_dir(output.join("tiles"))
        .expect("tiles dir")
        .count();
    assert!(written >= 1, "at least the root tile is written");
    for id in 0..written {
        let glb = std::fs::read(output.join(format!("tiles/{id}.glb")))
            .unwrap_or_else(|e| panic!("tile {id} missing: {e}"));
        assert_eq!(&glb[0..4], b"glTF", "tile {id} is binary glTF");
    }
}

#[test]
fn skips_scene_spz_when_not_requested() {
    let ply = write_ply(8);
    let dir = tempfile::tempdir().expect("tempdir");
    let output = dir.path().join("tileset");

    let mut args = splat_args(ply.path(), &output);
    args.emit_scene_spz = false;
    geolith_cli::run_splat_tiles(&args).expect("splat pipeline runs");

    assert!(output.join("tileset.json").exists());
    assert!(
        !output.join("scene.spz").exists(),
        "scene.spz is opt-in via --emit-scene-spz"
    );
}

#[test]
fn the_standalone_scene_carries_the_spz_magic() {
    let ply = write_ply(16);
    let dir = tempfile::tempdir().expect("tempdir");
    let output = dir.path().join("tileset");

    geolith_cli::run_splat_tiles(&splat_args(ply.path(), &output)).expect("splat pipeline runs");

    let raw = gunzip(&std::fs::read(output.join("scene.spz")).expect("scene.spz"));
    assert_eq!(
        &raw[..4],
        &SPZ_MAGIC.to_le_bytes(),
        "the packet carries the SPZ magic"
    );
}

#[test]
fn honours_the_antialiased_flag() {
    let ply = write_ply(8);
    let dir = tempfile::tempdir().expect("tempdir");
    let output = dir.path().join("tileset");

    let mut args = splat_args(ply.path(), &output);
    args.antialiased = true;
    geolith_cli::run_splat_tiles(&args).expect("splat pipeline runs");

    let raw = gunzip(&std::fs::read(output.join("scene.spz")).expect("scene.spz"));
    assert_eq!(
        raw[14] & 0x1,
        1,
        "the antialiased flag occupies bit 0 of the flags byte"
    );
}

#[test]
fn propagates_a_ply_read_failure() {
    let dir = tempfile::tempdir().expect("tempdir");
    let args = splat_args(
        Path::new("/nonexistent/scene.ply"),
        &dir.path().join("tileset"),
    );
    assert!(
        geolith_cli::run_splat_tiles(&args).is_err(),
        "missing PLY is an error"
    );
}

#[test]
fn rejects_an_empty_cloud() {
    let ply = write_ply(0);
    let dir = tempfile::tempdir().expect("tempdir");
    let err = geolith_cli::run_splat_tiles(&splat_args(ply.path(), &dir.path().join("tileset")))
        .expect_err("an empty cloud cannot be tiled");
    assert!(format!("{err}").contains("empty splat cloud"), "{err}");
}

#[test]
fn crop_and_scale_flags_shrink_the_scene() {
    let ply = write_ply(64);
    let dir = tempfile::tempdir().expect("tempdir");
    let output = dir.path().join("tileset");

    let mut args = splat_args(ply.path(), &output);
    args.crop_percentile = 5.0;
    args.scale = 0.5;
    geolith_cli::run_splat_tiles(&args).expect("crop + scale pipeline runs");

    let tileset: serde_json::Value =
        serde_json::from_slice(&std::fs::read(output.join("tileset.json")).expect("json"))
            .expect("valid tileset json");
    let root_box = tileset["root"]["boundingVolume"]["box"]
        .as_array()
        .expect("box");
    let half_x = root_box[3].as_f64().expect("half x");

    let full = splat_args(ply.path(), &dir.path().join("full"));
    geolith_cli::run_splat_tiles(&full).expect("uncropped pipeline runs");
    let full_tileset: serde_json::Value =
        serde_json::from_slice(&std::fs::read(dir.path().join("full/tileset.json")).expect("json"))
            .expect("valid tileset json");
    let full_half_x = full_tileset["root"]["boundingVolume"]["box"][3]
        .as_f64()
        .expect("half x");

    assert!(
        half_x < full_half_x * 0.6,
        "crop 5% + scale 0.5 must shrink the root box: {half_x} vs {full_half_x}"
    );
}

#[test]
fn run_command_dispatches_the_splat_verb() {
    let ply = write_ply(8);
    let dir = tempfile::tempdir().expect("tempdir");
    let output = dir.path().join("dispatched");

    let command = geolith_cli::cli::parse_from([
        "geolith",
        "splat-tiles",
        "--ply",
        ply.path().to_str().expect("utf-8 path"),
        "--output",
        output.to_str().expect("utf-8 path"),
        "--longitude",
        "7.9797",
        "--latitude",
        "45.9756",
    ])
    .expect("splat-tiles parses");

    geolith_cli::run_command(command).expect("the verb reaches run_splat_tiles");
    assert!(
        output.join("tileset.json").exists(),
        "dispatch produced the tileset"
    );
}