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 >=, >, <, <=.
This commit is contained in:
2011-02-05 10:40:42 +00:00
parent 998198a041
commit d694115ac4
4 changed files with 124 additions and 167 deletions

View File

@@ -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;
ok= EXPP_VectorsAreEqual(colA->col, colB->col, COLOR_SIZE, 1) ? 0 : -1;
}
if (!colA || !colB){
if (comparison_type == Py_NE){
Py_RETURN_TRUE;
}else{
Py_RETURN_FALSE;
}
}
colA = (ColorObject*)objectA;
colB = (ColorObject*)objectB;
switch (op) {
case Py_NE:
ok = !ok; /* pass through */
case Py_EQ:
res = ok ? Py_False : Py_True;
break;
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;
}
if (result == 1){
Py_RETURN_TRUE;
}else{
Py_RETURN_FALSE;
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------------------------

View File

@@ -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;
ok= ((eulA->order == eulB->order) && EXPP_VectorsAreEqual(eulA->eul, eulB->eul, EULER_SIZE, 1)) ? 0 : -1;
}
if (!eulA || !eulB){
if (comparison_type == Py_NE){
Py_RETURN_TRUE;
}else{
Py_RETURN_FALSE;
}
}
eulA = (EulerObject*)objectA;
eulB = (EulerObject*)objectB;
switch (op) {
case Py_NE:
ok = !ok; /* pass through */
case Py_EQ:
res = ok ? Py_False : Py_True;
break;
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;
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))

View File

@@ -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;
PyObject *res;
int ok= -1; /* zero is true */
if (!MatrixObject_Check(objectA) || !MatrixObject_Check(objectB)){
if (comparison_type == Py_NE){
Py_RETURN_TRUE;
}else{
Py_RETURN_FALSE;
}
if (MatrixObject_Check(a) && MatrixObject_Check(b)) {
MatrixObject *matA= (MatrixObject*)a;
MatrixObject *matB= (MatrixObject*)b;
if(!BaseMath_ReadCallback(matA) || !BaseMath_ReadCallback(matB))
return NULL;
ok= ( (matA->colSize == matB->colSize) &&
(matA->rowSize == matB->rowSize) &&
EXPP_VectorsAreEqual(matA->contigPtr, matB->contigPtr, (matA->rowSize * matA->colSize), 1)
) ? 0 : -1;
}
matA = (MatrixObject*)objectA;
matB = (MatrixObject*)objectB;
if(!BaseMath_ReadCallback(matA) || !BaseMath_ReadCallback(matB))
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;
if (matA->colSize != matB->colSize || matA->rowSize != matB->rowSize){
if (comparison_type == Py_NE){
Py_RETURN_TRUE;
}else{
Py_RETURN_FALSE;
}
}
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;
}
if (result == 1){
Py_RETURN_TRUE;
}else{
Py_RETURN_FALSE;
}
return Py_INCREF(res), res;
}
/*---------------------SEQUENCE PROTOCOLS------------------------

View File

@@ -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;
ok= (EXPP_VectorsAreEqual(quatA->quat, quatB->quat, QUAT_SIZE, 1)) ? 0 : -1;
}
if (!quatA || !quatB){
if (comparison_type == Py_NE){
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;
}
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;
}
return Py_INCREF(res), res;
}
//---------------------SEQUENCE PROTOCOLS------------------------