mln-rs-engine 0.0.23

Part of mln-rs, a MapLibre map renderer written in Rust. Depend on mln-rs instead.
Documentation
use mln_style::constants::EXTENT;
use mln_style::subdivision_granularity::SubdivisionGranularityExpression;
use mln_style::tile_id::CanonicalTileID;

use crate::render::globe::{NORTH_POLE_Y, SOUTH_POLE_Y};

const EXTENT_STENCIL_BORDER: i32 = EXTENT / 128;

#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub(crate) struct TileMeshOptions {
    /// Quads per axis; 1 is the plain tile quad. Capped so that every index
    /// fits in 16 bits.
    pub(crate) granularity: u32,
    /// Adds a ring of quads `EXTENT / 128` units outside the tile, for
    /// stencil masks.
    pub(crate) generate_borders: bool,
    /// Replaces the north border with geometry reaching the north pole
    /// vertex.
    pub(crate) extend_to_north_pole: bool,
    pub(crate) extend_to_south_pole: bool,
}

impl Default for TileMeshOptions {
    fn default() -> Self {
        TileMeshOptions {
            granularity: 1,
            generate_borders: false,
            extend_to_north_pole: false,
            extend_to_south_pole: false,
        }
    }
}

/// A grid over a tile on the globe: its vertices' `x, y` in tile units,
/// and its triangles, counter-clockwise in tile space, of 16-bit indexes.
/// A vertex at [`NORTH_POLE_Y`] or [`SOUTH_POLE_Y`] is at the pole.
#[derive(Clone, Debug, Default, PartialEq, Eq)]
pub struct TileMesh {
    pub vertices: Vec<[i16; 2]>,
    pub triangles: Vec<[u16; 3]>,
}

/// The grid that draws the tile `canonical` on the globe, subdivided as
/// `granularity` says at its zoom, reaching the poles where it touches
/// them, and with a border outside the tile if `generate_borders`, for
/// stencil masks.
#[must_use]
pub fn tile_mesh(
    canonical: &CanonicalTileID,
    generate_borders: bool,
    granularity: SubdivisionGranularityExpression,
) -> TileMesh {
    create_tile_mesh(TileMeshOptions {
        granularity: granularity.granularity_for_zoom_level(canonical.z),
        generate_borders,
        extend_to_north_pole: canonical.y == 0,
        extend_to_south_pole: canonical.y == (1u32 << canonical.z) - 1,
    })
}

/// A grid mesh covering one tile, optionally with borders and pole caps.
/// Triangles are counter-clockwise in tile space.
#[must_use]
pub(crate) fn create_tile_mesh(options: TileMeshOptions) -> TileMesh {
    let extra_quads_x: i32 = if options.generate_borders { 2 } else { 0 };
    let extra_quads_y = i32::from(options.extend_to_north_pole || options.generate_borders)
        + i32::from(options.extend_to_south_pole || options.generate_borders);
    // At most 256 vertices per axis, so every index fits in 16 bits.
    let max_granularity = 255 - extra_quads_x.max(extra_quads_y);
    let granularity = options.granularity.clamp(1, max_granularity as u32) as i32;
    let quads_per_axis_x = granularity + extra_quads_x;
    let quads_per_axis_y = granularity + extra_quads_y;
    let vertices_per_axis_x = quads_per_axis_x + 1;
    let vertices_per_axis_y = quads_per_axis_y + 1;
    let offset_x = if options.generate_borders { -1 } else { 0 };
    let offset_y = if options.generate_borders || options.extend_to_north_pole { -1 } else { 0 };
    let end_x = granularity + i32::from(options.generate_borders);
    let end_y = granularity + i32::from(options.generate_borders || options.extend_to_south_pole);

    let mut mesh = TileMesh::default();
    mesh.vertices.reserve(vertices_per_axis_x as usize * vertices_per_axis_y as usize);
    for y in offset_y..=end_y {
        for x in offset_x..=end_x {
            let mut vx = x * EXTENT / granularity;
            if x == -1 {
                vx = -EXTENT_STENCIL_BORDER;
            }
            if x == granularity + 1 {
                vx = EXTENT + EXTENT_STENCIL_BORDER;
            }
            let mut vy = y * EXTENT / granularity;
            if y == -1 {
                vy = if options.extend_to_north_pole { i32::from(NORTH_POLE_Y) } else { -EXTENT_STENCIL_BORDER };
            }
            if y == granularity + 1 {
                vy =
                    if options.extend_to_south_pole { i32::from(SOUTH_POLE_Y) } else { EXTENT + EXTENT_STENCIL_BORDER };
            }
            mesh.vertices.push([vx as i16, vy as i16]);
        }
    }

    mesh.triangles.reserve(quads_per_axis_x as usize * quads_per_axis_y as usize * 2);
    for y in 0..quads_per_axis_y {
        for x in 0..quads_per_axis_x {
            let v0 = (x + y * vertices_per_axis_x) as u16;
            let v1 = ((x + 1) + y * vertices_per_axis_x) as u16;
            let v2 = (x + (y + 1) * vertices_per_axis_x) as u16;
            let v3 = ((x + 1) + (y + 1) * vertices_per_axis_x) as u16;
            mesh.triangles.extend([[v0, v2, v1], [v1, v2, v3]]);
        }
    }
    mesh
}

#[cfg(test)]
mod tests {
    use super::*;

    /// A granularity past what 16-bit indexes can address is capped to the
    /// largest that fits.
    #[test]
    fn granularity_past_the_index_limit_is_capped() {
        let vertex_count = |options: TileMeshOptions| create_tile_mesh(options).vertices.len();
        let plain = |granularity| TileMeshOptions { granularity, ..TileMeshOptions::default() };
        let bordered =
            |granularity| TileMeshOptions { granularity, generate_borders: true, ..TileMeshOptions::default() };
        assert_eq!(256 * 256, vertex_count(plain(255)));
        for granularity in [256, 50_000, u32::MAX] {
            assert_eq!(256 * 256, vertex_count(plain(granularity)));
            assert_eq!(256 * 256, vertex_count(bordered(granularity)));
        }
    }
}