svn merge ^/trunk/blender -r44118:44136
[blender.git] / source / blender / editors / mesh / meshtools.c
index 3caafb12f82cc04fd2a71de0eab43d24ee3f4d13..1b5011402fc9d83f360d53a0a582fdf291d99865 100644 (file)
@@ -66,6 +66,7 @@
 #include "BKE_mesh.h"
 #include "BKE_material.h"
 #include "BKE_report.h"
+#include "BKE_tessmesh.h"
 #include "BKE_multires.h"
 
 #include "BLO_sys_types.h" // for intptr_t support
 
 /* own include */
 #include "mesh_intern.h"
-
+#include "uvedit_intern.h"
 
 /* * ********************** no editmode!!! *********** */
 
 /*********************** JOIN ***************************/
 
 /* join selected meshes into the active mesh, context sensitive
-return 0 if no join is made (error) and 1 of the join is done */
+return 0 if no join is made (error) and 1 if the join is done */
 
 int join_mesh_exec(bContext *C, wmOperator *op)
 {
@@ -97,16 +98,17 @@ int join_mesh_exec(bContext *C, wmOperator *op)
        Mesh *me;
        MVert *mvert, *mv;
        MEdge *medge = NULL;
-       MFace *mface = NULL;
+       MPoly *mpoly = NULL;
+       MLoop *mloop = NULL;
        Key *key, *nkey=NULL;
        KeyBlock *kb, *okb, *kbn;
        float imat[4][4], cmat[4][4], *fp1, *fp2, curpos;
-       int a, b, totcol, totmat=0, totedge=0, totvert=0, totface=0, ok=0;
-       int vertofs, *matmap=NULL;
-       int     i, j, index, haskey=0, edgeofs, faceofs;
+       int a, b, totcol, totmat=0, totedge=0, totvert=0, ok=0;
+       int totloop=0, totpoly=0, vertofs, *matmap=NULL;
+       int i, j, index, haskey=0, edgeofs, loopofs, polyofs;
        bDeformGroup *dg, *odg;
        MDeformVert *dvert;
-       CustomData vdata, edata, fdata;
+       CustomData vdata, edata, fdata, ldata, pdata;
 
        if(scene->obedit) {
                BKE_report(op->reports, RPT_WARNING, "Cant join while in editmode");
@@ -126,7 +128,8 @@ int join_mesh_exec(bContext *C, wmOperator *op)
                        
                        totvert+= me->totvert;
                        totedge+= me->totedge;
-                       totface+= me->totface;
+                       totloop+= me->totloop;
+                       totpoly+= me->totpoly;
                        totmat+= base->object->totcol;
                        
                        if(base->object == ob)
@@ -280,14 +283,18 @@ int join_mesh_exec(bContext *C, wmOperator *op)
        memset(&vdata, 0, sizeof(vdata));
        memset(&edata, 0, sizeof(edata));
        memset(&fdata, 0, sizeof(fdata));
+       memset(&ldata, 0, sizeof(ldata));
+       memset(&pdata, 0, sizeof(pdata));
        
        mvert= CustomData_add_layer(&vdata, CD_MVERT, CD_CALLOC, NULL, totvert);
        medge= CustomData_add_layer(&edata, CD_MEDGE, CD_CALLOC, NULL, totedge);
-       mface= CustomData_add_layer(&fdata, CD_MFACE, CD_CALLOC, NULL, totface);
+       mloop= CustomData_add_layer(&ldata, CD_MLOOP, CD_CALLOC, NULL, totloop);
+       mpoly= CustomData_add_layer(&pdata, CD_MPOLY, CD_CALLOC, NULL, totpoly);
 
        vertofs= 0;
        edgeofs= 0;
-       faceofs= 0;
+       loopofs= 0;
+       polyofs= 0;
        
        /* inverse transform for all selected meshes in this object */
        invert_m4_m4(imat, ob->obmat);
@@ -327,7 +334,7 @@ int join_mesh_exec(bContext *C, wmOperator *op)
                                /* if this is the object we're merging into, no need to do anything */
                                if(base->object != ob) {
                                        /* watch this: switch matmul order really goes wrong */
-                                       mul_m4_m4m4(cmat, base->object->obmat, imat);
+                                       mult_m4_m4m4(cmat, imat, base->object->obmat);
                                        
                                        /* transform vertex coordinates into new space */
                                        for(a=0, mv=mvert; a < me->totvert; a++, mv++) {
@@ -398,7 +405,30 @@ int join_mesh_exec(bContext *C, wmOperator *op)
                                mvert+= me->totvert;
                        }
                        
-                       if(me->totface) {
+                       if(me->totedge) {
+                               CustomData_merge(&me->edata, &edata, CD_MASK_MESH, CD_DEFAULT, totedge);
+                               CustomData_copy_data(&me->edata, &edata, 0, edgeofs, me->totedge);
+                               
+                               for(a=0; a<me->totedge; a++, medge++) {
+                                       medge->v1+= vertofs;
+                                       medge->v2+= vertofs;
+                               }
+                       }
+
+                       if (me->totloop) {
+                               if(base->object!=ob)
+                                       multiresModifier_prepare_join(scene, base->object, ob);
+                               
+                               CustomData_merge(&me->ldata, &ldata, CD_MASK_MESH, CD_DEFAULT, totloop);
+                               CustomData_copy_data(&me->ldata, &ldata, 0, loopofs, me->totloop);
+                               
+                               for(a=0; a<me->totloop; a++, mloop++) {
+                                       mloop->v += vertofs;
+                                       mloop->e += edgeofs;
+                               }
+                       }
+                       
+                       if(me->totpoly) {
                                /* make mapping for materials */
                                for(a=1; a<=base->object->totcol; a++) {
                                        ma= give_current_material(base->object, a);
@@ -411,43 +441,23 @@ int join_mesh_exec(bContext *C, wmOperator *op)
                                        }
                                }
                                
-                               if(base->object!=ob)
-                                       multiresModifier_prepare_join(scene, base->object, ob);
-
-                               CustomData_merge(&me->fdata, &fdata, CD_MASK_MESH, CD_DEFAULT, totface);
-                               CustomData_copy_data(&me->fdata, &fdata, 0, faceofs, me->totface);
+                               CustomData_merge(&me->pdata, &pdata, CD_MASK_MESH, CD_DEFAULT, totpoly);
+                               CustomData_copy_data(&me->pdata, &pdata, 0, polyofs, me->totpoly);
                                
-                               for(a=0; a<me->totface; a++, mface++) {
-                                       mface->v1+= vertofs;
-                                       mface->v2+= vertofs;
-                                       mface->v3+= vertofs;
-                                       if(mface->v4) mface->v4+= vertofs;
-                                       
-                                       if (matmap)
-                                               mface->mat_nr= matmap[(int)mface->mat_nr];
-                                       else 
-                                               mface->mat_nr= 0;
+                               for(a=0; a<me->totpoly; a++, mpoly++) {
+                                       mpoly->loopstart += loopofs;
+                                       mpoly->mat_nr= matmap ? matmap[(int)mpoly->mat_nr] : 0;
                                }
                                
-                               faceofs += me->totface;
+                               polyofs += me->totpoly;
                        }
-                       
-                       if(me->totedge) {
-                               CustomData_merge(&me->edata, &edata, CD_MASK_MESH, CD_DEFAULT, totedge);
-                               CustomData_copy_data(&me->edata, &edata, 0, edgeofs, me->totedge);
-                               
-                               for(a=0; a<me->totedge; a++, medge++) {
-                                       medge->v1+= vertofs;
-                                       medge->v2+= vertofs;
-                               }
-                               
-                               edgeofs += me->totedge;
-                       }
-                       
-                       /* vertofs is used to help newly added verts be reattached to their edge/face 
-                        * (cannot be set earlier, or else reattaching goes wrong)
+
+                       /* these are used for relinking (cannot be set earlier, 
+                        * or else reattaching goes wrong)
                         */
                        vertofs += me->totvert;
+                       edgeofs += me->totedge;
+                       loopofs += me->totloop;
                        
                        /* free base, now that data is merged */
                        if(base->object != ob)
@@ -461,17 +471,20 @@ int join_mesh_exec(bContext *C, wmOperator *op)
        
        CustomData_free(&me->vdata, me->totvert);
        CustomData_free(&me->edata, me->totedge);
-       CustomData_free(&me->fdata, me->totface);
+       CustomData_free(&me->ldata, me->totloop);
+       CustomData_free(&me->pdata, me->totpoly);
 
        me->totvert= totvert;
        me->totedge= totedge;
-       me->totface= totface;
+       me->totloop= totloop;
+       me->totpoly= totpoly;
        
        me->vdata= vdata;
        me->edata= edata;
-       me->fdata= fdata;
+       me->ldata= ldata;
+       me->pdata= pdata;
 
-       mesh_update_customdata_pointers(me);
+       mesh_update_customdata_pointers(me, TRUE); /* BMESH_TODO, check if this arg can be failse, non urgent - campbell */
        
        /* old material array */
        for(a=1; a<=ob->totcol; a++) {
@@ -497,7 +510,6 @@ int join_mesh_exec(bContext *C, wmOperator *op)
                MEM_freeN(matar);
        
        ob->totcol= me->totcol= totcol;
-       ob->colbits= 0;
 
        if (matmap) MEM_freeN(matmap);
        
@@ -528,11 +540,11 @@ int join_mesh_exec(bContext *C, wmOperator *op)
        ED_object_exit_editmode(C, EM_FREEDATA|EM_WAITCURSOR|EM_DO_UNDO);
 #else
        /* toggle editmode using lower level functions so this can be called from python */
-       make_editMesh(scene, ob);
-       load_editMesh(scene, ob);
-       free_editMesh(me->edit_mesh);
-       MEM_freeN(me->edit_mesh);
-       me->edit_mesh= NULL;
+       EDBM_MakeEditBMesh(scene->toolsettings, scene, ob);
+       EDBM_LoadEditBMesh(scene, ob);
+       EDBM_FreeEditBMesh(me->edit_btmesh);
+       MEM_freeN(me->edit_btmesh);
+       me->edit_btmesh= NULL;
        DAG_id_tag_update(&ob->id, OB_RECALC_OB|OB_RECALC_DATA);
 #endif
        WM_event_add_notifier(C, NC_SCENE|ND_OB_ACTIVE, scene);
@@ -672,17 +684,17 @@ static void mesh_octree_free_node(MocNode **bt)
 
 
 /* temporal define, just to make nicer code below */
-#define MOC_ADDNODE(vx, vy, vz)        mesh_octree_add_node(basetable + ((vx)*MOC_RES*MOC_RES) + (vy)*MOC_RES + (vz), index)
+#define MOC_INDEX(vx, vy, vz)  (((vx)*MOC_RES*MOC_RES) + (vy)*MOC_RES + (vz))
 
 static void mesh_octree_add_nodes(MocNode **basetable, float *co, float *offs, float *div, intptr_t index)
 {
        float fx, fy, fz;
        int vx, vy, vz;
        
-       if (!finite(co[0]) ||
-               !finite(co[1]) ||
-               !finite(co[2])
-       {
+       if ( !finite(co[0]) ||
+            !finite(co[1]) ||
+            !finite(co[2]))
+       {
                return;
        }
        
@@ -693,33 +705,33 @@ static void mesh_octree_add_nodes(MocNode **basetable, float *co, float *offs, f
        CLAMP(fy, 0.0f, MOC_RES-MOC_THRESH);
        CLAMP(fz, 0.0f, MOC_RES-MOC_THRESH);
        
-       vx= floor(fx);
-       vy= floor(fy);
-       vz= floor(fz);
-       
-       MOC_ADDNODE(vx, vy, vz);
-       
-       if( vx>0 )
-               iffx-((float)vx)-MOC_THRESH < 0.0f)
-                       MOC_ADDNODE(vx-1, vy, vz);
-       if( vx<MOC_RES-2 )
-               iffx-((float)vx)+MOC_THRESH > 1.0f)
-                       MOC_ADDNODE(vx+1, vy, vz);
-
-       if( vy>0 )
-               if( fy-((float)vy)-MOC_THRESH < 0.0f) 
-                       MOC_ADDNODE(vx, vy-1, vz);
-       if( vy<MOC_RES-2 )
-               if( fy-((float)vy)+MOC_THRESH > 1.0f) 
-                       MOC_ADDNODE(vx, vy+1, vz);
-
-       if( vz>0 )
-               if( fz-((float)vz)-MOC_THRESH < 0.0f) 
-                       MOC_ADDNODE(vx, vy, vz-1);
-       if( vz<MOC_RES-2 )
-               if( fz-((float)vz)+MOC_THRESH > 1.0f) 
-                       MOC_ADDNODE(vx, vy, vz+1);
-       
+       vx= (int)floorf(fx);
+       vy= (int)floorf(fy);
+       vz= (int)floorf(fz);
+
+       mesh_octree_add_node(basetable + MOC_INDEX(vx, vy, vz), index);
+
+       if (vx > 0)
+               if (fx-((float)vx)-MOC_THRESH < 0.0f)
+                       mesh_octree_add_node(basetable + MOC_INDEX(vx - 1, vy, vz), index);
+       if (vx < MOC_RES - 2)
+               if (fx-((float)vx)+MOC_THRESH > 1.0f)
+                       mesh_octree_add_node(basetable + MOC_INDEX(vx + 1, vy, vz), index);
+
+       if (vy > 0)
+               if (fy-((float)vy)-MOC_THRESH < 0.0f)
+                       mesh_octree_add_node(basetable + MOC_INDEX(vx, vy - 1, vz), index);
+       if (vy < MOC_RES - 2)
+               if (fy-((float)vy)+MOC_THRESH > 1.0f)
+                       mesh_octree_add_node(basetable + MOC_INDEX(vx, vy + 1, vz), index);
+
+       if (vz > 0)
+               if (fz-((float)vz)-MOC_THRESH < 0.0f)
+                       mesh_octree_add_node(basetable + MOC_INDEX(vx, vy, vz - 1), index);
+       if (vz <MOC_RES - 2)
+               if (fz-((float)vz)+MOC_THRESH > 1.0f)
+                       mesh_octree_add_node(basetable + MOC_INDEX(vx, vy, vz + 1), index);
+
 }
 
 static intptr_t mesh_octree_find_index(MocNode **bt, MVert *mvert, float *co)
@@ -739,7 +751,7 @@ static intptr_t mesh_octree_find_index(MocNode **bt, MVert *mvert, float *co)
                                        return (*bt)->index[a]-1;
                        }
                        else {
-                               EditVert *eve= (EditVert *)((*bt)->index[a]);
+                               BMVert *eve= (BMVert *)((*bt)->index[a]);
                                if(compare_v3v3(eve->co, co, MOC_THRESH))
                                        return (*bt)->index[a];
                        }
@@ -759,14 +771,14 @@ static struct {
 
 /* mode is 's' start, or 'e' end, or 'u' use */
 /* if end, ob can be NULL */
-intptr_t mesh_octree_table(Object *ob, EditMesh *em, float *co, char mode)
+intptr_t mesh_octree_table(Object *ob, BMEditMesh *em, float *co, char mode)
 {
        MocNode **bt;
        
        if(mode=='u') {         /* use table */
                if(MeshOctree.table==NULL)
                        mesh_octree_table(ob, em, NULL, 's');
-          
+
                if(MeshOctree.table) {
                        Mesh *me= ob->data;
                        bt= MeshOctree.table + mesh_octree_get_base_offs(co, MeshOctree.offs, MeshOctree.div);
@@ -785,10 +797,11 @@ intptr_t mesh_octree_table(Object *ob, EditMesh *em, float *co, char mode)
                 * we are using the undeformed coordinates*/
                INIT_MINMAX(min, max);
 
-               if(em && me->edit_mesh==em) {
-                       EditVert *eve;
+               if(em && me->edit_btmesh==em) {
+                       BMIter iter;
+                       BMVert *eve;
                        
-                       for(eve= em->verts.first; eve; eve= eve->next)
+                       BM_ITER(eve, &iter, em->bm, BM_VERTS_OF_MESH, NULL)
                                DO_MINMAX(eve->co, min, max)
                }
                else {          
@@ -820,10 +833,11 @@ intptr_t mesh_octree_table(Object *ob, EditMesh *em, float *co, char mode)
                
                MeshOctree.table= MEM_callocN(MOC_RES*MOC_RES*MOC_RES*sizeof(void *), "sym table");
                
-               if(em && me->edit_mesh==em) {
-                       EditVert *eve;
+               if(em && me->edit_btmesh==em) {
+                       BMVert *eve;
+                       BMIter iter;
 
-                       for(eve= em->verts.first; eve; eve= eve->next) {
+                       BM_ITER(eve, &iter, em->bm, BM_VERTS_OF_MESH, NULL) {
                                mesh_octree_add_nodes(MeshOctree.table, eve->co, MeshOctree.offs, MeshOctree.div, (intptr_t)(eve));
                        }
                }
@@ -849,212 +863,23 @@ intptr_t mesh_octree_table(Object *ob, EditMesh *em, float *co, char mode)
        return 0;
 }
 
-
-/* ********************* MESH VERTEX MIRR TOPO LOOKUP *************** */
-
-#define MIRRHASH_TYPE int
-
-typedef struct MirrTopoPair {
-       long hash;
-       int vIndex;
-} MirrTopoPair;
-
-static int MirrTopo_long_sort(const void *l1, const void *l2)
-{
-       if(                     (MIRRHASH_TYPE)(intptr_t)l1 > (MIRRHASH_TYPE)(intptr_t)l2 ) return  1;
-       else if(        (MIRRHASH_TYPE)(intptr_t)l1 < (MIRRHASH_TYPE)(intptr_t)l2 ) return -1;
-       return 0;
-}
-
-static int MirrTopo_item_sort(const void *v1, const void *v2)
-{
-       if(                     ((MirrTopoPair *)v1)->hash > ((MirrTopoPair *)v2)->hash ) return  1;
-       else if(        ((MirrTopoPair *)v1)->hash < ((MirrTopoPair *)v2)->hash ) return -1;
-       return 0;
-}
-
-static long *mesh_topo_lookup = NULL;
-static int  mesh_topo_lookup_tot = -1;
-static int  mesh_topo_lookup_mode = -1;
+MirrTopoStore_t mesh_topo_store= {NULL, -1. -1, -1};
 
 /* mode is 's' start, or 'e' end, or 'u' use */
 /* if end, ob can be NULL */
-long mesh_mirrtopo_table(Object *ob, char mode)
+/* note, is supposed return -1 on error, which callers are currently checking for, but is not used so far */
+int mesh_mirrtopo_table(Object *ob, char mode)
 {
        if(mode=='u') {         /* use table */
-               Mesh *me= ob->data;
-               if(     (mesh_topo_lookup==NULL) ||
-                       (mesh_topo_lookup_mode != ob->mode) ||
-                       (me->edit_mesh && me->edit_mesh->totvert != mesh_topo_lookup_tot) ||
-                       (me->edit_mesh==NULL && me->totvert != mesh_topo_lookup_tot)
-               ) {
+               if (ED_mesh_mirrtopo_recalc_check(ob->data, ob->mode, &mesh_topo_store)) {
                        mesh_mirrtopo_table(ob, 's');
                }
-       } else if(mode=='s') { /* start table */
-               Mesh *me= ob->data;
-               MEdge *medge;
-               EditMesh *em= me->edit_mesh;
-               void **eve_tmp_back= NULL; /* some of the callers are using eve->tmp so restore after */
-
-
-               /* editmode*/
-               EditEdge *eed;
-
-               int a, last, totvert;
-               int totUnique= -1, totUniqueOld= -1;
-
-               MIRRHASH_TYPE *MirrTopoHash = NULL;
-               MIRRHASH_TYPE *MirrTopoHash_Prev = NULL;
-               MirrTopoPair *MirrTopoPairs;
-               mesh_topo_lookup_mode= ob->mode;
-
-               /* reallocate if needed */
-               if (mesh_topo_lookup) {
-                       MEM_freeN(mesh_topo_lookup);
-                       mesh_topo_lookup = NULL;
-               }
-
-               if(em) {
-                       EditVert *eve;
-                       totvert= 0;
-                       eve_tmp_back=  MEM_callocN( em->totvert * sizeof(void *), "TopoMirr" );
-                       for(eve= em->verts.first; eve; eve= eve->next) {
-                               eve_tmp_back[totvert]= eve->tmp.p;
-                               eve->tmp.l = totvert++;
-                       }
-               }
-               else {
-                       totvert = me->totvert;
-               }
-
-               MirrTopoHash = MEM_callocN( totvert * sizeof(MIRRHASH_TYPE), "TopoMirr" );
-
-               /* Initialize the vert-edge-user counts used to detect unique topology */
-               if(em) {
-                       for(eed=em->edges.first; eed; eed= eed->next) {
-                               MirrTopoHash[eed->v1->tmp.l]++;
-                               MirrTopoHash[eed->v2->tmp.l]++;
-                       }
-               } else {
-                       for(a=0, medge=me->medge; a<me->totedge; a++, medge++) {
-                               MirrTopoHash[medge->v1]++;
-                               MirrTopoHash[medge->v2]++;
-                       }
-               }
-
-               MirrTopoHash_Prev = MEM_dupallocN( MirrTopoHash );
-
-               totUniqueOld = -1;
-               while(1) {
-                       /* use the number of edges per vert to give verts unique topology IDs */
-
-                       if(em) {
-                               for(eed=em->edges.first; eed; eed= eed->next) {
-                                       MirrTopoHash[eed->v1->tmp.l] += MirrTopoHash_Prev[eed->v2->tmp.l];
-                                       MirrTopoHash[eed->v2->tmp.l] += MirrTopoHash_Prev[eed->v1->tmp.l];
-                               }
-                       } else {
-                               for(a=0, medge=me->medge; a<me->totedge; a++, medge++) {
-                                       /* This can make really big numbers, wrapping around here is fine */
-                                       MirrTopoHash[medge->v1] += MirrTopoHash_Prev[medge->v2];
-                                       MirrTopoHash[medge->v2] += MirrTopoHash_Prev[medge->v1];
-                               }
-                       }
-                       memcpy(MirrTopoHash_Prev, MirrTopoHash, sizeof(MIRRHASH_TYPE) * totvert);
-
-                       /* sort so we can count unique values */
-                       qsort(MirrTopoHash_Prev, totvert, sizeof(MIRRHASH_TYPE), MirrTopo_long_sort);
-
-                       totUnique = 1; /* account for skiping the first value */
-                       for(a=1; a<totvert; a++) {
-                               if (MirrTopoHash_Prev[a-1] != MirrTopoHash_Prev[a]) {
-                                       totUnique++;
-                               }
-                       }
-
-                       if (totUnique <= totUniqueOld) {
-                               /* Finish searching for unique valus when 1 loop dosnt give a
-                                * higher number of unique values compared to the previous loop */
-                               break;
-                       } else {
-                               totUniqueOld = totUnique;
-                       }
-                       /* Copy the hash calculated this iter, so we can use them next time */
-                       memcpy(MirrTopoHash_Prev, MirrTopoHash, sizeof(MIRRHASH_TYPE) * totvert);
-               }
-
-               /* restore eve->tmp.* */
-               if(eve_tmp_back) {
-                       EditVert *eve;
-                       totvert= 0;
-                       for(eve= em->verts.first; eve; eve= eve->next) {
-                               eve->tmp.p= eve_tmp_back[totvert++];
-                       }
-
-                       MEM_freeN(eve_tmp_back);
-                       eve_tmp_back= NULL;
-               }
-               
-               
-               /* Hash/Index pairs are needed for sorting to find index pairs */
-               MirrTopoPairs= MEM_callocN( sizeof(MirrTopoPair) * totvert, "MirrTopoPairs");
-
-               /* since we are looping through verts, initialize these values here too */
-               mesh_topo_lookup = MEM_mallocN( totvert * sizeof(long), "mesh_topo_lookup" );
-
-               if(em) {
-                       EM_init_index_arrays(em,1,0,0);
-               }
-
-
-               for(a=0; a<totvert; a++) {
-                       MirrTopoPairs[a].hash= MirrTopoHash[a];
-                       MirrTopoPairs[a].vIndex = a;
-
-                       /* initialize lookup */
-                       mesh_topo_lookup[a] = -1;
-               }
-
-               qsort(MirrTopoPairs, totvert, sizeof(MirrTopoPair), MirrTopo_item_sort);
-
-               /* Since the loop starts at 2, we must define the last index where the hash's differ */
-               last = ((totvert >= 2) && (MirrTopoPairs[0].hash == MirrTopoPairs[1].hash)) ? 0 : 1;
-
-               /* Get the pairs out of the sorted hashes, note, totvert+1 means we can use the previous 2,
-                * but you cant ever access the last 'a' index of MirrTopoPairs */
-               for(a=2; a < totvert+1; a++) {
-                       /* printf("I %d %ld %d\n", (a-last), MirrTopoPairs[a  ].hash, MirrTopoPairs[a  ].vIndex ); */
-                       if ((a==totvert) || (MirrTopoPairs[a-1].hash != MirrTopoPairs[a].hash)) {
-                               if (a-last==2) {
-                                       if(em) {
-                                               mesh_topo_lookup[MirrTopoPairs[a-1].vIndex] =   (long)EM_get_vert_for_index(MirrTopoPairs[a-2].vIndex);
-                                               mesh_topo_lookup[MirrTopoPairs[a-2].vIndex] =   (long)EM_get_vert_for_index(MirrTopoPairs[a-1].vIndex);
-                                       } else {
-                                               mesh_topo_lookup[MirrTopoPairs[a-1].vIndex] =   MirrTopoPairs[a-2].vIndex;
-                                               mesh_topo_lookup[MirrTopoPairs[a-2].vIndex] =   MirrTopoPairs[a-1].vIndex;
-                                       }
-                               }
-                               last= a;
-                       }
-               }
-               if(em) {
-                       EM_free_index_arrays();
-               }
-
-               MEM_freeN( MirrTopoPairs );
-               MirrTopoPairs = NULL;
-
-               MEM_freeN( MirrTopoHash );
-               MEM_freeN( MirrTopoHash_Prev );
-
-               mesh_topo_lookup_tot = totvert;
-
-       } else if(mode=='e') { /* end table */
-               if (mesh_topo_lookup) {
-                       MEM_freeN(mesh_topo_lookup);
-               }
-               mesh_topo_lookup = NULL;
-               mesh_topo_lookup_tot= -1;
+       }
+       else if(mode=='s') { /* start table */
+               ED_mesh_mirrtopo_init(ob->data, ob->mode, &mesh_topo_store, FALSE);
+       }
+       else if(mode=='e') { /* end table */
+               ED_mesh_mirrtopo_free(&mesh_topo_store);
        }
        return 0;
 }
@@ -1078,7 +903,7 @@ static int mesh_get_x_mirror_vert_topo(Object *ob, int index)
        if (mesh_mirrtopo_table(ob, 'u')==-1)
                return -1;
 
-       return mesh_topo_lookup[index];
+       return mesh_topo_store.index_lookup[index];
 }
 
 int mesh_get_x_mirror_vert(Object *ob, int index)
@@ -1088,9 +913,10 @@ int mesh_get_x_mirror_vert(Object *ob, int index)
        } else {
                return mesh_get_x_mirror_vert_spacial(ob, index);
        }
+       return 0;
 }
 
-static EditVert *editmesh_get_x_mirror_vert_spacial(Object *ob, EditMesh *em, float *co)
+static BMVert *editbmesh_get_x_mirror_vert_spacial(Object *ob, BMEditMesh *em, float *co)
 {
        float vec[3];
        intptr_t poinval;
@@ -1108,43 +934,52 @@ static EditVert *editmesh_get_x_mirror_vert_spacial(Object *ob, EditMesh *em, fl
        
        poinval= mesh_octree_table(ob, em, vec, 'u');
        if(poinval != -1)
-               return (EditVert *)(poinval);
+               return (BMVert *)(poinval);
        return NULL;
 }
 
-static EditVert *editmesh_get_x_mirror_vert_topo(Object *ob, struct EditMesh *em, EditVert *eve, int index)
+static BMVert *editbmesh_get_x_mirror_vert_topo(Object *ob, struct BMEditMesh *em, BMVert *eve, int index)
 {
-       long poinval;
+       intptr_t poinval;
        if (mesh_mirrtopo_table(ob, 'u')==-1)
                return NULL;
 
        if (index == -1) {
-               index = BLI_findindex(&em->verts, eve);
-
-               if (index == -1) {
+               BMIter iter;
+               BMVert *v;
+               
+               index = 0;
+               BM_ITER(v, &iter, em->bm, BM_VERTS_OF_MESH, NULL) {
+                       if (v == eve)
+                               break;
+                       index++;
+               }
+               
+               if (index == em->bm->totvert) {
                        return NULL;
                }
        }
 
-       poinval= mesh_topo_lookup[ index ];
+       poinval= mesh_topo_store.index_lookup[index];
 
        if(poinval != -1)
-               return (EditVert *)(poinval);
+               return (BMVert *)(poinval);
        return NULL;
 }      
 
-EditVert *editmesh_get_x_mirror_vert(Object *ob, struct EditMesh *em, EditVert *eve, float *co, int index)
+BMVert *editbmesh_get_x_mirror_vert(Object *ob, struct BMEditMesh *em, BMVert *eve, float *co, int index)
 {
        if (((Mesh *)ob->data)->editflag & ME_EDIT_MIRROR_TOPO) {
-               return editmesh_get_x_mirror_vert_topo(ob, em, eve, index);
-       } else {
-               return editmesh_get_x_mirror_vert_spacial(ob, em, co);
+               return editbmesh_get_x_mirror_vert_topo(ob, em, eve, index);
+       }
+       else {
+               return editbmesh_get_x_mirror_vert_spacial(ob, em, co);
        }
 }
 
-
 #if 0
-float *editmesh_get_mirror_uv(int axis, float *uv, float *mirrCent, float *face_cent)
+
+static float *editmesh_get_mirror_uv(BMEditMesh *em, int axis, float *uv, float *mirrCent, float *face_cent)
 {
        float vec[2];
        float cent_vec[2];
@@ -1172,25 +1007,30 @@ float *editmesh_get_mirror_uv(int axis, float *uv, float *mirrCent, float *face_
 
        /* TODO - Optimize */
        {
-               EditFace *efa;
-               int i, len;
-               for(efa=em->faces.first; efa; efa=efa->next) {
-                       MTFace *tf= (MTFace *)CustomData_em_get(&em->fdata, efa->data, CD_MTFACE);
-                       uv_center(tf->uv, cent, (void *)efa->v4);
-
+               BMIter iter;
+               BMFace *efa;
+               
+               BM_ITER(efa, &iter, em->bm, BM_FACES_OF_MESH, NULL) {
+                       poly_uv_center(em, efa, cent);
+                       
                        if ( (fabs(cent[0] - cent_vec[0]) < 0.001) && (fabs(cent[1] - cent_vec[1]) < 0.001) ) {
-                               len = efa->v4 ? 4 : 3;
-                               for (i=0; i<len; i++) {
-                                       if ( (fabs(tf->uv[i][0] - vec[0]) < 0.001) && (fabs(tf->uv[i][1] - vec[1]) < 0.001) ) {
-                                               return tf->uv[i];
-                                       }
+                               BMIter liter;
+                               BMLoop *l;
+                               
+                               BM_ITER(l, &liter, em->bm, BM_LOOPS_OF_FACE, efa) {
+                                       MLoopUV *luv = CustomData_bmesh_get(&em->bm->ldata, l->head.data, CD_MLOOPUV);
+                                       if ( (fabs(luv->uv[0] - vec[0]) < 0.001) && (fabs(luv->uv[1] - vec[1]) < 0.001) ) {
+                                               return luv->uv;
+                                                               
                                }
                        }
                }
        }
+       }
 
        return NULL;
 }
+
 #endif
 
 static unsigned int mirror_facehash(const void *ptr)
@@ -1239,7 +1079,8 @@ static int mirror_facecmp(const void *a, const void *b)
        return (mirror_facerotation((MFace*)a, (MFace*)b) == -1);
 }
 
-int *mesh_get_x_mirror_faces(Object *ob, EditMesh *em)
+/* BMESH_TODO, convert to MPoly (functions above also) */
+int *mesh_get_x_mirror_faces(Object *ob, BMEditMesh *em)
 {
        Mesh *me= ob->data;
        MVert *mv, *mvert= me->mvert;
@@ -1270,8 +1111,8 @@ int *mesh_get_x_mirror_faces(Object *ob, EditMesh *em)
 
                /* make sure v4 is not 0 if a quad */
                if(mf->v4 && mirrormf.v4==0) {
-                       SWAP(int, mirrormf.v1, mirrormf.v3);
-                       SWAP(int, mirrormf.v2, mirrormf.v4);
+                       SWAP(unsigned int, mirrormf.v1, mirrormf.v3);
+                       SWAP(unsigned int, mirrormf.v2, mirrormf.v4);
                }
 
                hashmf= BLI_ghash_lookup(fhash, &mirrormf);