[Bf-blender-cvs] SVN commit: /data/svn/bf-blender [34649] trunk/blender: mathutils rotate functions for Euler/Quaternion/Matrix/Vector types.

Campbell Barton ideasman42 at gmail.com
Sat Feb 5 10:57:03 CET 2011


Revision: 34649
          http://projects.blender.org/scm/viewvc.php?view=rev&root=bf-blender&revision=34649
Author:   campbellbarton
Date:     2011-02-05 09:57:02 +0000 (Sat, 05 Feb 2011)
Log Message:
-----------
mathutils rotate functions for Euler/Quaternion/Matrix/Vector types.
each accept Euler/Quaternion/Matrix types.

eg:
  Euler.rotate(Quaternion(axis, angle))
  Vector.rotate(Euler((pi/2, 0, 0)))

matrix.resize_4x4() and euler.make_compatible() were still returning an instance of themselves, now return None.

Modified Paths:
--------------
    trunk/blender/doc/python_api/examples/mathutils.Vector.py
    trunk/blender/doc/python_api/examples/mathutils.py
    trunk/blender/source/blender/python/generic/mathutils.c
    trunk/blender/source/blender/python/generic/mathutils.h
    trunk/blender/source/blender/python/generic/mathutils_euler.c
    trunk/blender/source/blender/python/generic/mathutils_matrix.c
    trunk/blender/source/blender/python/generic/mathutils_matrix.h
    trunk/blender/source/blender/python/generic/mathutils_quat.c
    trunk/blender/source/blender/python/generic/mathutils_vector.c

Modified: trunk/blender/doc/python_api/examples/mathutils.Vector.py
===================================================================
--- trunk/blender/doc/python_api/examples/mathutils.Vector.py	2011-02-05 07:04:23 UTC (rev 34648)
+++ trunk/blender/doc/python_api/examples/mathutils.Vector.py	2011-02-05 09:57:02 UTC (rev 34649)
@@ -10,7 +10,7 @@
 
 vec2d = mathutils.Vector((1, 2))
 vec3d = mathutils.Vector((1, 0, 0))
-vec4d = vec_a.copy().resize4D()
+vec4d = vec_a.to_4d()
 
 # other mathutuls types
 quat = mathutils.Quaternion()

Modified: trunk/blender/doc/python_api/examples/mathutils.py
===================================================================
--- trunk/blender/doc/python_api/examples/mathutils.py	2011-02-05 07:04:23 UTC (rev 34648)
+++ trunk/blender/doc/python_api/examples/mathutils.py	2011-02-05 09:57:02 UTC (rev 34649)
@@ -3,15 +3,15 @@
 
 vec = mathutils.Vector((1.0, 2.0, 3.0))
 
-mat_rot = mathutils.Matrix.Rotation(radians(90), 4, 'X')
+mat_rot = mathutils.Matrix.Rotation(radians(90.0), 4, 'X')
 mat_trans = mathutils.Matrix.Translation(vec)
 
 mat = mat_trans * mat_rot
 mat.invert()
 
-mat3 = mat.rotation_part()
-quat1 = mat.to_quat()
-quat2 = mat3.to_quat()
+mat3 = mat.to_3x3()
+quat1 = mat.to_quaternion()
+quat2 = mat3.to_quaternion()
 
 angle = quat1.difference(quat2)
 

Modified: trunk/blender/source/blender/python/generic/mathutils.c
===================================================================
--- trunk/blender/source/blender/python/generic/mathutils.c	2011-02-05 07:04:23 UTC (rev 34648)
+++ trunk/blender/source/blender/python/generic/mathutils.c	2011-02-05 09:57:02 UTC (rev 34649)
@@ -47,6 +47,7 @@
  * - toEuler --> to_euler
  * - toQuat --> to_quat
  * - Vector.toTrackQuat --> Vector.to_track_quat
