svn merge ^/trunk/blender -r41226:41227 .
[blender.git] / source / blender / editors / object / object_vgroup.c
index 378e4a4..6280837 100644 (file)
@@ -41,6 +41,7 @@
 #include "DNA_curve_types.h"
 #include "DNA_lattice_types.h"
 #include "DNA_meshdata_types.h"
+#include "DNA_mesh_types.h"
 #include "DNA_modifier_types.h"
 #include "DNA_object_types.h"
 #include "DNA_object_force.h"
@@ -49,7 +50,7 @@
 
 #include "BLI_math.h"
 #include "BLI_blenlib.h"
-#include "BLI_editVert.h"
+#include "BLI_cellalloc.h"
 #include "BLI_utildefines.h"
 
 #include "BKE_context.h"
@@ -58,6 +59,7 @@
 #include "BKE_depsgraph.h"
 #include "BKE_global.h"
 #include "BKE_mesh.h"
+#include "BKE_tessmesh.h"
 #include "BKE_report.h"
 #include "BKE_DerivedMesh.h"
 
@@ -89,7 +91,7 @@ static Lattice *vgroup_edit_lattice(Object *ob)
 int ED_vgroup_object_is_edit_mode(Object *ob)
 {
        if(ob->type == OB_MESH)
-               return (((Mesh*)ob->data)->edit_mesh != NULL);
+               return (((Mesh*)ob->data)->edit_btmesh != NULL);
        else if(ob->type == OB_LATTICE)
                return (((Lattice*)ob->data)->editlatt != NULL);
 
@@ -169,23 +171,25 @@ static int ED_vgroup_give_parray(ID *id, MDeformVert ***dvert_arr, int *dvert_to
                        {
                                Mesh *me = (Mesh *)id;
 
-                               if(me->edit_mesh) {
-                                       EditMesh *em = me->edit_mesh;
-                                       EditVert *eve;
+                               if(me->edit_btmesh) {
+                                       BMEditMesh *em = me->edit_btmesh;
+                                       BMIter iter;
+                                       BMVert *eve;
                                        int i;
 
-                                       if (!CustomData_has_layer(&em->vdata, CD_MDEFORMVERT)) {
+                                       if (!CustomData_has_layer(&em->bm->vdata, CD_MDEFORMVERT)) {
                                                return 0;
                                        }
 
-                                       i= BLI_countlist(&em->verts);
+                                       i = em->bm->totvert;
 
                                        *dvert_arr= MEM_mallocN(sizeof(void*)*i, "vgroup parray from me");
                                        *dvert_tot = i;
 
                                        i = 0;
-                                       for (eve=em->verts.first; eve; eve=eve->next, i++) {
-                                               (*dvert_arr)[i] = CustomData_em_get(&em->vdata, eve->data, CD_MDEFORMVERT);
+                                       BM_ITER(eve, &iter, em->bm, BM_VERTS_OF_MESH, NULL) {
+                                               (*dvert_arr)[i] = CustomData_em_get(&em->bm->vdata, eve->head.data, CD_MDEFORMVERT);
+                                               i++;
                                        }
 
                                        return 1;
@@ -312,12 +316,12 @@ int ED_vgroup_copy_array(Object *ob, Object *ob_from)
 
        for(i=0; i<dvert_tot; i++, dvf++, dv++) {
                if((*dv)->dw)
-                       MEM_freeN((*dv)->dw);
+                       BLI_cellalloc_free((*dv)->dw);
 
                *(*dv)= *(*dvf);
 
                if((*dv)->dw)
-                       (*dv)->dw= MEM_dupallocN((*dv)->dw);
+                       (*dv)->dw= BLI_cellalloc_dupalloc((*dv)->dw);
        }
 
        MEM_freeN(dvert_array);
@@ -371,13 +375,13 @@ static void ED_vgroup_nr_vert_remove(Object *ob, int def_nr, int vertnum)
                         * deform weight, and reshuffle the others
                         */
                        if(dvert->totweight) {
-                               newdw = MEM_mallocN(sizeof(MDeformWeight)*(dvert->totweight), 
+                               newdw = BLI_cellalloc_malloc(sizeof(MDeformWeight)*(dvert->totweight), 
                                                                         "deformWeight");
                                if(dvert->dw){
                                        memcpy(newdw, dvert->dw, sizeof(MDeformWeight)*i);
                                        memcpy(newdw+i, dvert->dw+i+1, 
                                                        sizeof(MDeformWeight)*(dvert->totweight-i));
-                                       MEM_freeN(dvert->dw);
+                                       BLI_cellalloc_free(dvert->dw);
                                }
                                dvert->dw=newdw;
                        }
@@ -385,7 +389,7 @@ static void ED_vgroup_nr_vert_remove(Object *ob, int def_nr, int vertnum)
                         * left then just remove the deform weight
                         */
                        else {
-                               MEM_freeN(dvert->dw);
+                               BLI_cellalloc_free(dvert->dw);
                                dvert->dw = NULL;
                                break;
                        }
@@ -467,11 +471,11 @@ static void ED_vgroup_nr_vert_add(Object *ob, int def_nr, int vertnum, float wei
                /* if we are doing an additive assignment, then
                 * we need to create the deform weight
                 */
-               newdw = MEM_callocN(sizeof(MDeformWeight)*(dv->totweight+1), 
+               newdw = BLI_cellalloc_calloc(sizeof(MDeformWeight)*(dv->totweight+1), 
                                                         "deformWeight");
                if(dv->dw){
                        memcpy(newdw, dv->dw, sizeof(MDeformWeight)*dv->totweight);
-                       MEM_freeN(dv->dw);
+                       BLI_cellalloc_free(dv->dw);
                }
                dv->dw=newdw;
 
@@ -526,7 +530,7 @@ void ED_vgroup_vert_remove(Object *ob, bDeformGroup *dg, int vertnum)
 static float get_vert_def_nr(Object *ob, int def_nr, int vertnum)
 {
        MDeformVert *dvert= NULL;
-       EditVert *eve;
+       BMVert *eve;
        Mesh *me;
        int i;
 
@@ -534,12 +538,12 @@ static float get_vert_def_nr(Object *ob, int def_nr, int vertnum)
        if(ob->type==OB_MESH) {
                me= ob->data;
 
-               if(me->edit_mesh) {
-                       eve= BLI_findlink(&me->edit_mesh->verts, vertnum);
+               if(me->edit_btmesh) {
+                       eve= BMIter_AtIndex(me->edit_btmesh->bm, BM_VERTS_OF_MESH, NULL, vertnum);
                        if(!eve) {
                                return 0.0f;
                        }
-                       dvert= CustomData_em_get(&me->edit_mesh->vdata, eve->data, CD_MDEFORMVERT);
+                       dvert= CustomData_bmesh_get(&me->edit_btmesh->bm->vdata, eve->head.data, CD_MDEFORMVERT);
                        vertnum= 0;
                }
                else {
@@ -594,34 +598,32 @@ void ED_vgroup_select_by_name(Object *ob, const char *name)
 /* only in editmode */
 static void vgroup_select_verts(Object *ob, int select)
 {
-       EditVert *eve;
+       BMVert *eve;
        MDeformVert *dvert;
        int i;
 
        if(ob->type == OB_MESH) {
                Mesh *me= ob->data;
-               EditMesh *em = BKE_mesh_get_editmesh(me);
+               BMEditMesh *em = me->edit_btmesh;
+               BMIter iter;
 
-               for(eve=em->verts.first; eve; eve=eve->next){
-                       dvert= CustomData_em_get(&em->vdata, eve->data, CD_MDEFORMVERT);
+               BM_ITER(eve, &iter, em->bm, BM_VERTS_OF_MESH, NULL) {
+                       dvert= CustomData_bmesh_get(&em->bm->vdata, eve->head.data, CD_MDEFORMVERT);
 
                        if(dvert && dvert->totweight){
                                for(i=0; i<dvert->totweight; i++){
                                        if(dvert->dw[i].def_nr == (ob->actdef-1)){
-                                               if(!eve->h) {
-                                                       if(select) eve->f |= SELECT;
-                                                       else eve->f &= ~SELECT;
+                                               if (!BM_TestHFlag(eve, BM_HIDDEN)) {
+                                                       BM_Select(em->bm, eve, select);
+                                                       break;
                                                }
-                                               break;
                                        }
                                }
                        }
                }
                /* this has to be called, because this function operates on vertices only */
-               if(select) EM_select_flush(em); // vertices to edges/faces
-               else EM_deselect_flush(em);
-
-               BKE_mesh_end_editmesh(me, em);
+               if(select) EDBM_selectmode_flush(em);   // vertices to edges/faces
+               else EDBM_deselect_flush(em);
        }
        else if(ob->type == OB_LATTICE) {
                Lattice *lt= vgroup_edit_lattice(ob);
@@ -1363,12 +1365,12 @@ static void vgroup_invert(Object *ob, int auto_assign, int auto_remove)
 
 static void vgroup_blend(Object *ob)
 {
+       BMEditMesh *em= ((Mesh *)ob->data)->edit_btmesh;
        bDeformGroup *dg;
        MDeformWeight *dw;
        MDeformVert *dvert_array=NULL, *dvert;
        int i, def_nr, dvert_tot=0;
 
-       EditMesh *em= BKE_mesh_get_editmesh(((Mesh *)ob->data));
        // ED_vgroup_give_array(ob->data, &dvert_array, &dvert_tot);
 
        if(em==NULL)
@@ -1377,46 +1379,47 @@ static void vgroup_blend(Object *ob)
        dg = BLI_findlink(&ob->defbase, (ob->actdef-1));
 
        if(dg) {
-               int sel1, sel2;
-               int i1, i2;
-
-               EditEdge *eed;
-               EditVert *eve;
+               BMEdge *eed;
+               BMVert *eve;
+               BMIter iter;
                float *vg_weights;
                float *vg_users;
+               int sel1, sel2;
+               int i1, i2;
 
                def_nr= ob->actdef-1;
 
                i= 0;
-               for(eve= em->verts.first; eve; eve= eve->next)
-                       eve->tmp.l= i++;
-
+               BM_ITER(eve, &iter, em->bm, BM_VERTS_OF_MESH, NULL) {
+                       BM_SetIndex(eve, i);
+                       i++;
+               }
                dvert_tot= i;
 
                vg_weights= MEM_callocN(sizeof(float)*dvert_tot, "vgroup_blend_f");
                vg_users= MEM_callocN(sizeof(int)*dvert_tot, "vgroup_blend_i");
 
-               for(eed= em->edges.first; eed; eed= eed->next) {
-                       sel1= eed->v1->f & SELECT;
-                       sel2= eed->v2->f & SELECT;
+               BM_ITER(eed, &iter, em->bm, BM_EDGES_OF_MESH, NULL) {
+                       sel1= BM_TestHFlag(eed->v1, BM_SELECT);
+                       sel2= BM_TestHFlag(eed->v2, BM_SELECT);
 
                        if(sel1 != sel2) {
                                /* i1 is always the selected one */
                                if(sel1==TRUE && sel2==FALSE) {
-                                       i1= eed->v1->tmp.l;
-                                       i2= eed->v2->tmp.l;
+                                       i1= BM_GetIndex(eed->v1);
+                                       i2= BM_GetIndex(eed->v2);
                                        eve= eed->v2;
                                }
                                else {
-                                       i2= eed->v1->tmp.l;
-                                       i1= eed->v2->tmp.l;
+                                       i2= BM_GetIndex(eed->v1);
+                                       i1= BM_GetIndex(eed->v2);
                                        eve= eed->v1;
                                }
 
                                vg_users[i1]++;
 
                                /* TODO, we may want object mode blending */
-                               if(em)  dvert= CustomData_em_get(&em->vdata, eve->data, CD_MDEFORMVERT);
+                               if(em)  dvert= CustomData_bmesh_get(&em->bm->vdata, eve->head.data, CD_MDEFORMVERT);
                                else    dvert= dvert_array+i2;
 
                                dw= defvert_find_index(dvert, def_nr);
@@ -1428,10 +1431,10 @@ static void vgroup_blend(Object *ob)
                }
 
                i= 0;
-               for(eve= em->verts.first; eve; eve= eve->next) {
-                       if(eve->f & SELECT && vg_users[i] > 0) {
+               BM_ITER(eve, &iter, em->bm, BM_VERTS_OF_MESH, NULL) {
+                       if(BM_TestHFlag(eve, BM_SELECT) && vg_users[i] > 0) {
                                /* TODO, we may want object mode blending */
-                               if(em)  dvert= CustomData_em_get(&em->vdata, eve->data, CD_MDEFORMVERT);
+                               if(em)  dvert= CustomData_bmesh_get(&em->bm->vdata, eve->head.data, CD_MDEFORMVERT);
                                else    dvert= dvert_array+i;
 
                                dw= defvert_verify_index(dvert, def_nr);
@@ -1559,7 +1562,10 @@ void ED_vgroup_mirror(Object *ob, const short mirror_weights, const short flip_v
 {
 #define VGROUP_MIRR_OP dvert_mirror_op(dvert, dvert_mirr, sel, sel_mirr, flip_map, mirror_weights, flip_vgroups)
 
+#if 0
        EditVert *eve, *eve_mirr;
+#endif
+
        MDeformVert *dvert, *dvert_mirr;
        short sel, sel_mirr;
        int     *flip_map;
@@ -1571,6 +1577,11 @@ void ED_vgroup_mirror(Object *ob, const short mirror_weights, const short flip_v
 
        /* only the active group */
        if(ob->type == OB_MESH) {
+#if 1 //BMESH_TODO
+               (void)dvert;
+               (void)dvert_mirr;
+               (void)flip_map;
+#else
                Mesh *me= ob->data;
                EditMesh *em = BKE_mesh_get_editmesh(me);
 
@@ -1601,6 +1612,7 @@ void ED_vgroup_mirror(Object *ob, const short mirror_weights, const short flip_v
                }
 
                BKE_mesh_end_editmesh(me, em);
+#endif // BMESH_TODO
        }
        else if (ob->type == OB_LATTICE) {
                Lattice *lt= ob->data;
@@ -1749,7 +1761,7 @@ static void vgroup_delete_object_mode(Object *ob, bDeformGroup *dg)
 /* removes from active defgroup, if allverts==0 only selected vertices */
 static void vgroup_active_remove_verts(Object *ob, const int allverts, bDeformGroup *dg)
 {
-       EditVert *eve;
+       BMVert *eve;
        MDeformVert *dvert;
        MDeformWeight *newdw;
        bDeformGroup *eg;
@@ -1757,29 +1769,30 @@ static void vgroup_active_remove_verts(Object *ob, const int allverts, bDeformGr
 
        if(ob->type == OB_MESH) {
                Mesh *me= ob->data;
-               EditMesh *em = BKE_mesh_get_editmesh(me);
+               BMEditMesh *em = me->edit_btmesh;
+               BMIter iter;
 
-               for(eve=em->verts.first; eve; eve=eve->next){
-                       dvert= CustomData_em_get(&em->vdata, eve->data, CD_MDEFORMVERT);
+               BM_ITER(eve, &iter, em->bm, BM_VERTS_OF_MESH, NULL) {
+                       dvert= CustomData_bmesh_get(&em->bm->vdata, eve->head.data, CD_MDEFORMVERT);
                
-                       if(dvert && dvert->dw && ((eve->f & SELECT) || allverts)){
+                       if(dvert && dvert->dw && (BM_TestHFlag(eve, BM_SELECT) || allverts)){
                                for(i=0; i<dvert->totweight; i++){
                                        /* Find group */
                                        eg = BLI_findlink(&ob->defbase, dvert->dw[i].def_nr);
                                        if(eg == dg){
                                                dvert->totweight--;
-                                               if(dvert->totweight){
-                                                       newdw = MEM_mallocN(sizeof(MDeformWeight)*(dvert->totweight), "deformWeight");
+                                               if (dvert->totweight){
+                                                       newdw = BLI_cellalloc_malloc (sizeof(MDeformWeight)*(dvert->totweight), "deformWeight");
                                                        
                                                        if(dvert->dw){
                                                                memcpy(newdw, dvert->dw, sizeof(MDeformWeight)*i);
                                                                memcpy(newdw+i, dvert->dw+i+1, sizeof(MDeformWeight)*(dvert->totweight-i));
-                                                               MEM_freeN(dvert->dw);
+                                                               BLI_cellalloc_free(dvert->dw);
                                                        }
                                                        dvert->dw=newdw;
                                                }
                                                else{
-                                                       MEM_freeN(dvert->dw);
+                                                       BLI_cellalloc_free (dvert->dw);
                                                        dvert->dw=NULL;
                                                        break;
                                                }
@@ -1787,7 +1800,6 @@ static void vgroup_active_remove_verts(Object *ob, const int allverts, bDeformGr
                                }
                        }
                }
-               BKE_mesh_end_editmesh(me, em);
        }
        else if(ob->type == OB_LATTICE) {
                Lattice *lt= vgroup_edit_lattice(ob);
@@ -1817,19 +1829,19 @@ static void vgroup_delete_edit_mode(Object *ob, bDeformGroup *dg)
        /* Make sure that any verts with higher indices are adjusted accordingly */
        if(ob->type==OB_MESH) {
                Mesh *me= ob->data;
-               EditMesh *em = BKE_mesh_get_editmesh(me);
-               EditVert *eve;
+               BMEditMesh *em = me->edit_btmesh;
+               BMIter iter;
+               BMVert *eve;
                MDeformVert *dvert;
                
-               for(eve=em->verts.first; eve; eve=eve->next){
-                       dvert= CustomData_em_get(&em->vdata, eve->data, CD_MDEFORMVERT);
+               BM_ITER(eve, &iter, em->bm, BM_VERTS_OF_MESH, NULL) {
+                       dvert= CustomData_bmesh_get(&em->bm->vdata, eve->head.data, CD_MDEFORMVERT);
 
                        if(dvert)
                                for(i=0; i<dvert->totweight; i++)
                                        if(dvert->dw[i].def_nr > dg_index)
                                                dvert->dw[i].def_nr--;
                }
-               BKE_mesh_end_editmesh(me, em);
        }
        else if(ob->type==OB_LATTICE) {
                Lattice *lt= vgroup_edit_lattice(ob);
@@ -1869,7 +1881,7 @@ static void vgroup_delete_edit_mode(Object *ob, bDeformGroup *dg)
                else if(ob->type==OB_LATTICE) {
                        Lattice *lt= vgroup_edit_lattice(ob);
                        if(lt->dvert) {
-                               MEM_freeN(lt->dvert);
+                               BLI_cellalloc_free(lt->dvert);
                                lt->dvert= NULL;
                        }
                }
@@ -1879,7 +1891,7 @@ static void vgroup_delete_edit_mode(Object *ob, bDeformGroup *dg)
 static int vgroup_object_in_edit_mode(Object *ob)
 {
        if(ob->type == OB_MESH)
-               return (((Mesh*)ob->data)->edit_mesh != NULL);
+               return (((Mesh*)ob->data)->edit_btmesh != NULL);
        else if(ob->type == OB_LATTICE)
                return (((Lattice*)ob->data)->editlatt != NULL);
        
@@ -1909,7 +1921,7 @@ static void vgroup_delete_all(Object *ob)
        else if(ob->type==OB_LATTICE) {
                Lattice *lt= vgroup_edit_lattice(ob);
                if(lt->dvert) {
-                       MEM_freeN(lt->dvert);
+                       BLI_cellalloc_free(lt->dvert);
                        lt->dvert= NULL;
                }
        }
@@ -1924,7 +1936,7 @@ static void vgroup_delete_all(Object *ob)
 /* only in editmode */
 static void vgroup_assign_verts(Object *ob, float weight)
 {
-       EditVert *eve;
+       BMVert *eve;
        bDeformGroup *dg, *eg;
        MDeformWeight *newdw;
        MDeformVert *dvert;
@@ -1936,16 +1948,17 @@ static void vgroup_assign_verts(Object *ob, float weight)
 
        if(ob->type == OB_MESH) {
                Mesh *me= ob->data;
-               EditMesh *em = BKE_mesh_get_editmesh(me);
+               BMEditMesh *em = me->edit_btmesh;
+               BMIter iter;
 
-               if(!CustomData_has_layer(&em->vdata, CD_MDEFORMVERT))
-                       EM_add_data_layer(em, &em->vdata, CD_MDEFORMVERT, NULL);
+               if(!CustomData_has_layer(&em->bm->vdata, CD_MDEFORMVERT))
+                       BM_add_data_layer(em->bm, &em->bm->vdata, CD_MDEFORMVERT);
 
                /* Go through the list of editverts and assign them */
-               for(eve=em->verts.first; eve; eve=eve->next){
-                       dvert= CustomData_em_get(&em->vdata, eve->data, CD_MDEFORMVERT);
+               BM_ITER(eve, &iter, em->bm, BM_VERTS_OF_MESH, NULL) {
+                       dvert= CustomData_bmesh_get(&em->bm->vdata, eve->head.data, CD_MDEFORMVERT);
 
-                       if(dvert && (eve->f & SELECT)){
+                       if(dvert && BM_TestHFlag(eve, BM_SELECT)) {
                                /* See if this vert already has a reference to this group */
                                /*              If so: Change its weight */
                                done=0;
@@ -1960,10 +1973,10 @@ static void vgroup_assign_verts(Object *ob, float weight)
                                 }
                                /*              If not: Add the group and set its weight */
                                if(!done){
-                                       newdw = MEM_callocN(sizeof(MDeformWeight)*(dvert->totweight+1), "deformWeight");
+                                       newdw = BLI_cellalloc_calloc(sizeof(MDeformWeight)*(dvert->totweight+1), "deformWeight");
                                        if(dvert->dw){
                                                memcpy(newdw, dvert->dw, sizeof(MDeformWeight)*dvert->totweight);
-                                               MEM_freeN(dvert->dw);
+                                               BLI_cellalloc_free(dvert->dw);
                                        }
                                        dvert->dw=newdw;
 
@@ -1975,7 +1988,6 @@ static void vgroup_assign_verts(Object *ob, float weight)
                                }
                        }
                }
-               BKE_mesh_end_editmesh(me, em);
        }
        else if(ob->type == OB_LATTICE) {
                Lattice *lt= vgroup_edit_lattice(ob);
@@ -2720,11 +2732,12 @@ static int vgroup_do_remap(Object *ob, char *name_array, wmOperator *op)
 
        if(ob->mode == OB_MODE_EDIT) {
                if(ob->type==OB_MESH) {
-                       EditMesh *em = BKE_mesh_get_editmesh(ob->data);
-                       EditVert *eve;
+                       BMEditMesh *em = ((Mesh*)ob->data)->edit_btmesh;
+                       BMIter iter;
+                       BMVert *eve;
 
-                       for(eve=em->verts.first; eve; eve=eve->next){
-                               dvert= CustomData_em_get(&em->vdata, eve->data, CD_MDEFORMVERT);
+                       BM_ITER(eve, &iter, em->bm, BM_VERTS_OF_MESH, NULL) {
+                               dvert= CustomData_bmesh_get(&em->bm->vdata, eve->head.data, CD_MDEFORMVERT);
                                if(dvert && dvert->totweight){
                                        defvert_remap(dvert, sort_map);
                                }