pub use embedded_quaternion_f32::Quaternion;
pub fn normalize(q: Quaternion) -> Option<Quaternion> {
q.normalize()
}
pub fn rotate_xw(q: Quaternion, angle: f32) -> Quaternion {
q.rotate_xw(angle)
}
pub fn rotate_yz(q: Quaternion, angle: f32) -> Quaternion {
q.rotate_yz(angle)
}
pub fn project_2d(q: Quaternion, cx: f32, cy: f32, dist_4d: f32, dist_3d: f32) -> (i16, i16, f32) {
q.project_2d(cx, cy, dist_4d, dist_3d)
}
#[cfg(test)]
mod tests {
use super::*;
use core::f32::consts::FRAC_PI_2;
#[test]
fn identities_and_arithmetic() {
let q = Quaternion::new(1.0, 0.0, 0.0, 0.0);
assert_eq!(q, Quaternion::ONE);
assert_eq!(Quaternion::ZERO.norm_squared(), 0.0);
assert_eq!(normalize(Quaternion::ZERO), None);
let a = Quaternion::new(1.0, 2.0, 3.0, 4.0);
let b = Quaternion::new(0.5, 0.5, 0.5, 0.5);
assert_eq!(a + b, Quaternion::new(1.5, 2.5, 3.5, 4.5));
assert_eq!(a - b, Quaternion::new(0.5, 1.5, 2.5, 3.5));
assert_eq!(a * 2.0, Quaternion::new(2.0, 4.0, 6.0, 8.0));
assert_eq!(a.conjugate(), Quaternion::new(1.0, -2.0, -3.0, -4.0));
}
#[test]
fn normalization() {
let q = Quaternion::new(1.0, 2.0, 3.0, 4.0);
let unit = normalize(q).expect("non-zero quaternion should normalize");
assert!((unit.norm_squared() - 1.0).abs() < 1e-3);
}
#[test]
fn rotation_and_projection() {
let p = Quaternion::new(0.0, 1.0, 0.0, 0.0);
let rotated = rotate_xw(p, FRAC_PI_2);
let (sx, sy, depth) = project_2d(rotated, 160.0, 80.0, 5.0, 5.0);
assert!((i16::MIN..=i16::MAX).contains(&sx));
assert!((i16::MIN..=i16::MAX).contains(&sy));
assert!(depth.is_finite());
}
}