bugfix: #1206 Object.getBoundBox() was returning obdata coordinates.

fix memory leak in vector module.  Memory allocated by vector constructor
was not being freed.
This commit is contained in:
Stephen Swaney 2004-05-15 23:10:37 +00:00
parent 7b78a57c03
commit ae20f7a95e
3 changed files with 83 additions and 10 deletions

View File

@ -914,6 +914,7 @@ static PyObject *Object_getType (BPy_Object *self)
}
}
static PyObject *Object_getBoundBox (BPy_Object *self)
{
int i;
@ -924,7 +925,7 @@ static PyObject *Object_getBoundBox (BPy_Object *self)
return EXPP_ReturnPyObjError (PyExc_AttributeError,
"This object isn't linked to any object data (mesh, curve, etc) yet");
if (!self->object->bb) {
if (!self->object->bb) { /* if no ob bbox, we look in obdata */
Mesh *me;
Curve *curve;
switch (self->object->type) {
@ -944,28 +945,84 @@ static PyObject *Object_getBoundBox (BPy_Object *self)
Py_INCREF (Py_None);
return Py_None;
}
{ /* transform our obdata bbox by the obmat.
the obmat is 4x4 homogeneous coords matrix.
each bbox coord is xyz, so we make it homogenous
by padding it with w=1.0 and doing the matrix mult.
afterwards we divide by w to get back to xyz.
*/
/* printmatrix4( "obmat", self->object->obmat); */
float tmpvec[4]; /* tmp vector for homogenous coords math */
int i;
float *from;
float *to;
bbox = PyList_New(8);
if (!bbox)
return EXPP_ReturnPyObjError (PyExc_MemoryError,
"couldn't create pylist");
for( i = 0, from = vec;
i < 8;
i++, from += 3 ) {
memcpy( tmpvec, from, 3*sizeof(float));
tmpvec[3]=1.0f; /* set w coord */
Mat4MulVec4fl( self->object->obmat, tmpvec );
/* divide x,y,z by w */
tmpvec[0] /= tmpvec[3];
tmpvec[1] /= tmpvec[3];
tmpvec[2] /= tmpvec[3];
#if 0
{ /* debug print stuff */
int i;
printf("\nobj bbox transformed\n");
for( i=0; i<4; ++i)
printf( "%f ", tmpvec[i]);
printf("\n");
}
#endif
/* because our bounding box is calculated and
does not have its own memory,
we must create vectors that allocate space */
vector = newVectorObject( NULL, 3);
memcpy( ((VectorObject*)vector)->vec,
tmpvec,
3*sizeof(float));
PyList_SET_ITEM(bbox, i, vector);
}
}
}
else vec = (float *)self->object->bb->vec;
else{ /* the ob bbox exists */
vec = (float *)self->object->bb->vec;
if (!vec)
return EXPP_ReturnPyObjError (PyExc_RuntimeError,
"couldn't retrieve bounding box data");
if (!vec)
return EXPP_ReturnPyObjError (PyExc_RuntimeError,
"couldn't retrieve bounding box data");
bbox = PyList_New(8);
bbox = PyList_New(8);
if (!bbox)
return EXPP_ReturnPyObjError (PyExc_MemoryError,
if (!bbox)
return EXPP_ReturnPyObjError (PyExc_MemoryError,
"couldn't create pylist");
for (i = 0; i < 8; i++) {
/* create vectors referencing object bounding box coords */
for (i = 0; i < 8; i++) {
vector = newVectorObject(vec, 3);
PyList_SET_ITEM(bbox, i, vector);
vec += 3;
}
}
return bbox;
}
static PyObject *Object_makeDisplayList (BPy_Object *self)
{
Object *ob = self->object;

View File

@ -181,7 +181,11 @@ PyObject *Vector_Resize4D(VectorObject *self)
static void Vector_dealloc(VectorObject *self)
{
PyObject_DEL (self);
/* if we own this memory we must delete it */
if( self->delete_pymem )
PyMem_Free( self->vec );
PyObject_DEL (self);
}
static PyObject *Vector_getattr(VectorObject *self, char *name)
@ -573,6 +577,15 @@ PyTypeObject vector_Type =
&Vector_SeqMethods, /*tp_as_sequence*/
};
/*
* create a Vector Object
* if vec arg is NULL
* allocate memory on python stack.
* initialize to zero in homogenous coords.
* size arg is number of floats to alloc.
*/
PyObject *newVectorObject(float *vec, int size)
{
VectorObject *self;
@ -588,8 +601,10 @@ PyObject *newVectorObject(float *vec, int size)
self->vec[x] = 0.0f;
}
if(size == 4) self->vec[3] = 1.0f;
self->delete_pymem = 1; /* must free this alloc later */
}else{
self->vec = vec;
self->delete_pymem = 0;
}
self->size = size;

View File

@ -57,6 +57,7 @@ typedef struct {
//0 - no coercion
//1 - coerced from int
//2 - coerced from float
int delete_pymem; /* do we need to delete the memory vec points at? */
} VectorObject;
//prototypes