From 3b3dbff331f51f25527cf2bcd307483dc62db660 Mon Sep 17 00:00:00 2001 From: Cameron Hart Date: Tue, 25 Jun 2024 23:39:50 +1200 Subject: [PATCH] Support extracting euler angles from matrix types. --- codegen/templates/mat.rs.tera | 7 +++++++ src/euler.rs | 16 ++++++++++++++++ src/f32/coresimd/mat3a.rs | 7 +++++++ src/f32/mat3.rs | 7 +++++++ src/f32/neon/mat3a.rs | 7 +++++++ src/f32/scalar/mat3a.rs | 7 +++++++ src/f32/sse2/mat3a.rs | 7 +++++++ src/f32/wasm32/mat3a.rs | 7 +++++++ src/f64/dmat3.rs | 7 +++++++ 9 files changed, 72 insertions(+) diff --git a/codegen/templates/mat.rs.tera b/codegen/templates/mat.rs.tera index c53954ec..98edbb40 100644 --- a/codegen/templates/mat.rs.tera +++ b/codegen/templates/mat.rs.tera @@ -524,6 +524,13 @@ impl {{ self_t }} { Self::from_euler_angles(order, a, b, c) } + /// Extract Euler angles with the given Euler rotation order. + #[inline] + #[must_use] + pub fn to_euler(&self, order: EulerRot) -> ({{ scalar_t }}, {{ scalar_t }}, {{ scalar_t }}) { + self.to_euler_angles(order) + } + /// Creates a 3D rotation matrix from `angle` (in radians) around the x axis. #[inline] #[must_use] diff --git a/src/euler.rs b/src/euler.rs index 602e781e..e07352a1 100644 --- a/src/euler.rs +++ b/src/euler.rs @@ -379,6 +379,20 @@ macro_rules! impl_mat3_to_euler { }; } +macro_rules! impl_mat4_to_euler { + ($scalar:ident, $mat4:ident, $mat3:ident) => { + impl ToEuler for $mat4 { + type Scalar = $scalar; + fn to_euler_angles( + self, + order: EulerRot, + ) -> (Self::Scalar, Self::Scalar, Self::Scalar) { + $mat3::from_mat4(self).to_euler_angles(order) + } + } + }; +} + macro_rules! impl_quat_to_euler { ($scalar:ident, $quat:ident, $mat3:ident) => { impl ToEuler for $quat { @@ -398,11 +412,13 @@ impl_euler_to_mat3!(f32, Mat3, Vec3); impl_mat3_to_euler!(f32, Mat3A, Vec3A); impl_euler_to_mat3!(f32, Mat3A, Vec3A); impl_euler_to_mat4!(f32, Mat4, Mat3); +impl_mat4_to_euler!(f32, Mat4, Mat3); impl_quat_to_euler!(f32, Quat, Mat3); impl_euler_to_quat!(f32, Quat, Vec3); impl_mat3_to_euler!(f64, DMat3, DVec3); impl_euler_to_mat3!(f64, DMat3, DVec3); +impl_mat4_to_euler!(f64, DMat4, DMat3); impl_euler_to_mat4!(f64, DMat4, DMat3); impl_quat_to_euler!(f64, DQuat, DMat3); impl_euler_to_quat!(f64, DQuat, DVec3); diff --git a/src/f32/coresimd/mat3a.rs b/src/f32/coresimd/mat3a.rs index a5b95349..55226138 100644 --- a/src/f32/coresimd/mat3a.rs +++ b/src/f32/coresimd/mat3a.rs @@ -226,6 +226,13 @@ impl Mat3A { Self::from_euler_angles(order, a, b, c) } + /// Extract Euler angles with the given Euler rotation order. + #[inline] + #[must_use] + pub fn to_euler(&self, order: EulerRot) -> (f32, f32, f32) { + self.to_euler_angles(order) + } + /// Creates a 3D rotation matrix from `angle` (in radians) around the x axis. #[inline] #[must_use] diff --git a/src/f32/mat3.rs b/src/f32/mat3.rs index eb1f0fcb..3251fcd0 100644 --- a/src/f32/mat3.rs +++ b/src/f32/mat3.rs @@ -227,6 +227,13 @@ impl Mat3 { Self::from_euler_angles(order, a, b, c) } + /// Extract Euler angles with the given Euler rotation order. + #[inline] + #[must_use] + pub fn to_euler(&self, order: EulerRot) -> (f32, f32, f32) { + self.to_euler_angles(order) + } + /// Creates a 3D rotation matrix from `angle` (in radians) around the x axis. #[inline] #[must_use] diff --git a/src/f32/neon/mat3a.rs b/src/f32/neon/mat3a.rs index 7711f23f..cad53260 100644 --- a/src/f32/neon/mat3a.rs +++ b/src/f32/neon/mat3a.rs @@ -226,6 +226,13 @@ impl Mat3A { Self::from_euler_angles(order, a, b, c) } + /// Extract Euler angles with the given Euler rotation order. + #[inline] + #[must_use] + pub fn to_euler(&self, order: EulerRot) -> (f32, f32, f32) { + self.to_euler_angles(order) + } + /// Creates a 3D rotation matrix from `angle` (in radians) around the x axis. #[inline] #[must_use] diff --git a/src/f32/scalar/mat3a.rs b/src/f32/scalar/mat3a.rs index ecd2da57..4dbb0914 100644 --- a/src/f32/scalar/mat3a.rs +++ b/src/f32/scalar/mat3a.rs @@ -227,6 +227,13 @@ impl Mat3A { Self::from_euler_angles(order, a, b, c) } + /// Extract Euler angles with the given Euler rotation order. + #[inline] + #[must_use] + pub fn to_euler(&self, order: EulerRot) -> (f32, f32, f32) { + self.to_euler_angles(order) + } + /// Creates a 3D rotation matrix from `angle` (in radians) around the x axis. #[inline] #[must_use] diff --git a/src/f32/sse2/mat3a.rs b/src/f32/sse2/mat3a.rs index 0ba17c6a..0cbef3e4 100644 --- a/src/f32/sse2/mat3a.rs +++ b/src/f32/sse2/mat3a.rs @@ -229,6 +229,13 @@ impl Mat3A { Self::from_euler_angles(order, a, b, c) } + /// Extract Euler angles with the given Euler rotation order. + #[inline] + #[must_use] + pub fn to_euler(&self, order: EulerRot) -> (f32, f32, f32) { + self.to_euler_angles(order) + } + /// Creates a 3D rotation matrix from `angle` (in radians) around the x axis. #[inline] #[must_use] diff --git a/src/f32/wasm32/mat3a.rs b/src/f32/wasm32/mat3a.rs index abb65dfe..2ec483e6 100644 --- a/src/f32/wasm32/mat3a.rs +++ b/src/f32/wasm32/mat3a.rs @@ -226,6 +226,13 @@ impl Mat3A { Self::from_euler_angles(order, a, b, c) } + /// Extract Euler angles with the given Euler rotation order. + #[inline] + #[must_use] + pub fn to_euler(&self, order: EulerRot) -> (f32, f32, f32) { + self.to_euler_angles(order) + } + /// Creates a 3D rotation matrix from `angle` (in radians) around the x axis. #[inline] #[must_use] diff --git a/src/f64/dmat3.rs b/src/f64/dmat3.rs index 12bd9975..ff8bcba2 100644 --- a/src/f64/dmat3.rs +++ b/src/f64/dmat3.rs @@ -226,6 +226,13 @@ impl DMat3 { Self::from_euler_angles(order, a, b, c) } + /// Extract Euler angles with the given Euler rotation order. + #[inline] + #[must_use] + pub fn to_euler(&self, order: EulerRot) -> (f64, f64, f64) { + self.to_euler_angles(order) + } + /// Creates a 3D rotation matrix from `angle` (in radians) around the x axis. #[inline] #[must_use]