mln-rs-engine 0.0.22

Part of mln-rs, a MapLibre map renderer written in Rust. Depend on mln-rs instead.
Documentation
use std::f64::consts::FRAC_1_SQRT_2;

use approx::assert_abs_diff_eq;
use glam::dvec3;

use super::*;

/// A pose's angles come back as they were set; looking straight down, the
/// roll turns into bearing.
#[test]
fn orientation_angles_round_trip() {
    for ((roll, pitch, bearing), expected) in [
        ((0.0, 0.34, 0.22), (0.0, 0.34, 0.22)),
        ((0.45, 0.34, 0.22), (0.45, 0.34, 0.22)),
        ((0.45, 0.0, 0.22), (0.0, 0.0, 0.22 - 0.45)),
    ] {
        let (roll, pitch, bearing) = CameraPose::from_angles(DVec3::ZERO, roll, pitch, bearing).orientation_angles();
        assert_abs_diff_eq!(expected.0, roll, epsilon = 1e-9);
        assert_abs_diff_eq!(expected.1, pitch, epsilon = 1e-9);
        assert_abs_diff_eq!(expected.2, bearing, epsilon = 1e-9);
    }
}

/// The orientation of a camera looking along a direction with an up
/// direction above it: its right, up and forward directions, with the up
/// direction straightened to meet the forward one; none when the two are
/// parallel or up has no direction.
#[test]
fn an_orientation_follows_its_forward_and_up_directions() {
    let c = FRAC_1_SQRT_2;
    for (forward, up, expected) in [
        // Pitched 45 degrees toward the north.
        (dvec3(0.0, -c, -c), DVec3::Z, Some((DVec3::X, dvec3(0.0, -c, c), dvec3(0.0, -c, -c)))),
        // Level, looking east.
        (DVec3::X, DVec3::Z, Some((DVec3::Y, DVec3::Z, DVec3::X))),
        // Straight down, north up, then turned 45 degrees.
        (DVec3::NEG_Z, DVec3::NEG_Y, Some((DVec3::X, DVec3::NEG_Y, DVec3::NEG_Z))),
        (DVec3::NEG_Z, dvec3(-c, -c, 0.0), Some((dvec3(c, -c, 0.0), dvec3(-c, -c, 0.0), DVec3::NEG_Z))),
        // Level, looking south, with up almost along it.
        (DVec3::Y, dvec3(0.0, 1.0, 1e-5).normalize(), Some((DVec3::NEG_X, DVec3::Z, DVec3::Y))),
        (DVec3::NEG_Z, DVec3::NEG_Z, None),
        (DVec3::NEG_Z, DVec3::ZERO, None),
    ] {
        let orientation = CameraPose::orientation_from_frame(forward, up);
        let frame = orientation.map(|q| (q * DVec3::X, q * DVec3::NEG_Y, q * DVec3::NEG_Z));
        match (frame, expected) {
            (Some(actual), Some(expected)) => {
                assert_abs_diff_eq!(expected.0, actual.0, epsilon = 1e-7);
                assert_abs_diff_eq!(expected.1, actual.1, epsilon = 1e-7);
                assert_abs_diff_eq!(expected.2, actual.2, epsilon = 1e-7);
            }
            (actual, expected) => assert_eq!(expected.is_none(), actual.is_none(), "{forward} {up}"),
        }
    }
}