diff --git a/nalgebra-glm/README b/nalgebra-glm/README index 6ef3ed83..dbbb8896 100644 --- a/nalgebra-glm/README +++ b/nalgebra-glm/README @@ -7,4 +7,5 @@ * Function overload: the vec version is suffixed by _vec * Function overload: the methods taking an epsilon as suffixed by _eps * L1Norm and L2Norm between two vectors have been renamed: l1_distance, l2_distance -* Matrix columnwise comparisons suffixed by _columns, e.g., `equal` -> `equal_columns` \ No newline at end of file +* Matrix columnwise comparisons suffixed by _columns, e.g., `equal` -> `equal_columns` +* All quaternion functions are prefixed by quat_ \ No newline at end of file diff --git a/nalgebra-glm/src/ext/mod.rs b/nalgebra-glm/src/ext/mod.rs index e5fb92fb..93e9d5fa 100644 --- a/nalgebra-glm/src/ext/mod.rs +++ b/nalgebra-glm/src/ext/mod.rs @@ -8,6 +8,11 @@ pub use self::scalar_common::*; pub use self::scalar_constants::*; pub use self::vector_common::*; pub use self::vector_relational::*; +pub use self::quaternion_common::*; +pub use self::quaternion_geometric::*; +pub use self::quaternion_relational::*; +pub use self::quaternion_transform::*; +pub use self::quaternion_trigonometric::*; mod matrix_clip_space; @@ -17,4 +22,9 @@ mod matrix_transform; mod scalar_common; mod scalar_constants; mod vector_common; -mod vector_relational; \ No newline at end of file +mod vector_relational; +mod quaternion_common; +mod quaternion_geometric; +mod quaternion_relational; +mod quaternion_transform; +mod quaternion_trigonometric; \ No newline at end of file diff --git a/nalgebra-glm/src/quat/quaternion_common.rs b/nalgebra-glm/src/ext/quaternion_common.rs similarity index 52% rename from nalgebra-glm/src/quat/quaternion_common.rs rename to nalgebra-glm/src/ext/quaternion_common.rs index 75eaae4d..edf02b18 100644 --- a/nalgebra-glm/src/quat/quaternion_common.rs +++ b/nalgebra-glm/src/ext/quaternion_common.rs @@ -3,33 +3,33 @@ use na::{self, Real, Unit}; use aliases::Qua; /// The conjugate of `q`. -pub fn conjugate(q: &Qua) -> Qua { +pub fn quat_conjugate(q: &Qua) -> Qua { q.conjugate() } /// The inverse of `q`. -pub fn inverse(q: &Qua) -> Qua { +pub fn quat_inverse(q: &Qua) -> Qua { q.try_inverse().unwrap_or(na::zero()) } -//pub fn isinf(x: &Qua) -> Vec { +//pub fn quat_isinf(x: &Qua) -> Vec { // x.coords.map(|e| e.is_inf()) //} -//pub fn isnan(x: &Qua) -> Vec { +//pub fn quat_isnan(x: &Qua) -> Vec { // x.coords.map(|e| e.is_nan()) //} /// Interpolate linearly between `x` and `y`. -pub fn lerp(x: &Qua, y: &Qua, a: N) -> Qua { +pub fn quat_lerp(x: &Qua, y: &Qua, a: N) -> Qua { x.lerp(y, a) } -//pub fn mix(x: &Qua, y: &Qua, a: N) -> Qua { +//pub fn quat_mix(x: &Qua, y: &Qua, a: N) -> Qua { // x * (N::one() - a) + y * a //} /// Interpolate spherically between `x` and `y`. -pub fn slerp(x: &Qua, y: &Qua, a: N) -> Qua { +pub fn quat_slerp(x: &Qua, y: &Qua, a: N) -> Qua { Unit::new_normalize(*x).slerp(&Unit::new_normalize(*y), a).unwrap() } diff --git a/nalgebra-glm/src/quat/quaternion_geometric.rs b/nalgebra-glm/src/ext/quaternion_geometric.rs similarity index 53% rename from nalgebra-glm/src/quat/quaternion_geometric.rs rename to nalgebra-glm/src/ext/quaternion_geometric.rs index 952815a8..88838f8a 100644 --- a/nalgebra-glm/src/quat/quaternion_geometric.rs +++ b/nalgebra-glm/src/ext/quaternion_geometric.rs @@ -3,26 +3,26 @@ use na::Real; use aliases::Qua; /// Multiplies two quaternions. -pub fn cross(q1: &Qua, q2: &Qua) -> Qua { +pub fn quat_cross(q1: &Qua, q2: &Qua) -> Qua { q1 * q2 } /// The scalar product of two quaternions. -pub fn dot(x: &Qua, y: &Qua) -> N { +pub fn quat_dot(x: &Qua, y: &Qua) -> N { x.dot(y) } /// The magnitude of the quaternion `q`. -pub fn length(q: &Qua) -> N { +pub fn quat_length(q: &Qua) -> N { q.norm() } /// The magnitude of the quaternion `q`. -pub fn magnitude(q: &Qua) -> N { +pub fn quat_magnitude(q: &Qua) -> N { q.norm() } /// Normalizes the quaternion `q`. -pub fn normalize(q: &Qua) -> Qua { +pub fn quat_normalize(q: &Qua) -> Qua { q.normalize() } \ No newline at end of file diff --git a/nalgebra-glm/src/quat/quaternion_relational.rs b/nalgebra-glm/src/ext/quaternion_relational.rs similarity index 61% rename from nalgebra-glm/src/quat/quaternion_relational.rs rename to nalgebra-glm/src/ext/quaternion_relational.rs index cdf95dc5..46af82d9 100644 --- a/nalgebra-glm/src/quat/quaternion_relational.rs +++ b/nalgebra-glm/src/ext/quaternion_relational.rs @@ -4,21 +4,21 @@ use na::{Real, U4}; use aliases::{Qua, Vec}; /// Component-wise equality comparison between two quaternions. -pub fn equal(x: &Qua, y: &Qua) -> Vec { +pub fn quat_equal(x: &Qua, y: &Qua) -> Vec { ::equal(&x.coords, &y.coords) } /// Component-wise approximate equality comparison between two quaternions. -pub fn equal_eps(x: &Qua, y: &Qua, epsilon: N) -> Vec { +pub fn quat_equal_eps(x: &Qua, y: &Qua, epsilon: N) -> Vec { ::equal_eps(&x.coords, &y.coords, epsilon) } /// Component-wise non-equality comparison between two quaternions. -pub fn not_equal(x: &Qua, y: &Qua) -> Vec { +pub fn quat_not_equal(x: &Qua, y: &Qua) -> Vec { ::not_equal(&x.coords, &y.coords) } /// Component-wise approximate non-equality comparison between two quaternions. -pub fn not_equal_eps(x: &Qua, y: &Qua, epsilon: N) -> Vec { +pub fn quat_not_equal_eps(x: &Qua, y: &Qua, epsilon: N) -> Vec { ::not_equal_eps(&x.coords, &y.coords, epsilon) } diff --git a/nalgebra-glm/src/quat/quaternion_transform.rs b/nalgebra-glm/src/ext/quaternion_transform.rs similarity index 61% rename from nalgebra-glm/src/quat/quaternion_transform.rs rename to nalgebra-glm/src/ext/quaternion_transform.rs index a0b29172..06268fc9 100644 --- a/nalgebra-glm/src/quat/quaternion_transform.rs +++ b/nalgebra-glm/src/ext/quaternion_transform.rs @@ -3,25 +3,25 @@ use na::{Real, U3, UnitQuaternion, Unit}; use aliases::{Vec, Qua}; /// Computes the quaternion exponential. -pub fn exp(q: &Qua) -> Qua { +pub fn quat_exp(q: &Qua) -> Qua { q.exp() } /// Computes the quaternion logarithm. -pub fn log(q: &Qua) -> Qua { +pub fn quat_log(q: &Qua) -> Qua { q.ln() } /// Raises the quaternion `q` to the power `y`. -pub fn pow(q: &Qua, y: N) -> Qua { +pub fn quat_pow(q: &Qua, y: N) -> Qua { q.powf(y) } /// Builds a quaternion from an axis and an angle, and right-multiply it to the quaternion `q`. -pub fn rotate(q: &Qua, angle: N, axis: &Vec) -> Qua { +pub fn quat_rotate(q: &Qua, angle: N, axis: &Vec) -> Qua { q * UnitQuaternion::from_axis_angle(&Unit::new_normalize(*axis), angle).unwrap() } -//pub fn sqrt(q: &Qua) -> Qua { +//pub fn quat_sqrt(q: &Qua) -> Qua { // unimplemented!() //} \ No newline at end of file diff --git a/nalgebra-glm/src/quat/quaternion_trigonometric.rs b/nalgebra-glm/src/ext/quaternion_trigonometric.rs similarity index 75% rename from nalgebra-glm/src/quat/quaternion_trigonometric.rs rename to nalgebra-glm/src/ext/quaternion_trigonometric.rs index 20f39cde..13f7cf32 100644 --- a/nalgebra-glm/src/quat/quaternion_trigonometric.rs +++ b/nalgebra-glm/src/ext/quaternion_trigonometric.rs @@ -3,17 +3,17 @@ use na::{Real, U3, Unit, UnitQuaternion, Vector3}; use aliases::{Vec, Qua}; /// The rotation angle of this quaternion assumed to be normalized. -pub fn angle(x: &Qua) -> N { +pub fn quat_angle(x: &Qua) -> N { UnitQuaternion::from_quaternion(*x).angle() } /// Creates a quaternion from an axis and an angle. -pub fn angle_axis(angle: N, axis: &Vec) -> Qua { +pub fn quat_angle_axis(angle: N, axis: &Vec) -> Qua { UnitQuaternion::from_axis_angle(&Unit::new_normalize(*axis), angle).unwrap() } /// The rotation axis of a quaternion assumed to be normalized. -pub fn axis(x: &Qua) -> Vec { +pub fn quat_axis(x: &Qua) -> Vec { if let Some(a) = UnitQuaternion::from_quaternion(*x).axis() { a.unwrap() } else { diff --git a/nalgebra-glm/src/gtc/mod.rs b/nalgebra-glm/src/gtc/mod.rs index 83078278..3f0efd7c 100644 --- a/nalgebra-glm/src/gtc/mod.rs +++ b/nalgebra-glm/src/gtc/mod.rs @@ -11,6 +11,7 @@ pub use self::matrix_inverse::*; //pub use self::round::*; pub use self::type_ptr::*; //pub use self::ulp::*; +pub use self::quaternion::*; //mod bitfield; @@ -23,4 +24,5 @@ mod matrix_inverse; //mod reciprocal; //mod round; mod type_ptr; -//mod ulp; \ No newline at end of file +//mod ulp; +mod quaternion; \ No newline at end of file diff --git a/nalgebra-glm/src/quat/gtc_quaternion.rs b/nalgebra-glm/src/gtc/quaternion.rs similarity index 76% rename from nalgebra-glm/src/quat/gtc_quaternion.rs rename to nalgebra-glm/src/gtc/quaternion.rs index 1bdd8ecc..a17721de 100644 --- a/nalgebra-glm/src/quat/gtc_quaternion.rs +++ b/nalgebra-glm/src/gtc/quaternion.rs @@ -4,41 +4,41 @@ use aliases::{Qua, Vec, Mat}; /// Euler angles of the quaternion `q` as (pitch, yaw, roll). -pub fn euler_angles(x: &Qua) -> Vec { +pub fn quat_euler_angles(x: &Qua) -> Vec { let q = UnitQuaternion::new_unchecked(*x); let a = q.to_euler_angles(); Vector3::new(a.2, a.1, a.0) } /// Component-wise `>` comparison between two quaternions. -pub fn greater_than(x: &Qua, y: &Qua) -> Vec { +pub fn quat_greater_than(x: &Qua, y: &Qua) -> Vec { ::greater_than(&x.coords, &y.coords) } /// Component-wise `>=` comparison between two quaternions. -pub fn greater_than_equal(x: &Qua, y: &Qua) -> Vec { +pub fn quat_greater_than_equal(x: &Qua, y: &Qua) -> Vec { ::greater_than_equal(&x.coords, &y.coords) } /// Component-wise `<` comparison between two quaternions. -pub fn less_than(x: &Qua, y: &Qua) -> Vec { +pub fn quat_less_than(x: &Qua, y: &Qua) -> Vec { ::less_than(&x.coords, &y.coords) } /// Component-wise `<=` comparison between two quaternions. -pub fn less_than_equal(x: &Qua, y: &Qua) -> Vec { +pub fn quat_less_than_equal(x: &Qua, y: &Qua) -> Vec { ::less_than_equal(&x.coords, &y.coords) } /// Convert a quaternion to a rotation matrix. -pub fn mat3_cast(x: Qua) -> Mat { +pub fn quat_mat3_cast(x: Qua) -> Mat { let q = UnitQuaternion::new_unchecked(x); q.to_rotation_matrix().unwrap() } /// Convert a quaternion to a rotation matrix in homogeneous coordinates. -pub fn mat4_cast(x: Qua) -> Mat { +pub fn quat_mat4_cast(x: Qua) -> Mat { let q = UnitQuaternion::new_unchecked(x); q.to_homogeneous() } @@ -70,19 +70,19 @@ pub fn quat_look_at_rh(direction: &Vec, up: &Vec) -> Qua< } /// The "roll" euler angle of the quaternion `x` assumed to be normalized. -pub fn roll(x: &Qua) -> N { +pub fn quat_roll(x: &Qua) -> N { // FIXME: optimize this. - euler_angles(x).z + quat_euler_angles(x).z } /// The "yaw" euler angle of the quaternion `x` assumed to be normalized. -pub fn yaw(x: &Qua) -> N { +pub fn quat_yaw(x: &Qua) -> N { // FIXME: optimize this. - euler_angles(x).y + quat_euler_angles(x).y } /// The "pitch" euler angle of the quaternion `x` assumed to be normalized. -pub fn pitch(x: &Qua) -> N { +pub fn quat_pitch(x: &Qua) -> N { // FIXME: optimize this. - euler_angles(x).x + quat_euler_angles(x).x } diff --git a/nalgebra-glm/src/gtx/mod.rs b/nalgebra-glm/src/gtx/mod.rs index fa4294b9..2fe1f2ff 100644 --- a/nalgebra-glm/src/gtx/mod.rs +++ b/nalgebra-glm/src/gtx/mod.rs @@ -17,6 +17,7 @@ pub use self::transform2::*; pub use self::transform2d::*; pub use self::vector_angle::*; pub use self::vector_query::*; +pub use self::quaternion::*; @@ -35,4 +36,5 @@ mod transform; mod transform2; mod transform2d; mod vector_angle; -mod vector_query; \ No newline at end of file +mod vector_query; +mod quaternion; \ No newline at end of file diff --git a/nalgebra-glm/src/quat/gtx_quaternion.rs b/nalgebra-glm/src/gtx/quaternion.rs similarity index 67% rename from nalgebra-glm/src/quat/gtx_quaternion.rs rename to nalgebra-glm/src/gtx/quaternion.rs index 74a0fca6..e2ab97bf 100644 --- a/nalgebra-glm/src/quat/gtx_quaternion.rs +++ b/nalgebra-glm/src/gtx/quaternion.rs @@ -3,77 +3,77 @@ use na::{Real, Unit, Rotation3, Vector4, UnitQuaternion, U3, U4}; use aliases::{Qua, Vec, Mat}; /// Rotate the vector `v` by the quaternion `q` assumed to be normalized. -pub fn cross(q: &Qua, v: &Vec) -> Vec { +pub fn quat_cross(q: &Qua, v: &Vec) -> Vec { UnitQuaternion::new_unchecked(*q) * v } /// Rotate the vector `v` by the inverse of the quaternion `q` assumed to be normalized. -pub fn cross2(v: &Vec, q: &Qua) -> Vec { +pub fn quat_cross2(v: &Vec, q: &Qua) -> Vec { UnitQuaternion::new_unchecked(*q).inverse() * v } /// The quaternion `w` component. -pub fn extract_real_component(q: &Qua) -> N { +pub fn quat_extract_real_component(q: &Qua) -> N { q.w } /// Normalized linear interpolation between two quaternions. -pub fn fast_mix(x: &Qua, y: &Qua, a: N) -> Qua { +pub fn quat_fast_mix(x: &Qua, y: &Qua, a: N) -> Qua { Unit::new_unchecked(*x).nlerp(&Unit::new_unchecked(*y), a).unwrap() } -//pub fn intermediate(prev: &Qua, curr: &Qua, next: &Qua) -> Qua { +//pub fn quat_intermediate(prev: &Qua, curr: &Qua, next: &Qua) -> Qua { // unimplemented!() //} /// The squared magnitude of a quaternion `q`. -pub fn length2(q: &Qua) -> N { +pub fn quat_length2(q: &Qua) -> N { q.norm_squared() } /// The squared magnitude of a quaternion `q`. -pub fn magnitude2(q: &Qua) -> N { +pub fn quat_magnitude2(q: &Qua) -> N { q.norm_squared() } /// The quaternion representing the identity rotation. -pub fn quat_identity() -> Qua { +pub fn quat_quat_identity() -> Qua { UnitQuaternion::identity().unwrap() } /// Rotates a vector by a quaternion assumed to be normalized. -pub fn rotate(q: &Qua, v: &Vec) -> Vec { +pub fn quat_rotate(q: &Qua, v: &Vec) -> Vec { UnitQuaternion::new_unchecked(*q) * v } /// Rotates a vector in homogeneous coordinates by a quaternion assumed to be normalized. -pub fn rotate2(q: &Qua, v: &Vec) -> Vec { +pub fn quat_rotate2(q: &Qua, v: &Vec) -> Vec { // UnitQuaternion::new_unchecked(*q) * v let rotated = Unit::new_unchecked(*q) * v.fixed_rows::(0); Vector4::new(rotated.x, rotated.y, rotated.z, v.w) } /// The rotation required to align `orig` to `dest`. -pub fn rotation(orig: &Vec, dest: &Vec) -> Qua { +pub fn quat_rotation(orig: &Vec, dest: &Vec) -> Qua { UnitQuaternion::rotation_between(orig, dest).unwrap_or(UnitQuaternion::identity()).unwrap() } /// The spherical linear interpolation between two quaternions. -pub fn short_mix(x: &Qua, y: &Qua, a: N) -> Qua { +pub fn quat_short_mix(x: &Qua, y: &Qua, a: N) -> Qua { Unit::new_normalize(*x).slerp(&Unit::new_normalize(*y), a).unwrap() } -//pub fn squad(q1: &Qua, q2: &Qua, s1: &Qua, s2: &Qua, h: N) -> Qua { +//pub fn quat_squad(q1: &Qua, q2: &Qua, s1: &Qua, s2: &Qua, h: N) -> Qua { // unimplemented!() //} /// Converts a quaternion to a rotation matrix. -pub fn to_mat3(x: &Qua) -> Mat { +pub fn quat_to_mat3(x: &Qua) -> Mat { UnitQuaternion::new_unchecked(*x).to_rotation_matrix().unwrap() } /// Converts a quaternion to a rotation matrix in homogenous coordinates. -pub fn to_mat4(x: &Qua) -> Mat { +pub fn quat_to_mat4(x: &Qua) -> Mat { UnitQuaternion::new_unchecked(*x).to_homogeneous() } diff --git a/nalgebra-glm/src/gtx/rotate_normalized_axis.rs b/nalgebra-glm/src/gtx/rotate_normalized_axis.rs index e465da8d..8346c801 100644 --- a/nalgebra-glm/src/gtx/rotate_normalized_axis.rs +++ b/nalgebra-glm/src/gtx/rotate_normalized_axis.rs @@ -1,6 +1,6 @@ -use na::{Real, Rotation3, Unit, U3, U4}; +use na::{Real, Rotation3, Unit, UnitQuaternion, U3, U4}; -use aliases::{Vec, Mat}; +use aliases::{Vec, Mat, Qua}; /// Builds a rotation 4 * 4 matrix created from a normalized axis and an angle. /// @@ -10,4 +10,14 @@ use aliases::{Vec, Mat}; /// * `axis` - Rotation axis, must be normalized. pub fn rotate_normalized_axis(m: &Mat, angle: N, axis: &Vec) -> Mat { m * Rotation3::from_axis_angle(&Unit::new_unchecked(*axis), angle).to_homogeneous() +} + +/// Rotates a quaternion from a vector of 3 components normalized axis and an angle. +/// +/// # Parameters +/// * `q` - Source orientation +/// * `angle` - Angle expressed in radians. +/// * `axis` - Normalized axis of the rotation, must be normalized. +pub fn quat_rotate_normalized_axis(q: &Qua, angle: N, axis: &Vec) -> Qua { + q * UnitQuaternion::from_axis_angle(&Unit::new_unchecked(*axis), angle).unwrap() } \ No newline at end of file diff --git a/nalgebra-glm/src/lib.rs b/nalgebra-glm/src/lib.rs index e56dd6bc..46d06870 100644 --- a/nalgebra-glm/src/lib.rs +++ b/nalgebra-glm/src/lib.rs @@ -37,5 +37,4 @@ mod exponential; pub mod ext; pub mod gtc; -pub mod gtx; -pub mod quat; \ No newline at end of file +pub mod gtx; \ No newline at end of file diff --git a/nalgebra-glm/src/quat/gtx_rotate_normalized_axis.rs b/nalgebra-glm/src/quat/gtx_rotate_normalized_axis.rs deleted file mode 100644 index 3cb9f515..00000000 --- a/nalgebra-glm/src/quat/gtx_rotate_normalized_axis.rs +++ /dev/null @@ -1,13 +0,0 @@ -use na::{Real, Unit, UnitQuaternion, U3}; - -use aliases::{Vec, Qua}; - -/// Rotates a quaternion from a vector of 3 components normalized axis and an angle. -/// -/// # Parameters -/// * `q` - Source orientation -/// * `angle` - Angle expressed in radians. -/// * `axis` - Normalized axis of the rotation, must be normalized. -pub fn rotate_normalized_axis(q: &Qua, angle: N, axis: &Vec) -> Qua { - q * UnitQuaternion::from_axis_angle(&Unit::new_unchecked(*axis), angle).unwrap() -} \ No newline at end of file diff --git a/nalgebra-glm/src/quat/mod.rs b/nalgebra-glm/src/quat/mod.rs deleted file mode 100644 index 05326932..00000000 --- a/nalgebra-glm/src/quat/mod.rs +++ /dev/null @@ -1,19 +0,0 @@ -//! Definition and operations on quaternions. - -pub use self::gtc_quaternion::*; -pub use self::gtx_quaternion::*; -pub use self::gtx_rotate_normalized_axis::*; -pub use self::quaternion_common::*; -pub use self::quaternion_geometric::*; -pub use self::quaternion_relational::*; -pub use self::quaternion_transform::*; -pub use self::quaternion_trigonometric::*; - -mod gtc_quaternion; -mod gtx_quaternion; -mod gtx_rotate_normalized_axis; -mod quaternion_common; -mod quaternion_geometric; -mod quaternion_relational; -mod quaternion_transform; -mod quaternion_trigonometric; \ No newline at end of file