use geo_types::{Coord, Geometry, LineString, Polygon};
use geometry::Point;
use mln_map_types::{TileCoordinate, TileLodMode, TransformState, ViewportMode};
use mln_math::{fmax, fmin};
use mln_tile_id::{OverscaledTileID, UnwrappedTileID};
use mln_util::bounding_volumes::{AABB, Frustum, IntersectionResult};
use mln_util::constants;
use mln_util::error::Error;
use mln_util::geo::{LatLng, LatLngBounds, ScreenCoordinate};
use mln_util::mat3::{vec3_length, vec3_scale, vec3_sub};
use mln_util::projection::Projection;
use mln_util::range::Range;
use mln_util::vectors::Vec3;
use crate::util::tile_cover_impl::Impl;
fn bounds_lat_lng(lat: f64, lon: f64) -> LatLng {
LatLng::new(lat, lon).expect("LatLng from LatLngBounds coordinates")
}
type ScanLine<'a> = dyn FnMut(i32, i32, i32) + 'a;
#[derive(Clone, Copy, Debug)]
struct Edge {
x0: f64,
y0: f64,
x1: f64,
y1: f64,
dx: f64,
dy: f64,
}
impl Edge {
fn new(mut a: Point<f64>, mut b: Point<f64>) -> Self {
if a.y > b.y {
std::mem::swap(&mut a, &mut b);
}
Edge { x0: a.x, y0: a.y, x1: b.x, y1: b.y, dx: b.x - a.x, dy: b.y - a.y }
}
}
fn scan_spans(mut e0: Edge, mut e1: Edge, ymin: i32, ymax: i32, scan_line: &mut ScanLine<'_>) {
let y0 = fmax(f64::from(ymin), e1.y0.floor());
let y1 = fmin(f64::from(ymax), e1.y1.ceil());
let swap = if e0.x0 == e1.x0 && e0.y0 == e1.y0 {
e0.x0 + e1.dy / e0.dy * e0.dx < e1.x1
} else {
e0.x1 - e1.dy / e0.dy * e0.dx < e1.x0
};
if swap {
std::mem::swap(&mut e0, &mut e1);
}
let m0 = e0.dx / e0.dy;
let m1 = e1.dx / e1.dy;
let d0 = if e0.dx > 0.0 { 1.0 } else { 0.0 }; let d1 = if e1.dx < 0.0 { 1.0 } else { 0.0 }; let mut y = y0;
while y < y1 {
let x0 = m0 * fmax(0.0, fmin(e0.dy, y + d0 - e0.y0)) + e0.x0;
let x1 = m1 * fmax(0.0, fmin(e1.dy, y + d1 - e1.y0)) + e1.x0;
scan_line(x1.floor() as i32, x0.ceil() as i32, y as i32);
y += 1.0;
}
}
fn scan_triangle(a: &Point<f64>, b: &Point<f64>, c: &Point<f64>, ymin: i32, ymax: i32, scan_line: &mut ScanLine<'_>) {
let mut ab = Edge::new(*a, *b);
let mut bc = Edge::new(*b, *c);
let mut ca = Edge::new(*c, *a);
if ab.dy > bc.dy {
std::mem::swap(&mut ab, &mut bc);
}
if ab.dy > ca.dy {
std::mem::swap(&mut ab, &mut ca);
}
if bc.dy > ca.dy {
std::mem::swap(&mut bc, &mut ca);
}
if ab.dy != 0.0 {
scan_spans(ca, ab, ymin, ymax, scan_line);
}
if bc.dy != 0.0 {
scan_spans(ca, bc, ymin, ymax, scan_line);
}
}
fn tile_cover_points(
tl: &Point<f64>,
tr: &Point<f64>,
br: &Point<f64>,
bl: &Point<f64>,
c: &Point<f64>,
z: u8,
) -> Vec<UnwrappedTileID> {
#[derive(Clone, Copy, Debug)]
struct ID {
x: i32,
y: i32,
sq_dist: f64,
}
let tiles: i32 = 1i32.wrapping_shl(u32::from(z));
let mut t: Vec<ID> = Vec::with_capacity(8);
{
let mut scan_line = |x0: i32, x1: i32, y: i32| {
if y >= 0 && y <= tiles {
let mut x = x0;
while x < x1 {
let dx = f64::from(x) + 0.5 - c.x;
let dy = f64::from(y) + 0.5 - c.y;
t.push(ID { x, y, sq_dist: dx * dx + dy * dy });
x += 1;
}
}
};
scan_triangle(tl, tr, br, 0, tiles, &mut scan_line);
scan_triangle(br, bl, tl, 0, tiles, &mut scan_line);
}
t.sort_unstable_by(|a, b| a.sq_dist.total_cmp(&b.sq_dist).then((a.x, a.y).cmp(&(b.x, b.y))));
t.dedup_by(|b, a| a.x == b.x && a.y == b.y);
let mut result = Vec::with_capacity(t.len());
for id in &t {
result.push(UnwrappedTileID::new(z, i64::from(id.x), i64::from(id.y)));
}
result
}
pub struct TileCover {
impl_: Box<Impl>,
}
impl TileCover {
#[must_use]
pub fn new_from_bounds(bounds_: &LatLngBounds, z: u8) -> TileCover {
let mut bounds = LatLngBounds::hull(
bounds_lat_lng(f64::max(bounds_.south(), -constants::LATITUDE_MAX), bounds_.west()),
bounds_lat_lng(f64::min(bounds_.north(), constants::LATITUDE_MAX), bounds_.east()),
);
if bounds.is_empty() || bounds.south() > constants::LATITUDE_MAX || bounds.north() < -constants::LATITUDE_MAX {
bounds = LatLngBounds::world();
}
let sw = Projection::project_zoom(&bounds.southwest(), i32::from(z));
let ne = Projection::project_zoom(&bounds.northeast(), i32::from(z));
let se = Projection::project_zoom(&bounds.southeast(), i32::from(z));
let nw = Projection::project_zoom(&bounds.northwest(), i32::from(z));
let p = Polygon::new(LineString([sw, nw, ne, se, sw].map(Coord::from).to_vec()), Vec::new());
TileCover {
impl_: Box::new(Impl::new(i32::from(z), &Geometry::Polygon(p), false).expect("unprojected tile cover")),
}
}
pub fn new_from_geometry(geom: &Geometry<f64>, z: u8, project: bool) -> Result<TileCover, Error> {
Ok(TileCover { impl_: Box::new(Impl::new(i32::from(z), geom, project)?) })
}
#[must_use]
pub fn has_next(&self) -> bool {
self.impl_.has_next()
}
}
impl Iterator for TileCover {
type Item = UnwrappedTileID;
fn next(&mut self) -> Option<UnwrappedTileID> {
self.impl_.next()
}
}
#[derive(Clone, Debug)]
pub(crate) struct TileCoverParameters {
pub(crate) transform_state: TransformState,
pub(crate) tile_lod_min_radius: f64,
pub(crate) tile_lod_scale: f64,
pub(crate) tile_lod_pitch_threshold: f64,
pub(crate) tile_lod_mode: TileLodMode,
pub(crate) requested_zoom: Option<f64>,
pub(crate) round_zoom: bool,
pub(crate) max_content_elevation: f64,
}
impl TileCoverParameters {
#[cfg(test)]
pub(crate) fn new(transform_state: TransformState) -> Self {
TileCoverParameters {
transform_state,
tile_lod_min_radius: 3.0,
tile_lod_scale: 1.0,
tile_lod_pitch_threshold: (60.0 / 180.0) * std::f64::consts::PI,
tile_lod_mode: TileLodMode::Default,
requested_zoom: None,
round_zoom: false,
max_content_elevation: 0.0,
}
}
}
#[must_use]
pub fn covering_zoom_level(zoom: f64, round_zoom: bool, size: u16) -> i32 {
let mut zoom = zoom;
zoom += (constants::TILE_SIZE_D / f64::from(size)).log2();
if round_zoom { zoom.round() as i32 } else { zoom.floor() as i32 }
}
mod globe {
use std::cmp::Ordering;
use std::f64::consts::PI;
use geometry::Point;
use mln_map_types::VerticalPerspectiveProjection;
use mln_tile_id::{CanonicalTileID, OverscaledTileID, UnwrappedTileID};
use mln_util::bounding_volumes::{Frustum, IntersectionResult};
use mln_util::constants;
use mln_util::mat3::{vec3_cross, vec3_dot, vec3_normalize, vec3_scale, vec3_sub};
use mln_util::mat4::{Mat4, matrix};
use mln_util::projection::Projection;
use mln_util::range::Range;
use mln_util::vectors::{Vec3, Vec4};
use super::TileCoverParameters;
use crate::util::tile_lod::{TileZoomFunction, elevation_for_tile_culling};
fn clamp(value: f64, lo: f64, hi: f64) -> f64 {
if value < lo {
lo
} else if hi < value {
hi
} else {
value
}
}
const SCREEN_SIZE_ZOOM_BIAS: f64 = 0.5;
struct ConvexVolume {
points: [Vec3; 8],
point_count: usize,
planes: [Vec4; 6],
}
impl ConvexVolume {
fn new() -> Self {
ConvexVolume { points: [[0.0; 3]; 8], point_count: 0, planes: [[0.0; 4]; 6] }
}
fn add_point(&mut self, point: Vec3) {
debug_assert!(self.point_count < self.points.len());
self.points[self.point_count] = point;
self.point_count += 1;
}
fn points(&self) -> &[Vec3] {
&self.points[..self.point_count]
}
}
fn passes(plane: &Vec4, point: &Vec3) -> bool {
plane[0] * point[0] + plane[1] * point[1] + plane[2] * point[2] + plane[3] >= 0.0
}
fn intersects_frustum(volume: &ConvexVolume, frustum: &Frustum) -> IntersectionResult {
let mut fully_inside = true;
for plane in frustum.get_planes() {
let passed = volume.points().iter().filter(|point| passes(plane, point)).count();
if passed == 0 {
return IntersectionResult::Separate;
}
if passed < volume.point_count {
fully_inside = false;
}
}
if fully_inside {
return IntersectionResult::Contains;
}
for plane in &volume.planes {
let passed = frustum.get_points().iter().filter(|point| passes(plane, point)).count();
if passed == 0 {
return IntersectionResult::Separate;
}
}
IntersectionResult::Intersects
}
fn intersects_plane(volume: &ConvexVolume, plane: &Vec4) -> IntersectionResult {
let positive = volume.points().iter().filter(|point| passes(plane, point)).count();
if positive == volume.point_count {
return IntersectionResult::Contains;
}
if positive == 0 {
return IntersectionResult::Separate;
}
IntersectionResult::Intersects
}
fn is_tile_visible(frustum: &Frustum, volume: &ConvexVolume, plane: &Vec4) -> IntersectionResult {
let frustum_test = intersects_frustum(volume, frustum);
if frustum_test == IntersectionResult::Separate {
return frustum_test;
}
let plane_test = intersects_plane(volume, plane);
if plane_test == IntersectionResult::Separate {
return IntersectionResult::Separate;
}
if frustum_test == IntersectionResult::Contains && plane_test == IntersectionResult::Contains {
return IntersectionResult::Contains;
}
IntersectionResult::Intersects
}
fn three_plane_intersection(p0: &Vec4, p1: &Vec4, p2: &Vec4) -> Vec3 {
let n0: Vec3 = [p0[0], p0[1], p0[2]];
let n1: Vec3 = [p1[0], p1[1], p1[2]];
let n2: Vec3 = [p2[0], p2[1], p2[2]];
let det = vec3_dot(&n0, &vec3_cross(&n1, &n2));
if det == 0.0 {
return [0.0, 0.0, 0.0];
}
let mut sum = vec3_scale(&vec3_cross(&n1, &n2), -p0[3]);
sum = vec3_sub(&sum, &vec3_scale(&vec3_cross(&n2, &n0), p1[3]));
sum = vec3_sub(&sum, &vec3_scale(&vec3_cross(&n0, &n1), p2[3]));
vec3_scale(&sum, 1.0 / det)
}
fn aabb_volume(min: &Vec3, max: &Vec3) -> ConvexVolume {
let mut volume = ConvexVolume::new();
for i in 0..8 {
volume.add_point([
if i & 1 != 0 { max[0] } else { min[0] },
if i & 2 != 0 { max[1] } else { min[1] },
if i & 4 != 0 { max[2] } else { min[2] },
]);
}
volume.planes = [
[-1.0, 0.0, 0.0, max[0]],
[1.0, 0.0, 0.0, -min[0]],
[0.0, -1.0, 0.0, max[1]],
[0.0, 1.0, 0.0, -min[1]],
[0.0, 0.0, -1.0, max[2]],
[0.0, 0.0, 1.0, -min[2]],
];
volume
}
fn axis_min_max(axis: &Vec3, points: &[Vec3]) -> (f64, f64) {
let mut min = f64::INFINITY;
let mut max = f64::NEG_INFINITY;
for point in points {
let dot = vec3_dot(axis, point);
min = f64::min(min, dot);
max = f64::max(max, dot);
}
(min, max)
}
fn to_sphere(x: f64, y: f64, tile: &CanonicalTileID) -> Vec3 {
VerticalPerspectiveProjection::tile_coordinates_to_sphere(
&Point::new(x, y),
&UnwrappedTileID::with_wrap(0, *tile),
)
}
fn tile_bounding_volume(tile: &CanonicalTileID, elevation: f64) -> ConvexVolume {
let min_radius = f64::min(0.0, elevation) / constants::EARTH_RADIUS_M + 1.0;
let max_radius = f64::max(0.0, elevation) / constants::EARTH_RADIUS_M + 1.0;
if tile.z == 0 {
return aabb_volume(&[-max_radius, -max_radius, -max_radius], &[max_radius, max_radius, max_radius]);
}
if tile.z == 1 {
return aabb_volume(
&[
if tile.x == 0 { -max_radius } else { 0.0 },
if tile.y == 0 { 0.0 } else { -max_radius },
-max_radius,
],
&[if tile.x == 0 { 0.0 } else { max_radius }, if tile.y == 0 { max_radius } else { 0.0 }, max_radius],
);
}
let extent = f64::from(constants::EXTENT);
let corners: [Vec3; 4] = [
to_sphere(0.0, 0.0, tile),
to_sphere(extent, 0.0, tile),
to_sphere(extent, extent, tile),
to_sphere(0.0, extent, tile),
];
let mut extremes: [Vec3; 11] = [[0.0; 3]; 11];
let mut extreme_count = 0;
for corner in &corners {
extremes[extreme_count] = vec3_scale(corner, max_radius);
extreme_count += 1;
}
if max_radius != min_radius {
for corner in &corners {
extremes[extreme_count] = vec3_scale(corner, min_radius);
extreme_count += 1;
}
}
let last_row: u32 = (1u32 << tile.z) - 1;
if tile.y == 0 {
extremes[extreme_count] = [0.0, 1.0, 0.0];
extreme_count += 1;
}
if tile.y == last_row {
extremes[extreme_count] = [0.0, -1.0, 0.0];
extreme_count += 1;
}
let center = to_sphere(extent / 2.0, extent / 2.0, tile);
let center_east = vec3_normalize(&vec3_cross(&[0.0, 1.0, 0.0], ¢er));
let north = vec3_normalize(&vec3_cross(¢er, ¢er_east));
let axis_east = vec3_normalize(&vec3_cross(&corners[2], &corners[1]));
let axis_west = vec3_normalize(&vec3_cross(&corners[0], &corners[3]));
extremes[extreme_count] = vec3_scale(¢er, max_radius);
extreme_count += 1;
extremes[extreme_count] = if tile.y >= (1u32 << tile.z) / 2 {
vec3_scale(&to_sphere(extent / 2.0, 0.0, tile), max_radius)
} else {
vec3_scale(&to_sphere(extent / 2.0, extent, tile), max_radius)
};
extreme_count += 1;
let extreme_span = &extremes[..extreme_count];
let (up_min, up_max) = axis_min_max(¢er, extreme_span);
let (north_min, north_max) = axis_min_max(&north, extreme_span);
let plane_up: Vec4 = [-center[0], -center[1], -center[2], up_max];
let plane_down: Vec4 = [center[0], center[1], center[2], -up_min];
let plane_north: Vec4 = [-north[0], -north[1], -north[2], north_max];
let plane_south: Vec4 = [north[0], north[1], north[2], -north_min];
let plane_east: Vec4 = [axis_east[0], axis_east[1], axis_east[2], 0.0];
let plane_west: Vec4 = [axis_west[0], axis_west[1], axis_west[2], 0.0];
let mut volume = ConvexVolume::new();
if tile.y == 0 {
volume.add_point(three_plane_intersection(&plane_west, &plane_east, &plane_up));
volume.add_point(three_plane_intersection(&plane_west, &plane_east, &plane_down));
} else {
volume.add_point(three_plane_intersection(&plane_north, &plane_east, &plane_up));
volume.add_point(three_plane_intersection(&plane_north, &plane_east, &plane_down));
volume.add_point(three_plane_intersection(&plane_north, &plane_west, &plane_up));
volume.add_point(three_plane_intersection(&plane_north, &plane_west, &plane_down));
}
if tile.y == last_row {
volume.add_point(three_plane_intersection(&plane_west, &plane_east, &plane_up));
volume.add_point(three_plane_intersection(&plane_west, &plane_east, &plane_down));
} else {
volume.add_point(three_plane_intersection(&plane_south, &plane_east, &plane_up));
volume.add_point(three_plane_intersection(&plane_south, &plane_east, &plane_down));
volume.add_point(three_plane_intersection(&plane_south, &plane_west, &plane_up));
volume.add_point(three_plane_intersection(&plane_south, &plane_west, &plane_down));
}
volume.planes = [plane_up, plane_down, plane_north, plane_south, plane_east, plane_west];
volume
}
fn distance_to_tile_simple(point: f64, tile: f64, tile_size: f64) -> f64 {
let delta = point - tile;
if delta < 0.0 { -delta } else { f64::max(0.0, delta - tile_size) }
}
fn distance_to_tile_wrap_x(point_x: f64, point_y: f64, corner_x: f64, corner_y: f64, tile_size: f64) -> f64 {
let to_point_x = point_x - corner_x;
let distance_x = if to_point_x < 0.0 {
f64::min(-to_point_x, 1.0 + to_point_x - tile_size)
} else if to_point_x > tile_size {
f64::min(f64::max(to_point_x - tile_size, 0.0), 1.0 - to_point_x)
} else {
0.0
};
f64::max(distance_x, distance_to_tile_simple(point_y, corner_y, tile_size))
}
fn distance_to_tile_2d(point_x: f64, point_y: f64, tile: &CanonicalTileID) -> f64 {
let scale = f64::from(1u32 << tile.z);
let tile_size = 1.0 / scale;
let corner_x = f64::from(tile.x) / scale;
let corner_y = f64::from(tile.y) / scale;
let mut smallest = 2.0;
smallest = f64::min(smallest, distance_to_tile_wrap_x(point_x, point_y, corner_x, corner_y, tile_size));
smallest = f64::min(
smallest,
distance_to_tile_wrap_x(point_x, point_y, corner_x + 0.5, -corner_y - tile_size, tile_size),
);
smallest = f64::min(
smallest,
distance_to_tile_wrap_x(point_x, point_y, corner_x + 0.5, 2.0 - corner_y - tile_size, tile_size),
);
smallest
}
struct ScreenScale<'a> {
matrix: &'a Mat4,
horizon: &'a Vec4,
half_viewport: [f64; 2],
nadir: [f64; 2],
}
impl<'a> ScreenScale<'a> {
fn new(matrix: &'a Mat4, horizon: &'a Vec4, half_viewport: [f64; 2], camera: &Vec3) -> Self {
let length = vec3_dot(camera, camera).sqrt();
let x = (camera[0].atan2(camera[2]) / (2.0 * PI) - 0.5).rem_euclid(1.0);
let y = 0.5 - clamp(camera[1] / length, -1.0, 1.0).atanh() / (2.0 * PI);
ScreenScale { matrix, horizon, half_viewport, nadir: [x, clamp(y, 0.0, 1.0)] }
}
fn to_screen(&self, point: &Vec3) -> Option<[f64; 2]> {
let clip = matrix::transform_mat4_value(&[point[0], point[1], point[2], 1.0], self.matrix);
(clip[3] > 0.0)
.then(|| [clip[0] / clip[3] * self.half_viewport[0], clip[1] / clip[3] * self.half_viewport[1]])
}
fn pixels_per_radian(&self, point: &Vec3, east: &Vec3, north: &Vec3) -> Option<f64> {
const STEP: f64 = 1e-4;
let stepped = |along: &Vec3| -> Vec3 {
[point[0] + along[0] * STEP, point[1] + along[1] * STEP, point[2] + along[2] * STEP]
};
let origin = self.to_screen(point)?;
let to_east = self.to_screen(&stepped(east))?;
let to_north = self.to_screen(&stepped(north))?;
let area = (to_east[0] - origin[0]) * (to_north[1] - origin[1])
- (to_east[1] - origin[1]) * (to_north[0] - origin[0]);
Some(area.abs().sqrt() / STEP)
}
}
fn projected_tile_size(tile: &CanonicalTileID, view: &ScreenScale) -> f64 {
const GRID: usize = 3;
let extent = f64::from(constants::EXTENT);
let tiles = f64::from(1u32 << tile.z);
let mut samples = [[0.0f64; 2]; GRID * GRID + 1];
for (index, sample) in samples.iter_mut().take(GRID * GRID).enumerate() {
let step = extent / (GRID - 1) as f64;
*sample = [(index % GRID) as f64 * step, (index / GRID) as f64 * step];
}
let tile_center_x = (f64::from(tile.x) + 0.5) / tiles;
let nadir_x = view.nadir[0] - (view.nadir[0] - tile_center_x).round();
samples[GRID * GRID] = [
clamp((nadir_x * tiles - f64::from(tile.x)) * extent, 0.0, extent),
clamp((view.nadir[1] * tiles - f64::from(tile.y)) * extent, 0.0, extent),
];
let mut largest: Option<f64> = None;
for sample in &samples {
let point = to_sphere(sample[0], sample[1], tile);
if !passes(view.horizon, &point) {
continue;
}
let latitude_radius = point[0].hypot(point[2]);
let mut size = 0.0;
if latitude_radius > 1e-9 {
let east: Vec3 = [point[2] / latitude_radius, 0.0, -point[0] / latitude_radius];
let north = vec3_cross(&point, &east);
let Some(scale) = view.pixels_per_radian(&point, &east, &north) else {
return f64::INFINITY;
};
size = scale * 2.0 * PI * latitude_radius / tiles;
}
largest = Some(f64::max(largest.unwrap_or(0.0), size));
}
largest.unwrap_or(f64::INFINITY)
}
fn wrap_for(center_x: f64, tile: &CanonicalTileID) -> i16 {
let scale = f64::from(1u32 << tile.z);
let tile_size = 1.0 / scale;
let tile_x = f64::from(tile.x) / scale;
let current = distance_to_tile_simple(center_x, tile_x, tile_size);
let left = distance_to_tile_simple(center_x, tile_x - 1.0, tile_size);
let right = distance_to_tile_simple(center_x, tile_x + 1.0, tile_size);
let smallest = f64::min(f64::min(current, left), right);
if smallest == right {
return 1;
}
if smallest == left {
return -1;
}
0
}
pub(super) fn tile_cover(
state: &TileCoverParameters,
z: u8,
zoom_range: Range<u8>,
overscaled_z: Option<u8>,
) -> Vec<OverscaledTileID> {
#[derive(Clone, Copy, Debug)]
struct Node {
zoom: u8,
x: u32,
y: u32,
wrap: i16,
fully_visible: bool,
}
#[derive(Clone, Copy, Debug)]
struct ResultTile {
id: OverscaledTileID,
sqr_dist: f64,
}
let transform = &state.transform_state;
let projection = transform.get_projection_data(&UnwrappedTileID::new(0, 0, 0));
let mut inverse: Mat4 = [0.0; 16];
if matrix::invert(&mut inverse, &projection.main_matrix) {
return Vec::new();
}
let horizon = projection.clipping_plane;
let mut frustum = Frustum::from_inv_proj_matrix(&inverse, 1.0, 0.0, true, Some(&horizon));
let mut culling_horizon = horizon;
let normal_length = horizon[0].hypot(horizon[1]).hypot(horizon[2]);
if state.max_content_elevation > 0.0 && normal_length > 0.0 {
let shell_radius = 1.0 + state.max_content_elevation / constants::EARTH_RADIUS_M;
let horizon_cos = (-horizon[3] / normal_length).clamp(-1.0, 1.0);
let shell_cos = (horizon_cos.acos() + (1.0 / shell_radius).acos()).cos();
culling_horizon[3] = -shell_cos * normal_length;
frustum = frustum.with_far_plane_pushed(
(shell_radius * shell_radius - 1.0).sqrt(),
&transform.get_globe_camera_position(),
);
}
let max_zoom = zoom_range.max;
let min_zoom = zoom_range.min;
let overscaled_zoom = std::cmp::max(overscaled_z.unwrap_or(z), max_zoom);
let num_tiles = 2.0f64.powf(f64::from(z));
let transform_zoom = transform.get_zoom();
let requested_center_zoom =
state.requested_zoom.unwrap_or(transform_zoom + (f64::from(z) - transform_zoom.floor()));
let whole_zoom = |zoom: f64| if state.round_zoom { zoom.round() } else { zoom.floor() };
let allow_variable_zoom = whole_zoom(requested_center_zoom) > 4.0;
let center = Projection::project(
&transform.get_lat_lng(mln_util::geo::WrapMode::Unwrapped),
1.0 / constants::TILE_SIZE_D,
);
let center_coord: Vec3 = [center.x, center.y, 0.0];
let center_world = center_coord[0].floor();
let first_copy_center_x = center_coord[0] - center_world;
let camera_position = transform.get_free_camera_options().position;
debug_assert!(camera_position.is_some());
let Some(camera_position) = camera_position else {
return Vec::new();
};
let mut camera_coord = camera_position;
let latitude = transform.get_lat_lng(mln_util::geo::WrapMode::Unwrapped).latitude();
if latitude.abs() > constants::LATITUDE_MAX {
camera_coord[1] += center.y - (0.5 - mln_math::deg2rad(latitude).tan().asinh() / constants::M2PI);
}
let distance_to_center_2d = (center_coord[0] - camera_coord[0]).hypot(center_coord[1] - camera_coord[1]);
let distance_z = (center_coord[2] - camera_coord[2]).abs();
let distance_to_center_3d = distance_to_center_2d.hypot(distance_z);
let fov_degrees = f64::from(transform.get_field_of_view()) * 180.0 / PI;
let tile_zoom = TileZoomFunction::new(
fov_degrees,
TileZoomFunction::DEFAULT_MAX_ZOOM_LEVELS_ON_SCREEN,
TileZoomFunction::DEFAULT_TILE_COUNT_MAX_MIN_RATIO,
);
let culling_elevation = elevation_for_tile_culling(
transform.get_pitch() * 180.0 / PI,
fov_degrees,
0.0,
state.max_content_elevation,
);
let nominal_tile_size = constants::TILE_SIZE_D * 2.0f64.powf(transform_zoom - requested_center_zoom);
let size = transform.get_size();
let screen_scale = ScreenScale::new(
&projection.main_matrix,
&horizon,
[f64::from(size.width) / 2.0, f64::from(size.height) / 2.0],
&transform.get_globe_camera_position(),
);
let children = |node: &Node| -> [Node; 4] {
std::array::from_fn(|i| Node {
zoom: node.zoom + 1,
x: (node.x << 1) + (i as u32 % 2),
y: (node.y << 1) + (i as u32 >> 1),
wrap: node.wrap,
fully_visible: node.fully_visible,
})
};
let mut stack: Vec<Node> = Vec::with_capacity(64);
let mut result: Vec<ResultTile> = Vec::with_capacity(64);
stack.push(Node { zoom: 0, x: 0, y: 0, wrap: 0, fully_visible: false });
while let Some(mut node) = stack.pop() {
let tile = CanonicalTileID::new(node.zoom, node.x, node.y);
let volume = tile_bounding_volume(&tile, culling_elevation);
if !node.fully_visible {
let intersection = is_tile_visible(&frustum, &volume, &culling_horizon);
if intersection == IntersectionResult::Separate {
continue;
}
node.fully_visible = intersection == IntersectionResult::Contains;
}
let mut desired_z = f64::from(z);
if allow_variable_zoom {
let dist_to_tile_2d = distance_to_tile_2d(camera_coord[0] - center_world, camera_coord[1], &tile);
desired_z = whole_zoom(tile_zoom.call(
requested_center_zoom,
dist_to_tile_2d,
distance_z,
distance_to_center_3d,
));
}
let target_zoom = clamp(desired_z, 0.0, f64::from(max_zoom)) as u8;
let small_on_screen = node.zoom < target_zoom && node.zoom >= min_zoom && {
let size = projected_tile_size(&tile, &screen_scale);
whole_zoom((size / nominal_tile_size).log2() + SCREEN_SIZE_ZOOM_BIAS) <= 0.0
};
if small_on_screen
&& !node.fully_visible
&& children(&node).iter().all(|child| {
let volume =
tile_bounding_volume(&CanonicalTileID::new(child.zoom, child.x, child.y), culling_elevation);
is_tile_visible(&frustum, &volume, &culling_horizon) == IntersectionResult::Separate
})
{
continue;
}
if node.zoom >= target_zoom || small_on_screen {
if node.zoom < min_zoom {
continue;
}
node.wrap = (f64::from(wrap_for(first_copy_center_x, &tile)) + center_world) as i16;
let dx = num_tiles * first_copy_center_x - 0.5 - f64::from(node.x);
let dy = num_tiles * center_coord[1] - 0.5 - f64::from(node.y);
result.push(ResultTile {
id: OverscaledTileID::with_zxy(
if node.zoom == max_zoom { overscaled_zoom } else { node.zoom },
node.wrap,
node.zoom,
node.x,
node.y,
),
sqr_dist: dx * dx + dy * dy,
});
continue;
}
stack.extend(children(&node));
}
result.sort_by(|a, b| {
if a.sqr_dist < b.sqr_dist {
Ordering::Less
} else if b.sqr_dist < a.sqr_dist {
Ordering::Greater
} else {
Ordering::Equal
}
});
result.iter().map(|tile| tile.id).collect()
}
pub(super) fn level_cover(
state: &TileCoverParameters,
z: u8,
zoom_range: Range<u8>,
overscaled_z: Option<u8>,
) -> Vec<OverscaledTileID> {
let center = Projection::project(
&state.transform_state.get_lat_lng(mln_util::geo::WrapMode::Unwrapped),
1.0 / constants::TILE_SIZE_D,
);
let center_world = center.x.floor();
let first_copy_center_x = center.x - center_world;
let overscaled_zoom =
if z == zoom_range.max { std::cmp::max(overscaled_z.unwrap_or(z), zoom_range.max) } else { z };
let tiles_per_side = 1u32 << z;
let mut result = Vec::with_capacity((tiles_per_side * tiles_per_side) as usize);
for y in 0..tiles_per_side {
for x in 0..tiles_per_side {
let wrap =
(f64::from(wrap_for(first_copy_center_x, &CanonicalTileID::new(z, x, y))) + center_world) as i16;
result.push(OverscaledTileID::with_zxy(overscaled_zoom, wrap, z, x, y));
}
}
result
}
}
pub(crate) fn globe_level_cover(
state: &TileCoverParameters,
z: u8,
zoom_range: Range<u8>,
overscaled_z: Option<u8>,
) -> Vec<OverscaledTileID> {
globe::level_cover(state, z, zoom_range, overscaled_z)
}
pub(crate) fn tile_cover(
state: &TileCoverParameters,
z: u8,
zoom_range: Range<u8>,
overscaled_z: Option<u8>,
) -> Vec<OverscaledTileID> {
#[derive(Clone, Copy, Debug)]
struct Node {
aabb: AABB,
zoom: u8,
x: u32,
y: u32,
wrap: i16,
fully_visible: bool,
}
#[derive(Clone, Copy, Debug)]
struct ResultTile {
id: OverscaledTileID,
sqr_dist: f64,
}
if state.transform_state.is_globe_rendering() {
return globe::tile_cover(state, z, zoom_range, overscaled_z);
}
let children_of = |node: &Node| -> Vec<Node> {
let mut children = vec![*node; 4];
for i in 0..4u32 {
let child_x = (node.x << 1).wrapping_add(i % 2);
let child_y = (node.y << 1).wrapping_add(i >> 1);
let child = &mut children[i as usize];
*child = *node;
child.aabb = node.aabb.quadrant(i as i32);
child.zoom = node.zoom.wrapping_add(1);
child.x = child_x;
child.y = child_y;
}
children
};
let transform = &state.transform_state;
let num_tiles = 2.0f64.powf(f64::from(z));
let world_size = Projection::world_size(transform.get_scale());
let allow_variable_zoom = transform.get_pitch() > state.tile_lod_pitch_threshold;
let min_zoom: u8 = if allow_variable_zoom { zoom_range.min } else { z };
let max_zoom: u8 =
if (state.tile_lod_mode == TileLodMode::Distance) && allow_variable_zoom { zoom_range.max } else { z };
let overscaled_zoom: u8 = std::cmp::max(overscaled_z.unwrap_or(z), max_zoom);
let flipped_y = transform.get_viewport_mode() == ViewportMode::FlippedY;
let size = transform.get_size();
let center_point = TileCoordinate::from_screen_coordinate(
transform,
z,
&ScreenCoordinate::new(f64::from(size.width) / 2.0, f64::from(size.height) / 2.0),
)
.p;
let center_coord: Vec3 = [center_point.x, center_point.y, 0.0];
let camera_position = transform.get_free_camera_options().position;
debug_assert!(camera_position.is_some());
let Some(camera_position_mercator) = camera_position else {
return Vec::new();
};
let nominal_scale = 2.0f64.powf(f64::from(z));
let camera_coord = vec3_scale(&camera_position_mercator, nominal_scale);
let camera_to_center_distance_mercator = vec3_length(&vec3_sub(&camera_coord, ¢er_coord)) / world_size;
let frustum = Frustum::from_inv_proj_matrix(
&transform.get_inv_projection_matrix(),
world_size,
f64::from(z),
flipped_y,
None,
);
debug_assert!(state.tile_lod_min_radius >= 1.0);
let radius_of_max_lvl_lod_in_tiles = f64::max(1.0, state.tile_lod_min_radius);
let new_root_tile = |wrap: i16| -> Node {
Node {
aabb: AABB::new(
[f64::from(wrap) * num_tiles, 0.0, 0.0],
[f64::from(i32::from(wrap) + 1) * num_tiles, num_tiles, 0.0],
),
zoom: 0,
x: 0,
y: 0,
wrap,
fully_visible: false,
}
};
let mut stack: Vec<Node> = Vec::new();
let mut result: Vec<ResultTile> = Vec::new();
stack.reserve(128);
for i in 1..=3i16 {
stack.push(new_root_tile(-i));
stack.push(new_root_tile(i));
}
stack.push(new_root_tile(0));
while let Some(mut node) = stack.pop() {
if !node.fully_visible {
let intersection = frustum.intersects(&node.aabb);
if intersection == IntersectionResult::Separate {
continue;
}
node.fully_visible = intersection == IntersectionResult::Contains;
}
let should_split_tile = if state.tile_lod_mode == TileLodMode::Distance {
let cam_to_tile_mercator = vec3_scale(&node.aabb.distance_xyz(&camera_coord), 1.0 / world_size);
let distance_to_tile_mercator = vec3_length(&cam_to_tile_mercator);
let cos_pitch_to_tile = f64::max(0.0, cam_to_tile_mercator[2] / distance_to_tile_mercator);
let pitch_exponent = 0.5; let tile_scale = 2.0f64.powf(f64::from(node.zoom));
distance_to_tile_mercator * tile_scale
< cos_pitch_to_tile.powf(pitch_exponent) * camera_to_center_distance_mercator / state.tile_lod_scale
* nominal_scale
} else {
let distance_xyz = node.aabb.distance_xyz(¢er_coord);
let mut longest_dim = 0;
for i in 1..distance_xyz.len() {
if distance_xyz[longest_dim] < distance_xyz[i] {
longest_dim = i;
}
}
let dist_to_split = radius_of_max_lvl_lod_in_tiles
+ f64::from(1i32.wrapping_shl((i32::from(max_zoom) - i32::from(node.zoom)) as u32))
- 2.0;
distance_xyz[longest_dim] * state.tile_lod_scale < dist_to_split
};
if node.zoom == max_zoom || (!should_split_tile && node.zoom >= min_zoom) {
if node.fully_visible || frustum.intersects_precise(&node.aabb, true) != IntersectionResult::Separate {
let id = OverscaledTileID::with_zxy(
if node.zoom == max_zoom { overscaled_zoom } else { node.zoom },
node.wrap,
node.zoom,
node.x,
node.y,
);
let coord_to_load_first: Vec3 =
if state.tile_lod_mode == TileLodMode::Distance { camera_coord } else { center_coord };
let dx = f64::from(node.wrap) * num_tiles + f64::from(node.x) + 0.5 - coord_to_load_first[0];
let dy = f64::from(node.y) + 0.5 - coord_to_load_first[1];
result.push(ResultTile { id, sqr_dist: dx * dx + dy * dy });
}
} else {
let children = children_of(&node);
stack.extend(children);
}
}
sort_by_distance(&mut result, |tile| tile.sqr_dist);
let mut ids = Vec::with_capacity(result.len());
for tile in &result {
ids.push(tile.id);
}
ids
}
fn sort_by_distance<T>(tiles: &mut [T], sqr_dist: impl Fn(&T) -> f64) {
tiles.sort_by(|a, b| sqr_dist(a).total_cmp(&sqr_dist(b)));
}
#[must_use]
pub fn tile_cover_bounds(bounds_: &LatLngBounds, z: u8) -> Vec<UnwrappedTileID> {
if bounds_.is_empty() || bounds_.south() > constants::LATITUDE_MAX || bounds_.north() < -constants::LATITUDE_MAX {
return Vec::new();
}
let bounds = LatLngBounds::hull(
bounds_lat_lng(f64::max(bounds_.south(), -constants::LATITUDE_MAX), bounds_.west()),
bounds_lat_lng(f64::min(bounds_.north(), constants::LATITUDE_MAX), bounds_.east()),
);
let zoom = i32::from(z);
tile_cover_points(
&Projection::project_zoom(&bounds.northwest(), zoom),
&Projection::project_zoom(&bounds.northeast(), zoom),
&Projection::project_zoom(&bounds.southeast(), zoom),
&Projection::project_zoom(&bounds.southwest(), zoom),
&Projection::project_zoom(&bounds.center(), zoom),
z,
)
}
#[cfg(test)]
pub(crate) fn tile_cover_geometry(geometry: &Geometry<f64>, z: u8) -> Result<Vec<UnwrappedTileID>, Error> {
let mut result = Vec::new();
let mut tc = TileCover::new_from_geometry(geometry, z, true)?;
while tc.has_next() {
result.push(tc.next().expect("has_next"));
}
Ok(result)
}
#[must_use]
pub fn tile_count_bounds(bounds: &LatLngBounds, zoom: u8) -> u64 {
if zoom == 0 {
return 1;
}
let sw = Projection::project_zoom(&bounds.southwest(), i32::from(zoom));
let ne = Projection::project_zoom(&bounds.northeast(), i32::from(zoom));
let max_tile = 2.0f64.powf(f64::from(zoom));
let x1 = sw.x.floor();
let x2 = ne.x.ceil() - 1.0;
let y1 = mln_math::clamp_f64(sw.y.floor(), 0.0, max_tile - 1.0);
let y2 = mln_math::clamp_f64(ne.y.floor(), 0.0, max_tile - 1.0);
let dx = if x1 > x2 { (max_tile - x1) + x2 } else { x2 - x1 };
let dy = y1 - y2;
((dx + 1.0) * (dy + 1.0)) as u64
}
pub fn tile_count_geometry(geometry: &Geometry<f64>, z: u8) -> Result<u64, Error> {
let mut tile_count: u64 = 0;
let mut tc = TileCover::new_from_geometry(geometry, z, true)?;
while tc.next().is_some() {
tile_count += 1;
}
Ok(tile_count)
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn sort_by_distance_keeps_ties_in_order() {
let distance = |i: u32| f64::from(3 - i % 4);
let mut tiles: Vec<u32> = (0..40).collect();
sort_by_distance(&mut tiles, |&i| distance(i));
let mut expected: Vec<u32> = (0..40).collect();
expected.sort_by_key(|&i| (3 - i % 4, i));
assert_eq!(expected, tiles);
}
#[test]
fn tile_count_geometry_counts_the_cover() {
let line =
vec![(-121.493_682_861_328_12, 38.579_037_146_674_59), (-122.442_283_630_371_1, 37.773_157_169_570_695)];
let geom = Geometry::LineString(line.into());
assert_eq!(7, tile_count_geometry(&geom, 10).unwrap());
assert_eq!(7, tile_cover_geometry(&geom, 10).unwrap().len());
assert!(tile_count_geometry(&Geometry::Point(geo_types::Point::new(0.0, 97.0)), 2).is_err());
}
}