mln-rs-engine 0.0.21

Part of mln-rs, a MapLibre map renderer written in Rust. Depend on mln-rs instead.
Documentation
use 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]
    pub fn with_data(view: &'a View, tile_id: &UnwrappedTileID, data: ProjectionData) -> Self {
        TileProjector { view, tile_id: *tile_id, data }
    }

    /// GL JS's `projectTileCoordinates`, 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)
    }

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

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