[Bf-blender-cvs] SVN commit: /data/svn/bf-blender [21160] branches/blender2.5/blender/source : Made Mathutils use radians rather then degrees.

Campbell Barton ideasman42 at gmail.com
Thu Jun 25 22:47:41 CEST 2009


Revision: 21160
          http://projects.blender.org/plugins/scmsvn/viewcvs.php?view=rev&root=bf-blender&revision=21160
Author:   campbellbarton
Date:     2009-06-25 22:47:41 +0200 (Thu, 25 Jun 2009)

Log Message:
-----------
Made Mathutils use radians rather then degrees. defining USE_MATHUTILS_DEG for testing existing scripts.
Added conversion for BGE Quaternion WXYZ (Blender/C) -> XYZW (Moto C++).
BGE Python API now uses WXYZ following mathutils (break script warning).

Modified Paths:
--------------
    branches/blender2.5/blender/source/blender/python/generic/Mathutils.c
    branches/blender2.5/blender/source/blender/python/generic/Mathutils.h
    branches/blender2.5/blender/source/blender/python/generic/euler.c
    branches/blender2.5/blender/source/blender/python/generic/matrix.c
    branches/blender2.5/blender/source/blender/python/generic/quat.c
    branches/blender2.5/blender/source/blender/python/generic/vector.c
    branches/blender2.5/blender/source/gameengine/Converter/BL_ActionActuator.cpp
    branches/blender2.5/blender/source/gameengine/Ketsji/KX_PyMath.cpp
    branches/blender2.5/blender/source/gameengine/Ketsji/KX_PyMath.h

Modified: branches/blender2.5/blender/source/blender/python/generic/Mathutils.c
===================================================================
--- branches/blender2.5/blender/source/blender/python/generic/Mathutils.c	2009-06-25 18:04:32 UTC (rev 21159)
+++ branches/blender2.5/blender/source/blender/python/generic/Mathutils.c	2009-06-25 20:47:41 UTC (rev 21160)
@@ -275,8 +275,11 @@
 
 	angleRads = (double)saacos(dot);
 
+#ifdef USE_MATHUTILS_DEG
 	return PyFloat_FromDouble(angleRads * (180/ Py_PI));
-
+#else
+	return PyFloat_FromDouble(angleRads);
+#endif
 AttributeError1:
 	PyErr_SetString(PyExc_AttributeError, "Mathutils.AngleBetweenVecs(): expects (2) VECTOR objects of the same size\n");
 	return NULL;
@@ -364,12 +367,19 @@
 		PyErr_SetString(PyExc_TypeError, "Mathutils.RotationMatrix(): expected float int and optional string and vector\n");
 		return NULL;
 	}
-	
+
+#ifdef USE_MATHUTILS_DEG
 	/* Clamp to -360:360 */
 	while (angle<-360.0f)
 		angle+=360.0;
 	while (angle>360.0f)
 		angle-=360.0;
+#else
+	while (angle<-(Py_PI*2))
+		angle+=(Py_PI*2);
+	while (angle>(Py_PI*2))
+		angle-=(Py_PI*2);
+#endif
 	
 	if(matSize != 2 && matSize != 3 && matSize != 4) {
 		PyErr_SetString(PyExc_AttributeError, "Mathutils.RotationMatrix(): can only return a 2x2 3x3 or 4x4 matrix\n");
@@ -399,8 +409,11 @@
 			return NULL;
 		
 	}
+#ifdef USE_MATHUTILS_DEG
 	//convert to radians
 	angle = angle * (float) (Py_PI / 180);
