Refcount fixes

* fixes posible reference count issues with mathutils
* mathutils classes should no longer memory leak
This commit is contained in:
Joseph Gilbert 2005-09-27 17:03:28 +00:00
parent d27212e647
commit 39a243f8d2
6 changed files with 153 additions and 233 deletions

@ -152,7 +152,7 @@ PyObject *column_vector_multiplication(MatrixObject * mat, VectorObject* vec)
vecNew[z++] = (float)dot; vecNew[z++] = (float)dot;
dot = 0.0f; dot = 0.0f;
} }
return (PyObject *) newVectorObject(vecNew, vec->size, Py_NEW); return newVectorObject(vecNew, vec->size, Py_NEW);
} }
//This is a helper for point/matrix translation //This is a helper for point/matrix translation
PyObject *column_point_multiplication(MatrixObject * mat, PointObject* pt) PyObject *column_point_multiplication(MatrixObject * mat, PointObject* pt)
@ -181,7 +181,7 @@ PyObject *column_point_multiplication(MatrixObject * mat, PointObject* pt)
ptNew[z++] = (float)dot; ptNew[z++] = (float)dot;
dot = 0.0f; dot = 0.0f;
} }
return (PyObject *) newPointObject(ptNew, pt->size, Py_NEW); return newPointObject(ptNew, pt->size, Py_NEW);
} }
//-----------------row_vector_multiplication (internal)----------- //-----------------row_vector_multiplication (internal)-----------
//ROW VECTOR Multiplication - Vector X Matrix //ROW VECTOR Multiplication - Vector X Matrix
@ -216,7 +216,7 @@ PyObject *row_vector_multiplication(VectorObject* vec, MatrixObject * mat)
vecNew[z++] = (float)dot; vecNew[z++] = (float)dot;
dot = 0.0f; dot = 0.0f;
} }
return (PyObject *) newVectorObject(vecNew, size, Py_NEW); return newVectorObject(vecNew, size, Py_NEW);
} }
//This is a helper for the point class //This is a helper for the point class
PyObject *row_point_multiplication(PointObject* pt, MatrixObject * mat) PyObject *row_point_multiplication(PointObject* pt, MatrixObject * mat)
@ -246,7 +246,7 @@ PyObject *row_point_multiplication(PointObject* pt, MatrixObject * mat)
ptNew[z++] = (float)dot; ptNew[z++] = (float)dot;
dot = 0.0f; dot = 0.0f;
} }
return (PyObject *) newPointObject(ptNew, size, Py_NEW); return newPointObject(ptNew, size, Py_NEW);
} }
//-----------------quat_rotation (internal)----------- //-----------------quat_rotation (internal)-----------
//This function multiplies a vector/point * quat or vice versa //This function multiplies a vector/point * quat or vice versa
@ -275,7 +275,7 @@ PyObject *quat_rotation(PyObject *arg1, PyObject *arg2)
quat->quat[3]*quat->quat[3]*vec->vec[2] - 2*quat->quat[0]*quat->quat[2]*vec->vec[0] - quat->quat[3]*quat->quat[3]*vec->vec[2] - 2*quat->quat[0]*quat->quat[2]*vec->vec[0] -
quat->quat[2]*quat->quat[2]*vec->vec[2] + 2*quat->quat[0]*quat->quat[1]*vec->vec[1] - quat->quat[2]*quat->quat[2]*vec->vec[2] + 2*quat->quat[0]*quat->quat[1]*vec->vec[1] -
quat->quat[1]*quat->quat[1]*vec->vec[2] + quat->quat[0]*quat->quat[0]*vec->vec[2]; quat->quat[1]*quat->quat[1]*vec->vec[2] + quat->quat[0]*quat->quat[0]*vec->vec[2];
return (PyObject *) newVectorObject(rot, 3, Py_NEW); return newVectorObject(rot, 3, Py_NEW);
}else if(PointObject_Check(arg2)){ }else if(PointObject_Check(arg2)){
pt = (PointObject*)arg2; pt = (PointObject*)arg2;
rot[0] = quat->quat[0]*quat->quat[0]*pt->coord[0] + 2*quat->quat[2]*quat->quat[0]*pt->coord[2] - rot[0] = quat->quat[0]*quat->quat[0]*pt->coord[0] + 2*quat->quat[2]*quat->quat[0]*pt->coord[2] -
@ -290,7 +290,7 @@ PyObject *quat_rotation(PyObject *arg1, PyObject *arg2)
quat->quat[3]*quat->quat[3]*pt->coord[2] - 2*quat->quat[0]*quat->quat[2]*pt->coord[0] - quat->quat[3]*quat->quat[3]*pt->coord[2] - 2*quat->quat[0]*quat->quat[2]*pt->coord[0] -
quat->quat[2]*quat->quat[2]*pt->coord[2] + 2*quat->quat[0]*quat->quat[1]*pt->coord[1] - quat->quat[2]*quat->quat[2]*pt->coord[2] + 2*quat->quat[0]*quat->quat[1]*pt->coord[1] -
quat->quat[1]*quat->quat[1]*pt->coord[2] + quat->quat[0]*quat->quat[0]*pt->coord[2]; quat->quat[1]*quat->quat[1]*pt->coord[2] + quat->quat[0]*quat->quat[0]*pt->coord[2];
return (PyObject *) newPointObject(rot, 3, Py_NEW); return newPointObject(rot, 3, Py_NEW);
} }
}else if(VectorObject_Check(arg1)){ }else if(VectorObject_Check(arg1)){
vec = (VectorObject*)arg1; vec = (VectorObject*)arg1;
@ -308,7 +308,7 @@ PyObject *quat_rotation(PyObject *arg1, PyObject *arg2)
quat->quat[3]*quat->quat[3]*vec->vec[2] - 2*quat->quat[0]*quat->quat[2]*vec->vec[0] - quat->quat[3]*quat->quat[3]*vec->vec[2] - 2*quat->quat[0]*quat->quat[2]*vec->vec[0] -
quat->quat[2]*quat->quat[2]*vec->vec[2] + 2*quat->quat[0]*quat->quat[1]*vec->vec[1] - quat->quat[2]*quat->quat[2]*vec->vec[2] + 2*quat->quat[0]*quat->quat[1]*vec->vec[1] -
quat->quat[1]*quat->quat[1]*vec->vec[2] + quat->quat[0]*quat->quat[0]*vec->vec[2]; quat->quat[1]*quat->quat[1]*vec->vec[2] + quat->quat[0]*quat->quat[0]*vec->vec[2];
return (PyObject *) newVectorObject(rot, 3, Py_NEW); return newVectorObject(rot, 3, Py_NEW);
} }
}else if(PointObject_Check(arg1)){ }else if(PointObject_Check(arg1)){
pt = (PointObject*)arg1; pt = (PointObject*)arg1;
@ -326,7 +326,7 @@ PyObject *quat_rotation(PyObject *arg1, PyObject *arg2)
quat->quat[3]*quat->quat[3]*pt->coord[2] - 2*quat->quat[0]*quat->quat[2]*pt->coord[0] - quat->quat[3]*quat->quat[3]*pt->coord[2] - 2*quat->quat[0]*quat->quat[2]*pt->coord[0] -
quat->quat[2]*quat->quat[2]*pt->coord[2] + 2*quat->quat[0]*quat->quat[1]*pt->coord[1] - quat->quat[2]*quat->quat[2]*pt->coord[2] + 2*quat->quat[0]*quat->quat[1]*pt->coord[1] -
quat->quat[1]*quat->quat[1]*pt->coord[2] + quat->quat[0]*quat->quat[0]*pt->coord[2]; quat->quat[1]*quat->quat[1]*pt->coord[2] + quat->quat[0]*quat->quat[0]*pt->coord[2];
return (PyObject *) newPointObject(rot, 3, Py_NEW); return newPointObject(rot, 3, Py_NEW);
} }
} }
@ -371,6 +371,7 @@ PyObject *M_Mathutils_Vector(PyObject * self, PyObject * args)
PyObject *listObject = NULL; PyObject *listObject = NULL;
int size, i; int size, i;
float vec[4]; float vec[4];
PyObject *v, *f;
size = PySequence_Length(args); size = PySequence_Length(args);
if (size == 1) { if (size == 1) {
@ -384,24 +385,25 @@ PyObject *M_Mathutils_Vector(PyObject * self, PyObject * args)
} }
} else if (size == 0) { } else if (size == 0) {
//returns a new empty 3d vector //returns a new empty 3d vector
return (PyObject *) newVectorObject(NULL, 3, Py_NEW); return newVectorObject(NULL, 3, Py_NEW);
} else { } else {
listObject = EXPP_incr_ret(args); listObject = EXPP_incr_ret(args);
} }
if (size<2 || size>4) { // Invalid vector size if (size<2 || size>4) { // Invalid vector size
Py_XDECREF(listObject); Py_XDECREF(listObject);
return EXPP_ReturnPyObjError(PyExc_AttributeError, return EXPP_ReturnPyObjError(PyExc_AttributeError,
"Mathutils.Vector(): 2-4 floats or ints expected (optionally in a sequence)\n"); "Mathutils.Vector(): 2-4 floats or ints expected (optionally in a sequence)\n");
} }
for (i=0; i<size; i++) {
PyObject *v, *f;
for (i=0; i<size; i++) {
v=PySequence_GetItem(listObject, i); v=PySequence_GetItem(listObject, i);
if (v==NULL) { // Failed to read sequence if (v==NULL) { // Failed to read sequence
Py_XDECREF(listObject); Py_XDECREF(listObject);
return EXPP_ReturnPyObjError(PyExc_RuntimeError, return EXPP_ReturnPyObjError(PyExc_RuntimeError,
"Mathutils.Vector(): 2-4 floats or ints expected (optionally in a sequence)\n"); "Mathutils.Vector(): 2-4 floats or ints expected (optionally in a sequence)\n");
} }
f=PyNumber_Float(v); f=PyNumber_Float(v);
if(f==NULL) { // parsed item not a number if(f==NULL) { // parsed item not a number
Py_DECREF(v); Py_DECREF(v);
@ -409,11 +411,12 @@ PyObject *M_Mathutils_Vector(PyObject * self, PyObject * args)
return EXPP_ReturnPyObjError(PyExc_TypeError, return EXPP_ReturnPyObjError(PyExc_TypeError,
"Mathutils.Vector(): 2-4 floats or ints expected (optionally in a sequence)\n"); "Mathutils.Vector(): 2-4 floats or ints expected (optionally in a sequence)\n");
} }
vec[i]=(float)PyFloat_AS_DOUBLE(f); vec[i]=(float)PyFloat_AS_DOUBLE(f);
EXPP_decr2(f,v); EXPP_decr2(f,v);
} }
Py_DECREF(listObject); Py_DECREF(listObject);
return (PyObject *) newVectorObject(vec, size, Py_NEW); return newVectorObject(vec, size, Py_NEW);
} }
//----------------------------------Mathutils.CrossVecs() --------------- //----------------------------------Mathutils.CrossVecs() ---------------
//finds perpendicular vector - only 3D is supported //finds perpendicular vector - only 3D is supported
@ -517,7 +520,7 @@ PyObject *M_Mathutils_MidpointVecs(PyObject * self, PyObject * args)
for(x = 0; x < vec1->size; x++) { for(x = 0; x < vec1->size; x++) {
vec[x] = 0.5f * (vec1->vec[x] + vec2->vec[x]); vec[x] = 0.5f * (vec1->vec[x] + vec2->vec[x]);
} }
return (PyObject *) newVectorObject(vec, vec1->size, Py_NEW); return newVectorObject(vec, vec1->size, Py_NEW);
} }
//----------------------------------Mathutils.ProjectVecs() ------------- //----------------------------------Mathutils.ProjectVecs() -------------
//projects vector 1 onto vector 2 //projects vector 1 onto vector 2
@ -548,7 +551,7 @@ PyObject *M_Mathutils_ProjectVecs(PyObject * self, PyObject * args)
for(x = 0; x < size; x++) { for(x = 0; x < size; x++) {
vec[x] = (float)(dot * vec2->vec[x]); vec[x] = (float)(dot * vec2->vec[x]);
} }
return (PyObject *) newVectorObject(vec, size, Py_NEW); return newVectorObject(vec, size, Py_NEW);
} }
//----------------------------------MATRIX FUNCTIONS-------------------- //----------------------------------MATRIX FUNCTIONS--------------------
//----------------------------------Mathutils.Matrix() ----------------- //----------------------------------Mathutils.Matrix() -----------------
@ -557,6 +560,8 @@ PyObject *M_Mathutils_ProjectVecs(PyObject * self, PyObject * args)
PyObject *M_Mathutils_Matrix(PyObject * self, PyObject * args) PyObject *M_Mathutils_Matrix(PyObject * self, PyObject * args)
{ {
PyObject *listObject = NULL; PyObject *listObject = NULL;
PyObject *argObject, *m, *s, *f;
MatrixObject *mat;
int argSize, seqSize = 0, i, j; int argSize, seqSize = 0, i, j;
float matrix[16] = {0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, float matrix[16] = {0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f,
0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 1.0f}; 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 1.0f};
@ -569,14 +574,12 @@ PyObject *M_Mathutils_Matrix(PyObject * self, PyObject * args)
return (PyObject *) newMatrixObject(NULL, 4, 4, Py_NEW); return (PyObject *) newMatrixObject(NULL, 4, 4, Py_NEW);
}else if (argSize == 1){ }else if (argSize == 1){
//copy constructor for matrix objects //copy constructor for matrix objects
PyObject *argObject;
argObject = PySequence_GetItem(args, 0); argObject = PySequence_GetItem(args, 0);
Py_INCREF(argObject);
if(MatrixObject_Check(argObject)){ if(MatrixObject_Check(argObject)){
MatrixObject *mat;
mat = (MatrixObject*)argObject; mat = (MatrixObject*)argObject;
argSize = mat->rowSize; //rows argSize = mat->rowSize; //rows
seqSize = mat->colSize; //cols seqSize = mat->colSize; //col
for(i = 0; i < (seqSize * argSize); i++){ for(i = 0; i < (seqSize * argSize); i++){
matrix[i] = mat->contigPtr[i]; matrix[i] = mat->contigPtr[i];
} }
@ -584,58 +587,54 @@ PyObject *M_Mathutils_Matrix(PyObject * self, PyObject * args)
Py_DECREF(argObject); Py_DECREF(argObject);
}else{ //2-4 arguments (all seqs? all same size?) }else{ //2-4 arguments (all seqs? all same size?)
for(i =0; i < argSize; i++){ for(i =0; i < argSize; i++){
PyObject *argObject;
argObject = PySequence_GetItem(args, i); argObject = PySequence_GetItem(args, i);
if (PySequence_Check(argObject)) { //seq? if (PySequence_Check(argObject)) { //seq?
if(seqSize){ //0 at first if(seqSize){ //0 at first
if(PySequence_Length(argObject) != seqSize){ //seq size not same if(PySequence_Length(argObject) != seqSize){ //seq size not same
Py_DECREF(argObject);
return EXPP_ReturnPyObjError(PyExc_AttributeError, return EXPP_ReturnPyObjError(PyExc_AttributeError,
"Mathutils.Matrix(): expects 0-4 numeric sequences of the same size\n"); "Mathutils.Matrix(): expects 0-4 numeric sequences of the same size\n");
} }
} }
seqSize = PySequence_Length(argObject); seqSize = PySequence_Length(argObject);
}else{ //arg not a sequence }else{ //arg not a sequence
Py_XDECREF(argObject);
return EXPP_ReturnPyObjError(PyExc_TypeError, return EXPP_ReturnPyObjError(PyExc_TypeError,
"Mathutils.Matrix(): expects 0-4 numeric sequences of the same size\n"); "Mathutils.Matrix(): expects 0-4 numeric sequences of the same size\n");
} }
Py_XDECREF(argObject); Py_DECREF(argObject);
} }
//all is well... let's continue parsing //all is well... let's continue parsing
listObject = EXPP_incr_ret(args); listObject = args;
for (i = 0; i < argSize; i++){ for (i = 0; i < argSize; i++){
PyObject *m;
m = PySequence_GetItem(listObject, i); m = PySequence_GetItem(listObject, i);
if (m == NULL) { // Failed to read sequence if (m == NULL) { // Failed to read sequence
Py_XDECREF(listObject);
return EXPP_ReturnPyObjError(PyExc_RuntimeError, return EXPP_ReturnPyObjError(PyExc_RuntimeError,
"Mathutils.Matrix(): failed to parse arguments...\n"); "Mathutils.Matrix(): failed to parse arguments...\n");
} }
for (j = 0; j < seqSize; j++) {
PyObject *s, *f;
for (j = 0; j < seqSize; j++) {
s = PySequence_GetItem(m, j); s = PySequence_GetItem(m, j);
if (s == NULL) { // Failed to read sequence if (s == NULL) { // Failed to read sequence
Py_DECREF(m); Py_DECREF(m);
Py_XDECREF(listObject);
return EXPP_ReturnPyObjError(PyExc_RuntimeError, return EXPP_ReturnPyObjError(PyExc_RuntimeError,
"Mathutils.Matrix(): failed to parse arguments...\n"); "Mathutils.Matrix(): failed to parse arguments...\n");
} }
f = PyNumber_Float(s); f = PyNumber_Float(s);
if(f == NULL) { // parsed item is not a number if(f == NULL) { // parsed item is not a number
EXPP_decr2(m,s); EXPP_decr2(m,s);
Py_XDECREF(listObject);
return EXPP_ReturnPyObjError(PyExc_AttributeError, return EXPP_ReturnPyObjError(PyExc_AttributeError,
"Mathutils.Matrix(): expects 0-4 numeric sequences of the same size\n"); "Mathutils.Matrix(): expects 0-4 numeric sequences of the same size\n");
} }
matrix[(seqSize*i)+j]=(float)PyFloat_AS_DOUBLE(f); matrix[(seqSize*i)+j]=(float)PyFloat_AS_DOUBLE(f);
EXPP_decr2(f,s); EXPP_decr2(f,s);
} }
Py_DECREF(m); Py_DECREF(m);
} }
Py_DECREF(listObject);
} }
return (PyObject *)newMatrixObject(matrix, argSize, seqSize, Py_NEW); return newMatrixObject(matrix, argSize, seqSize, Py_NEW);
} }
//----------------------------------Mathutils.RotationMatrix() ---------- //----------------------------------Mathutils.RotationMatrix() ----------
//mat is a 1D array of floats - row[0][0],row[0][1], row[1][0], etc. //mat is a 1D array of floats - row[0][0],row[0][1], row[1][0], etc.
@ -1033,14 +1032,14 @@ PyObject *M_Mathutils_Quaternion(PyObject * self, PyObject * args)
if ((size == 4 && PySequence_Length(args) !=1) || if ((size == 4 && PySequence_Length(args) !=1) ||
(size == 3 && PySequence_Length(args) !=2) || (size >4 || size < 3)) { (size == 3 && PySequence_Length(args) !=2) || (size >4 || size < 3)) {
// invalid args/size // invalid args/size
Py_XDECREF(listObject); Py_DECREF(listObject);
return EXPP_ReturnPyObjError(PyExc_AttributeError, return EXPP_ReturnPyObjError(PyExc_AttributeError,
"Mathutils.Quaternion(): 4d numeric sequence expected or 3d vector and number\n"); "Mathutils.Quaternion(): 4d numeric sequence expected or 3d vector and number\n");
} }
if(size == 3){ //get angle in axis/angle if(size == 3){ //get angle in axis/angle
n = PyNumber_Float(PySequence_GetItem(args, 1)); n = PyNumber_Float(PySequence_GetItem(args, 1));
if(n == NULL) { // parsed item not a number or getItem fail if(n == NULL) { // parsed item not a number or getItem fail
Py_XDECREF(listObject); Py_DECREF(listObject);
return EXPP_ReturnPyObjError(PyExc_TypeError, return EXPP_ReturnPyObjError(PyExc_TypeError,
"Mathutils.Quaternion(): 4d numeric sequence expected or 3d vector and number\n"); "Mathutils.Quaternion(): 4d numeric sequence expected or 3d vector and number\n");
} }
@ -1053,13 +1052,13 @@ PyObject *M_Mathutils_Quaternion(PyObject * self, PyObject * args)
size = PySequence_Length(listObject); size = PySequence_Length(listObject);
if (size != 3) { if (size != 3) {
// invalid args/size // invalid args/size
Py_XDECREF(listObject); Py_DECREF(listObject);
return EXPP_ReturnPyObjError(PyExc_AttributeError, return EXPP_ReturnPyObjError(PyExc_AttributeError,
"Mathutils.Quaternion(): 4d numeric sequence expected or 3d vector and number\n"); "Mathutils.Quaternion(): 4d numeric sequence expected or 3d vector and number\n");
} }
n = PyNumber_Float(PySequence_GetItem(args, 0)); n = PyNumber_Float(PySequence_GetItem(args, 0));
if(n == NULL) { // parsed item not a number or getItem fail if(n == NULL) { // parsed item not a number or getItem fail
Py_XDECREF(listObject); Py_DECREF(listObject);
return EXPP_ReturnPyObjError(PyExc_TypeError, return EXPP_ReturnPyObjError(PyExc_TypeError,
"Mathutils.Quaternion(): 4d numeric sequence expected or 3d vector and number\n"); "Mathutils.Quaternion(): 4d numeric sequence expected or 3d vector and number\n");
} }
@ -1072,38 +1071,40 @@ PyObject *M_Mathutils_Quaternion(PyObject * self, PyObject * args)
} }
} }
} else if (size == 0) { //returns a new empty quat } else if (size == 0) { //returns a new empty quat
return (PyObject *) newQuaternionObject(NULL, Py_NEW); return newQuaternionObject(NULL, Py_NEW);
} else { } else {
listObject = EXPP_incr_ret(args); listObject = EXPP_incr_ret(args);
} }
if (size == 3) { // invalid quat size if (size == 3) { // invalid quat size
if(PySequence_Length(args) != 2){ if(PySequence_Length(args) != 2){
Py_XDECREF(listObject); Py_DECREF(listObject);
return EXPP_ReturnPyObjError(PyExc_AttributeError, return EXPP_ReturnPyObjError(PyExc_AttributeError,
"Mathutils.Quaternion(): 4d numeric sequence expected or 3d vector and number\n"); "Mathutils.Quaternion(): 4d numeric sequence expected or 3d vector and number\n");
} }
}else{ }else{
if(size != 4){ if(size != 4){
Py_XDECREF(listObject); Py_DECREF(listObject);
return EXPP_ReturnPyObjError(PyExc_AttributeError, return EXPP_ReturnPyObjError(PyExc_AttributeError,
"Mathutils.Quaternion(): 4d numeric sequence expected or 3d vector and number\n"); "Mathutils.Quaternion(): 4d numeric sequence expected or 3d vector and number\n");
} }
} }
for (i=0; i<size; i++) { //parse for (i=0; i<size; i++) { //parse
q = PySequence_GetItem(listObject, i); q = PySequence_GetItem(listObject, i);
if (q == NULL) { // Failed to read sequence if (q == NULL) { // Failed to read sequence
Py_XDECREF(listObject); Py_DECREF(listObject);
return EXPP_ReturnPyObjError(PyExc_RuntimeError, return EXPP_ReturnPyObjError(PyExc_RuntimeError,
"Mathutils.Quaternion(): 4d numeric sequence expected or 3d vector and number\n"); "Mathutils.Quaternion(): 4d numeric sequence expected or 3d vector and number\n");
} }
f = PyNumber_Float(q); f = PyNumber_Float(q);
if(f == NULL) { // parsed item not a number if(f == NULL) { // parsed item not a number
Py_DECREF(q); EXPP_decr2(q, listObject);
Py_XDECREF(listObject);
return EXPP_ReturnPyObjError(PyExc_TypeError, return EXPP_ReturnPyObjError(PyExc_TypeError,
"Mathutils.Quaternion(): 4d numeric sequence expected or 3d vector and number\n"); "Mathutils.Quaternion(): 4d numeric sequence expected or 3d vector and number\n");
} }
quat[i] = (float)PyFloat_AS_DOUBLE(f); quat[i] = (float)PyFloat_AS_DOUBLE(f);
EXPP_decr2(f, q); EXPP_decr2(f, q);
} }
@ -1119,8 +1120,9 @@ PyObject *M_Mathutils_Quaternion(PyObject * self, PyObject * args)
quat[1] =(float) (sin(angle/ 2.0f)) * quat[0]; quat[1] =(float) (sin(angle/ 2.0f)) * quat[0];
quat[0] =(float) (cos(angle/ 2.0f)); quat[0] =(float) (cos(angle/ 2.0f));
} }
Py_DECREF(listObject); Py_DECREF(listObject);
return (PyObject *) newQuaternionObject(quat, Py_NEW); return newQuaternionObject(quat, Py_NEW);
} }
//----------------------------------Mathutils.CrossQuats() ---------------- //----------------------------------Mathutils.CrossQuats() ----------------
//quaternion multiplication - associate not commutative //quaternion multiplication - associate not commutative
@ -1134,7 +1136,7 @@ PyObject *M_Mathutils_CrossQuats(PyObject * self, PyObject * args)
return EXPP_ReturnPyObjError(PyExc_TypeError,"Mathutils.CrossQuats(): expected Quaternion types"); return EXPP_ReturnPyObjError(PyExc_TypeError,"Mathutils.CrossQuats(): expected Quaternion types");
QuatMul(quat, quatU->quat, quatV->quat); QuatMul(quat, quatU->quat, quatV->quat);
return (PyObject*) newQuaternionObject(quat, Py_NEW); return newQuaternionObject(quat, Py_NEW);
} }
//----------------------------------Mathutils.DotQuats() ---------------- //----------------------------------Mathutils.DotQuats() ----------------
//returns the dot product of 2 quaternions //returns the dot product of 2 quaternions
@ -1178,7 +1180,7 @@ PyObject *M_Mathutils_DifferenceQuats(PyObject * self, PyObject * args)
tempQuat[x] /= (float)(dot * dot); tempQuat[x] /= (float)(dot * dot);
} }
QuatMul(quat, tempQuat, quatV->quat); QuatMul(quat, tempQuat, quatV->quat);
return (PyObject *) newQuaternionObject(quat, Py_NEW); return newQuaternionObject(quat, Py_NEW);
} }
//----------------------------------Mathutils.Slerp() ------------------ //----------------------------------Mathutils.Slerp() ------------------
//attemps to interpolate 2 quaternions and return the result //attemps to interpolate 2 quaternions and return the result
@ -1235,7 +1237,7 @@ PyObject *M_Mathutils_Slerp(PyObject * self, PyObject * args)
quat[2] = (float)(quat_u[2] * x + quat_v[2] * y); quat[2] = (float)(quat_u[2] * x + quat_v[2] * y);
quat[3] = (float)(quat_u[3] * x + quat_v[3] * y); quat[3] = (float)(quat_u[3] * x + quat_v[3] * y);
return (PyObject *) newQuaternionObject(quat, Py_NEW); return newQuaternionObject(quat, Py_NEW);
} }
//----------------------------------EULER FUNCTIONS---------------------- //----------------------------------EULER FUNCTIONS----------------------
//----------------------------------Mathutils.Euler() ------------------- //----------------------------------Mathutils.Euler() -------------------
@ -1246,6 +1248,7 @@ PyObject *M_Mathutils_Euler(PyObject * self, PyObject * args)
PyObject *listObject = NULL; PyObject *listObject = NULL;
int size, i; int size, i;
float eul[3]; float eul[3];
PyObject *e, *f;
size = PySequence_Length(args); size = PySequence_Length(args);
if (size == 1) { if (size == 1) {
@ -1253,52 +1256,49 @@ PyObject *M_Mathutils_Euler(PyObject * self, PyObject * args)
if (PySequence_Check(listObject)) { if (PySequence_Check(listObject)) {
size = PySequence_Length(listObject); size = PySequence_Length(listObject);
} else { // Single argument was not a sequence } else { // Single argument was not a sequence
Py_XDECREF(listObject); Py_DECREF(listObject);
return EXPP_ReturnPyObjError(PyExc_TypeError, return EXPP_ReturnPyObjError(PyExc_TypeError,
"Mathutils.Euler(): 3d numeric sequence expected\n"); "Mathutils.Euler(): 3d numeric sequence expected\n");
} }
} else if (size == 0) { } else if (size == 0) {
//returns a new empty 3d euler //returns a new empty 3d euler
return (PyObject *) newEulerObject(NULL, Py_NEW); return newEulerObject(NULL, Py_NEW);
} else { } else {
listObject = EXPP_incr_ret(args); listObject = EXPP_incr_ret(args);
} }
if (size != 3) { // Invalid euler size if (size != 3) { // Invalid euler size
Py_XDECREF(listObject); Py_DECREF(listObject);
return EXPP_ReturnPyObjError(PyExc_AttributeError, return EXPP_ReturnPyObjError(PyExc_AttributeError,
"Mathutils.Euler(): 3d numeric sequence expected\n"); "Mathutils.Euler(): 3d numeric sequence expected\n");
} }
for (i=0; i<size; i++) {
PyObject *e, *f;
for (i=0; i<size; i++) {
e = PySequence_GetItem(listObject, i); e = PySequence_GetItem(listObject, i);
if (e == NULL) { // Failed to read sequence if (e == NULL) { // Failed to read sequence
Py_XDECREF(listObject); Py_DECREF(listObject);
return EXPP_ReturnPyObjError(PyExc_RuntimeError, return EXPP_ReturnPyObjError(PyExc_RuntimeError,
"Mathutils.Euler(): 3d numeric sequence expected\n"); "Mathutils.Euler(): 3d numeric sequence expected\n");
} }
f = PyNumber_Float(e); f = PyNumber_Float(e);
if(f == NULL) { // parsed item not a number if(f == NULL) { // parsed item not a number
Py_DECREF(e); EXPP_decr2(e, listObject);
Py_XDECREF(listObject);
return EXPP_ReturnPyObjError(PyExc_TypeError, return EXPP_ReturnPyObjError(PyExc_TypeError,
"Mathutils.Euler(): 3d numeric sequence expected\n"); "Mathutils.Euler(): 3d numeric sequence expected\n");
} }
eul[i]=(float)PyFloat_AS_DOUBLE(f); eul[i]=(float)PyFloat_AS_DOUBLE(f);
EXPP_decr2(f,e); EXPP_decr2(f,e);
} }
Py_DECREF(listObject); Py_DECREF(listObject);
return (PyObject *) newEulerObject(eul, Py_NEW); return newEulerObject(eul, Py_NEW);
} }
//---------------------------------INTERSECTION FUNCTIONS-------------------- //---------------------------------INTERSECTION FUNCTIONS--------------------
//----------------------------------Mathutils.Intersect() ------------------- //----------------------------------Mathutils.Intersect() -------------------
PyObject *M_Mathutils_Intersect( PyObject * self, PyObject * args ) PyObject *M_Mathutils_Intersect( PyObject * self, PyObject * args )
{ {
VectorObject *ray; VectorObject *ray, *ray_off, *vec1, *vec2, *vec3;
VectorObject *ray_off;
VectorObject *vec1;
VectorObject *vec2;
VectorObject *vec3;
float dir[3], orig[3], v1[3], v2[3], v3[3], e1[3], e2[3], pvec[3], tvec[3], qvec[3]; float dir[3], orig[3], v1[3], v2[3], v3[3], e1[3], e2[3], pvec[3], tvec[3], qvec[3];
float det, inv_det, u, v, t; float det, inv_det, u, v, t;
int clip = 1; int clip = 1;
@ -1370,10 +1370,7 @@ PyObject *M_Mathutils_Intersect( PyObject * self, PyObject * args )
PyObject *M_Mathutils_LineIntersect( PyObject * self, PyObject * args ) PyObject *M_Mathutils_LineIntersect( PyObject * self, PyObject * args )
{ {
PyObject * tuple; PyObject * tuple;
VectorObject *vec1; VectorObject *vec1, *vec2, *vec3, *vec4;
VectorObject *vec2;
VectorObject *vec3;
VectorObject *vec4;
float v1[3], v2[3], v3[3], v4[3], i1[3], i2[3]; float v1[3], v2[3], v3[3], v4[3], i1[3], i2[3];
if( !PyArg_ParseTuple if( !PyArg_ParseTuple
@ -1525,9 +1522,7 @@ PyObject *M_Mathutils_QuadNormal( PyObject * self, PyObject * args )
//----------------------------Mathutils.TriangleNormal() ------------------- //----------------------------Mathutils.TriangleNormal() -------------------
PyObject *M_Mathutils_TriangleNormal( PyObject * self, PyObject * args ) PyObject *M_Mathutils_TriangleNormal( PyObject * self, PyObject * args )
{ {
VectorObject *vec1; VectorObject *vec1, *vec2, *vec3;
VectorObject *vec2;
VectorObject *vec3;
float v1[3], v2[3], v3[3], e1[3], e2[3], n[3]; float v1[3], v2[3], v3[3], e1[3], e2[3], n[3];
if( !PyArg_ParseTuple if( !PyArg_ParseTuple
@ -1560,9 +1555,7 @@ PyObject *M_Mathutils_TriangleNormal( PyObject * self, PyObject * args )
//----------------------------------Mathutils.TriangleArea() ------------------- //----------------------------------Mathutils.TriangleArea() -------------------
PyObject *M_Mathutils_TriangleArea( PyObject * self, PyObject * args ) PyObject *M_Mathutils_TriangleArea( PyObject * self, PyObject * args )
{ {
VectorObject *vec1; VectorObject *vec1, *vec2, *vec3;
VectorObject *vec2;
VectorObject *vec3;
float v1[3], v2[3], v3[3]; float v1[3], v2[3], v3[3];
if( !PyArg_ParseTuple if( !PyArg_ParseTuple
@ -1675,7 +1668,6 @@ PyObject *M_Mathutils_MatMultVec(PyObject * self, PyObject * args)
{ {
MatrixObject *mat = NULL; MatrixObject *mat = NULL;
VectorObject *vec = NULL; VectorObject *vec = NULL;
PyObject *retObj = NULL;
//get pyObjects //get pyObjects
if(!PyArg_ParseTuple(args, "O!O!", &matrix_Type, &mat, &vector_Type, &vec)) if(!PyArg_ParseTuple(args, "O!O!", &matrix_Type, &mat, &vector_Type, &vec))
@ -1683,14 +1675,7 @@ PyObject *M_Mathutils_MatMultVec(PyObject * self, PyObject * args)
"Mathutils.MatMultVec(): MatMultVec() expects a matrix and a vector object - in that order\n"); "Mathutils.MatMultVec(): MatMultVec() expects a matrix and a vector object - in that order\n");
printf("Mathutils.MatMultVec(): Deprecated: use matrix * vec to perform column vector multiplication\n"); printf("Mathutils.MatMultVec(): Deprecated: use matrix * vec to perform column vector multiplication\n");
EXPP_incr2((PyObject*)vec, (PyObject*)mat); return column_vector_multiplication(mat, vec);
retObj = column_vector_multiplication(mat, vec);
if(!retObj){
return NULL;
}
EXPP_decr2((PyObject*)vec, (PyObject*)mat);
return retObj;
} }
//----------------------------------Mathutils.VecMultMat() --------------- //----------------------------------Mathutils.VecMultMat() ---------------
//ROW VECTOR Multiplication - Vector X Matrix //ROW VECTOR Multiplication - Vector X Matrix
@ -1698,7 +1683,6 @@ PyObject *M_Mathutils_VecMultMat(PyObject * self, PyObject * args)
{ {
MatrixObject *mat = NULL; MatrixObject *mat = NULL;
VectorObject *vec = NULL; VectorObject *vec = NULL;
PyObject *retObj = NULL;
//get pyObjects //get pyObjects
if(!PyArg_ParseTuple(args, "O!O!", &vector_Type, &vec, &matrix_Type, &mat)) if(!PyArg_ParseTuple(args, "O!O!", &vector_Type, &vec, &matrix_Type, &mat))
@ -1706,14 +1690,7 @@ PyObject *M_Mathutils_VecMultMat(PyObject * self, PyObject * args)
"Mathutils.VecMultMat(): VecMultMat() expects a vector and matrix object - in that order\n"); "Mathutils.VecMultMat(): VecMultMat() expects a vector and matrix object - in that order\n");
printf("Mathutils.VecMultMat(): Deprecated: use vec * matrix to perform row vector multiplication\n"); printf("Mathutils.VecMultMat(): Deprecated: use vec * matrix to perform row vector multiplication\n");
EXPP_incr2((PyObject*)vec, (PyObject*)mat); return row_vector_multiplication(vec, mat);
retObj = row_vector_multiplication(vec, mat);
if(!retObj){
return NULL;
}
EXPP_decr2((PyObject*)vec, (PyObject*)mat);
return retObj;
} }
//####################################################################### //#######################################################################
//#############################DEPRECATED################################ //#############################DEPRECATED################################

@ -57,15 +57,14 @@ struct PyMethodDef Euler_methods[] = {
//return a quaternion representation of the euler //return a quaternion representation of the euler
PyObject *Euler_ToQuat(EulerObject * self) PyObject *Euler_ToQuat(EulerObject * self)
{ {
float eul[3]; float eul[3], quat[4];
float quat[4];
int x; int x;
for(x = 0; x < 3; x++) { for(x = 0; x < 3; x++) {
eul[x] = self->eul[x] * ((float)Py_PI / 180); eul[x] = self->eul[x] * ((float)Py_PI / 180);
} }
EulToQuat(eul, quat); EulToQuat(eul, quat);
return (PyObject *) newQuaternionObject(quat, Py_NEW); return newQuaternionObject(quat, Py_NEW);
} }
//----------------------------Euler.toMatrix()--------------------- //----------------------------Euler.toMatrix()---------------------
//return a matrix representation of the euler //return a matrix representation of the euler
@ -79,7 +78,7 @@ PyObject *Euler_ToMatrix(EulerObject * self)
eul[x] = self->eul[x] * ((float)Py_PI / 180); eul[x] = self->eul[x] * ((float)Py_PI / 180);
} }
EulToMat3(eul, (float (*)[3]) mat); EulToMat3(eul, (float (*)[3]) mat);
return (PyObject *) newMatrixObject(mat, 3, 3 , Py_NEW); return newMatrixObject(mat, 3, 3 , Py_NEW);
} }
//----------------------------Euler.unique()----------------------- //----------------------------Euler.unique()-----------------------
//sets the x,y,z values to a unique euler rotation //sets the x,y,z values to a unique euler rotation
@ -247,7 +246,7 @@ static PyObject *Euler_repr(EulerObject * self)
} }
strcat(str, "](euler)"); strcat(str, "](euler)");
return EXPP_incr_ret(PyString_FromString(str)); return PyString_FromString(str);
} }
//---------------------SEQUENCE PROTOCOLS------------------------ //---------------------SEQUENCE PROTOCOLS------------------------
//----------------------------len(object)------------------------ //----------------------------len(object)------------------------
@ -314,6 +313,7 @@ static int Euler_ass_slice(EulerObject * self, int begin, int end,
{ {
int i, y, size = 0; int i, y, size = 0;
float eul[3]; float eul[3];
PyObject *e, *f;
CLAMP(begin, 0, 3); CLAMP(begin, 0, 3);
CLAMP(end, 0, 3); CLAMP(end, 0, 3);
@ -326,19 +326,19 @@ static int Euler_ass_slice(EulerObject * self, int begin, int end,
} }
for (i = 0; i < size; i++) { for (i = 0; i < size; i++) {
PyObject *e, *f;
e = PySequence_GetItem(seq, i); e = PySequence_GetItem(seq, i);
if (e == NULL) { // Failed to read sequence if (e == NULL) { // Failed to read sequence
return EXPP_ReturnIntError(PyExc_RuntimeError, return EXPP_ReturnIntError(PyExc_RuntimeError,
"euler[begin:end] = []: unable to read sequence\n"); "euler[begin:end] = []: unable to read sequence\n");
} }
f = PyNumber_Float(e); f = PyNumber_Float(e);
if(f == NULL) { // parsed item not a number if(f == NULL) { // parsed item not a number
Py_DECREF(e); Py_DECREF(e);
return EXPP_ReturnIntError(PyExc_TypeError, return EXPP_ReturnIntError(PyExc_TypeError,
"euler[begin:end] = []: sequence argument not a number\n"); "euler[begin:end] = []: sequence argument not a number\n");
} }
eul[i] = (float)PyFloat_AS_DOUBLE(f); eul[i] = (float)PyFloat_AS_DOUBLE(f);
EXPP_decr2(f,e); EXPP_decr2(f,e);
} }

@ -77,7 +77,7 @@ PyObject *Matrix_toQuat(MatrixObject * self)
Mat4ToQuat((float (*)[4])*self->matrix, quat); Mat4ToQuat((float (*)[4])*self->matrix, quat);
} }
return (PyObject *) newQuaternionObject(quat, Py_NEW); return newQuaternionObject(quat, Py_NEW);
} }
//---------------------------Matrix.toEuler() -------------------- //---------------------------Matrix.toEuler() --------------------
PyObject *Matrix_toEuler(MatrixObject * self) PyObject *Matrix_toEuler(MatrixObject * self)
@ -95,7 +95,7 @@ PyObject *Matrix_toEuler(MatrixObject * self)
for(x = 0; x < 3; x++) { for(x = 0; x < 3; x++) {
eul[x] *= (float) (180 / Py_PI); eul[x] *= (float) (180 / Py_PI);
} }
return (PyObject *) newEulerObject(eul, Py_NEW); return newEulerObject(eul, Py_NEW);
} }
//---------------------------Matrix.resize4x4() ------------------ //---------------------------Matrix.resize4x4() ------------------
PyObject *Matrix_Resize4x4(MatrixObject * self) PyObject *Matrix_Resize4x4(MatrixObject * self)
@ -372,7 +372,7 @@ static PyObject *Matrix_repr(MatrixObject * self)
} }
} }
return EXPP_incr_ret(PyString_FromString(str)); return PyString_FromString(str);
} }
//---------------------SEQUENCE PROTOCOLS------------------------ //---------------------SEQUENCE PROTOCOLS------------------------
@ -399,6 +399,7 @@ static int Matrix_ass_item(MatrixObject * self, int i, PyObject * ob)
{ {
int y, x, size = 0; int y, x, size = 0;
float vec[4]; float vec[4];
PyObject *m, *f;
if(i > self->rowSize || i < 0){ if(i > self->rowSize || i < 0){
return EXPP_ReturnIntError(PyExc_TypeError, return EXPP_ReturnIntError(PyExc_TypeError,
@ -412,19 +413,19 @@ static int Matrix_ass_item(MatrixObject * self, int i, PyObject * ob)
"matrix[attribute] = x: bad sequence size\n"); "matrix[attribute] = x: bad sequence size\n");
} }
for (x = 0; x < size; x++) { for (x = 0; x < size; x++) {
PyObject *m, *f;
m = PySequence_GetItem(ob, x); m = PySequence_GetItem(ob, x);
if (m == NULL) { // Failed to read sequence if (m == NULL) { // Failed to read sequence
return EXPP_ReturnIntError(PyExc_RuntimeError, return EXPP_ReturnIntError(PyExc_RuntimeError,
"matrix[attribute] = x: unable to read sequence\n"); "matrix[attribute] = x: unable to read sequence\n");
} }
f = PyNumber_Float(m); f = PyNumber_Float(m);
if(f == NULL) { // parsed item not a number if(f == NULL) { // parsed item not a number
Py_DECREF(m); Py_DECREF(m);
return EXPP_ReturnIntError(PyExc_TypeError, return EXPP_ReturnIntError(PyExc_TypeError,
"matrix[attribute] = x: sequence argument not a number\n"); "matrix[attribute] = x: sequence argument not a number\n");
} }
vec[x] = (float)PyFloat_AS_DOUBLE(f); vec[x] = (float)PyFloat_AS_DOUBLE(f);
EXPP_decr2(m, f); EXPP_decr2(m, f);
} }
@ -456,7 +457,7 @@ static PyObject *Matrix_slice(MatrixObject * self, int begin, int end)
newVectorObject(self->matrix[count], self->colSize, Py_WRAP)); newVectorObject(self->matrix[count], self->colSize, Py_WRAP));
} }
return EXPP_incr_ret(list); return list;
} }
//----------------------------object[z:y]------------------------ //----------------------------object[z:y]------------------------
//sequence slice (set) //sequence slice (set)
@ -465,6 +466,8 @@ static int Matrix_ass_slice(MatrixObject * self, int begin, int end,
{ {
int i, x, y, size, sub_size = 0; int i, x, y, size, sub_size = 0;
float mat[16]; float mat[16];
PyObject *subseq;
PyObject *m, *f;
CLAMP(begin, 0, self->rowSize); CLAMP(begin, 0, self->rowSize);
CLAMP(end, 0, self->rowSize); CLAMP(end, 0, self->rowSize);
@ -479,32 +482,35 @@ static int Matrix_ass_slice(MatrixObject * self, int begin, int end,
//parse sub items //parse sub items
for (i = 0; i < size; i++) { for (i = 0; i < size; i++) {
//parse each sub sequence //parse each sub sequence
PyObject *subseq;
subseq = PySequence_GetItem(seq, i); subseq = PySequence_GetItem(seq, i);
if (subseq == NULL) { // Failed to read sequence if (subseq == NULL) { // Failed to read sequence
return EXPP_ReturnIntError(PyExc_RuntimeError, return EXPP_ReturnIntError(PyExc_RuntimeError,
"matrix[begin:end] = []: unable to read sequence\n"); "matrix[begin:end] = []: unable to read sequence\n");
} }
if(PySequence_Check(subseq)){ if(PySequence_Check(subseq)){
//subsequence is also a sequence //subsequence is also a sequence
sub_size = PySequence_Length(subseq); sub_size = PySequence_Length(subseq);
if(sub_size != self->colSize){ if(sub_size != self->colSize){
Py_DECREF(subseq);
return EXPP_ReturnIntError(PyExc_TypeError, return EXPP_ReturnIntError(PyExc_TypeError,
"matrix[begin:end] = []: size mismatch in slice assignment\n"); "matrix[begin:end] = []: size mismatch in slice assignment\n");
} }
for (y = 0; y < sub_size; y++) { for (y = 0; y < sub_size; y++) {
PyObject *m, *f;
m = PySequence_GetItem(subseq, y); m = PySequence_GetItem(subseq, y);
if (m == NULL) { // Failed to read sequence if (m == NULL) { // Failed to read sequence
Py_DECREF(subseq);
return EXPP_ReturnIntError(PyExc_RuntimeError, return EXPP_ReturnIntError(PyExc_RuntimeError,
"matrix[begin:end] = []: unable to read sequence\n"); "matrix[begin:end] = []: unable to read sequence\n");
} }
f = PyNumber_Float(m); f = PyNumber_Float(m);
if(f == NULL) { // parsed item not a number if(f == NULL) { // parsed item not a number
Py_DECREF(m); EXPP_decr2(m, subseq);
return EXPP_ReturnIntError(PyExc_TypeError, return EXPP_ReturnIntError(PyExc_TypeError,
"matrix[begin:end] = []: sequence argument not a number\n"); "matrix[begin:end] = []: sequence argument not a number\n");
} }
mat[(i * self->colSize) + y] = (float)PyFloat_AS_DOUBLE(f); mat[(i * self->colSize) + y] = (float)PyFloat_AS_DOUBLE(f);
EXPP_decr2(f, m); EXPP_decr2(f, m);
} }
@ -513,6 +519,7 @@ static int Matrix_ass_slice(MatrixObject * self, int begin, int end,
return EXPP_ReturnIntError(PyExc_TypeError, return EXPP_ReturnIntError(PyExc_TypeError,
"matrix[begin:end] = []: illegal argument type for built-in operation\n"); "matrix[begin:end] = []: illegal argument type for built-in operation\n");
} }
Py_DECREF(subseq);
} }
//parsed well - now set in matrix //parsed well - now set in matrix
for(x = 0; x < (size * sub_size); x++){ for(x = 0; x < (size * sub_size); x++){
@ -533,7 +540,6 @@ static PyObject *Matrix_add(PyObject * m1, PyObject * m2)
0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 1.0f}; 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 1.0f};
MatrixObject *mat1 = NULL, *mat2 = NULL; MatrixObject *mat1 = NULL, *mat2 = NULL;
EXPP_incr2(m1, m2);
mat1 = (MatrixObject*)m1; mat1 = (MatrixObject*)m1;
mat2 = (MatrixObject*)m2; mat2 = (MatrixObject*)m2;
@ -542,7 +548,6 @@ static PyObject *Matrix_add(PyObject * m1, PyObject * m2)
"Matrix addition: arguments not valid for this operation....\n"); "Matrix addition: arguments not valid for this operation....\n");
} }
if(mat1->rowSize != mat2->rowSize || mat1->colSize != mat2->colSize){ if(mat1->rowSize != mat2->rowSize || mat1->colSize != mat2->colSize){
EXPP_decr2((PyObject*)mat1, (PyObject*)mat2);
return EXPP_ReturnPyObjError(PyExc_AttributeError, return EXPP_ReturnPyObjError(PyExc_AttributeError,
"Matrix addition: matrices must have the same dimensions for this operation\n"); "Matrix addition: matrices must have the same dimensions for this operation\n");
} }
@ -553,7 +558,6 @@ static PyObject *Matrix_add(PyObject * m1, PyObject * m2)
} }
} }
EXPP_decr2((PyObject*)mat1, (PyObject*)mat2);
return newMatrixObject(mat, mat1->rowSize, mat1->colSize, Py_NEW); return newMatrixObject(mat, mat1->rowSize, mat1->colSize, Py_NEW);
} }
//------------------------obj - obj------------------------------ //------------------------obj - obj------------------------------
@ -565,7 +569,6 @@ static PyObject *Matrix_sub(PyObject * m1, PyObject * m2)
0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 1.0f}; 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 1.0f};
MatrixObject *mat1 = NULL, *mat2 = NULL; MatrixObject *mat1 = NULL, *mat2 = NULL;
EXPP_incr2(m1, m2);
mat1 = (MatrixObject*)m1; mat1 = (MatrixObject*)m1;
mat2 = (MatrixObject*)m2; mat2 = (MatrixObject*)m2;
@ -574,7 +577,6 @@ static PyObject *Matrix_sub(PyObject * m1, PyObject * m2)
"Matrix addition: arguments not valid for this operation....\n"); "Matrix addition: arguments not valid for this operation....\n");
} }
if(mat1->rowSize != mat2->rowSize || mat1->colSize != mat2->colSize){ if(mat1->rowSize != mat2->rowSize || mat1->colSize != mat2->colSize){
EXPP_decr2((PyObject*)mat1, (PyObject*)mat2);
return EXPP_ReturnPyObjError(PyExc_AttributeError, return EXPP_ReturnPyObjError(PyExc_AttributeError,
"Matrix addition: matrices must have the same dimensions for this operation\n"); "Matrix addition: matrices must have the same dimensions for this operation\n");
} }
@ -585,7 +587,6 @@ static PyObject *Matrix_sub(PyObject * m1, PyObject * m2)
} }
} }
EXPP_decr2((PyObject*)mat1, (PyObject*)mat2);
return newMatrixObject(mat, mat1->rowSize, mat1->colSize, Py_NEW); return newMatrixObject(mat, mat1->rowSize, mat1->colSize, Py_NEW);
} }
//------------------------obj * obj------------------------------ //------------------------obj * obj------------------------------
@ -598,11 +599,10 @@ static PyObject *Matrix_mul(PyObject * m1, PyObject * m2)
0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 1.0f}; 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 1.0f};
double dot = 0.0f; double dot = 0.0f;
MatrixObject *mat1 = NULL, *mat2 = NULL; MatrixObject *mat1 = NULL, *mat2 = NULL;
PyObject *f = NULL, *retObj = NULL; PyObject *f = NULL;
VectorObject *vec = NULL; VectorObject *vec = NULL;
PointObject *pt = NULL; PointObject *pt = NULL;
EXPP_incr2(m1, m2);
mat1 = (MatrixObject*)m1; mat1 = (MatrixObject*)m1;
mat2 = (MatrixObject*)m2; mat2 = (MatrixObject*)m2;
@ -611,51 +611,44 @@ static PyObject *Matrix_mul(PyObject * m1, PyObject * m2)
PyInt_Check(mat1->coerced_object)){ // FLOAT/INT * MATRIX PyInt_Check(mat1->coerced_object)){ // FLOAT/INT * MATRIX
f = PyNumber_Float(mat1->coerced_object); f = PyNumber_Float(mat1->coerced_object);
if(f == NULL) { // parsed item not a number if(f == NULL) { // parsed item not a number
EXPP_decr2((PyObject*)mat1, (PyObject*)mat2);
return EXPP_ReturnPyObjError(PyExc_TypeError, return EXPP_ReturnPyObjError(PyExc_TypeError,
"Matrix multiplication: arguments not acceptable for this operation\n"); "Matrix multiplication: arguments not acceptable for this operation\n");
} }
scalar = (float)PyFloat_AS_DOUBLE(f); scalar = (float)PyFloat_AS_DOUBLE(f);
for(x = 0; x < mat2->rowSize; x++) { for(x = 0; x < mat2->rowSize; x++) {
for(y = 0; y < mat2->colSize; y++) { for(y = 0; y < mat2->colSize; y++) {
mat[((x * mat2->colSize) + y)] = scalar * mat2->matrix[x][y]; mat[((x * mat2->colSize) + y)] = scalar * mat2->matrix[x][y];
} }
} }
EXPP_decr2((PyObject*)mat1, (PyObject*)mat2);
return newMatrixObject(mat, mat2->rowSize, mat2->colSize, Py_NEW); return newMatrixObject(mat, mat2->rowSize, mat2->colSize, Py_NEW);
} }
}else{ }else{
if(mat2->coerced_object){ if(mat2->coerced_object){
if(VectorObject_Check(mat2->coerced_object)){ //MATRIX * VECTOR if(VectorObject_Check(mat2->coerced_object)){ //MATRIX * VECTOR
vec = (VectorObject*)EXPP_incr_ret(mat2->coerced_object); vec = (VectorObject*)mat2->coerced_object;
retObj = column_vector_multiplication(mat1, vec); return column_vector_multiplication(mat1, vec);
EXPP_decr3((PyObject*)mat1, (PyObject*)mat2, (PyObject*)vec);
return retObj;
}else if(PointObject_Check(mat2->coerced_object)){ //MATRIX * POINT }else if(PointObject_Check(mat2->coerced_object)){ //MATRIX * POINT
pt = (PointObject*)EXPP_incr_ret(mat2->coerced_object); pt = (PointObject*)mat2->coerced_object;
retObj = column_point_multiplication(mat1, pt); return column_point_multiplication(mat1, pt);
EXPP_decr3((PyObject*)mat1, (PyObject*)mat2, (PyObject*)pt);
return retObj;
}else if (PyFloat_Check(mat2->coerced_object) || }else if (PyFloat_Check(mat2->coerced_object) ||
PyInt_Check(mat2->coerced_object)){ // MATRIX * FLOAT/INT PyInt_Check(mat2->coerced_object)){ // MATRIX * FLOAT/INT
f = PyNumber_Float(mat2->coerced_object); f = PyNumber_Float(mat2->coerced_object);
if(f == NULL) { // parsed item not a number if(f == NULL) { // parsed item not a number
EXPP_decr2((PyObject*)mat1, (PyObject*)mat2);
return EXPP_ReturnPyObjError(PyExc_TypeError, return EXPP_ReturnPyObjError(PyExc_TypeError,
"Matrix multiplication: arguments not acceptable for this operation\n"); "Matrix multiplication: arguments not acceptable for this operation\n");
} }
scalar = (float)PyFloat_AS_DOUBLE(f); scalar = (float)PyFloat_AS_DOUBLE(f);
for(x = 0; x < mat1->rowSize; x++) { for(x = 0; x < mat1->rowSize; x++) {
for(y = 0; y < mat1->colSize; y++) { for(y = 0; y < mat1->colSize; y++) {
mat[((x * mat1->colSize) + y)] = scalar * mat1->matrix[x][y]; mat[((x * mat1->colSize) + y)] = scalar * mat1->matrix[x][y];
} }
} }
EXPP_decr2((PyObject*)mat1, (PyObject*)mat2);
return newMatrixObject(mat, mat1->rowSize, mat1->colSize, Py_NEW); return newMatrixObject(mat, mat1->rowSize, mat1->colSize, Py_NEW);
} }
}else{ //MATRIX * MATRIX }else{ //MATRIX * MATRIX
if(mat1->colSize != mat2->rowSize){ if(mat1->colSize != mat2->rowSize){
EXPP_decr2((PyObject*)mat1, (PyObject*)mat2);
return EXPP_ReturnPyObjError(PyExc_AttributeError, return EXPP_ReturnPyObjError(PyExc_AttributeError,
"Matrix multiplication: matrix A rowsize must equal matrix B colsize\n"); "Matrix multiplication: matrix A rowsize must equal matrix B colsize\n");
} }
@ -672,7 +665,6 @@ static PyObject *Matrix_mul(PyObject * m1, PyObject * m2)
} }
} }
EXPP_decr2((PyObject*)mat1, (PyObject*)mat2);
return EXPP_ReturnPyObjError(PyExc_TypeError, return EXPP_ReturnPyObjError(PyExc_TypeError,
"Matrix multiplication: arguments not acceptable for this operation\n"); "Matrix multiplication: arguments not acceptable for this operation\n");
} }
@ -702,8 +694,7 @@ static int Matrix_coerce(PyObject ** m1, PyObject ** m2)
"matrix.coerce(): unknown operand - can't coerce for numeric protocols\n"); "matrix.coerce(): unknown operand - can't coerce for numeric protocols\n");
} }
} }
Py_INCREF(*m2); EXPP_incr2(*m1, *m2);
Py_INCREF(*m1);
return 0; return 0;
} }
//-----------------PROTCOL DECLARATIONS-------------------------- //-----------------PROTCOL DECLARATIONS--------------------------

