use geometry::Point;
use glam::{DMat4, DVec2, DVec3, Vec2};
use mln_gfx::shaders::layer_ubo::ProjectionUBO;
use mln_style::constants::EXTENT;
use mln_style::types::TranslateAnchorType;
use crate::camera::{ProjectionData, View};
use crate::draw::cast_matrix;
use crate::draw::draws::LayerFrame;
use crate::geo::constants::M2PI;
use crate::render::paint_parameters::TransformParameters;
use crate::render::tile::RenderTile;
use crate::tile::UnwrappedTileID;
#[expect(
clippy::too_many_arguments,
reason = "the tile, the frame and the translation, origin, clipping and alignment options of its matrix"
)]
pub(crate) fn tile_matrix(
tile_id: &UnwrappedTileID,
frame: &LayerFrame<'_>,
translation: [f32; 2],
anchor: TranslateAnchorType,
near_clipped: bool,
in_viewport_pixel_units: bool,
origin: Option<Point<f64>>,
aligned: bool,
) -> DMat4 {
tile_matrix_for(
tile_id,
frame.view,
frame.transform_params,
translation,
anchor,
origin,
near_clipped,
in_viewport_pixel_units,
aligned,
)
}
#[expect(
clippy::too_many_arguments,
reason = "the tile, the frame and the translation, origin, clipping and alignment options of its projection"
)]
#[must_use]
pub fn projection_data(
tile_id: &UnwrappedTileID,
frame: &LayerFrame<'_>,
translation: [f32; 2],
anchor: TranslateAnchorType,
near_clipped: bool,
in_viewport_pixel_units: bool,
origin: Option<Point<f64>>,
aligned: bool,
) -> ProjectionData {
let mut data = frame.view.projection_data_for_matrix(
tile_id,
&tile_matrix(tile_id, frame, translation, anchor, near_clipped, in_viewport_pixel_units, origin, aligned),
);
if !in_viewport_pixel_units {
let translate = RenderTile::tile_unit_translation(tile_id, translation, anchor, frame.view);
data.translate = DVec2::new(f64::from(translate[0]), f64::from(translate[1]));
}
data
}
#[must_use]
pub fn to_projection_ubo(data: &ProjectionData) -> ProjectionUBO {
let anchor = data.sphere_anchor;
let plane = data.clipping_plane;
ProjectionUBO {
matrix: cast_matrix(&(data.main_matrix * DMat4::from_translation(anchor))),
fallback_matrix: cast_matrix(&data.fallback_matrix),
tile_mercator_coords: data.tile_mercator_coords.as_vec4().to_array(),
clipping_plane: data.clipping_plane.as_vec4().to_array(),
projection_transition: data.projection_transition as f32,
depth_offset: data.depth_offset as f32,
translate: data.translate.as_vec2().to_array(),
anchor: anchor.extend(anchor.dot(plane.truncate()) + plane.w).as_vec4().to_array(),
anchor_angles: data.sphere_anchor_angles.as_vec4().to_array(),
}
}
#[must_use]
pub fn globe_extrude_scale(tile_id: &UnwrappedTileID, zoom: f32, latitude_scale: f64) -> f32 {
(f64::from(tile_id.pixels_to_tile_units(1.0, zoom)) / (f64::from(EXTENT) * (1u64 << tile_id.canonical.z) as f64)
* M2PI
* latitude_scale) as f32
}
#[expect(
clippy::too_many_arguments,
reason = "the tile, the transform and the translation, origin, clipping and alignment options of its matrix"
)]
pub(crate) fn tile_matrix_for(
tile_id: &UnwrappedTileID,
view: &View,
transform_params: &TransformParameters,
translation: [f32; 2],
anchor: TranslateAnchorType,
origin: Option<Point<f64>>,
near_clipped: bool,
in_viewport_pixel_units: bool,
aligned: bool,
) -> DMat4 {
let mut tile_matrix = view.tile_matrix(tile_id);
if let Some(origin) = origin {
tile_matrix *= DMat4::from_translation(DVec3::new(origin.x, origin.y, 0.0));
}
let tile_matrix = with_projection_matrix(&tile_matrix, transform_params, near_clipped, aligned);
translate_vtx_matrix(tile_id, &tile_matrix, translation, anchor, view, in_viewport_pixel_units)
}
pub(crate) fn with_projection_matrix(
matrix: &DMat4,
transform_params: &TransformParameters,
near_clipped: bool,
aligned: bool,
) -> DMat4 {
let proj_matrix_ref = if aligned {
&transform_params.aligned_proj_matrix
} else if near_clipped {
&transform_params.near_clipped_proj_matrix
} else {
&transform_params.proj_matrix
};
*proj_matrix_ref * *matrix
}
pub(crate) fn translate_vtx_matrix(
id: &UnwrappedTileID,
tile_matrix: &DMat4,
translation: [f32; 2],
anchor: TranslateAnchorType,
state: &View,
in_viewport_pixel_units: bool,
) -> DMat4 {
if translation[0] == 0.0 && translation[1] == 0.0 {
return *tile_matrix;
}
let angle: f32 = if in_viewport_pixel_units {
if anchor == TranslateAnchorType::Map { state.angle() as f32 } else { 0.0 }
} else if anchor == TranslateAnchorType::Viewport {
(-state.angle()) as f32
} else {
0.0
};
let translate = Vec2::from_angle(angle).rotate(Vec2::new(translation[0], translation[1]));
let offset = if in_viewport_pixel_units {
DVec3::new(f64::from(translate.x), f64::from(translate.y), 0.0)
} else {
let zoom = state.zoom() as f32;
DVec3::new(
f64::from(id.pixels_to_tile_units(translate.x, zoom)),
f64::from(id.pixels_to_tile_units(translate.y, zoom)),
0.0,
)
};
*tile_matrix * DMat4::from_translation(offset)
}