+#endif
+
 	if(axis == NULL && matSize == 2) {
 		//2D rotation matrix
 		mat[0] = (float) cos (angle);

Modified: branches/blender2.5/blender/source/blender/python/generic/Mathutils.h
===================================================================
--- branches/blender2.5/blender/source/blender/python/generic/Mathutils.h	2009-06-25 18:04:32 UTC (rev 21159)
+++ branches/blender2.5/blender/source/blender/python/generic/Mathutils.h	2009-06-25 20:47:41 UTC (rev 21160)
@@ -38,6 +38,8 @@
 #include "quat.h"
 #include "euler.h"
 
+/* #define USE_MATHUTILS_DEG - for backwards compat */
+
 /* Can cast different mathutils types to this, use for generic funcs */
 
 typedef struct {

Modified: branches/blender2.5/blender/source/blender/python/generic/euler.c
===================================================================
--- branches/blender2.5/blender/source/blender/python/generic/euler.c	2009-06-25 18:04:32 UTC (rev 21159)
+++ branches/blender2.5/blender/source/blender/python/generic/euler.c	2009-06-25 20:47:41 UTC (rev 21160)
@@ -70,7 +70,7 @@
 
 	PyObject *listObject = NULL;
 	int size, i;
-	float eul[3], scalar;
+	float eul[3];
 	PyObject *e;
 
 	size = PyTuple_GET_SIZE(args);
@@ -102,15 +102,13 @@
 			return NULL;
 		}
 
-		scalar= (float)PyFloat_AsDouble(e);
+		eul[i]= (float)PyFloat_AsDouble(e);
 		Py_DECREF(e);
 		
-		if(scalar==-1 && PyErr_Occurred()) { // parsed item is not a number
+		if(eul[i]==-1 && PyErr_Occurred()) { // parsed item is not a number
 			PyErr_SetString(PyExc_TypeError, "Mathutils.Euler(): 3d numeric sequence expected\n");
 			return NULL;
 		}
-
-		eul[i]= scalar;
 	}
 	return newEulerObject(eul, Py_NEW);
 }
@@ -126,10 +124,15 @@
 	if(!BaseMath_ReadCallback(self))
 		return NULL;
 
+#ifdef USE_MATHUTILS_DEG
 	for(x = 0; x < 3; x++) {
 		eul[x] = self->eul[x] * ((float)Py_PI / 180);
 	}
 	EulToQuat(eul, quat);
+#else
+	EulToQuat(self->eul, quat);
+#endif
+
 	return newQuaternionObject(quat, Py_NEW);
 }
 //----------------------------Euler.toMatrix()---------------------
@@ -143,10 +146,14 @@
 	if(!BaseMath_ReadCallback(self))
 		return NULL;
 
+#ifdef USE_MATHUTILS_DEG
 	for(x = 0; x < 3; x++) {
 		eul[x] = self->eul[x] * ((float)Py_PI / 180);
 	}
 	EulToMat3(eul, (float (*)[3]) mat);
+#else
+	EulToMat3(self->eul, (float (*)[3]) mat);
+#endif
 	return newMatrixObject(mat, 3, 3 , Py_NEW);
 }
 //----------------------------Euler.unique()-----------------------
@@ -161,10 +168,12 @@
 	if(!BaseMath_ReadCallback(self))
 		return NULL;
 
+#ifdef USE_MATHUTILS_DEG
 	//radians
 	heading = self->eul[0] * (float)Py_PI / 180;
 	pitch = self->eul[1] * (float)Py_PI / 180;
 	bank = self->eul[2] * (float)Py_PI / 180;
+#endif
 
 	//wrap heading in +180 / -180
 	pitch += Py_PI;
@@ -195,10 +204,12 @@
 	heading -= (floor(heading * Opi2)) * pi2;
 	heading -= Py_PI;
 
+#ifdef USE_MATHUTILS_DEG
 	//back to degrees
 	self->eul[0] = (float)(heading * 180 / (float)Py_PI);
 	self->eul[1] = (float)(pitch * 180 / (float)Py_PI);
 	self->eul[2] = (float)(bank * 180 / (float)Py_PI);
+#endif
 
 	BaseMath_WriteCallback(self);
 	Py_INCREF(self);
@@ -237,16 +248,21 @@
 	if(!BaseMath_ReadCallback(self))
 		return NULL;
 
+#ifdef USE_MATHUTILS_DEG
 	//covert to radians
 	angle *= ((float)Py_PI / 180);
 	for(x = 0; x < 3; x++) {
 		self->eul[x] *= ((float)Py_PI / 180);
 	}
