use std::{f32::consts::FRAC_1_SQRT_2, ops::Mul};
use super::sample::DisplayRotation;
#[derive(Clone, Copy, Debug, PartialEq)]
pub struct Quaternion {
pub x: f32,
pub y: f32,
pub z: f32,
pub w: f32,
}
impl Quaternion {
pub const IDENTITY: Self = Self::new(0.0, 0.0, 0.0, 1.0);
pub const fn new(x: f32, y: f32, z: f32, w: f32) -> Self {
Self { x, y, z, w }
}
pub const fn conjugate(self) -> Self {
Self::new(-self.x, -self.y, -self.z, self.w)
}
pub fn length(self) -> f32 {
(self.x * self.x + self.y * self.y + self.z * self.z + self.w * self.w).sqrt()
}
pub fn normalized(self) -> Self {
let length = self.length();
if length.is_finite() && length > 0.0 {
Self::new(
self.x / length,
self.y / length,
self.z / length,
self.w / length,
)
} else {
Self::IDENTITY
}
}
pub fn rotate(self, v: [f32; 3]) -> [f32; 3] {
let q = [self.x, self.y, self.z];
let t = cross(q, v).map(|component| 2.0 * component);
let u = cross(q, t);
[
v[0] + self.w * t[0] + u[0],
v[1] + self.w * t[1] + u[1],
v[2] + self.w * t[2] + u[2],
]
}
pub fn from_rotation_vector(x: f32, y: f32, z: f32, w: Option<f32>) -> Self {
let w = w
.filter(|w| w.is_finite())
.unwrap_or_else(|| (1.0 - x * x - y * y - z * z).max(0.0).sqrt());
Self::new(x, y, z, w).normalized()
}
pub fn from_core_motion(x: f64, y: f64, z: f64, w: f64) -> Self {
Self::new(narrow(-x), narrow(-y), narrow(-z), narrow(w)).normalized()
}
pub fn in_screen_frame(self, rotation: DisplayRotation) -> Self {
let screen_from_device = match rotation {
DisplayRotation::Rotation0 => Self::IDENTITY,
DisplayRotation::Rotation90 => Self::new(0.0, 0.0, -FRAC_1_SQRT_2, FRAC_1_SQRT_2),
DisplayRotation::Rotation180 => Self::new(0.0, 0.0, -1.0, 0.0),
DisplayRotation::Rotation270 => Self::new(0.0, 0.0, FRAC_1_SQRT_2, FRAC_1_SQRT_2),
};
self * screen_from_device
}
pub fn from_device_orientation(alpha: f64, beta: f64, gamma: f64) -> Self {
let (sin_beta, cos_beta) = (beta.to_radians() / 2.0).sin_cos();
let (sin_gamma, cos_gamma) = (gamma.to_radians() / 2.0).sin_cos();
let (sin_alpha, cos_alpha) = (alpha.to_radians() / 2.0).sin_cos();
Self::new(
narrow(sin_beta * cos_gamma * cos_alpha - cos_beta * sin_gamma * sin_alpha),
narrow(cos_beta * sin_gamma * cos_alpha + sin_beta * cos_gamma * sin_alpha),
narrow(cos_beta * cos_gamma * sin_alpha + sin_beta * sin_gamma * cos_alpha),
narrow(cos_beta * cos_gamma * cos_alpha - sin_beta * sin_gamma * sin_alpha),
)
.normalized()
}
}
fn narrow(component: f64) -> f32 {
#[allow(
clippy::cast_possible_truncation,
reason = "a component within -1 and 1 loses only the digits `f32` does not hold"
)]
let narrow = component as f32;
narrow
}
impl Mul for Quaternion {
type Output = Self;
fn mul(self, other: Self) -> Self {
Self::new(
self.w * other.x + self.x * other.w + self.y * other.z - self.z * other.y,
self.w * other.y - self.x * other.z + self.y * other.w + self.z * other.x,
self.w * other.z + self.x * other.y - self.y * other.x + self.z * other.w,
self.w * other.w - self.x * other.x - self.y * other.y - self.z * other.z,
)
}
}
fn cross(a: [f32; 3], b: [f32; 3]) -> [f32; 3] {
[
a[1] * b[2] - a[2] * b[1],
a[2] * b[0] - a[0] * b[2],
a[0] * b[1] - a[1] * b[0],
]
}