nalgebra/nalgebra-glm/src/gtx/rotate_vector.rs

66 lines
2.6 KiB
Rust
Raw Normal View History

2021-08-08 18:59:40 +08:00
use na::{Rotation3, Unit, UnitComplex};
2018-09-21 01:54:12 +08:00
2019-03-23 21:29:07 +08:00
use crate::aliases::{TMat4, TVec2, TVec3, TVec4};
2021-08-08 18:59:40 +08:00
use crate::RealNumber;
2018-09-21 04:12:26 +08:00
/// Build the rotation matrix needed to align `normal` and `up`.
2021-08-08 18:59:40 +08:00
pub fn orientation<T: RealNumber>(normal: &TVec3<T>, up: &TVec3<T>) -> TMat4<T> {
2018-09-21 04:12:26 +08:00
if let Some(r) = Rotation3::rotation_between(normal, up) {
r.to_homogeneous()
} else {
2018-09-23 20:41:56 +08:00
TMat4::identity()
2018-09-21 04:12:26 +08:00
}
2018-09-21 01:54:12 +08:00
}
/// Rotate a two dimensional vector.
2021-08-08 18:59:40 +08:00
pub fn rotate_vec2<T: RealNumber>(v: &TVec2<T>, angle: T) -> TVec2<T> {
2018-09-21 04:12:26 +08:00
UnitComplex::new(angle) * v
2018-09-21 01:54:12 +08:00
}
/// Rotate a three dimensional vector around an axis.
2021-08-08 18:59:40 +08:00
pub fn rotate_vec3<T: RealNumber>(v: &TVec3<T>, angle: T, normal: &TVec3<T>) -> TVec3<T> {
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
}
/// Rotate a thee dimensional vector in homogeneous coordinates around an axis.
2021-08-08 18:59:40 +08:00
pub fn rotate_vec4<T: RealNumber>(v: &TVec4<T>, angle: T, normal: &TVec3<T>) -> TVec4<T> {
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
}
/// Rotate a three dimensional vector around the `X` axis.
2021-08-08 18:59:40 +08:00
pub fn rotate_x_vec3<T: RealNumber>(v: &TVec3<T>, angle: T) -> TVec3<T> {
2018-09-23 20:41:56 +08:00
Rotation3::from_axis_angle(&TVec3::x_axis(), angle) * v
2018-09-21 01:54:12 +08:00
}
/// Rotate a three dimensional vector in homogeneous coordinates around the `X` axis.
2021-08-08 18:59:40 +08:00
pub fn rotate_x_vec4<T: RealNumber>(v: &TVec4<T>, angle: T) -> TVec4<T> {
2018-09-23 20:41:56 +08:00
Rotation3::from_axis_angle(&TVec3::x_axis(), angle).to_homogeneous() * v
2018-09-21 01:54:12 +08:00
}
/// Rotate a three dimensional vector around the `Y` axis.
2021-08-08 18:59:40 +08:00
pub fn rotate_y_vec3<T: RealNumber>(v: &TVec3<T>, angle: T) -> TVec3<T> {
2018-09-23 20:41:56 +08:00
Rotation3::from_axis_angle(&TVec3::y_axis(), angle) * v
2018-09-21 01:54:12 +08:00
}
/// Rotate a three dimensional vector in homogeneous coordinates around the `Y` axis.
2021-08-08 18:59:40 +08:00
pub fn rotate_y_vec4<T: RealNumber>(v: &TVec4<T>, angle: T) -> TVec4<T> {
2018-09-23 20:41:56 +08:00
Rotation3::from_axis_angle(&TVec3::y_axis(), angle).to_homogeneous() * v
2018-09-21 01:54:12 +08:00
}
/// Rotate a three dimensional vector around the `Z` axis.
2021-08-08 18:59:40 +08:00
pub fn rotate_z_vec3<T: RealNumber>(v: &TVec3<T>, angle: T) -> TVec3<T> {
2018-09-23 20:41:56 +08:00
Rotation3::from_axis_angle(&TVec3::z_axis(), angle) * v
2018-09-21 01:54:12 +08:00
}
/// Rotate a three dimensional vector in homogeneous coordinates around the `Z` axis.
2021-08-08 18:59:40 +08:00
pub fn rotate_z_vec4<T: RealNumber>(v: &TVec4<T>, angle: T) -> TVec4<T> {
2018-09-23 20:41:56 +08:00
Rotation3::from_axis_angle(&TVec3::z_axis(), angle).to_homogeneous() * v
2018-09-21 01:54:12 +08:00
}
2018-09-24 12:48:42 +08:00
/// Computes a spherical linear interpolation between the vectors `x` and `y` assumed to be normalized.
2021-08-08 18:59:40 +08:00
pub fn slerp<T: RealNumber>(x: &TVec3<T>, y: &TVec3<T>, a: T) -> TVec3<T> {
2018-10-22 04:11:27 +08:00
Unit::new_unchecked(*x)
.slerp(&Unit::new_unchecked(*y), a)
2018-12-29 19:12:56 +08:00
.into_inner()
2018-09-21 01:54:12 +08:00
}