@ -56,7 +56,7 @@ PyObject *Point_toVector(PointObject * self)
vec[x] = self->coord[x]; vec[x] = self->coord[x];
} }
return (PyObject *) newVectorObject(vec, self->size, Py_NEW); return newVectorObject(vec, self->size, Py_NEW);
} }
//----------------------------Point.zero() ---------------------- //----------------------------Point.zero() ----------------------
//set the point data to 0,0,0 //set the point data to 0,0,0
@ -72,6 +72,7 @@ PyObject *Point_Zero(PointObject * self)
//free the py_object //free the py_object
static void Point_dealloc(PointObject * self) static void Point_dealloc(PointObject * self)
{ {
Py_XDECREF(self->coerced_object);
//only free py_data //only free py_data
if(self->data.py_data){ if(self->data.py_data){
PyMem_Free(self->data.py_data); PyMem_Free(self->data.py_data);
@ -154,7 +155,7 @@ static PyObject *Point_repr(PointObject * self)
} }
strcat(str, "](point)"); strcat(str, "](point)");
return EXPP_incr_ret(PyString_FromString(str)); return PyString_FromString(str);
} }
//---------------------SEQUENCE PROTOCOLS------------------------ //---------------------SEQUENCE PROTOCOLS------------------------
//----------------------------len(object)------------------------ //----------------------------len(object)------------------------
@ -221,6 +222,7 @@ static int Point_ass_slice(PointObject * self, int begin, int end,
{ {
int i, y, size = 0; int i, y, size = 0;
float coord[3]; float coord[3];
PyObject *v, *f;
CLAMP(begin, 0, self->size); CLAMP(begin, 0, self->size);
CLAMP(end, 0, self->size); CLAMP(end, 0, self->size);
@ -233,8 +235,6 @@ static int Point_ass_slice(PointObject * self, int begin, int end,
} }
for (i = 0; i < size; i++) { for (i = 0; i < size; i++) {
PyObject *v, *f;
v = PySequence_GetItem(seq, i); v = PySequence_GetItem(seq, i);
if (v == NULL) { // Failed to read sequence if (v == NULL) { // Failed to read sequence
return EXPP_ReturnIntError(PyExc_RuntimeError, return EXPP_ReturnIntError(PyExc_RuntimeError,
@ -246,6 +246,7 @@ static int Point_ass_slice(PointObject * self, int begin, int end,
return EXPP_ReturnIntError(PyExc_TypeError, return EXPP_ReturnIntError(PyExc_TypeError,
"point[begin:end] = []: sequence argument not a number\n"); "point[begin:end] = []: sequence argument not a number\n");
} }
coord[i] = (float)PyFloat_AS_DOUBLE(f); coord[i] = (float)PyFloat_AS_DOUBLE(f);
EXPP_decr2(f,v); EXPP_decr2(f,v);
} }
@ -265,7 +266,6 @@ static PyObject *Point_add(PyObject * v1, PyObject * v2)
PointObject *coord1 = NULL, *coord2 = NULL; PointObject *coord1 = NULL, *coord2 = NULL;
VectorObject *vec = NULL; VectorObject *vec = NULL;
EXPP_incr2(v1, v2);
coord1 = (PointObject*)v1; coord1 = (PointObject*)v1;
coord2 = (PointObject*)v2; coord2 = (PointObject*)v2;
@ -273,19 +273,17 @@ static PyObject *Point_add(PyObject * v1, PyObject * v2)
if(coord2->coerced_object){ if(coord2->coerced_object){
if(VectorObject_Check(coord2->coerced_object)){ //POINT + VECTOR if(VectorObject_Check(coord2->coerced_object)){ //POINT + VECTOR
//Point translation //Point translation
vec = (VectorObject*)EXPP_incr_ret(coord2->coerced_object); vec = (VectorObject*)coord2->coerced_object;
size = coord1->size; size = coord1->size;
if(vec->size == size){ if(vec->size == size){
for(x = 0; x < size; x++){ for(x = 0; x < size; x++){
coord[x] = coord1->coord[x] + vec->vec[x]; coord[x] = coord1->coord[x] + vec->vec[x];
} }
}else{ }else{
EXPP_decr3((PyObject*)coord1, (PyObject*)coord2, (PyObject*)vec);
return EXPP_ReturnPyObjError(PyExc_AttributeError, return EXPP_ReturnPyObjError(PyExc_AttributeError,
"Point addition: arguments are the wrong size....\n"); "Point addition: arguments are the wrong size....\n");
} }
EXPP_decr3((PyObject*)coord1, (PyObject*)coord2, (PyObject*)vec); return newPointObject(coord, size, Py_NEW);
return (PyObject *) newPointObject(coord, size, Py_NEW);
} }
}else{ //POINT + POINT }else{ //POINT + POINT
size = coord1->size; size = coord1->size;
@ -294,16 +292,13 @@ static PyObject *Point_add(PyObject * v1, PyObject * v2)
coord[x] = coord1->coord[x] + coord2->coord[x]; coord[x] = coord1->coord[x] + coord2->coord[x];
} }
}else{ }else{
EXPP_decr2((PyObject*)coord1, (PyObject*)coord2);
return EXPP_ReturnPyObjError(PyExc_AttributeError, return EXPP_ReturnPyObjError(PyExc_AttributeError,
"Point addition: arguments are the wrong size....\n"); "Point addition: arguments are the wrong size....\n");
} }
EXPP_decr2((PyObject*)coord1, (PyObject*)coord2); return newPointObject(coord, size, Py_NEW);
return (PyObject *) newPointObject(coord, size, Py_NEW);
} }
} }
EXPP_decr2((PyObject*)coord1, (PyObject*)coord2);
return EXPP_ReturnPyObjError(PyExc_AttributeError, return EXPP_ReturnPyObjError(PyExc_AttributeError,
"Point addition: arguments not valid for this operation....\n"); "Point addition: arguments not valid for this operation....\n");
} }
@ -315,7 +310,6 @@ static PyObject *Point_sub(PyObject * v1, PyObject * v2)
float coord[3]; float coord[3];
PointObject *coord1 = NULL, *coord2 = NULL; PointObject *coord1 = NULL, *coord2 = NULL;
EXPP_incr2(v1, v2);
coord1 = (PointObject*)v1; coord1 = (PointObject*)v1;
coord2 = (PointObject*)v2; coord2 = (PointObject*)v2;
@ -324,7 +318,6 @@ static PyObject *Point_sub(PyObject * v1, PyObject * v2)
"Point subtraction: arguments not valid for this operation....\n"); "Point subtraction: arguments not valid for this operation....\n");
} }
if(coord1->size != coord2->size){ if(coord1->size != coord2->size){
EXPP_decr2((PyObject*)coord1, (PyObject*)coord2);
return EXPP_ReturnPyObjError(PyExc_AttributeError, return EXPP_ReturnPyObjError(PyExc_AttributeError,
"Point subtraction: points must have the same dimensions for this operation\n"); "Point subtraction: points must have the same dimensions for this operation\n");
} }
@ -335,8 +328,7 @@ static PyObject *Point_sub(PyObject * v1, PyObject * v2)
} }
//Point - Point = Vector //Point - Point = Vector
EXPP_decr2((PyObject*)coord1, (PyObject*)coord2); return newVectorObject(coord, size, Py_NEW);
return (PyObject *) newVectorObject(coord, size, Py_NEW);
} }
//------------------------obj * obj------------------------------ //------------------------obj * obj------------------------------
//mulplication //mulplication
@ -345,11 +337,10 @@ static PyObject *Point_mul(PyObject * p1, PyObject * p2)
int x, size; int x, size;
float coord[3], scalar; float coord[3], scalar;
PointObject *coord1 = NULL, *coord2 = NULL; PointObject *coord1 = NULL, *coord2 = NULL;
PyObject *f = NULL, *retObj = NULL; PyObject *f = NULL;
MatrixObject *mat = NULL; MatrixObject *mat = NULL;
QuaternionObject *quat = NULL; QuaternionObject *quat = NULL;
EXPP_incr2(p1, p2);
coord1 = (PointObject*)p1; coord1 = (PointObject*)p1;
coord2 = (PointObject*)p2; coord2 = (PointObject*)p2;
@ -358,17 +349,17 @@ static PyObject *Point_mul(PyObject * p1, PyObject * p2)
PyInt_Check(coord1->coerced_object)){ // FLOAT/INT * POINT PyInt_Check(coord1->coerced_object)){ // FLOAT/INT * POINT
f = PyNumber_Float(coord1->coerced_object); f = PyNumber_Float(coord1->coerced_object);
if(f == NULL) { // parsed item not a number if(f == NULL) { // parsed item not a number
EXPP_decr2((PyObject*)coord1, (PyObject*)coord2);
return EXPP_ReturnPyObjError(PyExc_TypeError, return EXPP_ReturnPyObjError(PyExc_TypeError,
"Point multiplication: arguments not acceptable for this operation\n"); "Point multiplication: arguments not acceptable for this operation\n");
} }
scalar = (float)PyFloat_AS_DOUBLE(f); scalar = (float)PyFloat_AS_DOUBLE(f);
size = coord2->size; size = coord2->size;
for(x = 0; x < size; x++) { for(x = 0; x < size; x++) {
coord[x] = coord2->coord[x] * scalar; coord[x] = coord2->coord[x] * scalar;
} }
EXPP_decr2((PyObject*)coord1, (PyObject*)coord2); Py_DECREF(f);
return (PyObject *) newPointObject(coord, size, Py_NEW); return newPointObject(coord, size, Py_NEW);
} }
}else{ }else{
if(coord2->coerced_object){ if(coord2->coerced_object){
@ -376,37 +367,31 @@ static PyObject *Point_mul(PyObject * p1, PyObject * p2)
PyInt_Check(coord2->coerced_object)){ // POINT * FLOAT/INT PyInt_Check(coord2->coerced_object)){ // POINT * FLOAT/INT
f = PyNumber_Float(coord2->coerced_object); f = PyNumber_Float(coord2->coerced_object);
if(f == NULL) { // parsed item not a number if(f == NULL) { // parsed item not a number
EXPP_decr2((PyObject*)coord1, (PyObject*)coord2);
return EXPP_ReturnPyObjError(PyExc_TypeError, return EXPP_ReturnPyObjError(PyExc_TypeError,
"Point multiplication: arguments not acceptable for this operation\n"); "Point multiplication: arguments not acceptable for this operation\n");
} }
scalar = (float)PyFloat_AS_DOUBLE(f); scalar = (float)PyFloat_AS_DOUBLE(f);
size = coord1->size; size = coord1->size;
for(x = 0; x < size; x++) { for(x = 0; x < size; x++) {
coord[x] = coord1->coord[x] * scalar; coord[x] = coord1->coord[x] * scalar;
} }
EXPP_decr2((PyObject*)coord1, (PyObject*)coord2); Py_DECREF(f);
return (PyObject *) newPointObject(coord, size, Py_NEW); return newPointObject(coord, size, Py_NEW);
}else if(MatrixObject_Check(coord2->coerced_object)){ //POINT * MATRIX }else if(MatrixObject_Check(coord2->coerced_object)){ //POINT * MATRIX
mat = (MatrixObject*)EXPP_incr_ret(coord2->coerced_object); mat = (MatrixObject*)coord2->coerced_object;
retObj = row_point_multiplication(coord1, mat); return row_point_multiplication(coord1, mat);
EXPP_decr3((PyObject*)coord1, (PyObject*)coord2, (PyObject*)mat);
return retObj;
}else if(QuaternionObject_Check(coord2->coerced_object)){ //POINT * QUATERNION }else if(QuaternionObject_Check(coord2->coerced_object)){ //POINT * QUATERNION
quat = (QuaternionObject*)EXPP_incr_ret(coord2->coerced_object); quat = (QuaternionObject*)coord2->coerced_object;
if(coord1->size != 3){ if(coord1->size != 3){
EXPP_decr2((PyObject*)coord1, (PyObject*)coord2);
return EXPP_ReturnPyObjError(PyExc_TypeError, return EXPP_ReturnPyObjError(PyExc_TypeError,
"Point multiplication: only 3D point rotations (with quats) currently supported\n"); "Point multiplication: only 3D point rotations (with quats) currently supported\n");
} }
retObj = quat_rotation((PyObject*)coord1, (PyObject*)quat); return quat_rotation((PyObject*)coord1, (PyObject*)quat);
EXPP_decr3((PyObject*)coord1, (PyObject*)coord2, (PyObject*)quat);
return retObj;
} }
} }
} }
EXPP_decr2((PyObject*)coord1, (PyObject*)coord2);
return EXPP_ReturnPyObjError(PyExc_TypeError, return EXPP_ReturnPyObjError(PyExc_TypeError,
"Point multiplication: arguments not acceptable for this operation\n"); "Point multiplication: arguments not acceptable for this operation\n");
} }

