merge with trunk at r31523
[blender.git] / source / blender / editors / mesh / meshtools.c
index a08e08c25c0b07f63415736c9ab7c293e5765070..35e1a0975e51ce03d1e7662660322e2ba62c37f3 100644 (file)
@@ -44,7 +44,6 @@
 #include "DNA_meshdata_types.h"
 #include "DNA_object_types.h"
 #include "DNA_scene_types.h"
-#include "DNA_view3d_types.h"
 
 #include "BLI_math.h"
 #include "BLI_blenlib.h"
 #include "BLI_threads.h"
 
 
-#include "BKE_blender.h"
 #include "BKE_context.h"
 #include "BKE_depsgraph.h"
+#include "BKE_deform.h"
 #include "BKE_DerivedMesh.h"
-#include "BKE_customdata.h"
-#include "BKE_global.h"
-#include "BKE_image.h"
 #include "BKE_key.h"
 #include "BKE_library.h"
 #include "BKE_main.h"
 #include "BKE_mesh.h"
 #include "BKE_material.h"
-#include "BKE_object.h"
-#include "BKE_utildefines.h"
 #include "BKE_report.h"
+#include "BKE_tessmesh.h"
 
 #include "BLO_sys_types.h" // for intptr_t support
 
 /* own include */
 #include "mesh_intern.h"
 
-/* XXX */
-static int pupmenu(const char *dummy) {return 0;}
-/* XXX */
-
 
 /* * ********************** no editmode!!! *********** */
 
