1use geometry::Point;
2use glam::{DMat4, DVec2, DVec3, DVec4};
3use mln_style::constants;
4
5use crate::camera::vertical_perspective_projection::VerticalPerspectiveProjection;
6use crate::camera::view::View;
7use crate::geo::projection::Projection as WebMercator;
8use crate::tile::UnwrappedTileID;
9
10#[derive(Clone, Copy, Debug, Default, PartialEq)]
11pub struct ProjectionData {
12 pub main_matrix: DMat4,
13 pub(crate) tile_mercator_coords: DVec4,
14 pub clipping_plane: DVec4,
15 pub(crate) projection_transition: f64,
16 pub fallback_matrix: DMat4,
17 pub(crate) depth_offset: f64,
20 pub translate: DVec2,
23 pub(crate) camera_position: DVec3,
26 pub(crate) sphere_anchor: DVec3,
30 pub(crate) sphere_anchor_angles: DVec4,
33}
34
35#[derive(Clone, Copy, Debug, Default, PartialEq)]
37pub struct ProjectedTilePoint {
38 pub point: Point<f64>,
39 pub signed_distance_from_camera: f64,
40 pub occluded: bool,
42}
43
44pub(crate) fn mercator_tile_matrix(tile_id: &UnwrappedTileID, scale: f64) -> DMat4 {
46 let tile_scale: u64 = 1u64 << tile_id.canonical.z;
47 let s = WebMercator::world_size(scale) / tile_scale as f64;
48 let x = (i64::from(tile_id.canonical.x) + i64::from(tile_id.wrap) * tile_scale as i64) as f64 * s;
49 let y = i64::from(tile_id.canonical.y) as f64 * s;
50 let unit = s / f64::from(constants::EXTENT);
51 DMat4::from_translation(DVec3::new(x, y, 0.0)) * DMat4::from_scale(DVec3::new(unit, unit, 1.0))
52}
53
54#[must_use]
56pub(crate) fn mercator_tile_coords(tile_id: &UnwrappedTileID) -> DVec4 {
57 let tile_scale = (1u64 << tile_id.canonical.z) as f64;
58 DVec4::new(
59 f64::from(tile_id.canonical.x) / tile_scale,
60 f64::from(tile_id.canonical.y) / tile_scale,
61 1.0 / tile_scale / f64::from(constants::EXTENT),
62 1.0 / tile_scale / f64::from(constants::EXTENT),
63 )
64}
65
66#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
70pub enum Projection {
71 #[default]
72 Mercator,
73 VerticalPerspective,
75}
76
77impl Projection {
78 #[must_use]
81 pub fn projection_data(self, view: &View, tile_id: &UnwrappedTileID, mercator_matrix: &DMat4) -> ProjectionData {
82 match self {
83 Projection::Mercator => ProjectionData {
84 main_matrix: *mercator_matrix,
85 tile_mercator_coords: mercator_tile_coords(tile_id),
86 clipping_plane: DVec4::ZERO,
87 projection_transition: 0.0,
88 fallback_matrix: *mercator_matrix,
89 ..ProjectionData::default()
90 },
91 Projection::VerticalPerspective => {
92 VerticalPerspectiveProjection::projection_data(view, tile_id, mercator_matrix)
93 }
94 }
95 }
96
97 #[must_use]
100 pub(crate) fn project_tile_point(
101 self,
102 data: &ProjectionData,
103 tile_id: &UnwrappedTileID,
104 point: &Point<f64>,
105 elevation: f64,
106 ) -> ProjectedTilePoint {
107 match self {
108 Projection::Mercator => {
109 let pos = data.main_matrix * DVec4::new(point.x, point.y, elevation, 1.0);
110 ProjectedTilePoint {
111 point: Point::new(pos.x / pos.w, pos.y / pos.w),
112 signed_distance_from_camera: pos.w,
113 occluded: false,
114 }
115 }
116 Projection::VerticalPerspective => {
117 VerticalPerspectiveProjection::project_tile_point(data, tile_id, point, elevation)
118 }
119 }
120 }
121
122 #[must_use]
125 pub(crate) fn drawn_tile_point(
126 self,
127 data: &ProjectionData,
128 tile_id: &UnwrappedTileID,
129 point: &Point<f64>,
130 elevation: f64,
131 ) -> ProjectedTilePoint {
132 match self {
133 Projection::Mercator => self.project_tile_point(data, tile_id, point, elevation),
134 Projection::VerticalPerspective => {
135 VerticalPerspectiveProjection::drawn_tile_point(data, tile_id, point, elevation)
136 }
137 }
138 }
139
140 #[must_use]
144 pub fn circle_radius_correction(self, view: &View) -> f64 {
145 match self {
146 Projection::Mercator => 1.0,
147 Projection::VerticalPerspective => VerticalPerspectiveProjection::circle_radius_correction(view),
148 }
149 }
150
151 #[must_use]
154 pub fn pixel_scale(self, view: &View) -> f64 {
155 match self {
156 Projection::Mercator => 1.0,
157 Projection::VerticalPerspective => VerticalPerspectiveProjection::pixel_scale(view),
158 }
159 }
160
161 #[must_use]
164 pub(crate) fn pitched_text_correction(self, view: &View, point: &Point<f64>, tile_id: &UnwrappedTileID) -> f64 {
165 match self {
166 Projection::Mercator => 1.0,
167 Projection::VerticalPerspective => {
168 VerticalPerspectiveProjection::pitched_text_correction(view, point, tile_id)
169 }
170 }
171 }
172}