mln-rs-engine 0.0.23

Part of mln-rs, a MapLibre map renderer written in Rust. Depend on mln-rs instead.
Documentation
use super::*;

fn c(z: u8, x: u32, y: u32) -> CanonicalTileID {
    CanonicalTileID::new(z, x, y)
}

fn o(z: u8, x: u32, y: u32) -> OverscaledTileID {
    OverscaledTileID::from_zxy(z, x, y)
}

fn ow(overscaled_z: u8, wrap: i16, canonical: CanonicalTileID) -> OverscaledTileID {
    OverscaledTileID::new(overscaled_z, wrap, canonical)
}

fn u(z: u8, x: i64, y: i64) -> UnwrappedTileID {
    UnwrappedTileID::new(z, x, y)
}

fn uw(wrap: i16, canonical: CanonicalTileID) -> UnwrappedTileID {
    UnwrappedTileID::with_wrap(wrap, canonical)
}

#[test]
fn overscaled_ids_order_by_zoom_then_overscaling() {
    assert_eq!(ow(4, 0, c(4, 2, 3)), o(4, 2, 3));
    for other in [ow(5, 0, c(4, 2, 3)), o(4, 2, 4), o(4, 3, 3), o(5, 2, 3)] {
        assert_ne!(o(4, 2, 3), other);
    }
    for overscaled_z in 5..=7 {
        assert!(o(4, 2, 3) < ow(overscaled_z, 0, c(4, 2, 3)));
    }
}

#[test]
fn an_overscaled_id_scales_overscales_and_unwraps() {
    // (overscaled zoom, overscale factor)
    for (z, factor) in [(4, 1), (5, 2), (6, 4), (7, 8)] {
        assert_eq!(ow(z, 0, c(4, 2, 3)).overscale_factor(), factor);
    }
    assert_eq!(ow(31, 0, c(0, 0, 0)).overscale_factor(), 2_147_483_648);

    // (zoom, scaled)
    let scaled = [(0, c(0, 0, 0)), (1, c(1, 0, 0)), (2, c(2, 0, 0)), (3, c(3, 1, 1)), (4, c(4, 2, 3))];
    for (z, canonical) in scaled {
        assert_eq!(o(4, 2, 3).scaled_to(z), ow(z, 0, canonical));
    }
    for z in [5, 8, 32] {
        assert_eq!(o(4, 2, 3).scaled_to(z), ow(z, 0, c(4, 2, 3)));
        assert_eq!(u(4, 2, 3).overscale_to(z), ow(z, 0, c(4, 2, 3)));
    }

    for (z, wrap) in [(4, 0), (4, -1), (4, 1), (5, 0), (5, -1), (5, 1)] {
        assert_eq!(ow(z, wrap, c(4, 2, 3)).to_unwrapped(), uw(wrap, c(4, 2, 3)));
    }
}

#[test]
fn an_overscaled_id_is_a_child_of_its_ancestors_in_its_world() {
    let tile = |z, wrap| ow(z, wrap, c(4, 2, 3));
    // (child, parent, is a child)
    let cases = [
        (o(2, 0, 0), o(3, 1, 1), false),
        (o(3, 1, 1), o(3, 1, 1), false),
        (o(4, 2, 3), o(3, 1, 1), true),
        (tile(5, 0), o(3, 1, 1), true),
        (tile(7, 0), o(3, 1, 1), true),
        (tile(6, -1), o(3, 1, 1), false),
        (o(4, 2, 3), tile(5, 0), false),
        (tile(5, 0), tile(5, 0), false),
        (tile(6, 0), tile(5, 0), true),
        (tile(7, 0), tile(5, 0), true),
        (tile(7, 1), tile(5, 0), false),
        (tile(7, 0), tile(6, -1), false),
        (tile(7, 0), ow(5, 0, c(4, 0, 0)), false),
        (tile(4, 0), ow(5, 0, c(3, 1, 1)), false),
    ];
    for (child, parent, expected) in cases {
        assert_eq!(child.is_child_of(&parent), expected, "{child} of {parent}");
    }
}

#[test]
fn an_unwrapped_id_wraps_x_into_its_world_and_clamps_y() {
    // (zoom, x, y, canonical x, canonical y, wrap)
    let cases = [
        (0, 0, 0, 0, 0, 0),
        (0, 1, 0, 0, 0, 1),
        (0, 4, 0, 0, 0, 4),
        (0, -1, 0, 0, 0, -1),
        (0, 0, 1, 0, 0, 0),
        (0, 0, -1, 0, 0, 0),
        (18, 262_143, 0, 262_143, 0, 0),
        (18, 262_144, 0, 0, 0, 1),
        (2, 4, 3, 0, 3, 1),
        (1, -2, 1, 0, 1, -1),
        (1, -1, 0, 1, 0, -1),
    ];
    for (z, x, y, canonical_x, canonical_y, wrap) in cases {
        assert_eq!(u(z, x, y), uw(wrap, c(z, canonical_x, canonical_y)), "{z}/{x}/{y}");
    }

    assert!(u(0, 0, 0) < u(1, 0, 0));
    assert!(u(1, 0, 0) < u(0, 1, 0));
    assert_eq!([u(1, 0, 0), u(1, 0, 1), u(1, 1, 0), u(1, 1, 1)], u(0, 0, 0).children());
    assert_eq!([u(1, -2, 0), u(1, -2, 1), u(1, -1, 0), u(1, -1, 1)], u(0, -1, 0).children());
}

#[test]
fn ids_format_as_zoom_x_y_and_wrap_or_overscaled_zoom() {
    assert_eq!("4/2/3=>6", ow(6, 0, c(4, 2, 3)).to_string());
    assert_eq!("4/2/3+1", uw(1, c(4, 2, 3)).to_string());
    assert_eq!("4/2/3-1", uw(-1, c(4, 2, 3)).to_string());
    assert_eq!("4/2/3+0", uw(0, c(4, 2, 3)).to_string());
}