[Bf-blender-cvs] [e2ec045cc99] master: BLI_math: add utility to calculate compatible quaternions

Campbell Barton noreply at git.blender.org
Tue Mar 19 07:24:42 CET 2019


Commit: e2ec045cc995bb8767a5f88e8830aa1062921011
Author: Campbell Barton
Date:   Tue Mar 19 16:50:18 2019 +1100
Branches: master
https://developer.blender.org/rBe2ec045cc995bb8767a5f88e8830aa1062921011

BLI_math: add utility to calculate compatible quaternions

===================================================================

M	source/blender/blenlib/BLI_math_rotation.h
M	source/blender/blenlib/intern/math_rotation.c

===================================================================

diff --git a/source/blender/blenlib/BLI_math_rotation.h b/source/blender/blenlib/BLI_math_rotation.h
index 5246d7ad1a9..60adcf8c762 100644
--- a/source/blender/blenlib/BLI_math_rotation.h
+++ b/source/blender/blenlib/BLI_math_rotation.h
@@ -77,6 +77,8 @@ void add_qt_qtqt(float q[4], const float a[4], const float b[4], const float t);
 void quat_to_mat3(float mat[3][3], const float q[4]);
 void quat_to_mat4(float mat[4][4], const float q[4]);
 
+void quat_to_compatible_quat(float q[4], const float a[4], const float old[4]);
+
 void mat3_normalized_to_quat(float q[4], const float mat[3][3]);
 void mat4_normalized_to_quat(float q[4], const float mat[4][4]);
 void mat3_to_quat(float q[4], const float mat[3][3]);
diff --git a/source/blender/blenlib/intern/math_rotation.c b/source/blender/blenlib/intern/math_rotation.c
index a348edaece8..d52c8f54096 100644
--- a/source/blender/blenlib/intern/math_rotation.c
+++ b/source/blender/blenlib/intern/math_rotation.c
@@ -201,6 +201,23 @@ void pow_qt_fl_normalized(float q[4], const float fac)
 	normalize_v3_length(q + 1, si);
 }
 
+/**
+ * Apply the rotation of \a a to \a q keeping the values compatible with \a old.
+ * Avoid axis flipping for animated f-curves for eg.
+ */
+void quat_to_compatible_quat(float q[4], const float a[4], const float old[4])
+{
+	BLI_ASSERT_UNIT_QUAT(a);
+	float delta[4];
+	float old_unit[4];
+	normalize_qt_qt(old_unit, old);
+	rotation_between_quats_to_quat(delta, old_unit, a);
+	mul_qt_qtqt(q, old, delta);
+	if ((q[0] < 0.0f) != (old[0] < 0.0f)) {
+		negate_v4(q);
+	}
+}
+
 /* skip error check, currently only needed by mat3_to_quat_is_ok */
 static void quat_to_mat3_no_error(float m[3][3], const float q[4])
 {



More information about the Bf-blender-cvs mailing list