+#endif
 	euler_rot(self->eul, angle, *axis);
+
+#ifdef USE_MATHUTILS_DEG
 	//convert back from radians
 	for(x = 0; x < 3; x++) {
 		self->eul[x] *= (180 / (float)Py_PI);
 	}
+#endif
 
 	BaseMath_WriteCallback(self);
 	Py_INCREF(self);
@@ -266,17 +282,23 @@
 	if(!BaseMath_ReadCallback(self) || !BaseMath_ReadCallback(value))
 		return NULL;
 
+#ifdef USE_MATHUTILS_DEG
 	//covert to radians
 	for(x = 0; x < 3; x++) {
 		self->eul[x] = self->eul[x] * ((float)Py_PI / 180);
 		eul_from_rad[x] = value->eul[x] * ((float)Py_PI / 180);
 	}
 	compatible_eul(self->eul, eul_from_rad);
+#else
+	compatible_eul(self->eul, value->eul);
+#endif
+
+#ifdef USE_MATHUTILS_DEG
 	//convert back from radians
 	for(x = 0; x < 3; x++) {
 		self->eul[x] *= (180 / (float)Py_PI);
 	}
-	
+#endif
 	BaseMath_WriteCallback(self);
 	Py_INCREF(self);
 	return (PyObject *)self;

Modified: branches/blender2.5/blender/source/blender/python/generic/matrix.c
===================================================================
--- branches/blender2.5/blender/source/blender/python/generic/matrix.c	2009-06-25 18:04:32 UTC (rev 21159)
+++ branches/blender2.5/blender/source/blender/python/generic/matrix.c	2009-06-25 20:47:41 UTC (rev 21160)
@@ -257,10 +257,14 @@
 	if(eul_compat) {
 		if(!BaseMath_ReadCallback(eul_compat))
 			return NULL;
-		
+
+#ifdef USE_MATHUTILS_DEG
 		for(x = 0; x < 3; x++) {
 			eul_compatf[x] = eul_compat->eul[x] * ((float)Py_PI / 180);
 		}
+#else
+		VECCOPY(eul_compatf, eul_compat->eul);
+#endif
 	}
 	
 	/*must be 3-4 cols, 3-4 rows, square matrix*/
@@ -278,10 +282,12 @@
 		PyErr_SetString(PyExc_AttributeError, "Matrix.toEuler(): inappropriate matrix size - expects 3x3 or 4x4 matrix\n");
 		return NULL;
 	}
+#ifdef USE_MATHUTILS_DEG
 	/*have to convert to degrees*/
 	for(x = 0; x < 3; x++) {
 		eul[x] *= (float) (180 / Py_PI);
 	}
+#endif
 	return newEulerObject(eul, Py_NEW);
 }
 /*---------------------------Matrix.resize4x4() ------------------*/

Modified: branches/blender2.5/blender/source/blender/python/generic/quat.c
===================================================================
--- branches/blender2.5/blender/source/blender/python/generic/quat.c	2009-06-25 18:04:32 UTC (rev 21159)
+++ branches/blender2.5/blender/source/blender/python/generic/quat.c	2009-06-25 20:47:41 UTC (rev 21160)
@@ -77,7 +77,7 @@
 {
 	PyObject *listObject = NULL, *n, *q;
 	int size, i;
-	float quat[4], scalar;
+	float quat[4];
 	double angle = 0.0f;
 
 	size = PyTuple_GET_SIZE(args);
@@ -151,20 +151,22 @@
 			return NULL;
 		}
 
-		scalar = PyFloat_AsDouble(q);
+		quat[i] = PyFloat_AsDouble(q);
 		Py_DECREF(q);
 
-		if (scalar==-1 && PyErr_Occurred()) {
+		if (quat[i]==-1 && PyErr_Occurred()) {
 			PyErr_SetString(PyExc_TypeError, "Mathutils.Quaternion(): 4d numeric sequence expected or 3d vector and number\n");
 			return NULL;
 		}
-		quat[i] = scalar;
 	}
 
 	if(size == 3) //calculate the quat based on axis/angle
