mln-rs-engine 0.0.22

Part of mln-rs, a MapLibre map renderer written in Rust. Depend on mln-rs instead.
Documentation
//! The free camera: a position and an orientation that set the camera, and
//! what it keeps of values it can't take.

use std::f64::consts::PI;

use approx::{assert_abs_diff_eq, assert_ulps_eq};
use glam::{DQuat, DVec3, EulerRot, dvec3};
use mln_style::math::deg2rad;

use super::*;
use crate::camera::FreeCameraOptions;

/// The rotation by `x` about x, then `y` about y, then `z` about z.
fn euler(x: f64, y: f64, z: f64) -> DQuat {
    DQuat::from_euler(EulerRot::ZYX, z, y, x)
}

/// The camera's right, up and forward directions.
fn frame(q: DQuat) -> (DVec3, DVec3, DVec3) {
    (q * DVec3::X, q * DVec3::NEG_Y, q * DVec3::NEG_Z)
}

/// A camera without a size keeps the default camera: the origin, looking
/// straight down.
#[test]
fn an_unsized_camera_ignores_the_free_camera() {
    let mut transform = new_transform();
    assert_eq!(Some(DVec3::ZERO), transform.view().free_camera_options().position);
    transform.set_free_camera_options(&FreeCameraOptions {
        orientation: Some(DQuat::from_xyzw(1.0, 1.0, 1.0, 1.0)),
        position: Some(dvec3(0.1, 0.2, 0.3)),
    });
    let options = transform.view().free_camera_options();
    assert_eq!(Some(DQuat::IDENTITY), options.orientation);
    assert_abs_diff_eq!(DVec3::ZERO, options.position.unwrap(), epsilon = 1e-5);
}

/// A NaN position or orientation is ignored, and only it: the other value
/// still applies.
#[test]
fn a_nan_value_is_ignored_alone() {
    let mut transform = sized(100, 100);
    transform.set_free_camera_options(&FreeCameraOptions { position: Some(dvec3(0.5, 0.5, 0.25)), orientation: None });
    transform
        .set_free_camera_options(&FreeCameraOptions { position: Some(dvec3(0.0, 0.0, f64::NAN)), orientation: None });
    assert_eq!(Some(dvec3(0.5, 0.5, 0.25)), transform.view().free_camera_options().position);

    transform.set_free_camera_options(&FreeCameraOptions {
        position: Some(dvec3(0.3, 0.1, 0.2)),
        orientation: Some(DQuat::from_xyzw(f64::NAN, 0.0, f64::NAN, 0.0)),
    });
    let options = transform.view().free_camera_options();
    assert_abs_diff_eq!(dvec3(0.3, 0.1, 0.2), options.position.unwrap(), epsilon = 1e-5);
    assert_eq!(Some(DQuat::IDENTITY), options.orientation);
}

/// A height at or below the ground, or past the world's size, lands on the
/// zoom limits.
#[test]
fn a_height_out_of_range_lands_on_the_zoom_limits() {
    let mut transform = sized(100, 100);
    transform.set_free_camera_options(&FreeCameraOptions { position: Some(dvec3(0.1, 0.1, 0.0)), orientation: None });
    assert_ulps_eq!(transform.state().limits().max_zoom, transform.view().zoom());
    assert!(transform.view().free_camera_options().position.unwrap().z > 0.0);

    transform
        .set_free_camera_options(&FreeCameraOptions { position: Some(dvec3(0.5, 0.2, 123.456)), orientation: None });
    assert_ulps_eq!(transform.state().min_zoom(), transform.view().zoom());
    assert!(transform.view().free_camera_options().position.unwrap().z <= 1.0);
}

