Blender Python API

- Removed the gen_utils.c dependency from Mathutils (since gen_utils wont go into 2.5 but mathutils will), repalced with python functions.
- removed Blender.Mathutils.Point, since it was not documented, the C api never used it, none of our scripts used it (and I never saw a script that used it).
This commit is contained in:
2009-04-21 09:44:29 +00:00
parent 6c5e18ca98
commit 3e7cbd5388
23 changed files with 467 additions and 1202 deletions

View File

@@ -70,6 +70,7 @@
#include "api2_2x/Object.h"
#include "api2_2x/Registry.h"
#include "api2_2x/Pose.h"
#include "api2_2x/Mathutils.h"
#include "api2_2x/bpy.h" /* for the new "bpy" module */
#include "api2_2x/bpy_internal_import.h"

View File

@@ -32,7 +32,7 @@
#define EXPP_Geometry_H
#include <Python.h>
#include "vector.h"
#include "Mathutils.h"
PyObject *Geometry_Init( void );

View File

@@ -46,7 +46,7 @@
#include "gen_utils.h"
#include "gen_library.h"
#include "vector.h"
#include "Mathutils.h"
/* checks for the group being removed */
#define GROUP_DEL_CHECK_PY(bpy_group) if (!(bpy_group->group)) return ( EXPP_ReturnPyObjError( PyExc_RuntimeError, "Group has been removed" ) )

View File

@@ -29,7 +29,7 @@
*/
#include "Key.h" /*This must come first*/
#include "vector.h"
#include "Mathutils.h"
#include "DNA_scene_types.h"

View File

@@ -69,7 +69,6 @@ static char M_Mathutils_TriangleArea_doc[] = "(v1, v2, v3) - returns the area si
static char M_Mathutils_TriangleNormal_doc[] = "(v1, v2, v3) - returns the normal of the 3D triangle defined";
static char M_Mathutils_QuadNormal_doc[] = "(v1, v2, v3, v4) - returns the normal of the 3D quad defined";
static char M_Mathutils_LineIntersect_doc[] = "(v1, v2, v3, v4) - returns a tuple with the points on each line respectively closest to the other";
static char M_Mathutils_Point_doc[] = "Creates a 2d or 3d point object";
//-----------------------METHOD DEFINITIONS ----------------------
struct PyMethodDef M_Mathutils_methods[] = {
{"Rand", (PyCFunction) M_Mathutils_Rand, METH_VARARGS, M_Mathutils_Rand_doc},
@@ -103,7 +102,6 @@ struct PyMethodDef M_Mathutils_methods[] = {
{"TriangleNormal", ( PyCFunction ) M_Mathutils_TriangleNormal, METH_VARARGS, M_Mathutils_TriangleNormal_doc},
{"QuadNormal", ( PyCFunction ) M_Mathutils_QuadNormal, METH_VARARGS, M_Mathutils_QuadNormal_doc},
{"LineIntersect", ( PyCFunction ) M_Mathutils_LineIntersect, METH_VARARGS, M_Mathutils_LineIntersect_doc},
{"Point", (PyCFunction) M_Mathutils_Point, METH_VARARGS, M_Mathutils_Point_doc},
{NULL, NULL, 0, NULL}
};
/*----------------------------MODULE INIT-------------------------*/
@@ -184,36 +182,7 @@ PyObject *column_vector_multiplication(MatrixObject * mat, VectorObject* vec)
}
return 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 newPointObject(ptNew, pt->size, Py_NEW);
}
//-----------------row_vector_multiplication (internal)-----------
//ROW VECTOR Multiplication - Vector X Matrix
//[x][y][z] * [1][2][3]
@@ -249,36 +218,7 @@ PyObject *row_vector_multiplication(VectorObject* vec, MatrixObject * mat)
}
return newVectorObject(vecNew, vec_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 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
@@ -288,7 +228,6 @@ 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;
@@ -307,21 +246,6 @@ PyObject *quat_rotation(PyObject *arg1, PyObject *arg2)
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 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 newPointObject(rot, 3, Py_NEW);
}
}else if(VectorObject_Check(arg1)){
vec = (VectorObject*)arg1;
@@ -341,24 +265,6 @@ PyObject *quat_rotation(PyObject *arg1, PyObject *arg2)
quat->quat[1]*quat->quat[1]*vec->vec[2] + quat->quat[0]*quat->quat[0]*vec->vec[2];
return 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 newPointObject(rot, 3, Py_NEW);
}
}
return (EXPP_ReturnPyObjError(PyExc_RuntimeError,
@@ -1329,60 +1235,7 @@ PyObject *M_Mathutils_Euler(PyObject * self, PyObject * args)
Py_DECREF(listObject);
return newEulerObject(eul, Py_NEW);
}
//----------------------------------POINT FUNCTIONS---------------------
//----------------------------------Mathutils.Point() ------------------
PyObject *M_Mathutils_Point(PyObject * self, PyObject * args)
{
PyObject *listObject = NULL;
int size, i;
float point[3];
PyObject *v, *f;
size = PySequence_Length(args);
if (size == 1) {
listObject = PySequence_GetItem(args, 0);
if (PySequence_Check(listObject)) {
size = PySequence_Length(listObject);
} else { // Single argument was not a sequence
Py_XDECREF(listObject);
return EXPP_ReturnPyObjError(PyExc_TypeError,
"Mathutils.Point(): 2-3 floats or ints expected (optionally in a sequence)\n");
}
} else if (size == 0) {
//returns a new empty 3d point
return newPointObject(NULL, 3, Py_NEW);
} else {
listObject = EXPP_incr_ret(args);
}
if (size<2 || size>3) { // Invalid vector size
Py_XDECREF(listObject);
return EXPP_ReturnPyObjError(PyExc_AttributeError,
"Mathutils.Point(): 2-3 floats or ints expected (optionally in a sequence)\n");
}
for (i=0; i<size; i++) {
v=PySequence_GetItem(listObject, i);
if (v==NULL) { // Failed to read sequence
Py_XDECREF(listObject);
return EXPP_ReturnPyObjError(PyExc_RuntimeError,
"Mathutils.Point(): 2-3 floats or ints expected (optionally in a sequence)\n");
}
f=PyNumber_Float(v);
if(f==NULL) { // parsed item not a number
Py_DECREF(v);
Py_XDECREF(listObject);
return EXPP_ReturnPyObjError(PyExc_TypeError,
"Mathutils.Point(): 2-3 floats or ints expected (optionally in a sequence)\n");
}
point[i]=(float)PyFloat_AS_DOUBLE(f);
EXPP_decr2(f,v);
}
Py_DECREF(listObject);
return newPointObject(point, size, Py_NEW);
}
//---------------------------------INTERSECTION FUNCTIONS--------------------
//----------------------------------Mathutils.Intersect() -------------------
PyObject *M_Mathutils_Intersect( PyObject * self, PyObject * args )
@@ -1775,5 +1628,41 @@ PyObject *M_Mathutils_VecMultMat(PyObject * self, PyObject * args)
return row_vector_multiplication(vec, mat);
}
/* Utility functions */
/*---------------------- EXPP_FloatsAreEqual -------------------------
Floating point comparisons
floatStep = number of representable floats allowable in between
float A and float B to be considered equal. */
int EXPP_FloatsAreEqual(float A, float B, int floatSteps)
{
int a, b, delta;
assert(floatSteps > 0 && floatSteps < (4 * 1024 * 1024));
a = *(int*)&A;
if (a < 0)
a = 0x80000000 - a;
b = *(int*)&B;
if (b < 0)
b = 0x80000000 - b;
delta = abs(a - b);
if (delta <= floatSteps)
return 1;
return 0;
}
/*---------------------- EXPP_VectorsAreEqual -------------------------
Builds on EXPP_FloatsAreEqual to test vectors */
int EXPP_VectorsAreEqual(float *vecA, float *vecB, int size, int floatSteps){
int x;
for (x=0; x< size; x++){
if (EXPP_FloatsAreEqual(vecA[x], vecB[x], floatSteps) == 0)
return 0;
}
return 1;
}
//#######################################################################
//#############################DEPRECATED################################

View File

@@ -36,13 +36,10 @@
#include "matrix.h"
#include "quat.h"
#include "euler.h"
#include "point.h"
PyObject *Mathutils_Init( const char * from );
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);
@@ -69,7 +66,6 @@ PyObject *M_Mathutils_TriangleArea( PyObject * self, PyObject * args );
PyObject *M_Mathutils_TriangleNormal( PyObject * self, PyObject * args );
PyObject *M_Mathutils_QuadNormal( PyObject * self, PyObject * args );
PyObject *M_Mathutils_LineIntersect( PyObject * self, PyObject * args );
PyObject *M_Mathutils_Point(PyObject * self, PyObject * args);
//DEPRECATED
PyObject *M_Mathutils_CopyMat(PyObject * self, PyObject * args);
PyObject *M_Mathutils_CopyVec(PyObject * self, PyObject * args);
@@ -79,4 +75,25 @@ PyObject *M_Mathutils_RotateEuler(PyObject * self, PyObject * args);
PyObject *M_Mathutils_MatMultVec(PyObject * self, PyObject * args);
PyObject *M_Mathutils_VecMultMat(PyObject * self, PyObject * args);
int EXPP_FloatsAreEqual(float A, float B, int floatSteps);
int EXPP_VectorsAreEqual(float *vecA, float *vecB, int size, int floatSteps);
#define Py_PI 3.14159265358979323846
#define Py_WRAP 1024
#define Py_NEW 2048
/* Allow us to build with Py3k */
#if (PY_VERSION_HEX >= 0x03000000)
#define PyString_FromString PyUnicode_FromString
#define intobjargproc ssizeobjargproc
#define intintobjargproc ssizessizeobjargproc
#define intargfunc ssizeargfunc
#define intintargfunc ssizessizeargfunc
#endif
#endif /* EXPP_Mathutils_H */

View File

@@ -40,7 +40,7 @@
#include "BLI_blenlib.h"
#include "gen_utils.h"
#include "vector.h"
#include "Mathutils.h"
static PyObject *Node_repr( BPy_Node * self );
static int Node_compare(BPy_Node *a, BPy_Node *b);

View File

@@ -85,7 +85,7 @@ struct View3D;
#include "BKE_utildefines.h" /* vec copy */
#include "vector.h"
#include "Mathutils.h"
PyObject *M_Object_Get( PyObject * self, PyObject * args ); /* from Object.c */

View File

@@ -53,7 +53,7 @@
#include "gen_utils.h"
#include "gen_library.h"
#include "vector.h" /* for Texture_evaluate(vec) */
#include "Mathutils.h" /* for Texture_evaluate(vec) */
#include "Material.h" /* for EXPP_Colorband_fromPyList and EXPP_PyList_fromColorband */
#include "RE_shader_ext.h"

View File