@ -141,6 +141,7 @@ PyObject *Quaternion_Conjugate(QuaternionObject * self)
//free the py_object //free the py_object
static void Quaternion_dealloc(QuaternionObject * self) static void Quaternion_dealloc(QuaternionObject * self)
{ {
Py_XDECREF(self->coerced_object);
//only free py_data //only free py_data
if(self->data.py_data){ if(self->data.py_data){
PyMem_Free(self->data.py_data); PyMem_Free(self->data.py_data);
@ -244,7 +245,7 @@ static PyObject *Quaternion_repr(QuaternionObject * self)
} }
strcat(str, "](quaternion)"); strcat(str, "](quaternion)");
return EXPP_incr_ret(PyString_FromString(str)); return PyString_FromString(str);
} }
//---------------------SEQUENCE PROTOCOLS------------------------ //---------------------SEQUENCE PROTOCOLS------------------------
//----------------------------len(object)------------------------ //----------------------------len(object)------------------------
@ -311,6 +312,7 @@ static int Quaternion_ass_slice(QuaternionObject * self, int begin, int end,
{ {
int i, y, size = 0; int i, y, size = 0;
float quat[4]; float quat[4];
PyObject *q, *f;
CLAMP(begin, 0, 4); CLAMP(begin, 0, 4);
CLAMP(end, 0, 4); CLAMP(end, 0, 4);
@ -323,19 +325,19 @@ static int Quaternion_ass_slice(QuaternionObject * self, int begin, int end,
} }
for (i = 0; i < size; i++) { for (i = 0; i < size; i++) {
PyObject *q, *f;
q = PySequence_GetItem(seq, i); q = PySequence_GetItem(seq, i);
if (q == NULL) { // Failed to read sequence if (q == NULL) { // Failed to read sequence
return EXPP_ReturnIntError(PyExc_RuntimeError, return EXPP_ReturnIntError(PyExc_RuntimeError,
"quaternion[begin:end] = []: unable to read sequence\n"); "quaternion[begin:end] = []: unable to read sequence\n");
} }
f = PyNumber_Float(q); f = PyNumber_Float(q);
if(f == NULL) { // parsed item not a number if(f == NULL) { // parsed item not a number
Py_DECREF(q); Py_DECREF(q);
return EXPP_ReturnIntError(PyExc_TypeError, return EXPP_ReturnIntError(PyExc_TypeError,
"quaternion[begin:end] = []: sequence argument not a number\n"); "quaternion[begin:end] = []: sequence argument not a number\n");
} }
quat[i] = (float)PyFloat_AS_DOUBLE(f); quat[i] = (float)PyFloat_AS_DOUBLE(f);
EXPP_decr2(f,q); EXPP_decr2(f,q);
} }
@ -354,7 +356,6 @@ static PyObject *Quaternion_add(PyObject * q1, PyObject * q2)
float quat[4]; float quat[4];
QuaternionObject *quat1 = NULL, *quat2 = NULL; QuaternionObject *quat1 = NULL, *quat2 = NULL;
EXPP_incr2(q1, q2);
quat1 = (QuaternionObject*)q1; quat1 = (QuaternionObject*)q1;
quat2 = (QuaternionObject*)q2; quat2 = (QuaternionObject*)q2;
@ -366,8 +367,7 @@ static PyObject *Quaternion_add(PyObject * q1, PyObject * q2)
quat[x] = quat1->quat[x] + quat2->quat[x]; quat[x] = quat1->quat[x] + quat2->quat[x];
} }
EXPP_decr2((PyObject*)quat1, (PyObject*)quat2); return newQuaternionObject(quat, Py_NEW);
return (PyObject *) newQuaternionObject(quat, Py_NEW);
} }
//------------------------obj - obj------------------------------ //------------------------obj - obj------------------------------
//subtraction //subtraction
@ -377,7 +377,6 @@ static PyObject *Quaternion_sub(PyObject * q1, PyObject * q2)
float quat[4]; float quat[4];
QuaternionObject *quat1 = NULL, *quat2 = NULL; QuaternionObject *quat1 = NULL, *quat2 = NULL;
EXPP_incr2(q1, q2);
quat1 = (QuaternionObject*)q1; quat1 = (QuaternionObject*)q1;
quat2 = (QuaternionObject*)q2; quat2 = (QuaternionObject*)q2;
@ -389,8 +388,7 @@ static PyObject *Quaternion_sub(PyObject * q1, PyObject * q2)
quat[x] = quat1->quat[x] - quat2->quat[x]; quat[x] = quat1->quat[x] - quat2->quat[x];
} }
EXPP_decr2((PyObject*)quat1, (PyObject*)quat2); return newQuaternionObject(quat, Py_NEW);
return (PyObject *) newQuaternionObject(quat, Py_NEW);
} }
//------------------------obj * obj------------------------------ //------------------------obj * obj------------------------------
//mulplication //mulplication
@ -400,11 +398,10 @@ static PyObject *Quaternion_mul(PyObject * q1, PyObject * q2)
float quat[4], scalar; float quat[4], scalar;
double dot = 0.0f; double dot = 0.0f;
QuaternionObject *quat1 = NULL, *quat2 = NULL; QuaternionObject *quat1 = NULL, *quat2 = NULL;
PyObject *f = NULL, *retObj = NULL; PyObject *f = NULL;
VectorObject *vec = NULL; VectorObject *vec = NULL;
PointObject *pt = NULL; PointObject *pt = NULL;
EXPP_incr2(q1, q2);
quat1 = (QuaternionObject*)q1; quat1 = (QuaternionObject*)q1;
quat2 = (QuaternionObject*)q2; quat2 = (QuaternionObject*)q2;
@ -413,16 +410,15 @@ static PyObject *Quaternion_mul(PyObject * q1, PyObject * q2)
PyInt_Check(quat1->coerced_object)){ // FLOAT/INT * QUAT PyInt_Check(quat1->coerced_object)){ // FLOAT/INT * QUAT
f = PyNumber_Float(quat1->coerced_object); f = PyNumber_Float(quat1->coerced_object);
if(f == NULL) { // parsed item not a number if(f == NULL) { // parsed item not a number
EXPP_decr2((PyObject*)quat1, (PyObject*)quat2);
return EXPP_ReturnPyObjError(PyExc_TypeError, return EXPP_ReturnPyObjError(PyExc_TypeError,
"Quaternion multiplication: arguments not acceptable for this operation\n"); "Quaternion multiplication: arguments not acceptable for this operation\n");
} }
scalar = (float)PyFloat_AS_DOUBLE(f); scalar = (float)PyFloat_AS_DOUBLE(f);
for(x = 0; x < 4; x++) { for(x = 0; x < 4; x++) {
quat[x] = quat2->quat[x] * scalar; quat[x] = quat2->quat[x] * scalar;
} }
EXPP_decr2((PyObject*)quat1, (PyObject*)quat2); return newQuaternionObject(quat, Py_NEW);
return (PyObject *) newQuaternionObject(quat, Py_NEW);
} }
}else{ }else{
if(quat2->coerced_object){ if(quat2->coerced_object){
@ -430,47 +426,38 @@ static PyObject *Quaternion_mul(PyObject * q1, PyObject * q2)
PyInt_Check(quat2->coerced_object)){ // QUAT * FLOAT/INT PyInt_Check(quat2->coerced_object)){ // QUAT * FLOAT/INT
f = PyNumber_Float(quat2->coerced_object); f = PyNumber_Float(quat2->coerced_object);
if(f == NULL) { // parsed item not a number if(f == NULL) { // parsed item not a number
EXPP_decr2((PyObject*)quat1, (PyObject*)quat2);
return EXPP_ReturnPyObjError(PyExc_TypeError, return EXPP_ReturnPyObjError(PyExc_TypeError,
"Quaternion multiplication: arguments not acceptable for this operation\n"); "Quaternion multiplication: arguments not acceptable for this operation\n");
} }
scalar = (float)PyFloat_AS_DOUBLE(f); scalar = (float)PyFloat_AS_DOUBLE(f);
for(x = 0; x < 4; x++) { for(x = 0; x < 4; x++) {
quat[x] = quat1->quat[x] * scalar; quat[x] = quat1->quat[x] * scalar;
} }
EXPP_decr2((PyObject*)quat1, (PyObject*)quat2); return newQuaternionObject(quat, Py_NEW);
return (PyObject *) newQuaternionObject(quat, Py_NEW);
}else if(VectorObject_Check(quat2->coerced_object)){ //QUAT * VEC }else if(VectorObject_Check(quat2->coerced_object)){ //QUAT * VEC
vec = (VectorObject*)EXPP_incr_ret(quat2->coerced_object); vec = (VectorObject*)quat2->coerced_object;
if(vec->size != 3){ if(vec->size != 3){
EXPP_decr2((PyObject*)quat1, (PyObject*)quat2);
return EXPP_ReturnPyObjError(PyExc_TypeError, return EXPP_ReturnPyObjError(PyExc_TypeError,
"Quaternion multiplication: only 3D vector rotations currently supported\n"); "Quaternion multiplication: only 3D vector rotations currently supported\n");
} }
retObj = quat_rotation((PyObject*)quat1, (PyObject*)vec); return quat_rotation((PyObject*)quat1, (PyObject*)vec);
EXPP_decr3((PyObject*)quat1, (PyObject*)quat2, (PyObject*)vec);
return retObj;
}else if(PointObject_Check(quat2->coerced_object)){ //QUAT * POINT }else if(PointObject_Check(quat2->coerced_object)){ //QUAT * POINT
pt = (PointObject*)EXPP_incr_ret(quat2->coerced_object); pt = (PointObject*)quat2->coerced_object;
if(pt->size != 3){ if(pt->size != 3){
EXPP_decr2((PyObject*)quat1, (PyObject*)quat2);
return EXPP_ReturnPyObjError(PyExc_TypeError, return EXPP_ReturnPyObjError(PyExc_TypeError,
"Quaternion multiplication: only 3D point rotations currently supported\n"); "Quaternion multiplication: only 3D point rotations currently supported\n");
} }
retObj = quat_rotation((PyObject*)quat1, (PyObject*)pt); return quat_rotation((PyObject*)quat1, (PyObject*)pt);
EXPP_decr3((PyObject*)quat1, (PyObject*)quat2, (PyObject*)pt);
return retObj;
} }
}else{ //QUAT * QUAT (dot product) }else{ //QUAT * QUAT (dot product)
for(x = 0; x < 4; x++) { for(x = 0; x < 4; x++) {
dot += quat1->quat[x] * quat1->quat[x]; dot += quat1->quat[x] * quat1->quat[x];
} }
EXPP_decr2((PyObject*)quat1, (PyObject*)quat2);
return PyFloat_FromDouble(dot); return PyFloat_FromDouble(dot);
} }
} }
EXPP_decr2((PyObject*)quat1, (PyObject*)quat2);
return EXPP_ReturnPyObjError(PyExc_TypeError, return EXPP_ReturnPyObjError(PyExc_TypeError,
"Quaternion multiplication: arguments not acceptable for this operation\n"); "Quaternion multiplication: arguments not acceptable for this operation\n");
} }

