geolith-tiles-builder 0.4.2

Builds tiles from OpenStreetMap and Overture Maps: protomaps-schema vector PMTiles, Terrain-RGB, raster archives and 3D Tiles from Gaussian splats
Documentation
//! Gaussian splat pipeline: 3DGS PLY to OGC 3D Tiles.
//!
//! Reads a trained 3D Gaussian Splatting PLY, subdivides it into an octree of
//! streamable tiles, and emits an OGC 3D Tiles tileset whose GLB payloads carry
//! `KHR_gaussian_splatting` with `KHR_gaussian_splatting_compression_spz_2`.

pub mod glb;
pub mod ply;
pub mod spz;
pub mod tileset;

pub use ply::{SplatCloud, read_splat_ply};
pub use spz::encode_spz;
pub use tileset::{SplatTilesetOptions, SplatTilesetStats, write_splat_tileset};

/// What one splat tileset build reads and writes.
#[derive(Debug, Clone)]
pub struct SplatJob {
    /// The trained Gaussian splat PLY.
    pub ply: std::path::PathBuf,
    /// The tileset directory to write.
    pub output: std::path::PathBuf,
    /// Drop splats beyond this percentile of the distance from the centre;
    /// 0 keeps all.
    pub crop_percentile: f64,
    /// Scale applied to the splats.
    pub scale: f32,
    /// Coverage boost scale; 0 turns it off.
    pub coverage_boost_scale: f32,
    /// Also write a standalone `scene.spz`.
    pub emit_scene_spz: bool,
    /// The tileset options.
    pub options: SplatTilesetOptions,
}

/// Build an OGC 3D Tiles tileset from a trained Gaussian splat PLY.
///
/// # Errors
///
/// Returns an error when the PLY cannot be read or the tileset cannot be written.
pub fn run(job: &SplatJob) -> Result<(), crate::TilesError> {
    tracing::info!(ply = %job.ply.display(), "reading 3DGS PLY");
    let mut cloud = read_splat_ply(&job.ply).map_err(|source| crate::TilesError::ReadPly {
        path: job.ply.clone(),
        source,
    })?;
    tracing::info!(
        splats = cloud.len(),
        sh_degree = cloud.sh_degree,
        "splat cloud loaded"
    );

    if job.crop_percentile > 0.0 {
        let before = cloud.len();
        cloud = cloud.crop_percentile(job.crop_percentile)?;
        tracing::info!(
            kept = cloud.len(),
            dropped = before - cloud.len(),
            percentile = job.crop_percentile,
            "cropped far-field splats"
        );
    }
    cloud.rescale(job.scale);
    if job.coverage_boost_scale > 0.0 {
        cloud.coverage_boost(job.coverage_boost_scale);
        tracing::info!(boost = job.coverage_boost_scale, "applied coverage boost");
    }

    let options = &job.options;
    let stats = write_splat_tileset(&cloud, &job.output, options)?;
    tracing::info!(
        output = %job.output.display(),
        tiles = stats.tiles,
        splats = stats.splats,
        max_depth = stats.max_depth,
        "3D Tiles tileset written"
    );

    if job.emit_scene_spz {
        let scene = job.output.join("scene.spz");
        let mut centered = cloud.clone();
        if let Some((min, max)) = cloud.bounds() {
            let center = [
                (min[0] + max[0]) / 2.0,
                (min[1] + max[1]) / 2.0,
                (min[2] + max[2]) / 2.0,
            ];
            for position in &mut centered.positions {
                position[0] -= center[0];
                position[1] -= center[1];
                position[2] -= center[2];
            }
        }
        crate::fsio::write_file(&scene, encode_spz(&centered, options.antialiased)?)?;
        tracing::info!(path = %scene.display(), "standalone scene.spz written");
    }

    Ok(())
}