minor mathutils update

- docstring for Euler.rotate
- rotate_eul, use upper case in Py and C. 
- use less verbose repr method.
This commit is contained in:
2010-04-25 03:34:16 +00:00
parent b37ae4a375
commit 708667c6f6
6 changed files with 197 additions and 141 deletions

View File

@@ -1647,7 +1647,7 @@ static void rotlike_evaluate (bConstraint *con, bConstraintOb *cob, ListBase *ta
eul[0] = obeul[0]; eul[0] = obeul[0];
else { else {
if (data->flag & ROTLIKE_OFFSET) if (data->flag & ROTLIKE_OFFSET)
rotate_eulO(eul, cob->rotOrder, 'x', obeul[0]); rotate_eulO(eul, cob->rotOrder, 'X', obeul[0]);
if (data->flag & ROTLIKE_X_INVERT) if (data->flag & ROTLIKE_X_INVERT)
eul[0] *= -1; eul[0] *= -1;
@@ -1657,7 +1657,7 @@ static void rotlike_evaluate (bConstraint *con, bConstraintOb *cob, ListBase *ta
eul[1] = obeul[1]; eul[1] = obeul[1];
else { else {
if (data->flag & ROTLIKE_OFFSET) if (data->flag & ROTLIKE_OFFSET)
rotate_eulO(eul, cob->rotOrder, 'y', obeul[1]); rotate_eulO(eul, cob->rotOrder, 'Y', obeul[1]);
if (data->flag & ROTLIKE_Y_INVERT) if (data->flag & ROTLIKE_Y_INVERT)
eul[1] *= -1; eul[1] *= -1;
@@ -1667,7 +1667,7 @@ static void rotlike_evaluate (bConstraint *con, bConstraintOb *cob, ListBase *ta
eul[2] = obeul[2]; eul[2] = obeul[2];
else { else {
if (data->flag & ROTLIKE_OFFSET) if (data->flag & ROTLIKE_OFFSET)
rotate_eulO(eul, cob->rotOrder, 'z', obeul[2]); rotate_eulO(eul, cob->rotOrder, 'Z', obeul[2]);
if (data->flag & ROTLIKE_Z_INVERT) if (data->flag & ROTLIKE_Z_INVERT)
eul[2] *= -1; eul[2] *= -1;

View File

@@ -919,8 +919,8 @@ void rotate_eul(float *beul, char axis, float ang)
float eul[3], mat1[3][3], mat2[3][3], totmat[3][3]; float eul[3], mat1[3][3], mat2[3][3], totmat[3][3];
eul[0]= eul[1]= eul[2]= 0.0f; eul[0]= eul[1]= eul[2]= 0.0f;
if(axis=='x') eul[0]= ang; if(axis=='X') eul[0]= ang;
else if(axis=='y') eul[1]= ang; else if(axis=='Y') eul[1]= ang;
else eul[2]= ang; else eul[2]= ang;
eul_to_mat3(mat1,eul); eul_to_mat3(mat1,eul);
@@ -1238,9 +1238,9 @@ void rotate_eulO(float beul[3], short order, char axis, float ang)
float eul[3], mat1[3][3], mat2[3][3], totmat[3][3]; float eul[3], mat1[3][3], mat2[3][3], totmat[3][3];
eul[0]= eul[1]= eul[2]= 0.0f; eul[0]= eul[1]= eul[2]= 0.0f;
if (axis=='x') if (axis=='X')
eul[0]= ang; eul[0]= ang;
else if (axis=='y') else if (axis=='Y')
eul[1]= ang; eul[1]= ang;
else else
eul[2]= ang; eul[2]= ang;

View File

@@ -79,8 +79,27 @@ static PyObject *Color_new(PyTypeObject * type, PyObject * args, PyObject * kwar
//-----------------------------METHODS---------------------------- //-----------------------------METHODS----------------------------
//----------------------------Color.rotate()----------------------- /* note: BaseMath_ReadCallback must be called beforehand */
// return a copy of the color static PyObject *Color_ToTupleExt(ColorObject *self, int ndigits)
{
PyObject *ret;
int i;
ret= PyTuple_New(3);
if(ndigits >= 0) {
for(i= 0; i < 3; i++) {
PyTuple_SET_ITEM(ret, i, PyFloat_FromDouble(double_round((double)self->col[i], ndigits)));
}
}
else {
for(i= 0; i < 3; i++) {
PyTuple_SET_ITEM(ret, i, PyFloat_FromDouble(self->col[i]));
}
}
return ret;
}
static char Color_copy_doc[] = static char Color_copy_doc[] =
".. function:: copy()\n" ".. function:: copy()\n"
@@ -102,25 +121,22 @@ static PyObject *Color_copy(ColorObject * self, PyObject *args)
//----------------------------print object (internal)-------------- //----------------------------print object (internal)--------------
//print the object to screen //print the object to screen
static PyObject *Color_repr(ColorObject * self) static PyObject *Color_repr(ColorObject * self)
{ {
PyObject *r, *g, *b, *ret; PyObject *ret, *tuple;
if(!BaseMath_ReadCallback(self)) if(!BaseMath_ReadCallback(self))
return NULL; return NULL;
r= PyFloat_FromDouble(self->col[0]); tuple= Color_ToTupleExt(self, -1);
g= PyFloat_FromDouble(self->col[1]);
b= PyFloat_FromDouble(self->col[2]);
ret= PyUnicode_FromFormat("Color(%R, %R, %R)", r, g, b); ret= PyUnicode_FromFormat("Color%R", tuple);
Py_DECREF(r);
Py_DECREF(g);
Py_DECREF(b);
Py_DECREF(tuple);
return ret; return ret;
} }
//------------------------tp_richcmpr //------------------------tp_richcmpr
//returns -1 execption, 0 false, 1 true //returns -1 execption, 0 false, 1 true
static PyObject* Color_richcmpr(PyObject *objectA, PyObject *objectB, int comparison_type) static PyObject* Color_richcmpr(PyObject *objectA, PyObject *objectB, int comparison_type)

View File

@@ -102,8 +102,29 @@ short euler_order_from_string(const char *str, const char *error_prefix)
return -1; return -1;
} }
/* note: BaseMath_ReadCallback must be called beforehand */
static PyObject *Euler_ToTupleExt(EulerObject *self, int ndigits)
{
PyObject *ret;
int i;
ret= PyTuple_New(3);
if(ndigits >= 0) {
for(i= 0; i < 3; i++) {
PyTuple_SET_ITEM(ret, i, PyFloat_FromDouble(double_round((double)self->eul[i], ndigits)));
}
}
else {
for(i= 0; i < 3; i++) {
PyTuple_SET_ITEM(ret, i, PyFloat_FromDouble(self->eul[i]));
}
}
return ret;
}
//-----------------------------METHODS---------------------------- //-----------------------------METHODS----------------------------
//----------------------------Euler.toQuat()----------------------
//return a quaternion representation of the euler //return a quaternion representation of the euler
static char Euler_ToQuat_doc[] = static char Euler_ToQuat_doc[] =
@@ -126,7 +147,7 @@ static PyObject *Euler_ToQuat(EulerObject * self)
return newQuaternionObject(quat, Py_NEW, NULL); return newQuaternionObject(quat, Py_NEW, NULL);
} }
//----------------------------Euler.toMatrix()---------------------
//return a matrix representation of the euler //return a matrix representation of the euler
static char Euler_ToMatrix_doc[] = static char Euler_ToMatrix_doc[] =
".. method:: to_matrix()\n" ".. method:: to_matrix()\n"
@@ -148,7 +169,7 @@ static PyObject *Euler_ToMatrix(EulerObject * self)
return newMatrixObject(mat, 3, 3 , Py_NEW, NULL); return newMatrixObject(mat, 3, 3 , Py_NEW, NULL);
} }
//----------------------------Euler.unique()-----------------------
//sets the x,y,z values to a unique euler rotation //sets the x,y,z values to a unique euler rotation
// TODO, check if this works with rotation order!!! // TODO, check if this works with rotation order!!!
static char Euler_Unique_doc[] = static char Euler_Unique_doc[] =
@@ -207,7 +228,7 @@ static PyObject *Euler_Unique(EulerObject * self)
Py_INCREF(self); Py_INCREF(self);
return (PyObject *)self; return (PyObject *)self;
} }
//----------------------------Euler.zero()-------------------------
//sets the euler to 0,0,0 //sets the euler to 0,0,0
static char Euler_Zero_doc[] = static char Euler_Zero_doc[] =
".. method:: zero()\n" ".. method:: zero()\n"
@@ -227,20 +248,30 @@ static PyObject *Euler_Zero(EulerObject * self)
Py_INCREF(self); Py_INCREF(self);
return (PyObject *)self; return (PyObject *)self;
} }
//----------------------------Euler.rotate()-----------------------
//rotates a euler a certain amount and returns the result static char Euler_Rotate_doc[] =
//should return a unique euler rotation (i.e. no 720 degree pitches :) ".. method:: rotate(angle, axis)\n"
"\n"
" Rotates the euler a certain amount and returning a unique euler rotation (no 720 degree pitches).\n"
"\n"
" :arg angle: angle in radians.\n"
" :type angle: float\n"
" :arg axis: single character in ['X, 'Y', 'Z'].\n"
" :type axis: string\n"
" :return: an instance of itself\n"
" :rtype: :class:`Euler`";
static PyObject *Euler_Rotate(EulerObject * self, PyObject *args) static PyObject *Euler_Rotate(EulerObject * self, PyObject *args)
{ {
float angle = 0.0f; float angle = 0.0f;
char *axis; char *axis;
if(!PyArg_ParseTuple(args, "fs", &angle, &axis)){ if(!PyArg_ParseTuple(args, "fs:rotate", &angle, &axis)){
PyErr_SetString(PyExc_TypeError, "euler.rotate():expected angle (float) and axis (x,y,z)"); PyErr_SetString(PyExc_TypeError, "euler.rotate(): expected angle (float) and axis (x,y,z)");
return NULL; return NULL;
} }
if(ELEM3(*axis, 'x', 'y', 'z') && axis[1]=='\0'){ if(ELEM3(*axis, 'X', 'Y', 'Z') && axis[1]=='\0'){
PyErr_SetString(PyExc_TypeError, "euler.rotate(): expected axis to be 'x', 'y' or 'z'"); PyErr_SetString(PyExc_TypeError, "euler.rotate(): expected axis to be 'X', 'Y' or 'Z'");
return NULL; return NULL;
} }
@@ -312,25 +343,22 @@ static PyObject *Euler_copy(EulerObject * self, PyObject *args)
//----------------------------print object (internal)-------------- //----------------------------print object (internal)--------------
//print the object to screen //print the object to screen
static PyObject *Euler_repr(EulerObject * self) static PyObject *Euler_repr(EulerObject * self)
{ {
PyObject *x, *y, *z, *ret; PyObject *ret, *tuple;
if(!BaseMath_ReadCallback(self)) if(!BaseMath_ReadCallback(self))
return NULL; return NULL;
x= PyFloat_FromDouble(self->eul[0]); tuple= Euler_ToTupleExt(self, -1);
y= PyFloat_FromDouble(self->eul[1]);
z= PyFloat_FromDouble(self->eul[2]);
ret= PyUnicode_FromFormat("Euler(%R, %R, %R)", x, y, z); ret= PyUnicode_FromFormat("Euler%R", tuple);
Py_DECREF(x);
Py_DECREF(y);
Py_DECREF(z);
Py_DECREF(tuple);
return ret; return ret;
} }
//------------------------tp_richcmpr //------------------------tp_richcmpr
//returns -1 execption, 0 false, 1 true //returns -1 execption, 0 false, 1 true
static PyObject* Euler_richcmpr(PyObject *objectA, PyObject *objectB, int comparison_type) static PyObject* Euler_richcmpr(PyObject *objectA, PyObject *objectB, int comparison_type)
@@ -565,7 +593,7 @@ static struct PyMethodDef Euler_methods[] = {
{"unique", (PyCFunction) Euler_Unique, METH_NOARGS, Euler_Unique_doc}, {"unique", (PyCFunction) Euler_Unique, METH_NOARGS, Euler_Unique_doc},
{"to_matrix", (PyCFunction) Euler_ToMatrix, METH_NOARGS, Euler_ToMatrix_doc}, {"to_matrix", (PyCFunction) Euler_ToMatrix, METH_NOARGS, Euler_ToMatrix_doc},
{"to_quat", (PyCFunction) Euler_ToQuat, METH_NOARGS, Euler_ToQuat_doc}, {"to_quat", (PyCFunction) Euler_ToQuat, METH_NOARGS, Euler_ToQuat_doc},
{"rotate", (PyCFunction) Euler_Rotate, METH_VARARGS, NULL}, {"rotate", (PyCFunction) Euler_Rotate, METH_VARARGS, Euler_Rotate_doc},
{"make_compatible", (PyCFunction) Euler_MakeCompatible, METH_O, Euler_MakeCompatible_doc}, {"make_compatible", (PyCFunction) Euler_MakeCompatible, METH_O, Euler_MakeCompatible_doc},
{"__copy__", (PyCFunction) Euler_copy, METH_VARARGS, Euler_copy_doc}, {"__copy__", (PyCFunction) Euler_copy, METH_VARARGS, Euler_copy_doc},
{"copy", (PyCFunction) Euler_copy, METH_VARARGS, Euler_copy_doc}, {"copy", (PyCFunction) Euler_copy, METH_VARARGS, Euler_copy_doc},

View File

@@ -32,6 +32,29 @@
#include "BKE_utildefines.h" #include "BKE_utildefines.h"
//-----------------------------METHODS------------------------------ //-----------------------------METHODS------------------------------
/* note: BaseMath_ReadCallback must be called beforehand */
static PyObject *Quaternion_ToTupleExt(QuaternionObject *self, int ndigits)
{
PyObject *ret;
int i;
ret= PyTuple_New(4);
if(ndigits >= 0) {
for(i= 0; i < 4; i++) {
PyTuple_SET_ITEM(ret, i, PyFloat_FromDouble(double_round((double)self->quat[i], ndigits)));
}
}
else {
for(i= 0; i < 4; i++) {
PyTuple_SET_ITEM(ret, i, PyFloat_FromDouble(self->quat[i]));
}
}
return ret;
}
static char Quaternion_ToEuler_doc[] = static char Quaternion_ToEuler_doc[] =
".. method:: to_euler(order, euler_compat)\n" ".. method:: to_euler(order, euler_compat)\n"
"\n" "\n"
@@ -351,25 +374,19 @@ static PyObject *Quaternion_copy(QuaternionObject * self)
//print the object to screen //print the object to screen
static PyObject *Quaternion_repr(QuaternionObject * self) static PyObject *Quaternion_repr(QuaternionObject * self)
{ {
PyObject *w, *x, *y, *z, *ret; PyObject *ret, *tuple;
if(!BaseMath_ReadCallback(self)) if(!BaseMath_ReadCallback(self))
return NULL; return NULL;
w= PyFloat_FromDouble(self->quat[0]); tuple= Quaternion_ToTupleExt(self, -1);
x= PyFloat_FromDouble(self->quat[1]);
y= PyFloat_FromDouble(self->quat[2]);
z= PyFloat_FromDouble(self->quat[3]);
ret= PyUnicode_FromFormat("Quaternion(%R, %R, %R, %R)", w, x, y, z); ret= PyUnicode_FromFormat("Quaternion%R", tuple);
Py_DECREF(w);
Py_DECREF(x);
Py_DECREF(y);
Py_DECREF(z);
Py_DECREF(tuple);
return ret; return ret;
} }
//------------------------tp_richcmpr //------------------------tp_richcmpr
//returns -1 execption, 0 false, 1 true //returns -1 execption, 0 false, 1 true
static PyObject* Quaternion_richcmpr(PyObject *objectA, PyObject *objectB, int comparison_type) static PyObject* Quaternion_richcmpr(PyObject *objectA, PyObject *objectB, int comparison_type)

View File

@@ -40,6 +40,7 @@
#define SWIZZLE_AXIS 0x3 #define SWIZZLE_AXIS 0x3
static PyObject *row_vector_multiplication(VectorObject* vec, MatrixObject * mat); /* utility func */ static PyObject *row_vector_multiplication(VectorObject* vec, MatrixObject * mat); /* utility func */
static PyObject *Vector_ToTupleExt(VectorObject *self, int ndigits);
//----------------------------------mathutils.Vector() ------------------ //----------------------------------mathutils.Vector() ------------------
// Supports 2D, 3D, and 4D vector objects both int and float values // Supports 2D, 3D, and 4D vector objects both int and float values
@@ -79,8 +80,7 @@ static PyObject *Vector_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
return NULL; return NULL;
} }
f= PyFloat_AsDouble(v); if((f=PyFloat_AsDouble(v)) == -1 && PyErr_Occurred()) { // parsed item not a number
if(f==-1 && PyErr_Occurred()) { // parsed item not a number
Py_DECREF(v); Py_DECREF(v);
PyErr_SetString(PyExc_TypeError, "mathutils.Vector(): 2-4 floats or ints expected (optionally in a sequence)\n"); PyErr_SetString(PyExc_TypeError, "mathutils.Vector(): 2-4 floats or ints expected (optionally in a sequence)\n");
return NULL; return NULL;
@@ -101,7 +101,7 @@ static char Vector_Zero_doc[] =
" :return: an instance of itself\n" " :return: an instance of itself\n"
" :rtype: :class:`Vector`\n"; " :rtype: :class:`Vector`\n";
static PyObject *Vector_Zero(VectorObject * self) static PyObject *Vector_Zero(VectorObject *self)
{ {
int i; int i;
for(i = 0; i < self->size; i++) { for(i = 0; i < self->size; i++) {
@@ -125,7 +125,7 @@ static char Vector_Normalize_doc[] =
"\n" "\n"
" .. note:: Normalize works for vectors of all sizes, however 4D Vectors w axis is left untouched.\n"; " .. note:: Normalize works for vectors of all sizes, however 4D Vectors w axis is left untouched.\n";
static PyObject *Vector_Normalize(VectorObject * self) static PyObject *Vector_Normalize(VectorObject *self)
{ {
int i; int i;
float norm = 0.0f; float norm = 0.0f;
@@ -156,7 +156,7 @@ static char Vector_Resize2D_doc[] =
" :return: an instance of itself\n" " :return: an instance of itself\n"
" :rtype: :class:`Vector`\n"; " :rtype: :class:`Vector`\n";
static PyObject *Vector_Resize2D(VectorObject * self) static PyObject *Vector_Resize2D(VectorObject *self)
{ {
if(self->wrapped==Py_WRAP) { if(self->wrapped==Py_WRAP) {
PyErr_SetString(PyExc_TypeError, "vector.resize2D(): cannot resize wrapped data - only python vectors\n"); PyErr_SetString(PyExc_TypeError, "vector.resize2D(): cannot resize wrapped data - only python vectors\n");
@@ -186,7 +186,7 @@ static char Vector_Resize3D_doc[] =
" :return: an instance of itself\n" " :return: an instance of itself\n"
" :rtype: :class:`Vector`\n"; " :rtype: :class:`Vector`\n";
static PyObject *Vector_Resize3D(VectorObject * self) static PyObject *Vector_Resize3D(VectorObject *self)
{ {
if (self->wrapped==Py_WRAP) { if (self->wrapped==Py_WRAP) {
PyErr_SetString(PyExc_TypeError, "vector.resize3D(): cannot resize wrapped data - only python vectors\n"); PyErr_SetString(PyExc_TypeError, "vector.resize3D(): cannot resize wrapped data - only python vectors\n");
@@ -219,7 +219,7 @@ static char Vector_Resize4D_doc[] =
" :return: an instance of itself\n" " :return: an instance of itself\n"
" :rtype: :class:`Vector`\n"; " :rtype: :class:`Vector`\n";
static PyObject *Vector_Resize4D(VectorObject * self) static PyObject *Vector_Resize4D(VectorObject *self)
{ {
if(self->wrapped==Py_WRAP) { if(self->wrapped==Py_WRAP) {
PyErr_SetString(PyExc_TypeError, "vector.resize4D(): cannot resize wrapped data - only python vectors"); PyErr_SetString(PyExc_TypeError, "vector.resize4D(): cannot resize wrapped data - only python vectors");
@@ -248,37 +248,53 @@ static PyObject *Vector_Resize4D(VectorObject * self)
/*----------------------------Vector.toTuple() ------------------ */ /*----------------------------Vector.toTuple() ------------------ */
static char Vector_ToTuple_doc[] = static char Vector_ToTuple_doc[] =
".. method:: to_tuple(precision)\n" ".. method:: to_tuple(precision=-1)\n"
"\n" "\n"
" Return this vector as a tuple with.\n" " Return this vector as a tuple with.\n"
"\n" "\n"
" :arg precision: The number to round the value to in [0, 21].\n" " :arg precision: The number to round the value to in [-1, 21].\n"
" :type precision: int\n" " :type precision: int\n"
" :return: the values of the vector rounded by *precision*\n" " :return: the values of the vector rounded by *precision*\n"
" :rtype: tuple\n"; " :rtype: tuple\n";
static PyObject *Vector_ToTuple(VectorObject * self, PyObject *value) /* note: BaseMath_ReadCallback must be called beforehand */
static PyObject *Vector_ToTupleExt(VectorObject *self, int ndigits)
{ {
int ndigits= PyLong_AsSsize_t(value);
int x;
PyObject *ret; PyObject *ret;
int i;
if(ndigits > 22 || ndigits < 0) { /* accounts for non ints */ ret= PyTuple_New(self->size);
if(ndigits >= 0) {
for(i = 0; i < self->size; i++) {
PyTuple_SET_ITEM(ret, i, PyFloat_FromDouble(double_round((double)self->vec[i], ndigits)));
}
}
else {
for(i = 0; i < self->size; i++) {
PyTuple_SET_ITEM(ret, i, PyFloat_FromDouble(self->vec[i]));
}
}
return ret;
}
static PyObject *Vector_ToTuple(VectorObject *self, PyObject *args)
{
int ndigits= 0;
if(!PyArg_ParseTuple(args, "|i:to_tuple", &ndigits) || (ndigits > 22 || ndigits < 0)) {
PyErr_SetString(PyExc_TypeError, "vector.to_tuple(ndigits): ndigits must be between 0 and 21"); PyErr_SetString(PyExc_TypeError, "vector.to_tuple(ndigits): ndigits must be between 0 and 21");
return NULL; return NULL;
} }
if(PyTuple_GET_SIZE(args)==0)
ndigits= -1;
if(!BaseMath_ReadCallback(self)) if(!BaseMath_ReadCallback(self))
return NULL; return NULL;
ret= PyTuple_New(self->size); return Vector_ToTupleExt(self, ndigits);
for(x = 0; x < self->size; x++) {
PyTuple_SET_ITEM(ret, x, PyFloat_FromDouble(double_round((double)self->vec[x], ndigits)));
}
return ret;
} }
/*----------------------------Vector.toTrackQuat(track, up) ---------------------- */ /*----------------------------Vector.toTrackQuat(track, up) ---------------------- */
@@ -294,7 +310,7 @@ static char Vector_ToTrackQuat_doc[] =
" :return: rotation from the vector and the track and up axis." " :return: rotation from the vector and the track and up axis."
" :rtype: :class:`Quaternion`\n"; " :rtype: :class:`Quaternion`\n";
static PyObject *Vector_ToTrackQuat( VectorObject * self, PyObject * args ) static PyObject *Vector_ToTrackQuat(VectorObject *self, PyObject *args )
{ {
float vec[3], quat[4]; float vec[3], quat[4];
char *strack, *sup; char *strack, *sup;
@@ -413,7 +429,7 @@ static char Vector_Reflect_doc[] =
" :return: The reflected vector matching the size of this vector.\n" " :return: The reflected vector matching the size of this vector.\n"
" :rtype: :class:`Vector`\n"; " :rtype: :class:`Vector`\n";
static PyObject *Vector_Reflect( VectorObject * self, VectorObject * value ) static PyObject *Vector_Reflect(VectorObject *self, VectorObject *value )
{ {
float mirror[3], vec[3]; float mirror[3], vec[3];
float reflect[3] = {0.0f, 0.0f, 0.0f}; float reflect[3] = {0.0f, 0.0f, 0.0f};
@@ -454,7 +470,7 @@ static char Vector_Cross_doc[] =
"\n" "\n"
" .. note:: both vectors must be 3D\n"; " .. note:: both vectors must be 3D\n";
static PyObject *Vector_Cross( VectorObject * self, VectorObject * value ) static PyObject *Vector_Cross(VectorObject *self, VectorObject *value )
{ {
VectorObject *vecCross = NULL; VectorObject *vecCross = NULL;
@@ -486,7 +502,7 @@ static char Vector_Dot_doc[] =
" :return: The dot product.\n" " :return: The dot product.\n"
" :rtype: :class:`Vector`\n"; " :rtype: :class:`Vector`\n";
static PyObject *Vector_Dot( VectorObject * self, VectorObject * value ) static PyObject *Vector_Dot(VectorObject *self, VectorObject *value )
{ {
double dot = 0.0; double dot = 0.0;
int x; int x;
@@ -520,7 +536,7 @@ static char Vector_Angle_doc[] =
" :rtype: float\n" " :rtype: float\n"
"\n" "\n"
" .. note:: Zero length vectors raise an :exc:`AttributeError`.\n"; " .. note:: Zero length vectors raise an :exc:`AttributeError`.\n";
static PyObject *Vector_Angle(VectorObject * self, VectorObject * value) static PyObject *Vector_Angle(VectorObject *self, VectorObject *value)
{ {
double dot = 0.0f, angleRads, test_v1 = 0.0f, test_v2 = 0.0f; double dot = 0.0f, angleRads, test_v1 = 0.0f, test_v2 = 0.0f;
int x, size; int x, size;
@@ -573,7 +589,7 @@ static char Vector_Difference_doc[] =
"\n" "\n"
" .. note:: 2D vectors raise an :exc:`AttributeError`.\n";; " .. note:: 2D vectors raise an :exc:`AttributeError`.\n";;
static PyObject *Vector_Difference( VectorObject * self, VectorObject * value ) static PyObject *Vector_Difference(VectorObject *self, VectorObject *value )
{ {
float quat[4], vec_a[3], vec_b[3]; float quat[4], vec_a[3], vec_b[3];
@@ -607,7 +623,7 @@ static char Vector_Project_doc[] =
" :return projection: the parallel projection vector\n" " :return projection: the parallel projection vector\n"
" :rtype: :class:`Vector`\n"; " :rtype: :class:`Vector`\n";
static PyObject *Vector_Project(VectorObject * self, VectorObject * value) static PyObject *Vector_Project(VectorObject *self, VectorObject *value)
{ {
float vec[4]; float vec[4];
double dot = 0.0f, dot2 = 0.0f; double dot = 0.0f, dot2 = 0.0f;
@@ -655,7 +671,7 @@ static char Vector_Lerp_doc[] =
" :return: The interpolated rotation.\n" " :return: The interpolated rotation.\n"
" :rtype: :class:`Vector`\n"; " :rtype: :class:`Vector`\n";
static PyObject *Vector_Lerp(VectorObject * self, PyObject * args) static PyObject *Vector_Lerp(VectorObject *self, PyObject *args)
{ {
VectorObject *vec2 = NULL; VectorObject *vec2 = NULL;
float fac, ifac, vec[4]; float fac, ifac, vec[4];
@@ -692,7 +708,7 @@ static char Vector_copy_doc[] =
"\n" "\n"
" .. note:: use this to get a copy of a wrapped vector with no reference to the original data.\n"; " .. note:: use this to get a copy of a wrapped vector with no reference to the original data.\n";
static PyObject *Vector_copy(VectorObject * self) static PyObject *Vector_copy(VectorObject *self)
{ {
if(!BaseMath_ReadCallback(self)) if(!BaseMath_ReadCallback(self))
return NULL; return NULL;
@@ -702,45 +718,29 @@ static PyObject *Vector_copy(VectorObject * self)
/*----------------------------print object (internal)------------- /*----------------------------print object (internal)-------------
print the object to screen */ print the object to screen */
static PyObject *Vector_repr(VectorObject * self) static PyObject *Vector_repr(VectorObject *self)
{ {
PyObject *axis[4], *ret; PyObject *ret, *tuple;
int i;
if(!BaseMath_ReadCallback(self)) if(!BaseMath_ReadCallback(self))
return NULL; return NULL;
for(i = 0; i < self->size; i++) tuple= Vector_ToTupleExt(self, -1);
axis[i] = PyFloat_FromDouble(self->vec[i]); ret= PyUnicode_FromFormat("Vector%R", tuple);
Py_DECREF(tuple);
switch(self->size) {
case 2:
ret= PyUnicode_FromFormat("Vector(%R, %R)", axis[0], axis[1]);
break;
case 3:
ret= PyUnicode_FromFormat("Vector(%R, %R, %R)", axis[0], axis[1], axis[2]);
break;
case 4:
ret= PyUnicode_FromFormat("Vector(%R, %R, %R, %R)", axis[0], axis[1], axis[2], axis[3]);
break;
}
for(i = 0; i < self->size; i++)
Py_DECREF(axis[i]);
return ret; return ret;
} }
/*---------------------SEQUENCE PROTOCOLS------------------------ /*---------------------SEQUENCE PROTOCOLS------------------------
----------------------------len(object)------------------------ ----------------------------len(object)------------------------
sequence length*/ sequence length*/
static int Vector_len(VectorObject * self) static int Vector_len(VectorObject *self)
{ {
return self->size; return self->size;
} }
/*----------------------------object[]--------------------------- /*----------------------------object[]---------------------------
sequence accessor (get)*/ sequence accessor (get)*/
static PyObject *Vector_item(VectorObject * self, int i) static PyObject *Vector_item(VectorObject *self, int i)
{ {
if(i<0) i= self->size-i; if(i<0) i= self->size-i;
@@ -757,10 +757,10 @@ static PyObject *Vector_item(VectorObject * self, int i)
} }
/*----------------------------object[]------------------------- /*----------------------------object[]-------------------------
sequence accessor (set)*/ sequence accessor (set)*/
static int Vector_ass_item(VectorObject * self, int i, PyObject * ob) static int Vector_ass_item(VectorObject *self, int i, PyObject * ob)
{ {
float scalar= (float)PyFloat_AsDouble(ob); float scalar;
if(scalar==-1.0f && PyErr_Occurred()) { /* parsed item not a number */ if((scalar=PyFloat_AsDouble(ob))==-1.0f && PyErr_Occurred()) { /* parsed item not a number */
PyErr_SetString(PyExc_TypeError, "vector[index] = x: index argument not a number\n"); PyErr_SetString(PyExc_TypeError, "vector[index] = x: index argument not a number\n");
return -1; return -1;
} }
@@ -780,7 +780,7 @@ static int Vector_ass_item(VectorObject * self, int i, PyObject * ob)
/*----------------------------object[z:y]------------------------ /*----------------------------object[z:y]------------------------
sequence slice (get) */ sequence slice (get) */
static PyObject *Vector_slice(VectorObject * self, int begin, int end) static PyObject *Vector_slice(VectorObject *self, int begin, int end)
{ {
PyObject *list = NULL; PyObject *list = NULL;
int count; int count;
@@ -802,7 +802,7 @@ static PyObject *Vector_slice(VectorObject * self, int begin, int end)
} }
/*----------------------------object[z:y]------------------------ /*----------------------------object[z:y]------------------------
sequence slice (set) */ sequence slice (set) */
static int Vector_ass_slice(VectorObject * self, int begin, int end, static int Vector_ass_slice(VectorObject *self, int begin, int end,
PyObject * seq) PyObject * seq)
{ {
int i, y, size = 0; int i, y, size = 0;
@@ -830,8 +830,7 @@ static int Vector_ass_slice(VectorObject * self, int begin, int end,
return -1; return -1;
} }
scalar= (float)PyFloat_AsDouble(v); if((scalar=PyFloat_AsDouble(v)) == -1.0f && PyErr_Occurred()) { /* parsed item not a number */
if(scalar==-1.0f && PyErr_Occurred()) { /* parsed item not a number */
Py_DECREF(v); Py_DECREF(v);
PyErr_SetString(PyExc_TypeError, "vector[begin:end] = []: sequence argument not a number\n"); PyErr_SetString(PyExc_TypeError, "vector[begin:end] = []: sequence argument not a number\n");
return -1; return -1;
@@ -1124,14 +1123,13 @@ static PyObject *Vector_div(PyObject * v1, PyObject * v2)
if(!BaseMath_ReadCallback(vec1)) if(!BaseMath_ReadCallback(vec1))
return NULL; return NULL;
scalar = (float)PyFloat_AsDouble(v2); if((scalar=PyFloat_AsDouble(v2)) == -1.0f && PyErr_Occurred()) { /* parsed item not a number */
if(scalar== -1.0f && PyErr_Occurred()) { /* parsed item not a number */
PyErr_SetString(PyExc_TypeError, "Vector division: Vector must be divided by a float\n"); PyErr_SetString(PyExc_TypeError, "Vector division: Vector must be divided by a float\n");
return NULL; return NULL;
} }
if(scalar==0.0) { /* not a vector */ if(scalar==0.0) {
PyErr_SetString(PyExc_ZeroDivisionError, "Vector division: divide by zero error.\n"); PyErr_SetString(PyExc_ZeroDivisionError, "Vector division: divide by zero error.\n");
return NULL; return NULL;
} }
@@ -1153,13 +1151,12 @@ static PyObject *Vector_idiv(PyObject * v1, PyObject * v2)
if(!BaseMath_ReadCallback(vec1)) if(!BaseMath_ReadCallback(vec1))
return NULL; return NULL;
scalar = (float)PyFloat_AsDouble(v2); if((scalar=PyFloat_AsDouble(v2)) == -1.0f && PyErr_Occurred()) { /* parsed item not a number */
if(scalar==-1.0f && PyErr_Occurred()) { /* parsed item not a number */
PyErr_SetString(PyExc_TypeError, "Vector division: Vector must be divided by a float\n"); PyErr_SetString(PyExc_TypeError, "Vector division: Vector must be divided by a float\n");
return NULL; return NULL;
} }
if(scalar==0.0) { /* not a vector */ if(scalar==0.0) {
PyErr_SetString(PyExc_ZeroDivisionError, "Vector division: divide by zero error.\n"); PyErr_SetString(PyExc_ZeroDivisionError, "Vector division: divide by zero error.\n");
return NULL; return NULL;
} }
@@ -1414,18 +1411,18 @@ static PyNumberMethods Vector_NumMethods = {
* vector axis, vector.x/y/z/w * vector axis, vector.x/y/z/w
*/ */
static PyObject *Vector_getAxis( VectorObject * self, void *type ) static PyObject *Vector_getAxis(VectorObject *self, void *type )
{ {
return Vector_item(self, GET_INT_FROM_POINTER(type)); return Vector_item(self, GET_INT_FROM_POINTER(type));
} }
static int Vector_setAxis( VectorObject * self, PyObject * value, void * type ) static int Vector_setAxis(VectorObject *self, PyObject * value, void * type )
{ {
return Vector_ass_item(self, GET_INT_FROM_POINTER(type), value); return Vector_ass_item(self, GET_INT_FROM_POINTER(type), value);
} }
/* vector.length */ /* vector.length */
static PyObject *Vector_getLength( VectorObject * self, void *type ) static PyObject *Vector_getLength(VectorObject *self, void *type )
{ {
double dot = 0.0f; double dot = 0.0f;
int i; int i;
@@ -1439,25 +1436,24 @@ static PyObject *Vector_getLength( VectorObject * self, void *type )
return PyFloat_FromDouble(sqrt(dot)); return PyFloat_FromDouble(sqrt(dot));
} }
static int Vector_setLength( VectorObject * self, PyObject * value ) static int Vector_setLength(VectorObject *self, PyObject * value )
{ {
double dot = 0.0f, param; double dot = 0.0f, param;
int i; int i;
if(!BaseMath_ReadCallback(self)) if(!BaseMath_ReadCallback(self))
return -1; return -1;
param= PyFloat_AsDouble( value ); if((param=PyFloat_AsDouble(value)) == -1.0 && PyErr_Occurred()) {
if(param==-1.0 && PyErr_Occurred()) {
PyErr_SetString(PyExc_TypeError, "length must be set to a number"); PyErr_SetString(PyExc_TypeError, "length must be set to a number");
return -1; return -1;
} }
if (param < 0) { if (param < 0.0f) {
PyErr_SetString( PyExc_TypeError, "cannot set a vectors length to a negative value" ); PyErr_SetString( PyExc_TypeError, "cannot set a vectors length to a negative value" );
return -1; return -1;
} }
if (param==0) { if (param == 0.0f) {
for(i = 0; i < self->size; i++){ for(i = 0; i < self->size; i++){
self->vec[i]= 0; self->vec[i]= 0;
} }
@@ -1490,7 +1486,7 @@ static int Vector_setLength( VectorObject * self, PyObject * value )
/* Get a new Vector according to the provided swizzle. This function has little /* Get a new Vector according to the provided swizzle. This function has little
error checking, as we are in control of the inputs: the closure is set by us error checking, as we are in control of the inputs: the closure is set by us
in Vector_createSwizzleGetSeter. */ in Vector_createSwizzleGetSeter. */
static PyObject *Vector_getSwizzle(VectorObject * self, void *closure) static PyObject *Vector_getSwizzle(VectorObject *self, void *closure)
{ {
size_t axisA; size_t axisA;
size_t axisB; size_t axisB;
@@ -1529,7 +1525,7 @@ static PyObject *Vector_getSwizzle(VectorObject * self, void *closure)
Returns 0 on success and -1 on failure. On failure, the vector will be Returns 0 on success and -1 on failure. On failure, the vector will be
unchanged. */ unchanged. */
static int Vector_setSwizzle(VectorObject * self, PyObject * value, void *closure) static int Vector_setSwizzle(VectorObject *self, PyObject * value, void *closure)
{ {
VectorObject *vecVal = NULL; VectorObject *vecVal = NULL;
PyObject *item; PyObject *item;
@@ -1591,21 +1587,20 @@ static int Vector_setSwizzle(VectorObject * self, PyObject * value, void *closur
else if (PyList_Check(value)) else if (PyList_Check(value))
{ {
/* Copy list contents onto swizzled axes. */ /* Copy list contents onto swizzled axes. */
listLen = PyList_Size(value); listLen = PyList_GET_SIZE(value);
swizzleClosure = GET_INT_FROM_POINTER(closure); swizzleClosure = GET_INT_FROM_POINTER(closure);
axisB = 0; axisB = 0;
while (swizzleClosure & SWIZZLE_VALID_AXIS && axisB < listLen) while (swizzleClosure & SWIZZLE_VALID_AXIS && axisB < listLen)
{ {
item = PyList_GetItem(value, axisB); item = PyList_GET_ITEM(value, axisB);
scalarVal = (float)PyFloat_AsDouble(item);
if (scalarVal==-1.0 && PyErr_Occurred()) { if((scalarVal=PyFloat_AsDouble(item))==-1.0 && PyErr_Occurred()) {
PyErr_SetString(PyExc_AttributeError, "Error: list item could not be used as a float.\n"); PyErr_SetString(PyExc_AttributeError, "Error: list item could not be used as a float.\n");
return -1; return -1;
} }
axisA = swizzleClosure & SWIZZLE_AXIS; axisA= swizzleClosure & SWIZZLE_AXIS;
vecTemp[axisA] = scalarVal; vecTemp[axisA] = scalarVal;
swizzleClosure = swizzleClosure >> SWIZZLE_BITS_PER_AXIS; swizzleClosure = swizzleClosure >> SWIZZLE_BITS_PER_AXIS;
@@ -1620,7 +1615,7 @@ static int Vector_setSwizzle(VectorObject * self, PyObject * value, void *closur
memcpy(self->vec, vecTemp, axisB * sizeof(float)); memcpy(self->vec, vecTemp, axisB * sizeof(float));
/* continue with BaseMathObject_WriteCallback at the end */ /* continue with BaseMathObject_WriteCallback at the end */
} }
else if (((scalarVal = (float)PyFloat_AsDouble(value)) == -1.0 && PyErr_Occurred())==0) else if (((scalarVal=PyFloat_AsDouble(value)) == -1 && PyErr_Occurred())==0)
{ {
/* Assign the same value to each axis. */ /* Assign the same value to each axis. */
swizzleClosure = GET_INT_FROM_POINTER(closure); swizzleClosure = GET_INT_FROM_POINTER(closure);
@@ -2086,7 +2081,7 @@ static char Vector_Negate_doc[] =
" :return: an instance of itself\n" " :return: an instance of itself\n"
" :rtype: :class:`Vector`\n"; " :rtype: :class:`Vector`\n";
static PyObject *Vector_Negate(VectorObject * self) static PyObject *Vector_Negate(VectorObject *self)
{ {
int i; int i;
if(!BaseMath_ReadCallback(self)) if(!BaseMath_ReadCallback(self))
@@ -2108,7 +2103,7 @@ static struct PyMethodDef Vector_methods[] = {
{"resize2D", (PyCFunction) Vector_Resize2D, METH_NOARGS, Vector_Resize2D_doc}, {"resize2D", (PyCFunction) Vector_Resize2D, METH_NOARGS, Vector_Resize2D_doc},
{"resize3D", (PyCFunction) Vector_Resize3D, METH_NOARGS, Vector_Resize3D_doc}, {"resize3D", (PyCFunction) Vector_Resize3D, METH_NOARGS, Vector_Resize3D_doc},
{"resize4D", (PyCFunction) Vector_Resize4D, METH_NOARGS, Vector_Resize4D_doc}, {"resize4D", (PyCFunction) Vector_Resize4D, METH_NOARGS, Vector_Resize4D_doc},
{"to_tuple", (PyCFunction) Vector_ToTuple, METH_O, Vector_ToTuple_doc}, {"to_tuple", (PyCFunction) Vector_ToTuple, METH_VARARGS, Vector_ToTuple_doc},
{"to_track_quat", ( PyCFunction ) Vector_ToTrackQuat, METH_VARARGS, Vector_ToTrackQuat_doc}, {"to_track_quat", ( PyCFunction ) Vector_ToTrackQuat, METH_VARARGS, Vector_ToTrackQuat_doc},
{"reflect", ( PyCFunction ) Vector_Reflect, METH_O, Vector_Reflect_doc}, {"reflect", ( PyCFunction ) Vector_Reflect, METH_O, Vector_Reflect_doc},
{"cross", ( PyCFunction ) Vector_Cross, METH_O, Vector_Cross_doc}, {"cross", ( PyCFunction ) Vector_Cross, METH_O, Vector_Cross_doc},
@@ -2136,7 +2131,7 @@ PyTypeObject vector_Type = {
PyVarObject_HEAD_INIT(NULL, 0) PyVarObject_HEAD_INIT(NULL, 0)
/* For printing, in format "<module>.<name>" */ /* For printing, in format "<module>.<name>" */
"vector", /* char *tp_name; */ "vector", /* char *tp_name; */
sizeof( VectorObject ), /* int tp_basicsize; */ sizeof(VectorObject), /* int tp_basicsize; */
0, /* tp_itemsize; For allocation */ 0, /* tp_itemsize; For allocation */
/* Methods to implement standard operations */ /* Methods to implement standard operations */