_new point class and update_
- adds a new point class * point/ vector math (p + v = p, p - p = v, etc.) * points can be transformed by matrices/quats * wraps 'place vector' type vectors that have no magnitude - wrapped toXXX() methods work correctly * toXXX() will NOT wrap data (this is due to the fact that wrapped data cannot be converted) * added a 'wrapped' attribute to mathutils classes to determine wether the object is accessing python or blender data - added the ability to negate vectors/points with "-vec" * deprecated vector.negate() - added the ability to shorhand inverse matrices with "~mat" (tilde) - conversion between vector/point with toXXX() methods
This commit is contained in:
@@ -143,6 +143,35 @@ PyObject *column_vector_multiplication(MatrixObject * mat, VectorObject* vec)
|
||||
}
|
||||
return (PyObject *) newVectorObject(vecNew, vec->size, Py_NEW);
|
||||
}
|
||||
//This is a helper for point/matrix translation
|
||||
PyObject *column_point_multiplication(MatrixObject * mat, PointObject* pt)
|
||||
{
|
||||
float ptNew[4], ptCopy[4];
|
||||
double dot = 0.0f;
|
||||
int x, y, z = 0;
|
||||
|
||||
if(mat->rowSize != pt->size){
|
||||
if(mat->rowSize == 4 && pt->size != 3){
|
||||
return EXPP_ReturnPyObjError(PyExc_AttributeError,
|
||||
"matrix * point: matrix row size and point size must be the same\n");
|
||||
}else{
|
||||
ptCopy[3] = 0.0f;
|
||||
}
|
||||
}
|
||||
|
||||
for(x = 0; x < pt->size; x++){
|
||||
ptCopy[x] = pt->coord[x];
|
||||
}
|
||||
|
||||
for(x = 0; x < mat->rowSize; x++) {
|
||||
for(y = 0; y < mat->colSize; y++) {
|
||||
dot += mat->matrix[x][y] * ptCopy[y];
|
||||
}
|
||||
ptNew[z++] = (float)dot;
|
||||
dot = 0.0f;
|
||||
}
|
||||
return (PyObject *) newPointObject(ptNew, pt->size, Py_NEW);
|
||||
}
|
||||
//-----------------row_vector_multiplication (internal)-----------
|
||||
//ROW VECTOR Multiplication - Vector X Matrix
|
||||
//[x][y][z] * [1][2][3]
|
||||
@@ -178,6 +207,122 @@ PyObject *row_vector_multiplication(VectorObject* vec, MatrixObject * mat)
|
||||
}
|
||||
return (PyObject *) newVectorObject(vecNew, size, Py_NEW);
|
||||
}
|
||||
//This is a helper for the point class
|
||||
PyObject *row_point_multiplication(PointObject* pt, MatrixObject * mat)
|
||||
{
|
||||
float ptNew[4], ptCopy[4];
|
||||
double dot = 0.0f;
|
||||
int x, y, z = 0, size;
|
||||
|
||||
if(mat->colSize != pt->size){
|
||||
if(mat->rowSize == 4 && pt->size != 3){
|
||||
return EXPP_ReturnPyObjError(PyExc_AttributeError,
|
||||
"point * matrix: matrix column size and the point size must be the same\n");
|
||||
}else{
|
||||
ptCopy[3] = 0.0f;
|
||||
}
|
||||
}
|
||||
size = pt->size;
|
||||
for(x = 0; x < pt->size; x++){
|
||||
ptCopy[x] = pt->coord[x];
|
||||
}
|
||||
|
||||
//muliplication
|
||||
for(x = 0; x < mat->colSize; x++) {
|
||||
for(y = 0; y < mat->rowSize; y++) {
|
||||
dot += mat->matrix[y][x] * ptCopy[y];
|
||||
}
|
||||
ptNew[z++] = (float)dot;
|
||||
dot = 0.0f;
|
||||
}
|
||||
return (PyObject *) newPointObject(ptNew, size, Py_NEW);
|
||||
}
|
||||
//-----------------quat_rotation (internal)-----------
|
||||
//This function multiplies a vector/point * quat or vice versa
|
||||
//to rotate the point/vector by the quaternion
|
||||
//arguments should all be 3D
|
||||
PyObject *quat_rotation(PyObject *arg1, PyObject *arg2)
|
||||
{
|
||||
float rot[3];
|
||||
QuaternionObject *quat = NULL;
|
||||
VectorObject *vec = NULL;
|
||||
PointObject *pt = NULL;
|
||||
|
||||
if(QuaternionObject_Check(arg1)){
|
||||
quat = (QuaternionObject*)arg1;
|
||||
if(VectorObject_Check(arg2)){
|
||||
vec = (VectorObject*)arg2;
|
||||
rot[0] = quat->quat[0]*quat->quat[0]*vec->vec[0] + 2*quat->quat[2]*quat->quat[0]*vec->vec[2] -
|
||||
2*quat->quat[3]*quat->quat[0]*vec->vec[1] + quat->quat[1]*quat->quat[1]*vec->vec[0] +
|
||||
2*quat->quat[2]*quat->quat[1]*vec->vec[1] + 2*quat->quat[3]*quat->quat[1]*vec->vec[2] -
|
||||
quat->quat[3]*quat->quat[3]*vec->vec[0] - quat->quat[2]*quat->quat[2]*vec->vec[0];
|
||||
rot[1] = 2*quat->quat[1]*quat->quat[2]*vec->vec[0] + quat->quat[2]*quat->quat[2]*vec->vec[1] +
|
||||
2*quat->quat[3]*quat->quat[2]*vec->vec[2] + 2*quat->quat[0]*quat->quat[3]*vec->vec[0] -
|
||||
quat->quat[3]*quat->quat[3]*vec->vec[1] + quat->quat[0]*quat->quat[0]*vec->vec[1] -
|
||||
2*quat->quat[1]*quat->quat[0]*vec->vec[2] - quat->quat[1]*quat->quat[1]*vec->vec[1];
|
||||
rot[2] = 2*quat->quat[1]*quat->quat[3]*vec->vec[0] + 2*quat->quat[2]*quat->quat[3]*vec->vec[1] +
|
||||
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[1]*quat->quat[1]*vec->vec[2] + quat->quat[0]*quat->quat[0]*vec->vec[2];
|
||||
return (PyObject *) newVectorObject(rot, 3, Py_NEW);
|
||||
}else if(PointObject_Check(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] -
|
||||
2*quat->quat[3]*quat->quat[0]*pt->coord[1] + quat->quat[1]*quat->quat[1]*pt->coord[0] +
|
||||
2*quat->quat[2]*quat->quat[1]*pt->coord[1] + 2*quat->quat[3]*quat->quat[1]*pt->coord[2] -
|
||||
quat->quat[3]*quat->quat[3]*pt->coord[0] - quat->quat[2]*quat->quat[2]*pt->coord[0];
|
||||
rot[1] = 2*quat->quat[1]*quat->quat[2]*pt->coord[0] + quat->quat[2]*quat->quat[2]*pt->coord[1] +
|
||||
2*quat->quat[3]*quat->quat[2]*pt->coord[2] + 2*quat->quat[0]*quat->quat[3]*pt->coord[0] -
|
||||
quat->quat[3]*quat->quat[3]*pt->coord[1] + quat->quat[0]*quat->quat[0]*pt->coord[1] -
|
||||
2*quat->quat[1]*quat->quat[0]*pt->coord[2] - quat->quat[1]*quat->quat[1]*pt->coord[1];
|
||||
rot[2] = 2*quat->quat[1]*quat->quat[3]*pt->coord[0] + 2*quat->quat[2]*quat->quat[3]*pt->coord[1] +
|
||||
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[1]*quat->quat[1]*pt->coord[2] + quat->quat[0]*quat->quat[0]*pt->coord[2];
|
||||
return (PyObject *) newPointObject(rot, 3, Py_NEW);
|
||||
}
|
||||
}else if(VectorObject_Check(arg1)){
|
||||
vec = (VectorObject*)arg1;
|
||||
if(QuaternionObject_Check(arg2)){
|
||||
quat = (QuaternionObject*)arg2;
|
||||
rot[0] = quat->quat[0]*quat->quat[0]*vec->vec[0] + 2*quat->quat[2]*quat->quat[0]*vec->vec[2] -
|
||||
2*quat->quat[3]*quat->quat[0]*vec->vec[1] + quat->quat[1]*quat->quat[1]*vec->vec[0] +
|
||||
2*quat->quat[2]*quat->quat[1]*vec->vec[1] + 2*quat->quat[3]*quat->quat[1]*vec->vec[2] -
|
||||
quat->quat[3]*quat->quat[3]*vec->vec[0] - quat->quat[2]*quat->quat[2]*vec->vec[0];
|
||||
rot[1] = 2*quat->quat[1]*quat->quat[2]*vec->vec[0] + quat->quat[2]*quat->quat[2]*vec->vec[1] +
|
||||
2*quat->quat[3]*quat->quat[2]*vec->vec[2] + 2*quat->quat[0]*quat->quat[3]*vec->vec[0] -
|
||||
quat->quat[3]*quat->quat[3]*vec->vec[1] + quat->quat[0]*quat->quat[0]*vec->vec[1] -
|
||||
2*quat->quat[1]*quat->quat[0]*vec->vec[2] - quat->quat[1]*quat->quat[1]*vec->vec[1];
|
||||
rot[2] = 2*quat->quat[1]*quat->quat[3]*vec->vec[0] + 2*quat->quat[2]*quat->quat[3]*vec->vec[1] +
|
||||
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[1]*quat->quat[1]*vec->vec[2] + quat->quat[0]*quat->quat[0]*vec->vec[2];
|
||||
return (PyObject *) newVectorObject(rot, 3, Py_NEW);
|
||||
}
|
||||
}else if(PointObject_Check(arg1)){
|
||||
pt = (PointObject*)arg1;
|
||||
if(QuaternionObject_Check(arg2)){
|
||||
quat = (QuaternionObject*)arg2;
|
||||
rot[0] = quat->quat[0]*quat->quat[0]*pt->coord[0] + 2*quat->quat[2]*quat->quat[0]*pt->coord[2] -
|
||||
2*quat->quat[3]*quat->quat[0]*pt->coord[1] + quat->quat[1]*quat->quat[1]*pt->coord[0] +
|
||||
2*quat->quat[2]*quat->quat[1]*pt->coord[1] + 2*quat->quat[3]*quat->quat[1]*pt->coord[2] -
|
||||
quat->quat[3]*quat->quat[3]*pt->coord[0] - quat->quat[2]*quat->quat[2]*pt->coord[0];
|
||||
rot[1] = 2*quat->quat[1]*quat->quat[2]*pt->coord[0] + quat->quat[2]*quat->quat[2]*pt->coord[1] +
|
||||
2*quat->quat[3]*quat->quat[2]*pt->coord[2] + 2*quat->quat[0]*quat->quat[3]*pt->coord[0] -
|
||||
quat->quat[3]*quat->quat[3]*pt->coord[1] + quat->quat[0]*quat->quat[0]*pt->coord[1] -
|
||||
2*quat->quat[1]*quat->quat[0]*pt->coord[2] - quat->quat[1]*quat->quat[1]*pt->coord[1];
|
||||
rot[2] = 2*quat->quat[1]*quat->quat[3]*pt->coord[0] + 2*quat->quat[2]*quat->quat[3]*pt->coord[1] +
|
||||
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[1]*quat->quat[1]*pt->coord[2] + quat->quat[0]*quat->quat[0]*pt->coord[2];
|
||||
return (PyObject *) newPointObject(rot, 3, Py_NEW);
|
||||
}
|
||||
}
|
||||
|
||||
return (EXPP_ReturnPyObjError(PyExc_RuntimeError,
|
||||
"quat_rotation(internal): internal problem rotating vector/point\n"));
|
||||
}
|
||||
|
||||
//----------------------------------Mathutils.Rand() --------------------
|
||||
//returns a random number between a high and low value
|
||||
PyObject *M_Mathutils_Rand(PyObject * self, PyObject * args)
|
||||
|
||||
@@ -39,11 +39,15 @@
|
||||
#include "matrix.h"
|
||||
#include "quat.h"
|
||||
#include "euler.h"
|
||||
#include "point.h"
|
||||
#include "Types.h"
|
||||
|
||||
PyObject *Mathutils_Init( void );
|
||||
PyObject *row_vector_multiplication(VectorObject* vec, MatrixObject * mat);
|
||||
PyObject *column_vector_multiplication(MatrixObject * mat, VectorObject* vec);
|
||||
PyObject *row_point_multiplication(PointObject* pt, MatrixObject * mat);
|
||||
PyObject *column_point_multiplication(MatrixObject * mat, PointObject* pt);
|
||||
PyObject *quat_rotation(PyObject *arg1, PyObject *arg2);
|
||||
|
||||
PyObject *M_Mathutils_Rand(PyObject * self, PyObject * args);
|
||||
PyObject *M_Mathutils_Vector(PyObject * self, PyObject * args);
|
||||
|
||||
@@ -85,6 +85,7 @@ void types_InitAll( void )
|
||||
rgbTuple_Type.ob_type = &PyType_Type;
|
||||
vector_Type.ob_type = &PyType_Type;
|
||||
property_Type.ob_type = &PyType_Type;
|
||||
point_Type.ob_type = &PyType_Type;
|
||||
}
|
||||
|
||||
/*****************************************************************************/
|
||||
@@ -169,6 +170,8 @@ PyObject *Types_Init( void )
|
||||
( PyObject * ) &Action_Type );
|
||||
PyDict_SetItemString( dict, "propertyType",
|
||||
( PyObject * ) &property_Type );
|
||||
PyDict_SetItemString( dict, "pointType",
|
||||
( PyObject * ) &point_Type );
|
||||
|
||||
return submodule;
|
||||
}
|
||||
|
||||
@@ -53,6 +53,7 @@ extern PyTypeObject Wave_Type, World_Type;
|
||||
extern PyTypeObject property_Type;
|
||||
extern PyTypeObject buffer_Type, constant_Type, euler_Type;
|
||||
extern PyTypeObject matrix_Type, quaternion_Type, rgbTuple_Type, vector_Type;
|
||||
extern PyTypeObject point_Type;
|
||||
|
||||
PyObject *Types_Init( void );
|
||||
void types_InitAll( void );
|
||||
|
||||
@@ -65,10 +65,7 @@ PyObject *Euler_ToQuat(EulerObject * self)
|
||||
eul[x] = self->eul[x] * ((float)Py_PI / 180);
|
||||
}
|
||||
EulToQuat(eul, quat);
|
||||
if(self->data.blend_data)
|
||||
return (PyObject *) newQuaternionObject(quat, Py_WRAP);
|
||||
else
|
||||
return (PyObject *) newQuaternionObject(quat, Py_NEW);
|
||||
return (PyObject *) newQuaternionObject(quat, Py_NEW);
|
||||
}
|
||||
//----------------------------Euler.toMatrix()---------------------
|
||||
//return a matrix representation of the euler
|
||||
@@ -82,10 +79,7 @@ PyObject *Euler_ToMatrix(EulerObject * self)
|
||||
eul[x] = self->eul[x] * ((float)Py_PI / 180);
|
||||
}
|
||||
EulToMat3(eul, (float (*)[3]) mat);
|
||||
if(self->data.blend_data)
|
||||
return (PyObject *) newMatrixObject(mat, 3, 3 , Py_WRAP);
|
||||
else
|
||||
return (PyObject *) newMatrixObject(mat, 3, 3 , Py_NEW);
|
||||
return (PyObject *) newMatrixObject(mat, 3, 3 , Py_NEW);
|
||||
}
|
||||
//----------------------------Euler.unique()-----------------------
|
||||
//sets the x,y,z values to a unique euler rotation
|
||||
@@ -199,7 +193,12 @@ static PyObject *Euler_getattr(EulerObject * self, char *name)
|
||||
}else if(STREQ(name, "z")){
|
||||
return PyFloat_FromDouble(self->eul[2]);
|
||||
}
|
||||
|
||||
if(STREQ(name, "wrapped")){
|
||||
if(self->wrapped == Py_WRAP)
|
||||
return EXPP_incr_ret((PyObject *)Py_True);
|
||||
else
|
||||
return EXPP_incr_ret((PyObject *)Py_False);
|
||||
}
|
||||
return Py_FindMethod(Euler_methods, (PyObject *) self, name);
|
||||
}
|
||||
//----------------------------setattr()(internal) ------------------
|
||||
@@ -394,6 +393,7 @@ PyObject *newEulerObject(float *eul, int type)
|
||||
if(type == Py_WRAP){
|
||||
self->data.blend_data = eul;
|
||||
self->eul = self->data.blend_data;
|
||||
self->wrapped = Py_WRAP;
|
||||
}else if (type == Py_NEW){
|
||||
self->data.py_data = PyMem_Malloc(3 * sizeof(float));
|
||||
self->eul = self->data.py_data;
|
||||
@@ -406,6 +406,7 @@ PyObject *newEulerObject(float *eul, int type)
|
||||
self->eul[x] = eul[x];
|
||||
}
|
||||
}
|
||||
self->wrapped = Py_NEW;
|
||||
}else{ //bad type
|
||||
return NULL;
|
||||
}
|
||||
|
||||
@@ -45,6 +45,7 @@ typedef struct {
|
||||
float *blend_data; //blender managed
|
||||
}data;
|
||||
float *eul; //1D array of data (alias)
|
||||
int wrapped; //is wrapped data?
|
||||
} EulerObject;
|
||||
|
||||
/*struct data contains a pointer to the actual data that the
|
||||
|
||||
@@ -77,10 +77,7 @@ PyObject *Matrix_toQuat(MatrixObject * self)
|
||||
Mat4ToQuat((float (*)[4])*self->matrix, quat);
|
||||
}
|
||||
|
||||
if(self->data.blend_data)
|
||||
return (PyObject *) newQuaternionObject(quat, Py_WRAP);
|
||||
else
|
||||
return (PyObject *) newQuaternionObject(quat, Py_NEW);
|
||||
return (PyObject *) newQuaternionObject(quat, Py_NEW);
|
||||
}
|
||||
//---------------------------Matrix.toEuler() --------------------
|
||||
PyObject *Matrix_toEuler(MatrixObject * self)
|
||||
@@ -98,10 +95,7 @@ PyObject *Matrix_toEuler(MatrixObject * self)
|
||||
for(x = 0; x < 3; x++) {
|
||||
eul[x] *= (float) (180 / Py_PI);
|
||||
}
|
||||
if(self->data.blend_data)
|
||||
return (PyObject *) newEulerObject(eul, Py_WRAP);
|
||||
else
|
||||
return (PyObject *) newEulerObject(eul, Py_NEW);
|
||||
return (PyObject *) newEulerObject(eul, Py_NEW);
|
||||
}
|
||||
//---------------------------Matrix.resize4x4() ------------------
|
||||
PyObject *Matrix_Resize4x4(MatrixObject * self)
|
||||
@@ -339,7 +333,12 @@ static PyObject *Matrix_getattr(MatrixObject * self, char *name)
|
||||
} else if(STREQ(name, "colSize")) {
|
||||
return PyInt_FromLong((long) self->colSize);
|
||||
}
|
||||
|
||||
if(STREQ(name, "wrapped")){
|
||||
if(self->wrapped == Py_WRAP)
|
||||
return EXPP_incr_ret((PyObject *)Py_True);
|
||||
else
|
||||
return EXPP_incr_ret((PyObject *)Py_False);
|
||||
}
|
||||
return Py_FindMethod(Matrix_methods, (PyObject *) self, name);
|
||||
}
|
||||
//----------------------------setattr()(internal) ----------------
|
||||
@@ -601,6 +600,7 @@ static PyObject *Matrix_mul(PyObject * m1, PyObject * m2)
|
||||
MatrixObject *mat1 = NULL, *mat2 = NULL;
|
||||
PyObject *f = NULL, *retObj = NULL;
|
||||
VectorObject *vec = NULL;
|
||||
PointObject *pt = NULL;
|
||||
|
||||
EXPP_incr2(m1, m2);
|
||||
mat1 = (MatrixObject*)m1;
|
||||
@@ -631,6 +631,11 @@ static PyObject *Matrix_mul(PyObject * m1, PyObject * m2)
|
||||
retObj = column_vector_multiplication(mat1, vec);
|
||||
EXPP_decr3((PyObject*)mat1, (PyObject*)mat2, (PyObject*)vec);
|
||||
return retObj;
|
||||
}else if(PointObject_Check(mat2->coerced_object)){ //MATRIX * POINT
|
||||
pt = (PointObject*)EXPP_incr_ret(mat2->coerced_object);
|
||||
retObj = column_point_multiplication(mat1, pt);
|
||||
EXPP_decr3((PyObject*)mat1, (PyObject*)mat2, (PyObject*)pt);
|
||||
return retObj;
|
||||
}else if (PyFloat_Check(mat2->coerced_object) ||
|
||||
PyInt_Check(mat2->coerced_object)){ // MATRIX * FLOAT/INT
|
||||
f = PyNumber_Float(mat2->coerced_object);
|
||||
@@ -671,6 +676,10 @@ static PyObject *Matrix_mul(PyObject * m1, PyObject * m2)
|
||||
return EXPP_ReturnPyObjError(PyExc_TypeError,
|
||||
"Matrix multiplication: arguments not acceptable for this operation\n");
|
||||
}
|
||||
PyObject* Matrix_inv(MatrixObject *self)
|
||||
{
|
||||
return Matrix_Invert(self);
|
||||
}
|
||||
//------------------------coerce(obj, obj)-----------------------
|
||||
//coercion of unknown types to type MatrixObject for numeric protocols
|
||||
/*Coercion() is called whenever a math operation has 2 operands that
|
||||
@@ -683,7 +692,8 @@ static int Matrix_coerce(PyObject ** m1, PyObject ** m2)
|
||||
{
|
||||
PyObject *coerced = NULL;
|
||||
if(!MatrixObject_Check(*m2)) {
|
||||
if(VectorObject_Check(*m2) || PyFloat_Check(*m2) || PyInt_Check(*m2)) {
|
||||
if(VectorObject_Check(*m2) || PyFloat_Check(*m2) || PyInt_Check(*m2) ||
|
||||
PointObject_Check(*m2)) {
|
||||
coerced = EXPP_incr_ret(*m2);
|
||||
*m2 = newMatrixObject(NULL,3,3,Py_NEW);
|
||||
((MatrixObject*)*m2)->coerced_object = coerced;
|
||||
@@ -718,7 +728,7 @@ static PyNumberMethods Matrix_NumMethods = {
|
||||
(unaryfunc) 0, /* __pos__ */
|
||||
(unaryfunc) 0, /* __abs__ */
|
||||
(inquiry) 0, /* __nonzero__ */
|
||||
(unaryfunc) 0, /* __invert__ */
|
||||
(unaryfunc) Matrix_inv, /* __invert__ */
|
||||
(binaryfunc) 0, /* __lshift__ */
|
||||
(binaryfunc) 0, /* __rshift__ */
|
||||
(binaryfunc) 0, /* __and__ */
|
||||
@@ -794,6 +804,7 @@ PyObject *newMatrixObject(float *mat, int rowSize, int colSize, int type)
|
||||
for(x = 0; x < rowSize; x++) {
|
||||
self->matrix[x] = self->contigPtr + (x * colSize);
|
||||
}
|
||||
self->wrapped = Py_WRAP;
|
||||
}else if (type == Py_NEW){
|
||||
self->data.py_data = PyMem_Malloc(rowSize * colSize * sizeof(float));
|
||||
if(self->data.py_data == NULL) { //allocation failure
|
||||
@@ -822,6 +833,7 @@ PyObject *newMatrixObject(float *mat, int rowSize, int colSize, int type)
|
||||
} else { //or if no arguments are passed return identity matrix
|
||||
Matrix_Identity(self);
|
||||
}
|
||||
self->wrapped = Py_NEW;
|
||||
}else{ //bad type
|
||||
return NULL;
|
||||
}
|
||||
|
||||
@@ -48,6 +48,7 @@ typedef struct _Matrix {
|
||||
float *contigPtr; //1D array of data (alias)
|
||||
int rowSize;
|
||||
int colSize;
|
||||
int wrapped; //is wrapped data?
|
||||
PyObject *coerced_object;
|
||||
} MatrixObject;
|
||||
/*coerced_object is a pointer to the object that it was
|
||||
|
||||
543
source/blender/python/api2_2x/point.c
Normal file
543
source/blender/python/api2_2x/point.c
Normal file
@@ -0,0 +1,543 @@
|
||||
/*
|
||||
*
|
||||
*
|
||||
* ***** BEGIN GPL/BL DUAL LICENSE BLOCK *****
|
||||
*
|
||||
* This program is free software; you can redistribute it and/or
|
||||
* modify it under the terms of the GNU General Public License
|
||||
* as published by the Free Software Foundation; either version 2
|
||||
* of the License, or (at your option) any later version. The Blender
|
||||
* Foundation also sells licenses for use in proprietary software under
|
||||
* the Blender License. See http://www.blender.org/BL/ for information
|
||||
* about this.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program; if not, write to the Free Software Foundation,
|
||||
* Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
|
||||
*
|
||||
* The Original Code is Copyright (C) 2001-2002 by NaN Holding BV.
|
||||
* All rights reserved.
|
||||
*
|
||||
* This is a new part of Blender.
|
||||
*
|
||||
* Contributor(s): Joseph Gilbert
|
||||
*
|
||||
* ***** END GPL/BL DUAL LICENSE BLOCK *****
|
||||
*/
|
||||
#include "Mathutils.h"
|
||||
|
||||
#include "BLI_blenlib.h"
|
||||
#include "BKE_utildefines.h"
|
||||
#include "gen_utils.h"
|
||||
|
||||
//-------------------------DOC STRINGS ---------------------------
|
||||
char Point_Zero_doc[] = "() - set all values in the point to 0";
|
||||
char Point_toVector_doc[] = "() - create a vector representation of this point";
|
||||
//-----------------------METHOD DEFINITIONS ----------------------
|
||||
struct PyMethodDef Point_methods[] = {
|
||||
{"zero", (PyCFunction) Point_Zero, METH_NOARGS, Point_Zero_doc},
|
||||
{"toVector", (PyCFunction) Point_toVector, METH_NOARGS, Point_toVector_doc},
|
||||
{NULL, NULL, 0, NULL}
|
||||
};
|
||||
//-----------------------------METHODS----------------------------
|
||||
//--------------------------Vector.toPoint()----------------------
|
||||
//create a new point object to represent this vector
|
||||
PyObject *Point_toVector(PointObject * self)
|
||||
{
|
||||
float vec[3];
|
||||
int x;
|
||||
|
||||
for(x = 0; x < self->size; x++){
|
||||
vec[x] = self->coord[x];
|
||||
}
|
||||
|
||||
return (PyObject *) newVectorObject(vec, self->size, Py_NEW);
|
||||
}
|
||||
//----------------------------Point.zero() ----------------------
|
||||
//set the point data to 0,0,0
|
||||
PyObject *Point_Zero(PointObject * self)
|
||||
{
|
||||
int x;
|
||||
for(x = 0; x < self->size; x++) {
|
||||
self->coord[x] = 0.0f;
|
||||
}
|
||||
return EXPP_incr_ret((PyObject*)self);
|
||||
}
|
||||
//----------------------------dealloc()(internal) ----------------
|
||||
//free the py_object
|
||||
static void Point_dealloc(PointObject * self)
|
||||
{
|
||||
//only free py_data
|
||||
if(self->data.py_data){
|
||||
PyMem_Free(self->data.py_data);
|
||||
}
|
||||
PyObject_DEL(self);
|
||||
}
|
||||
//----------------------------getattr()(internal) ----------------
|
||||
//object.attribute access (get)
|
||||
static PyObject *Point_getattr(PointObject * self, char *name)
|
||||
{
|
||||
if(STREQ(name,"x")){
|
||||
return PyFloat_FromDouble(self->coord[0]);
|
||||
}else if(STREQ(name, "y")){
|
||||
return PyFloat_FromDouble(self->coord[1]);
|
||||
}else if(STREQ(name, "z")){
|
||||
if(self->size > 2){
|
||||
return PyFloat_FromDouble(self->coord[2]);
|
||||
}else{
|
||||
return EXPP_ReturnPyObjError(PyExc_AttributeError,
|
||||
"point.z: illegal attribute access\n");
|
||||
}
|
||||
}
|
||||
if(STREQ(name, "wrapped")){
|
||||
if(self->wrapped == Py_WRAP)
|
||||
return EXPP_incr_ret((PyObject *)Py_True);
|
||||
else
|
||||
return EXPP_incr_ret((PyObject *)Py_False);
|
||||
}
|
||||
return Py_FindMethod(Point_methods, (PyObject *) self, name);
|
||||
}
|
||||
//----------------------------setattr()(internal) ----------------
|
||||
//object.attribute access (set)
|
||||
static int Point_setattr(PointObject * self, char *name, PyObject * v)
|
||||
{
|
||||
PyObject *f = NULL;
|
||||
|
||||
f = PyNumber_Float(v);
|
||||
if(f == NULL) { // parsed item not a number
|
||||
return EXPP_ReturnIntError(PyExc_TypeError,
|
||||
"point.attribute = x: argument not a number\n");
|
||||
}
|
||||
|
||||
if(STREQ(name,"x")){
|
||||
self->coord[0] = (float)PyFloat_AS_DOUBLE(f);
|
||||
}else if(STREQ(name, "y")){
|
||||
self->coord[1] = (float)PyFloat_AS_DOUBLE(f);
|
||||
}else if(STREQ(name, "z")){
|
||||
if(self->size > 2){
|
||||
self->coord[2] = (float)PyFloat_AS_DOUBLE(f);
|
||||
}else{
|
||||
Py_DECREF(f);
|
||||
return EXPP_ReturnIntError(PyExc_AttributeError,
|
||||
"point.z = x: illegal attribute access\n");
|
||||
}
|
||||
}else{
|
||||
Py_DECREF(f);
|
||||
return EXPP_ReturnIntError(PyExc_AttributeError,
|
||||
"point.attribute = x: unknown attribute\n");
|
||||
}
|
||||
|
||||
Py_DECREF(f);
|
||||
return 0;
|
||||
}
|
||||
//----------------------------print object (internal)-------------
|
||||
//print the object to screen
|
||||
static PyObject *Point_repr(PointObject * self)
|
||||
{
|
||||
int i;
|
||||
char buffer[48], str[1024];
|
||||
|
||||
BLI_strncpy(str,"[",1024);
|
||||
for(i = 0; i < self->size; i++){
|
||||
if(i < (self->size - 1)){
|
||||
sprintf(buffer, "%.6f, ", self->coord[i]);
|
||||
strcat(str,buffer);
|
||||
}else{
|
||||
sprintf(buffer, "%.6f", self->coord[i]);
|
||||
strcat(str,buffer);
|
||||
}
|
||||
}
|
||||
strcat(str, "](point)");
|
||||
|
||||
return EXPP_incr_ret(PyString_FromString(str));
|
||||
}
|
||||
//---------------------SEQUENCE PROTOCOLS------------------------
|
||||
//----------------------------len(object)------------------------
|
||||
//sequence length
|
||||
static int Point_len(PointObject * self)
|
||||
{
|
||||
return self->size;
|
||||
}
|
||||
//----------------------------object[]---------------------------
|
||||
//sequence accessor (get)
|
||||
static PyObject *Point_item(PointObject * self, int i)
|
||||
{
|
||||
if(i < 0 || i >= self->size)
|
||||
return EXPP_ReturnPyObjError(PyExc_IndexError,
|
||||
"point[attribute]: array index out of range\n");
|
||||
|
||||
return Py_BuildValue("f", self->coord[i]);
|
||||
|
||||
}
|
||||
//----------------------------object[]-------------------------
|
||||
//sequence accessor (set)
|
||||
static int Point_ass_item(PointObject * self, int i, PyObject * ob)
|
||||
{
|
||||
PyObject *f = NULL;
|
||||
|
||||
f = PyNumber_Float(ob);
|
||||
if(f == NULL) { // parsed item not a number
|
||||
return EXPP_ReturnIntError(PyExc_TypeError,
|
||||
"point[attribute] = x: argument not a number\n");
|
||||
}
|
||||
|
||||
if(i < 0 || i >= self->size){
|
||||
Py_DECREF(f);
|
||||
return EXPP_ReturnIntError(PyExc_IndexError,
|
||||
"point[attribute] = x: array assignment index out of range\n");
|
||||
}
|
||||
self->coord[i] = (float)PyFloat_AS_DOUBLE(f);
|
||||
Py_DECREF(f);
|
||||
return 0;
|
||||
}
|
||||
//----------------------------object[z:y]------------------------
|
||||
//sequence slice (get)
|
||||
static PyObject *Point_slice(PointObject * self, int begin, int end)
|
||||
{
|
||||
PyObject *list = NULL;
|
||||
int count;
|
||||
|
||||
CLAMP(begin, 0, self->size);
|
||||
CLAMP(end, 0, self->size);
|
||||
begin = MIN2(begin,end);
|
||||
|
||||
list = PyList_New(end - begin);
|
||||
for(count = begin; count < end; count++) {
|
||||
PyList_SetItem(list, count - begin,
|
||||
PyFloat_FromDouble(self->coord[count]));
|
||||
}
|
||||
|
||||
return list;
|
||||
}
|
||||
//----------------------------object[z:y]------------------------
|
||||
//sequence slice (set)
|
||||
static int Point_ass_slice(PointObject * self, int begin, int end,
|
||||
PyObject * seq)
|
||||
{
|
||||
int i, y, size = 0;
|
||||
float coord[3];
|
||||
|
||||
CLAMP(begin, 0, self->size);
|
||||
CLAMP(end, 0, self->size);
|
||||
begin = MIN2(begin,end);
|
||||
|
||||
size = PySequence_Length(seq);
|
||||
if(size != (end - begin)){
|
||||
return EXPP_ReturnIntError(PyExc_TypeError,
|
||||
"point[begin:end] = []: size mismatch in slice assignment\n");
|
||||
}
|
||||
|
||||
for (i = 0; i < size; i++) {
|
||||
PyObject *v, *f;
|
||||
|
||||
v = PySequence_GetItem(seq, i);
|
||||
if (v == NULL) { // Failed to read sequence
|
||||
return EXPP_ReturnIntError(PyExc_RuntimeError,
|
||||
"point[begin:end] = []: unable to read sequence\n");
|
||||
}
|
||||
f = PyNumber_Float(v);
|
||||
if(f == NULL) { // parsed item not a number
|
||||
Py_DECREF(v);
|
||||
return EXPP_ReturnIntError(PyExc_TypeError,
|
||||
"point[begin:end] = []: sequence argument not a number\n");
|
||||
}
|
||||
coord[i] = (float)PyFloat_AS_DOUBLE(f);
|
||||
EXPP_decr2(f,v);
|
||||
}
|
||||
//parsed well - now set in point
|
||||
for(y = 0; y < size; y++){
|
||||
self->coord[begin + y] = coord[y];
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
//------------------------NUMERIC PROTOCOLS----------------------
|
||||
//------------------------obj + obj------------------------------
|
||||
//addition
|
||||
static PyObject *Point_add(PyObject * v1, PyObject * v2)
|
||||
{
|
||||
int x, size;
|
||||
float coord[3];
|
||||
PointObject *coord1 = NULL, *coord2 = NULL;
|
||||
VectorObject *vec = NULL;
|
||||
|
||||
EXPP_incr2(v1, v2);
|
||||
coord1 = (PointObject*)v1;
|
||||
coord2 = (PointObject*)v2;
|
||||
|
||||
if(!coord1->coerced_object){
|
||||
if(coord2->coerced_object){
|
||||
if(VectorObject_Check(coord2->coerced_object)){ //POINT + VECTOR
|
||||
//Point translation
|
||||
vec = (VectorObject*)EXPP_incr_ret(coord2->coerced_object);
|
||||
size = coord1->size;
|
||||
if(vec->size == size){
|
||||
for(x = 0; x < size; x++){
|
||||
coord[x] = coord1->coord[x] + vec->vec[x];
|
||||
}
|
||||
}else{
|
||||
EXPP_decr3((PyObject*)coord1, (PyObject*)coord2, (PyObject*)vec);
|
||||
return EXPP_ReturnPyObjError(PyExc_AttributeError,
|
||||
"Point addition: arguments are the wrong size....\n");
|
||||
}
|
||||
EXPP_decr3((PyObject*)coord1, (PyObject*)coord2, (PyObject*)vec);
|
||||
return (PyObject *) newPointObject(coord, size, Py_NEW);
|
||||
}
|
||||
}else{ //POINT + POINT
|
||||
size = coord1->size;
|
||||
if(coord2->size == size){
|
||||
for(x = 0; x < size; x++) {
|
||||
coord[x] = coord1->coord[x] + coord2->coord[x];
|
||||
}
|
||||
}else{
|
||||
EXPP_decr2((PyObject*)coord1, (PyObject*)coord2);
|
||||
return EXPP_ReturnPyObjError(PyExc_AttributeError,
|
||||
"Point addition: arguments are the wrong size....\n");
|
||||
}
|
||||
EXPP_decr2((PyObject*)coord1, (PyObject*)coord2);
|
||||
return (PyObject *) newPointObject(coord, size, Py_NEW);
|
||||
}
|
||||
}
|
||||
|
||||
EXPP_decr2((PyObject*)coord1, (PyObject*)coord2);
|
||||
return EXPP_ReturnPyObjError(PyExc_AttributeError,
|
||||
"Point addition: arguments not valid for this operation....\n");
|
||||
}
|
||||
//------------------------obj - obj------------------------------
|
||||
//subtraction
|
||||
static PyObject *Point_sub(PyObject * v1, PyObject * v2)
|
||||
{
|
||||
int x, size;
|
||||
float coord[3];
|
||||
PointObject *coord1 = NULL, *coord2 = NULL;
|
||||
|
||||
EXPP_incr2(v1, v2);
|
||||
coord1 = (PointObject*)v1;
|
||||
coord2 = (PointObject*)v2;
|
||||
|
||||
if(coord1->coerced_object || coord2->coerced_object){
|
||||
return EXPP_ReturnPyObjError(PyExc_AttributeError,
|
||||
"Point subtraction: arguments not valid for this operation....\n");
|
||||
}
|
||||
if(coord1->size != coord2->size){
|
||||
EXPP_decr2((PyObject*)coord1, (PyObject*)coord2);
|
||||
return EXPP_ReturnPyObjError(PyExc_AttributeError,
|
||||
"Point subtraction: points must have the same dimensions for this operation\n");
|
||||
}
|
||||
|
||||
size = coord1->size;
|
||||
for(x = 0; x < size; x++) {
|
||||
coord[x] = coord1->coord[x] - coord2->coord[x];
|
||||
}
|
||||
|
||||
//Point - Point = Vector
|
||||
EXPP_decr2((PyObject*)coord1, (PyObject*)coord2);
|
||||
return (PyObject *) newVectorObject(coord, size, Py_NEW);
|
||||
}
|
||||
//------------------------obj * obj------------------------------
|
||||
//mulplication
|
||||
static PyObject *Point_mul(PyObject * p1, PyObject * p2)
|
||||
{
|
||||
int x, size;
|
||||
float coord[3], scalar;
|
||||
PointObject *coord1 = NULL, *coord2 = NULL;
|
||||
PyObject *f = NULL, *retObj = NULL;
|
||||
MatrixObject *mat = NULL;
|
||||
QuaternionObject *quat = NULL;
|
||||
|
||||
EXPP_incr2(p1, p2);
|
||||
coord1 = (PointObject*)p1;
|
||||
coord2 = (PointObject*)p2;
|
||||
|
||||
if(coord1->coerced_object){
|
||||
if (PyFloat_Check(coord1->coerced_object) ||
|
||||
PyInt_Check(coord1->coerced_object)){ // FLOAT/INT * POINT
|
||||
f = PyNumber_Float(coord1->coerced_object);
|
||||
if(f == NULL) { // parsed item not a number
|
||||
EXPP_decr2((PyObject*)coord1, (PyObject*)coord2);
|
||||
return EXPP_ReturnPyObjError(PyExc_TypeError,
|
||||
"Point multiplication: arguments not acceptable for this operation\n");
|
||||
}
|
||||
scalar = (float)PyFloat_AS_DOUBLE(f);
|
||||
size = coord2->size;
|
||||
for(x = 0; x < size; x++) {
|
||||
coord[x] = coord2->coord[x] * scalar;
|
||||
}
|
||||
EXPP_decr2((PyObject*)coord1, (PyObject*)coord2);
|
||||
return (PyObject *) newPointObject(coord, size, Py_NEW);
|
||||
}
|
||||
}else{
|
||||
if(coord2->coerced_object){
|
||||
if (PyFloat_Check(coord2->coerced_object) ||
|
||||
PyInt_Check(coord2->coerced_object)){ // POINT * FLOAT/INT
|
||||
f = PyNumber_Float(coord2->coerced_object);
|
||||
if(f == NULL) { // parsed item not a number
|
||||
EXPP_decr2((PyObject*)coord1, (PyObject*)coord2);
|
||||
return EXPP_ReturnPyObjError(PyExc_TypeError,
|
||||
"Point multiplication: arguments not acceptable for this operation\n");
|
||||
}
|
||||
scalar = (float)PyFloat_AS_DOUBLE(f);
|
||||
size = coord1->size;
|
||||
for(x = 0; x < size; x++) {
|
||||
coord[x] = coord1->coord[x] * scalar;
|
||||
}
|
||||
EXPP_decr2((PyObject*)coord1, (PyObject*)coord2);
|
||||
return (PyObject *) newPointObject(coord, size, Py_NEW);
|
||||
}else if(MatrixObject_Check(coord2->coerced_object)){ //POINT * MATRIX
|
||||
mat = (MatrixObject*)EXPP_incr_ret(coord2->coerced_object);
|
||||
retObj = row_point_multiplication(coord1, mat);
|
||||
EXPP_decr3((PyObject*)coord1, (PyObject*)coord2, (PyObject*)mat);
|
||||
return retObj;
|
||||
}else if(QuaternionObject_Check(coord2->coerced_object)){ //POINT * QUATERNION
|
||||
quat = (QuaternionObject*)EXPP_incr_ret(coord2->coerced_object);
|
||||
if(coord1->size != 3){
|
||||
EXPP_decr2((PyObject*)coord1, (PyObject*)coord2);
|
||||
return EXPP_ReturnPyObjError(PyExc_TypeError,
|
||||
"Point multiplication: only 3D point rotations (with quats) currently supported\n");
|
||||
}
|
||||
retObj = 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,
|
||||
"Point multiplication: arguments not acceptable for this operation\n");
|
||||
}
|
||||
//-------------------------- -obj -------------------------------
|
||||
//returns the negative of this object
|
||||
static PyObject *Point_neg(PointObject *self)
|
||||
{
|
||||
int x;
|
||||
for(x = 0; x < self->size; x++){
|
||||
self->coord[x] = -self->coord[x];
|
||||
}
|
||||
|
||||
return EXPP_incr_ret((PyObject *)self);
|
||||
}
|
||||
//------------------------coerce(obj, obj)-----------------------
|
||||
//coercion of unknown types to type PointObject for numeric protocols
|
||||
/*Coercion() is called whenever a math operation has 2 operands that
|
||||
it doesn't understand how to evaluate. 2+Matrix for example. We want to
|
||||
evaluate some of these operations like: (vector * 2), however, for math
|
||||
to proceed, the unknown operand must be cast to a type that python math will
|
||||
understand. (e.g. in the case above case, 2 must be cast to a vector and
|
||||
then call vector.multiply(vector, scalar_cast_as_vector)*/
|
||||
static int Point_coerce(PyObject ** p1, PyObject ** p2)
|
||||
{
|
||||
PyObject *coerced = NULL;
|
||||
|
||||
if(!PointObject_Check(*p2)) {
|
||||
if(VectorObject_Check(*p2) || PyFloat_Check(*p2) || PyInt_Check(*p2) ||
|
||||
MatrixObject_Check(*p2) || QuaternionObject_Check(*p2)) {
|
||||
coerced = EXPP_incr_ret(*p2);
|
||||
*p2 = newPointObject(NULL,3,Py_NEW);
|
||||
((PointObject*)*p2)->coerced_object = coerced;
|
||||
}else{
|
||||
return EXPP_ReturnIntError(PyExc_TypeError,
|
||||
"point.coerce(): unknown operand - can't coerce for numeric protocols\n");
|
||||
}
|
||||
}
|
||||
EXPP_incr2(*p1, *p2);
|
||||
return 0;
|
||||
}
|
||||
//-----------------PROTCOL DECLARATIONS--------------------------
|
||||
static PySequenceMethods Point_SeqMethods = {
|
||||
(inquiry) Point_len, /* sq_length */
|
||||
(binaryfunc) 0, /* sq_concat */
|
||||
(intargfunc) 0, /* sq_repeat */
|
||||
(intargfunc) Point_item, /* sq_item */
|
||||
(intintargfunc) Point_slice, /* sq_slice */
|
||||
(intobjargproc) Point_ass_item, /* sq_ass_item */
|
||||
(intintobjargproc) Point_ass_slice, /* sq_ass_slice */
|
||||
};
|
||||
static PyNumberMethods Point_NumMethods = {
|
||||
(binaryfunc) Point_add, /* __add__ */
|
||||
(binaryfunc) Point_sub, /* __sub__ */
|
||||
(binaryfunc) Point_mul, /* __mul__ */
|
||||
(binaryfunc) 0, /* __div__ */
|
||||
(binaryfunc) 0, /* __mod__ */
|
||||
(binaryfunc) 0, /* __divmod__ */
|
||||
(ternaryfunc) 0, /* __pow__ */
|
||||
(unaryfunc) Point_neg, /* __neg__ */
|
||||
(unaryfunc) 0, /* __pos__ */
|
||||
(unaryfunc) 0, /* __abs__ */
|
||||
(inquiry) 0, /* __nonzero__ */
|
||||
(unaryfunc) 0, /* __invert__ */
|
||||
(binaryfunc) 0, /* __lshift__ */
|
||||
(binaryfunc) 0, /* __rshift__ */
|
||||
(binaryfunc) 0, /* __and__ */
|
||||
(binaryfunc) 0, /* __xor__ */
|
||||
(binaryfunc) 0, /* __or__ */
|
||||
(coercion) Point_coerce, /* __coerce__ */
|
||||
(unaryfunc) 0, /* __int__ */
|
||||
(unaryfunc) 0, /* __long__ */
|
||||
(unaryfunc) 0, /* __float__ */
|
||||
(unaryfunc) 0, /* __oct__ */
|
||||
(unaryfunc) 0, /* __hex__ */
|
||||
|
||||
};
|
||||
//------------------PY_OBECT DEFINITION--------------------------
|
||||
PyTypeObject point_Type = {
|
||||
PyObject_HEAD_INIT(NULL)
|
||||
0, /*ob_size */
|
||||
"point", /*tp_name */
|
||||
sizeof(PointObject), /*tp_basicsize */
|
||||
0, /*tp_itemsize */
|
||||
(destructor) Point_dealloc, /*tp_dealloc */
|
||||
(printfunc) 0, /*tp_print */
|
||||
(getattrfunc) Point_getattr, /*tp_getattr */
|
||||
(setattrfunc) Point_setattr, /*tp_setattr */
|
||||
0, /*tp_compare */
|
||||
(reprfunc) Point_repr, /*tp_repr */
|
||||
&Point_NumMethods, /*tp_as_number */
|
||||
&Point_SeqMethods, /*tp_as_sequence */
|
||||
};
|
||||
//------------------------newPointObject (internal)-------------
|
||||
//creates a new point object
|
||||
/*pass Py_WRAP - if point is a WRAPPER for data allocated by BLENDER
|
||||
(i.e. it was allocated elsewhere by MEM_mallocN())
|
||||
pass Py_NEW - if point is not a WRAPPER and managed by PYTHON
|
||||
(i.e. it must be created here with PyMEM_malloc())*/
|
||||
PyObject *newPointObject(float *coord, int size, int type)
|
||||
{
|
||||
PointObject *self;
|
||||
int x;
|
||||
|
||||
point_Type.ob_type = &PyType_Type;
|
||||
self = PyObject_NEW(PointObject, &point_Type);
|
||||
self->data.blend_data = NULL;
|
||||
self->data.py_data = NULL;
|
||||
if(size > 3 || size < 2)
|
||||
return NULL;
|
||||
self->size = size;
|
||||
self->coerced_object = NULL;
|
||||
|
||||
if(type == Py_WRAP){
|
||||
self->data.blend_data = coord;
|
||||
self->coord = self->data.blend_data;
|
||||
self->wrapped = Py_WRAP;
|
||||
}else if (type == Py_NEW){
|
||||
self->data.py_data = PyMem_Malloc(size * sizeof(float));
|
||||
self->coord = self->data.py_data;
|
||||
if(!coord) { //new empty
|
||||
for(x = 0; x < size; x++){
|
||||
self->coord[x] = 0.0f;
|
||||
}
|
||||
}else{
|
||||
for(x = 0; x < size; x++){
|
||||
self->coord[x] = coord[x];
|
||||
}
|
||||
}
|
||||
self->wrapped = Py_NEW;
|
||||
}else{ //bad type
|
||||
return NULL;
|
||||
}
|
||||
return (PyObject *) EXPP_incr_ret((PyObject *)self);
|
||||
}
|
||||
65
source/blender/python/api2_2x/point.h
Normal file
65
source/blender/python/api2_2x/point.h
Normal file
@@ -0,0 +1,65 @@
|
||||
/*
|
||||
*
|
||||
*
|
||||
* ***** BEGIN GPL/BL DUAL LICENSE BLOCK *****
|
||||
*
|
||||
* This program is free software; you can redistribute it and/or
|
||||
* modify it under the terms of the GNU General Public License
|
||||
* as published by the Free Software Foundation; either version 2
|
||||
* of the License, or (at your option) any later version. The Blender
|
||||
* Foundation also sells licenses for use in proprietary software under
|
||||
* the Blender License. See http://www.blender.org/BL/ for information
|
||||
* about this.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program; if not, write to the Free Software Foundation,
|
||||
* Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
|
||||
*
|
||||
* The Original Code is Copyright (C) 2001-2002 by NaN Holding BV.
|
||||
* All rights reserved.
|
||||
*
|
||||
* This is a new part of Blender.
|
||||
*
|
||||
* Contributor(s): Joseph Gilbert
|
||||
*
|
||||
* ***** END GPL/BL DUAL LICENSE BLOCK *****
|
||||
*/
|
||||
|
||||
#ifndef EXPP_point_h
|
||||
#define EXPP_point_h
|
||||
|
||||
#include <Python.h>
|
||||
|
||||
#define PointObject_Check(v) ((v)->ob_type == &point_Type)
|
||||
|
||||
typedef struct {
|
||||
PyObject_VAR_HEAD
|
||||
struct{
|
||||
float *py_data; //python managed
|
||||
float *blend_data; //blender managed
|
||||
}data;
|
||||
float *coord; //1D array of data (alias)
|
||||
int size;
|
||||
int wrapped; //is wrapped data?
|
||||
PyObject *coerced_object;
|
||||
} PointObject;
|
||||
/*coerced_object is a pointer to the object that it was
|
||||
coerced from when a dummy vector needs to be created from
|
||||
the coerce() function for numeric protocol operations*/
|
||||
|
||||
/*struct data contains a pointer to the actual data that the
|
||||
object uses. It can use either PyMem allocated data (which will
|
||||
be stored in py_data) or be a wrapper for data allocated through
|
||||
blender (stored in blend_data). This is an either/or struct not both*/
|
||||
|
||||
//prototypes
|
||||
PyObject *Point_Zero( PointObject * self );
|
||||
PyObject *Point_toVector(PointObject * self);
|
||||
PyObject *newPointObject(float *coord, int size, int type);
|
||||
|
||||
#endif /* EXPP_point_h */
|
||||
@@ -68,10 +68,7 @@ PyObject *Quaternion_ToEuler(QuaternionObject * self)
|
||||
for(x = 0; x < 3; x++) {
|
||||
eul[x] *= (180 / (float)Py_PI);
|
||||
}
|
||||
if(self->data.blend_data)
|
||||
return newEulerObject(eul, Py_WRAP);
|
||||
else
|
||||
return newEulerObject(eul, Py_NEW);
|
||||
return newEulerObject(eul, Py_NEW);
|
||||
}
|
||||
//----------------------------Quaternion.toMatrix()------------------
|
||||
//return the quat as a matrix
|
||||
@@ -80,10 +77,7 @@ PyObject *Quaternion_ToMatrix(QuaternionObject * self)
|
||||
float mat[9] = {0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f};
|
||||
QuatToMat3(self->quat, (float (*)[3]) mat);
|
||||
|
||||
if(self->data.blend_data)
|
||||
return newMatrixObject(mat, 3, 3, Py_WRAP);
|
||||
else
|
||||
return newMatrixObject(mat, 3, 3, Py_NEW);
|
||||
return newMatrixObject(mat, 3, 3, Py_NEW);
|
||||
}
|
||||
//----------------------------Quaternion.normalize()----------------
|
||||
//normalize the axis of rotation of [theta,vector]
|
||||
@@ -193,6 +187,12 @@ static PyObject *Quaternion_getattr(QuaternionObject * self, char *name)
|
||||
Normalise(vec);
|
||||
return (PyObject *) newVectorObject(vec, 3, Py_NEW);
|
||||
}
|
||||
if(STREQ(name, "wrapped")){
|
||||
if(self->wrapped == Py_WRAP)
|
||||
return EXPP_incr_ret((PyObject *)Py_True);
|
||||
else
|
||||
return EXPP_incr_ret((PyObject *)Py_False);
|
||||
}
|
||||
|
||||
return Py_FindMethod(Quaternion_methods, (PyObject *) self, name);
|
||||
}
|
||||
@@ -397,11 +397,12 @@ static PyObject *Quaternion_sub(PyObject * q1, PyObject * q2)
|
||||
static PyObject *Quaternion_mul(PyObject * q1, PyObject * q2)
|
||||
{
|
||||
int x;
|
||||
float quat[4], scalar, newVec[3];
|
||||
float quat[4], scalar;
|
||||
double dot = 0.0f;
|
||||
QuaternionObject *quat1 = NULL, *quat2 = NULL;
|
||||
PyObject *f = NULL;
|
||||
PyObject *f = NULL, *retObj = NULL;
|
||||
VectorObject *vec = NULL;
|
||||
PointObject *pt = NULL;
|
||||
|
||||
EXPP_incr2(q1, q2);
|
||||
quat1 = (QuaternionObject*)q1;
|
||||
@@ -446,32 +447,19 @@ static PyObject *Quaternion_mul(PyObject * q1, PyObject * q2)
|
||||
return EXPP_ReturnPyObjError(PyExc_TypeError,
|
||||
"Quaternion multiplication: only 3D vector rotations currently supported\n");
|
||||
}
|
||||
newVec[0] = quat1->quat[0]*quat1->quat[0]*vec->vec[0] +
|
||||
2*quat1->quat[2]*quat1->quat[0]*vec->vec[2] -
|
||||
2*quat1->quat[3]*quat1->quat[0]*vec->vec[1] +
|
||||
quat1->quat[1]*quat1->quat[1]*vec->vec[0] +
|
||||
2*quat1->quat[2]*quat1->quat[1]*vec->vec[1] +
|
||||
2*quat1->quat[3]*quat1->quat[1]*vec->vec[2] -
|
||||
quat1->quat[3]*quat1->quat[3]*vec->vec[0] -
|
||||
quat1->quat[2]*quat1->quat[2]*vec->vec[0];
|
||||
newVec[1] = 2*quat1->quat[1]*quat1->quat[2]*vec->vec[0] +
|
||||
quat1->quat[2]*quat1->quat[2]*vec->vec[1] +
|
||||
2*quat1->quat[3]*quat1->quat[2]*vec->vec[2] +
|
||||
2*quat1->quat[0]*quat1->quat[3]*vec->vec[0] -
|
||||
quat1->quat[3]*quat1->quat[3]*vec->vec[1] +
|
||||
quat1->quat[0]*quat1->quat[0]*vec->vec[1] -
|
||||
2*quat1->quat[1]*quat1->quat[0]*vec->vec[2] -
|
||||
quat1->quat[1]*quat1->quat[1]*vec->vec[1];
|
||||
newVec[2] = 2*quat1->quat[1]*quat1->quat[3]*vec->vec[0] +
|
||||
2*quat1->quat[2]*quat1->quat[3]*vec->vec[1] +
|
||||
quat1->quat[3]*quat1->quat[3]*vec->vec[2] -
|
||||
2*quat1->quat[0]*quat1->quat[2]*vec->vec[0] -
|
||||
quat1->quat[2]*quat1->quat[2]*vec->vec[2] +
|
||||
2*quat1->quat[0]*quat1->quat[1]*vec->vec[1] -
|
||||
quat1->quat[1]*quat1->quat[1]*vec->vec[2] +
|
||||
quat1->quat[0]*quat1->quat[0]*vec->vec[2];
|
||||
retObj = quat_rotation((PyObject*)quat1, (PyObject*)vec);
|
||||
EXPP_decr3((PyObject*)quat1, (PyObject*)quat2, (PyObject*)vec);
|
||||
return newVectorObject(newVec,3,Py_NEW);
|
||||
return retObj;
|
||||
}else if(PointObject_Check(quat2->coerced_object)){ //QUAT * POINT
|
||||
pt = (PointObject*)EXPP_incr_ret(quat2->coerced_object);
|
||||
if(pt->size != 3){
|
||||
EXPP_decr2((PyObject*)quat1, (PyObject*)quat2);
|
||||
return EXPP_ReturnPyObjError(PyExc_TypeError,
|
||||
"Quaternion multiplication: only 3D point rotations currently supported\n");
|
||||
}
|
||||
retObj = quat_rotation((PyObject*)quat1, (PyObject*)pt);
|
||||
EXPP_decr3((PyObject*)quat1, (PyObject*)quat2, (PyObject*)pt);
|
||||
return retObj;
|
||||
}
|
||||
}else{ //QUAT * QUAT (dot product)
|
||||
for(x = 0; x < 4; x++) {
|
||||
@@ -499,7 +487,8 @@ static int Quaternion_coerce(PyObject ** q1, PyObject ** q2)
|
||||
PyObject *coerced = NULL;
|
||||
|
||||
if(!QuaternionObject_Check(*q2)) {
|
||||
if(VectorObject_Check(*q2) || PyFloat_Check(*q2) || PyInt_Check(*q2)) {
|
||||
if(VectorObject_Check(*q2) || PyFloat_Check(*q2) || PyInt_Check(*q2) ||
|
||||
PointObject_Check(*q2)) {
|
||||
coerced = EXPP_incr_ret(*q2);
|
||||
*q2 = newQuaternionObject(NULL,Py_NEW);
|
||||
((QuaternionObject*)*q2)->coerced_object = coerced;
|
||||
@@ -583,6 +572,7 @@ PyObject *newQuaternionObject(float *quat, int type)
|
||||
if(type == Py_WRAP){
|
||||
self->data.blend_data = quat;
|
||||
self->quat = self->data.blend_data;
|
||||
self->wrapped = Py_WRAP;
|
||||
}else if (type == Py_NEW){
|
||||
self->data.py_data = PyMem_Malloc(4 * sizeof(float));
|
||||
self->quat = self->data.py_data;
|
||||
@@ -593,6 +583,7 @@ PyObject *newQuaternionObject(float *quat, int type)
|
||||
self->quat[x] = quat[x];
|
||||
}
|
||||
}
|
||||
self->wrapped = Py_NEW;
|
||||
}else{ //bad type
|
||||
return NULL;
|
||||
}
|
||||
|
||||
@@ -45,6 +45,7 @@ typedef struct {
|
||||
float *blend_data; //blender managed
|
||||
}data;
|
||||
float *quat; //1D array of data (alias)
|
||||
int wrapped; //is wrapped data?
|
||||
PyObject *coerced_object;
|
||||
} QuaternionObject;
|
||||
/*coerced_object is a pointer to the object that it was
|
||||
|
||||
@@ -42,6 +42,7 @@ char Vector_Negate_doc[] = "() - changes vector to it's additive inverse";
|
||||
char Vector_Resize2D_doc[] = "() - resize a vector to [x,y]";
|
||||
char Vector_Resize3D_doc[] = "() - resize a vector to [x,y,z]";
|
||||
char Vector_Resize4D_doc[] = "() - resize a vector to [x,y,z,w]";
|
||||
char Vector_toPoint_doc[] = "() - create a new Point Object from this vector";
|
||||
//-----------------------METHOD DEFINITIONS ----------------------
|
||||
struct PyMethodDef Vector_methods[] = {
|
||||
{"zero", (PyCFunction) Vector_Zero, METH_NOARGS, Vector_Zero_doc},
|
||||
@@ -50,9 +51,27 @@ struct PyMethodDef Vector_methods[] = {
|
||||
{"resize2D", (PyCFunction) Vector_Resize2D, METH_NOARGS, Vector_Resize2D_doc},
|
||||
{"resize3D", (PyCFunction) Vector_Resize3D, METH_NOARGS, Vector_Resize2D_doc},
|
||||
{"resize4D", (PyCFunction) Vector_Resize4D, METH_NOARGS, Vector_Resize2D_doc},
|
||||
{"toPoint", (PyCFunction) Vector_toPoint, METH_NOARGS, Vector_toPoint_doc},
|
||||
{NULL, NULL, 0, NULL}
|
||||
};
|
||||
//-----------------------------METHODS----------------------------
|
||||
//--------------------------Vector.toPoint()----------------------
|
||||
//create a new point object to represent this vector
|
||||
PyObject *Vector_toPoint(VectorObject * self)
|
||||
{
|
||||
float coord[3];
|
||||
int x;
|
||||
|
||||
if(self->size < 2 || self->size > 3) {
|
||||
return EXPP_ReturnPyObjError(PyExc_AttributeError,
|
||||
"Vector.toPoint(): inappropriate vector size - expects 2d or 3d vector\n");
|
||||
}
|
||||
for(x = 0; x < self->size; x++){
|
||||
coord[x] = self->vec[x];
|
||||
}
|
||||
|
||||
return (PyObject *) newPointObject(coord, self->size, Py_NEW);
|
||||
}
|
||||
//----------------------------Vector.zero() ----------------------
|
||||
//set the vector data to 0,0,0
|
||||
PyObject *Vector_Zero(VectorObject * self)
|
||||
@@ -79,16 +98,6 @@ PyObject *Vector_Normalize(VectorObject * self)
|
||||
}
|
||||
return EXPP_incr_ret((PyObject*)self);
|
||||
}
|
||||
//----------------------------Vector.negate() --------------------
|
||||
//set the vector to it's negative -x, -y, -z
|
||||
PyObject *Vector_Negate(VectorObject * self)
|
||||
{
|
||||
int x;
|
||||
for(x = 0; x < self->size; x++) {
|
||||
self->vec[x] = -(self->vec[x]);
|
||||
}
|
||||
return EXPP_incr_ret((PyObject*)self);
|
||||
}
|
||||
//----------------------------Vector.resize2D() ------------------
|
||||
//resize the vector to x,y
|
||||
PyObject *Vector_Resize2D(VectorObject * self)
|
||||
@@ -196,7 +205,12 @@ static PyObject *Vector_getattr(VectorObject * self, char *name)
|
||||
}
|
||||
return PyFloat_FromDouble(sqrt(dot));
|
||||
}
|
||||
|
||||
if(STREQ(name, "wrapped")){
|
||||
if(self->wrapped == Py_WRAP)
|
||||
return EXPP_incr_ret((PyObject *)Py_True);
|
||||
else
|
||||
return EXPP_incr_ret((PyObject *)Py_False);
|
||||
}
|
||||
return Py_FindMethod(Vector_methods, (PyObject *) self, name);
|
||||
}
|
||||
//----------------------------setattr()(internal) ----------------
|
||||
@@ -368,28 +382,47 @@ static PyObject *Vector_add(PyObject * v1, PyObject * v2)
|
||||
int x, size;
|
||||
float vec[4];
|
||||
VectorObject *vec1 = NULL, *vec2 = NULL;
|
||||
PointObject *pt = NULL;
|
||||
|
||||
EXPP_incr2(v1, v2);
|
||||
vec1 = (VectorObject*)v1;
|
||||
vec2 = (VectorObject*)v2;
|
||||
|
||||
if(vec1->coerced_object || vec2->coerced_object){
|
||||
return EXPP_ReturnPyObjError(PyExc_AttributeError,
|
||||
"Vector addition: arguments not valid for this operation....\n");
|
||||
}
|
||||
if(vec1->size != vec2->size){
|
||||
EXPP_decr2((PyObject*)vec1, (PyObject*)vec2);
|
||||
return EXPP_ReturnPyObjError(PyExc_AttributeError,
|
||||
"Vector addition: vectors must have the same dimensions for this operation\n");
|
||||
if(!vec1->coerced_object){
|
||||
if(vec2->coerced_object){
|
||||
if(PointObject_Check(vec2->coerced_object)){ //VECTOR + POINT
|
||||
//Point translation
|
||||
pt = (PointObject*)EXPP_incr_ret(vec2->coerced_object);
|
||||
size = vec1->size;
|
||||
if(pt->size == size){
|
||||
for(x = 0; x < size; x++){
|
||||
vec[x] = vec1->vec[x] + pt->coord[x];
|
||||
}
|
||||
}else{
|
||||
EXPP_decr3((PyObject*)vec1, (PyObject*)vec2, (PyObject*)pt);
|
||||
return EXPP_ReturnPyObjError(PyExc_AttributeError,
|
||||
"Vector addition: arguments are the wrong size....\n");
|
||||
}
|
||||
EXPP_decr3((PyObject*)vec1, (PyObject*)vec2, (PyObject*)pt);
|
||||
return (PyObject *) newPointObject(vec, size, Py_NEW);
|
||||
}
|
||||
}else{ //VECTOR + VECTOR
|
||||
if(vec1->size != vec2->size){
|
||||
EXPP_decr2((PyObject*)vec1, (PyObject*)vec2);
|
||||
return EXPP_ReturnPyObjError(PyExc_AttributeError,
|
||||
"Vector addition: vectors must have the same dimensions for this operation\n");
|
||||
}
|
||||
size = vec1->size;
|
||||
for(x = 0; x < size; x++) {
|
||||
vec[x] = vec1->vec[x] + vec2->vec[x];
|
||||
}
|
||||
EXPP_decr2((PyObject*)vec1, (PyObject*)vec2);
|
||||
return (PyObject *) newVectorObject(vec, size, Py_NEW);
|
||||
}
|
||||
}
|
||||
|
||||
size = vec1->size;
|
||||
for(x = 0; x < size; x++) {
|
||||
vec[x] = vec1->vec[x] + vec2->vec[x];
|
||||
}
|
||||
|
||||
EXPP_decr2((PyObject*)vec1, (PyObject*)vec2);
|
||||
return (PyObject *) newVectorObject(vec, size, Py_NEW);
|
||||
return EXPP_ReturnPyObjError(PyExc_AttributeError,
|
||||
"Vector addition: arguments not valid for this operation....\n");
|
||||
}
|
||||
//------------------------obj - obj------------------------------
|
||||
//subtraction
|
||||
@@ -426,7 +459,7 @@ static PyObject *Vector_sub(PyObject * v1, PyObject * v2)
|
||||
static PyObject *Vector_mul(PyObject * v1, PyObject * v2)
|
||||
{
|
||||
int x, size;
|
||||
float vec[4], scalar, newVec[3];
|
||||
float vec[4], scalar;
|
||||
double dot = 0.0f;
|
||||
VectorObject *vec1 = NULL, *vec2 = NULL;
|
||||
PyObject *f = NULL, *retObj = NULL;
|
||||
@@ -476,39 +509,16 @@ static PyObject *Vector_mul(PyObject * v1, PyObject * v2)
|
||||
}
|
||||
EXPP_decr2((PyObject*)vec1, (PyObject*)vec2);
|
||||
return (PyObject *) newVectorObject(vec, size, Py_NEW);
|
||||
}else if(QuaternionObject_Check(vec2->coerced_object)){ //QUAT * VEC
|
||||
}else if(QuaternionObject_Check(vec2->coerced_object)){ //VECTOR * QUATERNION
|
||||
quat = (QuaternionObject*)EXPP_incr_ret(vec2->coerced_object);
|
||||
if(vec1->size != 3){
|
||||
EXPP_decr2((PyObject*)vec1, (PyObject*)vec2);
|
||||
return EXPP_ReturnPyObjError(PyExc_TypeError,
|
||||
"Vector multiplication: only 3D vector rotations (with quats) currently supported\n");
|
||||
}
|
||||
newVec[0] = quat->quat[0]*quat->quat[0]*vec1->vec[0] +
|
||||
2*quat->quat[2]*quat->quat[0]*vec1->vec[2] -
|
||||
2*quat->quat[3]*quat->quat[0]*vec1->vec[1] +
|
||||
quat->quat[1]*quat->quat[1]*vec1->vec[0] +
|
||||
2*quat->quat[2]*quat->quat[1]*vec1->vec[1] +
|
||||
2*quat->quat[3]*quat->quat[1]*vec1->vec[2] -
|
||||
quat->quat[3]*quat->quat[3]*vec1->vec[0] -
|
||||
quat->quat[2]*quat->quat[2]*vec1->vec[0];
|
||||
newVec[1] = 2*quat->quat[1]*quat->quat[2]*vec1->vec[0] +
|
||||
quat->quat[2]*quat->quat[2]*vec1->vec[1] +
|
||||
2*quat->quat[3]*quat->quat[2]*vec1->vec[2] +
|
||||
2*quat->quat[0]*quat->quat[3]*vec1->vec[0] -
|
||||
quat->quat[3]*quat->quat[3]*vec1->vec[1] +
|
||||
quat->quat[0]*quat->quat[0]*vec1->vec[1] -
|
||||
2*quat->quat[1]*quat->quat[0]*vec1->vec[2] -
|
||||
quat->quat[1]*quat->quat[1]*vec1->vec[1];
|
||||
newVec[2] = 2*quat->quat[1]*quat->quat[3]*vec1->vec[0] +
|
||||
2*quat->quat[2]*quat->quat[3]*vec1->vec[1] +
|
||||
quat->quat[3]*quat->quat[3]*vec1->vec[2] -
|
||||
2*quat->quat[0]*quat->quat[2]*vec1->vec[0] -
|
||||
quat->quat[2]*quat->quat[2]*vec1->vec[2] +
|
||||
2*quat->quat[0]*quat->quat[1]*vec1->vec[1] -
|
||||
quat->quat[1]*quat->quat[1]*vec1->vec[2] +
|
||||
quat->quat[0]*quat->quat[0]*vec1->vec[2];
|
||||
retObj = quat_rotation((PyObject*)vec1, (PyObject*)quat);
|
||||
EXPP_decr3((PyObject*)vec1, (PyObject*)vec2, (PyObject*)quat);
|
||||
return newVectorObject(newVec,3,Py_NEW);
|
||||
return retObj;
|
||||
}
|
||||
}else{ //VECTOR * VECTOR
|
||||
if(vec1->size != vec2->size){
|
||||
@@ -530,35 +540,16 @@ static PyObject *Vector_mul(PyObject * v1, PyObject * v2)
|
||||
return EXPP_ReturnPyObjError(PyExc_TypeError,
|
||||
"Vector multiplication: arguments not acceptable for this operation\n");
|
||||
}
|
||||
//------------------------obj / obj------------------------------
|
||||
//division
|
||||
static PyObject *Vector_div(PyObject * v1, PyObject * v2)
|
||||
//-------------------------- -obj -------------------------------
|
||||
//returns the negative of this object
|
||||
static PyObject *Vector_neg(VectorObject *self)
|
||||
{
|
||||
int x, size;
|
||||
float vec[4];
|
||||
VectorObject *vec1 = NULL, *vec2 = NULL;
|
||||
|
||||
EXPP_incr2(v1, v2);
|
||||
vec1 = (VectorObject*)v1;
|
||||
vec2 = (VectorObject*)v2;
|
||||
|
||||
if(vec1->coerced_object || vec2->coerced_object){
|
||||
return EXPP_ReturnPyObjError(PyExc_AttributeError,
|
||||
"Vector division: arguments not valid for this operation....\n");
|
||||
}
|
||||
if(vec1->size != vec2->size){
|
||||
EXPP_decr2((PyObject*)vec1, (PyObject*)vec2);
|
||||
return EXPP_ReturnPyObjError(PyExc_AttributeError,
|
||||
"Vector division: vectors must have the same dimensions for this operation\n");
|
||||
int x;
|
||||
for(x = 0; x < self->size; x++){
|
||||
self->vec[x] = -self->vec[x];
|
||||
}
|
||||
|
||||
size = vec1->size;
|
||||
for(x = 0; x < size; x++) {
|
||||
vec[x] = vec1->vec[x] / vec2->vec[x];
|
||||
}
|
||||
|
||||
EXPP_decr2((PyObject*)vec1, (PyObject*)vec2);
|
||||
return (PyObject *) newVectorObject(vec, size, Py_NEW);
|
||||
return EXPP_incr_ret((PyObject *)self);
|
||||
}
|
||||
//------------------------coerce(obj, obj)-----------------------
|
||||
//coercion of unknown types to type VectorObject for numeric protocols
|
||||
@@ -573,7 +564,8 @@ static int Vector_coerce(PyObject ** v1, PyObject ** v2)
|
||||
PyObject *coerced = NULL;
|
||||
|
||||
if(!VectorObject_Check(*v2)) {
|
||||
if(MatrixObject_Check(*v2) || PyFloat_Check(*v2) || PyInt_Check(*v2) || QuaternionObject_Check(*v2)) {
|
||||
if(MatrixObject_Check(*v2) || PyFloat_Check(*v2) || PyInt_Check(*v2) ||
|
||||
QuaternionObject_Check(*v2) || PointObject_Check(*v2)) {
|
||||
coerced = EXPP_incr_ret(*v2);
|
||||
*v2 = newVectorObject(NULL,3,Py_NEW);
|
||||
((VectorObject*)*v2)->coerced_object = coerced;
|
||||
@@ -599,11 +591,11 @@ static PyNumberMethods Vector_NumMethods = {
|
||||
(binaryfunc) Vector_add, /* __add__ */
|
||||
(binaryfunc) Vector_sub, /* __sub__ */
|
||||
(binaryfunc) Vector_mul, /* __mul__ */
|
||||
(binaryfunc) Vector_div, /* __div__ */
|
||||
(binaryfunc) 0, /* __div__ */
|
||||
(binaryfunc) 0, /* __mod__ */
|
||||
(binaryfunc) 0, /* __divmod__ */
|
||||
(ternaryfunc) 0, /* __pow__ */
|
||||
(unaryfunc) 0, /* __neg__ */
|
||||
(unaryfunc) Vector_neg, /* __neg__ */
|
||||
(unaryfunc) 0, /* __pos__ */
|
||||
(unaryfunc) 0, /* __abs__ */
|
||||
(inquiry) 0, /* __nonzero__ */
|
||||
@@ -652,12 +644,15 @@ PyObject *newVectorObject(float *vec, int size, int type)
|
||||
self = PyObject_NEW(VectorObject, &vector_Type);
|
||||
self->data.blend_data = NULL;
|
||||
self->data.py_data = NULL;
|
||||
if(size > 4 || size < 2)
|
||||
return NULL;
|
||||
self->size = size;
|
||||
self->coerced_object = NULL;
|
||||
|
||||
if(type == Py_WRAP){
|
||||
self->data.blend_data = vec;
|
||||
self->vec = self->data.blend_data;
|
||||
self->wrapped = Py_WRAP;
|
||||
}else if (type == Py_NEW){
|
||||
self->data.py_data = PyMem_Malloc(size * sizeof(float));
|
||||
self->vec = self->data.py_data;
|
||||
@@ -672,9 +667,25 @@ PyObject *newVectorObject(float *vec, int size, int type)
|
||||
self->vec[x] = vec[x];
|
||||
}
|
||||
}
|
||||
self->wrapped = Py_NEW;
|
||||
}else{ //bad type
|
||||
return NULL;
|
||||
}
|
||||
return (PyObject *) EXPP_incr_ret((PyObject *)self);
|
||||
}
|
||||
|
||||
//#############################DEPRECATED################################
|
||||
//#######################################################################
|
||||
//----------------------------Vector.negate() --------------------
|
||||
//set the vector to it's negative -x, -y, -z
|
||||
PyObject *Vector_Negate(VectorObject * self)
|
||||
{
|
||||
int x;
|
||||
for(x = 0; x < self->size; x++) {
|
||||
self->vec[x] = -(self->vec[x]);
|
||||
}
|
||||
printf("Vector.negate(): Deprecated: use -vector instead\n");
|
||||
return EXPP_incr_ret((PyObject*)self);
|
||||
}
|
||||
//#######################################################################
|
||||
//#############################DEPRECATED################################
|
||||
@@ -45,6 +45,7 @@ typedef struct {
|
||||
}data;
|
||||
float *vec; //1D array of data (alias)
|
||||
int size;
|
||||
int wrapped; //is wrapped data?
|
||||
PyObject *coerced_object;
|
||||
} VectorObject;
|
||||
/*coerced_object is a pointer to the object that it was
|
||||
@@ -63,6 +64,7 @@ PyObject *Vector_Negate( VectorObject * self );
|
||||
PyObject *Vector_Resize2D( VectorObject * self );
|
||||
PyObject *Vector_Resize3D( VectorObject * self );
|
||||
PyObject *Vector_Resize4D( VectorObject * self );
|
||||
PyObject *Vector_toPoint( VectorObject * self );
|
||||
PyObject *newVectorObject(float *vec, int size, int type);
|
||||
|
||||
#endif /* EXPP_vector_h */
|
||||
|
||||
Reference in New Issue
Block a user