@@ -60,7 +60,6 @@ extern PyTypeObject 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;
extern PyTypeObject Modifier_Type, ModSeq_Type;
extern PyTypeObject EditBone_Type;
extern PyTypeObject ThemeSpace_Type;
@@ -246,7 +245,6 @@ void types_InitAll( void )
PyType_Ready( &rgbTuple_Type );
vector_Type.ob_type = &PyType_Type;
property_Type.ob_type = &PyType_Type;
point_Type.ob_type = &PyType_Type;
PyType_Ready( &Modifier_Type );
PyType_Ready( &ModSeq_Type );
PyType_Ready( &EditBone_Type );
@@ -358,8 +356,6 @@ PyObject *Types_Init( void )
( PyObject * ) &Pose_Type );
PyDict_SetItemString( dict, "propertyType",
( PyObject * ) &property_Type );
PyDict_SetItemString( dict, "pointType",
( PyObject * ) &point_Type );
PyDict_SetItemString( dict, "ModifierType",
( PyObject * ) &Modifier_Type );
PyDict_SetItemString( dict, "ModSeqType",

View File

@@ -31,7 +31,6 @@
#include "BLI_arithb.h"
#include "BKE_utildefines.h"
#include "BLI_blenlib.h"
#include "gen_utils.h"
//-------------------------DOC STRINGS ---------------------------
@@ -128,7 +127,8 @@ PyObject *Euler_Unique(EulerObject * self)
self->eul[1] = (float)(pitch * 180 / (float)Py_PI);
self->eul[2] = (float)(bank * 180 / (float)Py_PI);
return EXPP_incr_ret((PyObject*)self);
Py_INCREF(self);
return (PyObject *)self;
}
//----------------------------Euler.zero()-------------------------
//sets the euler to 0,0,0
@@ -138,7 +138,8 @@ PyObject *Euler_Zero(EulerObject * self)
self->eul[1] = 0.0;
self->eul[2] = 0.0;
return EXPP_incr_ret((PyObject*)self);
Py_INCREF(self);
return (PyObject *)self;
}
//----------------------------Euler.rotate()-----------------------
//rotates a euler a certain amount and returns the result
@@ -150,12 +151,12 @@ PyObject *Euler_Rotate(EulerObject * self, PyObject *args)
int x;
if(!PyArg_ParseTuple(args, "fs", &angle, &axis)){
return EXPP_ReturnPyObjError(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;
}
if(!STREQ3(axis,"x","y","z")){
return EXPP_ReturnPyObjError(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;
}
//covert to radians
@@ -169,7 +170,8 @@ PyObject *Euler_Rotate(EulerObject * self, PyObject *args)
self->eul[x] *= (180 / (float)Py_PI);
}
return EXPP_incr_ret((PyObject*)self);
Py_INCREF(self);
return (PyObject *)self;
}
//----------------------------Euler.rotate()-----------------------
// return a copy of the euler
@@ -207,9 +209,9 @@ static PyObject* Euler_richcmpr(PyObject *objectA, PyObject *objectB, int compar
if (!EulerObject_Check(objectA) || !EulerObject_Check(objectB)){
if (comparison_type == Py_NE){
return EXPP_incr_ret(Py_True);
Py_RETURN_TRUE;
}else{
return EXPP_incr_ret(Py_False);
Py_RETURN_FALSE;
}
}
eulA = (EulerObject*)objectA;
@@ -232,9 +234,9 @@ static PyObject* Euler_richcmpr(PyObject *objectA, PyObject *objectB, int compar
break;
}
if (result == 1){
return EXPP_incr_ret(Py_True);
Py_RETURN_TRUE;
}else{
return EXPP_incr_ret(Py_False);
Py_RETURN_FALSE;
}
}
//------------------------tp_doc
@@ -250,32 +252,36 @@ static int Euler_len(EulerObject * self)
//sequence accessor (get)
static PyObject *Euler_item(EulerObject * self, int i)
{
if(i < 0 || i >= 3)
return EXPP_ReturnPyObjError(PyExc_IndexError,
"euler[attribute]: array index out of range\n");
if(i<0)
i= 3-i;
if(i < 0 || i >= 3) {
PyErr_SetString(PyExc_IndexError, "euler[attribute]: array index out of range");
return NULL;
}
return PyFloat_FromDouble(self->eul[i]);
}
//----------------------------object[]-------------------------
//sequence accessor (set)
static int Euler_ass_item(EulerObject * self, int i, PyObject * ob)
static int Euler_ass_item(EulerObject * self, int i, PyObject * value)
{
PyObject *f = NULL;
float f = PyFloat_AsDouble(value);
f = PyNumber_Float(ob);
if(f == NULL) { // parsed item not a number
return EXPP_ReturnIntError(PyExc_TypeError,
"euler[attribute] = x: argument not a number\n");
if(f == -1 && PyErr_Occurred()) { // parsed item not a number
PyErr_SetString(PyExc_TypeError, "euler[attribute] = x: argument not a number");
return -1;
}
if(i<0)
i= 3-i;
if(i < 0 || i >= 3){
Py_DECREF(f);
return EXPP_ReturnIntError(PyExc_IndexError,
"euler[attribute] = x: array assignment index out of range\n");
PyErr_SetString(PyExc_IndexError, "euler[attribute] = x: array assignment index out of range\n");
return -1;
}
self->eul[i] = (float)PyFloat_AS_DOUBLE(f);
Py_DECREF(f);
self->eul[i] = f;
return 0;
}
//----------------------------object[z:y]------------------------
@@ -314,26 +320,27 @@ static int Euler_ass_slice(EulerObject * self, int begin, int end,
size = PySequence_Length(seq);
if(size != (end - begin)){
return EXPP_ReturnIntError(PyExc_TypeError,
"euler[begin:end] = []: size mismatch in slice assignment\n");
PyErr_SetString(PyExc_TypeError, "euler[begin:end] = []: size mismatch in slice assignment");
return -1;
}
for (i = 0; i < size; i++) {
e = PySequence_GetItem(seq, i);
if (e == NULL) { // Failed to read sequence
return EXPP_ReturnIntError(PyExc_RuntimeError,
"euler[begin:end] = []: unable to read sequence\n");
PyErr_SetString(PyExc_RuntimeError, "euler[begin:end] = []: unable to read sequence");
return -1;
}
f = PyNumber_Float(e);
if(f == NULL) { // parsed item not a number
Py_DECREF(e);
return EXPP_ReturnIntError(PyExc_TypeError,
"euler[begin:end] = []: sequence argument not a number\n");
PyErr_SetString(PyExc_TypeError, "euler[begin:end] = []: sequence argument not a number");
return -1;
}
eul[i] = (float)PyFloat_AS_DOUBLE(f);
EXPP_decr2(f,e);
Py_DECREF(f);
Py_DECREF(e);
}
//parsed well - now set in vector
for(y = 0; y < 3; y++){
@@ -377,9 +384,10 @@ static int Euler_setAxis( EulerObject * self, PyObject * value, void * type )
{
float param= (float)PyFloat_AsDouble( value );
if (param==-1 && PyErr_Occurred())
return EXPP_ReturnIntError( PyExc_TypeError,
"expected a number for the vector axis" );
if (param==-1 && PyErr_Occurred()) {
PyErr_SetString(PyExc_TypeError, "expected a number for the vector axis");
return -1;
}
switch( (long)type ) {
case 'X': /* these are backwards, but that how it works */
@@ -430,8 +438,13 @@ static PyGetSetDef Euler_getseters[] = {
//------------------PY_OBECT DEFINITION--------------------------
PyTypeObject euler_Type = {
PyObject_HEAD_INIT(NULL) //tp_head
0, //tp_internal
#if (PY_VERSION_HEX >= 0x02060000)
PyVarObject_HEAD_INIT(NULL, 0)
#else
/* python 2.5 and below */
PyObject_HEAD_INIT( NULL ) /* required py macro */
0, /* ob_size */
#endif
"euler", //tp_name
sizeof(EulerObject), //tp_basicsize
0, //tp_itemsize
@@ -513,6 +526,6 @@ PyObject *newEulerObject(float *eul, int type)
}else{ //bad type
return NULL;
}
return (PyObject *) self;
return (PyObject *)self;
}

View File

@@ -42,36 +42,6 @@
#include "constant.h"
/*---------------------- EXPP_FloatsAreEqual -------------------------
Floating point comparisons
floatStep = number of representable floats allowable in between
float A and float B to be considered equal. */
int EXPP_FloatsAreEqual(float A, float B, int floatSteps)
{
int a, b, delta;
assert(floatSteps > 0 && floatSteps < (4 * 1024 * 1024));
a = *(int*)&A;
if (a < 0)
a = 0x80000000 - a;
b = *(int*)&B;
if (b < 0)
b = 0x80000000 - b;
delta = abs(a - b);
if (delta <= floatSteps)
return 1;
return 0;
}
/*---------------------- EXPP_VectorsAreEqual -------------------------
Builds on EXPP_FloatsAreEqual to test vectors */
int EXPP_VectorsAreEqual(float *vecA, float *vecB, int size, int floatSteps){
int x;
for (x=0; x< size; x++){
if (EXPP_FloatsAreEqual(vecA[x], vecB[x], floatSteps) == 0)
return 0;
}
return 1;
}
/*---------------------- EXPP_GetModuleConstant -------------------------
Helper function for returning a module constant */
PyObject *EXPP_GetModuleConstant(char *module, char *constant)
@@ -781,7 +751,7 @@ int EXPP_setModuleConstant ( BPy_constant *constant, void *param, char type )
{
PyObject *item;
if( constant->ob_type != &constant_Type )
if( ((PyObject *)constant)->ob_type != &constant_Type )
return EXPP_ReturnIntError( PyExc_TypeError,
"expected module constant" );

View File

@@ -37,9 +37,9 @@
#include "constant.h"
#define Py_PI 3.14159265358979323846
#define Py_WRAP 1024
#define Py_NEW 2048
// #define Py_PI 3.14159265358979323846
// #define Py_WRAP 1024
// #define Py_NEW 2048
/*
Py_RETURN_NONE
@@ -77,9 +77,6 @@ typedef int Py_ssize_t;
/* name of list of Armature weak refs built into __main__ */
#define ARM_WEAKREF_LIST_NAME "__arm_weakrefs"
int EXPP_FloatsAreEqual(float A, float B, int floatSteps);
int EXPP_VectorsAreEqual(float *vecA, float *vecB, int size, int floatSteps);
PyObject *EXPP_GetModuleConstant(char *module, char *constant);
int StringEqual( const char *string1, const char *string2 );

View File

@@ -30,7 +30,6 @@
#include "BKE_utildefines.h"
#include "BLI_arithb.h"
#include "BLI_blenlib.h"
#include "gen_utils.h"
/*-------------------------DOC STRINGS ---------------------------*/
char Matrix_Zero_doc[] = "() - set all values in the matrix to 0";
@@ -70,8 +69,8 @@ PyObject *Matrix_toQuat(MatrixObject * self)
/*must be 3-4 cols, 3-4 rows, square matrix*/
if(self->colSize < 3 || self->rowSize < 3 || (self->colSize != self->rowSize)) {
return EXPP_ReturnPyObjError(PyExc_AttributeError,
"Matrix.toQuat(): inappropriate matrix size - expects 3x3 or 4x4 matrix\n");
PyErr_SetString(PyExc_AttributeError, "Matrix.toQuat(): inappropriate matrix size - expects 3x3 or 4x4 matrix");
return NULL;
}
if(self->colSize == 3){
Mat3ToQuat((float (*)[3])*self->matrix, quat);
@@ -95,10 +94,10 @@ PyObject *Matrix_toEuler(MatrixObject * self)
float tempmat3[3][3];
Mat3CpyMat4(tempmat3, (float (*)[4])*self->matrix);
Mat3ToEul(tempmat3, eul);
}else
return EXPP_ReturnPyObjError(PyExc_AttributeError,
"Matrix.toEuler(): inappropriate matrix size - expects 3x3 or 4x4 matrix\n");
}else {
PyErr_SetString(PyExc_AttributeError, "Matrix.toEuler(): inappropriate matrix size - expects 3x3 or 4x4 matrix\n");
return NULL;
}
/*have to convert to degrees*/
for(x = 0; x < 3; x++) {
eul[x] *= (float) (180 / Py_PI);
@@ -111,20 +110,20 @@ PyObject *Matrix_Resize4x4(MatrixObject * self)
int x, first_row_elem, curr_pos, new_pos, blank_columns, blank_rows, index;
if(self->data.blend_data){
return EXPP_ReturnPyObjError(PyExc_TypeError,
"cannot resize wrapped data - only python matrices\n");
PyErr_SetString(PyExc_TypeError, "cannot resize wrapped data - only python matrices");
return NULL;
}
self->data.py_data = PyMem_Realloc(self->data.py_data, (sizeof(float) * 16));
if(self->data.py_data == NULL) {
return EXPP_ReturnPyObjError(PyExc_MemoryError,
"matrix.resize4x4(): problem allocating pointer space\n\n");
PyErr_SetString(PyExc_MemoryError, "matrix.resize4x4(): problem allocating pointer space");
return NULL;
}
self->contigPtr = self->data.py_data; /*force*/
self->matrix = PyMem_Realloc(self->matrix, (sizeof(float *) * 4));
if(self->matrix == NULL) {
return EXPP_ReturnPyObjError(PyExc_MemoryError,
"matrix.resize4x4(): problem allocating pointer space\n\n");
PyErr_SetString(PyExc_MemoryError, "matrix.resize4x4(): problem allocating pointer space");
return NULL;
}
/*set row pointers*/
for(x = 0; x < 4; x++) {
@@ -155,7 +154,9 @@ PyObject *Matrix_Resize4x4(MatrixObject * self)
}
self->rowSize = 4;
self->colSize = 4;
return EXPP_incr_ret((PyObject*)self);
Py_INCREF(self);
return (PyObject *)self;
}
/*---------------------------Matrix.translationPart() ------------*/
PyObject *Matrix_TranslationPart(MatrixObject * self)
@@ -163,8 +164,8 @@ PyObject *Matrix_TranslationPart(MatrixObject * self)
float vec[4];
if(self->colSize < 3 || self->rowSize < 4){
return EXPP_ReturnPyObjError(PyExc_AttributeError,
"Matrix.translationPart: inappropriate matrix size\n");
PyErr_SetString(PyExc_AttributeError, "Matrix.translationPart: inappropriate matrix size");
return NULL;
}
vec[0] = self->matrix[3][0];
@@ -180,8 +181,8 @@ PyObject *Matrix_RotationPart(MatrixObject * self)
0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 1.0f};
if(self->colSize < 3 || self->rowSize < 3){
return EXPP_ReturnPyObjError(PyExc_AttributeError,
"Matrix.rotationPart: inappropriate matrix size\n");
PyErr_SetString(PyExc_AttributeError, "Matrix.rotationPart: inappropriate matrix size\n");
return NULL;
}
mat[0] = self->matrix[0][0];
@@ -207,10 +208,10 @@ PyObject *Matrix_scalePart(MatrixObject * self)
Mat3CpyMat4(mat, (float (*)[4])*self->matrix);
else if(self->colSize == 3 && self->rowSize == 3)
Mat3CpyMat3(mat, (float (*)[3])*self->matrix);
else
return EXPP_ReturnPyObjError(PyExc_AttributeError,
"Matrix.scalePart(): inappropriate matrix size - expects 3x3 or 4x4 matrix\n");
else {
PyErr_SetString(PyExc_AttributeError, "Matrix.scalePart(): inappropriate matrix size - expects 3x3 or 4x4 matrix\n");
return NULL;
}
/* functionality copied from editobject.c apply_obmat */
Mat3ToEul(mat, rot);
EulToMat3(rot, tmat);
@@ -233,8 +234,8 @@ PyObject *Matrix_Invert(MatrixObject * self)
0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 1.0f};
if(self->rowSize != self->colSize){
return EXPP_ReturnPyObjError(PyExc_AttributeError,
"Matrix.invert(ed): only square matrices are supported\n");
PyErr_SetString(PyExc_AttributeError, "Matrix.invert(ed): only square matrices are supported");
return NULL;
}
/*calculate the determinant*/
@@ -268,10 +269,12 @@ PyObject *Matrix_Invert(MatrixObject * self)
/*transpose
Matrix_Transpose(self);*/
} else {
return EXPP_ReturnPyObjError(PyExc_ValueError,
"matrix does not have an inverse");
PyErr_SetString(PyExc_ValueError, "matrix does not have an inverse");
return NULL;
}
return EXPP_incr_ret((PyObject*)self);
Py_INCREF(self);
return (PyObject *)self;
}
@@ -281,8 +284,8 @@ PyObject *Matrix_Determinant(MatrixObject * self)
float det = 0.0f;
if(self->rowSize != self->colSize){
return EXPP_ReturnPyObjError(PyExc_AttributeError,
"Matrix.determinant: only square matrices are supported\n");
PyErr_SetString(PyExc_AttributeError, "Matrix.determinant: only square matrices are supported");
return NULL;
}
if(self->rowSize == 2) {
@@ -306,8 +309,8 @@ PyObject *Matrix_Transpose(MatrixObject * self)
float t = 0.0f;
if(self->rowSize != self->colSize){
return EXPP_ReturnPyObjError(PyExc_AttributeError,
"Matrix.transpose(d): only square matrices are supported\n");
PyErr_SetString(PyExc_AttributeError, "Matrix.transpose(d): only square matrices are supported");
return NULL;
}
if(self->rowSize == 2) {
@@ -320,7 +323,8 @@ PyObject *Matrix_Transpose(MatrixObject * self)
Mat4Transp((float (*)[4])*self->matrix);
}
return EXPP_incr_ret((PyObject*)self);
Py_INCREF(self);
return (PyObject *)self;
}
@@ -334,14 +338,15 @@ PyObject *Matrix_Zero(MatrixObject * self)
self->matrix[row][col] = 0.0f;
}
}
return EXPP_incr_ret((PyObject*)self);
Py_INCREF(self);
return (PyObject *)self;
}
/*---------------------------Matrix.identity(() ------------------*/
PyObject *Matrix_Identity(MatrixObject * self)
{
if(self->rowSize != self->colSize){
return EXPP_ReturnPyObjError(PyExc_AttributeError,
"Matrix.identity: only square matrices are supported\n");
PyErr_SetString(PyExc_AttributeError, "Matrix.identity: only square matrices are supported\n");
return NULL;
}
if(self->rowSize == 2) {
@@ -355,7 +360,8 @@ PyObject *Matrix_Identity(MatrixObject * self)
Mat4One((float (*)[4]) *self->matrix);
}
return EXPP_incr_ret((PyObject*)self);
Py_INCREF(self);
return (PyObject *)self;
}
/*---------------------------Matrix.inverted() ------------------*/
@@ -387,9 +393,9 @@ static PyObject *Matrix_getattr(MatrixObject * self, char *name)
}
if(STREQ(name, "wrapped")){
if(self->wrapped == Py_WRAP)
return EXPP_incr_ret((PyObject *)Py_True);
Py_RETURN_TRUE;
else
return EXPP_incr_ret((PyObject *)Py_False);
Py_RETURN_FALSE;
}
return Py_FindMethod(Matrix_methods, (PyObject *) self, name);
}
@@ -435,9 +441,9 @@ static PyObject* Matrix_richcmpr(PyObject *objectA, PyObject *objectB, int compa
if (!MatrixObject_Check(objectA) || !MatrixObject_Check(objectB)){
if (comparison_type == Py_NE){
return EXPP_incr_ret(Py_True);
Py_RETURN_TRUE;
}else{
return EXPP_incr_ret(Py_False);
Py_RETURN_FALSE;
}
}
matA = (MatrixObject*)objectA;
@@ -445,9 +451,9 @@ static PyObject* Matrix_richcmpr(PyObject *objectA, PyObject *objectB, int compa
if (matA->colSize != matB->colSize || matA->rowSize != matB->rowSize){
if (comparison_type == Py_NE){
return EXPP_incr_ret(Py_True);
Py_RETURN_TRUE;
}else{
return EXPP_incr_ret(Py_False);
Py_RETURN_FALSE;
}
}
@@ -471,9 +477,9 @@ static PyObject* Matrix_richcmpr(PyObject *objectA, PyObject *objectB, int compa
break;
}
if (result == 1){
return EXPP_incr_ret(Py_True);
Py_RETURN_TRUE;
}else{
return EXPP_incr_ret(Py_False);
Py_RETURN_FALSE;
}
}
/*------------------------tp_doc*/
@@ -490,10 +496,10 @@ static int Matrix_len(MatrixObject * self)
the wrapped vector gives direct access to the matrix data*/
static PyObject *Matrix_item(MatrixObject * self, int i)
{
if(i < 0 || i >= self->rowSize)
return EXPP_ReturnPyObjError(PyExc_IndexError,
"matrix[attribute]: array index out of range\n");
if(i < 0 || i >= self->rowSize) {
PyErr_SetString(PyExc_IndexError, "matrix[attribute]: array index out of range");
return NULL;
}
return newVectorObject(self->matrix[i], self->colSize, Py_WRAP);
}
/*----------------------------object[]-------------------------
@@ -505,32 +511,33 @@ static int Matrix_ass_item(MatrixObject * self, int i, PyObject * ob)
PyObject *m, *f;
if(i >= self->rowSize || i < 0){
return EXPP_ReturnIntError(PyExc_TypeError,
"matrix[attribute] = x: bad row\n");
PyErr_SetString(PyExc_TypeError, "matrix[attribute] = x: bad row\n");
return -1;
}
if(PySequence_Check(ob)){
size = PySequence_Length(ob);
if(size != self->colSize){
return EXPP_ReturnIntError(PyExc_TypeError,
"matrix[attribute] = x: bad sequence size\n");
PyErr_SetString(PyExc_TypeError, "matrix[attribute] = x: bad sequence size\n");
return -1;
}
for (x = 0; x < size; x++) {
m = PySequence_GetItem(ob, x);
if (m == NULL) { /*Failed to read sequence*/
return EXPP_ReturnIntError(PyExc_RuntimeError,
"matrix[attribute] = x: unable to read sequence\n");
PyErr_SetString(PyExc_RuntimeError, "matrix[attribute] = x: unable to read sequence\n");
return -1;
}
f = PyNumber_Float(m);
if(f == NULL) { /*parsed item not a number*/
Py_DECREF(m);
return EXPP_ReturnIntError(PyExc_TypeError,
"matrix[attribute] = x: sequence argument not a number\n");
PyErr_SetString(PyExc_TypeError, "matrix[attribute] = x: sequence argument not a number\n");
return -1;
}
vec[x] = (float)PyFloat_AS_DOUBLE(f);
EXPP_decr2(m, f);
Py_DECREF(m);
Py_DECREF(f);
}
/*parsed well - now set in matrix*/
for(y = 0; y < size; y++){
@@ -538,8 +545,8 @@ static int Matrix_ass_item(MatrixObject * self, int i, PyObject * ob)
}
return 0;
}else{
return EXPP_ReturnIntError(PyExc_TypeError,
"matrix[attribute] = x: expects a sequence of column size\n");
PyErr_SetString(PyExc_TypeError, "matrix[attribute] = x: expects a sequence of column size\n");
return -1;
}
}
/*----------------------------object[z:y]------------------------
@@ -579,16 +586,16 @@ static int Matrix_ass_slice(MatrixObject * self, int begin, int end,
if(PySequence_Check(seq)){
size = PySequence_Length(seq);
if(size != (end - begin)){
return EXPP_ReturnIntError(PyExc_TypeError,
"matrix[begin:end] = []: size mismatch in slice assignment\n");
PyErr_SetString(PyExc_TypeError, "matrix[begin:end] = []: size mismatch in slice assignment\n");
return -1;
}
/*parse sub items*/
for (i = 0; i < size; i++) {
/*parse each sub sequence*/
subseq = PySequence_GetItem(seq, i);
if (subseq == NULL) { /*Failed to read sequence*/
return EXPP_ReturnIntError(PyExc_RuntimeError,
"matrix[begin:end] = []: unable to read sequence\n");
PyErr_SetString(PyExc_RuntimeError, "matrix[begin:end] = []: unable to read sequence");
return -1;
}
if(PySequence_Check(subseq)){
@@ -596,31 +603,33 @@ static int Matrix_ass_slice(MatrixObject * self, int begin, int end,
sub_size = PySequence_Length(subseq);
if(sub_size != self->colSize){
Py_DECREF(subseq);
return EXPP_ReturnIntError(PyExc_TypeError,
"matrix[begin:end] = []: size mismatch in slice assignment\n");
PyErr_SetString(PyExc_TypeError, "matrix[begin:end] = []: size mismatch in slice assignment\n");
return -1;
}
for (y = 0; y < sub_size; y++) {
m = PySequence_GetItem(subseq, y);
if (m == NULL) { /*Failed to read sequence*/
Py_DECREF(subseq);
return EXPP_ReturnIntError(PyExc_RuntimeError,
"matrix[begin:end] = []: unable to read sequence\n");
PyErr_SetString(PyExc_RuntimeError, "matrix[begin:end] = []: unable to read sequence\n");
return -1;
}
f = PyNumber_Float(m);
if(f == NULL) { /*parsed item not a number*/
EXPP_decr2(m, subseq);
return EXPP_ReturnIntError(PyExc_TypeError,
"matrix[begin:end] = []: sequence argument not a number\n");
Py_DECREF(m);
Py_DECREF(subseq);
PyErr_SetString(PyExc_TypeError, "matrix[begin:end] = []: sequence argument not a number\n");
return -1;
}
mat[(i * self->colSize) + y] = (float)PyFloat_AS_DOUBLE(f);
EXPP_decr2(f, m);
Py_DECREF(m);
Py_DECREF(subseq);
}
}else{
Py_DECREF(subseq);
return EXPP_ReturnIntError(PyExc_TypeError,
"matrix[begin:end] = []: illegal argument type for built-in operation\n");
PyErr_SetString(PyExc_TypeError, "matrix[begin:end] = []: illegal argument type for built-in operation\n");
return -1;
}
Py_DECREF(subseq);
}
@@ -630,8 +639,8 @@ static int Matrix_ass_slice(MatrixObject * self, int begin, int end,
}
return 0;
}else{
return EXPP_ReturnIntError(PyExc_TypeError,
"matrix[begin:end] = []: illegal argument type for built-in operation\n");
PyErr_SetString(PyExc_TypeError, "matrix[begin:end] = []: illegal argument type for built-in operation\n");
return -1;
}
}
/*------------------------NUMERIC PROTOCOLS----------------------
@@ -647,12 +656,12 @@ static PyObject *Matrix_add(PyObject * m1, PyObject * m2)
mat2 = (MatrixObject*)m2;
if(mat1->coerced_object || mat2->coerced_object){
return EXPP_ReturnPyObjError(PyExc_AttributeError,
"Matrix addition: arguments not valid for this operation....\n");
PyErr_SetString(PyExc_AttributeError, "Matrix addition: arguments not valid for this operation....");
return NULL;
}
if(mat1->rowSize != mat2->rowSize || mat1->colSize != mat2->colSize){
return EXPP_ReturnPyObjError(PyExc_AttributeError,
"Matrix addition: matrices must have the same dimensions for this operation\n");
PyErr_SetString(PyExc_AttributeError, "Matrix addition: matrices must have the same dimensions for this operation");
return NULL;
}
for(x = 0; x < mat1->rowSize; x++) {
@@ -676,12 +685,12 @@ static PyObject *Matrix_sub(PyObject * m1, PyObject * m2)
mat2 = (MatrixObject*)m2;
if(mat1->coerced_object || mat2->coerced_object){
return EXPP_ReturnPyObjError(PyExc_AttributeError,
"Matrix addition: arguments not valid for this operation....\n");
PyErr_SetString(PyExc_AttributeError, "Matrix addition: arguments not valid for this operation....");
return NULL;
}
if(mat1->rowSize != mat2->rowSize || mat1->colSize != mat2->colSize){
return EXPP_ReturnPyObjError(PyExc_AttributeError,
"Matrix addition: matrices must have the same dimensions for this operation\n");
PyErr_SetString(PyExc_AttributeError, "Matrix addition: matrices must have the same dimensions for this operation");
return NULL;
}
for(x = 0; x < mat1->rowSize; x++) {
@@ -703,7 +712,6 @@ static PyObject *Matrix_mul(PyObject * m1, PyObject * m2)
double dot = 0.0f;
MatrixObject *mat1 = NULL, *mat2 = NULL;
PyObject *f = NULL;
PointObject *pt = NULL;
mat1 = (MatrixObject*)m1;
mat2 = (MatrixObject*)m2;
@@ -713,8 +721,8 @@ static PyObject *Matrix_mul(PyObject * m1, PyObject * m2)
PyInt_Check(mat1->coerced_object)){ /*FLOAT/INT * MATRIX*/
f = PyNumber_Float(mat1->coerced_object);
if(f == NULL) { /*parsed item not a number*/
return EXPP_ReturnPyObjError(PyExc_TypeError,
"Matrix multiplication: arguments not acceptable for this operation\n");
PyErr_SetString(PyExc_TypeError, "Matrix multiplication: arguments not acceptable for this operation");
return NULL;
}
scalar = (float)PyFloat_AS_DOUBLE(f);
@@ -733,15 +741,11 @@ static PyObject *Matrix_mul(PyObject * m1, PyObject * m2)
vec = (VectorObject*)mat2->coerced_object;
return column_vector_multiplication(mat1, vec);
}else */
if(PointObject_Check(mat2->coerced_object)){ /*MATRIX * POINT*/
pt = (PointObject*)mat2->coerced_object;
return column_point_multiplication(mat1, pt);
}else if (PyFloat_Check(mat2->coerced_object) ||
PyInt_Check(mat2->coerced_object)){ /*MATRIX * FLOAT/INT*/
if (PyFloat_Check(mat2->coerced_object) || PyInt_Check(mat2->coerced_object)){ /*MATRIX * FLOAT/INT*/
f = PyNumber_Float(mat2->coerced_object);
if(f == NULL) { /*parsed item not a number*/
return EXPP_ReturnPyObjError(PyExc_TypeError,
"Matrix multiplication: arguments not acceptable for this operation\n");
PyErr_SetString(PyExc_TypeError, "Matrix multiplication: arguments not acceptable for this operation\n");
return NULL;
}
scalar = (float)PyFloat_AS_DOUBLE(f);
@@ -755,8 +759,8 @@ static PyObject *Matrix_mul(PyObject * m1, PyObject * m2)
}
}else{ /*MATRIX * MATRIX*/
if(mat1->colSize != mat2->rowSize){
return EXPP_ReturnPyObjError(PyExc_AttributeError,
"Matrix multiplication: matrix A rowsize must equal matrix B colsize\n");
PyErr_SetString(PyExc_AttributeError,"Matrix multiplication: matrix A rowsize must equal matrix B colsize");
return NULL;
}
for(x = 0; x < mat1->rowSize; x++) {
for(y = 0; y < mat2->colSize; y++) {
@@ -771,8 +775,8 @@ static PyObject *Matrix_mul(PyObject * m1, PyObject * m2)
}
}
return EXPP_ReturnPyObjError(PyExc_TypeError,
"Matrix multiplication: arguments not acceptable for this operation\n");
PyErr_SetString(PyExc_TypeError, "Matrix multiplication: arguments not acceptable for this operation\n");
return NULL;
}
static PyObject* Matrix_inv(MatrixObject *self)
{
@@ -789,17 +793,17 @@ static PyObject* Matrix_inv(MatrixObject *self)
then call vector.multiply(vector, scalar_cast_as_vector)*/
static int Matrix_coerce(PyObject ** m1, PyObject ** m2)
{
if(VectorObject_Check(*m2) || PyFloat_Check(*m2) || PyInt_Check(*m2) ||
PointObject_Check(*m2)) {
PyObject *coerced = EXPP_incr_ret(*m2);
if(VectorObject_Check(*m2) || PyFloat_Check(*m2) || PyInt_Check(*m2)) {
PyObject *coerced = (PyObject *)(*m2);
Py_INCREF(coerced);
*m2 = newMatrixObject(NULL,3,3,Py_NEW);
((MatrixObject*)*m2)->coerced_object = coerced;
Py_INCREF (*m1);
return 0;
}
return EXPP_ReturnIntError(PyExc_TypeError,
"matrix.coerce(): unknown operand - can't coerce for numeric protocols");
PyErr_SetString(PyExc_TypeError, "matrix.coerce(): unknown operand - can't coerce for numeric protocols");
return -1;
}
/*-----------------PROTOCOL DECLARATIONS--------------------------*/
static PySequenceMethods Matrix_SeqMethods = {
@@ -838,8 +842,13 @@ static PyNumberMethods Matrix_NumMethods = {
};
/*------------------PY_OBECT DEFINITION--------------------------*/
PyTypeObject matrix_Type = {
PyObject_HEAD_INIT(NULL) /*tp_head*/
0, /*tp_internal*/
#if (PY_VERSION_HEX >= 0x02060000)
PyVarObject_HEAD_INIT(NULL, 0)
#else
/* python 2.5 and below */
PyObject_HEAD_INIT( NULL ) /* required py macro */
0, /* ob_size */
#endif
"matrix", /*tp_name*/
sizeof(MatrixObject), /*tp_basicsize*/
0, /*tp_itemsize*/
@@ -910,8 +919,8 @@ PyObject *newMatrixObject(float *mat, int rowSize, int colSize, int type)
/*matrix objects can be any 2-4row x 2-4col matrix*/
if(rowSize < 2 || rowSize > 4 || colSize < 2 || colSize > 4){
return EXPP_ReturnPyObjError(PyExc_RuntimeError,
"matrix(): row and column sizes must be between 2 and 4\n");
PyErr_SetString(PyExc_RuntimeError, "matrix(): row and column sizes must be between 2 and 4");
return NULL;
}
self = PyObject_NEW(MatrixObject, &matrix_Type);
@@ -927,8 +936,8 @@ PyObject *newMatrixObject(float *mat, int rowSize, int colSize, int type)
/*create pointer array*/
self->matrix = PyMem_Malloc(rowSize * sizeof(float *));
if(self->matrix == NULL) { /*allocation failure*/
return EXPP_ReturnPyObjError( PyExc_MemoryError,
"matrix(): problem allocating pointer space\n");
PyErr_SetString( PyExc_MemoryError, "matrix(): problem allocating pointer space");
return NULL;
}
/*pointer array points to contigous memory*/
for(x = 0; x < rowSize; x++) {
@@ -938,16 +947,16 @@ PyObject *newMatrixObject(float *mat, int rowSize, int colSize, int type)
}else if (type == Py_NEW){
self->data.py_data = PyMem_Malloc(rowSize * colSize * sizeof(float));
if(self->data.py_data == NULL) { /*allocation failure*/
return EXPP_ReturnPyObjError( PyExc_MemoryError,
"matrix(): problem allocating pointer space\n");
PyErr_SetString( PyExc_MemoryError, "matrix(): problem allocating pointer space\n");
return NULL;
}
self->contigPtr = self->data.py_data;
/*create pointer array*/
self->matrix = PyMem_Malloc(rowSize * sizeof(float *));
if(self->matrix == NULL) { /*allocation failure*/
PyMem_Free(self->data.py_data);
return EXPP_ReturnPyObjError( PyExc_MemoryError,
"matrix(): problem allocating pointer space\n");
PyErr_SetString( PyExc_MemoryError, "matrix(): problem allocating pointer space");
return NULL;
}
/*pointer array points to contigous memory*/
for(x = 0; x < rowSize; x++) {

View File

@@ -1,523 +0,0 @@
/*
* $Id$
*
* ***** BEGIN GPL 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.
*
* 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 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 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)
{
Py_XDECREF(self->coerced_object);
//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 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 PyFloat_FromDouble( (double)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];
PyObject *v, *f;
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++) {
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;
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*)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{
return EXPP_ReturnPyObjError(PyExc_AttributeError,
"Point addition: arguments are the wrong size....\n");
}
return 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{
return EXPP_ReturnPyObjError(PyExc_AttributeError,
"Point addition: arguments are the wrong size....\n");
}
return newPointObject(coord, size, Py_NEW);
}
}
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;
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){
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
return 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;
MatrixObject *mat = NULL;
QuaternionObject *quat = NULL;
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
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;
}
Py_DECREF(f);
return 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
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;
}
Py_DECREF(f);
return newPointObject(coord, size, Py_NEW);
}else if(MatrixObject_Check(coord2->coerced_object)){ //POINT * MATRIX
mat = (MatrixObject*)coord2->coerced_object;
return row_point_multiplication(coord1, mat);
}else if(QuaternionObject_Check(coord2->coerced_object)){ //POINT * QUATERNION
quat = (QuaternionObject*)coord2->coerced_object;
if(coord1->size != 3){
return EXPP_ReturnPyObjError(PyExc_TypeError,
"Point multiplication: only 3D point rotations (with quats) currently supported\n");
}
return quat_rotation((PyObject*)coord1, (PyObject*)quat);
}
}
}
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;
float coord[3];
for(x = 0; x < self->size; x++)
coord[x] = -self->coord[x];
return newPointObject(coord, self->size, Py_NEW);
}
//------------------------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)
{
if(VectorObject_Check(*p2) || PyFloat_Check(*p2) || PyInt_Check(*p2) ||
MatrixObject_Check(*p2) || QuaternionObject_Check(*p2)) {
PyObject *coerced = EXPP_incr_ret(*p2);
*p2 = newPointObject(NULL,3,Py_NEW);
((PointObject*)*p2)->coerced_object = coerced;
Py_INCREF (*p1);
return 0;
}
return EXPP_ReturnIntError(PyExc_TypeError,
"point.coerce(): unknown operand - can't coerce for numeric protocols");
}
//-----------------PROTOCOL 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 *) self;
}

View File

@@ -1,64 +0,0 @@
/*
* $Id$
*
* ***** BEGIN GPL 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.
*
* 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 LICENSE BLOCK *****
*/
#ifndef EXPP_point_h
#define EXPP_point_h
#include <Python.h>
extern PyTypeObject point_Type;
#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 */

View File

@@ -31,7 +31,6 @@
#include "BLI_arithb.h"
#include "BKE_utildefines.h"
#include "BLI_blenlib.h"
#include "gen_utils.h"
//-------------------------DOC STRINGS ---------------------------
@@ -84,7 +83,8 @@ PyObject *Quaternion_ToMatrix(QuaternionObject * self)
PyObject *Quaternion_Normalize(QuaternionObject * self)
{
NormalQuat(self->quat);
return EXPP_incr_ret((PyObject*)self);
Py_INCREF(self);
return (PyObject*)self;
}
//----------------------------Quaternion.inverse()------------------
//invert the quat
@@ -104,7 +104,8 @@ PyObject *Quaternion_Inverse(QuaternionObject * self)
self->quat[x] /= (float)(mag * mag);
}
return EXPP_incr_ret((PyObject*)self);
Py_INCREF(self);
return (PyObject*)self;
}
//----------------------------Quaternion.identity()-----------------
//generate the identity quaternion
@@ -115,7 +116,8 @@ PyObject *Quaternion_Identity(QuaternionObject * self)
self->quat[2] = 0.0;
self->quat[3] = 0.0;
return EXPP_incr_ret((PyObject*)self);
Py_INCREF(self);
return (PyObject*)self;
}
//----------------------------Quaternion.negate()-------------------
//negate the quat
@@ -125,7 +127,8 @@ PyObject *Quaternion_Negate(QuaternionObject * self)
for(x = 0; x < 4; x++) {
self->quat[x] = -self->quat[x];
}
return EXPP_incr_ret((PyObject*)self);
Py_INCREF(self);
return (PyObject*)self;
}
//----------------------------Quaternion.conjugate()----------------
//negate the vector part
@@ -135,7 +138,8 @@ PyObject *Quaternion_Conjugate(QuaternionObject * self)
for(x = 1; x < 4; x++) {
self->quat[x] = -self->quat[x];
}
return EXPP_incr_ret((PyObject*)self);
Py_INCREF(self);
return (PyObject*)self;
}
//----------------------------Quaternion.copy()----------------
//return a copy of the quat
@@ -216,9 +220,10 @@ static int Quaternion_len(QuaternionObject * self)
//sequence accessor (get)
static PyObject *Quaternion_item(QuaternionObject * self, int i)
{
if(i < 0 || i >= 4)
return EXPP_ReturnPyObjError(PyExc_IndexError,
"quaternion[attribute]: array index out of range\n");
if(i < 0 || i >= 4) {
PyErr_SetString(PyExc_IndexError, "quaternion[attribute]: array index out of range\n");
return NULL;
}
return PyFloat_FromDouble(self->quat[i]);
}
@@ -230,14 +235,14 @@ static int Quaternion_ass_item(QuaternionObject * self, int i, PyObject * ob)
f = PyNumber_Float(ob);
if(f == NULL) { // parsed item not a number
return EXPP_ReturnIntError(PyExc_TypeError,
"quaternion[attribute] = x: argument not a number\n");
PyErr_SetString(PyExc_TypeError, "quaternion[attribute] = x: argument not a number\n");
return -1;
}
if(i < 0 || i >= 4){
Py_DECREF(f);
return EXPP_ReturnIntError(PyExc_IndexError,
"quaternion[attribute] = x: array assignment index out of range\n");
PyErr_SetString(PyExc_IndexError, "quaternion[attribute] = x: array assignment index out of range\n");
return -1;
}
self->quat[i] = (float)PyFloat_AS_DOUBLE(f);
Py_DECREF(f);
@@ -279,26 +284,27 @@ static int Quaternion_ass_slice(QuaternionObject * self, int begin, int end,
size = PySequence_Length(seq);
if(size != (end - begin)){
return EXPP_ReturnIntError(PyExc_TypeError,
"quaternion[begin:end] = []: size mismatch in slice assignment\n");
PyErr_SetString(PyExc_TypeError, "quaternion[begin:end] = []: size mismatch in slice assignment\n");
return -1;
}
for (i = 0; i < size; i++) {
q = PySequence_GetItem(seq, i);
if (q == NULL) { // Failed to read sequence
return EXPP_ReturnIntError(PyExc_RuntimeError,
"quaternion[begin:end] = []: unable to read sequence\n");
PyErr_SetString(PyExc_RuntimeError, "quaternion[begin:end] = []: unable to read sequence\n");
return -1;
}
f = PyNumber_Float(q);
if(f == NULL) { // parsed item not a number
Py_DECREF(q);
return EXPP_ReturnIntError(PyExc_TypeError,
"quaternion[begin:end] = []: sequence argument not a number\n");
PyErr_SetString(PyExc_TypeError, "quaternion[begin:end] = []: sequence argument not a number\n");
return -1;
}
quat[i] = (float)PyFloat_AS_DOUBLE(f);
EXPP_decr2(f,q);
Py_DECREF(f);
Py_DECREF(q);
}
//parsed well - now set in vector
for(y = 0; y < size; y++){
@@ -319,8 +325,8 @@ static PyObject *Quaternion_add(PyObject * q1, PyObject * q2)
quat2 = (QuaternionObject*)q2;
if(quat1->coerced_object || quat2->coerced_object){
return EXPP_ReturnPyObjError(PyExc_AttributeError,
"Quaternion addition: arguments not valid for this operation....\n");
PyErr_SetString(PyExc_AttributeError, "Quaternion addition: arguments not valid for this operation....\n");
return NULL;
}
for(x = 0; x < 4; x++) {
quat[x] = quat1->quat[x] + quat2->quat[x];
@@ -340,8 +346,8 @@ static PyObject *Quaternion_sub(PyObject * q1, PyObject * q2)
quat2 = (QuaternionObject*)q2;
if(quat1->coerced_object || quat2->coerced_object){
return EXPP_ReturnPyObjError(PyExc_AttributeError,
"Quaternion addition: arguments not valid for this operation....\n");
PyErr_SetString(PyExc_AttributeError, "Quaternion addition: arguments not valid for this operation....\n");
return NULL;
}
for(x = 0; x < 4; x++) {
quat[x] = quat1->quat[x] - quat2->quat[x];
@@ -359,7 +365,6 @@ static PyObject *Quaternion_mul(PyObject * q1, PyObject * q2)
QuaternionObject *quat1 = NULL, *quat2 = NULL;
PyObject *f = NULL;
VectorObject *vec = NULL;
PointObject *pt = NULL;
quat1 = (QuaternionObject*)q1;
quat2 = (QuaternionObject*)q2;
@@ -369,8 +374,8 @@ static PyObject *Quaternion_mul(PyObject * q1, PyObject * q2)
PyInt_Check(quat1->coerced_object)){ // FLOAT/INT * QUAT
f = PyNumber_Float(quat1->coerced_object);
if(f == NULL) { // parsed item not a number
return EXPP_ReturnPyObjError(PyExc_TypeError,
"Quaternion multiplication: arguments not acceptable for this operation\n");
PyErr_SetString(PyExc_TypeError, "Quaternion multiplication: arguments not acceptable for this operation\n");
return NULL;
}
scalar = (float)PyFloat_AS_DOUBLE(f);
@@ -386,8 +391,8 @@ static PyObject *Quaternion_mul(PyObject * q1, PyObject * q2)
PyInt_Check(quat2->coerced_object)){ // QUAT * FLOAT/INT
f = PyNumber_Float(quat2->coerced_object);
if(f == NULL) { // parsed item not a number
return EXPP_ReturnPyObjError(PyExc_TypeError,
"Quaternion multiplication: arguments not acceptable for this operation\n");
PyErr_SetString(PyExc_TypeError, "Quaternion multiplication: arguments not acceptable for this operation\n");
return NULL;
}
scalar = (float)PyFloat_AS_DOUBLE(f);
@@ -399,17 +404,10 @@ static PyObject *Quaternion_mul(PyObject * q1, PyObject * q2)
}else if(VectorObject_Check(quat2->coerced_object)){ //QUAT * VEC
vec = (VectorObject*)quat2->coerced_object;
if(vec->size != 3){
return EXPP_ReturnPyObjError(PyExc_TypeError,
"Quaternion multiplication: only 3D vector rotations currently supported\n");
PyErr_SetString(PyExc_TypeError, "Quaternion multiplication: only 3D vector rotations currently supported\n");
return NULL;
}
return quat_rotation((PyObject*)quat1, (PyObject*)vec);
}else if(PointObject_Check(quat2->coerced_object)){ //QUAT * POINT
pt = (PointObject*)quat2->coerced_object;
if(pt->size != 3){
return EXPP_ReturnPyObjError(PyExc_TypeError,
"Quaternion multiplication: only 3D point rotations currently supported\n");
}
return quat_rotation((PyObject*)quat1, (PyObject*)pt);
}
}else{ //QUAT * QUAT (dot product)
for(x = 0; x < 4; x++) {
@@ -419,8 +417,8 @@ static PyObject *Quaternion_mul(PyObject * q1, PyObject * q2)
}
}
return EXPP_ReturnPyObjError(PyExc_TypeError,
"Quaternion multiplication: arguments not acceptable for this operation\n");
PyErr_SetString(PyExc_TypeError, "Quaternion multiplication: arguments not acceptable for this operation\n");
return NULL;
}
//------------------------coerce(obj, obj)-----------------------
//coercion of unknown types to type QuaternionObject for numeric protocols
@@ -432,17 +430,18 @@ static PyObject *Quaternion_mul(PyObject * q1, PyObject * q2)
then call vector.multiply(vector, scalar_cast_as_vector)*/
static int Quaternion_coerce(PyObject ** q1, PyObject ** q2)
{
if(VectorObject_Check(*q2) || PyFloat_Check(*q2) || PyInt_Check(*q2) ||
PointObject_Check(*q2)) {
PyObject *coerced = EXPP_incr_ret(*q2);
if(VectorObject_Check(*q2) || PyFloat_Check(*q2) || PyInt_Check(*q2)) {
PyObject *coerced = (PyObject *)(*q2);
Py_INCREF(coerced);
*q2 = newQuaternionObject(NULL,Py_NEW);
((QuaternionObject*)*q2)->coerced_object = coerced;
Py_INCREF (*q1);
return 0;
}
return EXPP_ReturnIntError(PyExc_TypeError,
"quaternion.coerce(): unknown operand - can't coerce for numeric protocols");
PyErr_SetString(PyExc_TypeError, "quaternion.coerce(): unknown operand - can't coerce for numeric protocols");
return -1;
}
//-----------------PROTOCOL DECLARATIONS--------------------------
static PySequenceMethods Quaternion_SeqMethods = {
@@ -503,10 +502,10 @@ static int Quaternion_setAxis( QuaternionObject * self, PyObject * value, void *
{
float param= (float)PyFloat_AsDouble( value );
if (param==-1 && PyErr_Occurred())
return EXPP_ReturnIntError( PyExc_TypeError,
"expected a number for the vector axis" );
if (param==-1 && PyErr_Occurred()) {
PyErr_SetString( PyExc_TypeError, "expected a number for the vector axis" );
return -1;
}
switch( (long)type ) {
case 'W':
self->quat[0]= param;

View File

@@ -30,7 +30,6 @@
#include "BLI_blenlib.h"
#include "BKE_utildefines.h"
#include "BLI_arithb.h"
#include "gen_utils.h"
/*-------------------------DOC STRINGS ---------------------------*/
@@ -40,7 +39,6 @@ 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";
char Vector_ToTrackQuat_doc[] = "(track, up) - extract a quaternion from the vector and the track and up axis";
char Vector_reflect_doc[] = "(mirror) - return a vector reflected on the mirror normal";
char Vector_copy_doc[] = "() - return a copy of the vector";
@@ -52,7 +50,6 @@ 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},
{"toTrackQuat", ( PyCFunction ) Vector_ToTrackQuat, METH_VARARGS, Vector_ToTrackQuat_doc},
{"reflect", ( PyCFunction ) Vector_reflect, METH_O, Vector_reflect_doc},
{"copy", (PyCFunction) Vector_copy, METH_NOARGS, Vector_copy_doc},
@@ -61,23 +58,6 @@ struct PyMethodDef Vector_methods[] = {
};
/*-----------------------------METHODS----------------------------
--------------------------Vector.toPoint()----------------------
create a new point object to represent this vector */
PyObject *Vector_toPoint(VectorObject * self)
{
float coord[3];
int i;
if(self->size < 2 || self->size > 3) {
return EXPP_ReturnPyObjError(PyExc_AttributeError,
"Vector.toPoint(): inappropriate vector size - expects 2d or 3d vector\n");
}
for(i = 0; i < self->size; i++){
coord[i] = self->vec[i];
}
return newPointObject(coord, self->size, Py_NEW);
}
/*----------------------------Vector.zero() ----------------------
set the vector data to 0,0,0 */
PyObject *Vector_Zero(VectorObject * self)
@@ -86,7 +66,8 @@ PyObject *Vector_Zero(VectorObject * self)
for(i = 0; i < self->size; i++) {
self->vec[i] = 0.0f;
}
return EXPP_incr_ret((PyObject*)self);
Py_INCREF(self);
return (PyObject*)self;
}
/*----------------------------Vector.normalize() -----------------
normalize the vector data to a unit vector */
@@ -102,7 +83,8 @@ PyObject *Vector_Normalize(VectorObject * self)
for(i = 0; i < self->size; i++) {
self->vec[i] /= norm;
}
return EXPP_incr_ret((PyObject*)self);
Py_INCREF(self);
return (PyObject*)self;
}
@@ -110,50 +92,54 @@ PyObject *Vector_Normalize(VectorObject * self)
resize the vector to x,y */
PyObject *Vector_Resize2D(VectorObject * self)
{
if(self->wrapped==Py_WRAP)
return EXPP_ReturnPyObjError(PyExc_TypeError,
"vector.resize2d(): cannot resize wrapped data - only python vectors\n");
if(self->wrapped==Py_WRAP) {
PyErr_SetString(PyExc_TypeError, "vector.resize2d(): cannot resize wrapped data - only python vectors\n");
return NULL;
}
self->vec = PyMem_Realloc(self->vec, (sizeof(float) * 2));
if(self->vec == NULL)
return EXPP_ReturnPyObjError(PyExc_MemoryError,
"vector.resize2d(): problem allocating pointer space\n\n");
if(self->vec == NULL) {
PyErr_SetString(PyExc_MemoryError, "vector.resize2d(): problem allocating pointer space\n\n");
return NULL;
}
self->size = 2;
return EXPP_incr_ret((PyObject*)self);
Py_INCREF(self);
return (PyObject*)self;
}
/*----------------------------Vector.resize3D() ------------------
resize the vector to x,y,z */
PyObject *Vector_Resize3D(VectorObject * self)
{
if (self->wrapped==Py_WRAP)
return EXPP_ReturnPyObjError(PyExc_TypeError,
"vector.resize3d(): cannot resize wrapped data - only python vectors\n");
if (self->wrapped==Py_WRAP) {
PyErr_SetString(PyExc_TypeError, "vector.resize3d(): cannot resize wrapped data - only python vectors\n");
return NULL;
}
self->vec = PyMem_Realloc(self->vec, (sizeof(float) * 3));
if(self->vec == NULL)
return EXPP_ReturnPyObjError(PyExc_MemoryError,
"vector.resize3d(): problem allocating pointer space\n\n");
if(self->vec == NULL) {
PyErr_SetString(PyExc_MemoryError, "vector.resize3d(): problem allocating pointer space\n\n");
return NULL;
}
if(self->size == 2)
self->vec[2] = 0.0f;
self->size = 3;
return EXPP_incr_ret((PyObject*)self);
Py_INCREF(self);
return (PyObject*)self;
}
/*----------------------------Vector.resize4D() ------------------
resize the vector to x,y,z,w */
PyObject *Vector_Resize4D(VectorObject * self)
{
if(self->wrapped==Py_WRAP)
return EXPP_ReturnPyObjError(PyExc_TypeError,
"vector.resize4d(): cannot resize wrapped data - only python vectors\n");
if(self->wrapped==Py_WRAP) {
PyErr_SetString(PyExc_TypeError, "vector.resize4d(): cannot resize wrapped data - only python vectors");
return NULL;
}
self->vec = PyMem_Realloc(self->vec, (sizeof(float) * 4));
if(self->vec == NULL)
return EXPP_ReturnPyObjError(PyExc_MemoryError,
"vector.resize4d(): problem allocating pointer space\n\n");
if(self->vec == NULL) {
PyErr_SetString(PyExc_MemoryError, "vector.resize4d(): problem allocating pointer space\n\n");
return NULL;
}
if(self->size == 2){
self->vec[2] = 0.0f;
self->vec[3] = 1.0f;
@@ -161,7 +147,8 @@ PyObject *Vector_Resize4D(VectorObject * self)
self->vec[3] = 1.0f;
}
self->size = 4;
return EXPP_incr_ret((PyObject*)self);
Py_INCREF(self);
return (PyObject*)self;
}
/*----------------------------Vector.toTrackQuat(track, up) ----------------------
extract a quaternion from the vector and the track and up axis */
@@ -172,11 +159,12 @@ PyObject *Vector_ToTrackQuat( VectorObject * self, PyObject * args )
short track = 2, up = 1;
if( !PyArg_ParseTuple ( args, "|ss", &strack, &sup ) ) {
return EXPP_ReturnPyObjError( PyExc_TypeError,
"expected optional two strings\n" );
PyErr_SetString( PyExc_TypeError, "expected optional two strings\n" );
return NULL;
}
if (self->size != 3) {
return EXPP_ReturnPyObjError( PyExc_TypeError, "only for 3D vectors\n" );
PyErr_SetString( PyExc_TypeError, "only for 3D vectors\n" );
return NULL;
}
if (strack) {
@@ -196,13 +184,13 @@ PyObject *Vector_ToTrackQuat( VectorObject * self, PyObject * args )
track = 5;
break;
default:
return EXPP_ReturnPyObjError( PyExc_ValueError,
"only X, -X, Y, -Y, Z or -Z for track axis\n" );
PyErr_SetString( PyExc_ValueError, "only X, -X, Y, -Y, Z or -Z for track axis\n" );
return NULL;
}
}
else {
return EXPP_ReturnPyObjError( PyExc_ValueError,
"only X, -X, Y, -Y, Z or -Z for track axis\n" );
PyErr_SetString( PyExc_ValueError, "only X, -X, Y, -Y, Z or -Z for track axis\n" );
return NULL;
}
}
else if (strlen(strack) == 1) {
@@ -221,13 +209,13 @@ PyObject *Vector_ToTrackQuat( VectorObject * self, PyObject * args )
track = 2;
break;
default:
return EXPP_ReturnPyObjError( PyExc_ValueError,
"only X, -X, Y, -Y, Z or -Z for track axis\n" );
PyErr_SetString( PyExc_ValueError, "only X, -X, Y, -Y, Z or -Z for track axis\n" );
return NULL;
}
}
else {
return EXPP_ReturnPyObjError( PyExc_ValueError,
"only X, -X, Y, -Y, Z or -Z for track axis\n" );
PyErr_SetString( PyExc_ValueError, "only X, -X, Y, -Y, Z or -Z for track axis\n" );
return NULL;
}
}
@@ -247,19 +235,19 @@ PyObject *Vector_ToTrackQuat( VectorObject * self, PyObject * args )
up = 2;
break;
default:
return EXPP_ReturnPyObjError( PyExc_ValueError,
"only X, Y or Z for up axis\n" );
PyErr_SetString( PyExc_ValueError, "only X, Y or Z for up axis\n" );
return NULL;
}
}
else {
return EXPP_ReturnPyObjError( PyExc_ValueError,
"only X, Y or Z for up axis\n" );
PyErr_SetString( PyExc_ValueError, "only X, Y or Z for up axis\n" );
return NULL;
}
}
if (track == up) {
return EXPP_ReturnPyObjError( PyExc_ValueError,
"Can't have the same axis for track and up\n" );
PyErr_SetString( PyExc_ValueError, "Can't have the same axis for track and up\n" );
return NULL;
}
/*
@@ -291,9 +279,10 @@ PyObject *Vector_reflect( VectorObject * self, PyObject * value )
int i;
float norm = 0.0f;
if (!VectorObject_Check(value))
return EXPP_ReturnPyObjError( PyExc_TypeError, "expected a vector argument" );
if (!VectorObject_Check(value)) {
PyErr_SetString( PyExc_TypeError, "expected a vector argument" );
return NULL;
}
mirrvec = (VectorObject *)value;
mirror[0] = mirrvec->vec[0];
@@ -375,9 +364,10 @@ static int Vector_len(VectorObject * self)
sequence accessor (get)*/
static PyObject *Vector_item(VectorObject * self, int i)
{
if(i < 0 || i >= self->size)
return EXPP_ReturnPyObjError(PyExc_IndexError,
"vector[index]: out of range\n");
if(i < 0 || i >= self->size) {
PyErr_SetString(PyExc_IndexError,"vector[index]: out of range\n");
return NULL;
}
return PyFloat_FromDouble(self->vec[i]);
@@ -388,13 +378,13 @@ static int Vector_ass_item(VectorObject * self, int i, PyObject * ob)
{
if(!(PyNumber_Check(ob))) { /* parsed item not a number */
return EXPP_ReturnIntError(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;
}
if(i < 0 || i >= self->size){
return EXPP_ReturnIntError(PyExc_IndexError,
"vector[index] = x: assignment index out of range\n");
PyErr_SetString(PyExc_IndexError, "vector[index] = x: assignment index out of range\n");
return -1;
}
self->vec[i] = (float)PyFloat_AsDouble(ob);
return 0;
@@ -436,21 +426,21 @@ static int Vector_ass_slice(VectorObject * self, int begin, int end,
size = PySequence_Length(seq);
if(size != (end - begin)){
return EXPP_ReturnIntError(PyExc_TypeError,
"vector[begin:end] = []: size mismatch in slice assignment\n");
PyErr_SetString(PyExc_TypeError, "vector[begin:end] = []: size mismatch in slice assignment\n");
return -1;
}
for (i = 0; i < size; i++) {
v = PySequence_GetItem(seq, i);
if (v == NULL) { /* Failed to read sequence */
return EXPP_ReturnIntError(PyExc_RuntimeError,
"vector[begin:end] = []: unable to read sequence\n");
PyErr_SetString(PyExc_RuntimeError, "vector[begin:end] = []: unable to read sequence\n");
return -1;
}
if(!PyNumber_Check(v)) { /* parsed item not a number */
Py_DECREF(v);
return EXPP_ReturnIntError(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;
}
vec[i] = (float)PyFloat_AsDouble(v);
@@ -481,33 +471,18 @@ static PyObject *Vector_add(PyObject * v1, PyObject * v2)
/* make sure v1 is always the vector */
if (vec1 && vec2 ) {
/*VECTOR + VECTOR*/
if(vec1->size != vec2->size)
return EXPP_ReturnPyObjError(PyExc_AttributeError,
"Vector addition: vectors must have the same dimensions for this operation\n");
if(vec1->size != vec2->size) {
PyErr_SetString(PyExc_AttributeError, "Vector addition: vectors must have the same dimensions for this operation\n");
return NULL;
}
for(i = 0; i < vec1->size; i++) {
vec[i] = vec1->vec[i] + vec2->vec[i];
}
return newVectorObject(vec, vec1->size, Py_NEW);
}
if(PointObject_Check(v2)){ /*VECTOR + POINT*/
/*Point translation*/
PointObject *pt = (PointObject*)v2;
if(pt->size == vec1->size){
for(i = 0; i < vec1->size; i++){
vec[i] = vec1->vec[i] + pt->coord[i];
}
}else{
return EXPP_ReturnPyObjError(PyExc_AttributeError,
"Vector addition: arguments are the wrong size....\n");
}
return newPointObject(vec, vec1->size, Py_NEW);
}
return EXPP_ReturnPyObjError(PyExc_AttributeError,
"Vector addition: arguments not valid for this operation....\n");
PyErr_SetString(PyExc_AttributeError, "Vector addition: arguments not valid for this operation....\n");
return NULL;
}
/* ------------------------obj += obj------------------------------
@@ -527,10 +502,10 @@ static PyObject *Vector_iadd(PyObject * v1, PyObject * v2)
/* make sure v1 is always the vector */
if (vec1 && vec2 ) {
/*VECTOR + VECTOR*/
if(vec1->size != vec2->size)
return EXPP_ReturnPyObjError(PyExc_AttributeError,
"Vector addition: vectors must have the same dimensions for this operation\n");
if(vec1->size != vec2->size) {
PyErr_SetString(PyExc_AttributeError, "Vector addition: vectors must have the same dimensions for this operation\n");
return NULL;
}
for(i = 0; i < vec1->size; i++) {
vec1->vec[i] += vec2->vec[i];
}
@@ -538,24 +513,8 @@ static PyObject *Vector_iadd(PyObject * v1, PyObject * v2)
return v1;
}
if(PointObject_Check(v2)){ /*VECTOR + POINT*/
/*Point translation*/
PointObject *pt = (PointObject*)v2;
if(pt->size == vec1->size){
for(i = 0; i < vec1->size; i++){
vec1->vec[i] += pt->coord[i];
}
}else{
return EXPP_ReturnPyObjError(PyExc_AttributeError,
"Vector addition: arguments are the wrong size....\n");
}
Py_INCREF( v1 );
return v1;
}
return EXPP_ReturnPyObjError(PyExc_AttributeError,
"Vector addition: arguments not valid for this operation....\n");
PyErr_SetString(PyExc_AttributeError, "Vector addition: arguments not valid for this operation....\n");
return NULL;
}
/*------------------------obj - obj------------------------------
@@ -566,17 +525,17 @@ static PyObject *Vector_sub(PyObject * v1, PyObject * v2)
float vec[4];
VectorObject *vec1 = NULL, *vec2 = NULL;
if (!VectorObject_Check(v1) || !VectorObject_Check(v2))
return EXPP_ReturnPyObjError(PyExc_AttributeError,
"Vector subtraction: arguments not valid for this operation....\n");
if (!VectorObject_Check(v1) || !VectorObject_Check(v2)) {
PyErr_SetString(PyExc_AttributeError, "Vector subtraction: arguments not valid for this operation....\n");
return NULL;
}
vec1 = (VectorObject*)v1;
vec2 = (VectorObject*)v2;
if(vec1->size != vec2->size)
return EXPP_ReturnPyObjError(PyExc_AttributeError,
"Vector subtraction: vectors must have the same dimensions for this operation\n");
if(vec1->size != vec2->size) {
PyErr_SetString(PyExc_AttributeError, "Vector subtraction: vectors must have the same dimensions for this operation\n");
return NULL;
}
for(i = 0; i < vec1->size; i++) {
vec[i] = vec1->vec[i] - vec2->vec[i];
}
@@ -591,16 +550,17 @@ static PyObject *Vector_isub(PyObject * v1, PyObject * v2)
int i, size;
VectorObject *vec1 = NULL, *vec2 = NULL;
if (!VectorObject_Check(v1) || !VectorObject_Check(v2))
return EXPP_ReturnPyObjError(PyExc_AttributeError,
"Vector subtraction: arguments not valid for this operation....\n");
if (!VectorObject_Check(v1) || !VectorObject_Check(v2)) {
PyErr_SetString(PyExc_AttributeError, "Vector subtraction: arguments not valid for this operation....\n");
return NULL;
}
vec1 = (VectorObject*)v1;
vec2 = (VectorObject*)v2;
if(vec1->size != vec2->size)
return EXPP_ReturnPyObjError(PyExc_AttributeError,
"Vector subtraction: vectors must have the same dimensions for this operation\n");
if(vec1->size != vec2->size) {
PyErr_SetString(PyExc_AttributeError, "Vector subtraction: vectors must have the same dimensions for this operation\n");
return NULL;
}
size = vec1->size;
for(i = 0; i < vec1->size; i++) {
@@ -628,9 +588,10 @@ static PyObject *Vector_mul(PyObject * v1, PyObject * v2)
int i;
double dot = 0.0f;
if(vec1->size != vec2->size)
return EXPP_ReturnPyObjError(PyExc_AttributeError,
"Vector multiplication: vectors must have the same dimensions for this operation\n");
if(vec1->size != vec2->size) {
PyErr_SetString(PyExc_AttributeError, "Vector multiplication: vectors must have the same dimensions for this operation\n");
return NULL;
}
/*dot product*/
for(i = 0; i < vec1->size; i++) {
@@ -664,15 +625,15 @@ static PyObject *Vector_mul(PyObject * v1, PyObject * v2)
return row_vector_multiplication(vec1, (MatrixObject*)v2);
} else if (QuaternionObject_Check(v2)) {
QuaternionObject *quat = (QuaternionObject*)v2;
if(vec1->size != 3)
return EXPP_ReturnPyObjError(PyExc_TypeError,
"Vector multiplication: only 3D vector rotations (with quats) currently supported\n");
if(vec1->size != 3) {
PyErr_SetString(PyExc_TypeError, "Vector multiplication: only 3D vector rotations (with quats) currently supported\n");
return NULL;
}
return quat_rotation((PyObject*)vec1, (PyObject*)quat);
}
return EXPP_ReturnPyObjError(PyExc_TypeError,
"Vector multiplication: arguments not acceptable for this operation\n");
PyErr_SetString(PyExc_TypeError, "Vector multiplication: arguments not acceptable for this operation\n");
return NULL;
}
/*------------------------obj *= obj------------------------------
@@ -702,8 +663,8 @@ static PyObject *Vector_imul(PyObject * v1, PyObject * v2)
if(mat->colSize != size){
if(mat->rowSize == 4 && vec->size != 3){
return EXPP_ReturnPyObjError(PyExc_AttributeError,
"vector * matrix: matrix column size and the vector size must be the same");
PyErr_SetString(PyExc_AttributeError, "vector * matrix: matrix column size and the vector size must be the same");
return NULL;
} else {
vecCopy[3] = 1.0f;
}
@@ -726,8 +687,8 @@ static PyObject *Vector_imul(PyObject * v1, PyObject * v2)
Py_INCREF( v1 );
return v1;
}
return EXPP_ReturnPyObjError(PyExc_TypeError,
"Vector multiplication: arguments not acceptable for this operation\n");
PyErr_SetString(PyExc_TypeError, "Vector multiplication: arguments not acceptable for this operation\n");
return NULL;
}
/*------------------------obj / obj------------------------------
@@ -738,22 +699,22 @@ static PyObject *Vector_div(PyObject * v1, PyObject * v2)
float vec[4], scalar;
VectorObject *vec1 = NULL;
if(!VectorObject_Check(v1)) /* not a vector */
return EXPP_ReturnPyObjError(PyExc_TypeError,
"Vector division: Vector must be divided by a float\n");
if(!VectorObject_Check(v1)) { /* not a vector */
PyErr_SetString(PyExc_TypeError, "Vector division: Vector must be divided by a float\n");
return NULL;
}
vec1 = (VectorObject*)v1; /* vector */
if(!PyNumber_Check(v2)) /* parsed item not a number */
return EXPP_ReturnPyObjError(PyExc_TypeError,
"Vector division: Vector must be divided by a float\n");
if(!PyNumber_Check(v2)) { /* parsed item not a number */
PyErr_SetString(PyExc_TypeError, "Vector division: Vector must be divided by a float\n");
return NULL;
}
scalar = (float)PyFloat_AsDouble(v2);
if(scalar==0.0) /* not a vector */
return EXPP_ReturnPyObjError(PyExc_ZeroDivisionError,
"Vector division: divide by zero error.\n");
if(scalar==0.0) { /* not a vector */
PyErr_SetString(PyExc_ZeroDivisionError, "Vector division: divide by zero error.\n");
return NULL;
}
size = vec1->size;
for(i = 0; i < size; i++) {
vec[i] = vec1->vec[i] / scalar;
@@ -769,22 +730,24 @@ static PyObject *Vector_idiv(PyObject * v1, PyObject * v2)
float scalar;
VectorObject *vec1 = NULL;
/*if(!VectorObject_Check(v1))
return EXPP_ReturnIntError(PyExc_TypeError,
"Vector division: Vector must be divided by a float\n");*/
/*if(!VectorObject_Check(v1)) {
PyErr_SetString(PyExc_TypeError, "Vector division: Vector must be divided by a float\n");
return -1;
}*/
vec1 = (VectorObject*)v1; /* vector */
if(!PyNumber_Check(v2)) /* parsed item not a number */
return EXPP_ReturnPyObjError(PyExc_TypeError,
"Vector division: Vector must be divided by a float\n");
if(!PyNumber_Check(v2)) { /* parsed item not a number */
PyErr_SetString(PyExc_TypeError, "Vector division: Vector must be divided by a float\n");
return NULL;
}
scalar = (float)PyFloat_AsDouble(v2);
if(scalar==0.0) /* not a vector */
return EXPP_ReturnPyObjError(PyExc_ZeroDivisionError,
"Vector division: divide by zero error.\n");
if(scalar==0.0) { /* not a vector */
PyErr_SetString(PyExc_ZeroDivisionError, "Vector division: divide by zero error.\n");
return NULL;
}
size = vec1->size;
for(i = 0; i < size; i++) {
vec1->vec[i] /= scalar;
@@ -854,9 +817,9 @@ PyObject* Vector_richcmpr(PyObject *objectA, PyObject *objectB, int comparison_t
if (!VectorObject_Check(objectA) || !VectorObject_Check(objectB)){
if (comparison_type == Py_NE){
return EXPP_incr_ret(Py_True);
Py_RETURN_TRUE;
}else{
return EXPP_incr_ret(Py_False);
Py_RETURN_FALSE;
}
}
vecA = (VectorObject*)objectA;
@@ -864,9 +827,9 @@ PyObject* Vector_richcmpr(PyObject *objectA, PyObject *objectB, int comparison_t
if (vecA->size != vecB->size){
if (comparison_type == Py_NE){
return EXPP_incr_ret(Py_True);
Py_RETURN_TRUE;
}else{
return EXPP_incr_ret(Py_False);
Py_RETURN_FALSE;
}
}
@@ -919,9 +882,9 @@ PyObject* Vector_richcmpr(PyObject *objectA, PyObject *objectB, int comparison_t
break;
}
if (result == 1){
return EXPP_incr_ret(Py_True);
Py_RETURN_TRUE;
}else{
return EXPP_incr_ret(Py_False);
Py_RETURN_FALSE;
}
}
/*-----------------PROTCOL DECLARATIONS--------------------------*/
@@ -1003,23 +966,24 @@ static PyObject *Vector_getAxis( VectorObject * self, void *type )
case 'Y':
return PyFloat_FromDouble(self->vec[1]);
case 'Z': /* these are backwards, but that how it works */
if(self->size < 3)
return EXPP_ReturnPyObjError(PyExc_AttributeError,
"vector.z: error, cannot get this axis for a 2D vector\n");
else
if(self->size < 3) {
PyErr_SetString(PyExc_AttributeError, "vector.z: error, cannot get this axis for a 2D vector\n");
return NULL;
}
else {
return PyFloat_FromDouble(self->vec[2]);
}
case 'W':
if(self->size < 4)
return EXPP_ReturnPyObjError(PyExc_AttributeError,
"vector.w: error, cannot get this axis for a 3D vector\n");
if(self->size < 4) {
PyErr_SetString(PyExc_AttributeError, "vector.w: error, cannot get this axis for a 3D vector\n");
return NULL;
}
return PyFloat_FromDouble(self->vec[3]);
default:
{
char errstr[1024];
sprintf( errstr, "undefined type '%d' in Vector_getAxis",
(int)((long)type & 0xff));
return EXPP_ReturnPyObjError( PyExc_RuntimeError, errstr );
PyErr_SetString( PyExc_RuntimeError, "undefined type in Vector_getAxis" );
return NULL;
}
}
}
@@ -1028,10 +992,10 @@ static int Vector_setAxis( VectorObject * self, PyObject * value, void * type )
{
float param= (float)PyFloat_AsDouble( value );
if (param==-1 && PyErr_Occurred())
return EXPP_ReturnIntError( PyExc_TypeError,
"expected a number for the vector axis" );
if (param==-1 && PyErr_Occurred()) {
PyErr_SetString( PyExc_TypeError, "expected a number for the vector axis" );
return -1;
}
switch( (long)type ) {
case 'X': /* these are backwards, but that how it works */
self->vec[0]= param;
@@ -1040,16 +1004,17 @@ static int Vector_setAxis( VectorObject * self, PyObject * value, void * type )
self->vec[1]= param;
break;
case 'Z': /* these are backwards, but that how it works */
if(self->size < 3)
return EXPP_ReturnIntError(PyExc_AttributeError,
"vector.z: error, cannot get this axis for a 2D vector\n");
if(self->size < 3) {
PyErr_SetString(PyExc_AttributeError, "vector.z: error, cannot get this axis for a 2D vector\n");
return -1;
}
self->vec[2]= param;
break;
case 'W':
if(self->size < 4)
return EXPP_ReturnIntError(PyExc_AttributeError,
"vector.w: error, cannot get this axis for a 3D vector\n");
if(self->size < 4) {
PyErr_SetString(PyExc_AttributeError, "vector.w: error, cannot get this axis for a 3D vector\n");
return -1;
}
self->vec[3]= param;
break;
}
@@ -1074,16 +1039,16 @@ static int Vector_setLength( VectorObject * self, PyObject * value )
double dot = 0.0f, param;
int i;
if (!PyNumber_Check(value))
return EXPP_ReturnIntError( PyExc_TypeError,
"expected a number for the vector axis" );
if (!PyNumber_Check(value)) {
PyErr_SetString( PyExc_TypeError, "expected a number for the vector axis" );
return -1;
}
param= PyFloat_AsDouble( value );
if (param < 0)
return EXPP_ReturnIntError( PyExc_TypeError,
"cannot set a vectors length to a negative value" );
if (param < 0) {
PyErr_SetString( PyExc_TypeError, "cannot set a vectors length to a negative value" );
return -1;
}
if (param==0) {
for(i = 0; i < self->size; i++){
self->vec[i]= 0;
@@ -1297,7 +1262,8 @@ PyObject *Vector_Negate(VectorObject * self)
self->vec[i] = -(self->vec[i]);
}
/*printf("Vector.negate(): Deprecated: use -vector instead\n");*/
return EXPP_incr_ret((PyObject*)self);
Py_INCREF(self);
return (PyObject*)self;
}
/*###################################################################
###########################DEPRECATED##############################*/

View File

@@ -50,7 +50,6 @@ 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 *Vector_ToTrackQuat( VectorObject * self, PyObject * args );
PyObject *Vector_reflect( VectorObject * self, PyObject * value );
PyObject *Vector_copy( VectorObject * self );