Skip to main content

mln_engine/geo/
mod.rs

1//! The map's geometry: the camera's screen coordinates, edge insets and
2//! orientation; spherical Mercator ([`projection`]); the camera's frustum
3//! and the bounding volumes it culls (`bounding_volumes`); intersection
4//! tests, point math, and the Earth's size ([`constants`]). How lng/lat maps
5//! to the world square is [`mln_style::crs::WorldMapping`].
6
7pub(crate) mod bounding_volumes;
8pub mod constants;
9pub mod intersection_tests;
10pub mod math;
11pub mod projection;
12
13#[cfg(test)]
14#[path = "world_test.rs"]
15mod world_test;
16
17use geometry::Point;
18use mln_style::geo::LatLng;
19use mln_style::size::Size;
20
21pub type ScreenCoordinate = Point<f64>;
22pub(crate) type ScreenLineString = Vec<Point<f64>>;
23
24/// Determines the orientation of the map.
25#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
26pub enum NorthOrientation {
27    #[default]
28    Upwards,
29    Rightwards,
30    Downwards,
31    Leftwards,
32}
33
34/// The distance on each side between a rectangle and a rectangle within.
35///
36/// Any values are allowed; the users of insets say which they accept.
37#[derive(Clone, Copy, Debug, Default, PartialEq)]
38pub struct EdgeInsets {
39    /// Number of pixels inset from the top edge.
40    pub top: f64,
41    /// Number of pixels inset from the left edge.
42    pub left: f64,
43    /// Number of pixels inset from the bottom edge.
44    pub bottom: f64,
45    /// Number of pixels inset from the right edge.
46    pub right: f64,
47}
48
49impl EdgeInsets {
50    #[must_use]
51    pub const fn new(top: f64, left: f64, bottom: f64, right: f64) -> Self {
52        EdgeInsets { top, left, bottom, right }
53    }
54
55    #[must_use]
56    pub fn top(&self) -> f64 {
57        self.top
58    }
59    #[must_use]
60    pub fn left(&self) -> f64 {
61        self.left
62    }
63    #[must_use]
64    pub fn bottom(&self) -> f64 {
65        self.bottom
66    }
67    #[must_use]
68    pub fn right(&self) -> f64 {
69        self.right
70    }
71
72    /// The center of the area these insets leave in a viewport of `size`, in
73    /// pixels from its top left.
74    #[must_use]
75    pub(crate) fn center_in(&self, size: Size) -> ScreenCoordinate {
76        Point::new(
77            (f64::from(size.width) - self.left() - self.right()) / 2.0 + self.left(),
78            (f64::from(size.height) - self.top() - self.bottom()) / 2.0 + self.top(),
79        )
80    }
81}
82
83impl std::ops::AddAssign for EdgeInsets {
84    fn add_assign(&mut self, o: EdgeInsets) {
85        self.top += o.top;
86        self.left += o.left;
87        self.bottom += o.bottom;
88        self.right += o.right;
89    }
90}
91
92impl std::ops::Add for EdgeInsets {
93    type Output = EdgeInsets;
94    fn add(mut self, o: EdgeInsets) -> EdgeInsets {
95        self += o;
96        self
97    }
98}
99
100#[derive(Clone, Copy, Debug, Default, PartialEq)]
101#[cfg(test)]
102pub(crate) struct LatLngAltitude {
103    pub(crate) location: LatLng,
104    pub(crate) altitude: f64,
105}
106
107/// The latitudes of the north and the south edges of `id`, in degrees, by
108/// which the hillshade shader undoes Mercator's stretch of slopes: `[0, 0]`,
109/// no correction, in a registered projection's `world`, which doesn't wrap
110/// and draws in its own units.
111#[must_use]
112pub fn lat_range(id: &crate::tile::UnwrappedTileID, world: &mln_style::crs::WorldMapping) -> [f32; 2] {
113    if !world.wraps() {
114        return [0.0, 0.0];
115    }
116    let canonical = id.canonical;
117    let north = LatLng::from_unwrapped_tile(canonical.z, canonical.x, canonical.y, id.wrap);
118    // The south edge is the north edge of the row below, which for the
119    // bottom row is past the last tile, at the world's south edge.
120    let south = LatLng::from_unwrapped_tile(canonical.z, canonical.x, canonical.y + 1, id.wrap);
121    [north.latitude() as f32, south.latitude() as f32]
122}