style cleanup / comment formatting for bli/bke/bmesh
[blender.git] / source / blender / blenkernel / intern / editderivedmesh.c
index 89339b2df060c181ccedc54360d6e7c6d4616a2c..0080367d9d0ed3e1cdcbbdafdc7a7e8a71adef69 100644 (file)
@@ -38,7 +38,6 @@
 #include "BLI_utildefines.h"
 #include "BLI_blenlib.h"
 #include "BLI_edgehash.h"
 #include "BLI_utildefines.h"
 #include "BLI_blenlib.h"
 #include "BLI_edgehash.h"
-#include "BLI_editVert.h"
 #include "BLI_math.h"
 #include "BLI_pbvh.h"
 
 #include "BLI_math.h"
 #include "BLI_pbvh.h"
 
@@ -48,9 +47,9 @@
 #include "BKE_paint.h"
 
 
 #include "BKE_paint.h"
 
 
+#include "DNA_mesh_types.h"
 #include "DNA_meshdata_types.h"
 #include "DNA_object_types.h"
 #include "DNA_meshdata_types.h"
 #include "DNA_object_types.h"
-#include "DNA_curve_types.h" /* for Curve */
 
 #include "MEM_guardedalloc.h"
 
 
 #include "MEM_guardedalloc.h"
 
 extern GLubyte stipple_quarttone[128]; /* glutil.c, bad level data */
 
 
 extern GLubyte stipple_quarttone[128]; /* glutil.c, bad level data */
 
 
-BMEditMesh *BMEdit_Create(BMesh *bm)
+BMEditMesh *BMEdit_Create(BMesh *bm, int do_tesselate)
 {
        BMEditMesh *tm = MEM_callocN(sizeof(BMEditMesh), __func__);
 
        tm->bm = bm;
 {
        BMEditMesh *tm = MEM_callocN(sizeof(BMEditMesh), __func__);
 
        tm->bm = bm;
-
-       BMEdit_RecalcTesselation(tm);
+       if (do_tesselate) {
+               BMEdit_RecalcTessellation(tm);
+       }
 
        return tm;
 }
 
        return tm;
 }
@@ -88,15 +88,15 @@ BMEditMesh *BMEdit_Copy(BMEditMesh *tm)
 
        tm2->derivedCage = tm2->derivedFinal = NULL;
 
 
        tm2->derivedCage = tm2->derivedFinal = NULL;
 
-       tm2->bm = BM_Copy_Mesh(tm->bm);
+       tm2->bm = BM_mesh_copy(tm->bm);
 
 
-       /*The tesselation is NOT calculated on the copy here,
-         because currently all the callers of this function use
-         it to make a backup copy of the BMEditMesh to restore
-         it in the case of errors in an operation. For perf
-         reasons, in that case it makes more sense to do the
-         tesselation only when/if that copy ends up getting
-         used.*/
+       /* The tessellation is NOT calculated on the copy here,
+        * because currently all the callers of this function use
+        * it to make a backup copy of the BMEditMesh to restore
+        * it in the case of errors in an operation. For perf
+        * reasons, in that case it makes more sense to do the
+        * tessellation only when/if that copy ends up getting
+        * used.*/
        tm2->looptris = NULL;
 
        tm2->vert_index = NULL;
        tm2->looptris = NULL;
 
        tm2->vert_index = NULL;
@@ -106,7 +106,7 @@ BMEditMesh *BMEdit_Copy(BMEditMesh *tm)
        return tm2;
 }
 
        return tm2;
 }
 