+ * - Vector.rotate(axis, angle) --> rotate(other), where other can be Euler/Quaternion/Matrix.
  * - Quaternion * Quaternion --> cross product (not dot product)
  * - Euler.rotate(angle, axis) --> Euler.rotate_axis(axis, angle)
  * - Euler.unique() *removed*, not a standard function only toggled different rotations.
@@ -164,6 +165,49 @@
 	}
 }
 
+int mathutils_any_to_rotmat(float rmat[3][3], PyObject *value, const char *error_prefix)
+{
+	if(EulerObject_Check(value)) {
+		if(!BaseMath_ReadCallback((BaseMathObject *)value)) {
+			return -1;
+		}
+		else {
+			eulO_to_mat3(rmat, ((EulerObject *)value)->eul, ((EulerObject *)value)->order);
+			return 0;
+		}
+	}
+	else if (QuaternionObject_Check(value)) {
+		if(!BaseMath_ReadCallback((BaseMathObject *)value)) {
+			return -1;
+		}
+		else {
+			float tquat[4];
+			normalize_qt_qt(tquat, ((QuaternionObject *)value)->quat);
+			quat_to_mat3(rmat, tquat);
+			return 0;
+		}
+	}
+	else if (MatrixObject_Check(value)) {
+		if(!BaseMath_ReadCallback((BaseMathObject *)value)) {
+			return -1;
+		}
+		else if(((MatrixObject *)value)->colSize < 3 || ((MatrixObject *)value)->rowSize < 3) {
+			PyErr_Format(PyExc_ValueError, "%.200s: matrix must have minimum 3x3 dimensions", error_prefix);
+			return -1;
+		}
+		else {
+			matrix_as_3x3(rmat, (MatrixObject *)value);
+			normalize_m3(rmat);
+			return 0;
+		}
+	}
+	else {
+		PyErr_Format(PyExc_TypeError, "%.200s: expected a Euler, Quaternion or Matrix type, found %.200s", error_prefix, Py_TYPE(value)->tp_name);
+		return -1;
+	}
+}
+
+
 //----------------------------------MATRIX FUNCTIONS--------------------
 
 

Modified: trunk/blender/source/blender/python/generic/mathutils.h
===================================================================
--- trunk/blender/source/blender/python/generic/mathutils.h	2011-02-05 07:04:23 UTC (rev 34648)
+++ trunk/blender/source/blender/python/generic/mathutils.h	2011-02-05 09:57:02 UTC (rev 34649)
@@ -100,5 +100,6 @@
 
 /* utility func */
 int mathutils_array_parse(float *array, int array_min, int array_max, PyObject *value, const char *error_prefix);
+int mathutils_any_to_rotmat(float rmat[3][3], PyObject *value, const char *error_prefix);
 
 #endif /* MATHUTILS_H */

Modified: trunk/blender/source/blender/python/generic/mathutils_euler.c
===================================================================
--- trunk/blender/source/blender/python/generic/mathutils_euler.c	2011-02-05 07:04:23 UTC (rev 34648)
+++ trunk/blender/source/blender/python/generic/mathutils_euler.c	2011-02-05 09:57:02 UTC (rev 34649)
@@ -180,8 +180,6 @@
 "   :type axis: string\n"
 "   :arg angle: angle in radians.\n"
 "   :type angle: float\n"
-"   :return: an instance of itself\n"
-"   :rtype: :class:`Euler`"
 ;
 static PyObject *Euler_rotate_axis(EulerObject * self, PyObject *args)
 {
@@ -204,10 +202,37 @@
 	else								rotate_eulO(self->eul, self->order, *axis, angle);
 
 	(void)BaseMath_WriteCallback(self);
-	Py_INCREF(self);
-	return (PyObject *)self;
+
+	Py_RETURN_NONE;
 }
 
