2018-09-20 20:23:31 +08:00
|
|
|
use na::{self, Real, U3, U4, UnitQuaternion, Vector3, Rotation3};
|
2018-09-20 16:50:34 +08:00
|
|
|
|
|
|
|
use aliases::{Qua, Vec, Mat};
|
|
|
|
|
|
|
|
|
2018-09-20 20:23:31 +08:00
|
|
|
/// Euler angles of the quaternion as (pitch, yaw, roll).
|
|
|
|
pub fn euler_angles<N: Real>(x: &Qua<N>) -> Vec<N, U3> {
|
|
|
|
let q = UnitQuaternion::new_unchecked(*x);
|
|
|
|
let a = q.to_euler_angles();
|
|
|
|
Vector3::new(a.2, a.1, a.0)
|
2018-09-20 16:50:34 +08:00
|
|
|
}
|
|
|
|
|
2018-09-20 20:23:31 +08:00
|
|
|
pub fn greater_than<N: Real>(x: &Qua<N>, y: &Qua<N>) -> Vec<bool, U4> {
|
|
|
|
::greater_than(&x.coords, &y.coords)
|
2018-09-20 16:50:34 +08:00
|
|
|
}
|
|
|
|
|
2018-09-20 20:23:31 +08:00
|
|
|
pub fn greater_than_equal<N: Real>(x: &Qua<N>, y: &Qua<N>) -> Vec<bool, U4> {
|
|
|
|
::greater_than_equal(&x.coords, &y.coords)
|
2018-09-20 16:50:34 +08:00
|
|
|
}
|
|
|
|
|
2018-09-20 20:23:31 +08:00
|
|
|
pub fn less_than<N: Real>(x: &Qua<N>, y: &Qua<N>) -> Vec<bool, U4> {
|
|
|
|
::less_than(&x.coords, &y.coords)
|
2018-09-20 16:50:34 +08:00
|
|
|
}
|
|
|
|
|
2018-09-20 20:23:31 +08:00
|
|
|
pub fn less_than_equal<N: Real>(x: &Qua<N>, y: &Qua<N>) -> Vec<bool, U4> {
|
|
|
|
::less_than_equal(&x.coords, &y.coords)
|
2018-09-20 16:50:34 +08:00
|
|
|
}
|
|
|
|
|
2018-09-20 20:23:31 +08:00
|
|
|
pub fn mat3_cast<N: Real>(x: Qua<N>) -> Mat<N, U3, U3> {
|
|
|
|
let q = UnitQuaternion::new_unchecked(x);
|
|
|
|
q.to_rotation_matrix().unwrap()
|
2018-09-20 16:50:34 +08:00
|
|
|
}
|
|
|
|
|
2018-09-20 20:23:31 +08:00
|
|
|
pub fn mat4_cast<N: Real>(x: Qua<N>) -> Mat<N, U4, U4> {
|
|
|
|
let q = UnitQuaternion::new_unchecked(x);
|
|
|
|
q.to_homogeneous()
|
2018-09-20 16:50:34 +08:00
|
|
|
}
|
|
|
|
|
2018-09-20 20:23:31 +08:00
|
|
|
pub fn quat_cast<N: Real>(x: Mat<N, U3, U3>) -> Qua<N> {
|
|
|
|
let rot = Rotation3::from_matrix_unchecked(x);
|
|
|
|
UnitQuaternion::from_rotation_matrix(&rot).unwrap()
|
2018-09-20 16:50:34 +08:00
|
|
|
}
|
|
|
|
|
2018-09-20 20:23:31 +08:00
|
|
|
pub fn quat_cast2<N: Real>(x: Mat<N, U4, U4>) -> Qua<N> {
|
|
|
|
quat_cast(x.fixed_slice::<U3, U3>(0, 0).into_owned())
|
2018-09-20 16:50:34 +08:00
|
|
|
}
|
|
|
|
|
2018-09-20 20:23:31 +08:00
|
|
|
pub fn quat_look_at<N: Real>(direction: &Vec<N, U3>, up: &Vec<N, U3>) -> Qua<N> {
|
|
|
|
quat_look_at_rh(direction, up)
|
2018-09-20 16:50:34 +08:00
|
|
|
}
|
|
|
|
|
2018-09-20 20:23:31 +08:00
|
|
|
pub fn quat_look_at_lh<N: Real>(direction: &Vec<N, U3>, up: &Vec<N, U3>) -> Qua<N> {
|
|
|
|
UnitQuaternion::look_at_lh(direction, up).unwrap()
|
2018-09-20 16:50:34 +08:00
|
|
|
}
|
|
|
|
|
2018-09-20 20:23:31 +08:00
|
|
|
pub fn quat_look_at_rh<N: Real>(direction: &Vec<N, U3>, up: &Vec<N, U3>) -> Qua<N> {
|
|
|
|
UnitQuaternion::look_at_rh(direction, up).unwrap()
|
2018-09-20 16:50:34 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
pub fn roll<N: Real>(x: &Qua<N>) -> N {
|
2018-09-20 20:23:31 +08:00
|
|
|
// FIXME: optimize this.
|
|
|
|
euler_angles(x).z
|
2018-09-20 16:50:34 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
pub fn yaw<N: Real>(x: &Qua<N>) -> N {
|
2018-09-20 20:23:31 +08:00
|
|
|
// FIXME: optimize this.
|
|
|
|
euler_angles(x).y
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn pitch<N: Real>(x: &Qua<N>) -> N {
|
|
|
|
// FIXME: optimize this.
|
|
|
|
euler_angles(x).x
|
2018-09-20 16:50:34 +08:00
|
|
|
}
|