mln-rs-engine 0.0.19

Part of mln-rs, a Rust port of the MapLibre Native map rendering core. Depend on mln-rs instead.
Documentation
use geometry::Point;
use mln_style::constants;
use mln_style::geo::LatLng;

use crate::camera::view::View;
use crate::geo::ScreenCoordinate;
use crate::geo::projection::Projection;
use crate::tile::UnwrappedTileID;

pub(crate) type TileCoordinatePoint = Point<f64>;

/// Has floating point x/y coordinates. Used for computing the tiles that
/// need to be visible in the viewport. In mapbox-gl-js, this is named
/// `MercatorCoordinate`.
#[derive(Clone, Copy, Debug, PartialEq)]
pub struct TileCoordinate {
    pub p: TileCoordinatePoint,
    pub(crate) z: f64,
}

impl TileCoordinate {
    #[must_use]
    pub fn from_lat_lng(zoom: f64, lat_lng: &LatLng) -> TileCoordinate {
        let scale = 2.0f64.powf(zoom);
        let p = Projection::project(lat_lng, scale);
        TileCoordinate { p: Point::new(p.x / constants::TILE_SIZE, p.y / constants::TILE_SIZE), z: zoom }
    }

    #[must_use]
    pub(crate) fn from_screen_coordinate(
        view: &View,
        zoom: u8,
        screen_coordinate: &ScreenCoordinate,
    ) -> TileCoordinate {
        view.screen_coordinate_to_tile_coordinate(screen_coordinate, zoom)
    }

    #[must_use]
    pub(crate) fn zoom_to(&self, zoom: f64) -> TileCoordinate {
        let scale_diff = 2.0f64.powf(zoom - self.z);
        TileCoordinate { p: Point::new(self.p.x * scale_diff, self.p.y * scale_diff), z: zoom }
    }

    #[must_use]
    pub fn to_geometry_coordinate(tile_id: &UnwrappedTileID, point: &TileCoordinatePoint) -> Point<i16> {
        let scale = 2.0f64.powf(f64::from(tile_id.canonical.z));
        let zoomed = TileCoordinate { p: *point, z: 0.0 }.zoom_to(f64::from(tile_id.canonical.z));
        let clamp = |v: f64| -> i16 {
            // util::clamp<int64_t>(static_cast<int64_t>(v), INT16_MIN, INT16_MAX)
            let v = v as i64;
            v.clamp(i64::from(i16::MIN), i64::from(i16::MAX)) as i16
        };
        Point::new(
            clamp(
                (zoomed.p.x - f64::from(tile_id.canonical.x) - f64::from(tile_id.wrap) * scale)
                    * f64::from(constants::EXTENT),
            ),
            clamp((zoomed.p.y - f64::from(tile_id.canonical.y)) * f64::from(constants::EXTENT)),
        )
    }
}