diff options
Diffstat (limited to 'source/blender/blenlib/intern/pbvh.c')
-rw-r--r-- | source/blender/blenlib/intern/pbvh.c | 65 |
1 files changed, 65 insertions, 0 deletions
diff --git a/source/blender/blenlib/intern/pbvh.c b/source/blender/blenlib/intern/pbvh.c index 1fd18e2967c..92e2c88e8a1 100644 --- a/source/blender/blenlib/intern/pbvh.c +++ b/source/blender/blenlib/intern/pbvh.c @@ -29,6 +29,7 @@ #include "BLI_pbvh.h" #include "BKE_DerivedMesh.h" +#include "BKE_mesh.h" /* for mesh_calc_normals */ #include "gpu_buffers.h" @@ -120,6 +121,9 @@ struct PBVH { #ifdef PERFCNTRS int perf_modified; #endif + + /* flag are verts/faces deformed */ + int deformed; }; #define STACK_FIXED_DEPTH 100 @@ -576,6 +580,15 @@ void BLI_pbvh_free(PBVH *bvh) } } + if (bvh->deformed) { + if (bvh->verts) { + /* if pbvh was deformed, new memory was allocated for verts/faces -- free it */ + + MEM_freeN(bvh->verts); + MEM_freeN(bvh->faces); + } + } + MEM_freeN(bvh->nodes); MEM_freeN(bvh->prim_indices); MEM_freeN(bvh); @@ -1330,3 +1343,55 @@ void BLI_pbvh_grids_update(PBVH *bvh, DMGridData **grids, DMGridAdjacency *grida bvh->gridfaces= gridfaces; } +float (*BLI_pbvh_get_vertCos(PBVH *pbvh))[3] +{ + int a; + float (*vertCos)[3]= NULL; + + if (pbvh->verts) { + float *co; + MVert *mvert= pbvh->verts; + + vertCos= MEM_callocN(3*pbvh->totvert*sizeof(float), "BLI_pbvh_get_vertCoords"); + co= (float*)vertCos; + + for (a= 0; a<pbvh->totvert; a++, mvert++, co+= 3) { + copy_v3_v3(co, mvert->co); + } + } + + return vertCos; +} + +void BLI_pbvh_apply_vertCos(PBVH *pbvh, float (*vertCos)[3]) +{ + int a; + + if (!pbvh->deformed) { + if (pbvh->verts) { + /* if pbvh is not already deformed, verts/faces points to the */ + /* original data and applying new coords to this arrays would lead to */ + /* unneeded deformation -- duplicate verts/faces to avoid this */ + + pbvh->verts= MEM_dupallocN(pbvh->verts); + pbvh->faces= MEM_dupallocN(pbvh->faces); + + pbvh->deformed= 1; + } + } + + if (pbvh->verts) { + /* copy new verts coords */ + for (a= 0; a < pbvh->totvert; ++a) { + copy_v3_v3(pbvh->verts[a].co, vertCos[a]); + } + + /* coordinates are new -- normals should also be updated */ + mesh_calc_normals(pbvh->verts, pbvh->totvert, pbvh->faces, pbvh->totprim, NULL); + } +} + +int BLI_pbvh_isDeformed(PBVH *pbvh) +{ + return pbvh->deformed; +} |