pub(crate) const IDENTITY: [f64; 4] = [0.0, 0.0, 0.0, 1.0];
pub(crate) fn mul(a: [f64; 4], b: [f64; 4]) -> [f64; 4] {
let [ax, ay, az, aw] = a;
let [bx, by, bz, bw] = b;
[
aw * bx + ax * bw + ay * bz - az * by,
aw * by - ax * bz + ay * bw + az * bx,
aw * bz + ax * by - ay * bx + az * bw,
aw * bw - ax * bx - ay * by - az * bz,
]
}
pub(crate) fn conjugate(q: [f64; 4]) -> [f64; 4] {
[-q[0], -q[1], -q[2], q[3]]
}
pub(crate) fn positive_w(q: [f64; 4]) -> [f64; 4] {
if q[3] < 0.0 { q.map(|c| -c) } else { q }
}
pub(crate) fn about(axis: usize, radians: f64) -> [f64; 4] {
let (s, c) = (radians * 0.5).sin_cos();
let mut q = [0.0, 0.0, 0.0, c];
q[axis] = s;
q
}
pub(crate) fn about_axis(axis: [f64; 3], radians: f64) -> [f64; 4] {
let (s, c) = (radians * 0.5).sin_cos();
[axis[0] * s, axis[1] * s, axis[2] * s, c]
}
pub(crate) fn euler_xyz([x, y, z]: [f64; 3]) -> [f64; 4] {
mul(about(2, z), mul(about(1, y), about(0, x)))
}
pub(crate) fn angle(a: [f64; 4], b: [f64; 4]) -> f64 {
let d = a.iter().zip(&b).map(|(x, y)| x * y).sum::<f64>().abs().min(1.0);
2.0 * d.acos()
}
pub(crate) fn rotate(q: [f64; 4], v: [f64; 3]) -> [f64; 3] {
let [x, y, z, w] = q;
let [a, b, c] = v;
let t = [2.0 * (y * c - z * b), 2.0 * (z * a - x * c), 2.0 * (x * b - y * a)];
[
a + w * t[0] + (y * t[2] - z * t[1]),
b + w * t[1] + (z * t[0] - x * t[2]),
c + w * t[2] + (x * t[1] - y * t[0]),
]
}
pub(crate) fn length(q: [f64; 4]) -> f64 {
q.iter().map(|c| c * c).sum::<f64>().sqrt()
}
pub(crate) fn unit_above(q: [f64; 4], min: f64) -> Option<[f64; 4]> {
let l = length(q);
(l > min && l.is_finite()).then(|| q.map(|c| c / l))
}
pub(crate) fn unit(q: [f64; 4]) -> [f64; 4] {
unit_above(q, 1e-12).unwrap_or(IDENTITY)
}
pub(crate) fn unit_unchecked(q: [f64; 4]) -> [f64; 4] {
let l = length(q);
q.map(|c| c / l)
}
pub(crate) fn normalize(q: [f64; 4]) -> [f32; 4] {
unit_above(q, 0.0).map_or([0.0, 0.0, 0.0, 1.0], |q| q.map(crate::num::f32_of))
}
pub(crate) fn matrix(q: [f64; 4]) -> [f64; 9] {
let [x, y, z, w] = q;
[
1.0 - 2.0 * (y * y + z * z),
2.0 * (x * y + z * w),
2.0 * (x * z - y * w),
2.0 * (x * y - z * w),
1.0 - 2.0 * (x * x + z * z),
2.0 * (y * z + x * w),
2.0 * (x * z + y * w),
2.0 * (y * z - x * w),
1.0 - 2.0 * (x * x + y * y),
]
}
pub(crate) fn of_rotation(at: impl Fn(usize, usize) -> f64) -> [f64; 4] {
let trace = at(0, 0) + at(1, 1) + at(2, 2);
if trace > 0.0 {
let s = (trace + 1.0).sqrt() * 2.0;
[
(at(2, 1) - at(1, 2)) / s,
(at(0, 2) - at(2, 0)) / s,
(at(1, 0) - at(0, 1)) / s,
0.25 * s,
]
} else {
largest_diagonal(&at)
}
}
pub(crate) fn of_rotation_reciprocal(at: impl Fn(usize, usize) -> f64) -> [f64; 4] {
let trace = at(0, 0) + at(1, 1) + at(2, 2);
if trace > 0.0 {
let k = 0.5 / (trace + 1.0).sqrt();
[
(at(2, 1) - at(1, 2)) * k,
(at(0, 2) - at(2, 0)) * k,
(at(1, 0) - at(0, 1)) * k,
0.25 / k,
]
} else {
largest_diagonal(&at)
}
}
fn largest_diagonal(at: &impl Fn(usize, usize) -> f64) -> [f64; 4] {
if at(0, 0) > at(1, 1) && at(0, 0) > at(2, 2) {
let s = (1.0 + at(0, 0) - at(1, 1) - at(2, 2)).sqrt() * 2.0;
[
0.25 * s,
(at(0, 1) + at(1, 0)) / s,
(at(0, 2) + at(2, 0)) / s,
(at(2, 1) - at(1, 2)) / s,
]
} else if at(1, 1) > at(2, 2) {
let s = (1.0 + at(1, 1) - at(0, 0) - at(2, 2)).sqrt() * 2.0;
[
(at(0, 1) + at(1, 0)) / s,
0.25 * s,
(at(1, 2) + at(2, 1)) / s,
(at(0, 2) - at(2, 0)) / s,
]
} else {
let s = (1.0 + at(2, 2) - at(0, 0) - at(1, 1)).sqrt() * 2.0;
[
(at(0, 2) + at(2, 0)) / s,
(at(1, 2) + at(2, 1)) / s,
0.25 * s,
(at(1, 0) - at(0, 1)) / s,
]
}
}
pub(crate) fn from_rotation(at: impl Fn(usize, usize) -> f64) -> [f64; 4] {
unit(of_rotation(at))
}
pub(crate) fn from_matrix(m: &[f64; 9]) -> [f64; 4] {
from_rotation(|r, c| m[c * 3 + r])
}
fn nearer(a: [f64; 4], b: [f64; 4]) -> ([f64; 4], f64) {
let d: f64 = (0..4).map(|k| a[k] * b[k]).sum();
if d < 0.0 { (b.map(|c| -c), -d) } else { (b, d) }
}
fn weights(d: f64, t: f64) -> Option<(f64, f64)> {
if d > 0.9995 {
return None;
}
let theta = d.acos();
let sin = theta.sin();
Some((((1.0 - t) * theta).sin() / sin, (t * theta).sin() / sin))
}
pub(crate) fn slerp(a: [f64; 4], b: [f64; 4], t: f64) -> [f64; 4] {
let (b, d) = nearer(a, b);
let (wa, wb) = weights(d, t).unwrap_or((1.0 - t, t));
std::array::from_fn(|k| wa * a[k] + wb * b[k])
}
pub(crate) fn slerp_stepped(a: [f64; 4], b: [f64; 4], t: f64) -> [f64; 4] {
let (b, d) = nearer(a, b);
match weights(d, t) {
Some((wa, wb)) => std::array::from_fn(|k| wa * a[k] + wb * b[k]),
None => std::array::from_fn(|k| a[k] + (b[k] - a[k]) * t),
}
}
#[cfg(test)]
mod tests {
use super::*;
fn close(a: &[f64], b: &[f64], eps: f64) -> bool {
a.len() == b.len() && a.iter().zip(b).all(|(x, y)| (x - y).abs() < eps)
}
fn same_turn(a: [f64; 4], b: [f64; 4]) -> bool {
close(&a, &b, 1e-9) || close(&a, &b.map(|c| -c), 1e-9)
}
fn mat_mul(a: &[f64; 9], b: &[f64; 9]) -> [f64; 9] {
std::array::from_fn(|k| {
let (r, c) = (k % 3, k / 3);
(0..3).map(|i| a[i * 3 + r] * b[c * 3 + i]).sum()
})
}
#[test]
fn matrices_and_quaternions_round_trip() {
let turns = [
unit([0.1, 0.7, -0.2, 0.6]),
about(0, 3.0),
about(1, -3.0),
about(2, 3.1),
unit([0.3, -0.5, 0.6, -0.2]),
IDENTITY,
];
for q in turns {
let m = matrix(q);
assert!(same_turn(from_matrix(&m), q), "{q:?}");
assert!(close(&matrix(from_matrix(&m)), &m, 1e-12), "{q:?}");
let by_rows = from_rotation(|r, c| m[c * 3 + r]);
assert_eq!(by_rows.map(f64::to_bits), from_matrix(&m).map(f64::to_bits));
assert!(close(
&unit(of_rotation_reciprocal(|r, c| m[c * 3 + r])),
&by_rows,
1e-12
));
let unchecked = unit_unchecked(of_rotation(|r, c| m[c * 3 + r]));
assert_eq!(unchecked.map(f64::to_bits), by_rows.map(f64::to_bits));
}
let q = unit([0.2, -0.4, 0.1, 0.9]);
let m = matrix(q);
let v = [1.0, -2.0, 0.5];
let by_matrix: [f64; 3] = std::array::from_fn(|r| (0..3).map(|c| m[c * 3 + r] * v[c]).sum());
assert!(close(&by_matrix, &rotate(q, v), 1e-12));
assert_eq!(from_matrix(&[f64::NAN; 9]), [0.0, 0.0, 0.0, 1.0]);
}
#[test]
fn products_compose_rotations() {
let a = about_axis([0.0, 0.0, 1.0], 0.7);
let b = about_axis([1.0, 0.0, 0.0], -1.1);
assert!(close(&matrix(mul(a, b)), &mat_mul(&matrix(a), &matrix(b)), 1e-12));
assert!(close(&mul(a, conjugate(a)), &IDENTITY, 1e-15));
assert_eq!(about(1, 0.4), about_axis([0.0, 1.0, 0.0], 0.4));
assert!(close(
&rotate(about(2, std::f64::consts::FRAC_PI_2), [1.0, 0.0, 0.0]),
&[0.0, 1.0, 0.0],
1e-15
));
let e = euler_xyz([0.3, -0.2, 1.1]);
assert_eq!(e, mul(about(2, 1.1), mul(about(1, -0.2), about(0, 0.3))));
assert!((angle(about(0, 0.5), about(0, -0.25)) - 0.75).abs() < 1e-12);
assert!(angle(e, e.map(|c| -c)) < 1e-6);
}
#[test]
fn normalizing_zero_and_broken_quaternions() {
assert_eq!(unit([0.0; 4]), [0.0, 0.0, 0.0, 1.0]);
assert_eq!(unit([1e-13, 0.0, 0.0, 0.0]), [0.0, 0.0, 0.0, 1.0]);
assert_eq!(unit([f64::NAN, 0.0, 0.0, 1.0]), [0.0, 0.0, 0.0, 1.0]);
assert_eq!(unit([0.0, 0.0, 0.0, -2.0]), [0.0, 0.0, 0.0, -1.0]);
assert_eq!(unit_above([1e-13, 0.0, 0.0, 0.0], 0.0), Some([1.0, 0.0, 0.0, 0.0]));
assert_eq!(unit_above([0.0; 4], 0.0), None);
assert_eq!(unit_above([f64::INFINITY, 0.0, 0.0, 0.0], 0.0), None);
assert_eq!(normalize([0.0; 4]), [0.0, 0.0, 0.0, 1.0]);
assert_eq!(normalize([0.0, 3.0, 0.0, 4.0]), [0.0, 0.6, 0.0, 0.8]);
assert!(unit_unchecked([0.0; 4]).iter().all(|c| c.is_nan()));
assert_eq!(positive_w([1.0, 0.0, 0.0, -0.5]), [-1.0, -0.0, -0.0, 0.5]);
assert_eq!(positive_w([1.0, 0.0, 0.0, 0.5]), [1.0, 0.0, 0.0, 0.5]);
}
#[test]
fn slerp_ends_and_takes_the_short_way() {
let a = about(1, 0.2);
let b = about(1, 1.4);
for f in [slerp, slerp_stepped] {
assert!(close(&f(a, b, 0.0), &a, 1e-15));
assert!(close(&f(a, b, 1.0), &b, 1e-15));
let half = about(1, 0.8);
assert!(close(&f(a, b, 0.5), &half, 1e-12));
assert!(close(&f(a, b.map(|c| -c), 0.5), &half, 1e-12));
let c = about(1, 0.2005);
assert!(close(&f(a, c, 0.5), &about(1, 0.20025), 1e-7));
}
let (p, q) = ([0.0, 0.0, 0.6, 0.8], [0.0, 0.0, 0.600_003_5, 0.8]);
let t: f64 = 0.3;
let (blended, stepped) = (slerp(p, q, t)[2], slerp_stepped(p, q, t)[2]);
assert_eq!(blended.to_bits(), ((1.0 - t) * p[2] + t * q[2]).to_bits());
assert_eq!(stepped.to_bits(), (p[2] + (q[2] - p[2]) * t).to_bits());
assert_ne!(blended.to_bits(), stepped.to_bits());
}
}