2018-09-21 04:12:26 +08:00
|
|
|
use na::{Real, U2, U3, U4, Rotation3, Vector3, Unit, UnitComplex};
|
2018-09-21 01:54:12 +08:00
|
|
|
|
2018-09-21 04:12:26 +08:00
|
|
|
use aliases::{Vec, Mat};
|
|
|
|
|
2018-09-22 19:18:59 +08:00
|
|
|
/// Build the rotation matrix needed to align `normal` and `up`.
|
2018-09-21 04:12:26 +08:00
|
|
|
pub fn orientation<N: Real>(normal: &Vec<N, U3>, up: &Vec<N, U3>) -> Mat<N, U4, U4> {
|
|
|
|
if let Some(r) = Rotation3::rotation_between(normal, up) {
|
|
|
|
r.to_homogeneous()
|
|
|
|
} else {
|
|
|
|
Mat::<N, U4, U4>::identity()
|
|
|
|
}
|
2018-09-21 01:54:12 +08:00
|
|
|
}
|
|
|
|
|
2018-09-22 19:18:59 +08:00
|
|
|
/// Rotate a two dimensional vector.
|
2018-09-21 01:54:12 +08:00
|
|
|
pub fn rotate2<N: Real>(v: &Vec<N, U2>, angle: N) -> Vec<N, U2> {
|
2018-09-21 04:12:26 +08:00
|
|
|
UnitComplex::new(angle) * v
|
2018-09-21 01:54:12 +08:00
|
|
|
}
|
|
|
|
|
2018-09-22 19:18:59 +08:00
|
|
|
/// Rotate a three dimensional vector around an axis.
|
2018-09-21 01:54:12 +08:00
|
|
|
pub fn rotate<N: Real>(v: &Vec<N, U3>, angle: N, normal: &Vec<N, U3>) -> Vec<N, U3> {
|
2018-09-21 04:12:26 +08:00
|
|
|
Rotation3::from_axis_angle(&Unit::new_normalize(*normal), angle) * v
|
2018-09-21 01:54:12 +08:00
|
|
|
}
|
|
|
|
|
2018-09-22 19:18:59 +08:00
|
|
|
/// Rotate a thee dimensional vector in homogeneous coordinates around an axis.
|
2018-09-21 01:54:12 +08:00
|
|
|
pub fn rotate4<N: Real>(v: &Vec<N, U4>, angle: N, normal: &Vec<N, U3>) -> Vec<N, U4> {
|
2018-09-21 04:12:26 +08:00
|
|
|
Rotation3::from_axis_angle(&Unit::new_normalize(*normal), angle).to_homogeneous() * v
|
2018-09-21 01:54:12 +08:00
|
|
|
}
|
|
|
|
|
2018-09-22 19:18:59 +08:00
|
|
|
/// Rotate a three dimensional vector around the `X` axis.
|
|
|
|
pub fn rotate_x<N: Real>(v: &Vec<N, U3>, angle: N) -> Vec<N, U3> {
|
2018-09-21 04:12:26 +08:00
|
|
|
Rotation3::from_axis_angle(&Vector3::x_axis(), angle) * v
|
2018-09-21 01:54:12 +08:00
|
|
|
}
|
|
|
|
|
2018-09-22 19:18:59 +08:00
|
|
|
/// Rotate a three dimensional vector in homogeneous coordinates around the `X` axis.
|
|
|
|
pub fn rotate_x4<N: Real>(v: &Vec<N, U4>, angle: N) -> Vec<N, U4> {
|
2018-09-21 04:12:26 +08:00
|
|
|
Rotation3::from_axis_angle(&Vector3::x_axis(), angle).to_homogeneous() * v
|
2018-09-21 01:54:12 +08:00
|
|
|
}
|
|
|
|
|
2018-09-22 19:18:59 +08:00
|
|
|
/// Rotate a three dimensional vector around the `Y` axis.
|
|
|
|
pub fn rotate_y<N: Real>(v: &Vec<N, U3>, angle: N) -> Vec<N, U3> {
|
2018-09-21 04:12:26 +08:00
|
|
|
Rotation3::from_axis_angle(&Vector3::y_axis(), angle) * v
|
2018-09-21 01:54:12 +08:00
|
|
|
}
|
|
|
|
|
2018-09-22 19:18:59 +08:00
|
|
|
/// Rotate a three dimensional vector in homogeneous coordinates around the `Y` axis.
|
|
|
|
pub fn rotate_y4<N: Real>(v: &Vec<N, U4>, angle: N) -> Vec<N, U4> {
|
2018-09-21 04:12:26 +08:00
|
|
|
Rotation3::from_axis_angle(&Vector3::y_axis(), angle).to_homogeneous() * v
|
2018-09-21 01:54:12 +08:00
|
|
|
}
|
|
|
|
|
2018-09-22 19:18:59 +08:00
|
|
|
/// Rotate a three dimensional vector around the `Z` axis.
|
|
|
|
pub fn rotate_z<N: Real>(v: &Vec<N, U3>, angle: N) -> Vec<N, U3> {
|
2018-09-21 04:12:26 +08:00
|
|
|
Rotation3::from_axis_angle(&Vector3::z_axis(), angle) * v
|
2018-09-21 01:54:12 +08:00
|
|
|
}
|
|
|
|
|
2018-09-22 19:18:59 +08:00
|
|
|
/// Rotate a three dimensional vector in homogeneous coordinates around the `Z` axis.
|
|
|
|
pub fn rotate_z4<N: Real>(v: &Vec<N, U4>, angle: N) -> Vec<N, U4> {
|
2018-09-21 04:12:26 +08:00
|
|
|
Rotation3::from_axis_angle(&Vector3::z_axis(), angle).to_homogeneous() * v
|
2018-09-21 01:54:12 +08:00
|
|
|
}
|
|
|
|
|
2018-09-22 19:18:59 +08:00
|
|
|
/// Computes a spehical linear interpolation between the vectors `x` and `y` assumed to be normalized.
|
2018-09-21 01:54:12 +08:00
|
|
|
pub fn slerp<N: Real>(x: &Vec<N, U3>, y: &Vec<N, U3>, a: N) -> Vec<N, U3> {
|
2018-09-22 19:18:59 +08:00
|
|
|
Unit::new_unchecked(*x).slerp(&Unit::new_unchecked(*y), a).unwrap()
|
2018-09-21 01:54:12 +08:00
|
|
|
}
|