diff options
author | Stephen Swaney <sswaney@centurytel.net> | 2004-05-16 03:10:37 +0400 |
---|---|---|
committer | Stephen Swaney <sswaney@centurytel.net> | 2004-05-16 03:10:37 +0400 |
commit | ae20f7a95e818175ea76cf4b93fb413416c10f73 (patch) | |
tree | c02b3336083764827bd1725ded42391f1648b95f /source/blender | |
parent | 7b78a57c033d6aa6c6746e3502b9436b69a60cbc (diff) |
bugfix: #1206 Object.getBoundBox() was returning obdata coordinates.
fix memory leak in vector module. Memory allocated by vector constructor
was not being freed.
Diffstat (limited to 'source/blender')
-rw-r--r-- | source/blender/python/api2_2x/Object.c | 75 | ||||
-rw-r--r-- | source/blender/python/api2_2x/vector.c | 17 | ||||
-rw-r--r-- | source/blender/python/api2_2x/vector.h | 1 |
3 files changed, 83 insertions, 10 deletions
diff --git a/source/blender/python/api2_2x/Object.c b/source/blender/python/api2_2x/Object.c index 91cd24c345b..7285d427099 100644 --- a/source/blender/python/api2_2x/Object.c +++ b/source/blender/python/api2_2x/Object.c @@ -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; diff --git a/source/blender/python/api2_2x/vector.c b/source/blender/python/api2_2x/vector.c index 5ef10cdb9e6..a7e12a3d788 100644 --- a/source/blender/python/api2_2x/vector.c +++ b/source/blender/python/api2_2x/vector.c @@ -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; diff --git a/source/blender/python/api2_2x/vector.h b/source/blender/python/api2_2x/vector.h index f8ad164543b..733602f2f12 100644 --- a/source/blender/python/api2_2x/vector.h +++ b/source/blender/python/api2_2x/vector.h @@ -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 |