use std::f64::consts::PI;
use geometry::Point;
use glam::dcamera::rh::proj::opengl::perspective;
use glam::{DMat4, DVec2, DVec3, DVec4};
use mln_style::constants::TILE_SIZE_D;
use mln_style::error::Error;
use mln_style::geo::{LatLng, WrapMode};
use mln_style::size::Size;
use mln_style::types::ProjectionDefinition;
use crate::camera::camera::FreeCameraOptions;
use crate::camera::camera_pose::CameraPose;
use crate::camera::constants::DEFAULT_FOV;
use crate::camera::map_view::{Camera, LogicalExtent, MapView, Viewport};
use crate::camera::mode::{ConstrainMode, ViewportMode};
use crate::camera::projection::{ProjectionData, mercator_tile_matrix};
use crate::camera::projection_mode::{AxonometricMode, ProjectionMode};
use crate::camera::tile_coordinate::TileCoordinate;
use crate::camera::vertical_perspective_projection::VerticalPerspectiveProjection;
pub use crate::geo::NorthOrientation;
use crate::geo::constants::M2PI;
use crate::geo::projection::Projection;
use crate::geo::{EdgeInsets, ScreenCoordinate};
use crate::tile::UnwrappedTileID;
pub(crate) fn max_mercator_horizon_angle() -> f64 {
mln_style::math::deg2rad(89.25)
}
fn round_for_accuracy(x: f64) -> f64 {
let round_x = x.round();
let diff = (round_x - x).abs();
if diff < 1e-9 && diff > 0.0 { round_x } else { x }
}
#[must_use]
pub fn zoom_scale(zoom: f64) -> f64 {
round_for_accuracy(2.0f64.powf(zoom))
}
#[must_use]
pub fn scale_zoom(scale: f64) -> f64 {
round_for_accuracy(scale.log2())
}
pub(crate) fn map_offset(center: &LatLng, scale: f64, globe: bool) -> (f64, f64) {
let world_size = Projection::world_size(scale);
let latitude = mln_style::math::deg2rad(center.latitude());
let m = 1.0 - 1e-15;
let f = mln_style::math::clamp_f64(latitude.sin(), -m, m);
let x = -center.longitude() * (world_size / 360.0);
let cc = world_size / M2PI;
let y = if globe { cc * latitude.tan().asinh() } else { 0.5 * cc * ((1.0 + f) / (1.0 - f)).ln() };
(x, y)
}
fn modf_fraction(x: f64) -> f64 {
if x.is_infinite() {
return 0.0f64.copysign(x);
}
(x - x.trunc()).copysign(x)
}
#[derive(Clone, Copy, Debug, PartialEq)]
pub(crate) struct ViewParams {
pub(crate) size: Size,
pub(crate) frustum_offset: EdgeInsets,
pub(crate) north_orientation: NorthOrientation,
pub(crate) viewport_mode: ViewportMode,
pub(crate) constrain_mode: ConstrainMode,
pub(crate) center_clamped_to_ground: bool,
pub(crate) globe_free_bearing: bool,
pub(crate) projection_definition: ProjectionDefinition,
pub(crate) projection_transition: f64,
pub(crate) padding: EdgeInsets,
pub(crate) x: f64,
pub(crate) y: f64,
pub(crate) center_altitude: f64,
pub(crate) scale: f64,
pub(crate) angle: f64,
pub(crate) pitch: f64,
pub(crate) roll: f64,
pub(crate) fov: f64,
pub(crate) x_skew: f64,
pub(crate) y_skew: f64,
pub(crate) axonometric: bool,
pub(crate) center_wrap: WrapMode,
pub(crate) terrain_min_elevation: Option<f64>,
}
impl ViewParams {
pub(crate) fn new(constrain_mode: ConstrainMode, viewport_mode: ViewportMode) -> Self {
ViewParams {
size: Size::default(),
frustum_offset: EdgeInsets::default(),
north_orientation: NorthOrientation::Upwards,
viewport_mode,
constrain_mode,
center_clamped_to_ground: true,
globe_free_bearing: false,
projection_definition: ProjectionDefinition::default(),
projection_transition: 0.0,
padding: EdgeInsets::default(),
x: 0.0,
y: 0.0,
center_altitude: 0.0,
scale: 1.0,
angle: 0.0,
pitch: 0.0,
roll: 0.0,
fov: DEFAULT_FOV,
x_skew: 0.0,
y_skew: 1.0,
axonometric: false,
center_wrap: WrapMode::Unwrapped,
terrain_min_elevation: None,
}
}
}
#[derive(Clone, Copy, Debug)]
struct Matrices {
projection: DMat4,
inv_projection: DMat4,
coord: DMat4,
inverted_from_center: DMat4,
globe_view_projection: DMat4,
inv_globe_view_projection: DMat4,
globe_clipping_plane: DVec4,
globe_camera_position: DVec3,
}
impl Matrices {
const ZERO: Matrices = Matrices {
projection: DMat4::ZERO,
inv_projection: DMat4::ZERO,
coord: DMat4::ZERO,
inverted_from_center: DMat4::ZERO,
globe_view_projection: DMat4::ZERO,
inv_globe_view_projection: DMat4::ZERO,
globe_clipping_plane: DVec4::ZERO,
globe_camera_position: DVec3::ZERO,
};
}
#[derive(Clone, Debug)]
pub struct View {
params: ViewParams,
matrices: Matrices,
}
impl PartialEq for View {
fn eq(&self, other: &Self) -> bool {
self.params == other.params
}
}
impl Default for View {
fn default() -> Self {
View::new(ViewParams::new(ConstrainMode::HeightOnly, ViewportMode::Default))
}
}
impl View {
pub(crate) fn new(params: ViewParams) -> Self {
let mut view = View { params, matrices: Matrices::ZERO };
view.matrices = view.compute_matrices();
view
}
pub(crate) fn params(&self) -> &ViewParams {
&self.params
}
fn compute_matrices(&self) -> Matrices {
if self.params.size.is_empty() {
return Matrices::ZERO;
}
let projection = self.projection_matrix_with(self.near_z(), false);
let coord = self.coordinate_point_matrix(&projection);
let from_center = coord * DMat4::from_translation(self.center_coordinate());
let (Some(inv_projection), Some(inverted_from_center)) = (projection.try_inverse(), from_center.try_inverse())
else {
return Matrices::ZERO;
};
let mut matrices = Matrices { projection, inv_projection, coord, inverted_from_center, ..Matrices::ZERO };
if self.is_globe() {
let globe_radius = VerticalPerspectiveProjection::globe_radius_pixels(
Projection::world_size(self.params.scale),
self.center(WrapMode::Unwrapped).latitude(),
);
let globe_view_projection = VerticalPerspectiveProjection::globe_view_projection_matrix(self, globe_radius);
matrices.globe_view_projection = globe_view_projection;
matrices.inv_globe_view_projection = globe_view_projection.try_inverse().unwrap_or(DMat4::IDENTITY);
matrices.globe_clipping_plane = VerticalPerspectiveProjection::clipping_plane(self, globe_radius);
matrices.globe_camera_position = VerticalPerspectiveProjection::camera_position(self, globe_radius);
}
matrices
}
#[must_use]
pub fn size(&self) -> Size {
self.params.size
}
#[must_use]
pub fn frustum_offset(&self) -> EdgeInsets {
self.params.frustum_offset
}
#[must_use]
pub fn padding(&self) -> EdgeInsets {
self.params.padding
}
#[must_use]
pub fn north_orientation(&self) -> NorthOrientation {
self.params.north_orientation
}
#[must_use]
pub fn north_orientation_angle(&self) -> f64 {
match self.params.north_orientation {
NorthOrientation::Upwards => 0.0,
NorthOrientation::Rightwards => PI / 2.0,
NorthOrientation::Downwards => PI,
NorthOrientation::Leftwards => -PI / 2.0,
}
}
#[must_use]
pub fn viewport_mode(&self) -> ViewportMode {
self.params.viewport_mode
}
#[must_use]
pub fn center_clamped_to_ground(&self) -> bool {
self.params.center_clamped_to_ground
}
#[must_use]
pub fn globe_free_bearing(&self) -> bool {
self.params.globe_free_bearing
}
pub(crate) fn center_offset(&self) -> ScreenCoordinate {
let padding = &self.params.padding;
ScreenCoordinate::new(0.5 * (padding.left - padding.right), 0.5 * (padding.top - padding.bottom))
}
#[must_use]
pub fn center(&self, wrap_mode: WrapMode) -> LatLng {
let world_size = Projection::world_size(self.params.scale);
LatLng::saturating(
mln_style::math::rad2deg(2.0 * (self.params.y / (world_size / M2PI)).exp().atan() - 0.5 * PI),
-self.params.x / (world_size / 360.0),
wrap_mode,
)
}
#[must_use]
pub fn lat_lng_near_center(&self, location: &LatLng) -> LatLng {
let center = self.center(WrapMode::Unwrapped);
let wrapped_center = center.wrapped();
let mut nearest = location.wrapped();
nearest.unwrap_for_shortest_path(&wrapped_center);
LatLng::saturating(
nearest.latitude(),
nearest.longitude() + center.longitude() - wrapped_center.longitude(),
WrapMode::Unwrapped,
)
}
#[must_use]
pub fn center_altitude(&self) -> f64 {
self.params.center_altitude
}
pub(crate) fn meters_per_pixel(&self) -> f64 {
Projection::meters_per_pixel_at_latitude(self.center(WrapMode::Unwrapped).latitude(), self.zoom())
}
fn center_altitude_pixels(&self) -> f64 {
if self.params.center_altitude == 0.0 {
return self.params.center_altitude;
}
self.params.center_altitude / self.meters_per_pixel()
}
#[must_use]
pub fn zoom(&self) -> f64 {
scale_zoom(self.params.scale)
}
#[must_use]
pub fn integer_zoom(&self) -> u8 {
f64::max(0.0, self.zoom()) as u8
}
#[must_use]
pub fn zoom_fraction(&self) -> f64 {
self.zoom() - f64::from(self.integer_zoom())
}
#[must_use]
pub fn scale(&self) -> f64 {
self.params.scale
}
#[must_use]
pub fn x(&self) -> f64 {
self.params.x
}
#[must_use]
pub fn y(&self) -> f64 {
self.params.y
}
#[must_use]
pub fn angle(&self) -> f64 {
self.params.angle
}
#[must_use]
pub fn pitch(&self) -> f64 {
self.params.pitch
}
#[must_use]
pub fn roll(&self) -> f64 {
self.params.roll
}
#[must_use]
pub fn fov(&self) -> f64 {
self.params.fov
}
pub(crate) fn perspective_fov(&self) -> f64 {
f64::from(self.params.fov as f32)
}
#[must_use]
pub fn camera_to_center_distance(&self) -> f64 {
0.5 * f64::from(self.params.size.height) / (self.params.fov / 2.0).tan()
}
#[must_use]
pub fn mercator_horizon(&self) -> f64 {
let pitch = self.params.pitch;
self.camera_to_center_distance()
* f64::min((PI / 2.0 - pitch).tan() * 0.85, (max_mercator_horizon_angle() - pitch).tan())
}
#[must_use]
pub fn projection_mode(&self) -> ProjectionMode {
ProjectionMode {
axonometric: Some(AxonometricMode::from(self.params.axonometric)),
x_skew: Some(self.params.x_skew),
y_skew: Some(self.params.y_skew),
}
}
#[must_use]
pub fn camera(&self) -> Camera {
Camera {
center: self.center(self.params.center_wrap),
center_altitude: self.center_altitude(),
padding: self.params.padding,
zoom: self.zoom(),
bearing: mln_style::math::rad2deg(-self.params.angle),
pitch: mln_style::math::rad2deg(self.params.pitch),
roll: mln_style::math::rad2deg(self.params.roll),
fov: mln_style::math::rad2deg(self.params.fov),
}
}
#[must_use]
pub fn map_view(&self, scale_factor: f64) -> MapView {
MapView {
camera: self.camera(),
extent: LogicalExtent { size: self.params.size, scale_factor },
projection_mode: self.projection_mode(),
viewport: Viewport {
north_orientation: self.params.north_orientation,
constrain_mode: self.params.constrain_mode,
viewport_mode: self.params.viewport_mode,
center_clamped_to_ground: self.params.center_clamped_to_ground,
globe_free_bearing: self.params.globe_free_bearing,
frustum_offset: self.params.frustum_offset,
},
projection: self.params.projection_definition,
}
}
#[must_use]
pub fn is_valid(&self) -> bool {
!self.params.size.is_empty() && self.params.scale.is_finite() && self.params.scale > 0.0
}
pub(crate) fn camera_pose(&self) -> CameraPose {
if !self.is_valid() {
return CameraPose::default();
}
let world_size = Projection::world_size(self.params.scale);
let center = DVec3::new(
0.5 * world_size - self.params.x,
0.5 * world_size - self.params.y,
self.center_altitude_pixels(),
);
let mut camera = CameraPose::from_angles(DVec3::ZERO, self.params.roll, self.params.pitch, self.params.angle);
camera.position = (center - camera.forward() * self.camera_to_center_distance()) / world_size;
camera
}
#[must_use]
pub fn free_camera_options(&self) -> FreeCameraOptions {
let camera = self.camera_pose();
FreeCameraOptions { position: Some(camera.position), orientation: Some(camera.orientation) }
}
#[must_use]
pub fn eye_position(&self) -> DVec3 {
if self.is_globe() && self.is_valid() {
let eye = self.matrices.globe_camera_position;
return VerticalPerspectiveProjection::sphere_point_to_world(self, eye).unwrap_or(eye);
}
let position = self.camera_pose().position;
let world_size = Projection::world_size(self.params.scale);
DVec3::new(position.x, position.y, position.z * world_size * self.meters_per_pixel())
}
#[must_use]
pub fn near_plane_points(&self) -> [DVec3; 9] {
let eye = self.eye_position();
if self.matrices.projection == DMat4::ZERO {
return [eye; 9];
}
let clip = |index: usize| DVec3::new((index % 3) as f64 - 1.0, (index / 3) as f64 - 1.0, -1.0);
if self.is_globe() {
let inverse = self.matrices.inv_globe_view_projection;
return std::array::from_fn(|index| {
let point = inverse.project_point3(clip(index));
VerticalPerspectiveProjection::sphere_point_to_world(self, point).unwrap_or(eye)
});
}
let world_size = Projection::world_size(self.params.scale);
std::array::from_fn(|index| {
let point = self.matrices.inv_projection.project_point3(clip(index));
DVec3::new(point.x / world_size, point.y / world_size, point.z)
})
}
#[must_use]
pub fn projection_definition(&self) -> ProjectionDefinition {
self.params.projection_definition
}
#[must_use]
pub fn projection_transition(&self) -> f64 {
self.params.projection_transition
}
#[must_use]
pub fn is_globe(&self) -> bool {
self.params.projection_transition > 0.0
}
#[must_use]
pub fn projection(&self) -> crate::camera::Projection {
if self.is_globe() {
crate::camera::Projection::VerticalPerspective
} else {
crate::camera::Projection::Mercator
}
}
#[must_use]
pub fn mercator(&self) -> View {
if !self.is_globe() {
return self.clone();
}
View::new(ViewParams { projection_transition: 0.0, ..self.params })
}
#[must_use]
pub fn tile_matrix(&self, tile_id: &UnwrappedTileID) -> DMat4 {
mercator_tile_matrix(tile_id, self.params.scale)
}
#[must_use]
pub fn projection_data_with(&self, tile_id: &UnwrappedTileID, proj_matrix: &DMat4) -> ProjectionData {
let mercator_matrix = *proj_matrix * self.tile_matrix(tile_id);
self.projection().projection_data(self, tile_id, &mercator_matrix)
}
#[must_use]
pub fn projection_data(&self, tile_id: &UnwrappedTileID) -> ProjectionData {
self.projection_data_with(tile_id, &self.matrices.projection)
}
#[must_use]
pub fn projection_data_for_matrix(&self, tile_id: &UnwrappedTileID, mercator_matrix: &DMat4) -> ProjectionData {
self.projection().projection_data(self, tile_id, mercator_matrix)
}
#[must_use]
pub fn terrain_min_elevation(&self) -> Option<f64> {
self.params.terrain_min_elevation
}
#[must_use]
pub fn near_z(&self) -> f64 {
match self.params.terrain_min_elevation {
Some(_) => f64::from(self.params.size.height) / 50.0,
None => 1.0,
}
}
#[must_use]
pub fn far_z(&self) -> f64 {
match self.params.terrain_min_elevation {
Some(min_elevation) => self.terrain_far_z(min_elevation),
None => self.flat_far_z(),
}
}
fn flat_far_z(&self) -> f64 {
let camera_to_center_distance = self.camera_to_center_distance();
let limited_pitch = mln_style::math::clamp_f64(self.params.pitch, 0.0, max_mercator_horizon_angle());
let camera_to_sea_level_distance =
camera_to_center_distance + self.center_altitude_pixels().abs() / limited_pitch.cos();
let tan_multiple = mln_style::math::clamp_f64(self.tan_fov_above_center() * limited_pitch.tan(), 0.0, 0.99);
let horizon_multiple =
mln_style::math::clamp_f64(self.fov_center_to_horizon().tan() * limited_pitch.tan(), 0.0, 0.99);
let furthest_distance = camera_to_sea_level_distance / (1.0 - tan_multiple.min(horizon_multiple));
furthest_distance * 1.01
}
#[must_use]
pub fn camera_to_sea_level_distance(&self) -> f64 {
let camera_to_center_distance = self.camera_to_center_distance();
let limited_pitch = self.params.pitch.min(max_mercator_horizon_angle());
f64::max(
camera_to_center_distance / 2.0,
camera_to_center_distance + self.center_altitude_pixels() / limited_pitch.cos(),
)
}
fn terrain_far_z(&self, min_elevation: f64) -> f64 {
let params = &self.params;
let camera_to_center_distance = self.camera_to_center_distance();
let pixels_per_meter = 1.0 / self.meters_per_pixel();
let center_altitude = self.center_altitude_pixels();
let limited_pitch = params.pitch.min(max_mercator_horizon_angle());
let camera_to_sea_level_distance = self.camera_to_sea_level_distance();
let camera_altitude = params.pitch.cos() * camera_to_center_distance + center_altitude;
let lowest =
center_altitude.min(min_elevation * pixels_per_meter).min(camera_altitude - 100.0 * pixels_per_meter);
let lowest_plane = if lowest < 0.0 {
camera_to_sea_level_distance - lowest / limited_pitch.cos()
} else {
camera_to_sea_level_distance
};
let ground_angle = PI / 2.0 + params.pitch;
let distance_past_center = |angle: f64| {
angle.sin() * lowest_plane / mln_style::math::clamp_f64(PI - ground_angle - angle, 0.01, PI - 0.01).sin()
};
let top_half_distance = distance_past_center(self.tan_fov_above_center().atan())
.min(distance_past_center(self.fov_center_to_horizon()));
(limited_pitch.sin() * top_half_distance + lowest_plane) * 1.01
}
fn tan_fov_above_center(&self) -> f64 {
let params = &self.params;
let half_width = 0.5 * f64::from(params.size.width);
let half_height = 0.5 * f64::from(params.size.height);
let offset = self.center_offset();
let (left, right) = (half_width + offset.x, half_width - offset.x);
let (top, bottom) = (half_height + offset.y - params.frustum_offset.top, half_height - offset.y);
let up = self.up_the_map();
let reach = (up.x * right).max(-up.x * left) + (up.y * top).max(-up.y * bottom);
reach / half_height * (params.fov / 2.0).tan()
}
fn fov_center_to_horizon(&self) -> f64 {
let camera_to_center = self.camera_to_center_distance();
let horizon = self.mercator_horizon();
let min_horizon_angle = PI / 2.0 - max_mercator_horizon_angle();
if (horizon / camera_to_center).atan() <= min_horizon_angle {
return min_horizon_angle;
}
let offset = self.center_offset();
let offset_up = DVec2::new(-offset.x, offset.y).dot(self.up_the_map());
let farthest = camera_to_center * (max_mercator_horizon_angle() - self.params.pitch).tan();
(mln_style::math::clamp_f64(horizon + offset_up, 0.0, farthest) / camera_to_center).atan()
}
fn up_the_map(&self) -> DVec2 {
let (sin, cos) = self.params.roll.sin_cos();
let flip = if self.params.viewport_mode == ViewportMode::FlippedY { -1.0 } else { 1.0 };
DVec2::from_angle(-self.north_orientation_angle()).rotate(DVec2::new(-sin, flip * cos))
}
#[must_use]
pub fn projection_matrix_with(&self, near_z: f64, aligned: bool) -> DMat4 {
let params = &self.params;
if params.size.is_empty() {
return DMat4::ZERO;
}
let offset = self.center_offset();
let width = f64::from(params.size.width);
let height = f64::from(params.size.height);
let camera = self.camera_pose();
let pixels_per_meter = 1.0 / self.meters_per_pixel();
let world_to_camera =
camera.world_to_camera(params.scale, pixels_per_meter, params.viewport_mode == ViewportMode::FlippedY);
let mut camera_to_clip = perspective(self.perspective_fov(), width / height, near_z, self.far_z());
if !params.axonometric {
camera_to_clip.z_axis.x = -offset.x * 2.0 / width;
camera_to_clip.z_axis.y = offset.y * 2.0 / height;
}
if params.north_orientation != NorthOrientation::Upwards {
camera_to_clip *= DMat4::from_rotation_z(-self.north_orientation_angle());
}
let mut proj_matrix = camera_to_clip * world_to_camera;
if params.axonometric {
proj_matrix.z_axis.w = 0.0;
proj_matrix.z_axis.x = params.x_skew * pixels_per_meter;
proj_matrix.z_axis.y = params.y_skew * pixels_per_meter;
}
if aligned {
let world_size = Projection::world_size(params.scale);
let dx = params.x - 0.5 * world_size;
let dy = params.y - 0.5 * world_size;
let x_shift = f64::from(params.size.width % 2) / 2.0;
let y_shift = f64::from(params.size.height % 2) / 2.0;
let angle_cos = params.angle.cos();
let angle_sin = params.angle.sin();
let dxa = -modf_fraction(dx) + angle_cos * x_shift + angle_sin * y_shift;
let dya = -modf_fraction(dy) + angle_cos * y_shift + angle_sin * x_shift;
proj_matrix *= DMat4::from_translation(DVec3::new(
if dxa > 0.5 { dxa - 1.0 } else { dxa },
if dya > 0.5 { dya - 1.0 } else { dya },
0.0,
));
}
proj_matrix
}
#[must_use]
pub fn projection_matrix(&self) -> DMat4 {
self.matrices.projection
}
#[must_use]
pub fn inverse_projection_matrix(&self) -> DMat4 {
self.matrices.inv_projection
}
#[must_use]
pub fn globe_view_projection_matrix(&self) -> DMat4 {
self.matrices.globe_view_projection
}
#[must_use]
pub fn inverse_globe_view_projection_matrix(&self) -> DMat4 {
self.matrices.inv_globe_view_projection
}
#[must_use]
pub fn globe_clipping_plane(&self) -> DVec4 {
self.matrices.globe_clipping_plane
}
#[must_use]
pub fn globe_camera_position(&self) -> DVec3 {
self.matrices.globe_camera_position
}
fn center_coordinate(&self) -> DVec3 {
let world_size = Projection::world_size(self.params.scale);
DVec3::new(
(0.5 * world_size - self.params.x) / TILE_SIZE_D,
(0.5 * world_size - self.params.y) / TILE_SIZE_D,
0.0,
)
}
fn coordinate_point_matrix(&self, proj_matrix: &DMat4) -> DMat4 {
let size = self.params.size;
let pixel_matrix =
DMat4::from_scale(DVec3::new(f64::from(size.width) / 2.0, -f64::from(size.height) / 2.0, 1.0))
* DMat4::from_translation(DVec3::new(1.0, -1.0, 0.0));
pixel_matrix * *proj_matrix * DMat4::from_scale(DVec3::new(TILE_SIZE_D, TILE_SIZE_D, 1.0))
}
#[must_use]
pub fn lat_lng_to_screen_coordinate(&self, lat_lng: &LatLng) -> ScreenCoordinate {
self.lat_lng_to_screen_coordinate_at(lat_lng, 0.0)
}
#[must_use]
pub fn lat_lng_to_screen_coordinate_at(&self, lat_lng: &LatLng, height: f64) -> ScreenCoordinate {
self.clip_point_at(lat_lng, height).0
}
#[must_use]
pub fn lat_lng_to_screen_coordinate_with_clip(&self, lat_lng: &LatLng) -> (ScreenCoordinate, DVec4) {
self.clip_point_at(lat_lng, 0.0)
}
fn clip_point_at(&self, lat_lng: &LatLng, height: f64) -> (ScreenCoordinate, DVec4) {
if self.params.size.is_empty() {
return (ScreenCoordinate::new(0.0, 0.0), DVec4::ZERO);
}
if self.is_globe() {
return VerticalPerspectiveProjection::lat_lng_to_screen_coordinate(self, lat_lng, height);
}
let pt = Projection::project(lat_lng, self.params.scale);
let p = self.matrices.coord * DVec4::new(pt.x / TILE_SIZE_D, pt.y / TILE_SIZE_D, height, 1.0);
(ScreenCoordinate::new(p.x / p.w, f64::from(self.params.size.height) - p.y / p.w), p)
}
#[must_use]
pub fn is_location_occluded(&self, lat_lng: &LatLng) -> bool {
if self.params.size.is_empty() || !self.is_globe() {
return false;
}
let surface = VerticalPerspectiveProjection::surface_vector(lat_lng);
self.globe_clipping_plane().dot(surface.extend(1.0)) < 0.0
}
#[must_use]
pub fn screen_coordinate_to_tile_coordinate(&self, point: &ScreenCoordinate, at_zoom: u8) -> TileCoordinate {
if self.params.size.is_empty() {
return TileCoordinate { p: Point::new(0.0, 0.0), z: 0.0 };
}
if self.is_globe() {
return match VerticalPerspectiveProjection::screen_coordinate_to_lat_lng(
self,
point,
0.0,
WrapMode::Unwrapped,
) {
Some(lat_lng) => TileCoordinate::from_lat_lng(f64::from(at_zoom), &lat_lng),
None => TileCoordinate { p: Point::new(0.0, 0.0), z: 0.0 },
};
}
self.mercator_screen_coordinate_to_tile_coordinate(point, at_zoom, 0.0)
}
fn mercator_screen_coordinate_to_tile_coordinate(
&self,
point: &ScreenCoordinate,
at_zoom: u8,
height: f64,
) -> TileCoordinate {
let flipped_y = f64::from(self.params.size.height) - point.y;
let inverted = self.matrices.inverted_from_center;
let offset0 = inverted.project_point3(DVec3::new(point.x, flipped_y, 0.0));
let offset1 = inverted.project_point3(DVec3::new(point.x, flipped_y, 1.0));
let z0 = offset0.z;
let z1 = offset1.z;
let t = if z0 <= z1 { 0.0 } else { -(z0 - height) / (z1 - z0) };
let offset = offset0 + (offset1 - offset0) * t;
let center = self.center_coordinate();
let factor = f64::from(1i32.wrapping_shl(u32::from(at_zoom)));
let scale = self.params.scale;
TileCoordinate {
p: Point::new((center.x + offset.x) / scale * factor, (center.y + offset.y) / scale * factor),
z: f64::from(at_zoom),
}
}
pub fn screen_coordinate_to_lat_lng(&self, point: &ScreenCoordinate, wrap_mode: WrapMode) -> Result<LatLng, Error> {
self.screen_coordinate_to_lat_lng_at(point, 0.0, wrap_mode)
}
pub fn screen_coordinate_to_lat_lng_at(
&self,
point: &ScreenCoordinate,
height: f64,
wrap_mode: WrapMode,
) -> Result<LatLng, Error> {
if self.is_globe() && !self.params.size.is_empty() {
if point.x.is_nan() || point.y.is_nan() {
return Err(Error::invalid_argument("screen coordinate must not be NaN"));
}
if !point.x.is_finite() || !point.y.is_finite() {
return Err(Error::invalid_argument("screen coordinate must not be infinite"));
}
return VerticalPerspectiveProjection::screen_coordinate_to_lat_lng(self, point, height, wrap_mode)
.ok_or_else(|| Error::invalid_argument("the camera shows no location"));
}
let coord = if self.params.size.is_empty() {
self.screen_coordinate_to_tile_coordinate(point, 0)
} else {
self.mercator_screen_coordinate_to_tile_coordinate(point, 0, height)
};
Projection::unproject(&coord.p, 1.0 / TILE_SIZE_D, wrap_mode)
}
#[must_use]
pub fn line_of_sight(&self, point: &ScreenCoordinate) -> Option<(DVec3, DVec3)> {
if self.is_globe() || self.matrices.projection == DMat4::ZERO {
return None;
}
let flipped_y = f64::from(self.params.size.height) - point.y;
let inverted = self.matrices.inverted_from_center;
let center = self.center_coordinate();
let scale = self.params.scale;
let world = |depth: f64| {
let offset = inverted.project_point3(DVec3::new(point.x, flipped_y, depth));
DVec3::new((center.x + offset.x) / scale, (center.y + offset.y) / scale, offset.z)
};
let (near, far) = (world(-1.0), world(1.0));
(near.is_finite() && far.is_finite()).then_some((near, far))
}
#[must_use]
pub fn max_pitch_scale_factor(&self) -> f64 {
if self.params.size.is_empty() {
return 0.0;
}
let corner = self.mercator_screen_coordinate_to_tile_coordinate(
&ScreenCoordinate::new(0.0, f64::from(self.params.size.height)),
0,
0.0,
);
let Ok(lat_lng) = Projection::unproject(&corner.p, 1.0 / TILE_SIZE_D, WrapMode::Unwrapped) else {
return 0.0;
};
let scale = self.params.scale;
let pt = if self.is_globe() {
Point::new(corner.p.x * scale, corner.p.y * scale)
} else {
let pt = Projection::project(&lat_lng, scale);
Point::new(pt.x / TILE_SIZE_D, pt.y / TILE_SIZE_D)
};
let top_point = self.matrices.coord * DVec4::new(pt.x, pt.y, 0.0, 1.0);
top_point.w / self.camera_to_center_distance()
}
}