@@ -96,6 +87,7 @@ return 0 if no join is made (error) and 1 of the join is done */
 
 int join_mesh_exec(bContext *C, wmOperator *op)
 {
+       Main *bmain= CTX_data_main(C);
        Scene *scene= CTX_data_scene(C);
        Object *ob= CTX_data_active_object(C);
        Material **matar, *ma;
@@ -103,15 +95,17 @@ int join_mesh_exec(bContext *C, wmOperator *op)
        MVert *mvert, *mv, *mvertmain;
        MEdge *medge = NULL, *medgemain;
        MFace *mface = NULL, *mfacemain;
+       MPoly *mpoly = NULL, *mpolymain;
+       MLoop *mloop = NULL, *mloopmain;
        Key *key, *nkey=NULL;
        KeyBlock *kb, *okb, *kbn;
        float imat[4][4], cmat[4][4], *fp1, *fp2, curpos;
        int a, b, totcol, totmat=0, totedge=0, totvert=0, totface=0, ok=0;
-       int vertofs, *matmap=NULL;
-       int     i, j, index, haskey=0, edgeofs, faceofs;
+       int totloop=0, totpoly=0, vertofs, *matmap=NULL;
+       int i, j, index, haskey=0, edgeofs, faceofs, loopofs, polyofs;
        bDeformGroup *dg, *odg;
        MDeformVert *dvert;
-       CustomData vdata, edata, fdata;
+       CustomData vdata, edata, fdata, ldata, pdata;
 
        if(scene->obedit)
                return OPERATOR_CANCELLED;
@@ -128,6 +122,8 @@ int join_mesh_exec(bContext *C, wmOperator *op)
                        totvert+= me->totvert;
                        totedge+= me->totedge;
                        totface+= me->totface;
+                       totloop+= me->totloop;
+                       totpoly+= me->totpoly;
                        totmat+= base->object->totcol;
                        
                        if(base->object == ob)
@@ -194,12 +190,7 @@ int join_mesh_exec(bContext *C, wmOperator *op)
                        /* Join this object's vertex groups to the base one's */
                        for(dg=base->object->defbase.first; dg; dg=dg->next) {
                                /* See if this group exists in the object (if it doesn't, add it to the end) */
-                               for(odg=ob->defbase.first; odg; odg=odg->next) {
-                                       if(!strcmp(odg->name, dg->name)) {
-                                               break;
-                                       }
-                               }
-                               if(!odg) {
+                               if(!defgroup_find_name(ob, dg->name)) {
                                        odg = MEM_callocN(sizeof(bDeformGroup), "join deformGroup");
                                        memcpy(odg, dg, sizeof(bDeformGroup));
                                        BLI_addtail(&ob->defbase, odg);
@@ -275,18 +266,26 @@ int join_mesh_exec(bContext *C, wmOperator *op)
        memset(&vdata, 0, sizeof(vdata));
        memset(&edata, 0, sizeof(edata));
        memset(&fdata, 0, sizeof(fdata));
+       memset(&ldata, 0, sizeof(ldata));
+       memset(&pdata, 0, sizeof(pdata));
        
        mvert= CustomData_add_layer(&vdata, CD_MVERT, CD_CALLOC, NULL, totvert);
        medge= CustomData_add_layer(&edata, CD_MEDGE, CD_CALLOC, NULL, totedge);
        mface= CustomData_add_layer(&fdata, CD_MFACE, CD_CALLOC, NULL, totface);
+       mloop= CustomData_add_layer(&ldata, CD_MLOOP, CD_CALLOC, NULL, totloop);
+       mpoly= CustomData_add_layer(&pdata, CD_MPOLY, CD_CALLOC, NULL, totpoly);
        
        mvertmain= mvert;
        medgemain= medge;
        mfacemain= mface;
+       mloopmain = mloop;
+       mpolymain = mpoly;
        
        vertofs= 0;
        edgeofs= 0;
        faceofs= 0;
+       loopofs= 0;
+       polyofs= 0;
        
        /* inverse transform for all selected meshes in this object */
        invert_m4_m4(imat, ob->obmat);
@@ -439,7 +438,43 @@ int join_mesh_exec(bContext *C, wmOperator *op)
                                
                                edgeofs += me->totedge;
                        }
+
+                       if (me->totloop) {
+                               CustomData_merge(&me->ldata, &ldata, CD_MASK_MESH, CD_DEFAULT, totloop);
+                               CustomData_copy_data(&me->ldata, &ldata, 0, loopofs, me->totloop);
+                               
+                               for(a=0; a<me->totloop; a++, mloop++) {
+                                       mloop->v += vertofs;
+                                       mloop->e += edgeofs;
+                               }
+                               
+                               loopofs += me->totloop;
+                       }
                        
+                       if(me->totpoly) {
+                               /* make mapping for materials */
+                               for(a=1; a<=base->object->totcol; a++) {
+                                       ma= give_current_material(base->object, a);
+
+                                       for(b=0; b<totcol; b++) {
+                                               if(ma == matar[b]) {
+                                                       matmap[a-1]= b;
+                                                       break;
+                                               }
+                                       }
+                               }
+                               
+                               CustomData_merge(&me->pdata, &pdata, CD_MASK_MESH, CD_DEFAULT, totpoly);
+                               CustomData_copy_data(&me->pdata, &pdata, 0, polyofs, me->totpoly);
+                               
+                               for(a=0; a<me->totpoly; a++, mpoly++) {
+                                       mpoly->loopstart += loopofs;
+                                       mpoly->mat_nr= matmap[(int)mpoly->mat_nr];
+                               }
+                               
+                               polyofs += me->totface;
+                       }
+
                        /* vertofs is used to help newly added verts be reattached to their edge/face 
                         * (cannot be set earlier, or else reattaching goes wrong)
                         */
@@ -447,7 +482,7 @@ int join_mesh_exec(bContext *C, wmOperator *op)
                        
                        /* free base, now that data is merged */
                        if(base->object != ob)
-                               ED_base_object_free_and_unlink(scene, base);
+                               ED_base_object_free_and_unlink(bmain, scene, base);
                }
        }
        CTX_DATA_END;
@@ -458,14 +493,20 @@ int join_mesh_exec(bContext *C, wmOperator *op)
        CustomData_free(&me->vdata, me->totvert);
        CustomData_free(&me->edata, me->totedge);
        CustomData_free(&me->fdata, me->totface);
+       CustomData_free(&me->ldata, me->totloop);
+       CustomData_free(&me->pdata, me->totpoly);
 
        me->totvert= totvert;
        me->totedge= totedge;
        me->totface= totface;
+       me->totloop= totloop;
+       me->totpoly= totpoly;
        
        me->vdata= vdata;
        me->edata= edata;
        me->fdata= fdata;
+       me->ldata= ldata;
+       me->pdata= pdata;
 
        mesh_update_customdata_pointers(me);
        
@@ -507,17 +548,17 @@ int join_mesh_exec(bContext *C, wmOperator *op)
                /* free it's ipo too - both are not actually freed from memory yet as ID-blocks */
                if(nkey->ipo) {
                        free_ipo(nkey->ipo);
-                       BLI_remlink(&G.main->ipo, nkey->ipo);
+                       BLI_remlink(&bmain->ipo, nkey->ipo);
                        MEM_freeN(nkey->ipo);
                }
 #endif
                
                free_key(nkey);
-               BLI_remlink(&G.main->key, nkey);
+               BLI_remlink(&bmain->key, nkey);
                MEM_freeN(nkey);
        }
        
