bmesh: fixed merge issues with navmesh (though I've not tested if it works yet).
authorJoseph Eagar <joeedh@gmail.com>
Tue, 8 Nov 2011 00:20:50 +0000 (00:20 +0000)
committerJoseph Eagar <joeedh@gmail.com>
Tue, 8 Nov 2011 00:20:50 +0000 (00:20 +0000)
also fixed a small crasher in bridge.

source/blender/blenkernel/intern/DerivedMesh.c
source/blender/blenkernel/intern/navmesh_conversion.c
source/blender/bmesh/bmesh.h
source/blender/bmesh/intern/bmesh_mesh.c
source/blender/bmesh/operators/connectops.c
source/blender/editors/include/ED_mesh.h
source/blender/editors/mesh/bmeshutils.c
source/blender/editors/mesh/mesh_navmesh.c
source/gameengine/Ketsji/KX_NavMeshObject.cpp

index 389b97557146ed57193b337ff776ee258ce47dda..a6d912712186dbf4953aa9fd24fb6a4bee52fd71 100644 (file)
@@ -2400,7 +2400,7 @@ static DerivedMesh *navmesh_dm_createNavMeshForVisualization(DerivedMesh *dm)
        int *dtrisToPolysMap=NULL, *dtrisToTrisMap=NULL, *trisToFacesMap=NULL;
        int res;
 
-       result = CDDM_copy(dm);
+       result = CDDM_copy(dm, 0);
        if (!CustomData_has_layer(&result->faceData, CD_RECAST)) {
                int *sourceRecastData = (int*)CustomData_get_layer(&dm->faceData, CD_RECAST);
                if (sourceRecastData) {
index e3b5b83964e9f540e77fa51297a744d31a02a332..53eee35de3ac92bd786c71e2ff7526f451f74284 100644 (file)
@@ -135,7 +135,7 @@ int buildRawVertIndicesData(DerivedMesh* dm, int *nverts_r, float **verts_r,
 
        //calculate number of tris
        nfaces = dm->getNumFaces(dm);
-       faces = dm->getFaceArray(dm);
+       faces = dm->getTessFaceArray(dm);
        ntris = nfaces;
        for (fi=0; fi<nfaces; fi++)
        {
index ade68f6dfbb0dbd8571b3fd6c6c475f3a08817a1..70a0820bece0e40821b09f9bd7d2c8c7dc25eb31 100644 (file)
@@ -302,6 +302,9 @@ void BM_Kill_Face_Edges(BMesh *bm, BMFace *f);
   those vertices*/
 void BM_Kill_Face_Verts(BMesh *bm, BMFace *f) ;
 
+/*clear all data in bm*/
+void BM_Clear_Mesh(BMesh *bm);
+
 /*start/stop edit*/
 void bmesh_begin_edit(struct BMesh *bm, int flag);
 void bmesh_end_edit(struct BMesh *bm, int flag);
index d77ea8b5cc50266b18c9f080f808a75f7cec6fad..cd75c301358b5e6b08cf30ca8be097302c3d3b13 100644 (file)
@@ -160,6 +160,47 @@ void BM_Free_Mesh_Data(BMesh *bm)
        BMO_ClearStack(bm);
 }
 
+void BM_Clear_Mesh(BMesh *bm)
+{
+       /*allocate the structure*/
+       int vsize, esize, lsize, fsize, lstsize;
+       /*I really need to make the allocation sizes defines, there's no reason why the API
+         should allow client code to mess around with this - joeedh*/
+       int allocsize[5] = {512, 512, 512, 2048, 512};
+       Object *ob = bm->ob;
+       
+       /*free old mesh*/
+       BM_Free_Mesh_Data(bm);
+       memset(bm, 0, sizeof(BMesh));
+       
+       /*re-initialize mesh*/
+       vsize = sizeof(BMVert);
+       esize = sizeof(BMEdge);
+       lsize = sizeof(BMLoop);
+       fsize = sizeof(BMFace);
+       lstsize = sizeof(BMLoopList);
+
+       bm->ob = ob;
+       
+   /*allocate the memory pools for the mesh elements*/
+       bm->vpool = BLI_mempool_create(vsize, allocsize[0], allocsize[0], 0, 1);
+       bm->epool = BLI_mempool_create(esize, allocsize[1], allocsize[1], 0, 1);
+       bm->lpool = BLI_mempool_create(lsize, allocsize[2], allocsize[2], 0, 0);
+       bm->looplistpool = BLI_mempool_create(lstsize, allocsize[3], allocsize[3], 0, 0);
+       bm->fpool = BLI_mempool_create(fsize, allocsize[4], allocsize[4], 0, 1);
+
+       /*allocate one flag pool that we dont get rid of.*/
+       bm->toolflagpool = BLI_mempool_create(sizeof(BMFlagLayer), 512, 512, 0, 0);
+       bm->stackdepth = 1;
+       bm->totflags = 1;
+}
+
+/*     
+ *     BMESH FREE MESH
+ *
+ *     Frees a BMesh structure.
+*/
+
 void BM_Free_Mesh(BMesh *bm)
 {
        BM_Free_Mesh_Data(bm);
index 1e7dbc3f08c68de34f4bdb3e56f77b3306933783..302d9b805b144b27cc7c1cbc4e5e7be20997b018 100644 (file)
@@ -352,7 +352,7 @@ void bmesh_bridge_loops_exec(BMesh *bm, BMOperator *op)
                if (wdir == 0) {
                        for (i=0; i<BLI_array_count(ee1); i++) {
                                j = CLAMP_INDEX((i*dir1)+starti, lenv1);
-                               if (ee1[j]->l) {
+                               if (ee1[j]->l && ee2[j]->l) {
                                        wdir = (ee2[j]->l->v == vv2[j]) ? (1) : (-1);
                                        break;
                                }
index 6aa41112491efa7be19a95d18d760d98592998b0..7680da03a90b3f157e8ecb090053d102a43e927f 100644 (file)
@@ -296,6 +296,7 @@ int ED_mesh_color_add(struct bContext *C, struct Scene *scene, struct Object *ob
 int ED_mesh_color_remove(struct bContext *C, struct Object *ob, struct Mesh *me);
 
 void EDBM_selectmode_to_scene(struct bContext *C);
+void EDBM_ClearMesh(struct BMEditMesh *em);
 
 #include "../mesh/editbmesh_bvh.h"
 
index da628d98be29b1664529c382b1cfe9792bf5b8c3..6f77cfb06bbcd6bfff74f272e02acbf7d0657551 100644 (file)
@@ -94,6 +94,29 @@ void EDBM_RecalcNormals(BMEditMesh *em)
        BM_Compute_Normals(em->bm);
 }
 
+void EDBM_ClearMesh(BMEditMesh *em)
+{
+       /*clear bmesh*/
+       BM_Clear_Mesh(em->bm);
+       
+       /*free derived meshes*/
+       if (em->derivedCage) {
+               em->derivedCage->needsFree = 1;
+               em->derivedCage->release(em->derivedCage);
+       }
+       if (em->derivedFinal && em->derivedFinal != em->derivedCage) {
+               em->derivedFinal->needsFree = 1;
+               em->derivedFinal->release(em->derivedFinal);
+       }
+       
+       em->derivedCage = em->derivedFinal = NULL;
+       
+       /*free tesselation data*/
+       em->tottri = 0;
+       if (em->looptris) 
+               MEM_freeN(em->looptris);
+}
+
 void EDBM_stats_update(BMEditMesh *em)
 {
        BMIter iter;
index 48d2c6291486460cec4d203ce1eb4eb25770deb5..9bf8b6e605b877536670e2473e16410849661554 100644 (file)
@@ -46,6 +46,7 @@
 #include "BKE_DerivedMesh.h"
 #include "BKE_cdderivedmesh.h"
 #include "BKE_report.h"
+#include "BKE_tessmesh.h"
 
 #include "BLI_editVert.h"
 #include "BLI_listbase.h"
@@ -90,11 +91,11 @@ static void createVertsTrisData(bContext *C, LinkNode* obs, int *nverts_r, float
                BLI_linklist_append(&dms, (void*)dm);
 
                nverts+= dm->getNumVerts(dm);
-               nfaces= dm->getNumFaces(dm);
+               nfaces= dm->getNumTessFaces(dm);
                ntris+= nfaces;
 
                /* resolve quad faces */
-               mface= dm->getFaceArray(dm);
+               mface= dm->getTessFaceArray(dm);
                for(i= 0; i<nfaces; i++) {
                        MFace* mf= &mface[i];
                        if(mf->v4)
@@ -129,8 +130,8 @@ static void createVertsTrisData(bContext *C, LinkNode* obs, int *nverts_r, float
                }
 
                /* create tris */
-               curnfaces= dm->getNumFaces(dm);
-               mface= dm->getFaceArray(dm);
+               curnfaces= dm->getNumTessFaces(dm);
+               mface= dm->getTessFaceArray(dm);
 
                for(i= 0; i<curnfaces; i++) {
                        MFace* mf= &mface[i];
@@ -292,7 +293,7 @@ static int buildNavMesh(const RecastData *recastParams, int nverts, float *verts
 static Object* createRepresentation(bContext *C, struct recast_polyMesh *pmesh, struct recast_polyMeshDetail *dmesh, Base* base)
 {
        float co[3], rot[3];
-       EditMesh *em;
+       BMEditMesh *em;
        int i,j, k;
        unsigned short* v;
        int face[3];
@@ -320,14 +321,11 @@ static Object* createRepresentation(bContext *C, struct recast_polyMesh *pmesh,
        }
 
        ED_object_enter_editmode(C, EM_DO_UNDO|EM_IGNORE_LAYER);
-       em= BKE_mesh_get_editmesh(((Mesh *)obedit->data));
+       em= (((Mesh *)obedit->data))->edit_btmesh;
 
        if(!createob) {
                /* clear */
-               if(em->verts.first) free_vertlist(em, &em->verts);
-               if(em->edges.first) free_edgelist(em, &em->edges);
-               if(em->faces.first) free_facelist(em, &em->faces);
-               if(em->selected.first) BLI_freelistN(&(em->selected));
+               EDBM_ClearMesh(em);
        }
 
        /* create verts for polygon mesh */
@@ -341,12 +339,12 @@ static Object* createRepresentation(bContext *C, struct recast_polyMesh *pmesh,
                co[1]= bmin[1] + v[1]*ch;
                co[2]= bmin[2] + v[2]*cs;
                SWAP(float, co[1], co[2]);
-               addvertlist(em, co, NULL);
+               BM_Make_Vert(em->bm, co, NULL);
        }
 
        /* create custom data layer to save polygon idx */
-       CustomData_add_layer_named(&em->fdata, CD_RECAST, CD_CALLOC, NULL, 0, "createRepresentation recastData");
-
+       CustomData_add_layer_named(&em->bm->pdata, CD_RECAST, CD_CALLOC, NULL, 0, "createRepresentation recastData");
+       
        /* create verts and faces for detailed mesh */
        meshes= recast_polyMeshDetailGetMeshes(dmesh, &nmeshes);
        polys= recast_polyMeshGetPolys(pmesh, NULL, &nvp);
@@ -354,7 +352,7 @@ static Object* createRepresentation(bContext *C, struct recast_polyMesh *pmesh,
        tris= recast_polyMeshDetailGetTris(dmesh, NULL);
 
        for(i= 0; i<nmeshes; i++) {
-               int uniquevbase= em->totvert;
+               int uniquevbase= em->bm->totvert;
                unsigned int vbase= meshes[4*i+0];
                unsigned short ndv= meshes[4*i+1];
                unsigned short tribase= meshes[4*i+2];
@@ -371,15 +369,15 @@ static Object* createRepresentation(bContext *C, struct recast_polyMesh *pmesh,
                for(j= nv; j<ndv; j++) {
                        copy_v3_v3(co, &dverts[3*(vbase + j)]);
                        SWAP(float, co[1], co[2]);
-                       addvertlist(em, co, NULL);
+                       BM_Make_Vert(em->bm, co, NULL);
                }
 
-               EM_init_index_arrays(em, 1, 0, 0);
+               EDBM_init_index_arrays(em, 1, 0, 0);
 
                /* create faces */
                for(j= 0; j<trinum; j++) {
                        unsigned char* tri= &tris[4*(tribase+j)];
-                       EditFace* newFace;
+                       BMFace* newFace;
                        int* polygonIdx;
 
                        for(k= 0; k<3; k++) {
@@ -388,22 +386,20 @@ static Object* createRepresentation(bContext *C, struct recast_polyMesh *pmesh,
                                else
                                        face[k]= uniquevbase+tri[k]-nv; /* unique vertex */
                        }
-                       newFace= addfacelist(em, EM_get_vert_for_index(face[0]), EM_get_vert_for_index(face[2]),
-                                                                       EM_get_vert_for_index(face[1]), NULL, NULL, NULL);
+                       newFace= BM_Make_QuadTri(em->bm, EDBM_get_vert_for_index(em, face[0]), EDBM_get_vert_for_index(em, face[2]),
+                                                                       EDBM_get_vert_for_index(em, face[1]), NULL, NULL, 0);
 
                        /* set navigation polygon idx to the custom layer */
-                       polygonIdx= (int*)CustomData_em_get(&em->fdata, newFace->data, CD_RECAST);
+                       polygonIdx= (int*)CustomData_bmesh_get(&em->bm->pdata, newFace->head.data, CD_RECAST);
                        *polygonIdx= i+1; /* add 1 to avoid zero idx */
                }
                
-               EM_free_index_arrays();
+               EDBM_free_index_arrays(em);
        }
 
        recast_destroyPolyMesh(pmesh);
        recast_destroyPolyMeshDetail(dmesh);
 
-       BKE_mesh_end_editmesh((Mesh*)obedit->data, em);
-       
        DAG_id_tag_update((ID*)obedit->data, OB_RECALC_DATA);
        WM_event_add_notifier(C, NC_GEOM|ND_DATA, obedit->data);
 
@@ -485,22 +481,23 @@ void MESH_OT_navmesh_make(wmOperatorType *ot)
 static int navmesh_face_copy_exec(bContext *C, wmOperator *op)
 {
        Object *obedit= CTX_data_edit_object(C);
-       EditMesh *em= BKE_mesh_get_editmesh((Mesh *)obedit->data);
+       BMEditMesh *em= ((Mesh *)obedit->data)->edit_btmesh;
 
        /* do work here */
-       EditFace *efa_act= EM_get_actFace(em, 0);
+       BMFace *efa_act= BM_get_actFace(em->bm, 0);
 
        if(efa_act) {
-               if(CustomData_has_layer(&em->fdata, CD_RECAST)) {
-                       EditFace *efa;
-                       int targetPolyIdx= *(int*)CustomData_em_get(&em->fdata, efa_act->data, CD_RECAST);
+               if(CustomData_has_layer(&em->bm->pdata, CD_RECAST)) {
+                       BMFace *efa;
+                       BMIter iter;
+                       int targetPolyIdx= *(int*)CustomData_bmesh_get(&em->bm->pdata, efa_act->head.data, CD_RECAST);
                        targetPolyIdx= targetPolyIdx>=0? targetPolyIdx : -targetPolyIdx;
 
                        if(targetPolyIdx > 0) {
                                /* set target poly idx to other selected faces */
-                               for (efa= (EditFace *)em->faces.first; efa; efa= efa->next) {
-                                       if((efa->f & SELECT) && efa != efa_act)  {
-                                               int* recastDataBlock= (int*)CustomData_em_get(&em->fdata, efa->data, CD_RECAST);
+                               BM_ITER(efa, &iter, em->bm, BM_FACES_OF_MESH, NULL) {
+                                       if(BM_TestHFlag(efa, BM_SELECT) && efa != efa_act)  {
+                                               int* recastDataBlock= (int*)CustomData_bmesh_get(&em->bm->pdata, efa->head.data, CD_RECAST);
                                                *recastDataBlock= targetPolyIdx;
                                        }
                                }
@@ -514,8 +511,6 @@ static int navmesh_face_copy_exec(bContext *C, wmOperator *op)
        DAG_id_tag_update((ID*)obedit->data, OB_RECALC_DATA);
        WM_event_add_notifier(C, NC_GEOM|ND_DATA, obedit->data);
 
-       BKE_mesh_end_editmesh((Mesh*)obedit->data, em);
-
        return OPERATOR_FINISHED;
 }
 
@@ -538,19 +533,20 @@ static int compare(const void * a, const void * b){
        return ( *(int*)a - *(int*)b );
 }
 
-static int findFreeNavPolyIndex(EditMesh* em)
+static int findFreeNavPolyIndex(BMEditMesh* em)
 {
        /* construct vector of indices */
-       int numfaces= em->totface;
+       int numfaces= em->bm->totface;
        int* indices= MEM_callocN(sizeof(int)*numfaces, "findFreeNavPolyIndex(indices)");
-       EditFace* ef= (EditFace*)em->faces.last;
-       int i, idx= 0, freeIdx= 1;
+       BMFace* ef;
+       BMIter iter;
+       int i, idx= em->bm->totface-1, freeIdx= 1;
 
-       while(ef) {
-               int polyIdx= *(int*)CustomData_em_get(&em->fdata, ef->data, CD_RECAST);
+       /*XXX this originally went last to first, but that isn't possible anymore*/
+       BM_ITER(ef, &iter, em->bm, BM_FACES_OF_MESH, NULL) {
+               int polyIdx= *(int*)CustomData_bmesh_get(&em->bm->pdata, ef->head.data, CD_RECAST);
                indices[idx]= polyIdx;
-               idx++;
-               ef= ef->prev;
+               idx--;
        }
 
        qsort(indices, numfaces, sizeof(int), compare);
@@ -572,21 +568,22 @@ static int findFreeNavPolyIndex(EditMesh* em)
 static int navmesh_face_add_exec(bContext *C, wmOperator *UNUSED(op))
 {
        Object *obedit= CTX_data_edit_object(C);
-       EditMesh *em= BKE_mesh_get_editmesh((Mesh *)obedit->data);
-       EditFace *ef;
-
-       if(CustomData_has_layer(&em->fdata, CD_RECAST)) {
+       BMEditMesh *em= ((Mesh *)obedit->data)->edit_btmesh;
+       BMFace *ef;
+       BMIter iter;
+       
+       if(CustomData_has_layer(&em->bm->pdata, CD_RECAST)) {
                int targetPolyIdx= findFreeNavPolyIndex(em);
 
                if(targetPolyIdx>0) {
                        /* set target poly idx to selected faces */
-                       ef= (EditFace*)em->faces.last;
-                       while(ef) {
-                               if(ef->f & SELECT) {
-                                       int *recastDataBlock= (int*)CustomData_em_get(&em->fdata, ef->data, CD_RECAST);
+                       /*XXX this originally went last to first, but that isn't possible anymore*/
+                       
+                       BM_ITER(ef, &iter, em->bm, BM_FACES_OF_MESH, NULL) {
+                               if(BM_TestHFlag(ef, BM_SELECT)) {
+                                       int *recastDataBlock= (int*)CustomData_bmesh_get(&em->bm->pdata, ef->head.data, CD_RECAST);
                                        *recastDataBlock= targetPolyIdx;
                                }
-                               ef= ef->prev;
                        }
                }
        }
@@ -594,7 +591,6 @@ static int navmesh_face_add_exec(bContext *C, wmOperator *UNUSED(op))
        DAG_id_tag_update((ID*)obedit->data, OB_RECALC_DATA);
        WM_event_add_notifier(C, NC_GEOM|ND_DATA, obedit->data);
 
-       BKE_mesh_end_editmesh((Mesh*)obedit->data, em);
        return OPERATOR_FINISHED;
 }
 
index 6765f07b34454acb0ebcbd359086e600d8fa60db..3b63c85feeac930161d48fad3156819489595aaf 100644 (file)
@@ -112,7 +112,7 @@ bool KX_NavMeshObject::BuildVertIndArrays(float *&vertices, int& nverts,
 {
        DerivedMesh* dm = mesh_create_derived_no_virtual(KX_GetActiveScene()->GetBlenderScene(), GetBlenderObject(), 
                                                                                                        NULL, CD_MASK_MESH);
-       int* recastData = (int*) dm->getFaceDataArray(dm, CD_RECAST);
+       int* recastData = (int*) dm->getTessFaceDataArray(dm, CD_RECAST);
        if (recastData)
        {
                int *dtrisToPolysMap=NULL, *dtrisToTrisMap=NULL, *trisToFacesMap=NULL;