From d694115ac497ee8db2bd6be221e5b67a7d1fbb3a Mon Sep 17 00:00:00 2001 From: Campbell Barton Date: Sat, 5 Feb 2011 10:40:42 +0000 Subject: mathutils fixes noticed when refactoring. - comparing eulers was ignoring the order. - printing Euler()'s now prints the order too. - un-orderable types (all except for Vector's), were not raising an exception when compared with >=, >, <, <=. --- source/blender/python/generic/mathutils_color.c | 64 ++++++++--------- source/blender/python/generic/mathutils_euler.c | 89 +++++++++++------------- source/blender/python/generic/mathutils_matrix.c | 77 +++++++++----------- source/blender/python/generic/mathutils_quat.c | 67 +++++++----------- 4 files changed, 127 insertions(+), 170 deletions(-) (limited to 'source/blender/python') diff --git a/source/blender/python/generic/mathutils_color.c b/source/blender/python/generic/mathutils_color.c index 3906f3e2b38..af4742e74c6 100644 --- a/source/blender/python/generic/mathutils_color.c +++ b/source/blender/python/generic/mathutils_color.c @@ -118,48 +118,40 @@ static PyObject *Color_repr(ColorObject * self) //------------------------tp_richcmpr //returns -1 execption, 0 false, 1 true -static PyObject* Color_richcmpr(PyObject *objectA, PyObject *objectB, int comparison_type) +static PyObject* Color_richcmpr(PyObject *a, PyObject *b, int op) { - ColorObject *colA = NULL, *colB = NULL; - int result = 0; + PyObject *res; + int ok= -1; /* zero is true */ - if(ColorObject_Check(objectA)) { - colA = (ColorObject*)objectA; - if(!BaseMath_ReadCallback(colA)) - return NULL; - } - if(ColorObject_Check(objectB)) { - colB = (ColorObject*)objectB; - if(!BaseMath_ReadCallback(colB)) + if (ColorObject_Check(a) && ColorObject_Check(b)) { + ColorObject *colA= (ColorObject*)a; + ColorObject *colB= (ColorObject*)b; + + if(!BaseMath_ReadCallback(colA) || !BaseMath_ReadCallback(colB)) return NULL; - } - if (!colA || !colB){ - if (comparison_type == Py_NE){ - Py_RETURN_TRUE; - }else{ - Py_RETURN_FALSE; - } - } - colA = (ColorObject*)objectA; - colB = (ColorObject*)objectB; - - switch (comparison_type){ - case Py_EQ: - result = EXPP_VectorsAreEqual(colA->col, colB->col, COLOR_SIZE, 1); - break; - case Py_NE: - result = !EXPP_VectorsAreEqual(colA->col, colB->col, COLOR_SIZE, 1); - break; - default: - printf("The result of the comparison could not be evaluated"); - break; + ok= EXPP_VectorsAreEqual(colA->col, colB->col, COLOR_SIZE, 1) ? 0 : -1; } - if (result == 1){ - Py_RETURN_TRUE; - }else{ - Py_RETURN_FALSE; + + switch (op) { + case Py_NE: + ok = !ok; /* pass through */ + case Py_EQ: + res = ok ? Py_False : Py_True; + break; + + case Py_LT: + case Py_LE: + case Py_GT: + case Py_GE: + res = Py_NotImplemented; + break; + default: + PyErr_BadArgument(); + return NULL; } + + return Py_INCREF(res), res; } //---------------------SEQUENCE PROTOCOLS------------------------ diff --git a/source/blender/python/generic/mathutils_euler.c b/source/blender/python/generic/mathutils_euler.c index 7b5fbcb4b6f..509388de8cb 100644 --- a/source/blender/python/generic/mathutils_euler.c +++ b/source/blender/python/generic/mathutils_euler.c @@ -72,6 +72,13 @@ static PyObject *Euler_new(PyTypeObject *type, PyObject *args, PyObject *kwds) return newEulerObject(eul, order, Py_NEW, type); } +/* internal use, assuem read callback is done */ +static const char *euler_order_str(EulerObject *self) +{ + static const char order[][4] = {"XYZ", "XZY", "YXZ", "YZX", "ZXY", "ZYX"}; + return order[self->order-EULER_ORDER_XYZ]; +} + short euler_order_from_string(const char *str, const char *error_prefix) { if((str[0] && str[1] && str[2] && str[3]=='\0')) { @@ -129,8 +136,7 @@ static PyObject *Euler_to_quaternion(EulerObject * self) if(!BaseMath_ReadCallback(self)) return NULL; - if(self->order==EULER_ORDER_XYZ) eul_to_quat(quat, self->eul); - else eulO_to_quat(quat, self->eul, self->order); + eulO_to_quat(quat, self->eul, self->order); return newQuaternionObject(quat, Py_NEW, NULL); } @@ -151,8 +157,7 @@ static PyObject *Euler_to_matrix(EulerObject * self) if(!BaseMath_ReadCallback(self)) return NULL; - if(self->order==EULER_ORDER_XYZ) eul_to_mat3((float (*)[3])mat, self->eul); - else eulO_to_mat3((float (*)[3])mat, self->eul, self->order); + eulO_to_mat3((float (*)[3])mat, self->eul, self->order); return newMatrixObject(mat, 3, 3 , Py_NEW, NULL); } @@ -198,8 +203,8 @@ static PyObject *Euler_rotate_axis(EulerObject * self, PyObject *args) if(!BaseMath_ReadCallback(self)) return NULL; - if(self->order == EULER_ORDER_XYZ) rotate_eul(self->eul, *axis, angle); - else rotate_eulO(self->eul, self->order, *axis, angle); + + rotate_eulO(self->eul, self->order, *axis, angle); (void)BaseMath_WriteCallback(self); @@ -293,56 +298,46 @@ static PyObject *Euler_repr(EulerObject * self) tuple= Euler_ToTupleExt(self, -1); - ret= PyUnicode_FromFormat("Euler(%R)", tuple); + ret= PyUnicode_FromFormat("Euler(%R, '%s')", tuple, euler_order_str(self)); Py_DECREF(tuple); return ret; } -//------------------------tp_richcmpr -//returns -1 execption, 0 false, 1 true -static PyObject* Euler_richcmpr(PyObject *objectA, PyObject *objectB, int comparison_type) +static PyObject* Euler_richcmpr(PyObject *a, PyObject *b, int op) { - EulerObject *eulA = NULL, *eulB = NULL; - int result = 0; + PyObject *res; + int ok= -1; /* zero is true */ - if(EulerObject_Check(objectA)) { - eulA = (EulerObject*)objectA; - if(!BaseMath_ReadCallback(eulA)) - return NULL; - } - if(EulerObject_Check(objectB)) { - eulB = (EulerObject*)objectB; - if(!BaseMath_ReadCallback(eulB)) + if (EulerObject_Check(a) && EulerObject_Check(b)) { + EulerObject *eulA= (EulerObject*)a; + EulerObject *eulB= (EulerObject*)b; + + if(!BaseMath_ReadCallback(eulA) || !BaseMath_ReadCallback(eulB)) return NULL; - } - if (!eulA || !eulB){ - if (comparison_type == Py_NE){ - Py_RETURN_TRUE; - }else{ - Py_RETURN_FALSE; - } + ok= ((eulA->order == eulB->order) && EXPP_VectorsAreEqual(eulA->eul, eulB->eul, EULER_SIZE, 1)) ? 0 : -1; } - eulA = (EulerObject*)objectA; - eulB = (EulerObject*)objectB; - - switch (comparison_type){ - case Py_EQ: - result = EXPP_VectorsAreEqual(eulA->eul, eulB->eul, EULER_SIZE, 1); - break; - case Py_NE: - result = !EXPP_VectorsAreEqual(eulA->eul, eulB->eul, EULER_SIZE, 1); - break; - default: - printf("The result of the comparison could not be evaluated"); - break; - } - if (result == 1){ - Py_RETURN_TRUE; - }else{ - Py_RETURN_FALSE; + + switch (op) { + case Py_NE: + ok = !ok; /* pass through */ + case Py_EQ: + res = ok ? Py_False : Py_True; + break; + + case Py_LT: + case Py_LE: + case Py_GT: + case Py_GE: + res = Py_NotImplemented; + break; + default: + PyErr_BadArgument(); + return NULL; } + + return Py_INCREF(res), res; } //---------------------SEQUENCE PROTOCOLS------------------------ @@ -545,12 +540,10 @@ static int Euler_setAxis(EulerObject *self, PyObject *value, void *type) /* rotation order */ static PyObject *Euler_getOrder(EulerObject *self, void *UNUSED(closure)) { - const char order[][4] = {"XYZ", "XZY", "YXZ", "YZX", "ZXY", "ZYX"}; - if(!BaseMath_ReadCallback(self)) /* can read order too */ return NULL; - return PyUnicode_FromString(order[self->order-EULER_ORDER_XYZ]); + return PyUnicode_FromString(euler_order_str(self)); } static int Euler_setOrder(EulerObject *self, PyObject *value, void *UNUSED(closure)) diff --git a/source/blender/python/generic/mathutils_matrix.c b/source/blender/python/generic/mathutils_matrix.c index fe5503afdbe..2837c5223a1 100644 --- a/source/blender/python/generic/mathutils_matrix.c +++ b/source/blender/python/generic/mathutils_matrix.c @@ -1263,58 +1263,43 @@ static PyObject *Matrix_repr(MatrixObject *self) return NULL; } -/*------------------------tp_richcmpr*/ -/*returns -1 execption, 0 false, 1 true*/ -static PyObject* Matrix_richcmpr(PyObject *objectA, PyObject *objectB, int comparison_type) +static PyObject* Matrix_richcmpr(PyObject *a, PyObject *b, int op) { - MatrixObject *matA = NULL, *matB = NULL; - int result = 0; - - if (!MatrixObject_Check(objectA) || !MatrixObject_Check(objectB)){ - if (comparison_type == Py_NE){ - Py_RETURN_TRUE; - }else{ - Py_RETURN_FALSE; - } - } - matA = (MatrixObject*)objectA; - matB = (MatrixObject*)objectB; + PyObject *res; + int ok= -1; /* zero is true */ - if(!BaseMath_ReadCallback(matA) || !BaseMath_ReadCallback(matB)) - return NULL; + if (MatrixObject_Check(a) && MatrixObject_Check(b)) { + MatrixObject *matA= (MatrixObject*)a; + MatrixObject *matB= (MatrixObject*)b; - if (matA->colSize != matB->colSize || matA->rowSize != matB->rowSize){ - if (comparison_type == Py_NE){ - Py_RETURN_TRUE; - }else{ - Py_RETURN_FALSE; - } - } + if(!BaseMath_ReadCallback(matA) || !BaseMath_ReadCallback(matB)) + return NULL; - switch (comparison_type){ - case Py_EQ: - /*contigPtr is basically a really long vector*/ - result = EXPP_VectorsAreEqual(matA->contigPtr, matB->contigPtr, - (matA->rowSize * matA->colSize), 1); - break; - case Py_NE: - result = EXPP_VectorsAreEqual(matA->contigPtr, matB->contigPtr, - (matA->rowSize * matA->colSize), 1); - if (result == 0){ - result = 1; - }else{ - result = 0; - } - break; - default: - printf("The result of the comparison could not be evaluated"); - break; + ok= ( (matA->colSize == matB->colSize) && + (matA->rowSize == matB->rowSize) && + EXPP_VectorsAreEqual(matA->contigPtr, matB->contigPtr, (matA->rowSize * matA->colSize), 1) + ) ? 0 : -1; } - if (result == 1){ - Py_RETURN_TRUE; - }else{ - Py_RETURN_FALSE; + + switch (op) { + case Py_NE: + ok = !ok; /* pass through */ + case Py_EQ: + res = ok ? Py_False : Py_True; + break; + + case Py_LT: + case Py_LE: + case Py_GT: + case Py_GE: + res = Py_NotImplemented; + break; + default: + PyErr_BadArgument(); + return NULL; } + + return Py_INCREF(res), res; } /*---------------------SEQUENCE PROTOCOLS------------------------ diff --git a/source/blender/python/generic/mathutils_quat.c b/source/blender/python/generic/mathutils_quat.c index e5234e504be..56757ae5d2d 100644 --- a/source/blender/python/generic/mathutils_quat.c +++ b/source/blender/python/generic/mathutils_quat.c @@ -448,53 +448,40 @@ static PyObject *Quaternion_repr(QuaternionObject *self) return ret; } -//------------------------tp_richcmpr -//returns -1 execption, 0 false, 1 true -static PyObject* Quaternion_richcmpr(PyObject *objectA, PyObject *objectB, int comparison_type) +static PyObject* Quaternion_richcmpr(PyObject *a, PyObject *b, int op) { - QuaternionObject *quatA = NULL, *quatB = NULL; - int result = 0; + PyObject *res; + int ok= -1; /* zero is true */ - if(QuaternionObject_Check(objectA)) { - quatA = (QuaternionObject*)objectA; - if(!BaseMath_ReadCallback(quatA)) - return NULL; - } - if(QuaternionObject_Check(objectB)) { - quatB = (QuaternionObject*)objectB; - if(!BaseMath_ReadCallback(quatB)) + if (QuaternionObject_Check(a) && QuaternionObject_Check(b)) { + QuaternionObject *quatA= (QuaternionObject *)a; + QuaternionObject *quatB= (QuaternionObject *)b; + + if(!BaseMath_ReadCallback(quatA) || !BaseMath_ReadCallback(quatB)) return NULL; - } - if (!quatA || !quatB){ - if (comparison_type == Py_NE){ - Py_RETURN_TRUE; - }else{ - Py_RETURN_FALSE; - } + ok= (EXPP_VectorsAreEqual(quatA->quat, quatB->quat, QUAT_SIZE, 1)) ? 0 : -1; } - switch (comparison_type){ - case Py_EQ: - result = EXPP_VectorsAreEqual(quatA->quat, quatB->quat, QUAT_SIZE, 1); - break; - case Py_NE: - result = EXPP_VectorsAreEqual(quatA->quat, quatB->quat, QUAT_SIZE, 1); - if (result == 0){ - result = 1; - }else{ - result = 0; - } - break; - default: - printf("The result of the comparison could not be evaluated"); - break; - } - if (result == 1){ - Py_RETURN_TRUE; - }else{ - Py_RETURN_FALSE; + switch (op) { + case Py_NE: + ok = !ok; /* pass through */ + case Py_EQ: + res = ok ? Py_False : Py_True; + break; + + case Py_LT: + case Py_LE: + case Py_GT: + case Py_GE: + res = Py_NotImplemented; + break; + default: + PyErr_BadArgument(); + return NULL; } + + return Py_INCREF(res), res; } //---------------------SEQUENCE PROTOCOLS------------------------ -- cgit v1.2.3