diff options
author | Campbell Barton <ideasman42@gmail.com> | 2009-06-26 00:47:41 +0400 |
---|---|---|
committer | Campbell Barton <ideasman42@gmail.com> | 2009-06-26 00:47:41 +0400 |
commit | 4b914c7f75faa65a7d2a991b7904c18051d13a13 (patch) | |
tree | 35ff731a37f612e491f21ee1b036f3d1679e9a69 /source/blender/python | |
parent | 47ca543b32e74b67e5f8eb6265cc08f93018b002 (diff) |
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).
Diffstat (limited to 'source/blender/python')
-rw-r--r-- | source/blender/python/generic/Mathutils.c | 17 | ||||
-rw-r--r-- | source/blender/python/generic/Mathutils.h | 2 | ||||
-rw-r--r-- | source/blender/python/generic/euler.c | 34 | ||||
-rw-r--r-- | source/blender/python/generic/matrix.c | 8 | ||||
-rw-r--r-- | source/blender/python/generic/quat.c | 29 | ||||
-rw-r--r-- | source/blender/python/generic/vector.c | 2 |
6 files changed, 72 insertions, 20 deletions
diff --git a/source/blender/python/generic/Mathutils.c b/source/blender/python/generic/Mathutils.c index d7330ac46c0..ec94a48ddbd 100644 --- a/source/blender/python/generic/Mathutils.c +++ b/source/blender/python/generic/Mathutils.c @@ -275,8 +275,11 @@ static PyObject *M_Mathutils_AngleBetweenVecs(PyObject * self, PyObject * args) 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 @@ static PyObject *M_Mathutils_RotationMatrix(PyObject * self, PyObject * args) 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 @@ static PyObject *M_Mathutils_RotationMatrix(PyObject * self, PyObject * args) 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); diff --git a/source/blender/python/generic/Mathutils.h b/source/blender/python/generic/Mathutils.h index d234ebf1452..6a4e28d6068 100644 --- a/source/blender/python/generic/Mathutils.h +++ b/source/blender/python/generic/Mathutils.h @@ -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 { diff --git a/source/blender/python/generic/euler.c b/source/blender/python/generic/euler.c index eb9358774e1..9041eb84a3d 100644 --- a/source/blender/python/generic/euler.c +++ b/source/blender/python/generic/euler.c @@ -70,7 +70,7 @@ static PyObject *Euler_new(PyObject * self, PyObject * args) PyObject *listObject = NULL; int size, i; - float eul[3], scalar; + float eul[3]; PyObject *e; size = PyTuple_GET_SIZE(args); @@ -102,15 +102,13 @@ static PyObject *Euler_new(PyObject * self, PyObject * args) 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 @@ static PyObject *Euler_ToQuat(EulerObject * self) 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 @@ static PyObject *Euler_ToMatrix(EulerObject * self) 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 @@ static PyObject *Euler_Unique(EulerObject * self) 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 @@ static PyObject *Euler_Unique(EulerObject * self) 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 @@ static PyObject *Euler_Rotate(EulerObject * self, PyObject *args) 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 @@ static PyObject *Euler_MakeCompatible(EulerObject * self, EulerObject *value) 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; diff --git a/source/blender/python/generic/matrix.c b/source/blender/python/generic/matrix.c index ef4f7280cdc..b546aa1226c 100644 --- a/source/blender/python/generic/matrix.c +++ b/source/blender/python/generic/matrix.c @@ -257,10 +257,14 @@ PyObject *Matrix_toEuler(MatrixObject * self, PyObject *args) 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 @@ PyObject *Matrix_toEuler(MatrixObject * self, PyObject *args) 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() ------------------*/ diff --git a/source/blender/python/generic/quat.c b/source/blender/python/generic/quat.c index b1e9f4a1d31..e7413d38ee5 100644 --- a/source/blender/python/generic/quat.c +++ b/source/blender/python/generic/quat.c @@ -77,7 +77,7 @@ static PyObject *Quaternion_new(PyTypeObject *type, PyObject *args, PyObject *kw { 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,19 +151,21 @@ static PyObject *Quaternion_new(PyTypeObject *type, PyObject *args, PyObject *kw 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 @@ static PyObject *Quaternion_ToEuler(QuaternionObject * self, PyObject *args) 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 @@ static PyObject *Quaternion_getAngle( QuaternionObject * self, void *type ) { double ang = self->quat[0]; ang = 2 * (saacos(ang)); +#ifdef USE_MATHUTILS_DEG ang *= (180 / Py_PI); +#endif return PyFloat_FromDouble(ang); } diff --git a/source/blender/python/generic/vector.c b/source/blender/python/generic/vector.c index d68d3f41370..9ce0a7ca2f9 100644 --- a/source/blender/python/generic/vector.c +++ b/source/blender/python/generic/vector.c @@ -1771,7 +1771,7 @@ PyTypeObject vector_Type = { 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 */ |