From e446a0a0360c408f183c08e7680d439157f71b6a Mon Sep 17 00:00:00 2001 From: t-yrka <78016060+t-yrka@users.noreply.github.com> Date: Tue, 17 Dec 2024 01:39:49 +0100 Subject: [PATCH] Return the target Quat when the angle is small (#591) Returning self is misleading as it skips the rotation completely --- codegen/templates/quat.rs.tera | 2 +- src/f32/coresimd/quat.rs | 2 +- src/f32/neon/quat.rs | 2 +- src/f32/scalar/quat.rs | 2 +- src/f32/sse2/quat.rs | 2 +- src/f32/wasm32/quat.rs | 2 +- src/f64/dquat.rs | 2 +- tests/quat.rs | 5 +++++ 8 files changed, 12 insertions(+), 7 deletions(-) diff --git a/codegen/templates/quat.rs.tera b/codegen/templates/quat.rs.tera index 017096d2..7f9ff9b3 100644 --- a/codegen/templates/quat.rs.tera +++ b/codegen/templates/quat.rs.tera @@ -729,7 +729,7 @@ impl {{ self_t }} { glam_assert!(self.is_normalized() && rhs.is_normalized()); let angle = self.angle_between(rhs); if angle <= 1e-4 { - return *self; + return rhs; } let s = (max_angle / angle).clamp(-1.0, 1.0); self.slerp(rhs, s) diff --git a/src/f32/coresimd/quat.rs b/src/f32/coresimd/quat.rs index 7bcb0b54..f0479a24 100644 --- a/src/f32/coresimd/quat.rs +++ b/src/f32/coresimd/quat.rs @@ -585,7 +585,7 @@ impl Quat { glam_assert!(self.is_normalized() && rhs.is_normalized()); let angle = self.angle_between(rhs); if angle <= 1e-4 { - return *self; + return rhs; } let s = (max_angle / angle).clamp(-1.0, 1.0); self.slerp(rhs, s) diff --git a/src/f32/neon/quat.rs b/src/f32/neon/quat.rs index 76664ad5..8390f2e6 100644 --- a/src/f32/neon/quat.rs +++ b/src/f32/neon/quat.rs @@ -590,7 +590,7 @@ impl Quat { glam_assert!(self.is_normalized() && rhs.is_normalized()); let angle = self.angle_between(rhs); if angle <= 1e-4 { - return *self; + return rhs; } let s = (max_angle / angle).clamp(-1.0, 1.0); self.slerp(rhs, s) diff --git a/src/f32/scalar/quat.rs b/src/f32/scalar/quat.rs index 2cbeb4c6..a55c0ece 100644 --- a/src/f32/scalar/quat.rs +++ b/src/f32/scalar/quat.rs @@ -597,7 +597,7 @@ impl Quat { glam_assert!(self.is_normalized() && rhs.is_normalized()); let angle = self.angle_between(rhs); if angle <= 1e-4 { - return *self; + return rhs; } let s = (max_angle / angle).clamp(-1.0, 1.0); self.slerp(rhs, s) diff --git a/src/f32/sse2/quat.rs b/src/f32/sse2/quat.rs index 634f7803..cc477ba7 100644 --- a/src/f32/sse2/quat.rs +++ b/src/f32/sse2/quat.rs @@ -593,7 +593,7 @@ impl Quat { glam_assert!(self.is_normalized() && rhs.is_normalized()); let angle = self.angle_between(rhs); if angle <= 1e-4 { - return *self; + return rhs; } let s = (max_angle / angle).clamp(-1.0, 1.0); self.slerp(rhs, s) diff --git a/src/f32/wasm32/quat.rs b/src/f32/wasm32/quat.rs index 8d0e19f2..b7307ba0 100644 --- a/src/f32/wasm32/quat.rs +++ b/src/f32/wasm32/quat.rs @@ -585,7 +585,7 @@ impl Quat { glam_assert!(self.is_normalized() && rhs.is_normalized()); let angle = self.angle_between(rhs); if angle <= 1e-4 { - return *self; + return rhs; } let s = (max_angle / angle).clamp(-1.0, 1.0); self.slerp(rhs, s) diff --git a/src/f64/dquat.rs b/src/f64/dquat.rs index 52b67e5d..b6c2d84b 100644 --- a/src/f64/dquat.rs +++ b/src/f64/dquat.rs @@ -579,7 +579,7 @@ impl DQuat { glam_assert!(self.is_normalized() && rhs.is_normalized()); let angle = self.angle_between(rhs); if angle <= 1e-4 { - return *self; + return rhs; } let s = (max_angle / angle).clamp(-1.0, 1.0); self.slerp(rhs, s) diff --git a/tests/quat.rs b/tests/quat.rs index 3c8a090a..eebc1279 100644 --- a/tests/quat.rs +++ b/tests/quat.rs @@ -356,6 +356,11 @@ macro_rules! impl_quat_tests { ); assert_approx_eq!(q2, q0.rotate_towards(q1, -FRAC_PI_2), eps); assert_approx_eq!(q2, q0.rotate_towards(q1, -FRAC_PI_2 * 1.5), eps); + + // Small angles + let q0 = $quat::from_euler(EulerRot::YXZ, 0.0, 0.0, 0.0); + let q1 = $quat::from_euler(EulerRot::YXZ, 1e-4, 0.0, 0.0); + assert_eq!(q1, q0.rotate_towards(q1, FRAC_PI_2)) }); glam_test!(test_fmt, {