mln-rs-engine 0.0.23

Part of mln-rs, a MapLibre map renderer written in Rust. Depend on mln-rs instead.
Documentation
//! The camera as a position and an orientation in Web Mercator space, the
//! form that free camera options set and that the projection matrix is built
//! from.

use glam::{DMat4, DQuat, DVec3};

use crate::geo::projection::Projection;

/// The rotation of a camera with these angles, in radians: it turns `bearing`
/// clockwise about the up axis, tilts `pitch` toward the horizon, then rolls.
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)
}

/// The roll, pitch and bearing of a camera that looks along `forward` with
/// `right` and `up` at its sides, in radians. A camera looking straight down
/// has no roll: its bearing takes it.
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)
}

/// A camera's position, in Web Mercator units where the world is 1 wide
/// (z included), and its orientation. It looks along -z toward the map, with
/// y toward the south.
#[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 {
    /// A pose at `position` with the orientation of these angles, in radians.
    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) }
    }

    /// The matrix from world space, in pixels at `scale` with z in meters of
    /// `pixels_per_meter` pixels, to camera space, with y down.
    pub(crate) fn world_to_camera(&self, scale: f64, pixels_per_meter: f64) -> DMat4 {
        let world_size = Projection::world_size(scale);
        DMat4::from_scale(DVec3::new(1.0, -1.0, 1.0))
            * 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))
    }

    /// The direction the camera looks, toward the map.
    pub(crate) fn forward(&self) -> DVec3 {
        self.orientation * DVec3::NEG_Z
    }

    pub(crate) fn right(&self) -> DVec3 {
        self.orientation * DVec3::X
    }

    /// The camera's up direction. Mercator's y points south, so it is the
    /// rotated -y.
    pub(crate) fn up(&self) -> DVec3 {
        self.orientation * DVec3::NEG_Y
    }

    /// The camera's orientation as `(roll, pitch, bearing)`, in radians.
    pub(crate) fn orientation_angles(&self) -> (f64, f64, f64) {
        roll_pitch_bearing(self.forward(), self.right(), self.up())
    }

    /// The orientation of a camera looking along `forward` with `up` above
    /// it, without the twist that a free orientation can have, or `None` if
    /// the two are parallel.
    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))
    }
}

#[cfg(test)]
#[path = "camera_pose_test.rs"]
mod camera_pose_test;