use martin_tile_utils::TileCoord;
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Default)]
pub struct AncestorWindow {
levels: u8,
column: u32,
row: u32,
}
impl AncestorWindow {
pub const NATIVE: Self = Self {
levels: 0,
column: 0,
row: 0,
};
pub fn above(tile: TileCoord, levels: u8) -> (TileCoord, Self) {
assert!(levels <= tile.z(), "an ancestor cannot be above zoom 0");
let mask = (1u32 << levels) - 1;
let ancestor = TileCoord::new_unchecked(tile.z() - levels, tile.x() >> levels, tile.y() >> levels);
let window = Self {
levels,
column: tile.x() & mask,
row: tile.y() & mask,
};
(ancestor, window)
}
#[must_use]
pub const fn is_native(self) -> bool {
self.levels == 0
}
#[must_use]
pub fn ancestor_x(self, tile_size: usize, pixel: f64) -> f64 {
self.map(self.column, tile_size, pixel)
}
#[must_use]
pub fn ancestor_y(self, tile_size: usize, pixel: f64) -> f64 {
self.map(self.row, tile_size, pixel)
}
#[expect(clippy::cast_precision_loss, reason = "a tile side is a small power of two")]
fn map(self, offset: u32, tile_size: usize, pixel: f64) -> f64 {
let scale = f64::from(1u32 << self.levels);
(f64::from(offset) * tile_size as f64 + pixel + 0.5) / scale - 0.5
}
}
#[cfg(test)]
mod tests {
use approx::assert_relative_eq;
use super::*;
#[test]
fn a_tile_zero_levels_above_is_itself() {
let tile = TileCoord::new_unchecked(11, 326, 793);
assert_eq!(AncestorWindow::above(tile, 0), (tile, AncestorWindow::NATIVE));
}
#[test]
fn an_ancestor_is_the_parent_chain_and_the_window_the_remainder() {
let (ancestor, window) = AncestorWindow::above(TileCoord::new_unchecked(13, 1307, 3174), 3);
assert_eq!(ancestor, TileCoord::new_unchecked(10, 163, 396));
assert_eq!(
window,
AncestorWindow {
levels: 3,
column: 3,
row: 6
}
);
}
#[test]
fn a_native_window_maps_every_pixel_onto_itself() {
for pixel in [-32.0, 0.0, 17.0, 511.0, 543.0] {
assert_relative_eq!(AncestorWindow::NATIVE.ancestor_x(512, pixel), pixel);
}
}
#[test]
fn a_child_window_maps_its_edges_onto_the_matching_ancestor_half() {
let (_, window) = AncestorWindow::above(TileCoord::new_unchecked(11, 327, 792), 1);
assert_relative_eq!(window.ancestor_x(512, -0.5), 256.0 - 0.5);
assert_relative_eq!(window.ancestor_x(512, 511.5), 512.0 - 0.5);
assert_relative_eq!(window.ancestor_y(512, -0.5), -0.5);
assert_relative_eq!(window.ancestor_y(512, 511.5), 256.0 - 0.5);
}
}