[Bf-blender-cvs] [756538b4a11] master: BLI_math: remove normalize from mat3_normalized_to_quat_fast
Campbell Barton
noreply at git.blender.org
Wed Nov 9 03:29:19 CET 2022
Commit: 756538b4a117cb51a15e848fa6170143b6aafcd8
Author: Campbell Barton
Date: Wed Nov 9 13:17:03 2022 +1100
Branches: master
https://developer.blender.org/rB756538b4a117cb51a15e848fa6170143b6aafcd8
BLI_math: remove normalize from mat3_normalized_to_quat_fast
The quaternion calculated are unit length unless the the input matrix is
degenerate. Detect degenerate cases and remove the normalize_qt call.
===================================================================
M source/blender/blenlib/intern/math_rotation.c
===================================================================
diff --git a/source/blender/blenlib/intern/math_rotation.c b/source/blender/blenlib/intern/math_rotation.c
index 17e43b545d8..180412c4a14 100644
--- a/source/blender/blenlib/intern/math_rotation.c
+++ b/source/blender/blenlib/intern/math_rotation.c
@@ -292,6 +292,10 @@ void mat3_normalized_to_quat_fast(float q[4], const float mat[3][3])
q[0] = (mat[1][2] - mat[2][1]) * s;
q[2] = (mat[0][1] + mat[1][0]) * s;
q[3] = (mat[2][0] + mat[0][2]) * s;
+ if (UNLIKELY((trace == 1.0f) && (q[0] == 0.0f && q[2] == 0.0f && q[3] == 0.0f))) {
+ /* Avoids the need to normalize the degenerate case. */
+ q[1] = 1.0f;
+ }
}
else {
const float trace = 1.0f - mat[0][0] + mat[1][1] - mat[2][2];
@@ -305,6 +309,10 @@ void mat3_normalized_to_quat_fast(float q[4], const float mat[3][3])
q[0] = (mat[2][0] - mat[0][2]) * s;
q[1] = (mat[0][1] + mat[1][0]) * s;
q[3] = (mat[1][2] + mat[2][1]) * s;
+ if (UNLIKELY((trace == 1.0f) && (q[0] == 0.0f && q[1] == 0.0f && q[3] == 0.0f))) {
+ /* Avoids the need to normalize the degenerate case. */
+ q[2] = 1.0f;
+ }
}
}
else {
@@ -320,6 +328,10 @@ void mat3_normalized_to_quat_fast(float q[4], const float mat[3][3])
q[0] = (mat[0][1] - mat[1][0]) * s;
q[1] = (mat[2][0] + mat[0][2]) * s;
q[2] = (mat[1][2] + mat[2][1]) * s;
+ if (UNLIKELY((trace == 1.0f) && (q[0] == 0.0f && q[1] == 0.0f && q[2] == 0.0f))) {
+ /* Avoids the need to normalize the degenerate case. */
+ q[3] = 1.0f;
+ }
}
else {
/* NOTE(@campbellbarton): A zero matrix will fall through to this block,
@@ -331,11 +343,15 @@ void mat3_normalized_to_quat_fast(float q[4], const float mat[3][3])
q[1] = (mat[1][2] - mat[2][1]) * s;
q[2] = (mat[2][0] - mat[0][2]) * s;
q[3] = (mat[0][1] - mat[1][0]) * s;
+ if (UNLIKELY((trace == 1.0f) && (q[1] == 0.0f && q[2] == 0.0f && q[3] == 0.0f))) {
+ /* Avoids the need to normalize the degenerate case. */
+ q[0] = 1.0f;
+ }
}
}
BLI_assert(!(q[0] < 0.0f));
- normalize_qt(q);
+ BLI_ASSERT_UNIT_QUAT(q);
}
static void mat3_normalized_to_quat_with_checks(float q[4], float mat[3][3])
More information about the Bf-blender-cvs
mailing list