style cleanup: follow style guide for formatting of if/for/while loops, and else...
[blender.git] / source / blender / editors / mesh / mesh_data.c
index d752d90ffdd9e802b3f3854daaa009223c5ed219..a8836b466a8a790cc4de49fc95f8c3788ded7add 100644 (file)
 #include "MEM_guardedalloc.h"
 
 #include "DNA_material_types.h"
+#include "DNA_mesh_types.h"
 #include "DNA_meshdata_types.h"
 #include "DNA_object_types.h"
 #include "DNA_scene_types.h"
 #include "DNA_view3d_types.h"
 
+#include "BLI_utildefines.h"
+#include "BLI_array.h"
 #include "BLI_math.h"
-#include "BLI_editVert.h"
 #include "BLI_edgehash.h"
 #include "BLI_utildefines.h"
 
@@ -55,6 +57,7 @@
 #include "BKE_material.h"
 #include "BKE_mesh.h"
 #include "BKE_report.h"
+#include "BKE_tessmesh.h"
 
 #include "RNA_access.h"
 #include "RNA_define.h"
 
 #include "mesh_intern.h"
 
+#define GET_CD_DATA(me, data) (me->edit_btmesh ? &me->edit_btmesh->bm->data : &me->data)
 static void delete_customdata_layer(bContext *C, Object *ob, CustomDataLayer *layer)
 {
        Mesh *me = ob->data;
-       CustomData *data= (me->edit_mesh)? &me->edit_mesh->fdata: &me->fdata;
+       CustomData *data;
        void *actlayerdata, *rndlayerdata, *clonelayerdata, *stencillayerdata, *layerdata=layer->data;
        int type= layer->type;
-       int index= CustomData_get_layer_index(data, type);
-       int i, actindex, rndindex, cloneindex, stencilindex;
+       int index;
+       int i, actindex, rndindex, cloneindex, stencilindex, tot;
+       
+       if (layer->type == CD_MLOOPCOL || layer->type == CD_MLOOPUV) {
+               data = (me->edit_btmesh)? &me->edit_btmesh->bm->ldata: &me->ldata;
+               tot = me->totloop;
+       }
+       else {
+               data = (me->edit_btmesh)? &me->edit_btmesh->bm->pdata: &me->pdata;
+               tot = me->totpoly;
+       }
        
+       index = CustomData_get_layer_index(data, type);
+
        /* ok, deleting a non-active layer needs to preserve the active layer indices.
-         to do this, we store a pointer to the .data member of both layer and the active layer,
-         (to detect if we're deleting the active layer or not), then use the active
-         layer data pointer to find where the active layer has ended up.
-         
-         this is necassary because the deletion functions only support deleting the active
-         layer. */
+        * to do this, we store a pointer to the .data member of both layer and the active layer,
+        * (to detect if we're deleting the active layer or not), then use the active
+        * layer data pointer to find where the active layer has ended up.
+        *
+        * this is necessary because the deletion functions only support deleting the active
+        * layer. */
        actlayerdata = data->layers[CustomData_get_active_layer_index(data, type)].data;
        rndlayerdata = data->layers[CustomData_get_render_layer_index(data, type)].data;
        clonelayerdata = data->layers[CustomData_get_clone_layer_index(data, type)].data;
        stencillayerdata = data->layers[CustomData_get_stencil_layer_index(data, type)].data;
        CustomData_set_layer_active(data, type, layer - &data->layers[index]);
 
-       if(me->edit_mesh) {
-               EM_free_data_layer(me->edit_mesh, data, type);
+       if (me->edit_btmesh) {
+               BM_data_layer_free(me->edit_btmesh->bm, data, type);
        }
        else {
-               CustomData_free_layer_active(data, type, me->totface);
-               mesh_update_customdata_pointers(me);
+               CustomData_free_layer_active(data, type, tot);
+               mesh_update_customdata_pointers(me, TRUE);
        }
 
-       if(!CustomData_has_layer(data, type) && (type == CD_MCOL && (ob->mode & OB_MODE_VERTEX_PAINT)))
+       if (!CustomData_has_layer(data, type) && (type == CD_MLOOPCOL && (ob->mode & OB_MODE_VERTEX_PAINT)))
                ED_object_toggle_modes(C, OB_MODE_VERTEX_PAINT);
 
        /* reconstruct active layer */
@@ -162,72 +177,215 @@ static void delete_customdata_layer(bContext *C, Object *ob, CustomDataLayer *la
        }
 }
 
-static void copy_editface_active_customdata(EditMesh *em, int type, int index)
+static void copy_editface_active_customdata(BMEditMesh *em, int type, int index)
 {
+#if 1 /*BMESH_TODO*/
+       (void)em;
+       (void)type;
+       (void)index;
+#else
        EditFace *efa;
        int n= CustomData_get_active_layer(&em->fdata, type);
 
-       for(efa= em->faces.first; efa; efa= efa->next) {
+       for (efa= em->faces.first; efa; efa= efa->next) {
                void *data= CustomData_em_get_n(&em->fdata, efa->data, type, n);
                CustomData_em_set_n(&em->fdata, efa->data, type, index, data);
        }
+#endif
+}
+
+int ED_mesh_uv_loop_reset(struct bContext *C, struct Mesh *me)
+{
+       BMEditMesh *em= me->edit_btmesh;
+       MLoopUV *luv;
+       BLI_array_declare(polylengths);
+       int *polylengths = NULL;
+       BLI_array_declare(uvs);
+       float **uvs = NULL;
+       float **fuvs = NULL;
+       int i, j;
+
+       if (em) {
+               /* Collect BMesh UVs */
+
+               BMFace *efa;
+               BMLoop *l;
+               BMIter iter, liter;
+
+               BLI_assert(CustomData_has_layer(&em->bm->ldata, CD_MLOOPUV));
+
+               BM_ITER(efa, &iter, em->bm, BM_FACES_OF_MESH, NULL) {
+                       if (!BM_elem_flag_test(efa, BM_ELEM_SELECT))
+                               continue;
+
+                       i = 0;
+                       BM_ITER(l, &liter, em->bm, BM_LOOPS_OF_FACE, efa) {
+                               luv = CustomData_bmesh_get(&em->bm->ldata, l->head.data, CD_MLOOPUV);
+                               BLI_array_append(uvs, luv->uv);
+                               i++;
+                       }
+
+                       BLI_array_append(polylengths, efa->len);
+               }
+       }
+       else {
+               /* Collect Mesh UVs */
+
+               MPoly *mp;
+
+               BLI_assert(CustomData_has_layer(&me->ldata, CD_MLOOPUV));
+
+               for (j = 0; j < me->totpoly; j++) {
+                       mp = &me->mpoly[j];
+
+                       for (i = 0; i < mp->totloop; i++) {
+                               luv = &me->mloopuv[mp->loopstart + i];
+                               BLI_array_append(uvs, luv->uv);
+                       }
+
+                       BLI_array_append(polylengths, mp->totloop);
+               }
+       }
+
+       fuvs = uvs;
+       for (j = 0; j < BLI_array_count(polylengths); j++) {
+               int len = polylengths[j];
+
+               if (len == 3) {
+                       fuvs[0][0] = 0.0;
+                       fuvs[0][1] = 0.0;
+                       
+                       fuvs[1][0] = 1.0;
+                       fuvs[1][1] = 0.0;
+
+                       fuvs[2][0] = 1.0;
+                       fuvs[2][1] = 1.0;
+               }
+               else if (len == 4) {
+                       fuvs[0][0] = 0.0;
+                       fuvs[0][1] = 0.0;
+                       
+                       fuvs[1][0] = 1.0;
+                       fuvs[1][1] = 0.0;
+
+                       fuvs[2][0] = 1.0;
+                       fuvs[2][1] = 1.0;
+
+                       fuvs[3][0] = 0.0;
+                       fuvs[3][1] = 1.0;
+                 /*make sure we ignore 2-sided faces*/
+               }
+               else if (len > 2) {
+                       float fac = 0.0f, dfac = 1.0f / (float)len;
+
+                       dfac *= M_PI*2;
+
+                       for (i = 0; i < len; i++) {
+                               fuvs[i][0] = 0.5f * sin(fac) + 0.5f;
+                               fuvs[i][1] = 0.5f * cos(fac) + 0.5f;
+
+                               fac += dfac;
+                       }
+               }
+
+               fuvs += len;
+       }
+
+       /* BMESH_TODO: Copy poly UVs onto CD_MTFACE layer for tessellated faces */
+
+       BLI_array_free(uvs);
+       BLI_array_free(polylengths);
+
+       DAG_id_tag_update(&me->id, 0);
+       WM_event_add_notifier(C, NC_GEOM|ND_DATA, me);
+
+       return 1;
 }
 
 int ED_mesh_uv_texture_add(bContext *C, Mesh *me, const char *name, int active_set)
 {
-       EditMesh *em;
+       BMEditMesh *em;
        int layernum;
 
-       if(me->edit_mesh) {
-               em= me->edit_mesh;
+       if (me->edit_btmesh) {
+               em= me->edit_btmesh;
 
-               layernum= CustomData_number_of_layers(&em->fdata, CD_MTFACE);
-               if(layernum >= MAX_MTFACE)
-                       return 0;
+               layernum = CustomData_number_of_layers(&em->bm->pdata, CD_MTEXPOLY);
+               if (layernum >= MAX_MTFACE)
+                       return -1;
 
-               EM_add_data_layer(em, &em->fdata, CD_MTFACE, name);
+               BM_data_layer_add(em->bm, &em->bm->pdata, CD_MTEXPOLY);
+               CustomData_set_layer_active(&em->bm->pdata, CD_MTEXPOLY, layernum);
+               CustomData_set_layer_name(&em->bm->pdata, CD_MTEXPOLY, layernum, name);
 
-               if(layernum) /* copy data from active UV */
+               /* copy data from active UV */
+               if (layernum)
                        copy_editface_active_customdata(em, CD_MTFACE, layernum);
 
-               if(active_set || layernum==0)
-                       CustomData_set_layer_active(&em->fdata, CD_MTFACE, layernum);
+               if (active_set || layernum == 0) {
+                       CustomData_set_layer_active(&em->bm->pdata, CD_MTEXPOLY, layernum);
+               }
+
+               BM_data_layer_add(em->bm, &em->bm->ldata, CD_MLOOPUV);
+               CustomData_set_layer_name(&em->bm->ldata, CD_MLOOPUV, layernum, name);
+               
+               CustomData_set_layer_active(&em->bm->ldata, CD_MLOOPUV, layernum);
+               if (active_set || layernum == 0) {
+                       CustomData_set_layer_active(&em->bm->ldata, CD_MLOOPUV, layernum);
+               }
        }
        else {
-               layernum= CustomData_number_of_layers(&me->fdata, CD_MTFACE);
-               if(layernum >= MAX_MTFACE)
-                       return 0;
+               layernum = CustomData_number_of_layers(&me->pdata, CD_MTEXPOLY);
+               if (layernum >= MAX_MTFACE)
+                       return -1;
 
-               if(me->mtface)
+               if (me->mtpoly) {
+                       CustomData_add_layer_named(&me->pdata, CD_MTEXPOLY, CD_DUPLICATE, me->mtpoly, me->totpoly, name);
+                       CustomData_add_layer_named(&me->ldata, CD_MLOOPUV, CD_DUPLICATE, me->mloopuv, me->totloop, name);
                        CustomData_add_layer_named(&me->fdata, CD_MTFACE, CD_DUPLICATE, me->mtface, me->totface, name);
-               else
+               }
+               else {
+                       CustomData_add_layer_named(&me->pdata, CD_MTEXPOLY, CD_DEFAULT, NULL, me->totpoly, name);
+                       CustomData_add_layer_named(&me->ldata, CD_MLOOPUV, CD_DEFAULT, NULL, me->totloop, name);
                        CustomData_add_layer_named(&me->fdata, CD_MTFACE, CD_DEFAULT, NULL, me->totface, name);
+               }
+               
+               if (active_set || layernum == 0) {
+                       CustomData_set_layer_active(&me->pdata, CD_MTEXPOLY, layernum);
+                       CustomData_set_layer_active(&me->ldata, CD_MLOOPUV, layernum);
 
-               if(active_set || layernum==0)
                        CustomData_set_layer_active(&me->fdata, CD_MTFACE, layernum);
+               }
 
-               mesh_update_customdata_pointers(me);
+               mesh_update_customdata_pointers(me, TRUE);
        }
 
+       ED_mesh_uv_loop_reset(C, me);
+
        DAG_id_tag_update(&me->id, 0);
        WM_event_add_notifier(C, NC_GEOM|ND_DATA, me);
 
-       return 1;
+       return layernum;
 }
 
 int ED_mesh_uv_texture_remove(bContext *C, Object *ob, Mesh *me)
 {
-       CustomData *data= (me->edit_mesh)? &me->edit_mesh->fdata: &me->fdata;
-       CustomDataLayer *cdl;
+       CustomData *pdata = GET_CD_DATA(me, pdata), *ldata = GET_CD_DATA(me, ldata);
+       CustomDataLayer *cdlp, *cdlu;
        int index;
 
-       index= CustomData_get_active_layer_index(data, CD_MTFACE);
-       cdl= (index == -1) ? NULL: &data->layers[index];
+       index= CustomData_get_active_layer_index(pdata, CD_MTEXPOLY);
+       cdlp= (index == -1)? NULL: &pdata->layers[index];
 
-       if(!cdl)
+       index= CustomData_get_active_layer_index(ldata, CD_MLOOPUV);
+       cdlu= (index == -1)? NULL: &ldata->layers[index];
+       
+       if (!cdlp || !cdlu)
                return 0;
 
-       delete_customdata_layer(C, ob, cdl);
+       delete_customdata_layer(C, ob, cdlp);
+       delete_customdata_layer(C, ob, cdlu);
+       
        DAG_id_tag_update(&me->id, 0);
        WM_event_add_notifier(C, NC_GEOM|ND_DATA, me);
 
@@ -236,56 +394,67 @@ int ED_mesh_uv_texture_remove(bContext *C, Object *ob, Mesh *me)
 
 int ED_mesh_color_add(bContext *C, Scene *UNUSED(scene), Object *UNUSED(ob), Mesh *me, const char *name, int active_set)
 {
-       EditMesh *em;
+       BMEditMesh *em;
        int layernum;
 
-       if(me->edit_mesh) {
-               em= me->edit_mesh;
+       if (me->edit_btmesh) {
+               em= me->edit_btmesh;
 
-               layernum= CustomData_number_of_layers(&em->fdata, CD_MCOL);
-               if(layernum >= MAX_MCOL)
-                       return 0;
+               layernum= CustomData_number_of_layers(&em->bm->ldata, CD_MLOOPCOL);
+               if (layernum >= MAX_MCOL) {
+                       return -1;
+               }
 
-               EM_add_data_layer(em, &em->fdata, CD_MCOL, name);
+               BM_data_layer_add(em->bm, &em->bm->pdata, CD_MLOOPCOL);
+               CustomData_set_layer_active(&em->bm->ldata, CD_MLOOPCOL, layernum);
 
-               if(layernum) /* copy data from active vertex color layer */
+               /* copy data from active vertex color layer */
+               if (layernum) {
                        copy_editface_active_customdata(em, CD_MCOL, layernum);
+               }
 
-               if(active_set || layernum==0)
-                       CustomData_set_layer_active(&em->fdata, CD_MCOL, layernum);
+               if (active_set || layernum == 0) {
+                       CustomData_set_layer_active(&em->bm->ldata, CD_MLOOPCOL, layernum);
+               }
        }
        else {
-               layernum= CustomData_number_of_layers(&me->fdata, CD_MCOL);
-               if(layernum >= MAX_MCOL)
-                       return 0;
+               layernum= CustomData_number_of_layers(&me->ldata, CD_MLOOPCOL);
+               if (layernum >= CD_MLOOPCOL) {
+                       return -1;
+               }
 
-               if(me->mcol)
+               if (me->mloopcol) {
+                       CustomData_add_layer_named(&me->ldata, CD_MLOOPCOL, CD_DUPLICATE, me->mloopcol, me->totloop, name);
                        CustomData_add_layer_named(&me->fdata, CD_MCOL, CD_DUPLICATE, me->mcol, me->totface, name);
-               else
+               }
+               else {
+                       CustomData_add_layer_named(&me->ldata, CD_MLOOPCOL, CD_DEFAULT, NULL, me->totloop, name);
                        CustomData_add_layer_named(&me->fdata, CD_MCOL, CD_DEFAULT, NULL, me->totface, name);
+               }
 
-               if(active_set || layernum==0)
+               if (active_set || layernum==0) {
+                       CustomData_set_layer_active(&me->ldata, CD_MLOOPCOL, layernum);
                        CustomData_set_layer_active(&me->fdata, CD_MCOL, layernum);
+               }
 
-               mesh_update_customdata_pointers(me);
+               mesh_update_customdata_pointers(me, TRUE);
        }
 
        DAG_id_tag_update(&me->id, 0);
        WM_event_add_notifier(C, NC_GEOM|ND_DATA, me);
 
-       return 1;
+       return layernum;
 }
 
 int ED_mesh_color_remove(bContext *C, Object *ob, Mesh *me)
 {
-       CustomData *data= (me->edit_mesh)? &me->edit_mesh->fdata: &me->fdata;
        CustomDataLayer *cdl;
        int index;
 
-       index= CustomData_get_active_layer_index(data, CD_MCOL);
-       cdl= (index == -1)? NULL: &data->layers[index];
+       index= CustomData_get_active_layer_index(&me->ldata, CD_MLOOPCOL);
+       cdl= (index == -1)? NULL: &me->ldata.layers[index];
 
-       if(!cdl)
+       if (!cdl)
                return 0;
 
        delete_customdata_layer(C, ob, cdl);
@@ -297,14 +466,13 @@ int ED_mesh_color_remove(bContext *C, Object *ob, Mesh *me)
 
 int ED_mesh_color_remove_named(bContext *C, Object *ob, Mesh *me, const char *name)
 {
-       CustomData *data= (me->edit_mesh)? &me->edit_mesh->fdata: &me->fdata;
        CustomDataLayer *cdl;
        int index;
 
-       index= CustomData_get_named_layer_index(data, CD_MCOL, name);
-       cdl= (index == -1)? NULL: &data->layers[index];
+       index= CustomData_get_named_layer_index(&me->ldata, CD_MLOOPCOL, name);
+       cdl= (index == -1)? NULL: &me->ldata.layers[index];
 
-       if(!cdl)
+       if (!cdl)
                return 0;
 
        delete_customdata_layer(C, ob, cdl);
@@ -323,12 +491,12 @@ static int layers_poll(bContext *C)
        return (ob && !ob->id.lib && ob->type==OB_MESH && data && !data->lib);
 }
 
-static int uv_texture_add_exec(bContext *C, wmOperator *UNUSED(op))
+static int mesh_uv_texture_add_exec(bContext *C, wmOperator *UNUSED(op))
 {
        Object *ob= ED_object_context(C);
        Mesh *me= ob->data;
 
-       if(!ED_mesh_uv_texture_add(C, me, NULL, TRUE))
+       if (ED_mesh_uv_texture_add(C, me, NULL, TRUE) == -1)
                return OPERATOR_CANCELLED;
 
        return OPERATOR_FINISHED;
@@ -337,16 +505,16 @@ static int uv_texture_add_exec(bContext *C, wmOperator *UNUSED(op))
 void MESH_OT_uv_texture_add(wmOperatorType *ot)
 {
        /* identifiers */
-       ot->name= "Add UV Map";
-       ot->description= "Add UV Map";
-       ot->idname= "MESH_OT_uv_texture_add";
+       ot->name = "Add UV Map";
+       ot->description = "Add UV Map";
+       ot->idname = "MESH_OT_uv_texture_add";
        
        /* api callbacks */
-       ot->poll= layers_poll;
-       ot->execuv_texture_add_exec;
+       ot->poll = layers_poll;
+       ot->exec = mesh_uv_texture_add_exec;
 
        /* flags */
-       ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
+       ot->flag = OPTYPE_REGISTER|OPTYPE_UNDO;
 }
 
 static int drop_named_image_invoke(bContext *C, wmOperator *op, wmEvent *event)
@@ -362,13 +530,13 @@ static int drop_named_image_invoke(bContext *C, wmOperator *op, wmEvent *event)
        char name[MAX_ID_NAME-2];
        
        /* Check context */
-       if(base==NULL || base->object->type!=OB_MESH) {
+       if (base==NULL || base->object->type!=OB_MESH) {
                BKE_report(op->reports, RPT_ERROR, "Not an Object or Mesh");
                return OPERATOR_CANCELLED;
        }
        
        /* check input variables */
-       if(RNA_struct_property_is_set(op->ptr, "filepath")) {
+       if (RNA_struct_property_is_set(op->ptr, "filepath")) {
                char path[FILE_MAX];
                
                RNA_string_get(op->ptr, "filepath", path);
@@ -379,34 +547,37 @@ static int drop_named_image_invoke(bContext *C, wmOperator *op, wmEvent *event)
                ima= (Image *)find_id("IM", name);
        }
        
-       if(!ima) {
+       if (!ima) {
                BKE_report(op->reports, RPT_ERROR, "Not an Image");
                return OPERATOR_CANCELLED;
        }
        
-       /* turn mesh in editmode */
-       /* BKE_mesh_get/end_editmesh: ED_uvedit_assign_image also calls this */
+       /* put mesh in editmode */
 
        obedit= base->object;
        me= obedit->data;
-       if(me->edit_mesh==NULL) {
-               make_editMesh(scene, obedit);
+       if (me->edit_btmesh==NULL) {
+               EDBM_MakeEditBMesh(scene->toolsettings, scene, obedit);
                exitmode= 1;
        }
-       if(me->edit_mesh==NULL)
+       if (me->edit_btmesh==NULL)
                return OPERATOR_CANCELLED;
        
        ED_uvedit_assign_image(bmain, scene, obedit, ima, NULL);
 
-       if(exitmode) {
-               load_editMesh(scene, obedit);
-               free_editMesh(me->edit_mesh);
-               MEM_freeN(me->edit_mesh);
-               me->edit_mesh= NULL;
+       if (exitmode) {
+               EDBM_LoadEditBMesh(scene, obedit);
+               EDBM_FreeEditBMesh(me->edit_btmesh);
+               MEM_freeN(me->edit_btmesh);
+               me->edit_btmesh= NULL;
+
+               /* load_editMesh free's pointers used by CustomData layers which might be used by DerivedMesh too,
+                * so signal to re-create DerivedMesh here (sergey) */
+               DAG_id_tag_update(&me->id, 0);
        }
 
        /* dummie drop support; ensure view shows a result :) */
-       if(v3d)
+       if (v3d)
                v3d->flag2 |= V3D_SOLID_TEX;
        
        WM_event_add_notifier(C, NC_GEOM|ND_DATA, obedit->data);
@@ -417,28 +588,28 @@ static int drop_named_image_invoke(bContext *C, wmOperator *op, wmEvent *event)
 void MESH_OT_drop_named_image(wmOperatorType *ot)
 {
        /* identifiers */
-       ot->name= "Assign Image to UV Map";
-       ot->description= "Assign Image to active UV Map, or create an UV Map";
-       ot->idname= "MESH_OT_drop_named_image";
+       ot->name = "Assign Image to UV Map";
+       ot->description = "Assign Image to active UV Map, or create an UV Map";
+       ot->idname = "MESH_OT_drop_named_image";
        
        /* api callbacks */
-       ot->poll= layers_poll;
-       ot->invoke= drop_named_image_invoke;
+       ot->poll = layers_poll;
+       ot->invoke = drop_named_image_invoke;
        
        /* flags */
-       ot->flag= OPTYPE_UNDO;
+       ot->flag = OPTYPE_UNDO;
        
        /* properties */
        RNA_def_string(ot->srna, "name", "Image", MAX_ID_NAME-2, "Name", "Image name to assign");
        RNA_def_string(ot->srna, "filepath", "Path", FILE_MAX, "Filepath", "Path to image file");
 }
 
-static int uv_texture_remove_exec(bContext *C, wmOperator *UNUSED(op))
+static int mesh_uv_texture_remove_exec(bContext *C, wmOperator *UNUSED(op))
 {
        Object *ob= ED_object_context(C);
        Mesh *me= ob->data;
 
-       if(!ED_mesh_uv_texture_remove(C, ob, me))
+       if (!ED_mesh_uv_texture_remove(C, ob, me))
                return OPERATOR_CANCELLED;
 
        return OPERATOR_FINISHED;
@@ -447,27 +618,27 @@ static int uv_texture_remove_exec(bContext *C, wmOperator *UNUSED(op))
 void MESH_OT_uv_texture_remove(wmOperatorType *ot)
 {
        /* identifiers */
-       ot->name= "Remove UV Map";
-       ot->description= "Remove UV Map";
-       ot->idname= "MESH_OT_uv_texture_remove";
+       ot->name = "Remove UV Map";
+       ot->description = "Remove UV Map";
+       ot->idname = "MESH_OT_uv_texture_remove";
        
        /* api callbacks */
-       ot->poll= layers_poll;
-       ot->execuv_texture_remove_exec;
+       ot->poll = layers_poll;
+       ot->exec = mesh_uv_texture_remove_exec;
 
        /* flags */
-       ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
+       ot->flag = OPTYPE_REGISTER|OPTYPE_UNDO;
 }
 
 /*********************** vertex color operators ************************/
 
-static int vertex_color_add_exec(bContext *C, wmOperator *UNUSED(op))
+static int mesh_vertex_color_add_exec(bContext *C, wmOperator *UNUSED(op))
 {
        Scene *scene= CTX_data_scene(C);
        Object *ob= ED_object_context(C);
        Mesh *me= ob->data;
 
-       if(!ED_mesh_color_add(C, scene, ob, me, NULL, TRUE))
+       if (ED_mesh_color_add(C, scene, ob, me, NULL, TRUE) == -1)
                return OPERATOR_CANCELLED;
 
        return OPERATOR_FINISHED;
@@ -476,24 +647,24 @@ static int vertex_color_add_exec(bContext *C, wmOperator *UNUSED(op))
 void MESH_OT_vertex_color_add(wmOperatorType *ot)
 {
        /* identifiers */
-       ot->name= "Add Vertex Color";
-       ot->description= "Add vertex color layer";
-       ot->idname= "MESH_OT_vertex_color_add";
+       ot->name = "Add Vertex Color";
+       ot->description = "Add vertex color layer";
+       ot->idname = "MESH_OT_vertex_color_add";
        
        /* api callbacks */
-       ot->poll= layers_poll;
-       ot->execvertex_color_add_exec;
+       ot->poll = layers_poll;
+       ot->exec = mesh_vertex_color_add_exec;
 
        /* flags */
-       ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
+       ot->flag = OPTYPE_REGISTER|OPTYPE_UNDO;
 }
 
-static int vertex_color_remove_exec(bContext *C, wmOperator *UNUSED(op))
+static int mesh_vertex_color_remove_exec(bContext *C, wmOperator *UNUSED(op))
 {
        Object *ob= ED_object_context(C);
        Mesh *me= ob->data;
 
-       if(!ED_mesh_color_remove(C, ob, me))
+       if (!ED_mesh_color_remove(C, ob, me))
                return OPERATOR_CANCELLED;
 
        return OPERATOR_FINISHED;
@@ -502,29 +673,32 @@ static int vertex_color_remove_exec(bContext *C, wmOperator *UNUSED(op))
 void MESH_OT_vertex_color_remove(wmOperatorType *ot)
 {
        /* identifiers */
-       ot->name= "Remove Vertex Color";
-       ot->description= "Remove vertex color layer";
-       ot->idname= "MESH_OT_vertex_color_remove";
+       ot->name = "Remove Vertex Color";
+       ot->description = "Remove vertex color layer";
+       ot->idname = "MESH_OT_vertex_color_remove";
        
        /* api callbacks */
-       ot->execvertex_color_remove_exec;
-       ot->poll= layers_poll;
+       ot->exec = mesh_vertex_color_remove_exec;
+       ot->poll = layers_poll;
 
        /* flags */
-       ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
+       ot->flag = OPTYPE_REGISTER|OPTYPE_UNDO;
 }
 
 /*********************** sticky operators ************************/
 
-static int sticky_add_exec(bContext *C, wmOperator *UNUSED(op))
+static int mesh_sticky_add_exec(bContext *C, wmOperator *UNUSED(op))
 {
        Scene *scene= CTX_data_scene(C);
        View3D *v3d= CTX_wm_view3d(C);
        Object *ob= ED_object_context(C);
        Mesh *me= ob->data;
 
-       /*if(me->msticky)
-               return OPERATOR_CANCELLED;*/
+       /* why is this commented out? */
+#if 0
+       if (me->msticky)
+               return OPERATOR_CANCELLED;
+#endif
 
        RE_make_sticky(scene, v3d);
 
@@ -537,24 +711,24 @@ static int sticky_add_exec(bContext *C, wmOperator *UNUSED(op))
 void MESH_OT_sticky_add(wmOperatorType *ot)
 {
        /* identifiers */
-       ot->name= "Add Sticky";
-       ot->description= "Add sticky UV texture layer";
-       ot->idname= "MESH_OT_sticky_add";
+       ot->name = "Add Sticky";
+       ot->description = "Add sticky UV texture layer";
+       ot->idname = "MESH_OT_sticky_add";
        
        /* api callbacks */
-       ot->poll= layers_poll;
-       ot->execsticky_add_exec;
+       ot->poll = layers_poll;
+       ot->exec = mesh_sticky_add_exec;
 
        /* flags */
-       ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
+       ot->flag = OPTYPE_REGISTER|OPTYPE_UNDO;
 }
 
-static int sticky_remove_exec(bContext *C, wmOperator *UNUSED(op))
+static int mesh_sticky_remove_exec(bContext *C, wmOperator *UNUSED(op))
 {
        Object *ob= ED_object_context(C);
        Mesh *me= ob->data;
 
-       if(!me->msticky)
+       if (!me->msticky)
                return OPERATOR_CANCELLED;
 
        CustomData_free_layer_active(&me->vdata, CD_MSTICKY, me->totvert);
@@ -569,26 +743,71 @@ static int sticky_remove_exec(bContext *C, wmOperator *UNUSED(op))
 void MESH_OT_sticky_remove(wmOperatorType *ot)
 {
        /* identifiers */
-       ot->name= "Remove Sticky";
-       ot->description= "Remove sticky UV texture layer";
-       ot->idname= "MESH_OT_sticky_remove";
+       ot->name = "Remove Sticky";
+       ot->description = "Remove sticky UV texture layer";
+       ot->idname = "MESH_OT_sticky_remove";
        
        /* api callbacks */
-       ot->poll= layers_poll;
-       ot->execsticky_remove_exec;
+       ot->poll = layers_poll;
+       ot->exec = mesh_sticky_remove_exec;
 
        /* flags */
-       ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
+       ot->flag = OPTYPE_REGISTER|OPTYPE_UNDO;
 }
 
 /************************** Add Geometry Layers *************************/
 
-void ED_mesh_update(Mesh *mesh, bContext *C, int calc_edges)
+void ED_mesh_update(Mesh *mesh, bContext *C, int calc_edges, int calc_tessface)
 {
-       if(calc_edges || (mesh->totface && mesh->totedge == 0))
+       int *polyindex = NULL;
+       float (*face_nors)[3];
+       int tessface_input = FALSE;
+
+       if (mesh->totface > 0 && mesh->totpoly == 0) {
+               convert_mfaces_to_mpolys(mesh);
+
+               /* would only be converting back again, don't bother */
+               tessface_input = TRUE;
+
+               /* it also happens that converting the faces calculates edges, skip this */
+               calc_edges = FALSE;
+       }
+
+       if (calc_edges || (mesh->totpoly && mesh->totedge == 0))
                BKE_mesh_calc_edges(mesh, calc_edges);
 
-       mesh_calc_normals(mesh->mvert, mesh->totvert, mesh->mface, mesh->totface, NULL);
+       if (calc_tessface) {
+               if (tessface_input == FALSE) {
+                       BKE_mesh_tessface_calc(mesh);
+               }
+       }
+       else {
+               /* default state is not to have tessface's so make sure this is the case */
+               BKE_mesh_tessface_clear(mesh);
+       }
+
+       /* note on this if/else - looks like these layers are not needed
+        * so rather then add poly-index layer and calculate normals for it
+        * calculate normals only for the mvert's. - campbell */
+#ifdef USE_BMESH_MPOLY_NORMALS
+       polyindex = CustomData_get_layer(&mesh->fdata, CD_POLYINDEX);
+       /* add a normals layer for tessellated faces, a tessface normal will
+        * contain the normal of the poly the face was tessellated from. */
+       face_nors = CustomData_add_layer(&mesh->fdata, CD_NORMAL, CD_CALLOC, NULL, mesh->totface);
+
+       mesh_calc_normals_mapping_ex(mesh->mvert, mesh->totvert,
+                                    mesh->mloop, mesh->mpoly,
+                                    mesh->totloop, mesh->totpoly,
+                                    NULL /* polyNors_r */,
+                                    mesh->mface, mesh->totface,
+                                    polyindex, face_nors, FALSE);
+#else
+       mesh_calc_normals(mesh->mvert, mesh->totvert,
+                         mesh->mloop, mesh->mpoly, mesh->totloop, mesh->totpoly,
+                         NULL);
+       (void)polyindex;
+       (void)face_nors;
+#endif
 
        DAG_id_tag_update(&mesh->id, 0);
        WM_event_add_notifier(C, NC_GEOM|ND_DATA, mesh);
@@ -600,24 +819,24 @@ static void mesh_add_verts(Mesh *mesh, int len)
        MVert *mvert;
        int i, totvert;
 
-       if(len == 0)
+       if (len == 0)
                return;
 
        totvert= mesh->totvert + len;
        CustomData_copy(&mesh->vdata, &vdata, CD_MASK_MESH, CD_DEFAULT, totvert);
        CustomData_copy_data(&mesh->vdata, &vdata, 0, 0, mesh->totvert);
 
-       if(!CustomData_has_layer(&vdata, CD_MVERT))
+       if (!CustomData_has_layer(&vdata, CD_MVERT))
                CustomData_add_layer(&vdata, CD_MVERT, CD_CALLOC, NULL, totvert);
 
        CustomData_free(&mesh->vdata, mesh->totvert);
        mesh->vdata= vdata;
-       mesh_update_customdata_pointers(mesh);
+       mesh_update_customdata_pointers(mesh, FALSE);
 
        /* scan the input list and insert the new vertices */
 
        mvert= &mesh->mvert[mesh->totvert];
-       for(i=0; i<len; i++, mvert++)
+       for (i=0; i<len; i++, mvert++)
                mvert->flag |= SELECT;
 
        /* set final vertex list size */
@@ -629,10 +848,10 @@ void ED_mesh_transform(Mesh *me, float *mat)
        int i;
        MVert *mvert= me->mvert;
 
-       for(i= 0; i < me->totvert; i++, mvert++)
+       for (i= 0; i < me->totvert; i++, mvert++)
                mul_m4_v3((float (*)[4])mat, mvert->co);
 
-       mesh_calc_normals(me->mvert, me->totvert, me->mface, me->totface, NULL);
+       mesh_calc_normals_mapping(me->mvert, me->totvert, me->mloop, me->mpoly, me->totloop, me->totpoly, NULL, NULL, 0, NULL, NULL);
 }
 
 static void mesh_add_edges(Mesh *mesh, int len)
@@ -641,7 +860,7 @@ static void mesh_add_edges(Mesh *mesh, int len)
        MEdge *medge;
        int i, totedge;
 
-       if(len == 0)
+       if (len == 0)
                return;
 
        totedge= mesh->totedge+len;
@@ -650,16 +869,16 @@ static void mesh_add_edges(Mesh *mesh, int len)
        CustomData_copy(&mesh->edata, &edata, CD_MASK_MESH, CD_DEFAULT, totedge);
        CustomData_copy_data(&mesh->edata, &edata, 0, 0, mesh->totedge);
 
-       if(!CustomData_has_layer(&edata, CD_MEDGE))
+       if (!CustomData_has_layer(&edata, CD_MEDGE))
                CustomData_add_layer(&edata, CD_MEDGE, CD_CALLOC, NULL, totedge);
 
        CustomData_free(&mesh->edata, mesh->totedge);
        mesh->edata= edata;
-       mesh_update_customdata_pointers(mesh);
+       mesh_update_customdata_pointers(mesh, FALSE); /* new edges don't change tessellation */
 
        /* set default flags */
        medge= &mesh->medge[mesh->totedge];
-       for(i=0; i<len; i++, medge++)
+       for (i=0; i<len; i++, medge++)
                medge->flag= ME_EDGEDRAW|ME_EDGERENDER|SELECT;
 
        mesh->totedge= totedge;
@@ -671,7 +890,7 @@ static void mesh_add_faces(Mesh *mesh, int len)
        MFace *mface;
        int i, totface;
 
-       if(len == 0)
+       if (len == 0)
                return;
 
        totface= mesh->totface + len;   /* new face count */
@@ -680,26 +899,80 @@ static void mesh_add_faces(Mesh *mesh, int len)
        CustomData_copy(&mesh->fdata, &fdata, CD_MASK_MESH, CD_DEFAULT, totface);
        CustomData_copy_data(&mesh->fdata, &fdata, 0, 0, mesh->totface);
 
-       if(!CustomData_has_layer(&fdata, CD_MFACE))
+       if (!CustomData_has_layer(&fdata, CD_MFACE))
                CustomData_add_layer(&fdata, CD_MFACE, CD_CALLOC, NULL, totface);
 
        CustomData_free(&mesh->fdata, mesh->totface);
        mesh->fdata= fdata;
-       mesh_update_customdata_pointers(mesh);
+       mesh_update_customdata_pointers(mesh, TRUE);
 
        /* set default flags */
        mface= &mesh->mface[mesh->totface];
-       for(i=0; i<len; i++, mface++)
+       for (i=0; i<len; i++, mface++)
                mface->flag= ME_FACE_SEL;
 
        mesh->totface= totface;
 }
 
+static void mesh_add_loops(Mesh *mesh, int len)
+{
+       CustomData ldata;
+       int totloop;
+
+       if (len == 0)
+               return;
+
+       totloop= mesh->totloop + len;   /* new face count */
+
+       /* update customdata */
+       CustomData_copy(&mesh->ldata, &ldata, CD_MASK_MESH, CD_DEFAULT, totloop);
+       CustomData_copy_data(&mesh->ldata, &ldata, 0, 0, mesh->totloop);
+
+       if (!CustomData_has_layer(&ldata, CD_MLOOP))
+               CustomData_add_layer(&ldata, CD_MLOOP, CD_CALLOC, NULL, totloop);
+
+       CustomData_free(&mesh->ldata, mesh->totloop);
+       mesh->ldata= ldata;
+       mesh_update_customdata_pointers(mesh, TRUE);
+
+       mesh->totloop= totloop;
+}
+
+static void mesh_add_polys(Mesh *mesh, int len)
+{
+       CustomData pdata;
+       MPoly *mpoly;
+       int i, totpoly;
+
+       if (len == 0)
+               return;
+
+       totpoly= mesh->totpoly + len;   /* new face count */
+
+       /* update customdata */
+       CustomData_copy(&mesh->pdata, &pdata, CD_MASK_MESH, CD_DEFAULT, totpoly);
+       CustomData_copy_data(&mesh->pdata, &pdata, 0, 0, mesh->totpoly);
+
+       if (!CustomData_has_layer(&pdata, CD_MPOLY))
+               CustomData_add_layer(&pdata, CD_MPOLY, CD_CALLOC, NULL, totpoly);
+
+       CustomData_free(&mesh->pdata, mesh->totpoly);
+       mesh->pdata= pdata;
+       mesh_update_customdata_pointers(mesh, TRUE);
+
+       /* set default flags */
+       mpoly= &mesh->mpoly[mesh->totpoly];
+       for (i=0; i<len; i++, mpoly++)
+               mpoly->flag= ME_FACE_SEL;
+
+       mesh->totpoly= totpoly;
+}
+
 static void mesh_remove_verts(Mesh *mesh, int len)
 {
        int totvert;
 
-       if(len == 0)
+       if (len == 0)
                return;
 
        totvert= mesh->totvert - len;
@@ -713,7 +986,7 @@ static void mesh_remove_edges(Mesh *mesh, int len)
 {
        int totedge;
 
-       if(len == 0)
+       if (len == 0)
                return;
 
        totedge= mesh->totedge - len;
@@ -726,7 +999,7 @@ static void mesh_remove_faces(Mesh *mesh, int len)
 {
        int totface;
 
-       if(len == 0)
+       if (len == 0)
                return;
 
        totface= mesh->totface - len;   /* new face count */
@@ -735,26 +1008,26 @@ static void mesh_remove_faces(Mesh *mesh, int len)
        mesh->totface= totface;
 }
 
-/*
+#if 0
 void ED_mesh_geometry_add(Mesh *mesh, ReportList *reports, int verts, int edges, int faces)
 {
-       if(mesh->edit_mesh) {
+       if (mesh->edit_btmesh) {
                BKE_report(reports, RPT_ERROR, "Can't add geometry in edit mode");
                return;
        }
 
-       if(verts)
+       if (verts)
                mesh_add_verts(mesh, verts);
-       if(edges)
+       if (edges)
                mesh_add_edges(mesh, edges);
-       if(faces)
+       if (faces)
                mesh_add_faces(mesh, faces);
 }
-*/
+#endif
 
 void ED_mesh_faces_add(Mesh *mesh, ReportList *reports, int count)
 {
-       if(mesh->edit_mesh) {
+       if (mesh->edit_btmesh) {
                BKE_report(reports, RPT_ERROR, "Can't add faces in edit mode");
                return;
        }
@@ -764,9 +1037,9 @@ void ED_mesh_faces_add(Mesh *mesh, ReportList *reports, int count)
 
 void ED_mesh_edges_add(Mesh *mesh, ReportList *reports, int count)
 {
-       if(mesh->edit_mesh) {
+       if (mesh->edit_btmesh) {
                BKE_report(reports, RPT_ERROR, "Can't add edges in edit mode");
-               return;
+                       return;
        }
 
        mesh_add_edges(mesh, count);
@@ -774,7 +1047,7 @@ void ED_mesh_edges_add(Mesh *mesh, ReportList *reports, int count)
 
 void ED_mesh_vertices_add(Mesh *mesh, ReportList *reports, int count)
 {
-       if(mesh->edit_mesh) {
+       if (mesh->edit_btmesh) {
                BKE_report(reports, RPT_ERROR, "Can't add vertices in edit mode");
                return;
        }
@@ -784,11 +1057,11 @@ void ED_mesh_vertices_add(Mesh *mesh, ReportList *reports, int count)
 
 void ED_mesh_faces_remove(Mesh *mesh, ReportList *reports, int count)
 {
-       if(mesh->edit_mesh) {
+       if (mesh->edit_btmesh) {
                BKE_report(reports, RPT_ERROR, "Can't remove faces in edit mode");
                return;
        }
-       else if(count > mesh->totface) {
+       else if (count > mesh->totface) {
                BKE_report(reports, RPT_ERROR, "Can't remove more faces than the mesh contains");
                return;
        }
@@ -798,11 +1071,11 @@ void ED_mesh_faces_remove(Mesh *mesh, ReportList *reports, int count)
 
 void ED_mesh_edges_remove(Mesh *mesh, ReportList *reports, int count)
 {
-       if(mesh->edit_mesh) {
+       if (mesh->edit_btmesh) {
                BKE_report(reports, RPT_ERROR, "Can't remove edges in edit mode");
                return;
        }
-       else if(count > mesh->totedge) {
+       else if (count > mesh->totedge) {
                BKE_report(reports, RPT_ERROR, "Can't remove more edges than the mesh contains");
                return;
        }
@@ -812,11 +1085,11 @@ void ED_mesh_edges_remove(Mesh *mesh, ReportList *reports, int count)
 
 void ED_mesh_vertices_remove(Mesh *mesh, ReportList *reports, int count)
 {
-       if(mesh->edit_mesh) {
+       if (mesh->edit_btmesh) {
                BKE_report(reports, RPT_ERROR, "Can't remove vertices in edit mode");
                return;
        }
-       else if(count > mesh->totvert) {
+       else if (count > mesh->totvert) {
                BKE_report(reports, RPT_ERROR, "Can't remove more vertices than the mesh contains");
                return;
        }
@@ -824,7 +1097,35 @@ void ED_mesh_vertices_remove(Mesh *mesh, ReportList *reports, int count)
        mesh_remove_verts(mesh, count);
 }
 
-void ED_mesh_calc_normals(Mesh *me)
+void ED_mesh_loops_add(Mesh *mesh, ReportList *reports, int count)
+{
+       if (mesh->edit_btmesh) {
+               BKE_report(reports, RPT_ERROR, "Can't add loops in edit mode.");
+                       return;
+       }
+
+       mesh_add_loops(mesh, count);
+}
+
+void ED_mesh_polys_add(Mesh *mesh, ReportList *reports, int count)
+{
+       if (mesh->edit_btmesh) {
+               BKE_report(reports, RPT_ERROR, "Can't add polys in edit mode.");
+               return;
+       }
+
+       mesh_add_polys(mesh, count);
+}
+
+void ED_mesh_calc_normals(Mesh *mesh)
 {
-       mesh_calc_normals(me->mvert, me->totvert, me->mface, me->totface, NULL);
+#ifdef USE_BMESH_MPOLY_NORMALS
+       mesh_calc_normals_mapping_ex(mesh->mvert, mesh->totvert,
+                                    mesh->mloop, mesh->mpoly, mesh->totloop, mesh->totpoly,
+                                    NULL, NULL, 0, NULL, NULL, FALSE);
+#else
+       mesh_calc_normals(mesh->mvert, mesh->totvert,
+                         mesh->mloop, mesh->mpoly, mesh->totloop, mesh->totpoly,
+                         NULL);
+#endif
 }