#[cfg(test)]
use std::f64::consts::PI;
use glam::{DMat4, DQuat, DVec3};
#[cfg(test)]
use mln_style::geo::LatLng;
use crate::camera::camera::FreeCameraOptions;
#[cfg(test)]
use crate::geo::LatLngAltitude;
#[cfg(test)]
use crate::geo::constants::M2PI;
use crate::geo::projection::Projection;
#[cfg(test)]
fn mercator_x_from_lng(lng: f64) -> f64 {
(180.0 + lng) / 360.0
}
#[cfg(test)]
fn mercator_y_from_lat(lat: f64) -> f64 {
(180.0 - (180.0 / PI * ((PI / 4.0) + lat * PI / 360.0).tan().ln())) / 360.0
}
#[cfg(test)]
fn lat_from_mercator_y(y: f64) -> f64 {
mln_style::math::rad2deg(2.0 * (PI - y * M2PI).exp().atan() - (PI / 2.0))
}
#[cfg(test)]
fn lng_from_mercator_x(x: f64) -> f64 {
x * 360.0 - 180.0
}
#[cfg(test)]
fn to_mercator(location: &LatLng, altitude_meters: f64) -> DVec3 {
let pixels_per_meter = 1.0 / Projection::meters_per_pixel_at_latitude(location.latitude(), 0.0);
let world_size = Projection::world_size(1.0);
DVec3::new(
mercator_x_from_lng(location.longitude()),
mercator_y_from_lat(location.latitude()),
altitude_meters * pixels_per_meter / world_size,
)
}
pub(crate) fn orientation_from_roll_pitch_bearing(roll: f64, pitch: f64, bearing: f64) -> DQuat {
DQuat::from_axis_angle(DVec3::Z, -bearing)
* DQuat::from_axis_angle(DVec3::X, -pitch)
* DQuat::from_axis_angle(DVec3::Z, roll)
}
fn roll_pitch_bearing(forward: DVec3, right: DVec3, up: DVec3) -> (f64, f64, f64) {
let f01 = forward.truncate().length();
let pitch = f01.atan2(-forward.z);
let (bearing, roll) = if f01 > 1.0e-3 {
(-forward.x.atan2(-forward.y), -right.z.atan2(up.z))
} else {
((-right.y).atan2(right.x), 0.0)
};
(roll, pitch, bearing)
}
#[derive(Clone, Copy, Debug, PartialEq)]
pub(crate) struct CameraPose {
pub(crate) position: DVec3,
pub(crate) orientation: DQuat,
}
impl Default for CameraPose {
fn default() -> Self {
CameraPose { position: DVec3::ZERO, orientation: DQuat::IDENTITY }
}
}
impl CameraPose {
pub(crate) fn from_angles(position: DVec3, roll: f64, pitch: f64, bearing: f64) -> Self {
CameraPose { position, orientation: orientation_from_roll_pitch_bearing(roll, pitch, bearing) }
}
pub(crate) fn world_to_camera(&self, scale: f64, pixels_per_meter: f64, flipped_y: bool) -> DMat4 {
let world_size = Projection::world_size(scale);
let flip = if flipped_y { DMat4::IDENTITY } else { DMat4::from_scale(DVec3::new(1.0, -1.0, 1.0)) };
flip * DMat4::from_quat(self.orientation.conjugate())
* DMat4::from_translation(-self.position * world_size)
* DMat4::from_scale(DVec3::new(1.0, 1.0, pixels_per_meter))
}
pub(crate) fn forward(&self) -> DVec3 {
self.orientation * DVec3::NEG_Z
}
pub(crate) fn right(&self) -> DVec3 {
self.orientation * DVec3::X
}
pub(crate) fn up(&self) -> DVec3 {
self.orientation * DVec3::NEG_Y
}
pub(crate) fn orientation_angles(&self) -> (f64, f64, f64) {
roll_pitch_bearing(self.forward(), self.right(), self.up())
}
pub(crate) fn orientation_from_frame(forward: DVec3, up: DVec3) -> Option<DQuat> {
let right = up.cross(forward);
if right.length() < 1e-15 {
return None;
}
let (roll, pitch, bearing) = roll_pitch_bearing(forward, right, up);
Some(orientation_from_roll_pitch_bearing(roll, pitch, bearing))
}
}
impl FreeCameraOptions {
#[cfg(test)]
pub(crate) fn set_location(&mut self, location: &LatLngAltitude) {
self.position = Some(to_mercator(&location.location, location.altitude));
}
#[cfg(test)]
#[must_use]
pub(crate) fn location(&self) -> Option<LatLngAltitude> {
let position_value = self.position?;
if !(0.0..=1.0).contains(&position_value.y) {
return None;
}
let location =
LatLng::new(lat_from_mercator_y(position_value.y), lng_from_mercator_x(position_value.x)).ok()?;
let meters_per_pixel = Projection::meters_per_pixel_at_latitude(location.latitude(), 0.0);
let world_size = Projection::world_size(1.0);
let altitude = position_value.z * world_size * meters_per_pixel;
Some(LatLngAltitude { location, altitude })
}
#[cfg(test)]
pub(crate) fn look_at_point(&mut self, location: &LatLng, up_vector: Option<DVec3>) {
self.orientation = None;
let Some(position) = self.position else {
return;
};
let forward = to_mercator(location, 0.0) - position;
let mut up = up_vector.unwrap_or(DVec3::Z);
up.z = up.z.abs();
self.orientation = CameraPose::orientation_from_frame(forward, up);
}
#[cfg(test)]
pub(crate) fn set_roll_pitch_bearing(&mut self, roll: f64, pitch: f64, bearing: f64) {
self.orientation = Some(orientation_from_roll_pitch_bearing(
mln_style::math::deg2rad(roll),
mln_style::math::deg2rad(pitch),
mln_style::math::deg2rad(bearing),
));
}
}
#[cfg(test)]
#[path = "camera_pose_test.rs"]
mod camera_pose_test;