use crate::geometry::mercator::TileCoord;
use crate::tile::feature::TILE_EXTENT;
#[inline]
#[must_use]
pub fn world_to_tile_int(world_x: f64, world_y: f64, tile: TileCoord) -> (i32, i32) {
let n = (1u32 << tile.z) as f64;
let extent = TILE_EXTENT as f64;
let px = (world_x * n - tile.x as f64) * extent;
let py = (world_y * n - tile.y as f64) * extent;
(px.round() as i32, py.round() as i32)
}
#[must_use]
pub fn dedup_consecutive(coords: &[(i32, i32)]) -> Vec<(i32, i32)> {
let mut result = coords.to_vec();
result.dedup();
result
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn snap_center_of_tile() {
let tile = TileCoord::new(1, 0, 0);
let (x, y) = world_to_tile_int(0.25, 0.25, tile);
assert_eq!(x, (TILE_EXTENT / 2) as i32);
assert_eq!(y, (TILE_EXTENT / 2) as i32);
}
#[test]
fn snap_origin_of_tile() {
let tile = TileCoord::new(1, 0, 0);
let (x, y) = world_to_tile_int(0.0, 0.0, tile);
assert_eq!(x, 0);
assert_eq!(y, 0);
}
#[test]
fn dedup_removes_consecutive() {
let coords = vec![(0, 0), (0, 0), (1, 1), (1, 1), (2, 2)];
let deduped = dedup_consecutive(&coords);
assert_eq!(deduped, vec![(0, 0), (1, 1), (2, 2)]);
}
}