2018-09-23 20:41:56 +08:00
|
|
|
use na::{Real, Unit, Rotation3, UnitQuaternion, U3};
|
2018-09-22 19:18:59 +08:00
|
|
|
|
2018-09-23 20:41:56 +08:00
|
|
|
use aliases::{Qua, TMat3, TMat4, TVec3, TVec4};
|
2018-09-22 19:18:59 +08:00
|
|
|
|
|
|
|
/// Rotate the vector `v` by the quaternion `q` assumed to be normalized.
|
2018-09-23 20:41:56 +08:00
|
|
|
pub fn quat_cross_vec<N: Real>(q: &Qua<N>, v: &TVec3<N>) -> TVec3<N> {
|
2018-09-22 19:18:59 +08:00
|
|
|
UnitQuaternion::new_unchecked(*q) * v
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Rotate the vector `v` by the inverse of the quaternion `q` assumed to be normalized.
|
2018-09-23 20:41:56 +08:00
|
|
|
pub fn quat_inv_cross_vec<N: Real>(v: &TVec3<N>, q: &Qua<N>) -> TVec3<N> {
|
2018-09-22 19:18:59 +08:00
|
|
|
UnitQuaternion::new_unchecked(*q).inverse() * v
|
|
|
|
}
|
|
|
|
|
|
|
|
/// The quaternion `w` component.
|
2018-09-22 22:40:58 +08:00
|
|
|
pub fn quat_extract_real_component<N: Real>(q: &Qua<N>) -> N {
|
2018-09-22 19:18:59 +08:00
|
|
|
q.w
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Normalized linear interpolation between two quaternions.
|
2018-09-22 22:40:58 +08:00
|
|
|
pub fn quat_fast_mix<N: Real>(x: &Qua<N>, y: &Qua<N>, a: N) -> Qua<N> {
|
2018-09-22 19:18:59 +08:00
|
|
|
Unit::new_unchecked(*x).nlerp(&Unit::new_unchecked(*y), a).unwrap()
|
|
|
|
}
|
|
|
|
|
2018-09-22 22:40:58 +08:00
|
|
|
//pub fn quat_intermediate<N: Real>(prev: &Qua<N>, curr: &Qua<N>, next: &Qua<N>) -> Qua<N> {
|
2018-09-22 19:18:59 +08:00
|
|
|
// unimplemented!()
|
|
|
|
//}
|
|
|
|
|
|
|
|
/// The squared magnitude of a quaternion `q`.
|
2018-09-22 22:40:58 +08:00
|
|
|
pub fn quat_length2<N: Real>(q: &Qua<N>) -> N {
|
2018-09-22 19:18:59 +08:00
|
|
|
q.norm_squared()
|
|
|
|
}
|
|
|
|
|
|
|
|
/// The squared magnitude of a quaternion `q`.
|
2018-09-22 22:40:58 +08:00
|
|
|
pub fn quat_magnitude2<N: Real>(q: &Qua<N>) -> N {
|
2018-09-22 19:18:59 +08:00
|
|
|
q.norm_squared()
|
|
|
|
}
|
|
|
|
|
|
|
|
/// The quaternion representing the identity rotation.
|
2018-09-23 00:11:51 +08:00
|
|
|
pub fn quat_identity<N: Real>() -> Qua<N> {
|
2018-09-22 19:18:59 +08:00
|
|
|
UnitQuaternion::identity().unwrap()
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Rotates a vector by a quaternion assumed to be normalized.
|
2018-09-23 20:41:56 +08:00
|
|
|
pub fn quat_rotate_vec3<N: Real>(q: &Qua<N>, v: &TVec3<N>) -> TVec3<N> {
|
2018-09-22 19:18:59 +08:00
|
|
|
UnitQuaternion::new_unchecked(*q) * v
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Rotates a vector in homogeneous coordinates by a quaternion assumed to be normalized.
|
2018-09-23 20:41:56 +08:00
|
|
|
pub fn quat_rotate_vec<N: Real>(q: &Qua<N>, v: &TVec4<N>) -> TVec4<N> {
|
2018-09-22 19:18:59 +08:00
|
|
|
let rotated = Unit::new_unchecked(*q) * v.fixed_rows::<U3>(0);
|
2018-09-23 20:41:56 +08:00
|
|
|
TVec4::new(rotated.x, rotated.y, rotated.z, v.w)
|
2018-09-22 19:18:59 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/// The rotation required to align `orig` to `dest`.
|
2018-09-23 20:41:56 +08:00
|
|
|
pub fn quat_rotation<N: Real>(orig: &TVec3<N>, dest: &TVec3<N>) -> Qua<N> {
|
2018-09-22 19:18:59 +08:00
|
|
|
UnitQuaternion::rotation_between(orig, dest).unwrap_or(UnitQuaternion::identity()).unwrap()
|
|
|
|
}
|
|
|
|
|
|
|
|
/// The spherical linear interpolation between two quaternions.
|
2018-09-22 22:40:58 +08:00
|
|
|
pub fn quat_short_mix<N: Real>(x: &Qua<N>, y: &Qua<N>, a: N) -> Qua<N> {
|
2018-09-22 19:18:59 +08:00
|
|
|
Unit::new_normalize(*x).slerp(&Unit::new_normalize(*y), a).unwrap()
|
|
|
|
}
|
|
|
|
|
2018-09-22 22:40:58 +08:00
|
|
|
//pub fn quat_squad<N: Real>(q1: &Qua<N>, q2: &Qua<N>, s1: &Qua<N>, s2: &Qua<N>, h: N) -> Qua<N> {
|
2018-09-22 19:18:59 +08:00
|
|
|
// unimplemented!()
|
|
|
|
//}
|
|
|
|
|
|
|
|
/// Converts a quaternion to a rotation matrix.
|
2018-09-23 20:41:56 +08:00
|
|
|
pub fn quat_to_mat3<N: Real>(x: &Qua<N>) -> TMat3<N> {
|
2018-09-22 19:18:59 +08:00
|
|
|
UnitQuaternion::new_unchecked(*x).to_rotation_matrix().unwrap()
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Converts a quaternion to a rotation matrix in homogenous coordinates.
|
2018-09-23 20:41:56 +08:00
|
|
|
pub fn quat_to_mat4<N: Real>(x: &Qua<N>) -> TMat4<N> {
|
2018-09-22 19:18:59 +08:00
|
|
|
UnitQuaternion::new_unchecked(*x).to_homogeneous()
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Converts a rotation matrix to a quaternion.
|
2018-09-23 20:41:56 +08:00
|
|
|
pub fn mat3_to_quat<N: Real>(x: &TMat3<N>) -> Qua<N> {
|
2018-09-22 19:18:59 +08:00
|
|
|
let r = Rotation3::from_matrix_unchecked(*x);
|
|
|
|
UnitQuaternion::from_rotation_matrix(&r).unwrap()
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Converts a rotation matrix in homogeneous coordinates to a quaternion.
|
2018-09-23 20:41:56 +08:00
|
|
|
pub fn to_quat<N: Real>(x: &TMat4<N>) -> Qua<N> {
|
2018-09-22 19:18:59 +08:00
|
|
|
let rot = x.fixed_slice::<U3, U3>(0, 0).into_owned();
|
2018-09-23 00:11:51 +08:00
|
|
|
mat3_to_quat(&rot)
|
2018-09-22 19:18:59 +08:00
|
|
|
}
|
|
|
|
|