[Bf-blender-cvs] [fbca69c] master: BLI_math: add mat3_normalized_to_* functions

Campbell Barton noreply at git.blender.org
Fri Oct 23 22:12:52 CEST 2015


Commit: fbca69c69afb370ddc5a0b52e10d9db61c025752
Author: Campbell Barton
Date:   Sat Oct 24 07:02:51 2015 +1100
Branches: master
https://developer.blender.org/rBfbca69c69afb370ddc5a0b52e10d9db61c025752

BLI_math: add mat3_normalized_to_* functions

Many uses of matrices for rotation keep them normalized,
so no need to normalize each time.

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

M	source/blender/blenkernel/intern/armature.c
M	source/blender/blenkernel/intern/constraint.c
M	source/blender/blenkernel/intern/object.c
M	source/blender/blenlib/BLI_math_rotation.h
M	source/blender/blenlib/intern/math_matrix.c
M	source/blender/blenlib/intern/math_rotation.c
M	source/blender/editors/space_view3d/view3d_edit.c
M	source/blender/editors/space_view3d/view3d_view.c
M	source/blender/python/mathutils/mathutils_Matrix.c
M	source/blender/python/mathutils/mathutils_Quaternion.c

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

diff --git a/source/blender/blenkernel/intern/armature.c b/source/blender/blenkernel/intern/armature.c
index 6afe7f1..fde2578 100644
--- a/source/blender/blenkernel/intern/armature.c
+++ b/source/blender/blenkernel/intern/armature.c
@@ -1325,18 +1325,20 @@ void BKE_armature_mat_pose_to_bone_ex(Object *ob, bPoseChannel *pchan, float inm
 /* same as BKE_object_mat3_to_rot() */
 void BKE_pchan_mat3_to_rot(bPoseChannel *pchan, float mat[3][3], bool use_compat)
 {
+	BLI_ASSERT_UNIT_M3(mat);
+
 	switch (pchan->rotmode) {
 		case ROT_MODE_QUAT:
-			mat3_to_quat(pchan->quat, mat);
+			mat3_normalized_to_quat(pchan->quat, mat);
 			break;
 		case ROT_MODE_AXISANGLE:
-			mat3_to_axis_angle(pchan->rotAxis, &pchan->rotAngle, mat);
+			mat3_normalized_to_axis_angle(pchan->rotAxis, &pchan->rotAngle, mat);
 			break;
 		default: /* euler */
 			if (use_compat)
-				mat3_to_compatible_eulO(pchan->eul, pchan->eul, pchan->rotmode, mat);
+				mat3_normalized_to_compatible_eulO(pchan->eul, pchan->eul, pchan->rotmode, mat);
 			else
-				mat3_to_eulO(pchan->eul, pchan->rotmode, mat);
+				mat3_normalized_to_eulO(pchan->eul, pchan->rotmode, mat);
 			break;
 	}
 }
diff --git a/source/blender/blenkernel/intern/constraint.c b/source/blender/blenkernel/intern/constraint.c
index 03406c6..906a9e4 100644
--- a/source/blender/blenkernel/intern/constraint.c
+++ b/source/blender/blenkernel/intern/constraint.c
@@ -3863,7 +3863,7 @@ static void pivotcon_evaluate(bConstraint *con, bConstraintOb *cob, ListBase *ta
 
 
 	/* correct the pivot by the rotation axis otherwise the pivot translates when it shouldnt */
-	mat3_to_axis_angle(axis, &angle, rotMat);
+	mat3_normalized_to_axis_angle(axis, &angle, rotMat);
 	if (angle) {
 		float dvec[3];
 		sub_v3_v3v3(vec, pivot, cob->matrix[3]);
diff --git a/source/blender/blenkernel/intern/object.c b/source/blender/blenkernel/intern/object.c
index 61c4073..4ec4137 100644
--- a/source/blender/blenkernel/intern/object.c
+++ b/source/blender/blenkernel/intern/object.c
@@ -1945,11 +1945,13 @@ void BKE_object_rot_to_mat3(Object *ob, float mat[3][3], bool use_drot)
 
 void BKE_object_mat3_to_rot(Object *ob, float mat[3][3], bool use_compat)
 {
+	BLI_ASSERT_UNIT_M3(mat);
+
 	switch (ob->rotmode) {
 		case ROT_MODE_QUAT:
 		{
 			float dquat[4];
-			mat3_to_quat(ob->quat, mat);
+			mat3_normalized_to_quat(ob->quat, mat);
 			normalize_qt_qt(dquat, ob->dquat);
 			invert_qt_normalized(dquat);
 			mul_qt_qtqt(ob->quat, dquat, ob->quat);
@@ -1961,7 +1963,7 @@ void BKE_object_mat3_to_rot(Object *ob, float mat[3][3], bool use_compat)
 			float dquat[4];
 
 			/* without drot we could apply 'mat' directly */
-			mat3_to_quat(quat, mat);
+			mat3_normalized_to_quat(quat, mat);
 			axis_angle_to_quat(dquat, ob->drotAxis, ob->drotAngle);
 			invert_qt_normalized(dquat);
 			mul_qt_qtqt(quat, dquat, quat);
@@ -1972,18 +1974,16 @@ void BKE_object_mat3_to_rot(Object *ob, float mat[3][3], bool use_compat)
 		{
 			float quat[4];
 			float dquat[4];
-			float tmat[3][3];
 
 			/* without drot we could apply 'mat' directly */
-			mat3_to_quat(quat, mat);
+			mat3_normalized_to_quat(quat, mat);
 			eulO_to_quat(dquat, ob->drot, ob->rotmode);
 			invert_qt_normalized(dquat);
 			mul_qt_qtqt(quat, dquat, quat);
-			quat_to_mat3(tmat, quat);
 			/* end drot correction */
 
-			if (use_compat) mat3_to_compatible_eulO(ob->rot, ob->rot, ob->rotmode, tmat);
-			else            mat3_to_eulO(ob->rot, ob->rotmode, tmat);
+			if (use_compat) quat_to_compatible_eulO(ob->rot, ob->rot, ob->rotmode, quat);
+			else            quat_to_eulO(ob->rot, ob->rotmode, quat);
 			break;
 		}
 	}
diff --git a/source/blender/blenlib/BLI_math_rotation.h b/source/blender/blenlib/BLI_math_rotation.h
index 4c4e305..24c20ee 100644
--- a/source/blender/blenlib/BLI_math_rotation.h
+++ b/source/blender/blenlib/BLI_math_rotation.h
@@ -79,6 +79,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 mat3_normalized_to_quat(float q[4], float mat[3][3]);
+void mat4_normalized_to_quat(float q[4], float mat[4][4]);
 void mat3_to_quat(float q[4], float mat[3][3]);
 void mat4_to_quat(float q[4], float mat[4][4]);
 void tri_to_quat_ex(float quat[4], const float v1[3], const float v2[3], const float v3[3],
@@ -114,9 +116,11 @@ void axis_angle_normalized_to_mat3_ex(float mat[3][3], const float axis[3],
 void axis_angle_normalized_to_mat3(float R[3][3], const float axis[3], const float angle);
 void axis_angle_to_mat4(float R[4][4], const float axis[3], const float angle);
 
-void quat_to_axis_angle(float axis[3], float *angle, const float q[4]);
+void mat3_normalized_to_axis_angle(float axis[3], float *angle, float M[3][3]);
+void mat4_normalized_to_axis_angle(float axis[3], float *angle, float M[4][4]);
 void mat3_to_axis_angle(float axis[3], float *angle, float M[3][3]);
 void mat4_to_axis_angle(float axis[3], float *angle, float M[4][4]);
+void quat_to_axis_angle(float axis[3], float *angle, const float q[4]);
 
 void axis_angle_to_mat3_single(float R[3][3], const char axis, const float angle);
 void      angle_to_mat2(float R[2][2], const float angle);
@@ -134,12 +138,16 @@ void eul_to_quat(float quat[4], const float eul[3]);
 void eul_to_mat3(float mat[3][3], const float eul[3]);
 void eul_to_mat4(float mat[4][4], const float eul[3]);
 
-void quat_to_eul(float eul[3], const float quat[4]);
+void mat3_normalized_to_eul(float eul[3], float mat[3][3]);
+void mat4_normalized_to_eul(float eul[3], float mat[4][4]);
 void mat3_to_eul(float eul[3], float mat[3][3]);
 void mat4_to_eul(float eul[3], float mat[4][4]);
+void quat_to_eul(float eul[3], const float quat[4]);
 
-void compatible_eul(float eul[3], const float old[3]);
+void mat3_normalized_to_compatible_eul(float eul[3], const float old[3], float mat[3][3]);
 void mat3_to_compatible_eul(float eul[3], const float old[3], float mat[3][3]);
+void quat_to_compatible_eul(float eul[3], const float oldrot[3], const float quat[4]);
+void compatible_eul(float eul[3], const float old[3]);
 
 void rotate_eul(float eul[3], const char axis, const float angle);
 
@@ -164,14 +172,19 @@ void eulO_to_mat3(float mat[3][3], const float eul[3], const short order);
 void eulO_to_mat4(float mat[4][4], const float eul[3], const short order);
 void eulO_to_axis_angle(float axis[3], float *angle, const float eul[3], const short order);
 void eulO_to_gimbal_axis(float gmat[3][3], const float eul[3], const short order);
- 
-void quat_to_eulO(float eul[3], const short order, const float quat[4]);
+
+void mat3_normalized_to_eulO(float eul[3], const short order, float mat[3][3]);
+void mat4_normalized_to_eulO(float eul[3], const short order, float mat[4][4]);
 void mat3_to_eulO(float eul[3], const short order, float mat[3][3]);
 void mat4_to_eulO(float eul[3], const short order, float mat[4][4]);
+void quat_to_eulO(float eul[3], const short order, const float quat[4]);
 void axis_angle_to_eulO(float eul[3], const short order, const float axis[3], const float angle);
 
+void mat3_normalized_to_compatible_eulO(float eul[3], float old[3], const short order, float mat[3][3]);
+void mat4_normalized_to_compatible_eulO(float eul[3], float old[3], const short order, float mat[4][4]);
 void mat3_to_compatible_eulO(float eul[3], float old[3], const short order, float mat[3][3]);
 void mat4_to_compatible_eulO(float eul[3], float old[3], const short order, float mat[4][4]);
+void quat_to_compatible_eulO(float eul[3], float old[3], const short order, const float quat[4]);
 
 void rotate_eulO(float eul[3], const short order, char axis, float angle);
 
diff --git a/source/blender/blenlib/intern/math_matrix.c b/source/blender/blenlib/intern/math_matrix.c
index 4bf6d16..19d1169 100644
--- a/source/blender/blenlib/intern/math_matrix.c
+++ b/source/blender/blenlib/intern/math_matrix.c
@@ -1549,7 +1549,7 @@ void mat4_to_loc_quat(float loc[3], float quat[4], float wmat[4][4])
 		negate_m3(mat3_n);
 	}
 
-	mat3_to_quat(quat, mat3_n);
+	mat3_normalized_to_quat(quat, mat3_n);
 	copy_v3_v3(loc, wmat[3]);
 }
 
@@ -1557,7 +1557,7 @@ void mat4_decompose(float loc[3], float quat[4], float size[3], float wmat[4][4]
 {
 	float rot[3][3];
 	mat4_to_loc_rot_size(loc, rot, size, wmat);
-	mat3_to_quat(quat, rot);
+	mat3_normalized_to_quat(quat, rot);
 }
 
 /**
@@ -1692,8 +1692,8 @@ void blend_m3_m3m3(float out[3][3], float dst[3][3], float src[3][3], const floa
 	mat3_to_rot_size(drot, dscale, dst);
 	mat3_to_rot_size(srot, sscale, src);
 
-	mat3_to_quat(dquat, drot);
-	mat3_to_quat(squat, srot);
+	mat3_normalized_to_quat(dquat, drot);
+	mat3_normalized_to_quat(squat, srot);
 
 	/* do blending */
 	interp_qt_qtqt(fquat, dquat, squat, srcweight);
@@ -1715,8 +1715,8 @@ void blend_m4_m4m4(float out[4][4], float dst[4][4], float src[4][4], const floa
 	mat4_to_loc_rot_size(dloc, drot, dscale, dst);
 	mat4_to_loc_rot_size(sloc, srot, sscale, src);
 
-	mat3_to_quat(dquat, drot);
-	mat3_to_quat(squat, srot);
+	mat3_normalized_to_quat(dquat, drot);
+	mat3_normalized_to_quat(squat, srot);
 
 	/* do blending */
 	interp_v3_v3v3(floc, dloc, sloc, srcweight);
diff --git a/source/blender/blenlib/intern/math_rotation.c b/source/blender/blenlib/intern/math_rotation.c
index 88c577f..f89f622 100644
--- a/source/blender/blenlib/intern/math_rotation.c
+++ b/source/blender/blenlib/intern/math_rotation.c
@@ -293,14 +293,11 @@ void quat_to_mat4(float m[4][4], const float q[4])
 	m[3][3] = 1.0f;
 }
 
-void mat3_to_quat(float q[4], float wmat[3][3])
+void mat3_normalized_to_quat(float q[4], float mat[3][3])
 {
 	double tr, s;
-	float mat[3][3];
 
-	/* work on a copy */
-	copy_m3_m3(mat, wmat);
-	normalize_m3(mat); /* this is needed AND a 'normalize_qt' in the end */
+	BLI_ASSERT_UNIT_M3(mat);
 
 	tr = 0.25 * (double)(1.0f + mat[0][0] + mat[1][1] + mat[2][2]);
 
@@ -344,13 +341,30 @@ void mat3_to_quat(float q[4], float wmat[3][3])
 
 	normalize_qt(q);
 }
+void mat3_to_quat(float q[4], float m[3][3])
+{
+	float unit_mat[3][3];
+
+	/* work on a copy */
+	/* this is needed AND a 'normalize_qt' in the end */
+	normalize_m3_m3(unit_mat, m);
+	mat3_normalized_to_quat(q, unit_mat);
+}
+
+void mat4_norma

@@ Diff output truncated at 10240 characters. @@




More information about the Bf-blender-cvs mailing list