use std::f64::consts::FRAC_1_SQRT_2;
use approx::assert_abs_diff_eq;
use glam::dvec3;
use super::*;
#[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);
}
}
#[test]
fn an_orientation_follows_its_forward_and_up_directions() {
let c = FRAC_1_SQRT_2;
for (forward, up, expected) in [
(dvec3(0.0, -c, -c), DVec3::Z, Some((DVec3::X, dvec3(0.0, -c, c), dvec3(0.0, -c, -c)))),
(DVec3::X, DVec3::Z, Some((DVec3::Y, DVec3::Z, DVec3::X))),
(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))),
(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}"),
}
}
}