diff --git a/source/blender/blenkernel/BKE_bmesh.h b/source/blender/blenkernel/BKE_bmesh.h index 51a5d29dbb7..8ec7144faf6 100644 --- a/source/blender/blenkernel/BKE_bmesh.h +++ b/source/blender/blenkernel/BKE_bmesh.h @@ -39,12 +39,12 @@ #include "DNA_listBase.h" #include "BLI_ghash.h" +#include "BLI_mempool.h" #include "BLI_memarena.h" #include "DNA_image_types.h" #include "BLI_editVert.h" #include "BKE_DerivedMesh.h" #include "transform.h" -#include "BKE_bmeshCustomData.h" /*forward declerations*/ struct BME_Vert; @@ -53,13 +53,6 @@ struct BME_Poly; struct BME_Loop; -/*structure for fast memory allocation/frees*/ -typedef struct BME_mempool{ - struct ListBase chunks; - int esize, csize, pchunk; /*size of elements and chunks in bytes and number of elements per chunk*/ - struct BME_freenode *free; /*free element list. Interleaved into chunk datas.*/ -}BME_mempool; - /*Notes on further structure Cleanup: -Remove the tflags, they belong in custom data layers -Remove the eflags completely, they are mostly not used @@ -78,10 +71,10 @@ typedef struct BME_Mesh { ListBase verts, edges, polys; /*memory pools used for storing mesh elements*/ - struct BME_mempool *vpool; - struct BME_mempool *epool; - struct BME_mempool *ppool; - struct BME_mempool *lpool; + struct BLI_mempool *vpool; + struct BLI_mempool *epool; + struct BLI_mempool *ppool; + struct BLI_mempool *lpool; /*some scratch arrays used by eulers*/ struct BME_Vert **vtar; struct BME_Edge **edar; @@ -90,7 +83,7 @@ typedef struct BME_Mesh int vtarlen, edarlen, lparlen, plarlen; int totvert, totedge, totpoly, totloop; /*record keeping*/ int nextv, nexte, nextp, nextl; /*Next element ID for verts/edges/faces/loops. Never reused*/ - struct BME_CustomData vdata, edata, pdata, ldata; /*Custom Data Layer information*/ + struct CustomData vdata, edata, pdata, ldata; /*Custom Data Layer information*/ } BME_Mesh; typedef struct BME_Vert @@ -169,7 +162,7 @@ int BME_radial_find_face(struct BME_Edge *e,struct BME_Poly *f); struct BME_Loop *BME_loop_find_loop(struct BME_Poly *f, struct BME_Vert *v); /*MESH CREATION/DESTRUCTION*/ -struct BME_Mesh *BME_make_mesh(int allocsize[4], struct BME_CustomDataInit init[4]); +struct BME_Mesh *BME_make_mesh(int allocsize[4]); void BME_free_mesh(struct BME_Mesh *bm); /*FULL MESH VALIDATION*/ int BME_validate_mesh(struct BME_Mesh *bm, int halt); diff --git a/source/blender/blenkernel/BKE_customdata.h b/source/blender/blenkernel/BKE_customdata.h index d0535f1752e..81c2e4a4b94 100644 --- a/source/blender/blenkernel/BKE_customdata.h +++ b/source/blender/blenkernel/BKE_customdata.h @@ -40,6 +40,7 @@ extern const CustomDataMask CD_MASK_BAREMESH; extern const CustomDataMask CD_MASK_MESH; extern const CustomDataMask CD_MASK_EDITMESH; extern const CustomDataMask CD_MASK_DERIVEDMESH; +extern const CustomDataMask CD_MASK_BMESH; /* for ORIGINDEX layer type, indicates no original index for this element */ #define ORIGINDEX_NONE -1 @@ -134,6 +135,9 @@ void CustomData_copy_data(const struct CustomData *source, void CustomData_em_copy_data(const struct CustomData *source, struct CustomData *dest, void *src_block, void **dest_block); +void CustomData_bmesh_copy_data(const struct CustomData *source, + struct CustomData *dest,void *src_block, + void **dest_block); /* frees data in a CustomData object * return 1 on success, 0 on failure @@ -160,6 +164,10 @@ void CustomData_interp(const struct CustomData *source, struct CustomData *dest, void CustomData_em_interp(struct CustomData *data, void **src_blocks, float *weights, float *sub_weights, int count, void *dest_block); +void CustomData_bmesh_interp(struct CustomData *data, void **src_blocks, + float *weights, float *sub_weights, int count, + void *dest_block); + /* swaps the data in the element corners, to new corners with indices as specified in corner_indices. for edges this is an array of length 2, for @@ -172,6 +180,8 @@ void CustomData_swap(struct CustomData *data, int index, int *corner_indices); void *CustomData_get(const struct CustomData *data, int index, int type); void *CustomData_em_get(const struct CustomData *data, void *block, int type); void *CustomData_em_get_n(const struct CustomData *data, void *block, int type, int n); +void *CustomData_bmesh_get(const struct CustomData *data, void *block, int type); +void *CustomData_bmesh_get_n(const struct CustomData *data, void *block, int type, int n); /* gets a pointer to the active or first layer of type * returns NULL if there is no layer of type @@ -199,6 +209,12 @@ void CustomData_em_set(struct CustomData *data, void *block, int type, void CustomData_em_set_n(struct CustomData *data, void *block, int type, int n, void *source); +void CustomData_bmesh_set(const struct CustomData *data, void *block, int type, + void *source); + +void CustomData_bmesh_set_n(struct CustomData *data, void *block, int type, int n, + void *source); + /* set the pointer of to the first layer of type. the old data is not freed. * returns the value of ptr if the layer is found, NULL otherwise */ @@ -220,12 +236,20 @@ void CustomData_set_layer_flag(struct CustomData *data, int type, int flag); void CustomData_em_set_default(struct CustomData *data, void **block); void CustomData_em_free_block(struct CustomData *data, void **block); +void CustomData_bmesh_set_default(struct CustomData *data, void **block); +void CustomData_bmesh_free_block(struct CustomData *data, void **block); + /* copy custom data to/from layers as in mesh/derivedmesh, to editmesh blocks of data. the CustomData's must not be compatible */ void CustomData_to_em_block(const struct CustomData *source, struct CustomData *dest, int index, void **block); void CustomData_from_em_block(const struct CustomData *source, struct CustomData *dest, void *block, int index); +void CustomData_to_bmesh_block(const struct CustomData *source, + struct CustomData *dest, int src_index, void **dest_block); +void CustomData_from_bmesh_block(const struct CustomData *source, + struct CustomData *dest, void *src_block, int dest_index); + /* query info over types */ void CustomData_file_write_info(int type, char **structname, int *structnum); @@ -240,5 +264,4 @@ void CustomData_set_layer_unique_name(struct CustomData *data, int index); /* for file reading compatibility, returns false if the layer was freed, only after this test passes, layer->data should be assigned */ int CustomData_verify_versions(struct CustomData *data, int index); - #endif diff --git a/source/blender/blenkernel/intern/BME_conversions.c b/source/blender/blenkernel/intern/BME_conversions.c index 08483711c45..7952546de7c 100644 --- a/source/blender/blenkernel/intern/BME_conversions.c +++ b/source/blender/blenkernel/intern/BME_conversions.c @@ -33,6 +33,7 @@ */ #include "MEM_guardedalloc.h" +#include "BKE_customdata.h" #include "DNA_listBase.h" #include "DNA_meshdata_types.h" @@ -54,11 +55,14 @@ #include "bmesh_private.h" #include "BSE_edit.h" +/*Converts an EditMesh to a BME_Mesh.*/ +static void bmesh_init_cdPool(CustomData *data, int allocsize){ + if(data->totlayer)data->pool = BLI_mempool_create(data->totsize, allocsize, allocsize); +} BME_Mesh *BME_editmesh_to_bmesh(EditMesh *em) { BME_Mesh *bm; int allocsize[4] = {512,512,2048,512}; - BME_CustomDataInit *init = MEM_callocN(sizeof(BME_CustomDataInit) * 4, "Bmesh custom data init"); BME_Vert *v1, *v2; BME_Edge *e, *edar[4]; BME_Poly *f; @@ -68,9 +72,12 @@ BME_Mesh *BME_editmesh_to_bmesh(EditMesh *em) { EditFace *efa; int len; - bm = BME_make_mesh(allocsize,init); + bm = BME_make_mesh(allocsize); + + CustomData_copy(&em->vdata, &bm->vdata, CD_MASK_BMESH, CD_CALLOC, 0); + bmesh_init_cdPool(&bm->vdata, allocsize[0]); + BME_model_begin(bm); - /*add verts*/ eve= em->verts.first; while(eve) { @@ -80,8 +87,8 @@ BME_Mesh *BME_editmesh_to_bmesh(EditMesh *em) { v1->h = eve->h; v1->bweight = eve->bweight; - /* link the verts for edge and face construction; - * kind of a dangerous thing - remember to cast back to BME_Vert before using! */ + /*Copy Custom Data*/ + CustomData_bmesh_copy_data(&em->vdata, &bm->vdata, eve->data, &v1->data); eve->tmp.v = (EditVert*)v1; eve = eve->next; } @@ -102,6 +109,8 @@ BME_Mesh *BME_editmesh_to_bmesh(EditMesh *em) { /* link the edges for face construction; * kind of a dangerous thing - remember to cast back to BME_Edge before using! */ + /*Copy CustomData*/ + eed->tmp.e = (EditEdge*)e; eed = eed->next; } @@ -137,7 +146,6 @@ BME_Mesh *BME_editmesh_to_bmesh(EditMesh *em) { efa = efa->next; } BME_model_end(bm); - MEM_freeN(init); return bm; } @@ -161,6 +169,8 @@ EditMesh *BME_bmesh_to_editmesh(BME_Mesh *bm, BME_TransData_Head *td) { if (em == NULL) return NULL; + + CustomData_copy(&bm->vdata, &em->vdata, CD_MASK_BMESH, CD_CALLOC, 0); /* convert to EditMesh */ /* make editverts */ totvert = BLI_countlist(&(bm->verts)); @@ -176,6 +186,7 @@ EditMesh *BME_bmesh_to_editmesh(BME_Mesh *bm, BME_TransData_Head *td) { eve1->f = (unsigned char)v1->flag; eve1->h = (unsigned char)v1->h; eve1->bweight = v1->bweight; + CustomData_em_copy_data(&bm->vdata, &em->vdata, v1->data, &eve1->data); } /* make edges */ @@ -234,7 +245,6 @@ BME_Mesh *BME_derivedmesh_to_bmesh(DerivedMesh *dm) BME_Mesh *bm; int allocsize[4] = {512,512,2048,512}; - BME_CustomDataInit *init = MEM_callocN(sizeof(BME_CustomDataInit) * 4, "Bmesh custom data init"); MVert *mvert, *mv; MEdge *medge, *me; MFace *mface, *mf; @@ -245,7 +255,7 @@ BME_Mesh *BME_derivedmesh_to_bmesh(DerivedMesh *dm) EdgeHash *edge_hash = BLI_edgehash_new(); - bm = BME_make_mesh(allocsize,init); + bm = BME_make_mesh(allocsize); totvert = dm->getNumVerts(dm); totedge = dm->getNumEdges(dm); totface = dm->getNumFaces(dm); @@ -300,7 +310,6 @@ BME_Mesh *BME_derivedmesh_to_bmesh(DerivedMesh *dm) BME_model_end(bm); BLI_edgehash_free(edge_hash, NULL); MEM_freeN(vert_array); - MEM_freeN(init); return bm; } diff --git a/source/blender/blenkernel/intern/BME_mesh.c b/source/blender/blenkernel/intern/BME_mesh.c index 184ef2b8a0e..ad46a7c1eb7 100644 --- a/source/blender/blenkernel/intern/BME_mesh.c +++ b/source/blender/blenkernel/intern/BME_mesh.c @@ -32,64 +32,33 @@ * ***** END GPL LICENSE BLOCK ***** */ + #include "MEM_guardedalloc.h" - #include "DNA_listBase.h" -#include "DNA_meshdata_types.h" -#include "DNA_mesh_types.h" -#include "DNA_object_types.h" -#include "DNA_scene_types.h" - - +#include "BLI_blenlib.h" #include "BKE_utildefines.h" #include "BKE_bmesh.h" -#include "BKE_global.h" -#include "BKE_depsgraph.h" -#include "BLI_blenlib.h" -#include "BLI_editVert.h" -#include "BIF_editmesh.h" -#include "BIF_space.h" -#include "editmesh.h" #include "bmesh_private.h" -#include "mydevice.h" - -#include "BSE_edit.h" /* * BME MAKE MESH * * Allocates a new BME_Mesh structure. - * The arguments are two arrays, one of type int - * and another of type BME_CustomDataInit. The first array - * contains the allocation size for each element pool in - * the mesh. For instance allocsize[0] contains the number - * of vertices to allocate at a time for the vertex pool. - * - * The second array contains structures describing the layout - * of custom data for each element type in the mesh. So init[0] - * contains the custom data layout information for vertices, init[1] - * the layout information for edges and so on. - * * Returns - * Pointer to a Bmesh * */ -BME_Mesh *BME_make_mesh(int allocsize[4], BME_CustomDataInit init[4]) +BME_Mesh *BME_make_mesh(int allocsize[4]) { /*allocate the structure*/ BME_Mesh *bm = MEM_callocN(sizeof(BME_Mesh),"BMesh"); /*allocate the memory pools for the mesh elements*/ - bm->vpool = BME_mempool_create(sizeof(BME_Vert), allocsize[0], allocsize[0]); - bm->epool = BME_mempool_create(sizeof(BME_Edge), allocsize[1], allocsize[1]); - bm->lpool = BME_mempool_create(sizeof(BME_Loop), allocsize[2], allocsize[2]); - bm->ppool = BME_mempool_create(sizeof(BME_Poly), allocsize[3], allocsize[3]); - /*Setup custom data layers*/ - BME_CD_Create(&bm->vdata, &init[0], allocsize[0]); - BME_CD_Create(&bm->edata, &init[1], allocsize[1]); - BME_CD_Create(&bm->ldata, &init[2], allocsize[2]); - BME_CD_Create(&bm->pdata, &init[3], allocsize[3]); + bm->vpool = BLI_mempool_create(sizeof(BME_Vert), allocsize[0], allocsize[0]); + bm->epool = BLI_mempool_create(sizeof(BME_Edge), allocsize[1], allocsize[1]); + bm->lpool = BLI_mempool_create(sizeof(BME_Loop), allocsize[2], allocsize[2]); + bm->ppool = BLI_mempool_create(sizeof(BME_Poly), allocsize[3], allocsize[3]); return bm; } /* @@ -105,26 +74,35 @@ void BME_free_mesh(BME_Mesh *bm) BME_Loop *l; BME_Poly *f; - for(v=bm->verts.first; v; v=v->next) BME_CD_free_block(&bm->vdata, &v->data); - for(e=bm->edges.first; e; e=e->next) BME_CD_free_block(&bm->edata, &e->data); + for(v=bm->verts.first; v; v=v->next) CustomData_bmesh_free_block(&bm->vdata, &v->data); + for(e=bm->edges.first; e; e=e->next) CustomData_bmesh_free_block(&bm->edata, &e->data); for(f=bm->polys.first; f; f=f->next){ - BME_CD_free_block(&bm->pdata, &f->data); + CustomData_bmesh_free_block(&bm->pdata, &f->data); l = f->loopbase; do{ - BME_CD_free_block(&bm->ldata, &l->data); + CustomData_bmesh_free_block(&bm->ldata, &l->data); l = l->next; }while(l!=f->loopbase); } + + /*Free custom data pools, This should probably go in CustomData_free?*/ + if(bm->vdata.totlayer) BLI_mempool_destroy(bm->vdata.pool); + if(bm->edata.totlayer) BLI_mempool_destroy(bm->edata.pool); + if(bm->ldata.totlayer) BLI_mempool_destroy(bm->ldata.pool); + if(bm->pdata.totlayer) BLI_mempool_destroy(bm->pdata.pool); + + /*free custom data*/ + CustomData_free(&bm->vdata,0); + CustomData_free(&bm->edata,0); + CustomData_free(&bm->ldata,0); + CustomData_free(&bm->pdata,0); + /*destroy element pools*/ - BME_mempool_destroy(bm->vpool); - BME_mempool_destroy(bm->epool); - BME_mempool_destroy(bm->ppool); - BME_mempool_destroy(bm->lpool); - /*free custom data pools*/ - BME_CD_Free(&bm->vdata); - BME_CD_Free(&bm->edata); - BME_CD_Free(&bm->ldata); - BME_CD_Free(&bm->pdata); + BLI_mempool_destroy(bm->vpool); + BLI_mempool_destroy(bm->epool); + BLI_mempool_destroy(bm->ppool); + BLI_mempool_destroy(bm->lpool); + MEM_freeN(bm); } diff --git a/source/blender/blenkernel/intern/BME_structure.c b/source/blender/blenkernel/intern/BME_structure.c index cbf780c6467..92ef9e3e03c 100644 --- a/source/blender/blenkernel/intern/BME_structure.c +++ b/source/blender/blenkernel/intern/BME_structure.c @@ -42,100 +42,6 @@ #include "BLI_linklist.h" #include "BLI_ghash.h" -#include "BKE_customdata.h" - -/* - Simple, fast memory allocator for allocating many elements of the same size. -*/ -typedef struct BME_mempool_chunk{ - struct BME_mempool_chunk *next, *prev; - void *data; -}BME_mempool_chunk; - -/*this is just to make things prettier*/ -typedef struct BME_freenode{ - struct BME_freenode *next; -}BME_freenode; - -BME_mempool *BME_mempool_create(int esize, int tote, int pchunk) -{ BME_mempool *pool = NULL; - BME_freenode *lasttail = NULL, *curnode = NULL; - int i,j, maxchunks; - char *addr; - - /*allocate the pool structure*/ - pool = MEM_mallocN(sizeof(BME_mempool),"memory pool"); - pool->esize = esize; - pool->pchunk = pchunk; - pool->csize = esize * pchunk; - pool->chunks.first = pool->chunks.last = NULL; - - maxchunks = tote / pchunk; - - /*allocate the actual chunks*/ - for(i=0; i < maxchunks; i++){ - BME_mempool_chunk *mpchunk = MEM_mallocN(sizeof(BME_mempool_chunk), "BME_Mempool Chunk"); - mpchunk->next = mpchunk->prev = NULL; - mpchunk->data = MEM_mallocN(pool->csize, "BME Mempool Chunk Data"); - BLI_addtail(&(pool->chunks), mpchunk); - - if(i==0) pool->free = mpchunk->data; /*start of the list*/ - /*loop through the allocated data, building the pointer structures*/ - for(addr = mpchunk->data, j=0; j < pool->pchunk; j++){ - curnode = ((BME_freenode*)addr); - addr += pool->esize; - curnode->next = (BME_freenode*)addr; - } - /*final pointer in the previously allocated chunk is wrong.*/ - if(lasttail) lasttail->next = mpchunk->data; - /*set the end of this chunks memory to the new tail for next iteration*/ - lasttail = curnode; - } - /*terminate the list*/ - curnode->next = NULL; - return pool; -} - -void *BME_mempool_alloc(BME_mempool *pool){ - void *retval=NULL; - BME_freenode *curnode=NULL; - char *addr=NULL; - int j; - - if(!(pool->free)){ - /*need to allocate a new chunk*/ - BME_mempool_chunk *mpchunk = MEM_mallocN(sizeof(BME_mempool_chunk), "BME_Mempool Chunk"); - mpchunk->next = mpchunk->prev = NULL; - mpchunk->data = MEM_mallocN(pool->csize, "BME_Mempool Chunk Data"); - BLI_addtail(&(pool->chunks), mpchunk); - - pool->free = mpchunk->data; /*start of the list*/ - for(addr = mpchunk->data, j=0; j < pool->pchunk; j++){ - curnode = ((BME_freenode*)addr); - addr += pool->esize; - curnode->next = (BME_freenode*)addr; - } - curnode->next = NULL; /*terminate the list*/ - } - - retval = pool->free; - pool->free = pool->free->next; - //memset(retval, 0, pool->esize); - return retval; -} - -void BME_mempool_free(BME_mempool *pool, void *addr){ //doesnt protect against double frees, dont be stupid! - BME_freenode *newhead = addr; - newhead->next = pool->free; - pool->free = newhead; -} -void BME_mempool_destroy(BME_mempool *pool) -{ - BME_mempool_chunk *mpchunk=NULL; - for(mpchunk = pool->chunks.first; mpchunk; mpchunk = mpchunk->next) MEM_freeN(mpchunk->data); - BLI_freelistN(&(pool->chunks)); - MEM_freeN(pool); -} /** * MISC utility functions. * @@ -179,7 +85,7 @@ int BME_edge_swapverts(BME_Edge *e, BME_Vert *orig, BME_Vert *new){ BME_Vert *BME_addvertlist(BME_Mesh *bm, BME_Vert *example){ BME_Vert *v=NULL; - v = BME_mempool_alloc(bm->vpool); + v = BLI_mempool_alloc(bm->vpool); v->next = v->prev = NULL; v->EID = bm->nextv; v->co[0] = v->co[1] = v->co[2] = 0.0f; @@ -195,16 +101,16 @@ BME_Vert *BME_addvertlist(BME_Mesh *bm, BME_Vert *example){ if(example){ VECCOPY(v->co,example->co); - BME_CD_copy_data(&bm->vdata, &bm->vdata, example->data, &v->data); + CustomData_bmesh_copy_data(&bm->vdata, &bm->vdata, example->data, &v->data); } else - BME_CD_set_default(&bm->vdata, &v->data); + CustomData_bmesh_set_default(&bm->vdata, &v->data); return v; } BME_Edge *BME_addedgelist(BME_Mesh *bm, BME_Vert *v1, BME_Vert *v2, BME_Edge *example){ BME_Edge *e=NULL; - e = BME_mempool_alloc(bm->epool); + e = BLI_mempool_alloc(bm->epool); e->next = e->prev = NULL; e->EID = bm->nexte; e->v1 = v1; @@ -222,16 +128,16 @@ BME_Edge *BME_addedgelist(BME_Mesh *bm, BME_Vert *v1, BME_Vert *v2, BME_Edge *ex BLI_addtail(&(bm->edges), e); if(example) - BME_CD_copy_data(&bm->edata, &bm->edata, example->data, &e->data); + CustomData_bmesh_copy_data(&bm->edata, &bm->edata, example->data, &e->data); else - BME_CD_set_default(&bm->edata, &e->data); + CustomData_bmesh_set_default(&bm->edata, &e->data); return e; } BME_Loop *BME_create_loop(BME_Mesh *bm, BME_Vert *v, BME_Edge *e, BME_Poly *f, BME_Loop *example){ BME_Loop *l=NULL; - l = BME_mempool_alloc(bm->lpool); + l = BLI_mempool_alloc(bm->lpool); l->next = l->prev = NULL; l->EID = bm->nextl; l->radial.next = l->radial.prev = NULL; @@ -246,16 +152,16 @@ BME_Loop *BME_create_loop(BME_Mesh *bm, BME_Vert *v, BME_Edge *e, BME_Poly *f, B bm->totloop++; if(example) - BME_CD_copy_data(&bm->ldata, &bm->ldata, example->data, &l->data); + CustomData_bmesh_copy_data(&bm->ldata, &bm->ldata, example->data, &l->data); else - BME_CD_set_default(&bm->ldata, &l->data); + CustomData_bmesh_set_default(&bm->ldata, &l->data); return l; } BME_Poly *BME_addpolylist(BME_Mesh *bm, BME_Poly *example){ BME_Poly *f = NULL; - f = BME_mempool_alloc(bm->ppool); + f = BLI_mempool_alloc(bm->ppool); f->next = f->prev = NULL; f->EID = bm->nextp; f->loopbase = NULL; @@ -268,9 +174,9 @@ BME_Poly *BME_addpolylist(BME_Mesh *bm, BME_Poly *example){ bm->totpoly++; if(example) - BME_CD_copy_data(&bm->pdata, &bm->pdata, example->data, &f->data); + CustomData_bmesh_copy_data(&bm->pdata, &bm->pdata, example->data, &f->data); else - BME_CD_set_default(&bm->pdata, &f->data); + CustomData_bmesh_set_default(&bm->pdata, &f->data); return f; @@ -281,23 +187,23 @@ BME_Poly *BME_addpolylist(BME_Mesh *bm, BME_Poly *example){ */ void BME_free_vert(BME_Mesh *bm, BME_Vert *v){ bm->totvert--; - BME_CD_free_block(&bm->vdata, &v->data); - BME_mempool_free(bm->vpool, v); + CustomData_bmesh_free_block(&bm->vdata, &v->data); + BLI_mempool_free(bm->vpool, v); } void BME_free_edge(BME_Mesh *bm, BME_Edge *e){ bm->totedge--; - BME_CD_free_block(&bm->edata, &e->data); - BME_mempool_free(bm->epool, e); + CustomData_bmesh_free_block(&bm->edata, &e->data); + BLI_mempool_free(bm->epool, e); } void BME_free_poly(BME_Mesh *bm, BME_Poly *f){ bm->totpoly--; - BME_CD_free_block(&bm->pdata, &f->data); - BME_mempool_free(bm->ppool, f); + CustomData_bmesh_free_block(&bm->pdata, &f->data); + BLI_mempool_free(bm->ppool, f); } void BME_free_loop(BME_Mesh *bm, BME_Loop *l){ bm->totloop--; - BME_CD_free_block(&bm->ldata, &l->data); - BME_mempool_free(bm->lpool, l); + CustomData_bmesh_free_block(&bm->ldata, &l->data); + BLI_mempool_free(bm->lpool, l); } /** * BMESH CYCLES diff --git a/source/blender/blenkernel/intern/BME_tools.c b/source/blender/blenkernel/intern/BME_tools.c index 7ce967d1d22..916e6bee59f 100644 --- a/source/blender/blenkernel/intern/BME_tools.c +++ b/source/blender/blenkernel/intern/BME_tools.c @@ -205,7 +205,21 @@ static BME_Poly *BME_split_face(BME_Mesh *bm, BME_Poly *f, BME_Vert *v1, BME_Ver return nf; } -/* a wrapper for BME_SEMV that transfers element flags */ + +static void BME_data_interp_from_verts(BME_Mesh *bm, BME_Vert *v1, BME_Vert *v2, BME_Vert *v, float fac) +{ + void *src[2]; + float w[2]; + if (v1->data && v2->data) { + src[0]= v1->data; + src[1]= v2->data; + w[0] = 1.0f-fac; + w[1] = fac; + CustomData_em_interp(&bm->vdata, src, w, NULL, 2, v->data); + } +} + +/* a wrapper for BME_SEMV that transfers element flags */ /*add custom data interpolation in here!*/ static BME_Vert *BME_split_edge(BME_Mesh *bm, BME_Vert *v, BME_Edge *e, BME_Edge **ne, float percent) { BME_Vert *nv, *v2; float len; @@ -224,10 +238,11 @@ static BME_Vert *BME_split_edge(BME_Mesh *bm, BME_Vert *v, BME_Edge *e, BME_Edge (*ne)->crease = e->crease; (*ne)->bweight = e->bweight; } - return nv; } + + static int BME_bevel_is_split_vert(BME_Loop *l) { /* look for verts that have already been added to the edge when * beveling other polys; this can be determined by testing the @@ -315,7 +330,7 @@ static float BME_bevel_project_vec(float *vec1, float *vec2, float *up_vec, int * Finally, return the split vert. */ static BME_Vert *BME_bevel_split_edge(BME_Mesh *bm, BME_Vert *v, BME_Vert *v1, BME_Loop *l, float *up_vec, float value, BME_TransData_Head *td) { BME_TransData *vtd, *vtd1, *vtd2; - BME_Vert *sv, *v2, *v3; + BME_Vert *sv, *v2, *v3, *ov; BME_Loop *lv1, *lv2; BME_Edge *ne, *e1, *e2; float maxfactor, scale, len, dis, vec1[3], vec2[3], t_up_vec[3]; @@ -349,7 +364,9 @@ static BME_Vert *BME_bevel_split_edge(BME_Mesh *bm, BME_Vert *v, BME_Vert *v1, B else { e1 = e2; } + ov = BME_edge_getothervert(e1,v); sv = BME_split_edge(bm,v,e1,&ne,0); + //BME_data_interp_from_verts(bm, v, ov, sv, 0.25); /*this is technically wrong...*/ BME_assign_transdata(td, bm, sv, sv->co, sv->co, NULL, sv->co, 0, -1, -1, NULL); /* quick default */ sv->tflag1 |= BME_BEVEL_BEVEL; ne->tflag1 = BME_BEVEL_ORIG; /* mark edge as original, even though it isn't */ @@ -388,7 +405,9 @@ static BME_Vert *BME_bevel_split_edge(BME_Mesh *bm, BME_Vert *v, BME_Vert *v1, B } else { is_split_vert = 0; + ov = BME_edge_getothervert(l->e,v); sv = BME_split_edge(bm,v,l->e,&ne,0); + //BME_data_interp_from_verts(bm, v, ov, sv, 0.25); /*this is technically wrong...*/ BME_assign_transdata(td, bm, sv, sv->co, sv->co, NULL, sv->co, 0, -1, -1, NULL); /* quick default */ sv->tflag1 |= BME_BEVEL_BEVEL; ne->tflag1 = BME_BEVEL_ORIG; /* mark edge as original, even though it isn't */ diff --git a/source/blender/blenkernel/intern/bmesh_private.h b/source/blender/blenkernel/intern/bmesh_private.h index 4aa2a85b8b1..f34ef0090f3 100644 --- a/source/blender/blenkernel/intern/bmesh_private.h +++ b/source/blender/blenkernel/intern/bmesh_private.h @@ -39,11 +39,6 @@ #include "BKE_bmesh.h" -struct BME_mempool *BME_mempool_create(int esize, int tote, int pchunk); -void BME_mempool_destroy(struct BME_mempool *pool); -void *BME_mempool_alloc(struct BME_mempool *pool); -void BME_mempool_free(struct BME_mempool *pool, void *address); - /*ALLOCATION/DEALLOCATION*/ struct BME_Vert *BME_addvertlist(struct BME_Mesh *bm, struct BME_Vert *example); struct BME_Edge *BME_addedgelist(struct BME_Mesh *bm, struct BME_Vert *v1, struct BME_Vert *v2, struct BME_Edge *example); @@ -54,7 +49,6 @@ void BME_free_vert(struct BME_Mesh *bm, struct BME_Vert *v); void BME_free_edge(struct BME_Mesh *bm, struct BME_Edge *e); void BME_free_poly(struct BME_Mesh *bm, struct BME_Poly *f); void BME_free_loop(struct BME_Mesh *bm, struct BME_Loop *l); -//void BME_delete_loop(struct BME_Mesh *bm, struct BME_Loop *l); /*DOUBLE CIRCULAR LINKED LIST FUNCTIONS*/ void BME_cycle_append(void *h, void *nt); diff --git a/source/blender/blenkernel/intern/customdata.c b/source/blender/blenkernel/intern/customdata.c index 77068d8ed66..946cb449912 100644 --- a/source/blender/blenkernel/intern/customdata.c +++ b/source/blender/blenkernel/intern/customdata.c @@ -36,6 +36,7 @@ #include "BLI_blenlib.h" #include "BLI_linklist.h" +#include "BLI_mempool.h" #include "DNA_customdata_types.h" #include "DNA_listBase.h" @@ -454,13 +455,16 @@ const LayerTypeInfo LAYERTYPEINFO[CD_NUMTYPES] = { {sizeof(MStringProperty), "MStringProperty",1,"String",NULL,NULL,NULL,NULL}, {sizeof(OrigSpaceFace), "OrigSpaceFace", 1, "UVTex", layerCopy_origspace_face, NULL, layerInterp_origspace_face, layerSwap_origspace_face, layerDefault_origspace_face}, - {sizeof(float)*3, "", 0, NULL, NULL, NULL, NULL, NULL, NULL} + {sizeof(float)*3, "", 0, NULL, NULL, NULL, NULL, NULL, NULL}, + {sizeof(MTexPoly), "MTexPoly", 1, "Face Texture", NULL, NULL, NULL, NULL, NULL}, + {sizeof(MLoopUV), "MLoopUV", 1, "UV coord", NULL, NULL, NULL, NULL, NULL}, + {sizeof(MLoopCol), "MLoopCol", 1, "Col", NULL, NULL, NULL, NULL, NULL} }; const char *LAYERTYPENAMES[CD_NUMTYPES] = { "CDMVert", "CDMSticky", "CDMDeformVert", "CDMEdge", "CDMFace", "CDMTFace", "CDMCol", "CDOrigIndex", "CDNormal", "CDFlags","CDMFloatProperty", - "CDMIntProperty","CDMStringProperty", "CDOrigSpace", "CDOrco"}; + "CDMIntProperty","CDMStringProperty", "CDOrigSpace", "CDOrco", "CDMTexPoly", "CDMLoopUV", "CDMloopCol"}; const CustomDataMask CD_MASK_BAREMESH = CD_MASK_MVERT | CD_MASK_MEDGE | CD_MASK_MFACE; @@ -475,6 +479,8 @@ const CustomDataMask CD_MASK_DERIVEDMESH = CD_MASK_MSTICKY | CD_MASK_MDEFORMVERT | CD_MASK_MTFACE | CD_MASK_MCOL | CD_MASK_ORIGINDEX | CD_MASK_PROP_FLT | CD_MASK_PROP_INT | CD_MASK_PROP_STR | CD_MASK_ORIGSPACE | CD_MASK_ORCO; +const CustomDataMask CD_MASK_BMESH = + CD_MASK_MDEFORMVERT | CD_MASK_MTFACE | CD_MASK_MCOL; static const LayerTypeInfo *layerType_getInfo(int type) { @@ -1449,6 +1455,273 @@ void CustomData_from_em_block(const CustomData *source, CustomData *dest, } +/*Bmesh functions*/ +void CustomData_bmesh_free_block(CustomData *data, void **block) +{ + const LayerTypeInfo *typeInfo; + int i; + + if(!*block) return; + for(i = 0; i < data->totlayer; ++i) { + if(!(data->layers[i].flag & CD_FLAG_NOFREE)) { + typeInfo = layerType_getInfo(data->layers[i].type); + + if(typeInfo->free) { + int offset = data->layers[i].offset; + typeInfo->free((char*)*block + offset, 1, typeInfo->size); + } + } + } + + BLI_mempool_free(data->pool, *block); + *block = NULL; +} + +static void CustomData_bmesh_alloc_block(CustomData *data, void **block) +{ + + if (*block) + CustomData_bmesh_free_block(data, block); + + if (data->totsize > 0) + *block = BLI_mempool_alloc(data->pool); + else + *block = NULL; +} + +void CustomData_bmesh_copy_data(const CustomData *source, CustomData *dest, + void *src_block, void **dest_block) +{ + const LayerTypeInfo *typeInfo; + int dest_i, src_i; + + if (!*dest_block) + CustomData_bmesh_alloc_block(dest, dest_block); + + /* copies a layer at a time */ + dest_i = 0; + for(src_i = 0; src_i < source->totlayer; ++src_i) { + + /* find the first dest layer with type >= the source type + * (this should work because layers are ordered by type) + */ + while(dest_i < dest->totlayer + && dest->layers[dest_i].type < source->layers[src_i].type) + ++dest_i; + + /* if there are no more dest layers, we're done */ + if(dest_i >= dest->totlayer) return; + + /* if we found a matching layer, copy the data */ + if(dest->layers[dest_i].type == source->layers[src_i].type && + strcmp(dest->layers[dest_i].name, source->layers[src_i].name) == 0) { + char *src_data = (char*)src_block + source->layers[src_i].offset; + char *dest_data = (char*)*dest_block + dest->layers[dest_i].offset; + + typeInfo = layerType_getInfo(source->layers[src_i].type); + + if(typeInfo->copy) + typeInfo->copy(src_data, dest_data, 1); + else + memcpy(dest_data, src_data, typeInfo->size); + + /* if there are multiple source & dest layers of the same type, + * we don't want to copy all source layers to the same dest, so + * increment dest_i + */ + ++dest_i; + } + } +} + +/*Bmesh Custom Data Functions. Should replace editmesh ones with these as well, due to more effecient memory alloc*/ +void *CustomData_bmesh_get(const CustomData *data, void *block, int type) +{ + int layer_index; + + /* get the layer index of the first layer of type */ + layer_index = CustomData_get_active_layer_index(data, type); + if(layer_index < 0) return NULL; + + return (char *)block + data->layers[layer_index].offset; +} + +void *CustomData_bmesh_get_n(const CustomData *data, void *block, int type, int n) +{ + int layer_index; + + /* get the layer index of the first layer of type */ + layer_index = CustomData_get_layer_index(data, type); + if(layer_index < 0) return NULL; + + return (char *)block + data->layers[layer_index+n].offset; +} + +void CustomData_bmesh_set(CustomData *data, void *block, int type, void *source) +{ + void *dest = CustomData_bmesh_get(data, block, type); + const LayerTypeInfo *typeInfo = layerType_getInfo(type); + + if(!dest) return; + + if(typeInfo->copy) + typeInfo->copy(source, dest, 1); + else + memcpy(dest, source, typeInfo->size); +} + +void CustomData_bmesh_set_n(CustomData *data, void *block, int type, int n, void *source) +{ + void *dest = CustomData_bmesh_get_n(data, block, type, n); + const LayerTypeInfo *typeInfo = layerType_getInfo(type); + + if(!dest) return; + + if(typeInfo->copy) + typeInfo->copy(source, dest, 1); + else + memcpy(dest, source, typeInfo->size); +} + +void CustomData_bmesh_interp(CustomData *data, void **src_blocks, float *weights, + float *sub_weights, int count, void *dest_block) +{ + int i, j; + void *source_buf[SOURCE_BUF_SIZE]; + void **sources = source_buf; + + /* slow fallback in case we're interpolating a ridiculous number of + * elements + */ + if(count > SOURCE_BUF_SIZE) + sources = MEM_callocN(sizeof(*sources) * count, + "CustomData_interp sources"); + + /* interpolates a layer at a time */ + for(i = 0; i < data->totlayer; ++i) { + CustomDataLayer *layer = &data->layers[i]; + const LayerTypeInfo *typeInfo = layerType_getInfo(layer->type); + + if(typeInfo->interp) { + for(j = 0; j < count; ++j) + sources[j] = (char *)src_blocks[j] + layer->offset; + + typeInfo->interp(sources, weights, sub_weights, count, + (char *)dest_block + layer->offset); + } + } + + if(count > SOURCE_BUF_SIZE) MEM_freeN(sources); +} + +void CustomData_bmesh_set_default(CustomData *data, void **block) +{ + const LayerTypeInfo *typeInfo; + int i; + + if (!*block) + CustomData_bmesh_alloc_block(data, block); + + for(i = 0; i < data->totlayer; ++i) { + int offset = data->layers[i].offset; + + typeInfo = layerType_getInfo(data->layers[i].type); + + if(typeInfo->set_default) + typeInfo->set_default((char*)*block + offset, 1); + } +} + +void CustomData_to_bmesh_block(const CustomData *source, CustomData *dest, + int src_index, void **dest_block) +{ + const LayerTypeInfo *typeInfo; + int dest_i, src_i, src_offset; + + if (!*dest_block) + CustomData_bmesh_alloc_block(dest, dest_block); + + /* copies a layer at a time */ + dest_i = 0; + for(src_i = 0; src_i < source->totlayer; ++src_i) { + + /* find the first dest layer with type >= the source type + * (this should work because layers are ordered by type) + */ + while(dest_i < dest->totlayer + && dest->layers[dest_i].type < source->layers[src_i].type) + ++dest_i; + + /* if there are no more dest layers, we're done */ + if(dest_i >= dest->totlayer) return; + + /* if we found a matching layer, copy the data */ + if(dest->layers[dest_i].type == source->layers[src_i].type) { + int offset = dest->layers[dest_i].offset; + char *src_data = source->layers[src_i].data; + char *dest_data = (char*)*dest_block + offset; + + typeInfo = layerType_getInfo(dest->layers[dest_i].type); + src_offset = src_index * typeInfo->size; + + if(typeInfo->copy) + typeInfo->copy(src_data + src_offset, dest_data, 1); + else + memcpy(dest_data, src_data + src_offset, typeInfo->size); + + /* if there are multiple source & dest layers of the same type, + * we don't want to copy all source layers to the same dest, so + * increment dest_i + */ + ++dest_i; + } + } +} + +void CustomData_from_bmesh_block(const CustomData *source, CustomData *dest, + void *src_block, int dest_index) +{ + const LayerTypeInfo *typeInfo; + int dest_i, src_i, dest_offset; + + /* copies a layer at a time */ + dest_i = 0; + for(src_i = 0; src_i < source->totlayer; ++src_i) { + + /* find the first dest layer with type >= the source type + * (this should work because layers are ordered by type) + */ + while(dest_i < dest->totlayer + && dest->layers[dest_i].type < source->layers[src_i].type) + ++dest_i; + + /* if there are no more dest layers, we're done */ + if(dest_i >= dest->totlayer) return; + + /* if we found a matching layer, copy the data */ + if(dest->layers[dest_i].type == source->layers[src_i].type) { + int offset = source->layers[src_i].offset; + char *src_data = (char*)src_block + offset; + char *dest_data = dest->layers[dest_i].data; + + typeInfo = layerType_getInfo(dest->layers[dest_i].type); + dest_offset = dest_index * typeInfo->size; + + if(typeInfo->copy) + typeInfo->copy(src_data, dest_data + dest_offset, 1); + else + memcpy(dest_data + dest_offset, src_data, typeInfo->size); + + /* if there are multiple source & dest layers of the same type, + * we don't want to copy all source layers to the same dest, so + * increment dest_i + */ + ++dest_i; + } + } + +} + void CustomData_file_write_info(int type, char **structname, int *structnum) { const LayerTypeInfo *typeInfo = layerType_getInfo(type); diff --git a/source/blender/makesdna/DNA_customdata_types.h b/source/blender/makesdna/DNA_customdata_types.h index 73a39abac55..72557145270 100644 --- a/source/blender/makesdna/DNA_customdata_types.h +++ b/source/blender/makesdna/DNA_customdata_types.h @@ -26,6 +26,7 @@ * * ***** END GPL LICENSE BLOCK ***** */ + #ifndef DNA_CUSTOMDATA_TYPES_H #define DNA_CUSTOMDATA_TYPES_H @@ -48,6 +49,7 @@ typedef struct CustomData { CustomDataLayer *layers; /* CustomDataLayers, ordered by type */ int totlayer, maxlayer; /* number of layers, size of layers array */ int totsize, pad; /* in editmode, total size of all data layers */ + void *pool; /* for Bmesh: Memory pool for allocation of blocks*/ } CustomData; /* CustomData.type */ @@ -66,7 +68,10 @@ typedef struct CustomData { #define CD_PROP_STR 12 #define CD_ORIGSPACE 13 /* for modifier stack face location mapping */ #define CD_ORCO 14 -#define CD_NUMTYPES 15 +#define CD_MTEXPOLY 15 +#define CD_MLOOPUV 16 +#define CD_MLOOPCOL 17 +#define CD_NUMTYPES 18 /* Bits for CustomDataMask */ #define CD_MASK_MVERT (1 << CD_MVERT) @@ -84,6 +89,9 @@ typedef struct CustomData { #define CD_MASK_PROP_STR (1 << CD_PROP_STR) #define CD_MASK_ORIGSPACE (1 << CD_ORIGSPACE) #define CD_MASK_ORCO (1 << CD_ORCO) +#define CD_MASK_MTEXPOLY (1 << CD_MTEXPOLY) +#define CD_MASK_MLOOPUV (1 << CD_MLOOPUV) +#define CD_MASK_MLOOPCOL (1 << CD_MLOOPCOL) /* CustomData.flag */ diff --git a/source/blender/makesdna/DNA_meshdata_types.h b/source/blender/makesdna/DNA_meshdata_types.h index a717df640f1..6d025839ac8 100644 --- a/source/blender/makesdna/DNA_meshdata_types.h +++ b/source/blender/makesdna/DNA_meshdata_types.h @@ -69,6 +69,21 @@ typedef struct MCol { char a, r, g, b; } MCol; +/*bmesh custom data stuff*/ +typedef struct MTexPoly{ + struct Image *tpage; + char flag, transp; + short mode,tile,unwrap; +}MTexPoly; + +typedef struct MLoopUV{ + float uv[2]; +}MLoopUV; + +typedef struct MLoopCol{ + char a, r, g, b; +}MLoopCol; + typedef struct MSticky { float co[2]; } MSticky;