-       DAG_scene_sort(scene);  // removed objects, need to rebuild dag before editmode call
+       DAG_scene_sort(bmain, scene);   // removed objects, need to rebuild dag before editmode call
        
        ED_object_enter_editmode(C, EM_WAITCURSOR);
        ED_object_exit_editmode(C, EM_FREEDATA|EM_WAITCURSOR|EM_DO_UNDO);
@@ -601,153 +642,6 @@ int join_mesh_shapes_exec(bContext *C, wmOperator *op)
        return OPERATOR_FINISHED;
 }
 
-/* ********************** SORT FACES ******************* */
-
-static void permutate(void *list, int num, int size, int *index)
-{
-       void *buf;
-       int len;
-       int i;
-
-       len = num * size;
-
-       buf = MEM_mallocN(len, "permutate");
-       memcpy(buf, list, len);
-       
-       for (i = 0; i < num; i++) {
-               memcpy((char *)list + (i * size), (char *)buf + (index[i] * size), size);
-       }
-       MEM_freeN(buf);
-}
-
-/* sort faces on view axis */
-static float *face_sort_floats;
-static int float_sort(const void *v1, const void *v2)
-{
-       float x1, x2;
-       
-       x1 = face_sort_floats[((int *) v1)[0]];
-       x2 = face_sort_floats[((int *) v2)[0]];
-       
-       if( x1 > x2 ) return 1;
-       else if( x1 < x2 ) return -1;
-       return 0;
-}
-
-
-void sort_faces(Scene *scene, View3D *v3d)
-{
-       RegionView3D *rv3d= NULL; // get from context 
-       Object *ob= OBACT;
-       Mesh *me;
-       CustomDataLayer *layer;
-       int i, *index;
-       short event;
-       float reverse = 1;
-       int ctrl= 0;    // XXX
-       
-       if(!ob) return;
-       if(scene->obedit) return;
-       if(ob->type!=OB_MESH) return;
-       if (!v3d) return;
-       
-       me= ob->data;
-       if(me->totface==0) return;
-       
-       event = pupmenu(
-       "Sort Faces (Ctrl to reverse)%t|"
-       "View Axis%x1|"
-       "Cursor Distance%x2|"
-       "Material%x3|"
-       "Selection%x4|"
-       "Randomize%x5");
-       
-       if (event==-1) return;
-       
-       if(ctrl)
-               reverse = -1;
-       
-/*     create index list */
-       index = (int *) MEM_mallocN(sizeof(int) * me->totface, "sort faces");
-       for (i = 0; i < me->totface; i++) {
-               index[i] = i;
-       }
-       
-       face_sort_floats = (float *) MEM_mallocN(sizeof(float) * me->totface, "sort faces float");
-       
-/* sort index list instead of faces itself 
-   and apply this permutation to all face layers */
-   
-         if (event == 5) {
-               /* Random */
-               for(i=0; i<me->totface; i++) {
-                       face_sort_floats[i] = BLI_frand();
-               }
-               qsort(index, me->totface, sizeof(int), float_sort);             
-       } else {
-               MFace *mf;
-               float vec[3];
-               float mat[4][4];
-               float cur[3];
-               
-               if (event == 1)
-                       mul_m4_m4m4(mat, OBACT->obmat, rv3d->viewmat); /* apply the view matrix to the object matrix */
-               else if (event == 2) { /* sort from cursor */
-                       if( v3d && v3d->localvd ) {
-                               VECCOPY(cur, v3d->cursor);
-                       } else {
-                               VECCOPY(cur, scene->cursor);
-                       }
-                       invert_m4_m4(mat, OBACT->obmat);
-                       mul_m4_v3(mat, cur);
-               }
-               
-               mf= me->mface;
-               for(i=0; i<me->totface; i++, mf++) {
-                       
-                       if (event==3) {
-                               face_sort_floats[i] = ((float)mf->mat_nr)*reverse;
-                       } else if (event==4) {
-                               /*selected first*/
-                               if (mf->flag & ME_FACE_SEL)     face_sort_floats[i] = 0.0;
-                               else                                            face_sort_floats[i] = reverse;
-                       } else {
-                               /* find the faces center */
-                               add_v3_v3v3(vec, (me->mvert+mf->v1)->co, (me->mvert+mf->v2)->co);
-                               if (mf->v4) {
-                                       add_v3_v3(vec, (me->mvert+mf->v3)->co);
-                                       add_v3_v3(vec, (me->mvert+mf->v4)->co);
-                                       mul_v3_fl(vec, 0.25f);
-                               } else {
-                                       add_v3_v3(vec, (me->mvert+mf->v3)->co);
-                                       mul_v3_fl(vec, 1.0f/3.0f);
-                               } /* done */
-                               
-                               if (event == 1) { /* sort on view axis */
-                                       mul_m4_v3(mat, vec);
-                                       face_sort_floats[i] = vec[2] * reverse;
-                               } else if(event == 2) { /* distance from cursor*/
-                                       face_sort_floats[i] = len_v3v3(cur, vec) * reverse; /* back to front */
-                               }
-                       }
-               }
-               qsort(index, me->totface, sizeof(int), float_sort);
-       }
-       
-       MEM_freeN(face_sort_floats);
-       
-       for(i = 0; i < me->fdata.totlayer; i++) {
-               layer = &me->fdata.layers[i];
-               permutate(layer->data, me->totface, CustomData_sizeof(layer->type), index);
-       }
-
-       MEM_freeN(index);
-
-       DAG_id_flush_update(ob->data, OB_RECALC_DATA);
-}
-
-
-
 /* ********************* MESH VERTEX OCTREE LOOKUP ************* */
 
 /* important note; this is unfinished, needs better API for editmode, and custom threshold */
