use geometry::Point;
use glam::{DMat4, DVec2, DVec3, DVec4};
use mln_style::constants;
use crate::camera::vertical_perspective_projection::VerticalPerspectiveProjection;
use crate::camera::view::View;
use crate::geo::projection::Projection as WebMercator;
use crate::tile::UnwrappedTileID;
#[derive(Clone, Copy, Debug, Default, PartialEq)]
pub struct ProjectionData {
pub main_matrix: DMat4,
pub(crate) tile_mercator_coords: DVec4,
pub clipping_plane: DVec4,
pub(crate) projection_transition: f64,
pub fallback_matrix: DMat4,
pub(crate) depth_offset: f64,
pub translate: DVec2,
pub(crate) camera_position: DVec3,
pub(crate) sphere_anchor: DVec3,
pub(crate) sphere_anchor_angles: DVec4,
}
#[derive(Clone, Copy, Debug, Default, PartialEq)]
pub struct ProjectedTilePoint {
pub point: Point<f64>,
pub signed_distance_from_camera: f64,
pub occluded: bool,
}
pub(crate) fn mercator_tile_matrix(tile_id: &UnwrappedTileID, scale: f64) -> DMat4 {
let tile_scale: u64 = 1u64 << tile_id.canonical.z;
let s = WebMercator::world_size(scale) / tile_scale as f64;
let x = (i64::from(tile_id.canonical.x) + i64::from(tile_id.wrap) * tile_scale as i64) as f64 * s;
let y = i64::from(tile_id.canonical.y) as f64 * s;
let unit = s / f64::from(constants::EXTENT);
DMat4::from_translation(DVec3::new(x, y, 0.0)) * DMat4::from_scale(DVec3::new(unit, unit, 1.0))
}
#[must_use]
pub(crate) fn mercator_tile_coords(tile_id: &UnwrappedTileID) -> DVec4 {
let tile_scale = (1u64 << tile_id.canonical.z) as f64;
DVec4::new(
f64::from(tile_id.canonical.x) / tile_scale,
f64::from(tile_id.canonical.y) / tile_scale,
1.0 / tile_scale / f64::from(constants::EXTENT),
1.0 / tile_scale / f64::from(constants::EXTENT),
)
}
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
pub enum Projection {
#[default]
Mercator,
VerticalPerspective,
}
impl Projection {
#[must_use]
pub fn projection_data(self, view: &View, tile_id: &UnwrappedTileID, mercator_matrix: &DMat4) -> ProjectionData {
match self {
Projection::Mercator => ProjectionData {
main_matrix: *mercator_matrix,
tile_mercator_coords: mercator_tile_coords(tile_id),
clipping_plane: DVec4::ZERO,
projection_transition: 0.0,
fallback_matrix: *mercator_matrix,
..ProjectionData::default()
},
Projection::VerticalPerspective => {
VerticalPerspectiveProjection::projection_data(view, tile_id, mercator_matrix)
}
}
}
#[must_use]
pub(crate) fn project_tile_point(
self,
data: &ProjectionData,
tile_id: &UnwrappedTileID,
point: &Point<f64>,
elevation: f64,
) -> ProjectedTilePoint {
match self {
Projection::Mercator => {
let pos = data.main_matrix * DVec4::new(point.x, point.y, elevation, 1.0);
ProjectedTilePoint {
point: Point::new(pos.x / pos.w, pos.y / pos.w),
signed_distance_from_camera: pos.w,
occluded: false,
}
}
Projection::VerticalPerspective => {
VerticalPerspectiveProjection::project_tile_point(data, tile_id, point, elevation)
}
}
}
#[must_use]
pub(crate) fn drawn_tile_point(
self,
data: &ProjectionData,
tile_id: &UnwrappedTileID,
point: &Point<f64>,
elevation: f64,
) -> ProjectedTilePoint {
match self {
Projection::Mercator => self.project_tile_point(data, tile_id, point, elevation),
Projection::VerticalPerspective => {
VerticalPerspectiveProjection::drawn_tile_point(data, tile_id, point, elevation)
}
}
}
#[must_use]
pub fn circle_radius_correction(self, view: &View) -> f64 {
match self {
Projection::Mercator => 1.0,
Projection::VerticalPerspective => VerticalPerspectiveProjection::circle_radius_correction(view),
}
}
#[must_use]
pub fn pixel_scale(self, view: &View) -> f64 {
match self {
Projection::Mercator => 1.0,
Projection::VerticalPerspective => VerticalPerspectiveProjection::pixel_scale(view),
}
}
#[must_use]
pub(crate) fn pitched_text_correction(self, view: &View, point: &Point<f64>, tile_id: &UnwrappedTileID) -> f64 {
match self {
Projection::Mercator => 1.0,
Projection::VerticalPerspective => {
VerticalPerspectiveProjection::pitched_text_correction(view, point, tile_id)
}
}
}
}