+static char Euler_rotate_doc[] =
+".. method:: rotate(other)\n"
+"\n"
+"   Rotates the euler a by another mathutils value.\n"
+"\n"
+"   :arg other: rotation component of mathutils value\n"
+"   :type other: :class:`Euler`, :class:`Quaternion` or :class:`Matrix`\n"
+;
+static PyObject *Euler_rotate(EulerObject * self, PyObject *value)
+{
+	float self_rmat[3][3], other_rmat[3][3], rmat[3][3];
+
+	if(!BaseMath_ReadCallback(self))
+		return NULL;
+
+	if(mathutils_any_to_rotmat(other_rmat, value, "euler.rotate(value)") == -1)
+		return NULL;
+
+	eulO_to_mat3(self_rmat, self->eul, self->order);
+	mul_m3_m3m3(rmat, self_rmat, other_rmat);
+
+	mat3_to_compatible_eulO(self->eul, self->eul, self->order, rmat);
+
+	(void)BaseMath_WriteCallback(self);
+	Py_RETURN_NONE;
+}
+
 static char Euler_make_compatible_doc[] =
 ".. method:: make_compatible(other)\n"
 "\n"
@@ -215,8 +240,6 @@
 "\n"
 "   :arg other: make compatible with this rotation.\n"
 "   :type other: :class:`Euler`\n"
-"   :return: an instance of itself.\n"
-"   :rtype: :class:`Euler`\n"
 "\n"
 "   .. note:: the rotation order is not taken into account for this function.\n"
 ;
@@ -233,8 +256,8 @@
 	compatible_eul(self->eul, teul);
 
 	(void)BaseMath_WriteCallback(self);
-	Py_INCREF(self);
-	return (PyObject *)self;
+
+	Py_RETURN_NONE;
 }
 
 //----------------------------Euler.rotate()-----------------------
@@ -564,6 +587,7 @@
 	{"to_matrix", (PyCFunction) Euler_to_matrix, METH_NOARGS, Euler_to_matrix_doc},
 	{"to_quaternion", (PyCFunction) Euler_to_quaternion, METH_NOARGS, Euler_to_quaternion_doc},
 	{"rotate_axis", (PyCFunction) Euler_rotate_axis, METH_VARARGS, Euler_rotate_axis_doc},
+	{"rotate", (PyCFunction) Euler_rotate, METH_O, Euler_rotate_doc},
 	{"make_compatible", (PyCFunction) Euler_make_compatible, METH_O, Euler_make_compatible_doc},
 	{"__copy__", (PyCFunction) Euler_copy, METH_NOARGS, Euler_copy_doc},
 	{"copy", (PyCFunction) Euler_copy, METH_NOARGS, Euler_copy_doc},

Modified: trunk/blender/source/blender/python/generic/mathutils_matrix.c
===================================================================
--- trunk/blender/source/blender/python/generic/mathutils_matrix.c	2011-02-05 07:04:23 UTC (rev 34648)
+++ trunk/blender/source/blender/python/generic/mathutils_matrix.c	2011-02-05 09:57:02 UTC (rev 34649)
@@ -608,7 +608,7 @@
 	return newMatrixObject(mat, matSize, matSize, Py_NEW, (PyTypeObject *)cls);
 }
 
-static void matrix_as_3x3(float mat[3][3], MatrixObject *self)
+void matrix_as_3x3(float mat[3][3], MatrixObject *self)
 {
 	copy_v3_v3(mat[0], self->matrix[0]);
 	copy_v3_v3(mat[1], self->matrix[1]);
@@ -733,9 +733,6 @@
 ".. method:: resize_4x4()\n"
 "\n"
 "   Resize the matrix to 4x4.\n"
-"\n"
-"   :return: an instance of itself.\n"
-"   :rtype: :class:`Matrix`\n"
 ;
 static PyObject *Matrix_resize_4x4(MatrixObject *self)
 {
@@ -785,8 +782,7 @@
 	self->rowSize = 4;
 	self->colSize = 4;
 
-	Py_INCREF(self);
-	return (PyObject *)self;
+	Py_RETURN_NONE;
 }
 
 static char Matrix_to_4x4_doc[] =
@@ -976,6 +972,40 @@
 	MATRIX_APPLY_TO_COPY(Matrix_invert, self);
 }
 