-static void BMEdit_RecalcTesselation_intern(BMEditMesh *tm)
+static void BMEdit_RecalcTessellation_intern(BMEditMesh *tm)
 {
        /* use this to avoid locking pthread for _every_ polygon
         * and calling the fill function */
 {
        /* use this to avoid locking pthread for _every_ polygon
         * and calling the fill function */
@@ -120,10 +120,36 @@ static void BMEdit_RecalcTesselation_intern(BMEditMesh *tm)
        BMLoop *l;
        int i = 0, j;
 
        BMLoop *l;
        int i = 0, j;
 
+#if 0
+       /* note, we could be clever and re-use this array but would need to ensure
+        * its realloced at some point, for now just free it */
        if (tm->looptris) MEM_freeN(tm->looptris);
 
        if (tm->looptris) MEM_freeN(tm->looptris);
 
-       f = BMIter_New(&iter, bm, BM_FACES_OF_MESH, NULL);
-       for ( ; f; f=BMIter_Step(&iter)) {
+       /* Use tm->tottri when set, this means no reallocs while transforming,
+        * (unless scanfill fails), otherwise... */
+       /* allocate the length of totfaces, avoid many small reallocs,
+        * if all faces are tri's it will be correct, quads == 2x allocs */
+       BLI_array_reserve(looptris, (tm->tottri && tm->tottri < bm->totface * 3) ? tm->tottri : bm->totface);
+#else
+
+       /* this means no reallocs for quad dominant models, for */
+       if ( (tm->looptris != NULL) &&
+            (tm->tottri != 0) &&
+            /* (totrti <= bm->totface * 2) would be fine for all quads,
+                 * but incase there are some ngons, still re-use the array */
+            (tm->tottri <= bm->totface * 3))
+       {
+               looptris = tm->looptris;
+       }
+       else {
+               if (tm->looptris) MEM_freeN(tm->looptris);
+               BLI_array_reserve(looptris, bm->totface);
+       }
+
+#endif
+
+       f = BM_iter_new(&iter, bm, BM_FACES_OF_MESH, NULL);
+       for ( ; f; f=BM_iter_step(&iter)) {
                /*don't consider two-edged faces*/
                if (f->len < 3) {
                        /* do nothing */
                /*don't consider two-edged faces*/
                if (f->len < 3) {
                        /* do nothing */
@@ -135,8 +161,8 @@ static void BMEdit_RecalcTesselation_intern(BMEditMesh *tm)
 
                else if (f->len == 3) {
                        BLI_array_growone(looptris);
 
                else if (f->len == 3) {
                        BLI_array_growone(looptris);
-                       l = BMIter_New(&liter, bm, BM_LOOPS_OF_FACE, f);
-                       for (j=0; l; l=BMIter_Step(&liter), j++) {
+                       l = BM_iter_new(&liter, bm, BM_LOOPS_OF_FACE, f);
+                       for (j=0; l; l=BM_iter_step(&liter), j++) {
                                looptris[i][j] = l;
                        }
                        i += 1;
                                looptris[i][j] = l;
                        }
                        i += 1;
@@ -145,8 +171,8 @@ static void BMEdit_RecalcTesselation_intern(BMEditMesh *tm)
                        BMLoop *ltmp[4];
                        BLI_array_growitems(looptris, 2);
 
                        BMLoop *ltmp[4];
                        BLI_array_growitems(looptris, 2);
 
-                       l = BMIter_New(&liter, bm, BM_LOOPS_OF_FACE, f);
-                       for (j=0; l; l=BMIter_Step(&liter), j++) {
+                       l = BM_iter_new(&liter, bm, BM_LOOPS_OF_FACE, f);
+                       for (j=0; l; l=BM_iter_step(&liter), j++) {
                                ltmp[j] = l;
                        }
 
                                ltmp[j] = l;
                        }
 
@@ -164,16 +190,17 @@ static void BMEdit_RecalcTesselation_intern(BMEditMesh *tm)
 #endif /* USE_TESSFACE_SPEEDUP */
 
                else {
 #endif /* USE_TESSFACE_SPEEDUP */
 
                else {
-                       EditVert *v, *lastv=NULL, *firstv=NULL;
-                       EditEdge *e;
-                       EditFace *efa;
+                       ScanFillVert *v, *lastv=NULL, *firstv=NULL;
+                       ScanFillEdge *e;
+                       ScanFillFace *efa;
+                       int totfilltri;
 
                        BLI_begin_edgefill();
                        /*scanfill time*/
 
                        BLI_begin_edgefill();
                        /*scanfill time*/
-                       l = BMIter_New(&liter, bm, BM_LOOPS_OF_FACE, f);
-                       for (j=0; l; l=BMIter_Step(&liter), j++) {
+                       l = BM_iter_new(&liter, bm, BM_LOOPS_OF_FACE, f);
+                       for (j=0; l; l=BM_iter_step(&liter), j++) {
                                /*mark order*/
                                /*mark order*/
-                               BM_SetIndex(l, j); /* set_loop */
+                               BM_elem_index_set(l, j); /* set_loop */
 
                                v = BLI_addfillvert(l->v->co);
                                v->tmp.p = l;
 
                                v = BLI_addfillvert(l->v->co);
                                v->tmp.p = l;
@@ -189,18 +216,17 @@ static void BMEdit_RecalcTesselation_intern(BMEditMesh *tm)
                        /*complete the loop*/
                        BLI_addfilledge(firstv, v);
 
                        /*complete the loop*/
                        BLI_addfilledge(firstv, v);
 
-                       BLI_edgefill(2);
+                       totfilltri = BLI_edgefill(2);
+                       BLI_array_growitems(looptris, totfilltri);
 
                        for (efa = fillfacebase.first; efa; efa=efa->next) {
                                BMLoop *l1= efa->v1->tmp.p;
                                BMLoop *l2= efa->v2->tmp.p;
                                BMLoop *l3= efa->v3->tmp.p;
 
 
                        for (efa = fillfacebase.first; efa; efa=efa->next) {
                                BMLoop *l1= efa->v1->tmp.p;
                                BMLoop *l2= efa->v2->tmp.p;
                                BMLoop *l3= efa->v3->tmp.p;
 
-                               BLI_array_growone(looptris);
-
-                               if (BM_GetIndex(l1) > BM_GetIndex(l2)) { SWAP(BMLoop*, l1, l2); }
-                               if (BM_GetIndex(l2) > BM_GetIndex(l3)) { SWAP(BMLoop*, l2, l3); }
-                               if (BM_GetIndex(l1) > BM_GetIndex(l2)) { SWAP(BMLoop*, l1, l2); }
+                               if (BM_elem_index_get(l1) > BM_elem_index_get(l2)) { SWAP(BMLoop*, l1, l2); }
+                               if (BM_elem_index_get(l2) > BM_elem_index_get(l3)) { SWAP(BMLoop*, l2, l3); }
+                               if (BM_elem_index_get(l1) > BM_elem_index_get(l2)) { SWAP(BMLoop*, l1, l2); }
 
                                looptris[i][0] = l1;
                                looptris[i][1] = l2;
 
                                looptris[i][0] = l1;
                                looptris[i][1] = l2;
@@ -219,21 +245,21 @@ static void BMEdit_RecalcTesselation_intern(BMEditMesh *tm)
 
 }
 
 
 }
 
-void BMEdit_RecalcTesselation(BMEditMesh *em)
+void BMEdit_RecalcTessellation(BMEditMesh *em)
 {
 {
-       BMEdit_RecalcTesselation_intern(em);
+       BMEdit_RecalcTessellation_intern(em);
 
        /* commented because editbmesh_build_data() ensures we get tessfaces */
 #if 0
        if (em->derivedFinal && em->derivedFinal == em->derivedCage) {
 
        /* commented because editbmesh_build_data() ensures we get tessfaces */
 #if 0
        if (em->derivedFinal && em->derivedFinal == em->derivedCage) {
-               if (em->derivedFinal->recalcTesselation)
-                       em->derivedFinal->recalcTesselation(em->derivedFinal);
+               if (em->derivedFinal->recalcTessellation)
+                       em->derivedFinal->recalcTessellation(em->derivedFinal);
        }
        else if (em->derivedFinal) {
        }
        else if (em->derivedFinal) {
-               if (em->derivedCage->recalcTesselation)
-                       em->derivedCage->recalcTesselation(em->derivedCage);
-               if (em->derivedFinal->recalcTesselation)
-                       em->derivedFinal->recalcTesselation(em->derivedFinal);
+               if (em->derivedCage->recalcTessellation)
+                       em->derivedCage->recalcTessellation(em->derivedCage);
+               if (em->derivedFinal->recalcTessellation)
+                       em->derivedFinal->recalcTessellation(em->derivedFinal);
        }
 #endif
 }
        }
 #endif
 }
@@ -281,22 +307,22 @@ void BMEdit_Free(BMEditMesh *em)
        if (em->face_index) MEM_freeN(em->face_index);
 
        if (em->bm)
        if (em->face_index) MEM_freeN(em->face_index);
 
        if (em->bm)
-               BM_Free_Mesh(em->bm);
+               BM_mesh_free(em->bm);
 }
 
 /*
 }
 
 /*
-ok, basic design:
-
-the bmesh derivedmesh exposes the mesh as triangles.  it stores pointers
-to three loops per triangle.  the derivedmesh stores a cache of tesselations
-for each face.  this cache will smartly update as needed (though at first
-it'll simply be more brute force).  keeping track of face/edge counts may
-be a small problbm.
-
-this won't be the most efficient thing, considering that internal edges and
-faces of tesselations are exposed.  looking up an edge by index in particular
-is likely to be a little slow.
-*/
+ * ok, basic design:
+ *
+ * the bmesh derivedmesh exposes the mesh as triangles.  it stores pointers
+ * to three loops per triangle.  the derivedmesh stores a cache of tessellations
+ * for each face.  this cache will smartly update as needed (though at first
+ * it'll simply be more brute force).  keeping track of face/edge counts may
+ * be a small problbm.
+ *
+ * this won't be the most efficient thing, considering that internal edges and
+ * faces of tessellations are exposed.  looking up an edge by index in particular
+ * is likely to be a little slow.
+ */
 
 typedef struct EditDerivedBMesh {
        DerivedMesh dm;
 
 typedef struct EditDerivedBMesh {
        DerivedMesh dm;
@@ -306,87 +332,22 @@ typedef struct EditDerivedBMesh {
 
        float (*vertexCos)[3];
        float (*vertexNos)[3];
 
        float (*vertexCos)[3];
        float (*vertexNos)[3];
-       float (*faceNos)[3];
-
-       /*lookup caches; these are rebuilt on dm->RecalcTesselation()
-         (or when the derivedmesh is created, of course)*/
-       GHash *vhash, *ehash, *fhash;
-       BMVert **vtable;
-       BMEdge **etable;
-       BMFace **ftable;
+       float (*polyNos)[3];
 
 
-       /*private variables, for number of verts/edges/faces
-         within the above hash/table members*/
+       /* private variables, for number of verts/edges/faces
+        * within the above hash/table members*/
        int tv, te, tf;
 } EditDerivedBMesh;
 
        int tv, te, tf;
 } EditDerivedBMesh;
 
-static void bmdm_recalc_lookups(EditDerivedBMesh *bmdm)
-{
-       BMIter iter;
-       BMHeader *h;
-       int a, i, iters[3] = {BM_VERTS_OF_MESH, BM_EDGES_OF_MESH, BM_FACES_OF_MESH};
-
-       bmdm->tv = bmdm->tc->bm->totvert;
-       bmdm->te = bmdm->tc->bm->totedge;
-       bmdm->tf = bmdm->tc->bm->totface;
-
-       if (bmdm->vhash) BLI_ghash_free(bmdm->vhash, NULL, NULL);
-       if (bmdm->ehash) BLI_ghash_free(bmdm->ehash, NULL, NULL);
-       if (bmdm->fhash) BLI_ghash_free(bmdm->fhash, NULL, NULL);
-
-       bmdm->vhash = BLI_ghash_new(BLI_ghashutil_ptrhash, BLI_ghashutil_ptrcmp, "bmesh derived");
-       bmdm->ehash = BLI_ghash_new(BLI_ghashutil_ptrhash, BLI_ghashutil_ptrcmp, "bmesh derived");
-       bmdm->fhash = BLI_ghash_new(BLI_ghashutil_ptrhash, BLI_ghashutil_ptrcmp, "bmesh derived");
-
-       if (bmdm->vtable) MEM_freeN(bmdm->vtable);
-       if (bmdm->etable) MEM_freeN(bmdm->etable);
-       if (bmdm->ftable) MEM_freeN(bmdm->ftable);
-
-       if (bmdm->tc->bm->totvert)
-               bmdm->vtable = MEM_mallocN(sizeof(void**)*bmdm->tc->bm->totvert, "bmdm->vtable");
-       else bmdm->vtable = NULL;
-
-       if (bmdm->tc->bm->totedge)
-               bmdm->etable = MEM_mallocN(sizeof(void**)*bmdm->tc->bm->totedge, "bmdm->etable");
-       else bmdm->etable = NULL;
-
-       if (bmdm->tc->bm->totface)
-               bmdm->ftable = MEM_mallocN(sizeof(void**)*bmdm->tc->bm->totface, "bmdm->ftable");
-       else bmdm->ftable = NULL;
-
-       for (a=0; a<3; a++) {
-               h = BMIter_New(&iter, bmdm->tc->bm, iters[a], NULL);
-               for (i=0; h; h=BMIter_Step(&iter), i++) {
-                       switch (a) {
-                               case 0:
-                                       bmdm->vtable[i] = (BMVert*) h;
-                                       BLI_ghash_insert(bmdm->vhash, h, SET_INT_IN_POINTER(i));
-                                       break;
-                               case 1:
-                                       bmdm->etable[i] = (BMEdge*) h;
-                                       BLI_ghash_insert(bmdm->ehash, h, SET_INT_IN_POINTER(i));
-                                       break;
-                               case 2:
-                                       bmdm->ftable[i] = (BMFace*) h;
-                                       BLI_ghash_insert(bmdm->fhash, h, SET_INT_IN_POINTER(i));
-                                       break;
-
-                       }
-               }
-       }
-}
-
 static void emDM_calcNormals(DerivedMesh *UNUSED(dm))
 {
        /* Nothing to do: normals are already calculated and stored on the
 static void emDM_calcNormals(DerivedMesh *UNUSED(dm))
 {
        /* Nothing to do: normals are already calculated and stored on the
-          BMVerts and BMFaces */
+        * BMVerts and BMFaces */
 }
 
 }
 
-static void emDM_recalcTesselation(DerivedMesh *UNUSED(dm))
+static void emDM_recalcTessellation(DerivedMesh *UNUSED(dm))
 {
 {
-       //EditDerivedBMesh *bmdm= (EditDerivedBMesh*) dm;
-
-       //bmdm_recalc_lookups(bmdm);
+       /* do nothing */
 }
 
 static void emDM_foreachMappedVert(
 }
 
 static void emDM_foreachMappedVert(
@@ -399,8 +360,8 @@ static void emDM_foreachMappedVert(
        BMIter iter;
        int i;
 
        BMIter iter;
        int i;
 
-       eve = BMIter_New(&iter, bmdm->tc->bm, BM_VERTS_OF_MESH, NULL);
-       for (i=0; eve; i++, eve=BMIter_Step(&iter)) {
+       eve = BM_iter_new(&iter, bmdm->tc->bm, BM_VERTS_OF_MESH, NULL);
+       for (i=0; eve; i++, eve=BM_iter_step(&iter)) {
                if (bmdm->vertexCos) {
                        func(userData, i, bmdm->vertexCos[i], bmdm->vertexNos[i], NULL);
                }
                if (bmdm->vertexCos) {
                        func(userData, i, bmdm->vertexCos[i], bmdm->vertexNos[i], NULL);
                }
@@ -421,17 +382,17 @@ static void emDM_foreachMappedEdge(
 
        if (bmdm->vertexCos) {
 
 
        if (bmdm->vertexCos) {
 
-               BM_ElemIndex_Ensure(bmdm->tc->bm, BM_VERT);
+               BM_mesh_elem_index_ensure(bmdm->tc->bm, BM_VERT);
 
 
-               eed = BMIter_New(&iter, bmdm->tc->bm, BM_EDGES_OF_MESH, NULL);
-               for (i=0; eed; i++,eed=BMIter_Step(&iter))
+               eed = BM_iter_new(&iter, bmdm->tc->bm, BM_EDGES_OF_MESH, NULL);
+               for (i=0; eed; i++,eed=BM_iter_step(&iter))
                        func(userData, i,
                        func(userData, i,
-                                bmdm->vertexCos[BM_GetIndex(eed->v1)],
-                                bmdm->vertexCos[BM_GetIndex(eed->v2)]);
+                                bmdm->vertexCos[BM_elem_index_get(eed->v1)],
+                                bmdm->vertexCos[BM_elem_index_get(eed->v2)]);
        }
        else {
        }
        else {
-               eed = BMIter_New(&iter, bmdm->tc->bm, BM_EDGES_OF_MESH, NULL);
-               for (i=0; eed; i++,eed=BMIter_Step(&iter))
+               eed = BM_iter_new(&iter, bmdm->tc->bm, BM_EDGES_OF_MESH, NULL);
+               for (i=0; eed; i++,eed=BM_iter_step(&iter))
                        func(userData, i, eed->v1->co, eed->v2->co);
        }
 }
                        func(userData, i, eed->v1->co, eed->v2->co);
        }
 }
@@ -448,22 +409,22 @@ static void emDM_drawMappedEdges(
 
        if (bmdm->vertexCos) {
 
 
        if (bmdm->vertexCos) {
 
-               BM_ElemIndex_Ensure(bmdm->tc->bm, BM_VERT);
+               BM_mesh_elem_index_ensure(bmdm->tc->bm, BM_VERT);
 
                glBegin(GL_LINES);
 
                glBegin(GL_LINES);
-               eed = BMIter_New(&iter, bmdm->tc->bm, BM_EDGES_OF_MESH, NULL);
-               for (i=0; eed; i++,eed=BMIter_Step(&iter)) {
+               eed = BM_iter_new(&iter, bmdm->tc->bm, BM_EDGES_OF_MESH, NULL);
+               for (i=0; eed; i++,eed=BM_iter_step(&iter)) {
                        if (!setDrawOptions || setDrawOptions(userData, i)) {
                        if (!setDrawOptions || setDrawOptions(userData, i)) {
-                               glVertex3fv(bmdm->vertexCos[BM_GetIndex(eed->v1)]);
-                               glVertex3fv(bmdm->vertexCos[BM_GetIndex(eed->v2)]);
+                               glVertex3fv(bmdm->vertexCos[BM_elem_index_get(eed->v1)]);
+                               glVertex3fv(bmdm->vertexCos[BM_elem_index_get(eed->v2)]);
                        }
                }
                glEnd();
        }
        else {
                glBegin(GL_LINES);
                        }
                }
                glEnd();
        }
        else {
                glBegin(GL_LINES);
-               eed = BMIter_New(&iter, bmdm->tc->bm, BM_EDGES_OF_MESH, NULL);
-               for (i=0; eed; i++,eed=BMIter_Step(&iter)) {
+               eed = BM_iter_new(&iter, bmdm->tc->bm, BM_EDGES_OF_MESH, NULL);
+               for (i=0; eed; i++,eed=BM_iter_step(&iter)) {
                        if (!setDrawOptions || setDrawOptions(userData, i)) {
                                glVertex3fv(eed->v1->co);
                                glVertex3fv(eed->v2->co);
                        if (!setDrawOptions || setDrawOptions(userData, i)) {
                                glVertex3fv(eed->v1->co);
                                glVertex3fv(eed->v2->co);
@@ -493,24 +454,24 @@ static void emDM_drawMappedEdgesInterp(
 
        if (bmdm->vertexCos) {
 
 
        if (bmdm->vertexCos) {
 
-               BM_ElemIndex_Ensure(bmdm->tc->bm, BM_VERT);
+               BM_mesh_elem_index_ensure(bmdm->tc->bm, BM_VERT);
 
                glBegin(GL_LINES);
 
                glBegin(GL_LINES);
-               eed = BMIter_New(&iter, bmdm->tc->bm, BM_EDGES_OF_MESH, NULL);
-               for (i=0; eed; i++,eed=BMIter_Step(&iter)) {
+               eed = BM_iter_new(&iter, bmdm->tc->bm, BM_EDGES_OF_MESH, NULL);
+               for (i=0; eed; i++,eed=BM_iter_step(&iter)) {
                        if (!setDrawOptions || setDrawOptions(userData, i)) {
                                setDrawInterpOptions(userData, i, 0.0);
                        if (!setDrawOptions || setDrawOptions(userData, i)) {
                                setDrawInterpOptions(userData, i, 0.0);
-                               glVertex3fv(bmdm->vertexCos[(int) BM_GetIndex(eed->v1)]);
+                               glVertex3fv(bmdm->vertexCos[BM_elem_index_get(eed->v1)]);
                                setDrawInterpOptions(userData, i, 1.0);
                                setDrawInterpOptions(userData, i, 1.0);
-                               glVertex3fv(bmdm->vertexCos[(int) BM_GetIndex(eed->v2)]);
+                               glVertex3fv(bmdm->vertexCos[BM_elem_index_get(eed->v2)]);
                        }
                }
                glEnd();
        }
        else {
                glBegin(GL_LINES);
                        }
                }
                glEnd();
        }
        else {
                glBegin(GL_LINES);
-               eed = BMIter_New(&iter, bmdm->tc->bm, BM_EDGES_OF_MESH, NULL);
-               for (i=0; eed; i++,eed=BMIter_Step(&iter)) {
+               eed = BM_iter_new(&iter, bmdm->tc->bm, BM_EDGES_OF_MESH, NULL);
+               for (i=0; eed; i++,eed=BM_iter_step(&iter)) {
                        if (!setDrawOptions || setDrawOptions(userData, i)) {
                                setDrawInterpOptions(userData, i, 0.0);
                                glVertex3fv(eed->v1->co);
                        if (!setDrawOptions || setDrawOptions(userData, i)) {
                                setDrawInterpOptions(userData, i, 0.0);
                                glVertex3fv(eed->v1->co);
@@ -535,7 +496,7 @@ static void emDM_drawUVEdges(DerivedMesh *dm)
                BMLoop *l;
                MLoopUV *lastluv = NULL, *firstluv = NULL;
 
                BMLoop *l;
                MLoopUV *lastluv = NULL, *firstluv = NULL;
 
-               if (BM_TestHFlag(efa, BM_HIDDEN))
+               if (BM_elem_flag_test(efa, BM_ELEM_HIDDEN))
                        continue;
 
                BM_ITER(l, &liter, em->bm, BM_LOOPS_OF_FACE, efa) {
                        continue;
 
                BM_ITER(l, &liter, em->bm, BM_LOOPS_OF_FACE, efa) {
@@ -571,15 +532,15 @@ static void emDM__calcFaceCent(BMesh *bm, BMFace *efa, float cent[3], float (*ve
        /*simple (and stupid) median (average) based method :/ */
 
        if (vertexCos) {
        /*simple (and stupid) median (average) based method :/ */
 
        if (vertexCos) {
-               l = BMIter_New(&iter, bm, BM_LOOPS_OF_FACE, efa);
-               for (; l; l=BMIter_Step(&iter)) {
-                       add_v3_v3(cent, vertexCos[BM_GetIndex(l->v)]);
+               l = BM_iter_new(&iter, bm, BM_LOOPS_OF_FACE, efa);
+               for (; l; l=BM_iter_step(&iter)) {
+                       add_v3_v3(cent, vertexCos[BM_elem_index_get(l->v)]);
                        tot++;
                }
        }
        else {
                        tot++;
                }
        }
        else {
-               l = BMIter_New(&iter, bm, BM_LOOPS_OF_FACE, efa);
-               for (; l; l=BMIter_Step(&iter)) {
+               l = BM_iter_new(&iter, bm, BM_LOOPS_OF_FACE, efa);
+               for (; l; l=BM_iter_step(&iter)) {
                        add_v3_v3(cent, l->v->co);
                        tot++;
                }
                        add_v3_v3(cent, l->v->co);
                        tot++;
                }
@@ -595,19 +556,24 @@ static void emDM_foreachMappedFaceCenter(
                void *userData)
 {
        EditDerivedBMesh *bmdm= (EditDerivedBMesh*) dm;
                void *userData)
 {
        EditDerivedBMesh *bmdm= (EditDerivedBMesh*) dm;
+       float (*polyNos)[3] = NULL;
        BMFace *efa;
        BMIter iter;
        float cent[3];
        int i;
 
        BMFace *efa;
        BMIter iter;
        float cent[3];
        int i;
 
+       /* ensure for face center calculation */
        if (bmdm->vertexCos) {
        if (bmdm->vertexCos) {
-               BM_ElemIndex_Ensure(bmdm->tc->bm, BM_VERT);
+               BM_mesh_elem_index_ensure(bmdm->tc->bm, BM_VERT);
+               polyNos = bmdm->polyNos;
+
+               BLI_assert(polyNos != NULL);
        }
 
        }
 
-       efa = BMIter_New(&iter, bmdm->tc->bm, BM_FACES_OF_MESH, NULL);
-       for (i=0; efa; efa=BMIter_Step(&iter), i++) {
+       efa = BM_iter_new(&iter, bmdm->tc->bm, BM_FACES_OF_MESH, NULL);
+       for (i=0; efa; efa=BM_iter_step(&iter), i++) {
                emDM__calcFaceCent(bmdm->tc->bm, efa, cent, bmdm->vertexCos);
                emDM__calcFaceCent(bmdm->tc->bm, efa, cent, bmdm->vertexCos);
-               func(userData, i, cent, bmdm->vertexCos?bmdm->faceNos[i]:efa->no);
+               func(userData, i, cent, polyNos ? polyNos[i] : efa->no);
        }
 }
 
        }
 }
 
@@ -639,18 +605,19 @@ static void emDM_drawMappedFaces(
                /* add direct access */
                float (*vertexCos)[3]= bmdm->vertexCos;
                float (*vertexNos)[3]= bmdm->vertexNos;
                /* add direct access */
                float (*vertexCos)[3]= bmdm->vertexCos;
                float (*vertexNos)[3]= bmdm->vertexNos;
-               float (*faceNos)[3]=   bmdm->faceNos;
+               float (*polyNos)[3]=   bmdm->polyNos;
+               // int *triPolyMap= bmdm->triPolyMap;
 
 
-               BM_ElemIndex_Ensure(bmdm->tc->bm, BM_VERT | BM_FACE);
+               BM_mesh_elem_index_ensure(bmdm->tc->bm, BM_VERT | BM_FACE);
 
                for (i=0; i < tottri; i++) {
                        BMLoop **l = looptris[i];
                        int drawSmooth;
 
                        efa = l[0]->f;
 
                for (i=0; i < tottri; i++) {
                        BMLoop **l = looptris[i];
                        int drawSmooth;
 
                        efa = l[0]->f;
-                       drawSmooth= BM_TestHFlag(efa, BM_SMOOTH);
+                       drawSmooth= BM_elem_flag_test(efa, BM_ELEM_SMOOTH);
 
 
-                       draw = setDrawOptions==NULL ? 1 : setDrawOptions(userData, BM_GetIndex(efa), &drawSmooth);
+                       draw = setDrawOptions==NULL ? 1 : setDrawOptions(userData, BM_elem_index_get(efa), &drawSmooth);
                        if (draw) {
                                const GLenum poly_type= GL_TRIANGLES; /* BMESH NOTE, this is odd but keep it for now to match trunk */
                                if (draw==2) { /* enabled with stipple */
                        if (draw) {
                                const GLenum poly_type= GL_TRIANGLES; /* BMESH NOTE, this is odd but keep it for now to match trunk */
                                if (draw==2) { /* enabled with stipple */
@@ -667,9 +634,9 @@ static void emDM_drawMappedFaces(
                                                if (poly_prev != GL_ZERO) glEnd();
                                                glBegin((poly_prev= poly_type)); /* BMesh: will always be GL_TRIANGLES */
                                        }
                                                if (poly_prev != GL_ZERO) glEnd();
                                                glBegin((poly_prev= poly_type)); /* BMesh: will always be GL_TRIANGLES */
                                        }
-                                       glVertex3fv(vertexCos[(int) BM_GetIndex(l[0]->v)]);
-                                       glVertex3fv(vertexCos[(int) BM_GetIndex(l[1]->v)]);
-                                       glVertex3fv(vertexCos[(int) BM_GetIndex(l[2]->v)]);
+                                       glVertex3fv(vertexCos[BM_elem_index_get(l[0]->v)]);
+                                       glVertex3fv(vertexCos[BM_elem_index_get(l[1]->v)]);
+                                       glVertex3fv(vertexCos[BM_elem_index_get(l[2]->v)]);
                                }
                                else {
                                        const GLenum shade_type= drawSmooth ? GL_SMOOTH : GL_FLAT;
                                }
                                else {
                                        const GLenum shade_type= drawSmooth ? GL_SMOOTH : GL_FLAT;
@@ -684,18 +651,18 @@ static void emDM_drawMappedFaces(
                                        }
 
                                        if (!drawSmooth) {
                                        }
 
                                        if (!drawSmooth) {
-                                               glNormal3fv(faceNos[i]);
-                                               glVertex3fv(vertexCos[(int) BM_GetIndex(l[0]->v)]);
-                                               glVertex3fv(vertexCos[(int) BM_GetIndex(l[1]->v)]);
-                                               glVertex3fv(vertexCos[(int) BM_GetIndex(l[2]->v)]);
+                                               glNormal3fv(polyNos[BM_elem_index_get(efa)]);
+                                               glVertex3fv(vertexCos[BM_elem_index_get(l[0]->v)]);
+                                               glVertex3fv(vertexCos[BM_elem_index_get(l[1]->v)]);
+                                               glVertex3fv(vertexCos[BM_elem_index_get(l[2]->v)]);
                                        }
                                        else {
                                        }
                                        else {
-                                               glNormal3fv(vertexNos[(int) BM_GetIndex(l[0]->v)]);
-                                               glVertex3fv(vertexCos[(int) BM_GetIndex(l[0]->v)]);
-                                               glNormal3fv(vertexNos[(int) BM_GetIndex(l[1]->v)]);
-                                               glVertex3fv(vertexCos[(int) BM_GetIndex(l[1]->v)]);
-                                               glNormal3fv(vertexNos[(int) BM_GetIndex(l[2]->v)]);
-                                               glVertex3fv(vertexCos[(int) BM_GetIndex(l[2]->v)]);
+                                               glNormal3fv(vertexNos[BM_elem_index_get(l[0]->v)]);
+                                               glVertex3fv(vertexCos[BM_elem_index_get(l[0]->v)]);
+                                               glNormal3fv(vertexNos[BM_elem_index_get(l[1]->v)]);
+                                               glVertex3fv(vertexCos[BM_elem_index_get(l[1]->v)]);
+                                               glNormal3fv(vertexNos[BM_elem_index_get(l[2]->v)]);
+                                               glVertex3fv(vertexCos[BM_elem_index_get(l[2]->v)]);
                                        }
                                }
 
                                        }
                                }
 
@@ -713,16 +680,16 @@ static void emDM_drawMappedFaces(
                }
        }
        else {
                }
        }
        else {
-               BM_ElemIndex_Ensure(bmdm->tc->bm, BM_FACE);
+               BM_mesh_elem_index_ensure(bmdm->tc->bm, BM_FACE);
 
                for (i=0; i < tottri; i++) {
                        BMLoop **l = looptris[i];
                        int drawSmooth;
 
                        efa = l[0]->f;
 
                for (i=0; i < tottri; i++) {
                        BMLoop **l = looptris[i];
                        int drawSmooth;
 
                        efa = l[0]->f;
-                       drawSmooth= BM_TestHFlag(efa, BM_SMOOTH);
+                       drawSmooth= BM_elem_flag_test(efa, BM_ELEM_SMOOTH);
 
 
-                       draw = setDrawOptions==NULL ? 1 : setDrawOptions(userData, BM_GetIndex(efa), &drawSmooth);
+                       draw = setDrawOptions==NULL ? 1 : setDrawOptions(userData, BM_elem_index_get(efa), &drawSmooth);
                        if (draw) {
                                const GLenum poly_type= GL_TRIANGLES; /* BMESH NOTE, this is odd but keep it for now to match trunk */
                                if (draw==2) { /* enabled with stipple */
                        if (draw) {
                                const GLenum poly_type= GL_TRIANGLES; /* BMESH NOTE, this is odd but keep it for now to match trunk */
                                if (draw==2) { /* enabled with stipple */
@@ -836,120 +803,100 @@ static void emDM_drawFacesTex_common(
        /* always use smooth shading even for flat faces, else vertex colors wont interpolate */
        glShadeModel(GL_SMOOTH);
 
        /* always use smooth shading even for flat faces, else vertex colors wont interpolate */
        glShadeModel(GL_SMOOTH);
 
-       BM_ElemIndex_Ensure(bm, BM_FACE);
+       BM_mesh_elem_index_ensure(bm, BM_FACE);
 
        if (vertexCos) {
 
        if (vertexCos) {
-               BM_ElemIndex_Ensure(bm, BM_VERT);
+               BM_mesh_elem_index_ensure(bm, BM_VERT);
 
                glBegin(GL_TRIANGLES);
                for (i=0; i<em->tottri; i++) {
                        BMLoop **ls = em->looptris[i];
 
                glBegin(GL_TRIANGLES);
                for (i=0; i<em->tottri; i++) {
                        BMLoop **ls = em->looptris[i];
-                       MTexPoly *tp= CustomData_bmesh_get(&bm->pdata, ls[0]->f->head.data, CD_MTEXPOLY);
+                       MTexPoly *tp= has_uv ? CustomData_bmesh_get(&bm->pdata, ls[0]->f->head.data, CD_MTEXPOLY) : NULL;
                        MTFace mtf = {{{0}}};
                        /*unsigned char *cp= NULL;*/ /*UNUSED*/
                        MTFace mtf = {{{0}}};
                        /*unsigned char *cp= NULL;*/ /*UNUSED*/
-                       int drawSmooth= BM_TestHFlag(ls[0]->f, BM_SMOOTH);
+                       int drawSmooth= BM_elem_flag_test(ls[0]->f, BM_ELEM_SMOOTH);
                        int flag;
 
                        efa = ls[0]->f;
 
                        if (has_uv) {
                        int flag;
 
                        efa = ls[0]->f;
 
                        if (has_uv) {
-                               mtf.flag = tp->flag;
-                               mtf.tpage = tp->tpage;
-                               mtf.transp = tp->transp;
-                               mtf.mode = tp->mode;
-                               mtf.tile = tp->tile;
-                               mtf.unwrap = tp->unwrap;
+                               ME_MTEXFACE_CPY(&mtf, tp);
                        }
 
                        if (drawParams)
                                flag= drawParams(&mtf, has_vcol, efa->mat_nr);
                        else if (drawParamsMapped)
                        }
 
                        if (drawParams)
                                flag= drawParams(&mtf, has_vcol, efa->mat_nr);
                        else if (drawParamsMapped)
-                               flag= drawParamsMapped(userData, BM_GetIndex(efa));
+                               flag= drawParamsMapped(userData, BM_elem_index_get(efa));
                        else
                                flag= 1;
 
                        if (flag != 0) { /* flag 0 == the face is hidden or invisible */
 
                        else
                                flag= 1;
 
                        if (flag != 0) { /* flag 0 == the face is hidden or invisible */
 
-                               /* we always want smooth here since otherwise vertex colors dont interpolate */
-                               if (!has_vcol) {
-                                       glShadeModel(drawSmooth?GL_SMOOTH:GL_FLAT);
-                               }
-
                                if (!drawSmooth) {
                                if (!drawSmooth) {
-                                       glNormal3fv(bmdm->faceNos[i]);
+                                       glNormal3fv(bmdm->polyNos[BM_elem_index_get(efa)]);
 
                                        bmdm_get_tri_tex(bm, ls, luv, lcol, has_uv, has_vcol);
 
                                        glTexCoord2fv(luv[0]->uv);
                                        glColor3ub(lcol[0]->b, lcol[0]->g, lcol[0]->r);
 
                                        bmdm_get_tri_tex(bm, ls, luv, lcol, has_uv, has_vcol);
 
                                        glTexCoord2fv(luv[0]->uv);
                                        glColor3ub(lcol[0]->b, lcol[0]->g, lcol[0]->r);
-                                       glVertex3fv(vertexCos[BM_GetIndex(ls[0]->v)]);
+                                       glVertex3fv(vertexCos[BM_elem_index_get(ls[0]->v)]);
 
                                        glTexCoord2fv(luv[1]->uv);
                                        glColor3ub(lcol[1]->b, lcol[1]->g, lcol[1]->r);
 
                                        glTexCoord2fv(luv[1]->uv);
                                        glColor3ub(lcol[1]->b, lcol[1]->g, lcol[1]->r);
-                                       glVertex3fv(vertexCos[BM_GetIndex(ls[1]->v)]);
+                                       glVertex3fv(vertexCos[BM_elem_index_get(ls[1]->v)]);
 
                                        glTexCoord2fv(luv[2]->uv);
                                        glColor3ub(lcol[2]->b, lcol[2]->g, lcol[2]->r);
 
                                        glTexCoord2fv(luv[2]->uv);
                                        glColor3ub(lcol[2]->b, lcol[2]->g, lcol[2]->r);
-                                       glVertex3fv(vertexCos[BM_GetIndex(ls[2]->v)]);
+                                       glVertex3fv(vertexCos[BM_elem_index_get(ls[2]->v)]);
                                }
                                else {
                                        bmdm_get_tri_tex(bm, ls, luv, lcol, has_uv, has_vcol);
 
                                        glTexCoord2fv(luv[0]->uv);
                                        glColor3ub(lcol[0]->b, lcol[0]->g, lcol[0]->r);
                                }
                                else {
                                        bmdm_get_tri_tex(bm, ls, luv, lcol, has_uv, has_vcol);
 
                                        glTexCoord2fv(luv[0]->uv);
                                        glColor3ub(lcol[0]->b, lcol[0]->g, lcol[0]->r);
-                                       glNormal3fv(vertexNos[BM_GetIndex(ls[0]->v)]);
-                                       glVertex3fv(vertexCos[BM_GetIndex(ls[0]->v)]);
+                                       glNormal3fv(vertexNos[BM_elem_index_get(ls[0]->v)]);
+                                       glVertex3fv(vertexCos[BM_elem_index_get(ls[0]->v)]);
 
                                        glTexCoord2fv(luv[1]->uv);
                                        glColor3ub(lcol[1]->b, lcol[1]->g, lcol[1]->r);
 
                                        glTexCoord2fv(luv[1]->uv);
                                        glColor3ub(lcol[1]->b, lcol[1]->g, lcol[1]->r);
-                                       glNormal3fv(vertexNos[BM_GetIndex(ls[1]->v)]);
-                                       glVertex3fv(vertexCos[BM_GetIndex(ls[1]->v)]);
+                                       glNormal3fv(vertexNos[BM_elem_index_get(ls[1]->v)]);
+                                       glVertex3fv(vertexCos[BM_elem_index_get(ls[1]->v)]);
 
                                        glTexCoord2fv(luv[2]->uv);
                                        glColor3ub(lcol[2]->b, lcol[2]->g, lcol[2]->r);
 
                                        glTexCoord2fv(luv[2]->uv);
                                        glColor3ub(lcol[2]->b, lcol[2]->g, lcol[2]->r);
-                                       glNormal3fv(vertexNos[BM_GetIndex(ls[2]->v)]);
-                                       glVertex3fv(vertexCos[BM_GetIndex(ls[2]->v)]);
+                                       glNormal3fv(vertexNos[BM_elem_index_get(ls[2]->v)]);
+                                       glVertex3fv(vertexCos[BM_elem_index_get(ls[2]->v)]);
                                }
                        }
                }
                glEnd();
        }
        else {
                                }
                        }
                }
                glEnd();
        }
        else {
-               BM_ElemIndex_Ensure(bm, BM_VERT);
+               BM_mesh_elem_index_ensure(bm, BM_VERT);
 
                for (i=0; i<em->tottri; i++) {
                        BMLoop **ls = em->looptris[i];
 
                for (i=0; i<em->tottri; i++) {
                        BMLoop **ls = em->looptris[i];
-                       MTexPoly *tp= CustomData_bmesh_get(&bm->pdata, ls[0]->f->head.data, CD_MTEXPOLY);
+                       MTexPoly *tp= has_uv ? CustomData_bmesh_get(&bm->pdata, ls[0]->f->head.data, CD_MTEXPOLY) : NULL;
                        MTFace mtf = {{{0}}};
                        /*unsigned char *cp= NULL;*/ /*UNUSED*/
                        MTFace mtf = {{{0}}};
                        /*unsigned char *cp= NULL;*/ /*UNUSED*/
-                       int drawSmooth= BM_TestHFlag(ls[0]->f, BM_SMOOTH);
+                       int drawSmooth= BM_elem_flag_test(ls[0]->f, BM_ELEM_SMOOTH);
                        int flag;
 
                        efa = ls[0]->f;
 
                        if (has_uv) {
                        int flag;
 
                        efa = ls[0]->f;
 
                        if (has_uv) {
-                               mtf.flag = tp->flag;
-                               mtf.tpage = tp->tpage;
-                               mtf.transp = tp->transp;
-                               mtf.mode = tp->mode;
-                               mtf.tile = tp->tile;
-                               mtf.unwrap = tp->unwrap;
+                               ME_MTEXFACE_CPY(&mtf, tp);
                        }
 
                        if (drawParams)
                                flag= drawParams(&mtf, has_vcol, efa->mat_nr);
                        else if (drawParamsMapped)
                        }
 
                        if (drawParams)
                                flag= drawParams(&mtf, has_vcol, efa->mat_nr);
                        else if (drawParamsMapped)
-                               flag= drawParamsMapped(userData, BM_GetIndex(efa));
+                               flag= drawParamsMapped(userData, BM_elem_index_get(efa));
                        else
                                flag= 1;
 
                        if (flag != 0) { /* flag 0 == the face is hidden or invisible */
 
                        else
                                flag= 1;
 
                        if (flag != 0) { /* flag 0 == the face is hidden or invisible */
 
-                               /* we always want smooth here since otherwise vertex colors dont interpolate */
-                               if (!has_vcol) {
-                                       glShadeModel(drawSmooth?GL_SMOOTH:GL_FLAT);
-                               }
-
                                glBegin(GL_TRIANGLES);
                                if (!drawSmooth) {
                                        glNormal3fv(efa->no);
                                glBegin(GL_TRIANGLES);
                                if (!drawSmooth) {
                                        glNormal3fv(efa->no);
@@ -1008,6 +955,8 @@ static void emDM_drawFacesTex_common(
                        }
                }
        }
                        }
                }
        }
+
+       glShadeModel(GL_FLAT);
 }
 
 static void emDM_drawFacesTex(
 }
 
 static void emDM_drawFacesTex(
@@ -1053,11 +1002,11 @@ static void emDM_drawMappedFacesGLSL(
 
        /* always use smooth shading even for flat faces, else vertex colors wont interpolate */
        glShadeModel(GL_SMOOTH);
 
        /* always use smooth shading even for flat faces, else vertex colors wont interpolate */
        glShadeModel(GL_SMOOTH);
-       BM_ElemIndex_Ensure(bm, BM_VERT | BM_FACE);
+       BM_mesh_elem_index_ensure(bm, BM_VERT | BM_FACE);
 
 #define PASSATTRIB(loop, eve, vert) {                                                                                  \
        if (attribs.totorco) {                                                                                                          \
 
 #define PASSATTRIB(loop, eve, vert) {                                                                                  \
        if (attribs.totorco) {                                                                                                          \
-               float *orco = attribs.orco.array[BM_GetIndex(eve)];                                             \
+               float *orco = attribs.orco.array[BM_elem_index_get(eve)];                                               \
                glVertexAttrib3fvARB(attribs.orco.glIndex, orco);                                               \
        }                                                                                                                                                       \
        for (b = 0; b < attribs.tottface; b++) {                                                                        \
                glVertexAttrib3fvARB(attribs.orco.glIndex, orco);                                               \
        }                                                                                                                                                       \
        for (b = 0; b < attribs.tottface; b++) {                                                                        \
@@ -1081,9 +1030,9 @@ static void emDM_drawMappedFacesGLSL(
                int drawSmooth;
 
                efa = ltri[0]->f;
                int drawSmooth;
 
                efa = ltri[0]->f;
-               drawSmooth= BM_TestHFlag(efa, BM_SMOOTH);
+               drawSmooth= BM_elem_flag_test(efa, BM_ELEM_SMOOTH);
 
 
-               if (setDrawOptions && !setDrawOptions(userData, BM_GetIndex(efa)))
+               if (setDrawOptions && !setDrawOptions(userData, BM_elem_index_get(efa)))
                        continue;
 
                new_matnr = efa->mat_nr + 1;
                        continue;
 
                new_matnr = efa->mat_nr + 1;
@@ -1096,26 +1045,26 @@ static void emDM_drawMappedFacesGLSL(
                if (dodraw) {
                        glBegin(GL_TRIANGLES);
                        if (!drawSmooth) {
                if (dodraw) {
                        glBegin(GL_TRIANGLES);
                        if (!drawSmooth) {
-                               if (vertexCos) glNormal3fv(bmdm->faceNos[i]);
+                               if (vertexCos) glNormal3fv(bmdm->polyNos[BM_elem_index_get(efa)]);
                                else glNormal3fv(efa->no);
 
                                PASSATTRIB(ltri[0], ltri[0]->v, 0);
                                else glNormal3fv(efa->no);
 
                                PASSATTRIB(ltri[0], ltri[0]->v, 0);
-                               if (vertexCos) glVertex3fv(vertexCos[BM_GetIndex(ltri[0]->v)]);
+                               if (vertexCos) glVertex3fv(vertexCos[BM_elem_index_get(ltri[0]->v)]);
                                else glVertex3fv(ltri[0]->v->co);
 
                                PASSATTRIB(ltri[1], ltri[1]->v, 1);
                                else glVertex3fv(ltri[0]->v->co);
 
                                PASSATTRIB(ltri[1], ltri[1]->v, 1);
-                               if (vertexCos) glVertex3fv(vertexCos[BM_GetIndex(ltri[1]->v)]);
+                               if (vertexCos) glVertex3fv(vertexCos[BM_elem_index_get(ltri[1]->v)]);
                                else glVertex3fv(ltri[1]->v->co);
 
                                PASSATTRIB(ltri[2], ltri[2]->v, 2);
                                else glVertex3fv(ltri[1]->v->co);
 
                                PASSATTRIB(ltri[2], ltri[2]->v, 2);
-                               if (vertexCos) glVertex3fv(vertexCos[BM_GetIndex(ltri[2]->v)]);
+                               if (vertexCos) glVertex3fv(vertexCos[BM_elem_index_get(ltri[2]->v)]);
                                else glVertex3fv(ltri[2]->v->co);
                        }
                        else {
                                PASSATTRIB(ltri[0], ltri[0]->v, 0);
                                if (vertexCos) {
                                else glVertex3fv(ltri[2]->v->co);
                        }
                        else {
                                PASSATTRIB(ltri[0], ltri[0]->v, 0);
                                if (vertexCos) {
-                                       glNormal3fv(vertexNos[BM_GetIndex(ltri[0]->v)]);
-                                       glVertex3fv(vertexCos[BM_GetIndex(ltri[0]->v)]);
+                                       glNormal3fv(vertexNos[BM_elem_index_get(ltri[0]->v)]);
+                                       glVertex3fv(vertexCos[BM_elem_index_get(ltri[0]->v)]);
                                }
                                else {
                                        glNormal3fv(ltri[0]->v->no);
                                }
                                else {
                                        glNormal3fv(ltri[0]->v->no);
@@ -1124,8 +1073,8 @@ static void emDM_drawMappedFacesGLSL(
 
                                PASSATTRIB(ltri[1], ltri[1]->v, 1);
                                if (vertexCos) {
 
                                PASSATTRIB(ltri[1], ltri[1]->v, 1);
                                if (vertexCos) {
-                                       glNormal3fv(vertexNos[BM_GetIndex(ltri[1]->v)]);
-                                       glVertex3fv(vertexCos[BM_GetIndex(ltri[1]->v)]);
+                                       glNormal3fv(vertexNos[BM_elem_index_get(ltri[1]->v)]);
+                                       glVertex3fv(vertexCos[BM_elem_index_get(ltri[1]->v)]);
                                }
                                else {
                                        glNormal3fv(ltri[1]->v->no);
                                }
                                else {
                                        glNormal3fv(ltri[1]->v->no);
@@ -1134,8 +1083,8 @@ static void emDM_drawMappedFacesGLSL(
 
                                PASSATTRIB(ltri[2], ltri[2]->v, 2);
                                if (vertexCos) {
 
                                PASSATTRIB(ltri[2], ltri[2]->v, 2);
                                if (vertexCos) {
-                                       glNormal3fv(vertexNos[BM_GetIndex(ltri[2]->v)]);
-                                       glVertex3fv(vertexCos[BM_GetIndex(ltri[2]->v)]);
+                                       glNormal3fv(vertexNos[BM_elem_index_get(ltri[2]->v)]);
+                                       glVertex3fv(vertexCos[BM_elem_index_get(ltri[2]->v)]);
                                }
                                else {
                                        glNormal3fv(ltri[2]->v->no);
                                }
                                else {
                                        glNormal3fv(ltri[2]->v->no);
@@ -1176,16 +1125,22 @@ static void emDM_drawMappedFacesMat(
        /* always use smooth shading even for flat faces, else vertex colors wont interpolate */
        glShadeModel(GL_SMOOTH);
 
        /* always use smooth shading even for flat faces, else vertex colors wont interpolate */
        glShadeModel(GL_SMOOTH);
 
-       BM_ElemIndex_Ensure(bm, BM_VERT);
+       BM_mesh_elem_index_ensure(bm, BM_VERT|BM_FACE);
 
 #define PASSATTRIB(loop, eve, vert) {                                                                                  \
        if (attribs.totorco) {                                                                                                          \
 
 #define PASSATTRIB(loop, eve, vert) {                                                                                  \
        if (attribs.totorco) {                                                                                                          \
-               float *orco = attribs.orco.array[BM_GetIndex(eve)];                                             \
-               glVertexAttrib3fvARB(attribs.orco.glIndex, orco);                                               \
+               float *orco = attribs.orco.array[BM_elem_index_get(eve)];                               \
+               if (attribs.orco.glTexco)                                                                                               \
+                       glTexCoord3fv(orco);                                                                                            \
+               else                                                                                                                                    \
+                       glVertexAttrib3fvARB(attribs.orco.glIndex, orco);                                       \
        }                                                                                                                                                       \
        for (b = 0; b < attribs.tottface; b++) {                                                                        \
                MLoopUV *_luv = CustomData_bmesh_get_n(&bm->ldata, loop->head.data, CD_MLOOPUV, b);\
        }                                                                                                                                                       \
        for (b = 0; b < attribs.tottface; b++) {                                                                        \
                MLoopUV *_luv = CustomData_bmesh_get_n(&bm->ldata, loop->head.data, CD_MLOOPUV, b);\
-               glVertexAttrib2fvARB(attribs.tface[b].glIndex, _luv->uv);                               \
+               if (attribs.tface[b].glTexco)                                                                                   \
+                       glTexCoord2fv(_luv->uv);                                                                                        \
+               else                                                                                                                                    \
+                       glVertexAttrib2fvARB(attribs.tface[b].glIndex, _luv->uv);                       \
        }                                                                                                                                                       \
        for (b = 0; b < attribs.totmcol; b++) {                                                                         \
                MLoopCol *_cp = CustomData_bmesh_get_n(&bm->ldata, loop->head.data, CD_MLOOPCOL, b);\
        }                                                                                                                                                       \
        for (b = 0; b < attribs.totmcol; b++) {                                                                         \
                MLoopCol *_cp = CustomData_bmesh_get_n(&bm->ldata, loop->head.data, CD_MLOOPCOL, b);\
@@ -1203,10 +1158,10 @@ static void emDM_drawMappedFacesMat(
                int drawSmooth;
 
                efa = ltri[0]->f;
                int drawSmooth;
 
                efa = ltri[0]->f;
-               drawSmooth = BM_TestHFlag(efa, BM_SMOOTH);
+               drawSmooth = BM_elem_flag_test(efa, BM_ELEM_SMOOTH);
 
                /* face hiding */
 
                /* face hiding */
-               if (setFace && !setFace(userData, BM_GetIndex(efa)))
+               if (setFace && !setFace(userData, BM_elem_index_get(efa)))
                        continue;
 
                /* material */
                        continue;
 
                /* material */
@@ -1219,27 +1174,27 @@ static void emDM_drawMappedFacesMat(
                /* face */
                glBegin(GL_TRIANGLES);
                if (!drawSmooth) {
                /* face */
                glBegin(GL_TRIANGLES);
                if (!drawSmooth) {
-                       if (vertexCos) glNormal3fv(bmdm->faceNos[i]);
+                       if (vertexCos) glNormal3fv(bmdm->polyNos[BM_elem_index_get(efa)]);
                        else glNormal3fv(efa->no);
 
                        PASSATTRIB(ltri[0], ltri[0]->v, 0);
                        else glNormal3fv(efa->no);
 
                        PASSATTRIB(ltri[0], ltri[0]->v, 0);
-                       if (vertexCos) glVertex3fv(vertexCos[BM_GetIndex(ltri[0]->v)]);
+                       if (vertexCos) glVertex3fv(vertexCos[BM_elem_index_get(ltri[0]->v)]);
                        else glVertex3fv(ltri[0]->v->co);
 
                        PASSATTRIB(ltri[1], ltri[1]->v, 1);
                        else glVertex3fv(ltri[0]->v->co);
 
                        PASSATTRIB(ltri[1], ltri[1]->v, 1);
-                       if (vertexCos) glVertex3fv(vertexCos[BM_GetIndex(ltri[1]->v)]);
+                       if (vertexCos) glVertex3fv(vertexCos[BM_elem_index_get(ltri[1]->v)]);
                        else glVertex3fv(ltri[1]->v->co);
 
                        PASSATTRIB(ltri[2], ltri[2]->v, 2);
                        else glVertex3fv(ltri[1]->v->co);
 
                        PASSATTRIB(ltri[2], ltri[2]->v, 2);
-                       if (vertexCos) glVertex3fv(vertexCos[BM_GetIndex(ltri[2]->v)]);
+                       if (vertexCos) glVertex3fv(vertexCos[BM_elem_index_get(ltri[2]->v)]);
                        else glVertex3fv(ltri[2]->v->co);
 
                }
                else {
                        PASSATTRIB(ltri[0], ltri[0]->v, 0);
                        if (vertexCos) {
                        else glVertex3fv(ltri[2]->v->co);
 
                }
                else {
                        PASSATTRIB(ltri[0], ltri[0]->v, 0);
                        if (vertexCos) {
-                               glNormal3fv(vertexNos[BM_GetIndex(ltri[0]->v)]);
-                               glVertex3fv(vertexCos[BM_GetIndex(ltri[0]->v)]);
+                               glNormal3fv(vertexNos[BM_elem_index_get(ltri[0]->v)]);
+                               glVertex3fv(vertexCos[BM_elem_index_get(ltri[0]->v)]);
                        }
                        else {
                                glNormal3fv(ltri[0]->v->no);
                        }
                        else {
                                glNormal3fv(ltri[0]->v->no);
@@ -1248,8 +1203,8 @@ static void emDM_drawMappedFacesMat(
 
                        PASSATTRIB(ltri[1], ltri[1]->v, 1);
                        if (vertexCos) {
 
                        PASSATTRIB(ltri[1], ltri[1]->v, 1);
                        if (vertexCos) {
-                               glNormal3fv(vertexNos[BM_GetIndex(ltri[1]->v)]);
-                               glVertex3fv(vertexCos[BM_GetIndex(ltri[1]->v)]);
+                               glNormal3fv(vertexNos[BM_elem_index_get(ltri[1]->v)]);
+                               glVertex3fv(vertexCos[BM_elem_index_get(ltri[1]->v)]);
                        }
                        else {
                                glNormal3fv(ltri[1]->v->no);
                        }
                        else {
                                glNormal3fv(ltri[1]->v->no);
@@ -1258,8 +1213,8 @@ static void emDM_drawMappedFacesMat(
 
                        PASSATTRIB(ltri[2], ltri[2]->v, 2);
                        if (vertexCos) {
 
                        PASSATTRIB(ltri[2], ltri[2]->v, 2);
                        if (vertexCos) {
-                               glNormal3fv(vertexNos[BM_GetIndex(ltri[2]->v)]);
-                               glVertex3fv(vertexCos[BM_GetIndex(ltri[2]->v)]);
+                               glNormal3fv(vertexNos[BM_elem_index_get(ltri[2]->v)]);
+                               glVertex3fv(vertexCos[BM_elem_index_get(ltri[2]->v)]);
                        }
                        else {
                                glNormal3fv(ltri[2]->v->no);
                        }
                        else {
                                glNormal3fv(ltri[2]->v->no);
@@ -1279,8 +1234,8 @@ static void emDM_getMinMax(DerivedMesh *dm, float min_r[3], float max_r[3])
        int i;
 
        if (bmdm->tc->bm->totvert) {
        int i;
 
        if (bmdm->tc->bm->totvert) {
-               eve = BMIter_New(&iter, bmdm->tc->bm, BM_VERTS_OF_MESH, NULL);
-               for (i=0; eve; eve=BMIter_Step(&iter), i++) {
+               eve = BM_iter_new(&iter, bmdm->tc->bm, BM_VERTS_OF_MESH, NULL);
+               for (i=0; eve; eve=BM_iter_step(&iter), i++) {
                        if (bmdm->vertexCos) {
                                DO_MINMAX(bmdm->vertexCos[i], min_r, max_r);
                        }
                        if (bmdm->vertexCos) {
                                DO_MINMAX(bmdm->vertexCos[i], min_r, max_r);
                        }
@@ -1334,10 +1289,10 @@ static int bmvert_to_mvert(BMesh *bm, BMVert *ev, MVert *vert_r)
 
        normal_float_to_short_v3(vert_r->no, ev->no);
 
 
        normal_float_to_short_v3(vert_r->no, ev->no);
 
-       vert_r->flag = BM_Vert_Flag_To_MEFlag(ev);
+       vert_r->flag = BM_vert_flag_to_mflag(ev);
 
        if (CustomData_has_layer(&bm->vdata, CD_BWEIGHT)) {
 
        if (CustomData_has_layer(&bm->vdata, CD_BWEIGHT)) {
-               vert_r->bweight = (unsigned char) (BM_GetCDf(&bm->vdata, ev, CD_BWEIGHT)*255.0f);
+               vert_r->bweight = (unsigned char) (BM_elem_float_data_get(&bm->vdata, ev, CD_BWEIGHT)*255.0f);
        }
 
        return 1;
        }
 
        return 1;
@@ -1345,42 +1300,46 @@ static int bmvert_to_mvert(BMesh *bm, BMVert *ev, MVert *vert_r)
 
 static void emDM_getVert(DerivedMesh *dm, int index, MVert *vert_r)
 {
 
 static void emDM_getVert(DerivedMesh *dm, int index, MVert *vert_r)
 {
+       EditDerivedBMesh *bmdm = (EditDerivedBMesh *)dm;
        BMVert *ev;
 
        BMVert *ev;
 
-       if (index < 0 || index >= ((EditDerivedBMesh *)dm)->tv) {
+       if (index < 0 || index >= bmdm->tv) {
                printf("error in emDM_getVert.\n");
                return;
        }
 
                printf("error in emDM_getVert.\n");
                return;
        }
 
-       ev = ((EditDerivedBMesh *)dm)->vtable[index];
-       bmvert_to_mvert(((EditDerivedBMesh *)dm)->tc->bm, ev, vert_r);
+       // ev = EDBM_get_vert_for_index(bmdm->tc, index);
+       ev = BM_vert_at_index(bmdm->tc->bm, index); /* warning, does list loop, _not_ ideal */
+
+       bmvert_to_mvert(bmdm->tc->bm, ev, vert_r);
 }
 
 static void emDM_getEdge(DerivedMesh *dm, int index, MEdge *edge_r)
 {
        EditDerivedBMesh *bmdm = (EditDerivedBMesh *)dm;
 }
 
 static void emDM_getEdge(DerivedMesh *dm, int index, MEdge *edge_r)
 {
        EditDerivedBMesh *bmdm = (EditDerivedBMesh *)dm;
-       BMesh *bm = ((EditDerivedBMesh *)dm)->tc->bm;
+       BMesh *bm = bmdm->tc->bm;
        BMEdge *e;
 
        BMEdge *e;
 
-       if (index < 0 || index >= ((EditDerivedBMesh *)dm)->te) {
+       if (index < 0 || index >= bmdm->te) {
                printf("error in emDM_getEdge.\n");
                return;
        }
 
                printf("error in emDM_getEdge.\n");
                return;
        }
 
-       e = bmdm->etable[index];
+       // e = EDBM_get_edge_for_index(bmdm->tc, index);
+       e = BM_edge_at_index(bmdm->tc->bm, index); /* warning, does list loop, _not_ ideal */
 
        if (CustomData_has_layer(&bm->edata, CD_BWEIGHT)) {
 
        if (CustomData_has_layer(&bm->edata, CD_BWEIGHT)) {
-               edge_r->bweight = (unsigned char) (BM_GetCDf(&bm->edata, e, CD_BWEIGHT)*255.0f);
+               edge_r->bweight = (unsigned char) (BM_elem_float_data_get(&bm->edata, e, CD_BWEIGHT)*255.0f);
        }
 
        if (CustomData_has_layer(&bm->edata, CD_CREASE)) {
        }
 
        if (CustomData_has_layer(&bm->edata, CD_CREASE)) {
-               edge_r->crease = (unsigned char) (BM_GetCDf(&bm->edata, e, CD_CREASE)*255.0f);
+               edge_r->crease = (unsigned char) (BM_elem_float_data_get(&bm->edata, e, CD_CREASE)*255.0f);
        }
 
        }
 
-       edge_r->flag = BM_Edge_Flag_To_MEFlag(e);
+       edge_r->flag = BM_edge_flag_to_mflag(e);
 
 
-       edge_r->v1 = GET_INT_FROM_POINTER(BLI_ghash_lookup(bmdm->vhash, e->v1));
-       edge_r->v2 = GET_INT_FROM_POINTER(BLI_ghash_lookup(bmdm->vhash, e->v2));
+       edge_r->v1 = BM_elem_index_get(e->v1);
+       edge_r->v2 = BM_elem_index_get(e->v2);
 }
 
 static void emDM_getTessFace(DerivedMesh *dm, int index, MFace *face_r)
 }
 
 static void emDM_getTessFace(DerivedMesh *dm, int index, MFace *face_r)
@@ -1389,21 +1348,21 @@ static void emDM_getTessFace(DerivedMesh *dm, int index, MFace *face_r)
        BMFace *ef;
        BMLoop **l;
 
        BMFace *ef;
        BMLoop **l;
 
-       if (index < 0 || index >= ((EditDerivedBMesh *)dm)->tf) {
+       if (index < 0 || index >= bmdm->tf) {
                printf("error in emDM_getTessFace.\n");
                return;
        }
 
                printf("error in emDM_getTessFace.\n");
                return;
        }
 
-       l = ((EditDerivedBMesh *)dm)->tc->looptris[index];
+       l = bmdm->tc->looptris[index];
 
        ef = l[0]->f;
 
        face_r->mat_nr = (unsigned char) ef->mat_nr;
 
        ef = l[0]->f;
 
        face_r->mat_nr = (unsigned char) ef->mat_nr;
-       face_r->flag = BM_Face_Flag_To_MEFlag(ef);
+       face_r->flag = BM_face_flag_to_mflag(ef);
 
 
-       face_r->v1 = GET_INT_FROM_POINTER(BLI_ghash_lookup(bmdm->vhash, l[0]->v));
-       face_r->v2 = GET_INT_FROM_POINTER(BLI_ghash_lookup(bmdm->vhash, l[1]->v));
-       face_r->v3 = GET_INT_FROM_POINTER(BLI_ghash_lookup(bmdm->vhash, l[2]->v));
+       face_r->v1 = BM_elem_index_get(l[0]->v);
+       face_r->v2 = BM_elem_index_get(l[1]->v);
+       face_r->v3 = BM_elem_index_get(l[2]->v);
        face_r->v4 = 0;
 
        test_index_face(face_r, NULL, 0, 3);
        face_r->v4 = 0;
 
        test_index_face(face_r, NULL, 0, 3);
@@ -1415,16 +1374,16 @@ static void emDM_copyVertArray(DerivedMesh *dm, MVert *vert_r)
        BMVert *ev;
        BMIter iter;
 
        BMVert *ev;
        BMIter iter;
 
-       ev = BMIter_New(&iter, bm, BM_VERTS_OF_MESH, NULL);
-       for ( ; ev; ev = BMIter_Step(&iter), ++vert_r) {
+       ev = BM_iter_new(&iter, bm, BM_VERTS_OF_MESH, NULL);
+       for ( ; ev; ev = BM_iter_step(&iter), ++vert_r) {
                copy_v3_v3(vert_r->co, ev->co);
 
                normal_float_to_short_v3(vert_r->no, ev->no);
 
                copy_v3_v3(vert_r->co, ev->co);
 
                normal_float_to_short_v3(vert_r->no, ev->no);
 
-               vert_r->flag = BM_Vert_Flag_To_MEFlag(ev);
+               vert_r->flag = BM_vert_flag_to_mflag(ev);
 
                if (CustomData_has_layer(&bm->vdata, CD_BWEIGHT)) {
 
                if (CustomData_has_layer(&bm->vdata, CD_BWEIGHT)) {
-                       vert_r->bweight = (unsigned char) (BM_GetCDf(&bm->vdata, ev, CD_BWEIGHT)*255.0f);
+                       vert_r->bweight = (unsigned char) (BM_elem_float_data_get(&bm->vdata, ev, CD_BWEIGHT)*255.0f);
                }
        }
 }
                }
        }
 }
@@ -1437,34 +1396,34 @@ static void emDM_copyEdgeArray(DerivedMesh *dm, MEdge *edge_r)
        int has_bweight = CustomData_has_layer(&bm->edata, CD_BWEIGHT);
        int has_crease = CustomData_has_layer(&bm->edata, CD_CREASE);
 
        int has_bweight = CustomData_has_layer(&bm->edata, CD_BWEIGHT);
        int has_crease = CustomData_has_layer(&bm->edata, CD_CREASE);
 
-       BM_ElemIndex_Ensure(bm, BM_VERT);
+       BM_mesh_elem_index_ensure(bm, BM_VERT);
 
 
-       ee = BMIter_New(&iter, bm, BM_EDGES_OF_MESH, NULL);
-       for ( ; ee; ee=BMIter_Step(&iter), edge_r++) {
+       ee = BM_iter_new(&iter, bm, BM_EDGES_OF_MESH, NULL);
+       for ( ; ee; ee=BM_iter_step(&iter), edge_r++) {
                if (has_bweight) {
                if (has_bweight) {
-                       edge_r->bweight = (unsigned char) (BM_GetCDf(&bm->edata, ee, CD_BWEIGHT)*255.0f);
+                       edge_r->bweight = (unsigned char) (BM_elem_float_data_get(&bm->edata, ee, CD_BWEIGHT)*255.0f);
                }
 
                if (has_crease) {
                }
 
                if (has_crease) {
-                       edge_r->crease = (unsigned char) (BM_GetCDf(&bm->edata, ee, CD_CREASE)*255.0f);
+                       edge_r->crease = (unsigned char) (BM_elem_float_data_get(&bm->edata, ee, CD_CREASE)*255.0f);
                }
 
                }
 
-               edge_r->flag = BM_Edge_Flag_To_MEFlag(ee);
+               edge_r->flag = BM_edge_flag_to_mflag(ee);
 
 
-               edge_r->v1 = (int)BM_GetIndex(ee->v1);
-               edge_r->v2 = (int)BM_GetIndex(ee->v2);
+               edge_r->v1 = BM_elem_index_get(ee->v1);
+               edge_r->v2 = BM_elem_index_get(ee->v2);
        }
 }
 
 static void emDM_copyTessFaceArray(DerivedMesh *dm, MFace *face_r)
 {
        EditDerivedBMesh *bmdm = (EditDerivedBMesh *)dm;
        }
 }
 
 static void emDM_copyTessFaceArray(DerivedMesh *dm, MFace *face_r)
 {
        EditDerivedBMesh *bmdm = (EditDerivedBMesh *)dm;
-       BMesh *bm = ((EditDerivedBMesh *)dm)->tc->bm;
+       BMesh *bm = bmdm->tc->bm;
        BMFace *ef;
        BMLoop **l;
        int i;
 
        BMFace *ef;
        BMLoop **l;
        int i;
 
-       BM_ElemIndex_Ensure(bm, BM_VERT);
+       BM_mesh_elem_index_ensure(bm, BM_VERT);
 
        for (i=0; i<bmdm->tc->tottri; i++, face_r++) {
                l = bmdm->tc->looptris[i];
 
        for (i=0; i<bmdm->tc->tottri; i++, face_r++) {
                l = bmdm->tc->looptris[i];
@@ -1472,11 +1431,11 @@ static void emDM_copyTessFaceArray(DerivedMesh *dm, MFace *face_r)
 
                face_r->mat_nr = (unsigned char) ef->mat_nr;
 
 
                face_r->mat_nr = (unsigned char) ef->mat_nr;
 
-               face_r->flag = BM_Face_Flag_To_MEFlag(ef);
+               face_r->flag = BM_face_flag_to_mflag(ef);
 
 
-               face_r->v1 = BM_GetIndex(l[0]->v);
-               face_r->v2 = BM_GetIndex(l[1]->v);
-               face_r->v3 = BM_GetIndex(l[2]->v);
+               face_r->v1 = BM_elem_index_get(l[0]->v);
+               face_r->v2 = BM_elem_index_get(l[1]->v);
+               face_r->v3 = BM_elem_index_get(l[2]->v);
                face_r->v4 = 0;
 
                test_index_face(face_r, NULL, 0, 3);
                face_r->v4 = 0;
 
                test_index_face(face_r, NULL, 0, 3);
@@ -1486,18 +1445,18 @@ static void emDM_copyTessFaceArray(DerivedMesh *dm, MFace *face_r)
 
 static void emDM_copyLoopArray(DerivedMesh *dm, MLoop *loop_r)
 {
 
 static void emDM_copyLoopArray(DerivedMesh *dm, MLoop *loop_r)
 {
-       /* EditDerivedBMesh *bmdm = (EditDerivedBMesh *)dm; */ /* UNUSED */
-       BMesh *bm = ((EditDerivedBMesh *)dm)->tc->bm;
+       EditDerivedBMesh *bmdm = (EditDerivedBMesh *)dm;
+       BMesh *bm = bmdm->tc->bm;
        BMIter iter, liter;
        BMFace *f;
        BMLoop *l;
 
        BMIter iter, liter;
        BMFace *f;
        BMLoop *l;
 
-       BM_ElemIndex_Ensure(bm, BM_VERT | BM_EDGE);
+       BM_mesh_elem_index_ensure(bm, BM_VERT | BM_EDGE);
 
        BM_ITER(f, &iter, bm, BM_FACES_OF_MESH, NULL) {
                BM_ITER(l, &liter, bm, BM_LOOPS_OF_FACE, f) {
 
        BM_ITER(f, &iter, bm, BM_FACES_OF_MESH, NULL) {
                BM_ITER(l, &liter, bm, BM_LOOPS_OF_FACE, f) {
-                       loop_r->v = BM_GetIndex(l->v);
-                       loop_r->e = BM_GetIndex(l->e);
+                       loop_r->v = BM_elem_index_get(l->v);
+                       loop_r->e = BM_elem_index_get(l->e);
                        loop_r++;
                }
        }
                        loop_r++;
                }
        }
@@ -1505,15 +1464,15 @@ static void emDM_copyLoopArray(DerivedMesh *dm, MLoop *loop_r)
 
 static void emDM_copyPolyArray(DerivedMesh *dm, MPoly *poly_r)
 {
 
 static void emDM_copyPolyArray(DerivedMesh *dm, MPoly *poly_r)
 {
-       /* EditDerivedBMesh *bmdm = (EditDerivedBMesh *)dm; */ /* UNUSED */
-       BMesh *bm = ((EditDerivedBMesh *)dm)->tc->bm;
+       EditDerivedBMesh *bmdm = (EditDerivedBMesh *)dm;
+       BMesh *bm = bmdm->tc->bm;
        BMIter iter;
        BMFace *f;
        int i;
 
        i = 0;
        BM_ITER(f, &iter, bm, BM_FACES_OF_MESH, NULL) {
        BMIter iter;
        BMFace *f;
        int i;
 
        i = 0;
        BM_ITER(f, &iter, bm, BM_FACES_OF_MESH, NULL) {
-               poly_r->flag = BM_Face_Flag_To_MEFlag(f);
+               poly_r->flag = BM_face_flag_to_mflag(f);
                poly_r->loopstart = i;
                poly_r->totloop = f->len;
                poly_r->mat_nr = f->mat_nr;
                poly_r->loopstart = i;
                poly_r->totloop = f->len;
                poly_r->mat_nr = f->mat_nr;
@@ -1552,8 +1511,8 @@ static void *emDM_getTessFaceDataArray(DerivedMesh *dm, int type)
                        data = datalayer = DM_get_tessface_data_layer(dm, type);
                        for (i=0; i<bmdm->tc->tottri; i++, data+=size) {
                                efa = bmdm->tc->looptris[i][0]->f;
                        data = datalayer = DM_get_tessface_data_layer(dm, type);
                        for (i=0; i<bmdm->tc->tottri; i++, data+=size) {
                                efa = bmdm->tc->looptris[i][0]->f;
-                               /*BMESH_TODO: need to still add tface data,
-                                 derived from the loops.*/
+                               /* BMESH_TODO: need to still add tface data,
+                                * derived from the loops.*/
                                bmdata = CustomData_bmesh_get(&bm->pdata, efa->head.data, type);
                                memcpy(data, bmdata, size);
                        }
                                bmdata = CustomData_bmesh_get(&bm->pdata, efa->head.data, type);
                                memcpy(data, bmdata, size);
                        }
@@ -1591,17 +1550,9 @@ static void emDM_release(DerivedMesh *dm)
                if (bmdm->vertexCos) {
                        MEM_freeN(bmdm->vertexCos);
                        MEM_freeN(bmdm->vertexNos);
                if (bmdm->vertexCos) {
                        MEM_freeN(bmdm->vertexCos);
                        MEM_freeN(bmdm->vertexNos);
-                       MEM_freeN(bmdm->faceNos);
+                       MEM_freeN(bmdm->polyNos);
                }
 
                }
 
-               if (bmdm->fhash) BLI_ghash_free(bmdm->fhash, NULL, NULL);
-               if (bmdm->ehash) BLI_ghash_free(bmdm->ehash, NULL, NULL);
-               if (bmdm->vhash) BLI_ghash_free(bmdm->vhash, NULL, NULL);
-
-               if (bmdm->vtable) MEM_freeN(bmdm->vtable);
-               if (bmdm->etable) MEM_freeN(bmdm->etable);
-               if (bmdm->ftable) MEM_freeN(bmdm->ftable);
-
                MEM_freeN(bmdm);
        }
 }
                MEM_freeN(bmdm);
        }
 }
@@ -1684,7 +1635,7 @@ DerivedMesh *getEditDerivedBMesh(
        bmdm->dm.getTessFaceDataArray = emDM_getTessFaceDataArray;
 
        bmdm->dm.calcNormals = emDM_calcNormals;
        bmdm->dm.getTessFaceDataArray = emDM_getTessFaceDataArray;
 
        bmdm->dm.calcNormals = emDM_calcNormals;
-       bmdm->dm.recalcTesselation = emDM_recalcTesselation;
+       bmdm->dm.recalcTessellation = emDM_recalcTessellation;
 
        bmdm->dm.foreachMappedVert = emDM_foreachMappedVert;
        bmdm->dm.foreachMappedEdge = emDM_foreachMappedEdge;
 
        bmdm->dm.foreachMappedVert = emDM_foreachMappedVert;
        bmdm->dm.foreachMappedEdge = emDM_foreachMappedEdge;
@@ -1712,39 +1663,39 @@ DerivedMesh *getEditDerivedBMesh(
 
                DM_add_vert_layer(&bmdm->dm, CD_MDEFORMVERT, CD_CALLOC, NULL);
 
 
                DM_add_vert_layer(&bmdm->dm, CD_MDEFORMVERT, CD_CALLOC, NULL);
 
-               eve = BMIter_New(&iter, bmdm->tc->bm, BM_VERTS_OF_MESH, NULL);
-               for (i=0; eve; eve=BMIter_Step(&iter), i++)
+               eve = BM_iter_new(&iter, bmdm->tc->bm, BM_VERTS_OF_MESH, NULL);
+               for (i=0; eve; eve=BM_iter_step(&iter), i++)
                        DM_set_vert_data(&bmdm->dm, i, CD_MDEFORMVERT,
                                                         CustomData_bmesh_get(&bm->vdata, eve->head.data, CD_MDEFORMVERT));
        }
 
        if (vertexCos) {
                        DM_set_vert_data(&bmdm->dm, i, CD_MDEFORMVERT,
                                                         CustomData_bmesh_get(&bm->vdata, eve->head.data, CD_MDEFORMVERT));
        }
 
        if (vertexCos) {
+               BMFace *efa;
                BMVert *eve;
                BMVert *eve;
-               BMIter iter;
-               int totface = bmdm->tc->tottri;
+               BMIter fiter;
+               BMIter viter;
                int i;
 
                int i;
 
-               BM_ElemIndex_Ensure(bm, BM_VERT);
+               BM_mesh_elem_index_ensure(bm, BM_VERT);
 
                bmdm->vertexNos = MEM_callocN(sizeof(*bmdm->vertexNos) * bm->totvert, "bmdm_vno");
 
                bmdm->vertexNos = MEM_callocN(sizeof(*bmdm->vertexNos) * bm->totvert, "bmdm_vno");
-               bmdm->faceNos = MEM_mallocN(sizeof(*bmdm->faceNos)*totface, "bmdm_vno");
-
-               for (i=0; i<bmdm->tc->tottri; i++) {
-                       BMLoop **l = bmdm->tc->looptris[i];
-                       float *v1 = vertexCos[(int) BM_GetIndex(l[0]->v)];
-                       float *v2 = vertexCos[(int) BM_GetIndex(l[1]->v)];
-                       float *v3 = vertexCos[(int) BM_GetIndex(l[2]->v)];
-                       float *no = bmdm->faceNos[i];
-
-                       normal_tri_v3( no,v1, v2, v3);
-                       add_v3_v3v3(bmdm->vertexNos[BM_GetIndex(l[0]->v)], bmdm->vertexNos[BM_GetIndex(l[0]->v)], no);
-                       add_v3_v3v3(bmdm->vertexNos[BM_GetIndex(l[1]->v)], bmdm->vertexNos[BM_GetIndex(l[1]->v)], no);
-                       add_v3_v3v3(bmdm->vertexNos[BM_GetIndex(l[2]->v)], bmdm->vertexNos[BM_GetIndex(l[2]->v)], no);
+               bmdm->polyNos = MEM_mallocN(sizeof(*bmdm->polyNos)*bm->totface, "bmdm_pno");
+
+               i = 0;
+               BM_ITER(efa, &fiter, bm, BM_FACES_OF_MESH, NULL) {
+                       BM_elem_index_set(efa, i); /* set_inline */
+                       BM_face_normal_update_vcos(bm, efa, bmdm->polyNos[i], vertexCos);
+                       i++;
                }
                }
+               bm->elem_index_dirty &= ~BM_FACE;
 
 
-               eve=BMIter_New(&iter, bm, BM_VERTS_OF_MESH, NULL);
-               for (i=0; eve; eve=BMIter_Step(&iter), i++) {
+               eve=BM_iter_new(&viter, bm, BM_VERTS_OF_MESH, NULL);
+               for (i=0; eve; eve=BM_iter_step(&viter), i++) {
                        float *no = bmdm->vertexNos[i];
                        float *no = bmdm->vertexNos[i];
+                       BM_ITER(efa, &fiter, bm, BM_FACES_OF_VERT, eve) {
+                               add_v3_v3(no, bmdm->polyNos[BM_elem_index_get(efa)]);
+                       }
+
                        /* following Mesh convention; we use vertex coordinate itself
                         * for normal in this case */
                        if (normalize_v3(no)==0.0) {
                        /* following Mesh convention; we use vertex coordinate itself
                         * for normal in this case */
                        if (normalize_v3(no)==0.0) {
@@ -1754,7 +1705,17 @@ DerivedMesh *getEditDerivedBMesh(
                }
        }
 
                }
        }
 
-       //bmdm_recalc_lookups(bmdm);
-
        return (DerivedMesh*) bmdm;
 }
        return (DerivedMesh*) bmdm;
 }
+
+/**
+ * \brief Return the BMEditMesh for a given object
+ *
+ * \note this function assumes this is a mesh object,
+ * don't add NULL data check here. caller must do that
+ */
+BMEditMesh *BMEdit_FromObject(Object *ob)
+{
+       BLI_assert(ob->type == OB_MESH);
+       return ((Mesh *)   ob->data   )->edit_btmesh;
+}