Skip to main content

mln_engine/camera/
tile_projector.rs

1use geometry::Point;
2
3use crate::camera::projection::{ProjectedTilePoint, ProjectionData};
4use crate::camera::view::View;
5use crate::tile::UnwrappedTileID;
6
7/// Projects the points of one tile through the current projection, the way
8/// the vertex shaders do it.
9#[derive(Clone, Copy)]
10pub struct TileProjector<'a> {
11    view: &'a View,
12    tile_id: UnwrappedTileID,
13    data: ProjectionData,
14}
15
16impl<'a> TileProjector<'a> {
17    #[must_use]
18    pub fn new(view: &'a View, tile_id: &UnwrappedTileID) -> Self {
19        Self::with_data(view, tile_id, view.projection_data(tile_id))
20    }
21
22    #[must_use]
23    pub fn with_data(view: &'a View, tile_id: &UnwrappedTileID, data: ProjectionData) -> Self {
24        TileProjector { view, tile_id: *tile_id, data }
25    }
26
27    /// GL JS's `projectTileCoordinates`, for queries and label placement.
28    #[must_use]
29    pub fn project(&self, point: &Point<f64>, elevation: f64) -> ProjectedTilePoint {
30        self.view.projection().project_tile_point(&self.data, &self.tile_id, point, elevation)
31    }
32
33    /// Where the renderer draws the point, for the rendered feature capture.
34    #[must_use]
35    pub fn project_as_drawn(&self, point: &Point<f64>, elevation: f64) -> ProjectedTilePoint {
36        self.view.projection().drawn_tile_point(&self.data, &self.tile_id, point, elevation)
37    }
38
39    #[must_use]
40    #[cfg(test)]
41    pub(crate) fn map_aligned_scale(&self) -> f64 {
42        self.view.projection().map_aligned_scale(self.view)
43    }
44
45    /// The tile units that a pixel of what is aligned with the map spans at
46    /// `tile_anchor`, a point of the tile, over the tile's
47    /// `pixels_to_tile_units`: on the globe, which draws it at the center's
48    /// scale everywhere ([`Projection::map_aligned_scale`]), that scale over
49    /// the cosine of the point's latitude; 1 on Mercator; blended through
50    /// the projection transition.
51    ///
52    /// [`Projection::map_aligned_scale`]: crate::camera::Projection::map_aligned_scale
53    #[must_use]
54    pub fn map_aligned_scale_at(&self, tile_anchor: &Point<f64>) -> f64 {
55        self.view.projection().map_aligned_scale_at(self.view, tile_anchor, &self.tile_id)
56    }
57
58    #[must_use]
59    pub fn view(&self) -> &View {
60        self.view
61    }
62
63    #[must_use]
64    pub fn tile_id(&self) -> &UnwrappedTileID {
65        &self.tile_id
66    }
67}