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`]).
5
6pub(crate) mod bounding_volumes;
7pub mod constants;
8pub mod intersection_tests;
9pub mod math;
10pub mod projection;
11
12use geometry::Point;
13use mln_style::geo::LatLng;
14use mln_style::size::Size;
15
16pub type ScreenCoordinate = Point<f64>;
17pub(crate) type ScreenLineString = Vec<Point<f64>>;
18
19/// Determines the orientation of the map.
20#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
21pub enum NorthOrientation {
22    #[default]
23    Upwards,
24    Rightwards,
25    Downwards,
26    Leftwards,
27}
28
29/// The distance on each side between a rectangle and a rectangle within.
30///
31/// Any values are allowed; the users of insets say which they accept.
32#[derive(Clone, Copy, Debug, Default, PartialEq)]
33pub struct EdgeInsets {
34    /// Number of pixels inset from the top edge.
35    pub top: f64,
36    /// Number of pixels inset from the left edge.
37    pub left: f64,
38    /// Number of pixels inset from the bottom edge.
39    pub bottom: f64,
40    /// Number of pixels inset from the right edge.
41    pub right: f64,
42}
43
44impl EdgeInsets {
45    #[must_use]
46    pub const fn new(top: f64, left: f64, bottom: f64, right: f64) -> Self {
47        EdgeInsets { top, left, bottom, right }
48    }
49
50    #[must_use]
51    pub fn top(&self) -> f64 {
52        self.top
53    }
54    #[must_use]
55    pub fn left(&self) -> f64 {
56        self.left
57    }
58    #[must_use]
59    pub fn bottom(&self) -> f64 {
60        self.bottom
61    }
62    #[must_use]
63    pub fn right(&self) -> f64 {
64        self.right
65    }
66
67    /// The center of the area these insets leave in a viewport of `size`, in
68    /// pixels from its top left.
69    #[must_use]
70    pub(crate) fn center_in(&self, size: Size) -> ScreenCoordinate {
71        Point::new(
72            (f64::from(size.width) - self.left() - self.right()) / 2.0 + self.left(),
73            (f64::from(size.height) - self.top() - self.bottom()) / 2.0 + self.top(),
74        )
75    }
76}
77
78impl std::ops::AddAssign for EdgeInsets {
79    fn add_assign(&mut self, o: EdgeInsets) {
80        self.top += o.top;
81        self.left += o.left;
82        self.bottom += o.bottom;
83        self.right += o.right;
84    }
85}
86
87impl std::ops::Add for EdgeInsets {
88    type Output = EdgeInsets;
89    fn add(mut self, o: EdgeInsets) -> EdgeInsets {
90        self += o;
91        self
92    }
93}
94
95#[derive(Clone, Copy, Debug, Default, PartialEq)]
96#[cfg(test)]
97pub(crate) struct LatLngAltitude {
98    pub(crate) location: LatLng,
99    pub(crate) altitude: f64,
100}
101
102/// The latitudes of the north and the south edges of `id`, in degrees.
103#[must_use]
104pub fn lat_range(id: &crate::tile::UnwrappedTileID) -> [f32; 2] {
105    let latlng0 = LatLng::from_unwrapped_tile(id.canonical.z, id.canonical.x, id.canonical.y, id.wrap);
106    let below =
107        crate::tile::UnwrappedTileID::new(id.canonical.z, i64::from(id.canonical.x), i64::from(id.canonical.y) + 1);
108    let latlng1 = LatLng::from_unwrapped_tile(below.canonical.z, below.canonical.x, below.canonical.y, below.wrap);
109    [latlng0.latitude() as f32, latlng1.latitude() as f32]
110}