Skip to main content

mln_engine/camera/
tile_projector.rs

1use mln_style::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    #[expect(clippy::large_types_passed_by_value, reason = "the projector keeps `data`")]
24    pub fn with_data(view: &'a View, tile_id: &UnwrappedTileID, data: ProjectionData) -> Self {
25        TileProjector { view, tile_id: *tile_id, data }
26    }
27
28    /// Where `point` of the tile, `elevation` meters up, falls on the
29    /// screen, for queries and label placement.
30    #[must_use]
31    pub fn project(&self, point: &Point<f64>, elevation: f64) -> ProjectedTilePoint {
32        self.view.projection().project_tile_point(&self.data, &self.tile_id, point, elevation)
33    }
34
35    /// Where the renderer draws the point, for the rendered feature capture.
36    #[must_use]
37    pub fn project_as_drawn(&self, point: &Point<f64>, elevation: f64) -> ProjectedTilePoint {
38        self.view.projection().drawn_tile_point(&self.data, &self.tile_id, point, elevation)
39    }
40
41    #[must_use]
42    #[cfg(test)]
43    pub(crate) fn map_aligned_scale(&self) -> f64 {
44        self.view.projection().map_aligned_scale(self.view)
45    }
46
47    /// The tile units that a pixel of what is aligned with the map spans at
48    /// `tile_anchor`, a point of the tile, over the tile's
49    /// `pixels_to_tile_units`: on the globe, which draws it at the center's
50    /// scale everywhere ([`Projection::map_aligned_scale`]), that scale over
51    /// the cosine of the point's latitude; 1 on Mercator; blended through
52    /// the projection transition.
53    ///
54    /// [`Projection::map_aligned_scale`]: crate::camera::Projection::map_aligned_scale
55    #[must_use]
56    pub fn map_aligned_scale_at(&self, tile_anchor: &Point<f64>) -> f64 {
57        self.view.projection().map_aligned_scale_at(self.view, tile_anchor, &self.tile_id)
58    }
59
60    /// Whether a point at `signed_distance_from_camera` lies past Mercator's
61    /// far clip plane when the horizon is visible.
62    #[must_use]
63    pub fn is_past_mercator_horizon(&self, signed_distance_from_camera: f64) -> bool {
64        !self.view.is_globe() && self.view.shows_horizon() && signed_distance_from_camera > self.view.far_z()
65    }
66
67    /// Whether `projected` is behind the camera or past the horizon: behind
68    /// the planet on the globe, or past `far_z` on Mercator when the horizon
69    /// is visible.
70    #[must_use]
71    pub fn is_behind_camera_or_horizon(&self, projected: &ProjectedTilePoint) -> bool {
72        projected.occluded
73            || projected.signed_distance_from_camera <= 0.0
74            || self.is_past_mercator_horizon(projected.signed_distance_from_camera)
75    }
76
77    #[must_use]
78    pub fn view(&self) -> &View {
79        self.view
80    }
81
82    #[must_use]
83    pub fn tile_id(&self) -> &UnwrappedTileID {
84        &self.tile_id
85    }
86}