1use std::fmt;
14
15use mln_util::constants;
16
17#[derive(Clone, Copy, Debug, PartialEq, Eq, PartialOrd, Ord, Hash, mln_wire::Wire)]
22pub struct CanonicalTileID {
23 pub z: u8,
24 pub x: u32,
25 pub y: u32,
26}
27
28impl CanonicalTileID {
29 #[must_use]
30 pub fn new(z: u8, x: u32, y: u32) -> Self {
31 debug_assert!(z <= 32);
32 debug_assert!(u64::from(x) < (1u64 << z));
33 debug_assert!(u64::from(y) < (1u64 << z));
34 CanonicalTileID { z, x, y }
35 }
36
37 #[must_use]
38 pub fn is_child_of(&self, parent: &CanonicalTileID) -> bool {
39 parent.z == 0
42 || (parent.z < self.z
43 && parent.x == self.x.wrapping_shr(u32::from(self.z - parent.z))
44 && parent.y == self.y.wrapping_shr(u32::from(self.z - parent.z)))
45 }
46
47 #[must_use]
48 pub(crate) fn scaled_to(&self, target_z: u8) -> CanonicalTileID {
49 if target_z <= self.z {
50 let shift = (i32::from(self.z) - i32::from(target_z)) as u32;
52 CanonicalTileID::new(target_z, self.x.wrapping_shr(shift), self.y.wrapping_shr(shift))
53 } else {
54 let shift = (i32::from(target_z) - i32::from(self.z)) as u32;
56 CanonicalTileID::new(target_z, self.x.wrapping_shl(shift), self.y.wrapping_shl(shift))
57 }
58 }
59
60 #[must_use]
61 pub fn children(&self) -> [CanonicalTileID; 4] {
62 let child_z = self.z.wrapping_add(1);
63 let child_x = self.x.wrapping_mul(2);
64 let child_y = self.y.wrapping_mul(2);
65 [
66 CanonicalTileID::new(child_z, child_x, child_y),
67 CanonicalTileID::new(child_z, child_x, child_y.wrapping_add(1)),
68 CanonicalTileID::new(child_z, child_x.wrapping_add(1), child_y),
69 CanonicalTileID::new(child_z, child_x.wrapping_add(1), child_y.wrapping_add(1)),
70 ]
71 }
72}
73
74impl fmt::Display for CanonicalTileID {
76 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
77 write!(f, "{}/{}/{}", self.z, self.x, self.y)
78 }
79}
80
81#[derive(Clone, Copy, Debug, PartialEq, Eq, PartialOrd, Ord, Hash, mln_wire::Wire)]
85pub struct OverscaledTileID {
86 pub overscaled_z: u8,
87 pub wrap: i16,
88 pub canonical: CanonicalTileID,
89}
90
91impl OverscaledTileID {
92 #[must_use]
93 pub fn new(overscaled_z: u8, wrap: i16, canonical: CanonicalTileID) -> Self {
94 debug_assert!(overscaled_z >= canonical.z);
95 OverscaledTileID { overscaled_z, wrap, canonical }
96 }
97
98 #[must_use]
99 pub fn with_zxy(overscaled_z: u8, wrap: i16, z: u8, x: u32, y: u32) -> Self {
100 Self::new(overscaled_z, wrap, CanonicalTileID::new(z, x, y))
101 }
102
103 #[must_use]
104 pub fn from_zxy(z: u8, x: u32, y: u32) -> Self {
105 OverscaledTileID { overscaled_z: z, wrap: 0, canonical: CanonicalTileID::new(z, x, y) }
106 }
107
108 #[must_use]
109 pub fn overscale_factor(&self) -> u32 {
110 1u32.wrapping_shl((i32::from(self.overscaled_z) - i32::from(self.canonical.z)) as u32)
111 }
112
113 #[must_use]
114 pub fn is_child_of(&self, rhs: &OverscaledTileID) -> bool {
115 self.wrap == rhs.wrap
116 && self.overscaled_z > rhs.overscaled_z
117 && (self.canonical == rhs.canonical || self.canonical.is_child_of(&rhs.canonical))
118 }
119
120 #[must_use]
121 pub fn scaled_to(&self, z: u8) -> OverscaledTileID {
122 OverscaledTileID::new(
123 z,
124 self.wrap,
125 if z >= self.canonical.z { self.canonical } else { self.canonical.scaled_to(z) },
126 )
127 }
128
129 #[must_use]
130 pub fn to_unwrapped(&self) -> UnwrappedTileID {
131 UnwrappedTileID::with_wrap(self.wrap, self.canonical)
132 }
133
134 #[must_use]
135 pub fn unwrap_to(&self, new_wrap: i16) -> OverscaledTileID {
136 OverscaledTileID::new(self.overscaled_z, new_wrap, self.canonical)
137 }
138}
139
140impl From<CanonicalTileID> for OverscaledTileID {
141 fn from(canonical: CanonicalTileID) -> Self {
142 OverscaledTileID::new(canonical.z, 0, canonical)
143 }
144}
145
146impl fmt::Display for OverscaledTileID {
148 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
149 write!(f, "{}=>{}", self.canonical, self.overscaled_z)
150 }
151}
152
153#[derive(Clone, Copy, Debug, PartialEq, Eq, PartialOrd, Ord, Hash)]
158pub struct UnwrappedTileID {
159 pub wrap: i16,
160 pub canonical: CanonicalTileID,
161}
162
163impl UnwrappedTileID {
164 #[must_use]
165 pub fn new(z: u8, x: i64, y: i64) -> Self {
166 let dim = 1i64.wrapping_shl(u32::from(z));
167 let wrap = ((if x < 0 { x - dim + 1 } else { x }) / dim) as i16;
168 let canonical_x = (x - i64::from(wrap) * dim) as u32;
169 let canonical_y = if y < 0 {
170 0
171 } else {
172 let max = (1u64.wrapping_shl(u32::from(z)) as u32).wrapping_sub(1);
173 let y = y as u32;
174 if max < y { max } else { y }
175 };
176 UnwrappedTileID { wrap, canonical: CanonicalTileID::new(z, canonical_x, canonical_y) }
177 }
178
179 #[must_use]
180 pub const fn with_wrap(wrap: i16, canonical: CanonicalTileID) -> Self {
181 UnwrappedTileID { wrap, canonical }
182 }
183
184 #[must_use]
185 pub fn is_child_of(&self, parent: &UnwrappedTileID) -> bool {
186 self.wrap == parent.wrap && self.canonical.is_child_of(&parent.canonical)
187 }
188
189 #[must_use]
190 pub fn children(&self) -> [UnwrappedTileID; 4] {
191 let child_z = self.canonical.z.wrapping_add(1);
192 let child_x = self.canonical.x.wrapping_mul(2);
193 let child_y = self.canonical.y.wrapping_mul(2);
194 [
195 UnwrappedTileID::with_wrap(self.wrap, CanonicalTileID::new(child_z, child_x, child_y)),
196 UnwrappedTileID::with_wrap(self.wrap, CanonicalTileID::new(child_z, child_x, child_y.wrapping_add(1))),
197 UnwrappedTileID::with_wrap(self.wrap, CanonicalTileID::new(child_z, child_x.wrapping_add(1), child_y)),
198 UnwrappedTileID::with_wrap(
199 self.wrap,
200 CanonicalTileID::new(child_z, child_x.wrapping_add(1), child_y.wrapping_add(1)),
201 ),
202 ]
203 }
204
205 #[must_use]
206 pub fn overscale_to(&self, overscaled_z: u8) -> OverscaledTileID {
207 debug_assert!(overscaled_z >= self.canonical.z);
208 OverscaledTileID::new(overscaled_z, self.wrap, self.canonical)
209 }
210
211 #[must_use]
212 pub fn pixels_to_tile_units(&self, pixel_value: f32, zoom: f32) -> f32 {
213 pixel_value
214 * (constants::EXTENT as f32
215 / (constants::TILE_SIZE as f32 * 2.0f32.powf(zoom - f32::from(self.canonical.z))))
216 }
217
218 #[must_use]
219 pub fn unwrap_to(&self, new_wrap: i16) -> UnwrappedTileID {
220 UnwrappedTileID::with_wrap(new_wrap, self.canonical)
221 }
222}
223
224impl fmt::Display for UnwrappedTileID {
226 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
227 write!(f, "{}{}{}", self.canonical, if self.wrap >= 0 { "+" } else { "" }, self.wrap)
228 }
229}
230
231#[cfg(test)]
232#[path = "tile_id_test.rs"]
233mod tile_id_test;