/// An orientation sets the bearing, the pitch and the roll, past 90 degrees
/// of pitch when the limits allow. The camera keeps its position, and the
/// center moves to what it looks at.
#[test]
fn an_orientation_sets_the_angles() {
    let mut transform = sized(100, 100);
    limit(&mut transform, |limits| limits.max_pitch = PI).unwrap();
    // Far enough in that the world's edges don't move the center.
    transform.jump_to(&CameraOptions { center: Some(ll(10.0, 20.0)), zoom: Some(10.0), ..Default::default() });
    let position = transform.view().free_camera_options().position.unwrap();
    for (orientation, bearing, pitch, roll) in [
        (DQuat::IDENTITY, 0.0, 0.0, 0.0),
        (DQuat::from_axis_angle(DVec3::X, deg2rad(-60.0)), 0.0, 60.0, 0.0),
        (DQuat::from_axis_angle(DVec3::Z, deg2rad(56.0)), 56.0, 0.0, 0.0),
        (euler(0.0, 0.0, deg2rad(-179.0)) * euler(deg2rad(-30.0), 0.0, 0.0), -179.0, 30.0, 0.0),
        (
            DQuat::from_axis_angle(DVec3::Z, deg2rad(-33.0))
                * DQuat::from_axis_angle(DVec3::X, deg2rad(-22.0))
                * DQuat::from_axis_angle(DVec3::Z, deg2rad(11.0)),
            -33.0,
            22.0,
            11.0,
        ),
    ] {
        transform.set_free_camera_options(&FreeCameraOptions { position: None, orientation: Some(orientation) });
        let view = transform.view();
        assert_abs_diff_eq!(deg2rad(-bearing), view.angle(), epsilon = 1e-12);
        assert_abs_diff_eq!(deg2rad(pitch), view.pitch(), epsilon = 1e-12);
        assert_abs_diff_eq!(deg2rad(roll), view.roll(), epsilon = 1e-12);
        assert_abs_diff_eq!(position, view.free_camera_options().position.unwrap(), epsilon = 1e-9);
    }

    for pitch in [90.0, 135.0] {
        let position = dvec3(0.1, 0.1, 0.1);
        let orientation = DQuat::from_axis_angle(DVec3::X, deg2rad(-pitch));
        transform
            .set_free_camera_options(&FreeCameraOptions { position: Some(position), orientation: Some(orientation) });
        assert_abs_diff_eq!(deg2rad(pitch), transform.view().pitch(), epsilon = 1e-12);
        assert_abs_diff_eq!(0.0, transform.view().angle(), epsilon = 1e-12);
        assert_abs_diff_eq!(position, transform.view().free_camera_options().position.unwrap(), epsilon = 1e-5);
    }
}

/// An orientation with no direction is the identity; one in gimbal lock,
/// looking along the horizon, and one upside down are kept as given.
#[test]
fn a_degenerate_orientation_is_kept_or_reset() {
    let mut transform = sized(100, 100);
    limit(&mut transform, |limits| limits.max_pitch = PI).unwrap();
    let set = |transform: &mut Transform, orientation: DQuat| {
        transform.set_free_camera_options(&FreeCameraOptions { orientation: Some(orientation), position: None });
        transform.view().free_camera_options().orientation.unwrap()
    };
    assert_eq!(DQuat::IDENTITY, set(&mut transform, DQuat::from_xyzw(0.0, 0.0, 0.0, 0.0)));
    let gimbal_lock = set(&mut transform, DQuat::from_axis_angle(DVec3::Y, PI / 2.0));
    assert_abs_diff_eq!(euler(PI / 2.0, PI / 2.0, PI / 2.0), gimbal_lock, epsilon = 1e-12);
    let upside_down = DQuat::from_axis_angle(DVec3::X, PI / 2.0 + PI / 4.0);
    assert_abs_diff_eq!(upside_down, set(&mut transform, upside_down), epsilon = 1e-12);
}

/// A camera placed far off the map, looking away from it, is pulled back to
/// the edge of the world it may show, with the orientation it asked for.
#[test]
fn a_camera_off_the_map_is_pulled_back_to_its_edge() {
    let mut transform = sized(100, 100);
    transform.set_constrain_mode(ConstrainMode::WidthAndHeight);
    transform.jump_to(&CameraOptions { zoom: Some(8.56), ..Default::default() });
    transform.set_free_camera_options(&FreeCameraOptions {
        position: Some(dvec3(-100.0, -10000.0, 1000.0)),
        orientation: Some(euler(deg2rad(-45.0), 0.0, 0.0)),
    });

    // The center is half the view, 50 px at zoom 2, from the world's edges.
    let view = transform.view();
    assert_ulps_eq!(206.0, view.x());
    assert_ulps_eq!(206.0, view.y());
    assert_ulps_eq!(0.0, view.angle());
    assert_ulps_eq!(deg2rad(45.0), view.pitch());
    let options = view.free_camera_options();
    assert_abs_diff_eq!(dvec3(0.097_656_2, 0.304_816, 0.20716), options.position.unwrap(), epsilon = 1e-5);
    let (right, up, forward) = frame(options.orientation.unwrap());
    let half = std::f64::consts::FRAC_1_SQRT_2;
    assert_abs_diff_eq!(DVec3::X, right, epsilon = 1e-9);
    assert_abs_diff_eq!(dvec3(0.0, -half, half), up, epsilon = 1e-9);
    assert_abs_diff_eq!(dvec3(0.0, -half, -half), forward, epsilon = 1e-9);
}