Skip to content

Commit

Permalink
A bit closer to working? Not for Quat though.
Browse files Browse the repository at this point in the history
  • Loading branch information
bitshifter committed Jun 25, 2024
1 parent 3b3dbff commit 31769e7
Show file tree
Hide file tree
Showing 15 changed files with 138 additions and 50 deletions.
11 changes: 9 additions & 2 deletions codegen/templates/mat.rs.tera
Original file line number Diff line number Diff line change
Expand Up @@ -72,15 +72,15 @@ use crate::{
Mat3A,
{% endif %}
{% elif dim == 3 %}
EulerRot, euler::FromEuler,
EulerRot, euler::{FromEuler, ToEuler},
{{ mat2_t }}, {{ mat4_t }}, {{ quat_t }}, {{ vec2_t }}, {{ col_t }},
{% if is_align %}
{{ mat3_t }}, {{ vec3_t }},
{% elif scalar_t == "f32" %}
Mat3A, Vec3A,
{% endif %}
{% elif dim == 4 %}
EulerRot, euler::FromEuler,
EulerRot, euler::{FromEuler, ToEuler},
{{ mat3_t }}, {{ quat_t }}, {{ vec3_t }}, {{ col_t }},
{% if scalar_t == "f32" %}
Mat3A, Vec3A,
Expand Down Expand Up @@ -862,6 +862,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 an affine transformation matrix containing a 3D rotation around the x axis of
/// `angle` (in radians).
///
Expand Down
54 changes: 29 additions & 25 deletions src/euler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -301,25 +301,31 @@ macro_rules! impl_mat3_to_euler {
use crate::$scalar::math;

// TODO: is this just rotation from the inital axis?
let mat3_from_rotation = |r: $vec3| -> $mat3 {
// TODO: switch to glam math wrapper
let (sin_rz, cos_rz) = math::sin_cos(r.z);
let (sin_ry, cos_ry) = math::sin_cos(r.y);
let (sin_rx, cos_rx) = math::sin_cos(r.x);

$mat3::from_cols(
$vec3::new(cos_rz * cos_ry, sin_rz * cos_ry, -sin_ry),
$vec3::new(
-sin_rz * cos_rx + cos_rz * sin_ry * sin_rx,
cos_rz * cos_rx + sin_rz * sin_ry * sin_rx,
cos_ry * sin_rx,
),
$vec3::new(
-sin_rz * -sin_rx + cos_rz * sin_ry * cos_rx,
cos_rz * -sin_rx + sin_rz * sin_ry * cos_rx,
cos_ry * cos_rx,
),
)
let mat3_from_rotation = |i: usize, r: $scalar| -> $mat3 {
match i {
0 => $mat3::from_rotation_x(r),
1 => $mat3::from_rotation_y(r),
2 => $mat3::from_rotation_z(r),
_ => unreachable!()
}
// // TODO: switch to glam math wrapper
// let (sin_rz, cos_rz) = math::sin_cos(r.z);
// let (sin_ry, cos_ry) = math::sin_cos(r.y);
// let (sin_rx, cos_rx) = math::sin_cos(r.x);

// $mat3::from_cols(
// $vec3::new(cos_rz * cos_ry, sin_rz * cos_ry, -sin_ry),
// $vec3::new(
// -sin_rz * cos_rx + cos_rz * sin_ry * sin_rx,
// cos_rz * cos_rx + sin_rz * sin_ry * sin_rx,
// cos_ry * sin_rx,
// ),
// $vec3::new(
// -sin_rz * -sin_rx + cos_rz * sin_ry * cos_rx,
// cos_rz * -sin_rx + sin_rz * sin_ry * cos_rx,
// cos_ry * cos_rx,
// ),
// )
};

let order = Order::from_euler(euler);
Expand All @@ -332,10 +338,9 @@ macro_rules! impl_mat3_to_euler {
// Remove the x rotation from M, so that the remaining
// rotation, N, is only around two axes, and gimbal lock
// cannot occur.
let mut r = $vec3::ZERO;
r[i] = if order.parity_even { -x } else { x };
let r = if order.parity_even { -x } else { x };

let n = mat3_from_rotation(r) * self;
let n = self * mat3_from_rotation(i, r);

// Extract the other two angles, y and z, from N.
let sy = math::sqrt(n.col(j)[i] * n.col(j)[i] + n.col(k)[i] * n.col(k)[i]);
Expand All @@ -349,10 +354,9 @@ macro_rules! impl_mat3_to_euler {
// Remove the x rotation from M, so that the remaining
// rotation, N, is only around two axes, and gimbal lock
// cannot occur.
let mut r = $vec3::ZERO;
r[i] = if order.parity_even { -x } else { x };
let r = if order.parity_even { -x } else { x };

let n = mat3_from_rotation(r) * self;
let n = self * mat3_from_rotation(i, r);

// Extract the other two angles, y and z, from N.
let cy = math::sqrt(n.col(i)[i] * n.col(i)[i] + n.col(i)[j] * n.col(i)[j]);
Expand Down
6 changes: 4 additions & 2 deletions src/f32/coresimd/mat3a.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
// Generated from mat.rs.tera template. Edit the template, not the generated file.

use crate::{
euler::FromEuler, f32::math, swizzles::*, DMat3, EulerRot, Mat2, Mat3, Mat4, Quat, Vec2, Vec3,
Vec3A,
euler::{FromEuler, ToEuler},
f32::math,
swizzles::*,
DMat3, EulerRot, Mat2, Mat3, Mat4, Quat, Vec2, Vec3, Vec3A,
};
#[cfg(not(target_arch = "spirv"))]
use core::fmt;
Expand Down
14 changes: 12 additions & 2 deletions src/f32/coresimd/mat4.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
// Generated from mat.rs.tera template. Edit the template, not the generated file.

use crate::{
coresimd::*, euler::FromEuler, f32::math, swizzles::*, DMat4, EulerRot, Mat3, Mat3A, Quat,
Vec3, Vec3A, Vec4,
coresimd::*,
euler::{FromEuler, ToEuler},
f32::math,
swizzles::*,
DMat4, EulerRot, Mat3, Mat3A, Quat, Vec3, Vec3A, Vec4,
};
#[cfg(not(target_arch = "spirv"))]
use core::fmt;
Expand Down Expand Up @@ -386,6 +389,13 @@ impl Mat4 {
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 an affine transformation matrix containing a 3D rotation around the x axis of
/// `angle` (in radians).
///
Expand Down
6 changes: 4 additions & 2 deletions src/f32/mat3.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
// Generated from mat.rs.tera template. Edit the template, not the generated file.

use crate::{
euler::FromEuler, f32::math, swizzles::*, DMat3, EulerRot, Mat2, Mat3A, Mat4, Quat, Vec2, Vec3,
Vec3A,
euler::{FromEuler, ToEuler},
f32::math,
swizzles::*,
DMat3, EulerRot, Mat2, Mat3A, Mat4, Quat, Vec2, Vec3, Vec3A,
};
#[cfg(not(target_arch = "spirv"))]
use core::fmt;
Expand Down
6 changes: 4 additions & 2 deletions src/f32/neon/mat3a.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
// Generated from mat.rs.tera template. Edit the template, not the generated file.

use crate::{
euler::FromEuler, f32::math, swizzles::*, DMat3, EulerRot, Mat2, Mat3, Mat4, Quat, Vec2, Vec3,
Vec3A,
euler::{FromEuler, ToEuler},
f32::math,
swizzles::*,
DMat3, EulerRot, Mat2, Mat3, Mat4, Quat, Vec2, Vec3, Vec3A,
};
#[cfg(not(target_arch = "spirv"))]
use core::fmt;
Expand Down
14 changes: 12 additions & 2 deletions src/f32/neon/mat4.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
// Generated from mat.rs.tera template. Edit the template, not the generated file.

use crate::{
euler::FromEuler, f32::math, neon::*, swizzles::*, DMat4, EulerRot, Mat3, Mat3A, Quat, Vec3,
Vec3A, Vec4,
euler::{FromEuler, ToEuler},
f32::math,
neon::*,
swizzles::*,
DMat4, EulerRot, Mat3, Mat3A, Quat, Vec3, Vec3A, Vec4,
};
#[cfg(not(target_arch = "spirv"))]
use core::fmt;
Expand Down Expand Up @@ -386,6 +389,13 @@ impl Mat4 {
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 an affine transformation matrix containing a 3D rotation around the x axis of
/// `angle` (in radians).
///
Expand Down
6 changes: 4 additions & 2 deletions src/f32/scalar/mat3a.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
// Generated from mat.rs.tera template. Edit the template, not the generated file.

use crate::{
euler::FromEuler, f32::math, swizzles::*, DMat3, EulerRot, Mat2, Mat3, Mat4, Quat, Vec2, Vec3,
Vec3A,
euler::{FromEuler, ToEuler},
f32::math,
swizzles::*,
DMat3, EulerRot, Mat2, Mat3, Mat4, Quat, Vec2, Vec3, Vec3A,
};
#[cfg(not(target_arch = "spirv"))]
use core::fmt;
Expand Down
12 changes: 11 additions & 1 deletion src/f32/scalar/mat4.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
// Generated from mat.rs.tera template. Edit the template, not the generated file.

use crate::{
euler::FromEuler, f32::math, swizzles::*, DMat4, EulerRot, Mat3, Mat3A, Quat, Vec3, Vec3A, Vec4,
euler::{FromEuler, ToEuler},
f32::math,
swizzles::*,
DMat4, EulerRot, Mat3, Mat3A, Quat, Vec3, Vec3A, Vec4,
};
#[cfg(not(target_arch = "spirv"))]
use core::fmt;
Expand Down Expand Up @@ -398,6 +401,13 @@ impl Mat4 {
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 an affine transformation matrix containing a 3D rotation around the x axis of
/// `angle` (in radians).
///
Expand Down
6 changes: 4 additions & 2 deletions src/f32/sse2/mat3a.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
// Generated from mat.rs.tera template. Edit the template, not the generated file.

use crate::{
euler::FromEuler, f32::math, swizzles::*, DMat3, EulerRot, Mat2, Mat3, Mat4, Quat, Vec2, Vec3,
Vec3A,
euler::{FromEuler, ToEuler},
f32::math,
swizzles::*,
DMat3, EulerRot, Mat2, Mat3, Mat4, Quat, Vec2, Vec3, Vec3A,
};
#[cfg(not(target_arch = "spirv"))]
use core::fmt;
Expand Down
14 changes: 12 additions & 2 deletions src/f32/sse2/mat4.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
// Generated from mat.rs.tera template. Edit the template, not the generated file.

use crate::{
euler::FromEuler, f32::math, sse2::*, swizzles::*, DMat4, EulerRot, Mat3, Mat3A, Quat, Vec3,
Vec3A, Vec4,
euler::{FromEuler, ToEuler},
f32::math,
sse2::*,
swizzles::*,
DMat4, EulerRot, Mat3, Mat3A, Quat, Vec3, Vec3A, Vec4,
};
#[cfg(not(target_arch = "spirv"))]
use core::fmt;
Expand Down Expand Up @@ -389,6 +392,13 @@ impl Mat4 {
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 an affine transformation matrix containing a 3D rotation around the x axis of
/// `angle` (in radians).
///
Expand Down
6 changes: 4 additions & 2 deletions src/f32/wasm32/mat3a.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
// Generated from mat.rs.tera template. Edit the template, not the generated file.

use crate::{
euler::FromEuler, f32::math, swizzles::*, DMat3, EulerRot, Mat2, Mat3, Mat4, Quat, Vec2, Vec3,
Vec3A,
euler::{FromEuler, ToEuler},
f32::math,
swizzles::*,
DMat3, EulerRot, Mat2, Mat3, Mat4, Quat, Vec2, Vec3, Vec3A,
};
#[cfg(not(target_arch = "spirv"))]
use core::fmt;
Expand Down
14 changes: 12 additions & 2 deletions src/f32/wasm32/mat4.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
// Generated from mat.rs.tera template. Edit the template, not the generated file.

use crate::{
euler::FromEuler, f32::math, swizzles::*, wasm32::*, DMat4, EulerRot, Mat3, Mat3A, Quat, Vec3,
Vec3A, Vec4,
euler::{FromEuler, ToEuler},
f32::math,
swizzles::*,
wasm32::*,
DMat4, EulerRot, Mat3, Mat3A, Quat, Vec3, Vec3A, Vec4,
};
#[cfg(not(target_arch = "spirv"))]
use core::fmt;
Expand Down Expand Up @@ -386,6 +389,13 @@ impl Mat4 {
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 an affine transformation matrix containing a 3D rotation around the x axis of
/// `angle` (in radians).
///
Expand Down
5 changes: 4 additions & 1 deletion src/f64/dmat3.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
// Generated from mat.rs.tera template. Edit the template, not the generated file.

use crate::{
euler::FromEuler, f64::math, swizzles::*, DMat2, DMat4, DQuat, DVec2, DVec3, EulerRot, Mat3,
euler::{FromEuler, ToEuler},
f64::math,
swizzles::*,
DMat2, DMat4, DQuat, DVec2, DVec3, EulerRot, Mat3,
};
#[cfg(not(target_arch = "spirv"))]
use core::fmt;
Expand Down
14 changes: 13 additions & 1 deletion src/f64/dmat4.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
// Generated from mat.rs.tera template. Edit the template, not the generated file.

use crate::{euler::FromEuler, f64::math, swizzles::*, DMat3, DQuat, DVec3, DVec4, EulerRot, Mat4};
use crate::{
euler::{FromEuler, ToEuler},
f64::math,
swizzles::*,
DMat3, DQuat, DVec3, DVec4, EulerRot, Mat4,
};
#[cfg(not(target_arch = "spirv"))]
use core::fmt;
use core::iter::{Product, Sum};
Expand Down Expand Up @@ -378,6 +383,13 @@ impl DMat4 {
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 an affine transformation matrix containing a 3D rotation around the x axis of
/// `angle` (in radians).
///
Expand Down

0 comments on commit 31769e7

Please sign in to comment.