-		AxisAngleToQuat(quat, quat, angle * (Py_PI / 180)); // TODO - 2.5 use radians, note using quat for src and target is ok here
+#ifdef USE_MATHUTILS_DEG
+		AxisAngleToQuat(quat, quat, angle * (Py_PI / 180));
+#else
+		AxisAngleToQuat(quat, quat, angle);
+#endif
 
-
 	return newQuaternionObject(quat, Py_NEW);
 }
 
@@ -189,28 +191,33 @@
 		if(!BaseMath_ReadCallback(eul_compat))
 			return NULL;
 		
+		QuatToMat3(self->quat, mat);
+
+#ifdef USE_MATHUTILS_DEG
 		for(x = 0; x < 3; x++) {
 			eul_compatf[x] = eul_compat->eul[x] * ((float)Py_PI / 180);
 		}
-		
-		QuatToMat3(self->quat, mat);
 		Mat3ToCompatibleEul(mat, eul, eul_compatf);
+#else
+		Mat3ToCompatibleEul(mat, eul, eul_compat->eul);
+#endif
 	}
 	else {
 		QuatToEul(self->quat, eul);
 	}
 	
-	
+#ifdef USE_MATHUTILS_DEG
 	for(x = 0; x < 3; x++) {
 		eul[x] *= (180 / (float)Py_PI);
 	}
+#endif
 	return newEulerObject(eul, Py_NEW);
 }
 //----------------------------Quaternion.toMatrix()------------------
 //return the quat as a matrix
 static PyObject *Quaternion_ToMatrix(QuaternionObject * self)
 {
-	float mat[9] = {0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f};
+	float mat[9]; /* all values are set */
 
 	if(!BaseMath_ReadCallback(self))
 		return NULL;
@@ -662,7 +669,9 @@
 {
 	double ang = self->quat[0];
 	ang = 2 * (saacos(ang));
+#ifdef USE_MATHUTILS_DEG
 	ang *= (180 / Py_PI);
+#endif
 	return PyFloat_FromDouble(ang);
 }
 

Modified: branches/blender2.5/blender/source/blender/python/generic/vector.c
===================================================================
--- branches/blender2.5/blender/source/blender/python/generic/vector.c	2009-06-25 18:04:32 UTC (rev 21159)
+++ branches/blender2.5/blender/source/blender/python/generic/vector.c	2009-06-25 20:47:41 UTC (rev 21160)
@@ -1771,7 +1771,7 @@
 	0,                          /* ob_size */
 #endif
 	/*  For printing, in format "<module>.<name>" */
-	"Blender Vector",             /* char *tp_name; */
+	"vector",             /* char *tp_name; */
 	sizeof( VectorObject ),         /* int tp_basicsize; */
 	0,                          /* tp_itemsize;  For allocation */
 

Modified: branches/blender2.5/blender/source/gameengine/Converter/BL_ActionActuator.cpp
===================================================================
--- branches/blender2.5/blender/source/gameengine/Converter/BL_ActionActuator.cpp	2009-06-25 18:04:32 UTC (rev 21159)
+++ branches/blender2.5/blender/source/gameengine/Converter/BL_ActionActuator.cpp	2009-06-25 20:47:41 UTC (rev 21160)
@@ -962,9 +962,9 @@
 	else {
 		MT_Vector3 loc;
 		MT_Vector3 size;
-		MT_Vector4 quat;
+		MT_Quaternion quat;
 		
-		if (!PyVecTo(pyloc, loc) || !PyVecTo(pysize, size) || !PyVecTo(pyquat, quat))
+		if (!PyVecTo(pyloc, loc) || !PyVecTo(pysize, size) || !PyQuatTo(pyquat, quat))
 			return NULL;
 		
 		// same as above
@@ -977,7 +977,7 @@

@@ Diff output truncated at 10240 characters. @@




More information about the Bf-blender-cvs mailing list