+static char Matrix_rotate_doc[] =
+".. method:: rotate(other)\n"
+"\n"
+"   Rotates the matrix a by another mathutils value.\n"
+"\n"
+"   :arg other: rotation component of mathutils value\n"
+"   :type other: :class:`Euler`, :class:`Quaternion` or :class:`Matrix`\n"
+"\n"
+"   .. note:: If any of the columns are not unit length this may not have desired results.\n"
+;
+static PyObject *Matrix_rotate(MatrixObject *self, PyObject *value)
+{
+	float self_rmat[3][3], other_rmat[3][3], rmat[3][3];
+
+	if(!BaseMath_ReadCallback(self))
+		return NULL;
+
+	if(mathutils_any_to_rotmat(other_rmat, value, "matrix.rotate(value)") == -1)
+		return NULL;
+
+	if(self->colSize != 3 || self->rowSize != 3) {
+		PyErr_SetString(PyExc_ValueError, "Matrix must have 3x3 dimensions");
+		return NULL;
+	}
+
+	matrix_as_3x3(self_rmat, self);
+	mul_m3_m3m3(rmat, self_rmat, other_rmat);
+
+	copy_m3_m3((float (*)[3])(self->contigPtr), rmat);
+
+	(void)BaseMath_WriteCallback(self);
+	Py_RETURN_NONE;
+}
+
 /*---------------------------Matrix.decompose() ---------------------*/
 static char Matrix_decompose_doc[] =
 ".. method:: decompose()\n"
@@ -1733,6 +1763,7 @@
 	// TODO. {"resize_3x3", (PyCFunction) Matrix_resize3x3, METH_NOARGS, Matrix_resize3x3_doc},
 	{"to_4x4", (PyCFunction) Matrix_to_4x4, METH_NOARGS, Matrix_to_4x4_doc},
 	{"resize_4x4", (PyCFunction) Matrix_resize_4x4, METH_NOARGS, Matrix_resize_4x4_doc},
+	{"rotate", (PyCFunction) Matrix_rotate, METH_O, Matrix_rotate_doc},
 
 	/* return converted representation */
 	{"to_euler", (PyCFunction) Matrix_to_euler, METH_VARARGS, Matrix_to_euler_doc},

Modified: trunk/blender/source/blender/python/generic/mathutils_matrix.h
===================================================================
--- trunk/blender/source/blender/python/generic/mathutils_matrix.h	2011-02-05 07:04:23 UTC (rev 34648)
+++ trunk/blender/source/blender/python/generic/mathutils_matrix.h	2011-02-05 09:57:02 UTC (rev 34649)
@@ -55,4 +55,6 @@
 extern int mathutils_matrix_vector_cb_index;
 extern struct Mathutils_Callback mathutils_matrix_vector_cb;
 
+void matrix_as_3x3(float mat[3][3], MatrixObject *self);
+
 #endif /* MATHUTILS_MATRIX_H */

Modified: trunk/blender/source/blender/python/generic/mathutils_quat.c
===================================================================
--- trunk/blender/source/blender/python/generic/mathutils_quat.c	2011-02-05 07:04:23 UTC (rev 34648)
+++ trunk/blender/source/blender/python/generic/mathutils_quat.c	2011-02-05 09:57:02 UTC (rev 34649)
@@ -256,6 +256,36 @@
 	return newQuaternionObject(quat, Py_NEW, Py_TYPE(self));
 }
 
+static char Quaternion_rotate_doc[] =

@@ Diff output truncated at 10240 characters. @@



More information about the Bf-blender-cvs mailing list