mln-rs-engine 0.0.22

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

use crate::camera::projection::{ProjectedTilePoint, ProjectionData};
use crate::camera::view::View;
use crate::tile::UnwrappedTileID;

/// Projects the points of one tile through the current projection, the way
/// the vertex shaders do it.
#[derive(Clone, Copy)]
pub struct TileProjector<'a> {
    view: &'a View,
    tile_id: UnwrappedTileID,
    data: ProjectionData,
}

impl<'a> TileProjector<'a> {
    #[must_use]
    pub fn new(view: &'a View, tile_id: &UnwrappedTileID) -> Self {
        Self::with_data(view, tile_id, view.projection_data(tile_id))
    }

    #[must_use]
    #[expect(clippy::large_types_passed_by_value, reason = "the projector keeps `data`")]
    pub fn with_data(view: &'a View, tile_id: &UnwrappedTileID, data: ProjectionData) -> Self {
        TileProjector { view, tile_id: *tile_id, data }
    }

    /// Where `point` of the tile, `elevation` meters up, falls on the
    /// screen, for queries and label placement.
    #[must_use]
    pub fn project(&self, point: &Point<f64>, elevation: f64) -> ProjectedTilePoint {
        self.view.projection().project_tile_point(&self.data, &self.tile_id, point, elevation)
    }

    /// Where the renderer draws the point, for the rendered feature capture.
    #[must_use]
    pub fn project_as_drawn(&self, point: &Point<f64>, elevation: f64) -> ProjectedTilePoint {
        self.view.projection().drawn_tile_point(&self.data, &self.tile_id, point, elevation)
    }

    #[must_use]
    #[cfg(test)]
    pub(crate) fn map_aligned_scale(&self) -> f64 {
        self.view.projection().map_aligned_scale(self.view)
    }

    /// The tile units that a pixel of what is aligned with the map spans at
    /// `tile_anchor`, a point of the tile, over the tile's
    /// `pixels_to_tile_units`: on the globe, which draws it at the center's
    /// scale everywhere ([`Projection::map_aligned_scale`]), that scale over
    /// the cosine of the point's latitude; 1 on Mercator; blended through
    /// the projection transition.
    ///
    /// [`Projection::map_aligned_scale`]: crate::camera::Projection::map_aligned_scale
    #[must_use]
    pub fn map_aligned_scale_at(&self, tile_anchor: &Point<f64>) -> f64 {
        self.view.projection().map_aligned_scale_at(self.view, tile_anchor, &self.tile_id)
    }

    /// Whether a point at `signed_distance_from_camera` lies past Mercator's
    /// far clip plane when the horizon is visible.
    #[must_use]
    pub fn is_past_mercator_horizon(&self, signed_distance_from_camera: f64) -> bool {
        !self.view.is_globe() && self.view.shows_horizon() && signed_distance_from_camera > self.view.far_z()
    }

    /// Whether `projected` is behind the camera or past the horizon: behind
    /// the planet on the globe, or past `far_z` on Mercator when the horizon
    /// is visible.
    #[must_use]
    pub fn is_behind_camera_or_horizon(&self, projected: &ProjectedTilePoint) -> bool {
        projected.occluded
            || projected.signed_distance_from_camera <= 0.0
            || self.is_past_mercator_horizon(projected.signed_distance_from_camera)
    }

    #[must_use]
    pub fn view(&self) -> &View {
        self.view
    }

    #[must_use]
    pub fn tile_id(&self) -> &UnwrappedTileID {
        &self.tile_id
    }
}