@ -73,7 +73,7 @@ PyObject *Vector_toPoint(VectorObject * self)
coord[x] = self->vec[x]; coord[x] = self->vec[x];
} }
return (PyObject *) newPointObject(coord, self->size, Py_NEW); return newPointObject(coord, self->size, Py_NEW);
} }
//----------------------------Vector.zero() ---------------------- //----------------------------Vector.zero() ----------------------
//set the vector data to 0,0,0 //set the vector data to 0,0,0
@ -176,14 +176,11 @@ PyObject *Vector_ToTrackQuat( VectorObject * self, PyObject * args )
short track = 2, up = 1; short track = 2, up = 1;
if( !PyArg_ParseTuple ( args, "|ss", &strack, &sup ) ) { if( !PyArg_ParseTuple ( args, "|ss", &strack, &sup ) ) {
return ( EXPP_ReturnPyObjError return EXPP_ReturnPyObjError( PyExc_TypeError,
( PyExc_TypeError, "expected optional two strings\n" );
"expected optional two strings\n" ) );
} }
if (self->size != 3) { if (self->size != 3) {
return ( EXPP_ReturnPyObjError return EXPP_ReturnPyObjError( PyExc_TypeError, "only for 3D vectors\n" );
( PyExc_TypeError,
"only for 3D vectors\n" ) );
} }
if (strack) { if (strack) {
@ -283,6 +280,7 @@ PyObject *Vector_ToTrackQuat( VectorObject * self, PyObject * args )
//free the py_object //free the py_object
static void Vector_dealloc(VectorObject * self) static void Vector_dealloc(VectorObject * self)
{ {
Py_XDECREF(self->coerced_object);
//only free py_data //only free py_data
if(self->data.py_data){ if(self->data.py_data){
PyMem_Free(self->data.py_data); PyMem_Free(self->data.py_data);
@ -388,7 +386,7 @@ static PyObject *Vector_repr(VectorObject * self)
} }
strcat(str, "](vector)"); strcat(str, "](vector)");
return EXPP_incr_ret(PyString_FromString(str)); return PyString_FromString(str);
} }
//---------------------SEQUENCE PROTOCOLS------------------------ //---------------------SEQUENCE PROTOCOLS------------------------
//----------------------------len(object)------------------------ //----------------------------len(object)------------------------
@ -455,6 +453,7 @@ static int Vector_ass_slice(VectorObject * self, int begin, int end,
{ {
int i, y, size = 0; int i, y, size = 0;
float vec[4]; float vec[4];
PyObject *v, *f;
CLAMP(begin, 0, self->size); CLAMP(begin, 0, self->size);
CLAMP(end, 0, self->size); CLAMP(end, 0, self->size);
@ -467,19 +466,19 @@ static int Vector_ass_slice(VectorObject * self, int begin, int end,
} }
for (i = 0; i < size; i++) { for (i = 0; i < size; i++) {
PyObject *v, *f;
v = PySequence_GetItem(seq, i); v = PySequence_GetItem(seq, i);
if (v == NULL) { // Failed to read sequence if (v == NULL) { // Failed to read sequence
return EXPP_ReturnIntError(PyExc_RuntimeError, return EXPP_ReturnIntError(PyExc_RuntimeError,
"vector[begin:end] = []: unable to read sequence\n"); "vector[begin:end] = []: unable to read sequence\n");
} }
f = PyNumber_Float(v); f = PyNumber_Float(v);
if(f == NULL) { // parsed item not a number if(f == NULL) { // parsed item not a number
Py_DECREF(v); Py_DECREF(v);
return EXPP_ReturnIntError(PyExc_TypeError, return EXPP_ReturnIntError(PyExc_TypeError,
"vector[begin:end] = []: sequence argument not a number\n"); "vector[begin:end] = []: sequence argument not a number\n");
} }
vec[i] = (float)PyFloat_AS_DOUBLE(f); vec[i] = (float)PyFloat_AS_DOUBLE(f);
EXPP_decr2(f,v); EXPP_decr2(f,v);
} }
@ -499,7 +498,6 @@ static PyObject *Vector_add(PyObject * v1, PyObject * v2)
VectorObject *vec1 = NULL, *vec2 = NULL; VectorObject *vec1 = NULL, *vec2 = NULL;
PointObject *pt = NULL; PointObject *pt = NULL;
EXPP_incr2(v1, v2);
vec1 = (VectorObject*)v1; vec1 = (VectorObject*)v1;
vec2 = (VectorObject*)v2; vec2 = (VectorObject*)v2;
@ -507,23 +505,20 @@ static PyObject *Vector_add(PyObject * v1, PyObject * v2)
if(vec2->coerced_object){ if(vec2->coerced_object){
if(PointObject_Check(vec2->coerced_object)){ //VECTOR + POINT if(PointObject_Check(vec2->coerced_object)){ //VECTOR + POINT
//Point translation //Point translation
pt = (PointObject*)EXPP_incr_ret(vec2->coerced_object); pt = (PointObject*)vec2->coerced_object;
size = vec1->size; size = vec1->size;
if(pt->size == size){ if(pt->size == size){
for(x = 0; x < size; x++){ for(x = 0; x < size; x++){
vec[x] = vec1->vec[x] + pt->coord[x]; vec[x] = vec1->vec[x] + pt->coord[x];
} }
}else{ }else{
EXPP_decr3((PyObject*)vec1, (PyObject*)vec2, (PyObject*)pt);
return EXPP_ReturnPyObjError(PyExc_AttributeError, return EXPP_ReturnPyObjError(PyExc_AttributeError,
"Vector addition: arguments are the wrong size....\n"); "Vector addition: arguments are the wrong size....\n");
} }
EXPP_decr3((PyObject*)vec1, (PyObject*)vec2, (PyObject*)pt); return newPointObject(vec, size, Py_NEW);
return (PyObject *) newPointObject(vec, size, Py_NEW);
} }
}else{ //VECTOR + VECTOR }else{ //VECTOR + VECTOR
if(vec1->size != vec2->size){ if(vec1->size != vec2->size){
EXPP_decr2((PyObject*)vec1, (PyObject*)vec2);
return EXPP_ReturnPyObjError(PyExc_AttributeError, return EXPP_ReturnPyObjError(PyExc_AttributeError,
"Vector addition: vectors must have the same dimensions for this operation\n"); "Vector addition: vectors must have the same dimensions for this operation\n");
} }
@ -531,8 +526,7 @@ static PyObject *Vector_add(PyObject * v1, PyObject * v2)
for(x = 0; x < size; x++) { for(x = 0; x < size; x++) {
vec[x] = vec1->vec[x] + vec2->vec[x]; vec[x] = vec1->vec[x] + vec2->vec[x];
} }
EXPP_decr2((PyObject*)vec1, (PyObject*)vec2); return newVectorObject(vec, size, Py_NEW);
return (PyObject *) newVectorObject(vec, size, Py_NEW);
} }
} }
@ -547,7 +541,6 @@ static PyObject *Vector_sub(PyObject * v1, PyObject * v2)
float vec[4]; float vec[4];
VectorObject *vec1 = NULL, *vec2 = NULL; VectorObject *vec1 = NULL, *vec2 = NULL;
EXPP_incr2(v1, v2);
vec1 = (VectorObject*)v1; vec1 = (VectorObject*)v1;
vec2 = (VectorObject*)v2; vec2 = (VectorObject*)v2;
@ -556,7 +549,6 @@ static PyObject *Vector_sub(PyObject * v1, PyObject * v2)
"Vector subtraction: arguments not valid for this operation....\n"); "Vector subtraction: arguments not valid for this operation....\n");
} }
if(vec1->size != vec2->size){ if(vec1->size != vec2->size){
EXPP_decr2((PyObject*)vec1, (PyObject*)vec2);
return EXPP_ReturnPyObjError(PyExc_AttributeError, return EXPP_ReturnPyObjError(PyExc_AttributeError,
"Vector subtraction: vectors must have the same dimensions for this operation\n"); "Vector subtraction: vectors must have the same dimensions for this operation\n");
} }
@ -566,8 +558,7 @@ static PyObject *Vector_sub(PyObject * v1, PyObject * v2)
vec[x] = vec1->vec[x] - vec2->vec[x]; vec[x] = vec1->vec[x] - vec2->vec[x];
} }
EXPP_decr2((PyObject*)vec1, (PyObject*)vec2); return newVectorObject(vec, size, Py_NEW);
return (PyObject *) newVectorObject(vec, size, Py_NEW);
} }
//------------------------obj * obj------------------------------ //------------------------obj * obj------------------------------
//mulplication //mulplication
@ -581,7 +572,6 @@ static PyObject *Vector_mul(PyObject * v1, PyObject * v2)
MatrixObject *mat = NULL; MatrixObject *mat = NULL;
QuaternionObject *quat = NULL; QuaternionObject *quat = NULL;
EXPP_incr2(v1, v2);
vec1 = (VectorObject*)v1; vec1 = (VectorObject*)v1;
vec2 = (VectorObject*)v2; vec2 = (VectorObject*)v2;
@ -590,54 +580,48 @@ static PyObject *Vector_mul(PyObject * v1, PyObject * v2)
PyInt_Check(vec1->coerced_object)){ // FLOAT/INT * VECTOR PyInt_Check(vec1->coerced_object)){ // FLOAT/INT * VECTOR
f = PyNumber_Float(vec1->coerced_object); f = PyNumber_Float(vec1->coerced_object);
if(f == NULL) { // parsed item not a number if(f == NULL) { // parsed item not a number
EXPP_decr2((PyObject*)vec1, (PyObject*)vec2);
return EXPP_ReturnPyObjError(PyExc_TypeError, return EXPP_ReturnPyObjError(PyExc_TypeError,
"Vector multiplication: arguments not acceptable for this operation\n"); "Vector multiplication: arguments not acceptable for this operation\n");
} }
scalar = (float)PyFloat_AS_DOUBLE(f); scalar = (float)PyFloat_AS_DOUBLE(f);
size = vec2->size; size = vec2->size;
for(x = 0; x < size; x++) { for(x = 0; x < size; x++) {
vec[x] = vec2->vec[x] * scalar; vec[x] = vec2->vec[x] * scalar;
} }
EXPP_decr2((PyObject*)vec1, (PyObject*)vec2); Py_DECREF(f);
return (PyObject *) newVectorObject(vec, size, Py_NEW); return newVectorObject(vec, size, Py_NEW);
} }
}else{ }else{
if(vec2->coerced_object){ if(vec2->coerced_object){
if(MatrixObject_Check(vec2->coerced_object)){ //VECTOR * MATRIX if(MatrixObject_Check(vec2->coerced_object)){ //VECTOR * MATRIX
mat = (MatrixObject*)EXPP_incr_ret(vec2->coerced_object); mat = (MatrixObject*)vec2->coerced_object;
retObj = row_vector_multiplication(vec1, mat); return retObj = row_vector_multiplication(vec1, mat);
EXPP_decr3((PyObject*)vec1, (PyObject*)vec2, (PyObject*)mat);
return retObj;
}else if (PyFloat_Check(vec2->coerced_object) || }else if (PyFloat_Check(vec2->coerced_object) ||
PyInt_Check(vec2->coerced_object)){ // VECTOR * FLOAT/INT PyInt_Check(vec2->coerced_object)){ // VECTOR * FLOAT/INT
f = PyNumber_Float(vec2->coerced_object); f = PyNumber_Float(vec2->coerced_object);
if(f == NULL) { // parsed item not a number if(f == NULL) { // parsed item not a number
EXPP_decr2((PyObject*)vec1, (PyObject*)vec2);
return EXPP_ReturnPyObjError(PyExc_TypeError, return EXPP_ReturnPyObjError(PyExc_TypeError,
"Vector multiplication: arguments not acceptable for this operation\n"); "Vector multiplication: arguments not acceptable for this operation\n");
} }
scalar = (float)PyFloat_AS_DOUBLE(f); scalar = (float)PyFloat_AS_DOUBLE(f);
size = vec1->size; size = vec1->size;
for(x = 0; x < size; x++) { for(x = 0; x < size; x++) {
vec[x] = vec1->vec[x] * scalar; vec[x] = vec1->vec[x] * scalar;
} }
EXPP_decr2((PyObject*)vec1, (PyObject*)vec2); Py_DECREF(f);
return (PyObject *) newVectorObject(vec, size, Py_NEW); return newVectorObject(vec, size, Py_NEW);
}else if(QuaternionObject_Check(vec2->coerced_object)){ //VECTOR * QUATERNION }else if(QuaternionObject_Check(vec2->coerced_object)){ //VECTOR * QUATERNION
quat = (QuaternionObject*)EXPP_incr_ret(vec2->coerced_object); quat = (QuaternionObject*)vec2->coerced_object;
if(vec1->size != 3){ if(vec1->size != 3){
EXPP_decr2((PyObject*)vec1, (PyObject*)vec2);
return EXPP_ReturnPyObjError(PyExc_TypeError, return EXPP_ReturnPyObjError(PyExc_TypeError,
"Vector multiplication: only 3D vector rotations (with quats) currently supported\n"); "Vector multiplication: only 3D vector rotations (with quats) currently supported\n");
} }
retObj = quat_rotation((PyObject*)vec1, (PyObject*)quat); return quat_rotation((PyObject*)vec1, (PyObject*)quat);
EXPP_decr3((PyObject*)vec1, (PyObject*)vec2, (PyObject*)quat);
return retObj;
} }
}else{ //VECTOR * VECTOR }else{ //VECTOR * VECTOR
if(vec1->size != vec2->size){ if(vec1->size != vec2->size){
EXPP_decr2((PyObject*)vec1, (PyObject*)vec2);
return EXPP_ReturnPyObjError(PyExc_AttributeError, return EXPP_ReturnPyObjError(PyExc_AttributeError,
"Vector multiplication: vectors must have the same dimensions for this operation\n"); "Vector multiplication: vectors must have the same dimensions for this operation\n");
} }
@ -646,12 +630,10 @@ static PyObject *Vector_mul(PyObject * v1, PyObject * v2)
for(x = 0; x < size; x++) { for(x = 0; x < size; x++) {
dot += vec1->vec[x] * vec2->vec[x]; dot += vec1->vec[x] * vec2->vec[x];
} }
EXPP_decr2((PyObject*)vec1, (PyObject*)vec2);
return PyFloat_FromDouble(dot); return PyFloat_FromDouble(dot);
} }
} }
EXPP_decr2((PyObject*)vec1, (PyObject*)vec2);
return EXPP_ReturnPyObjError(PyExc_TypeError, return EXPP_ReturnPyObjError(PyExc_TypeError,
"Vector multiplication: arguments not acceptable for this operation\n"); "Vector multiplication: arguments not acceptable for this operation\n");
} }
@ -733,8 +715,6 @@ PyObject* Vector_richcmpr(PyObject *objectA, PyObject *objectB, int comparison_t
return EXPP_incr_ret(Py_False); return EXPP_incr_ret(Py_False);
} }
} }
Py_INCREF(objectA);
Py_INCREF(objectB);
vecA = (VectorObject*)objectA; vecA = (VectorObject*)objectA;
vecB = (VectorObject*)objectB; vecB = (VectorObject*)objectB;