use std::fmt;
use mln_style::constants;
use mln_style::tile_id::CanonicalTileID;
#[derive(Clone, Copy, Debug, PartialEq, Eq, PartialOrd, Ord, Hash, mln_wire::Wire)]
pub struct OverscaledTileID {
pub overscaled_z: u8,
pub wrap: i16,
pub canonical: CanonicalTileID,
}
impl OverscaledTileID {
#[must_use]
pub fn new(overscaled_z: u8, wrap: i16, canonical: CanonicalTileID) -> Self {
debug_assert!(overscaled_z >= canonical.z);
OverscaledTileID { overscaled_z, wrap, canonical }
}
#[must_use]
pub fn with_zxy(overscaled_z: u8, wrap: i16, z: u8, x: u32, y: u32) -> Self {
Self::new(overscaled_z, wrap, CanonicalTileID::new(z, x, y))
}
#[must_use]
pub fn from_zxy(z: u8, x: u32, y: u32) -> Self {
OverscaledTileID { overscaled_z: z, wrap: 0, canonical: CanonicalTileID::new(z, x, y) }
}
#[must_use]
pub fn overscale_factor(&self) -> u32 {
1u32.wrapping_shl((i32::from(self.overscaled_z) - i32::from(self.canonical.z)) as u32)
}
#[must_use]
pub fn is_child_of(&self, rhs: &OverscaledTileID) -> bool {
self.wrap == rhs.wrap
&& self.overscaled_z > rhs.overscaled_z
&& (self.canonical == rhs.canonical || self.canonical.is_child_of(&rhs.canonical))
}
#[must_use]
pub fn scaled_to(&self, z: u8) -> OverscaledTileID {
OverscaledTileID::new(
z,
self.wrap,
if z >= self.canonical.z { self.canonical } else { self.canonical.scaled_to(z) },
)
}
#[must_use]
pub fn to_unwrapped(&self) -> UnwrappedTileID {
UnwrappedTileID::with_wrap(self.wrap, self.canonical)
}
#[must_use]
pub fn unwrap_to(&self, new_wrap: i16) -> OverscaledTileID {
OverscaledTileID::new(self.overscaled_z, new_wrap, self.canonical)
}
}
impl From<CanonicalTileID> for OverscaledTileID {
fn from(canonical: CanonicalTileID) -> Self {
OverscaledTileID::new(canonical.z, 0, canonical)
}
}
impl fmt::Display for OverscaledTileID {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "{}=>{}", self.canonical, self.overscaled_z)
}
}
#[derive(Clone, Copy, Debug, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub struct UnwrappedTileID {
pub wrap: i16,
pub canonical: CanonicalTileID,
}
impl UnwrappedTileID {
#[must_use]
pub fn new(z: u8, x: i64, y: i64) -> Self {
let dim = 1i64.wrapping_shl(u32::from(z));
let wrap = ((if x < 0 { x - dim + 1 } else { x }) / dim) as i16;
let canonical_x = (x - i64::from(wrap) * dim) as u32;
let canonical_y = if y < 0 {
0
} else {
let max = (1u64.wrapping_shl(u32::from(z)) as u32).wrapping_sub(1);
let y = y as u32;
if max < y { max } else { y }
};
UnwrappedTileID { wrap, canonical: CanonicalTileID::new(z, canonical_x, canonical_y) }
}
#[must_use]
pub const fn with_wrap(wrap: i16, canonical: CanonicalTileID) -> Self {
UnwrappedTileID { wrap, canonical }
}
#[must_use]
pub(crate) fn is_child_of(&self, parent: &UnwrappedTileID) -> bool {
self.wrap == parent.wrap && self.canonical.is_child_of(&parent.canonical)
}
#[must_use]
pub(crate) fn children(&self) -> [UnwrappedTileID; 4] {
let child_z = self.canonical.z.wrapping_add(1);
let child_x = self.canonical.x.wrapping_mul(2);
let child_y = self.canonical.y.wrapping_mul(2);
[
UnwrappedTileID::with_wrap(self.wrap, CanonicalTileID::new(child_z, child_x, child_y)),
UnwrappedTileID::with_wrap(self.wrap, CanonicalTileID::new(child_z, child_x, child_y.wrapping_add(1))),
UnwrappedTileID::with_wrap(self.wrap, CanonicalTileID::new(child_z, child_x.wrapping_add(1), child_y)),
UnwrappedTileID::with_wrap(
self.wrap,
CanonicalTileID::new(child_z, child_x.wrapping_add(1), child_y.wrapping_add(1)),
),
]
}
#[must_use]
pub fn overscale_to(&self, overscaled_z: u8) -> OverscaledTileID {
debug_assert!(overscaled_z >= self.canonical.z);
OverscaledTileID::new(overscaled_z, self.wrap, self.canonical)
}
#[must_use]
pub fn pixels_to_tile_units(&self, pixel_value: f32, zoom: f32) -> f32 {
pixel_value
* (constants::EXTENT as f32
/ (constants::TILE_SIZE as f32 * 2.0f32.powf(zoom - f32::from(self.canonical.z))))
}
#[must_use]
pub(crate) fn unwrap_to(&self, new_wrap: i16) -> UnwrappedTileID {
UnwrappedTileID::with_wrap(new_wrap, self.canonical)
}
}
impl fmt::Display for UnwrappedTileID {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "{}{}{}", self.canonical, if self.wrap >= 0 { "+" } else { "" }, self.wrap)
}
}
#[cfg(test)]
#[path = "tile_id_test.rs"]
mod tile_id_test;