merge with 2.5 (not trunk, last merge message said that on accident) at r22252
[blender.git] / source / blender / editors / mesh / meshtools.c
index 5d4be254593177c8dd7b190de7c217a3739cd352..49e721f34e8be897ca00293f958c036d7274b3bf 100644 (file)
@@ -72,6 +72,7 @@
 #include "BKE_object.h"
 #include "BKE_utildefines.h"
 #include "BKE_report.h"
+#include "BKE_tessmesh.h"
 
 #include "RE_pipeline.h"
 #include "RE_shader_ext.h"
@@ -118,15 +119,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;
-       int     i, j, index, haskey=0, edgeofs, faceofs;
+       int totloop=0, totpoly=0, vertofs, *matmap;
+       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;
@@ -143,6 +146,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)
@@ -290,18 +295,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 */
        Mat4Invert(imat, ob->obmat);
@@ -451,7 +464,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)
                         */
@@ -470,14 +519,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);
        
@@ -838,7 +893,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;
        
@@ -864,10 +919,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 {          
@@ -905,10 +962,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));
                        }
                }
@@ -964,7 +1023,7 @@ int mesh_get_x_mirror_vert(Object *ob, int index)
        return mesh_octree_table(ob, NULL, vec, 'u');
 }
 
-EditVert *editmesh_get_x_mirror_vert(Object *ob, EditMesh *em, float *co)
+BMVert *editmesh_get_x_mirror_vert(Object *ob, BMEditMesh *em, float *co)
 {
        float vec[3];
        intptr_t poinval;
@@ -982,7 +1041,7 @@ EditVert *editmesh_get_x_mirror_vert(Object *ob, EditMesh *em, float *co)
        
        poinval= mesh_octree_table(ob, em, vec, 'u');
        if(poinval != -1)
-               return (EditVert *)(poinval);
+               return (BMVert *)(poinval);
        return NULL;
 }
 
@@ -1032,7 +1091,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;