@@ -893,7 +787,7 @@ static struct {
 
 /* mode is 's' start, or 'e' end, or 'u' use */
 /* if end, ob can be NULL */
-intptr_t mesh_octree_table(Object *ob, EditMesh *em, float *co, char mode)
+intptr_t mesh_octree_table(Object *ob, BMEditMesh *em, float *co, char mode)
 {
        MocNode **bt;
        
@@ -919,10 +813,12 @@ intptr_t mesh_octree_table(Object *ob, EditMesh *em, float *co, char mode)
                 * we are using the undeformed coordinates*/
                INIT_MINMAX(min, max);
 
-               if(em && me->edit_mesh==em) {
-                       EditVert *eve;
+               if(em && me->edit_btmesh==em) {
+                       BMIter iter;
+                       BMVert *eve;
                        
-                       for(eve= em->verts.first; eve; eve= eve->next)
+                       eve = BMIter_New(&iter, em->bm, BM_VERTS_OF_MESH, NULL);
+                       for (; eve; eve=BMIter_Step(&iter))
                                DO_MINMAX(eve->co, min, max)
                }
                else {          
@@ -954,10 +850,12 @@ intptr_t mesh_octree_table(Object *ob, EditMesh *em, float *co, char mode)
                
                MeshOctree.table= MEM_callocN(MOC_RES*MOC_RES*MOC_RES*sizeof(void *), "sym table");
                
-               if(em && me->edit_mesh==em) {
-                       EditVert *eve;
+               if(em && me->edit_btmesh==em) {
+                       BMVert *eve;
+                       BMIter iter;
 
-                       for(eve= em->verts.first; eve; eve= eve->next) {
+                       eve = BMIter_New(&iter, em->bm, BM_VERTS_OF_MESH, NULL);
+                       for (; eve; eve=BMIter_Step(&iter)) {
                                mesh_octree_add_nodes(MeshOctree.table, eve->co, MeshOctree.offs, MeshOctree.div, (intptr_t)(eve));
                        }
                }
@@ -1015,6 +913,7 @@ static int  mesh_topo_lookup_mode = -1;
 /* if end, ob can be NULL */
 long mesh_mirrtopo_table(Object *ob, char mode)
 {
+#if 0
        if(mode=='u') {         /* use table */
                Mesh *me= ob->data;
                if(     (mesh_topo_lookup==NULL) ||
@@ -1173,6 +1072,9 @@ long mesh_mirrtopo_table(Object *ob, char mode)
                mesh_topo_lookup = NULL;
                mesh_topo_lookup_tot= -1;
        }
+       return 0;
+#endif
+
        return 0;
 }
 
@@ -1190,21 +1092,30 @@ int mesh_get_x_mirror_vert_spacial(Object *ob, int index)
        return mesh_octree_table(ob, NULL, vec, 'u');
 }
 
-static int mesh_get_x_mirror_vert_topo(Object *ob, int index)
+/*BMESH_TODO: hook into editbmesh_bvh.h code, though that'll require caching the
+              bvh structure most likely, not too difficult*/
+BMVert *editmesh_get_x_mirror_vert(Object *ob, BMEditMesh *em, float *co)
 {
+#if 0
        if (mesh_mirrtopo_table(ob, 'u')==-1)
                return -1;
 
        return mesh_topo_lookup[index];
+#endif
+       return NULL;
+
 }
 
 int mesh_get_x_mirror_vert(Object *ob, int index)
 {
+#if 0 //BMESH_TODO
        if (((Mesh *)ob->data)->editflag & ME_EDIT_MIRROR_TOPO) {
                return mesh_get_x_mirror_vert_topo(ob, index);
        } else {
                return mesh_get_x_mirror_vert_spacial(ob, index);
        }
+#endif
+       return 0;
 }
 
 static EditVert *editmesh_get_x_mirror_vert_spacial(Object *ob, EditMesh *em, float *co)
@@ -1225,7 +1136,7 @@ static EditVert *editmesh_get_x_mirror_vert_spacial(Object *ob, EditMesh *em, fl
        
        poinval= mesh_octree_table(ob, em, vec, 'u');
        if(poinval != -1)
-               return (EditVert *)(poinval);
+               return (BMVert *)(poinval);
        return NULL;
 }
 
@@ -1249,14 +1160,17 @@ static EditVert *editmesh_get_x_mirror_vert_topo(Object *ob, struct EditMesh *em
        return NULL;
 }
 
+#if 0
 EditVert *editmesh_get_x_mirror_vert(Object *ob, struct EditMesh *em, EditVert *eve, float *co, int index)
 {
+       //BMESH_TODO use this flag, ME_EDIT_MIRROR_TOPO, at appropriate places
        if (((Mesh *)ob->data)->editflag & ME_EDIT_MIRROR_TOPO) {
                return editmesh_get_x_mirror_vert_topo(ob, em, eve, index);
        } else {
                return editmesh_get_x_mirror_vert_spacial(ob, em, eve->co);
        }
 }
+#endif
 
 
 #if 0
@@ -1355,7 +1269,7 @@ static int mirror_facecmp(void *a, void *b)
        return (mirror_facerotation((MFace*)a, (MFace*)b) == -1);
 }
 
-int *mesh_get_x_mirror_faces(Object *ob, EditMesh *em)
+int *mesh_get_x_mirror_faces(Object *ob, BMEditMesh *em)
 {
        Mesh *me= ob->data;
        MVert *mv, *mvert= me->mvert;
@@ -1374,7 +1288,7 @@ int *mesh_get_x_mirror_faces(Object *ob, EditMesh *em)
 
        mesh_octree_table(ob, em, NULL, 'e');
 
-       fhash= BLI_ghash_new(mirror_facehash, mirror_facecmp);
+       fhash= BLI_ghash_new(mirror_facehash, mirror_facecmp, "mirror_facehash gh");
        for(a=0, mf=mface; a<me->totface; a++, mf++)
                BLI_ghash_insert(fhash, mf, mf);