Skip to main content

mln_engine/tile/
tile_id.rs

1// Integer arithmetic follows the C++ types, as `CanonicalTileID`'s does
2// (mln_style::tile_id), and the narrowing casts in the UnwrappedTileID
3// constructor are `as` casts like `static_cast`.
4//
5// Ordering: the derived `Ord` compares fields in declaration order, which is
6// the `std::tie` order of the C++ `operator<`.
7//
8// Hashing: `std::hash` specializations combine the fields with
9// `util::hash_combine`; the port derives `Hash` (field order differs from the
10// C++ combine order, but no ported code depends on hash values).
11
12use std::fmt;
13
14use mln_style::constants;
15use mln_style::tile_id::CanonicalTileID;
16
17/// Has integer z/x/y coordinates. `overscaled_z` describes the zoom level
18/// this tile is intended to represent, e.g. when parsing data. z is never
19/// larger than the source's maxzoom.
20#[derive(Clone, Copy, Debug, PartialEq, Eq, PartialOrd, Ord, Hash, mln_wire::Wire)]
21pub struct OverscaledTileID {
22    pub overscaled_z: u8,
23    pub wrap: i16,
24    pub canonical: CanonicalTileID,
25}
26
27impl OverscaledTileID {
28    #[must_use]
29    pub fn new(overscaled_z: u8, wrap: i16, canonical: CanonicalTileID) -> Self {
30        debug_assert!(overscaled_z >= canonical.z);
31        OverscaledTileID { overscaled_z, wrap, canonical }
32    }
33
34    #[must_use]
35    pub fn with_zxy(overscaled_z: u8, wrap: i16, z: u8, x: u32, y: u32) -> Self {
36        Self::new(overscaled_z, wrap, CanonicalTileID::new(z, x, y))
37    }
38
39    #[must_use]
40    pub fn from_zxy(z: u8, x: u32, y: u32) -> Self {
41        OverscaledTileID { overscaled_z: z, wrap: 0, canonical: CanonicalTileID::new(z, x, y) }
42    }
43
44    #[must_use]
45    pub fn overscale_factor(&self) -> u32 {
46        1u32.wrapping_shl((i32::from(self.overscaled_z) - i32::from(self.canonical.z)) as u32)
47    }
48
49    #[must_use]
50    pub fn is_child_of(&self, rhs: &OverscaledTileID) -> bool {
51        self.wrap == rhs.wrap
52            && self.overscaled_z > rhs.overscaled_z
53            && (self.canonical == rhs.canonical || self.canonical.is_child_of(&rhs.canonical))
54    }
55
56    #[must_use]
57    pub fn scaled_to(&self, z: u8) -> OverscaledTileID {
58        OverscaledTileID::new(
59            z,
60            self.wrap,
61            if z >= self.canonical.z { self.canonical } else { self.canonical.scaled_to(z) },
62        )
63    }
64
65    #[must_use]
66    pub fn to_unwrapped(&self) -> UnwrappedTileID {
67        UnwrappedTileID::with_wrap(self.wrap, self.canonical)
68    }
69
70    #[must_use]
71    pub fn unwrap_to(&self, new_wrap: i16) -> OverscaledTileID {
72        OverscaledTileID::new(self.overscaled_z, new_wrap, self.canonical)
73    }
74}
75
76impl From<CanonicalTileID> for OverscaledTileID {
77    fn from(canonical: CanonicalTileID) -> Self {
78        OverscaledTileID::new(canonical.z, 0, canonical)
79    }
80}
81
82/// `operator<<` and `util::toString`: `z/x/y=>overscaledZ`.
83impl fmt::Display for OverscaledTileID {
84    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
85        write!(f, "{}=>{}", self.canonical, self.overscaled_z)
86    }
87}
88
89/// Has integer z/x/y coordinates. `wrap` describes tiles that are left/right
90/// of the main tile pyramid, e.g. when wrapping the world. Used for describing
91/// what position tiles are getting rendered at (= calc the matrix). z is
92/// never larger than the source's maxzoom.
93#[derive(Clone, Copy, Debug, PartialEq, Eq, PartialOrd, Ord, Hash)]
94pub struct UnwrappedTileID {
95    pub wrap: i16,
96    pub canonical: CanonicalTileID,
97}
98
99impl UnwrappedTileID {
100    #[must_use]
101    pub fn new(z: u8, x: i64, y: i64) -> Self {
102        let dim = 1i64.wrapping_shl(u32::from(z));
103        let wrap = ((if x < 0 { x - dim + 1 } else { x }) / dim) as i16;
104        let canonical_x = (x - i64::from(wrap) * dim) as u32;
105        let canonical_y = if y < 0 {
106            0
107        } else {
108            let max = (1u64.wrapping_shl(u32::from(z)) as u32).wrapping_sub(1);
109            let y = y as u32;
110            if max < y { max } else { y }
111        };
112        UnwrappedTileID { wrap, canonical: CanonicalTileID::new(z, canonical_x, canonical_y) }
113    }
114
115    #[must_use]
116    pub const fn with_wrap(wrap: i16, canonical: CanonicalTileID) -> Self {
117        UnwrappedTileID { wrap, canonical }
118    }
119
120    #[must_use]
121    pub(crate) fn is_child_of(&self, parent: &UnwrappedTileID) -> bool {
122        self.wrap == parent.wrap && self.canonical.is_child_of(&parent.canonical)
123    }
124
125    #[must_use]
126    pub(crate) fn children(&self) -> [UnwrappedTileID; 4] {
127        let child_z = self.canonical.z.wrapping_add(1);
128        let child_x = self.canonical.x.wrapping_mul(2);
129        let child_y = self.canonical.y.wrapping_mul(2);
130        [
131            UnwrappedTileID::with_wrap(self.wrap, CanonicalTileID::new(child_z, child_x, child_y)),
132            UnwrappedTileID::with_wrap(self.wrap, CanonicalTileID::new(child_z, child_x, child_y.wrapping_add(1))),
133            UnwrappedTileID::with_wrap(self.wrap, CanonicalTileID::new(child_z, child_x.wrapping_add(1), child_y)),
134            UnwrappedTileID::with_wrap(
135                self.wrap,
136                CanonicalTileID::new(child_z, child_x.wrapping_add(1), child_y.wrapping_add(1)),
137            ),
138        ]
139    }
140
141    #[must_use]
142    pub fn overscale_to(&self, overscaled_z: u8) -> OverscaledTileID {
143        debug_assert!(overscaled_z >= self.canonical.z);
144        OverscaledTileID::new(overscaled_z, self.wrap, self.canonical)
145    }
146
147    #[must_use]
148    pub fn pixels_to_tile_units(&self, pixel_value: f32, zoom: f32) -> f32 {
149        pixel_value
150            * (constants::EXTENT as f32
151                / (constants::TILE_SIZE as f32 * 2.0f32.powf(zoom - f32::from(self.canonical.z))))
152    }
153
154    #[must_use]
155    pub(crate) fn unwrap_to(&self, new_wrap: i16) -> UnwrappedTileID {
156        UnwrappedTileID::with_wrap(new_wrap, self.canonical)
157    }
158}
159
160/// `operator<<` and `util::toString`: `z/x/y+wrap` (or `z/x/y-wrap`).
161impl fmt::Display for UnwrappedTileID {
162    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
163        write!(f, "{}{}{}", self.canonical, if self.wrap >= 0 { "+" } else { "" }, self.wrap)
164    }
165}
166
167#[cfg(test)]
168#[path = "tile_id_test.rs"]
169mod tile_id_test;