use std::f64::consts::PI;
use geometry::Point;
use glam::dcamera::rh::proj::opengl::perspective;
use glam::{DMat3, DMat4, DQuat, DVec3, DVec4, EulerRot, Vec3Swizzles, Vec4Swizzles};
use mln_style::constants::{EXTENT, LATITUDE_MAX, TILE_SIZE_D};
use mln_style::geo::{LatLng, WrapMode};
use mln_style::interpolate::interpolate;
use crate::camera::projection::{ProjectedTilePoint, ProjectionData, mercator_tile_coords};
use crate::camera::transform_state::TransformState;
use crate::camera::view::{NorthOrientation, View};
use crate::geo::ScreenCoordinate;
use crate::geo::projection::Projection;
use crate::tile::UnwrappedTileID;
const POLE_AXIS_DISTANCE: f64 = 1e-6;
fn rotate_x(v: DVec3, rad: f64) -> DVec3 {
DMat3::from_rotation_x(rad) * v
}
fn rotate_y(v: DVec3, rad: f64) -> DVec3 {
DMat3::from_rotation_y(rad) * v
}
fn rotate_z(v: DVec3, rad: f64) -> DVec3 {
DMat3::from_rotation_z(rad) * v
}
fn line_of_sight_blocked(camera: DVec3, point: DVec3) -> bool {
let to_point = point - camera;
let length_sq = to_point.length_squared();
if length_sq == 0.0 {
return false;
}
let t = (-camera.dot(to_point) / length_sq).clamp(0.0, 1.0);
(camera + to_point * t).length_squared() < 1.0
}
fn ray_sphere_intersection(origin: DVec3, direction: DVec3) -> Option<f64> {
let origin_dot_direction = origin.dot(direction);
let inner = origin - direction * origin_dot_direction;
let discriminant = 1.0 - inner.length_squared();
if discriminant.is_nan() || discriminant < 0.0 {
return None;
}
let c = origin.length_squared() - 1.0;
let q = -origin_dot_direction + (if origin_dot_direction < 0.0 { 1.0 } else { -1.0 }) * discriminant.sqrt();
let t0 = c / q;
let t1 = q;
if f64::max(t0, t1) < 0.0 {
return None;
}
Some(f64::min(t0, t1))
}
fn point_plane_signed_distance(plane: DVec4, point: DVec3) -> f64 {
plane.dot(point.extend(1.0))
}
fn ray_direction_from_pixel(view: &View, point: &ScreenCoordinate, inverse_view_projection: &DMat4) -> DVec3 {
let size = view.size();
let clip =
DVec3::new(point.x / f64::from(size.width) * 2.0 - 1.0, point.y / f64::from(size.height) * 2.0 - 1.0, 1.0);
(inverse_view_projection.project_point3(clip) - view.globe_camera_position()).normalize()
}
fn angle_to_rotate_between_vectors_2d(mut x0: f64, mut y0: f64, mut x1: f64, mut y1: f64) -> f64 {
let len0 = (x0 * x0 + y0 * y0).sqrt();
let len1 = (x1 * x1 + y1 * y1).sqrt();
x0 /= len0;
y0 /= len0;
x1 /= len1;
y1 /= len1;
let cross = x0 * y1 - x1 * y0;
let angle = (x0 * x1 + y0 * y1).clamp(-1.0, 1.0).acos();
if cross > 0.0 { angle } else { -angle }
}
fn distance_of_angles_radians(a: f64, b: f64) -> f64 {
let two_pi = 2.0 * PI;
let d = ((a - b) % two_pi + two_pi) % two_pi;
f64::min(d, two_pi - d)
}
fn integrate_secant(x: f64) -> f64 {
let half = 0.5 * x;
let sin = half.sin();
let cos = half.cos();
(sin + cos).ln() - (cos - sin).ln()
}
fn sign(value: f64) -> i32 {
i32::from(value > 0.0) - i32::from(value < 0.0)
}
#[derive(Clone, Copy, Debug, Default)]
pub struct VerticalPerspectiveProjection;
#[derive(Clone, Copy, Debug, PartialEq)]
pub struct CenterAndBearing {
pub(crate) center: LatLng,
pub(crate) bearing: f64,
}
const RAY_SURFACE_DISTANCE_FOR_SLOWING_START: f64 = 0.3;
const SLOWING_MULTIPLIER: f64 = 0.5;
const INTERPOLATE_TO_HEURISTIC_START_LNG: f64 = 45.0;
const INTERPOLATE_TO_HEURISTIC_END_LNG: f64 = 85.0;
const INTERPOLATE_TO_HEURISTIC_EXPONENT: f64 = 0.25;
const INTERPOLATE_TO_HEURISTIC_START_HORIZON: f64 = 0.95;
const INTERPOLATE_TO_HEURISTIC_END_HORIZON: f64 = 0.999;
const SLOWING_RADIUS_START: f64 = 0.9;
const SLOWING_RADIUS_STOP: f64 = 0.5;
const SLOWING_RADIUS_SLOW_FACTOR: f64 = 0.25;
const POLE_AREA_FADE_SCALE: f64 = 4.0;
fn remap_saturate(value: f64, old_min: f64, old_max: f64, new_min: f64, new_max: f64) -> f64 {
let t = ((value - old_min) / (old_max - old_min)).clamp(0.0, 1.0);
new_min + (new_max - new_min) * t
}
impl VerticalPerspectiveProjection {
pub const GLOBE_RADIUS_METERS: f64 = 6_371_008.8;
#[must_use]
pub(crate) fn globe_radius_pixels(world_size: f64, center_latitude: f64) -> f64 {
world_size / (2.0 * PI) / Self::planet_scale(center_latitude)
}
#[must_use]
pub(crate) fn planet_scale(center_latitude: f64) -> f64 {
mln_style::math::deg2rad(f64::min(center_latitude.abs(), LATITUDE_MAX)).cos()
}
#[must_use]
pub fn tile_coordinates_to_sphere(tile_point: &Point<f64>, tile_id: &UnwrappedTileID) -> DVec3 {
let [sin_x, cos_x, sin_y, cos_y] = Self::tile_coordinates_to_angles(tile_point, tile_id).to_array();
DVec3::new(sin_x * cos_y, sin_y, cos_x * cos_y)
}
fn tile_coordinates_to_angles(tile_point: &Point<f64>, tile_id: &UnwrappedTileID) -> DVec4 {
let scale = 1.0 / (1u64 << tile_id.canonical.z) as f64;
let mercator_x = tile_point.x / f64::from(EXTENT) * scale + f64::from(tile_id.canonical.x) * scale;
let mercator_y = tile_point.y / f64::from(EXTENT) * scale + f64::from(tile_id.canonical.y) * scale;
let spherical_x = mercator_x * PI * 2.0 + PI;
let t = (PI - (mercator_y * PI * 2.0)).exp();
let t2 = t * t;
DVec4::new(spherical_x.sin(), spherical_x.cos(), (t2 - 1.0) / (t2 + 1.0), (2.0 * t) / (t2 + 1.0))
}
fn center_radius(view: &View) -> f64 {
1.0 + view.center_altitude() / Self::GLOBE_RADIUS_METERS
}
#[must_use]
pub(crate) fn globe_view_projection_matrix(view: &View, radius: f64) -> DMat4 {
let size = view.size();
let camera_to_center_distance = view.camera_to_center_distance();
let center = view.center(WrapMode::Unwrapped);
let near = if view.terrain_min_elevation().is_some() { view.near_z() } else { 0.5 };
let center_radius = Self::center_radius(view);
let mut projection = perspective(
view.perspective_fov(),
f64::from(size.width) / f64::from(size.height),
near,
camera_to_center_distance + radius * (center_radius + 1.0),
);
let offset = view.center_offset();
projection.z_axis.x = -offset.x * 2.0 / f64::from(size.width);
projection.z_axis.y = offset.y * 2.0 / f64::from(size.height);
if view.north_orientation() != NorthOrientation::Upwards {
projection *= DMat4::from_rotation_z(-view.north_orientation_angle());
}
projection
* DMat4::from_translation(DVec3::new(0.0, 0.0, -camera_to_center_distance))
* DMat4::from_rotation_z(view.roll())
* DMat4::from_rotation_x(-view.pitch())
* DMat4::from_rotation_z(-view.angle())
* DMat4::from_translation(DVec3::new(0.0, 0.0, -radius * center_radius))
* DMat4::from_rotation_x(mln_style::math::deg2rad(center.latitude()))
* DMat4::from_rotation_y(-mln_style::math::deg2rad(center.longitude()))
* DMat4::from_scale(DVec3::splat(radius))
}
#[must_use]
pub(crate) fn clipping_plane(view: &View, radius: f64) -> DVec4 {
let pitch = view.pitch();
let distance_camera_to_b = view.camera_to_center_distance() / radius;
let distance_camera_to_a = pitch.sin() * distance_camera_to_b;
let distance_a_to_c = pitch.cos() * distance_camera_to_b + Self::center_radius(view);
let distance_camera_to_c =
(distance_camera_to_a * distance_camera_to_a + distance_a_to_c * distance_a_to_c).sqrt();
let tangent_plane_distance_to_c = 1.0 / distance_camera_to_c;
let mut plane =
DVec3::new(0.0, -distance_camera_to_a / distance_camera_to_c, distance_a_to_c / distance_camera_to_c);
let center = view.center(WrapMode::Unwrapped);
plane = rotate_z(plane, view.angle());
plane = rotate_x(plane, -mln_style::math::deg2rad(center.latitude()));
plane = rotate_y(plane, mln_style::math::deg2rad(center.longitude()));
plane.extend(-tangent_plane_distance_to_c) / plane.length()
}
#[must_use]
pub(crate) fn camera_position(view: &View, radius: f64) -> DVec3 {
let center = view.center(WrapMode::Unwrapped);
let mut position = DVec3::new(0.0, 0.0, view.camera_to_center_distance() / radius);
position = rotate_z(position, -view.roll());
position = rotate_x(position, view.pitch());
position = rotate_z(position, view.angle());
position += DVec3::Z * Self::center_radius(view);
position = rotate_x(position, -mln_style::math::deg2rad(center.latitude()));
rotate_y(position, mln_style::math::deg2rad(center.longitude()))
}
#[must_use]
pub(crate) fn surface_vector(lat_lng: &LatLng) -> DVec3 {
let lng = mln_style::math::deg2rad(lat_lng.longitude());
let lat = mln_style::math::deg2rad(lat_lng.latitude());
let len = lat.cos();
DVec3::new(lng.sin() * len, lat.sin(), lng.cos() * len)
}
pub(crate) fn surface_vector_to_lat_lng(surface: DVec3) -> Option<LatLng> {
let latitude = mln_style::math::rad2deg(surface.y.clamp(-1.0, 1.0).asin());
let length_xz = surface.xz().length();
if length_xz <= POLE_AXIS_DISTANCE {
return LatLng::with_mode(latitude, 0.0, WrapMode::Unwrapped).ok();
}
let acos_z = (surface.z / length_xz).clamp(-1.0, 1.0).acos();
let longitude = mln_style::math::rad2deg(if surface.x > 0.0 { acos_z } else { -acos_z });
LatLng::with_mode(latitude, longitude, WrapMode::Wrapped).ok()
}
pub(crate) fn sphere_point_to_world(view: &View, point: DVec3) -> Option<DVec3> {
let length = point.length();
let location = view.lat_lng_near_center(&Self::surface_vector_to_lat_lng(point / length)?);
let world = Projection::project(&location, 1.0 / TILE_SIZE_D);
let latitude = location.latitude();
let y = if latitude.abs() > LATITUDE_MAX {
(180.0 - mln_style::math::rad2deg((PI / 4.0 + mln_style::math::deg2rad(latitude) / 2.0).tan().ln())) / 360.0
} else {
world.y
};
Some(DVec3::new(world.x, y, (length - 1.0) * Self::GLOBE_RADIUS_METERS))
}
pub(crate) fn screen_coordinate_to_surface(view: &View, point: &ScreenCoordinate, height: f64) -> DVec3 {
let origin = camera_over_sphere(view, height);
let direction = ray_direction_from_pixel(view, point, &view.inverse_globe_view_projection_matrix());
if let Some(t) = ray_sphere_intersection(origin, direction) {
return (origin + direction * t).normalize();
}
let plane = if height == 0.0 {
view.globe_clipping_plane()
} else {
let distance = origin.length();
(origin / distance).extend(-1.0 / distance)
};
let normal = plane.xyz();
let distance_to_intersection = -point_plane_signed_distance(plane, origin) / direction.dot(normal);
let plane_intersection = if distance_to_intersection > 0.0 {
origin + direction * distance_to_intersection
} else {
const MAX_RAY_LENGTH: f64 = 2.0;
let distant_point = origin + direction * MAX_RAY_LENGTH;
distant_point - normal * point_plane_signed_distance(plane, distant_point)
};
let horizon_center = normal * -plane.w;
let horizon_radius = f64::max(0.0, 1.0 - plane.w * plane.w).sqrt();
let relative = plane_intersection - horizon_center;
horizon_center + relative * (horizon_radius / relative.length())
}
#[must_use]
pub(crate) fn screen_coordinate_hits_globe(view: &View, point: &ScreenCoordinate, height: f64) -> bool {
let direction = ray_direction_from_pixel(view, point, &view.inverse_globe_view_projection_matrix());
ray_sphere_intersection(camera_over_sphere(view, height), direction).is_some()
}
pub(crate) fn screen_coordinate_to_lat_lng(
view: &View,
point: &ScreenCoordinate,
height: f64,
wrap_mode: WrapMode,
) -> Option<LatLng> {
let lat_lng = Self::surface_vector_to_lat_lng(Self::screen_coordinate_to_surface(view, point, height))?;
Some(if wrap_mode == WrapMode::Wrapped { lat_lng.wrapped() } else { lat_lng })
}
#[must_use]
pub(crate) fn lat_lng_to_screen_coordinate(
view: &View,
lat_lng: &LatLng,
height: f64,
) -> (ScreenCoordinate, DVec4) {
let sphere = Self::surface_vector(lat_lng) * (1.0 + height / Self::GLOBE_RADIUS_METERS);
let clip = view.globe_view_projection_matrix() * sphere.extend(1.0);
let size = view.size();
let point = Point::new(
(clip.x / clip.w * 0.5 + 0.5) * f64::from(size.width),
(clip.y / clip.w * 0.5 + 0.5) * f64::from(size.height),
);
(point, clip)
}
pub(crate) fn center_for_location_at_point(
view: &View,
lat_lng: &LatLng,
anchor: &ScreenCoordinate,
height: f64,
) -> Option<LatLng> {
const LIMIT: f64 = PI * 0.5;
let center = view.center(WrapMode::Unwrapped);
let vec_to_pixel_current = Self::screen_coordinate_to_surface(view, anchor, height);
let vec_to_target = Self::surface_vector(lat_lng);
let rotated_pixel_vector = rotate_x(
rotate_y(vec_to_pixel_current, -mln_style::math::deg2rad(center.longitude())),
mln_style::math::deg2rad(center.latitude()),
);
let vec_to_target_xz_length_squared = vec_to_target.xz().length_squared();
let target_x_squared = rotated_pixel_vector.x * rotated_pixel_vector.x;
if vec_to_target_xz_length_squared < target_x_squared {
return None;
}
let intersection_a = (vec_to_target_xz_length_squared - target_x_squared).sqrt();
let intersection_b = -intersection_a;
let solve = |intersection: f64| {
let lng = angle_to_rotate_between_vectors_2d(
vec_to_target.x,
vec_to_target.z,
rotated_pixel_vector.x,
intersection,
);
let vec_to_target_lng = rotate_y(vec_to_target, -lng);
let lat = angle_to_rotate_between_vectors_2d(
vec_to_target_lng.y,
vec_to_target_lng.z,
rotated_pixel_vector.y,
rotated_pixel_vector.z,
);
(lng, lat)
};
let (lng_a, lat_a) = solve(intersection_a);
let (lng_b, lat_b) = solve(intersection_b);
let valid_a = (-LIMIT..=LIMIT).contains(&lat_a);
let valid_b = (-LIMIT..=LIMIT).contains(&lat_b);
let (lng, lat) = if valid_a && valid_b {
let center_lng = mln_style::math::deg2rad(center.longitude());
let center_lat = mln_style::math::deg2rad(center.latitude());
let distance_a =
distance_of_angles_radians(lng_a, center_lng) + distance_of_angles_radians(lat_a, center_lat);
let distance_b =
distance_of_angles_radians(lng_b, center_lng) + distance_of_angles_radians(lat_b, center_lat);
if distance_a < distance_b { (lng_a, lat_a) } else { (lng_b, lat_b) }
} else if valid_a {
(lng_a, lat_a)
} else if valid_b {
(lng_b, lat_b)
} else {
return None;
};
Some(LatLng::saturating(
(mln_style::math::rad2deg(lat)).clamp(-90.0, 90.0),
mln_style::math::rad2deg(lng),
WrapMode::Unwrapped,
))
}
pub(crate) fn zoom_around_point(
state: &mut TransformState,
anchor: &ScreenCoordinate,
zoom_location: &LatLng,
height: f64,
zoom_delta: f64,
free_bearing: bool,
) {
if zoom_delta == 0.0 {
return;
}
let center = state.view().center(WrapMode::Unwrapped);
let on_pole = mln_style::math::deg2rad(zoom_location.latitude()).cos() <= POLE_AXIS_DISTANCE;
let d_lng_raw = if on_pole {
0.0
} else {
Self::difference_of_angles_degrees(center.longitude(), zoom_location.longitude())
};
let d_lng = d_lng_raw / ((d_lng_raw / 180.0).abs() + 1.0);
let d_lat = Self::difference_of_angles_degrees(center.latitude(), zoom_location.latitude());
let ray_origin = camera_over_sphere(state.view(), height);
let ray_direction =
ray_direction_from_pixel(state.view(), anchor, &state.view().inverse_globe_view_projection_matrix());
let closest_point = ray_origin - ray_direction * ray_origin.dot(ray_direction);
let ray_distance_from_globe_center = closest_point.length();
let distance_factor =
(-f64::max(ray_distance_from_globe_center - 1.0 - RAY_SURFACE_DISTANCE_FOR_SLOWING_START, 0.0)
* SLOWING_MULTIPLIER)
.exp();
let interpolation_factor_horizon = remap_saturate(
ray_distance_from_globe_center,
INTERPOLATE_TO_HEURISTIC_START_HORIZON,
INTERPOLATE_TO_HEURISTIC_END_HORIZON,
0.0,
1.0,
);
let size = state.view().size();
let radius = Self::globe_radius_pixels(Projection::world_size(state.view().scale()), center.latitude())
/ f64::from(size.width.min(size.height));
let radius_factor =
remap_saturate(radius, SLOWING_RADIUS_START, SLOWING_RADIUS_STOP, 1.0, SLOWING_RADIUS_SLOW_FACTOR);
let slowing_factor = f64::min(distance_factor, 1.0 + (radius_factor - 1.0) * interpolation_factor_horizon);
let factor = (1.0 - 2.0f64.powf(-zoom_delta)) * slowing_factor;
if free_bearing {
zoom_turning_the_planet(state, anchor, zoom_location, height, factor, interpolation_factor_horizon);
return;
}
let heuristic_center = LatLng::saturating(
(center.latitude() + d_lat * factor).clamp(-90.0, 90.0),
center.longitude() + d_lng * factor,
WrapMode::Unwrapped,
);
let mut exact_center =
Self::center_for_location_at_point(state.view(), zoom_location, anchor, height).unwrap_or(center);
if on_pole {
exact_center = LatLng::saturating(exact_center.latitude(), center.longitude(), WrapMode::Unwrapped);
}
let interpolation_factor_longitude = remap_saturate(
d_lng_raw.abs(),
INTERPOLATE_TO_HEURISTIC_START_LNG,
INTERPOLATE_TO_HEURISTIC_END_LNG,
0.0,
1.0,
);
let heuristic_factor = f64::max(interpolation_factor_longitude, interpolation_factor_horizon)
.powf(INTERPOLATE_TO_HEURISTIC_EXPONENT);
let target = LatLng::saturating(
(exact_center.latitude()
+ Self::difference_of_angles_degrees(exact_center.latitude(), heuristic_center.latitude())
* heuristic_factor)
.clamp(-90.0, 90.0),
exact_center.longitude()
+ Self::difference_of_angles_degrees(exact_center.longitude(), heuristic_center.longitude())
* heuristic_factor,
WrapMode::Unwrapped,
);
let constrained = state.constrained_center(&target, state.view().zoom());
let zoom = state.view().zoom() + Self::zoom_adjustment(center.latitude(), constrained.latitude());
state.set_lat_lng_zoom(&constrained, zoom);
}
#[must_use]
pub(crate) fn zoom_adjustment(from_latitude: f64, to_latitude: f64) -> f64 {
(Self::planet_scale(to_latitude) / Self::planet_scale(from_latitude)).log2()
}
pub(crate) fn max_latitude(view: &View, zoom: f64) -> f64 {
let area_radians = mln_style::math::deg2rad(90.0 - LATITUDE_MAX);
let area_pixels =
Self::globe_radius_pixels(Projection::world_size(2.0f64.powf(zoom)), LATITUDE_MAX) * area_radians;
let size = view.size();
let reach_pixels = f64::from(size.width.min(size.height)) / 2.0;
let center_pixels = f64::min(area_pixels, reach_pixels)
* remap_saturate(area_pixels, reach_pixels, reach_pixels * POLE_AREA_FADE_SCALE, 1.0, 0.0);
LATITUDE_MAX + mln_style::math::rad2deg(area_radians * center_pixels / area_pixels)
}
#[must_use]
pub(crate) fn pan_center(view: &View, offset: &ScreenCoordinate) -> LatLng {
let bearing = -view.angle();
let x = offset.x * bearing.cos() - offset.y * bearing.sin();
let y = offset.x * bearing.sin() + offset.y * bearing.cos();
let center = view.center(WrapMode::Unwrapped);
let latitude = center.latitude();
let inverse_scale = |lat: f64| 1.0 / Self::planet_scale(lat);
let zoomed_out = ((7.0 - (view.zoom() + Self::zoom_adjustment(latitude, 0.0))) / 4.0).clamp(0.0, 1.0);
let longitude_speed =
interpolate(&inverse_scale(latitude), &inverse_scale(f64::min(latitude.abs(), 60.0)), zoomed_out);
let degrees_per_pixel =
360.0 / (2.0 * PI * Self::globe_radius_pixels(Projection::world_size(view.scale()), latitude));
LatLng::saturating(
(latitude + y * degrees_per_pixel).clamp(-90.0, 90.0),
center.longitude() - x * degrees_per_pixel * longitude_speed,
WrapMode::Unwrapped,
)
}
#[must_use]
pub(crate) fn drag_camera(
view: &View,
point: &ScreenCoordinate,
pan_delta: &ScreenCoordinate,
height: f64,
) -> CenterAndBearing {
center_and_bearing(
turned(
camera_orientation(view),
pan_surface_vector(view, &Point::new(point.x - pan_delta.x, point.y - pan_delta.y), height),
pan_surface_vector(view, point, height),
),
&view.center(WrapMode::Unwrapped),
)
}
#[must_use]
pub(crate) fn fling_camera(view: &View, center: &ScreenCoordinate, offset: &ScreenCoordinate) -> CenterAndBearing {
let lat_lng = view.center(WrapMode::Unwrapped);
let pixels = offset.x.hypot(offset.y);
if pixels == 0.0 {
return CenterAndBearing { center: lat_lng, bearing: -mln_style::math::rad2deg(view.angle()) };
}
let axis =
pan_surface_vector(view, &Point::new(center.x - offset.x / pixels, center.y - offset.y / pixels), 0.0)
.cross(pan_surface_vector(view, center, 0.0));
let degrees_per_pixel =
360.0 / (2.0 * PI * Self::globe_radius_pixels(Projection::world_size(view.scale()), lat_lng.latitude()));
center_and_bearing(
camera_orientation(view)
* rotation(axis, mln_style::math::deg2rad(f64::min(pixels * degrees_per_pixel, 179.5))),
&lat_lng,
)
}
#[must_use]
pub(crate) fn interpolate_camera(from: &CenterAndBearing, to: &CenterAndBearing, t: f64) -> CenterAndBearing {
center_and_bearing(
orientation_from_lat_lng_bearing(&from.center, from.bearing)
.slerp(orientation_from_lat_lng_bearing(&to.center, to.bearing), t),
&from.center,
)
}
#[must_use]
pub(crate) fn drag_center(
view: &View,
point: &ScreenCoordinate,
pan_delta: &ScreenCoordinate,
height: f64,
) -> LatLng {
let center = view.center(WrapMode::Unwrapped);
let bearing = -mln_style::math::rad2deg(view.angle());
let swung = orientation_to_lat_lng_bearing(turned(
camera_orientation(view),
pan_surface_vector(view, &Point::new(point.x - pan_delta.x, point.y - pan_delta.y), height),
pan_surface_vector(view, point, height),
));
let latitude = if Self::difference_of_angles_degrees(bearing, swung.bearing).abs() <= 90.0 {
swung.latitude
} else if swung.latitude > 0.0 {
90.0
} else {
-90.0
};
let pole_latitude = if center.latitude() >= 0.0 { 90.0 } else { -90.0 };
let (pole, _) = Self::lat_lng_to_screen_coordinate(
view,
&LatLng::saturating(pole_latitude, 0.0, WrapMode::Unwrapped),
height,
);
let rx = point.x - pole.x;
let ry = pole.y - point.y;
let ramp = (1.0 - (LATITUDE_MAX - center.latitude().abs()) / DIAL_RAMP_DEGREES).clamp(0.0, 1.0);
let dial = ramp * ramp * (3.0 - 2.0 * ramp);
let d_lng_swing = mln_style::math::wrap(swung.longitude - center.longitude() + 180.0, 0.0, 360.0) - 180.0;
let mut d_lng_dial = 0.0;
if dial > 0.0 {
let radius_squared = f64::max(rx * rx + ry * ry, DIAL_MIN_RADIUS_PIXELS * DIAL_MIN_RADIUS_PIXELS);
let d_theta = (rx * -pan_delta.y - ry * pan_delta.x) / radius_squared;
d_lng_dial = (if pole_latitude > 0.0 { 1.0 } else { -1.0 }) * mln_style::math::rad2deg(d_theta);
}
LatLng::saturating(
latitude,
center.longitude() + (1.0 - dial) * d_lng_swing + dial * d_lng_dial,
WrapMode::Unwrapped,
)
}
#[must_use]
pub(crate) fn drag_center_holding(
view: &View,
location: &LatLng,
point: &ScreenCoordinate,
height: f64,
) -> Option<LatLng> {
let center = view.center(WrapMode::Unwrapped);
if center.latitude().abs() > LATITUDE_MAX - DIAL_RAMP_DEGREES
|| !Self::screen_coordinate_hits_globe(view, point, height)
|| pan_falloff(view, point, height).is_some()
{
return None;
}
let held = Self::center_for_location_at_point(view, location, point, height)?;
let gain = mln_style::math::deg2rad(location.latitude()).cos()
* mln_style::math::deg2rad(location.longitude() - held.longitude()).cos();
if gain.abs() * DRAG_HOLD_MAX_GAIN < 1.0 {
return None;
}
Some(LatLng::saturating(
held.latitude(),
center.longitude() + Self::difference_of_angles_degrees(center.longitude(), held.longitude()),
WrapMode::Unwrapped,
))
}
#[must_use]
pub(crate) fn difference_of_angles_degrees(from: f64, to: f64) -> f64 {
let a = mln_style::math::wrap(from, 0.0, 360.0);
let b = mln_style::math::wrap(to, 0.0, 360.0);
let direct = b - a;
let around = if b > a { direct - 360.0 } else { direct + 360.0 };
if direct.abs() < around.abs() { direct } else { around }
}
#[must_use]
pub(crate) fn surface_distance_pixels(world_size: f64, center_latitude: f64, a: &LatLng, b: &LatLng) -> f64 {
let radians = Self::surface_vector(a).dot(Self::surface_vector(b)).clamp(-1.0, 1.0).acos();
radians * Self::globe_radius_pixels(world_size, center_latitude)
}
#[must_use]
pub(crate) fn interpolate_lat_lng(start: &LatLng, delta_latitude: f64, delta_longitude: f64, t: f64) -> LatLng {
let latitude = (start.latitude() + delta_latitude * t).clamp(-90.0, 90.0);
let start_latitude = start.latitude().clamp(-LATITUDE_MAX, LATITUDE_MAX);
let end_latitude = (start.latitude() + delta_latitude).clamp(-LATITUDE_MAX, LATITUDE_MAX);
if (end_latitude - start_latitude).abs() <= 1.0 {
return LatLng::saturating(latitude, start.longitude() + delta_longitude * t, WrapMode::Unwrapped);
}
let on_different_hemispheres = sign(end_latitude) != sign(start_latitude);
let sample_start = mln_style::math::deg2rad(if on_different_hemispheres {
-start_latitude.abs()
} else {
start_latitude.abs()
});
let sample_end = mln_style::math::deg2rad(end_latitude.abs());
let value_start = integrate_secant(sample_start);
let value_end = integrate_secant(sample_end);
let value_t = integrate_secant(sample_start + t * (sample_end - sample_start));
let longitude_t = (value_t - value_start) / (value_end - value_start);
LatLng::saturating(latitude, start.longitude() + delta_longitude * longitude_t, WrapMode::Unwrapped)
}
}
const PAN_FALLOFF_BAND: f64 = 0.1;
const DIAL_MIN_RADIUS_PIXELS: f64 = 20.0;
const DIAL_RAMP_DEGREES: f64 = 12.0;
const PAN_MAX_ANGLE: f64 = PI * 0.98;
const DRAG_HOLD_MAX_GAIN: f64 = 2.5;
fn orientation_from_lat_lng_bearing(center: &LatLng, bearing_degrees: f64) -> DQuat {
DQuat::from_euler(
EulerRot::ZYX,
mln_style::math::deg2rad(bearing_degrees),
mln_style::math::deg2rad(-center.latitude()),
mln_style::math::deg2rad(-center.longitude()),
)
}
struct Orientation {
latitude: f64,
longitude: f64,
bearing: f64,
}
fn orientation_to_lat_lng_bearing(q: DQuat) -> Orientation {
Orientation {
latitude: -mln_style::math::rad2deg((2.0 * (q.w * q.y - q.z * q.x)).clamp(-1.0, 1.0).asin()),
longitude: -mln_style::math::rad2deg(
(2.0 * (q.w * q.x + q.y * q.z)).atan2(1.0 - 2.0 * (q.x * q.x + q.y * q.y)),
),
bearing: mln_style::math::rad2deg((2.0 * (q.w * q.z + q.x * q.y)).atan2(1.0 - 2.0 * (q.y * q.y + q.z * q.z))),
}
}
struct PanFalloff {
toward_camera: DVec3,
distance: f64,
lateral: DVec3,
lateral_length: f64,
angle: f64,
handover_angle: f64,
}
fn pan_falloff(view: &View, point: &ScreenCoordinate, height: f64) -> Option<PanFalloff> {
let origin = camera_over_sphere(view, height);
let distance = origin.length();
if distance <= 1.0 {
return None;
}
let toward_camera = origin / distance;
let direction = ray_direction_from_pixel(view, point, &view.inverse_globe_view_projection_matrix());
let c = -direction.dot(toward_camera);
let lateral = direction + toward_camera * c;
let lateral_length = lateral.length();
if lateral_length < 1e-9 {
return None;
}
let angle = lateral_length.atan2(c);
let handover_angle = (1.0 / distance).asin() * (1.0 - PAN_FALLOFF_BAND);
(angle >= handover_angle).then_some(PanFalloff {
toward_camera,
distance,
lateral,
lateral_length,
angle,
handover_angle,
})
}
fn pan_surface_vector(view: &View, point: &ScreenCoordinate, height: f64) -> DVec3 {
let Some(PanFalloff { toward_camera: u, distance, lateral, lateral_length: s, angle, handover_angle }) =
pan_falloff(view, point, height)
else {
return VerticalPerspectiveProjection::screen_coordinate_to_surface(view, point, height);
};
let sin_handover = distance * handover_angle.sin();
let target_at_handover = sin_handover.clamp(-1.0, 1.0).asin() - handover_angle;
let slope_at_handover =
distance * handover_angle.cos() / f64::max(1.0 - sin_handover * sin_handover, 1e-12).sqrt() - 1.0;
let room = PAN_MAX_ANGLE - target_at_handover;
let excess = angle - handover_angle;
let target = (target_at_handover + room * (slope_at_handover * excess) / (room + slope_at_handover * excess))
.clamp(0.0, PAN_MAX_ANGLE);
(u * target.cos() + lateral * (target.sin() / s)).normalize()
}
fn camera_over_sphere(view: &View, height: f64) -> DVec3 {
view.globe_camera_position() / (1.0 + height / VerticalPerspectiveProjection::GLOBE_RADIUS_METERS)
}
fn camera_orientation(view: &View) -> DQuat {
orientation_from_lat_lng_bearing(&view.center(WrapMode::Unwrapped), -mln_style::math::rad2deg(view.angle()))
}
fn rotation(axis: DVec3, angle: f64) -> DQuat {
let l = axis.length();
if l == 0.0 {
return DQuat::IDENTITY;
}
let s = (angle / 2.0).sin();
DQuat::from_xyzw(axis.y / l * s, -axis.x / l * s, axis.z / l * s, (angle / 2.0).cos())
}
fn turned(orientation: DQuat, from: DVec3, to: DVec3) -> DQuat {
orientation * rotation(from.cross(to), from.dot(to).clamp(-1.0, 1.0).acos())
}
fn slerp_vec3(a: DVec3, b: DVec3, t: f64) -> DVec3 {
let angle = a.dot(b).clamp(-1.0, 1.0).acos();
if angle.sin() < 1e-9 {
return a;
}
a * (((1.0 - t) * angle).sin() / angle.sin()) + b * ((t * angle).sin() / angle.sin())
}
fn center_and_bearing(orientation: DQuat, center: &LatLng) -> CenterAndBearing {
let o = orientation_to_lat_lng_bearing(orientation);
CenterAndBearing {
center: LatLng::saturating(
o.latitude,
center.longitude()
+ VerticalPerspectiveProjection::difference_of_angles_degrees(center.longitude(), o.longitude),
WrapMode::Unwrapped,
),
bearing: o.bearing,
}
}
fn zoom_turning_the_planet(
state: &mut TransformState,
anchor: &ScreenCoordinate,
zoom_location: &LatLng,
height: f64,
factor: f64,
interpolation_factor_horizon: f64,
) {
let center = state.view().center(WrapMode::Unwrapped);
let orientation = camera_orientation(state.view());
let location = VerticalPerspectiveProjection::surface_vector(zoom_location);
let center_vector = VerticalPerspectiveProjection::surface_vector(¢er);
let exact = turned(orientation, location, pan_surface_vector(state.view(), anchor, height));
let heuristic = turned(orientation, slerp_vec3(center_vector, location, factor), center_vector);
let camera = center_and_bearing(
exact.slerp(heuristic, interpolation_factor_horizon.powf(INTERPOLATE_TO_HEURISTIC_EXPONENT)),
¢er,
);
let constrained = state.constrained_center(&camera.center, state.view().zoom());
let zoom =
state.view().zoom() + VerticalPerspectiveProjection::zoom_adjustment(center.latitude(), constrained.latitude());
state.set_lat_lng_zoom(&constrained, zoom);
state.set_angle(-mln_style::math::deg2rad(camera.bearing));
}
impl VerticalPerspectiveProjection {
pub(crate) fn projection_data(view: &View, tile_id: &UnwrappedTileID, mercator_matrix: &DMat4) -> ProjectionData {
let middle = Point::new(f64::from(EXTENT) / 2.0, f64::from(EXTENT) / 2.0);
ProjectionData {
sphere_anchor: Self::tile_coordinates_to_sphere(&middle, tile_id),
sphere_anchor_angles: Self::tile_coordinates_to_angles(&middle, tile_id),
main_matrix: view.globe_view_projection_matrix(),
tile_mercator_coords: mercator_tile_coords(tile_id),
clipping_plane: view.globe_clipping_plane(),
projection_transition: view.projection_transition(),
fallback_matrix: *mercator_matrix,
camera_position: view.globe_camera_position(),
..ProjectionData::default()
}
}
pub(crate) fn project_tile_point(
data: &ProjectionData,
tile_id: &UnwrappedTileID,
point: &Point<f64>,
elevation: f64,
) -> ProjectedTilePoint {
let surface = Self::tile_coordinates_to_sphere(point, tile_id);
let height_scale = 1.0 + elevation / Self::GLOBE_RADIUS_METERS;
let sphere = surface * height_scale;
let pos = data.main_matrix * sphere.extend(1.0);
let occluded = if height_scale <= 1.0 {
point_plane_signed_distance(data.clipping_plane, surface) < 0.0
} else {
line_of_sight_blocked(data.camera_position, sphere)
};
ProjectedTilePoint {
point: Point::new(pos.x / pos.w, pos.y / pos.w),
signed_distance_from_camera: pos.w,
occluded,
}
}
pub(crate) fn drawn_tile_point(
data: &ProjectionData,
tile_id: &UnwrappedTileID,
point: &Point<f64>,
elevation: f64,
) -> ProjectedTilePoint {
let globe = Self::project_tile_point(data, tile_id, point, elevation);
let t = data.projection_transition;
if t > 0.999 {
return globe;
}
let flat = data.fallback_matrix * DVec4::new(point.x, point.y, elevation, 1.0);
let globe_w = globe.signed_distance_from_camera;
let w = interpolate(&flat.w, &globe_w, t);
let globeness = ((t - 0.2) / 0.8).clamp(0.0, 1.0);
let mut occluded = globeness > 0.0 && globe.occluded;
if elevation <= 0.0 {
let surface = Self::tile_coordinates_to_sphere(point, tile_id);
let clipping_z = 1.0 - point_plane_signed_distance(data.clipping_plane, surface);
occluded = globeness * clipping_z * globe_w > w;
}
ProjectedTilePoint {
point: Point::new(
interpolate(&flat.x, &(globe.point.x * globe_w), t) / w,
interpolate(&flat.y, &(globe.point.y * globe_w), t) / w,
),
signed_distance_from_camera: w,
occluded,
}
}
pub(crate) fn circle_radius_correction(view: &View) -> f64 {
interpolate(
&1.0,
&Self::planet_scale(view.center(WrapMode::Unwrapped).latitude()),
view.projection_transition(),
)
}
pub(crate) fn pixel_scale(view: &View) -> f64 {
interpolate(
&1.0,
&(1.0 / Self::planet_scale(view.center(WrapMode::Unwrapped).latitude())),
view.projection_transition(),
)
}
pub(crate) fn pitched_text_correction(view: &View, tile_anchor: &Point<f64>, tile_id: &UnwrappedTileID) -> f64 {
let scale = 1.0 / (1u64 << tile_id.canonical.z) as f64;
let mercator_y = tile_anchor.y / f64::from(EXTENT) * scale + f64::from(tile_id.canonical.y) * scale;
let latitude = 2.0 * (PI - (mercator_y * PI * 2.0)).exp().atan() - PI * 0.5;
interpolate(
&1.0,
&(Self::planet_scale(view.center(WrapMode::Unwrapped).latitude()) / latitude.cos()),
view.projection_transition(),
)
}
}