1use std::fmt;
7
8use mln_style::constants;
9use mln_style::tile_id::CanonicalTileID;
10
11#[derive(Clone, Copy, Debug, PartialEq, Eq, PartialOrd, Ord, Hash, mln_wire::Wire)]
15pub struct OverscaledTileID {
16 pub overscaled_z: u8,
17 pub wrap: i16,
18 pub canonical: CanonicalTileID,
19}
20
21impl OverscaledTileID {
22 #[must_use]
23 pub fn new(overscaled_z: u8, wrap: i16, canonical: CanonicalTileID) -> Self {
24 debug_assert!(overscaled_z >= canonical.z);
25 OverscaledTileID { overscaled_z, wrap, canonical }
26 }
27
28 #[must_use]
29 pub fn with_zxy(overscaled_z: u8, wrap: i16, z: u8, x: u32, y: u32) -> Self {
30 Self::new(overscaled_z, wrap, CanonicalTileID::new(z, x, y))
31 }
32
33 #[must_use]
34 pub fn from_zxy(z: u8, x: u32, y: u32) -> Self {
35 OverscaledTileID { overscaled_z: z, wrap: 0, canonical: CanonicalTileID::new(z, x, y) }
36 }
37
38 #[must_use]
39 pub fn overscale_factor(&self) -> u32 {
40 1u32.wrapping_shl((i32::from(self.overscaled_z) - i32::from(self.canonical.z)) as u32)
41 }
42
43 #[must_use]
44 pub fn is_child_of(&self, rhs: &OverscaledTileID) -> bool {
45 self.wrap == rhs.wrap
46 && self.overscaled_z > rhs.overscaled_z
47 && (self.canonical == rhs.canonical || self.canonical.is_child_of(&rhs.canonical))
48 }
49
50 #[must_use]
51 pub fn scaled_to(&self, z: u8) -> OverscaledTileID {
52 OverscaledTileID::new(
53 z,
54 self.wrap,
55 if z >= self.canonical.z { self.canonical } else { self.canonical.scaled_to(z) },
56 )
57 }
58
59 #[must_use]
60 pub fn to_unwrapped(&self) -> UnwrappedTileID {
61 UnwrappedTileID::with_wrap(self.wrap, self.canonical)
62 }
63
64 #[must_use]
65 pub fn unwrap_to(&self, new_wrap: i16) -> OverscaledTileID {
66 OverscaledTileID::new(self.overscaled_z, new_wrap, self.canonical)
67 }
68}
69
70impl From<CanonicalTileID> for OverscaledTileID {
71 fn from(canonical: CanonicalTileID) -> Self {
72 OverscaledTileID::new(canonical.z, 0, canonical)
73 }
74}
75
76impl fmt::Display for OverscaledTileID {
78 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
79 write!(f, "{}=>{}", self.canonical, self.overscaled_z)
80 }
81}
82
83#[derive(Clone, Copy, Debug, PartialEq, Eq, PartialOrd, Ord, Hash)]
88pub struct UnwrappedTileID {
89 pub wrap: i16,
90 pub canonical: CanonicalTileID,
91}
92
93impl UnwrappedTileID {
94 #[must_use]
95 pub fn new(z: u8, x: i64, y: i64) -> Self {
96 let dim = 1i64.wrapping_shl(u32::from(z));
97 let wrap = ((if x < 0 { x - dim + 1 } else { x }) / dim) as i16;
98 let canonical_x = (x - i64::from(wrap) * dim) as u32;
99 let canonical_y = if y < 0 {
100 0
101 } else {
102 let max = (1u64.wrapping_shl(u32::from(z)) as u32).wrapping_sub(1);
103 let y = y as u32;
104 if max < y { max } else { y }
105 };
106 UnwrappedTileID { wrap, canonical: CanonicalTileID::new(z, canonical_x, canonical_y) }
107 }
108
109 #[must_use]
110 pub const fn with_wrap(wrap: i16, canonical: CanonicalTileID) -> Self {
111 UnwrappedTileID { wrap, canonical }
112 }
113
114 #[must_use]
115 pub(crate) fn is_child_of(&self, parent: &UnwrappedTileID) -> bool {
116 self.wrap == parent.wrap && self.canonical.is_child_of(&parent.canonical)
117 }
118
119 #[must_use]
120 pub(crate) fn children(&self) -> [UnwrappedTileID; 4] {
121 let child_z = self.canonical.z.wrapping_add(1);
122 let child_x = self.canonical.x.wrapping_mul(2);
123 let child_y = self.canonical.y.wrapping_mul(2);
124 [
125 UnwrappedTileID::with_wrap(self.wrap, CanonicalTileID::new(child_z, child_x, child_y)),
126 UnwrappedTileID::with_wrap(self.wrap, CanonicalTileID::new(child_z, child_x, child_y.wrapping_add(1))),
127 UnwrappedTileID::with_wrap(self.wrap, CanonicalTileID::new(child_z, child_x.wrapping_add(1), child_y)),
128 UnwrappedTileID::with_wrap(
129 self.wrap,
130 CanonicalTileID::new(child_z, child_x.wrapping_add(1), child_y.wrapping_add(1)),
131 ),
132 ]
133 }
134
135 #[must_use]
136 pub fn overscale_to(&self, overscaled_z: u8) -> OverscaledTileID {
137 debug_assert!(overscaled_z >= self.canonical.z);
138 OverscaledTileID::new(overscaled_z, self.wrap, self.canonical)
139 }
140
141 #[must_use]
142 pub fn pixels_to_tile_units(&self, pixel_value: f32, zoom: f32) -> f32 {
143 pixel_value
144 * (constants::EXTENT as f32
145 / (constants::TILE_SIZE as f32 * 2.0f32.powf(zoom - f32::from(self.canonical.z))))
146 }
147
148 #[must_use]
149 pub(crate) fn unwrap_to(&self, new_wrap: i16) -> UnwrappedTileID {
150 UnwrappedTileID::with_wrap(new_wrap, self.canonical)
151 }
152}
153
154impl fmt::Display for UnwrappedTileID {
156 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
157 write!(f, "{}{}{}", self.canonical, if self.wrap >= 0 { "+" } else { "" }, self.wrap)
158 }
159}
160
161#[cfg(test)]
162#[path = "tile_id_test.rs"]
163mod tile_id_test;