svn merge ^/trunk/blender -r44118:44136
[blender.git] / source / blender / editors / mesh / meshtools.c
index c74a994b9b648c278478bff56a93f9b1d8226b8c..1b5011402fc9d83f360d53a0a582fdf291d99865 100644 (file)
@@ -98,15 +98,14 @@ int join_mesh_exec(bContext *C, wmOperator *op)
        Mesh *me;
        MVert *mvert, *mv;
        MEdge *medge = NULL;
-       MFace *mface = NULL;
        MPoly *mpoly = NULL;
        MLoop *mloop = NULL;
        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 a, b, totcol, totmat=0, totedge=0, totvert=0, ok=0;
        int totloop=0, totpoly=0, vertofs, *matmap=NULL;
-       int i, j, index, haskey=0, edgeofs, faceofs, loopofs, polyofs;
+       int i, j, index, haskey=0, edgeofs, loopofs, polyofs;
        bDeformGroup *dg, *odg;
        MDeformVert *dvert;
        CustomData vdata, edata, fdata, ldata, pdata;
@@ -129,7 +128,6 @@ 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;
@@ -290,13 +288,11 @@ int join_mesh_exec(bContext *C, wmOperator *op)
        
        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);
 
        vertofs= 0;
        edgeofs= 0;
-       faceofs= 0;
        loopofs= 0;
        polyofs= 0;
        
@@ -338,7 +334,7 @@ int join_mesh_exec(bContext *C, wmOperator *op)
                                /* if this is the object we're merging into, no need to do anything */
                                if(base->object != ob) {
                                        /* watch this: switch matmul order really goes wrong */
-                                       mul_m4_m4m4(cmat, base->object->obmat, imat);
+                                       mult_m4_m4m4(cmat, imat, base->object->obmat);
                                        
                                        /* transform vertex coordinates into new space */
                                        for(a=0, mv=mvert; a < me->totvert; a++, mv++) {
@@ -409,37 +405,6 @@ int join_mesh_exec(bContext *C, wmOperator *op)
                                mvert+= me->totvert;
                        }
                        
-                       if(me->totface) {
-                               /* 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->fdata, &fdata, CD_MASK_MESH, CD_DEFAULT, totface);
-                               CustomData_copy_data(&me->fdata, &fdata, 0, faceofs, me->totface);
-                               
-                               for(a=0; a<me->totface; a++, mface++) {
-                                       mface->v1+= vertofs;
-                                       mface->v2+= vertofs;
-                                       mface->v3+= vertofs;
-                                       if(mface->v4) mface->v4+= vertofs;
-                                       
-                                       if (matmap)
-                                               mface->mat_nr= matmap[(int)mface->mat_nr];
-                                       else 
-                                               mface->mat_nr= 0;
-                               }
-                               
-                               faceofs += me->totface;
-                       }
-                       
                        if(me->totedge) {
                                CustomData_merge(&me->edata, &edata, CD_MASK_MESH, CD_DEFAULT, totedge);
                                CustomData_copy_data(&me->edata, &edata, 0, edgeofs, me->totedge);
@@ -506,23 +471,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);
+       mesh_update_customdata_pointers(me, TRUE); /* BMESH_TODO, check if this arg can be failse, non urgent - campbell */
        
        /* old material array */
        for(a=1; a<=ob->totcol; a++) {
@@ -548,7 +510,6 @@ int join_mesh_exec(bContext *C, wmOperator *op)
                MEM_freeN(matar);
        
        ob->totcol= me->totcol= totcol;
-       ob->colbits= 0;
 
        if (matmap) MEM_freeN(matmap);
        
@@ -723,17 +684,17 @@ static void mesh_octree_free_node(MocNode **bt)
 
 
 /* temporal define, just to make nicer code below */
-#define MOC_ADDNODE(vx, vy, vz)        mesh_octree_add_node(basetable + ((vx)*MOC_RES*MOC_RES) + (vy)*MOC_RES + (vz), index)
+#define MOC_INDEX(vx, vy, vz)  (((vx)*MOC_RES*MOC_RES) + (vy)*MOC_RES + (vz))
 
 static void mesh_octree_add_nodes(MocNode **basetable, float *co, float *offs, float *div, intptr_t index)
 {
        float fx, fy, fz;
        int vx, vy, vz;
        
-       if (!finite(co[0]) ||
-               !finite(co[1]) ||
-               !finite(co[2])
-       {
+       if ( !finite(co[0]) ||
+            !finite(co[1]) ||
+            !finite(co[2]))
+       {
                return;
        }
        
@@ -744,33 +705,33 @@ static void mesh_octree_add_nodes(MocNode **basetable, float *co, float *offs, f
        CLAMP(fy, 0.0f, MOC_RES-MOC_THRESH);
        CLAMP(fz, 0.0f, MOC_RES-MOC_THRESH);
        
-       vx= floor(fx);
-       vy= floor(fy);
-       vz= floor(fz);
-       
-       MOC_ADDNODE(vx, vy, vz);
-       
-       if( vx>0 )
-               iffx-((float)vx)-MOC_THRESH < 0.0f)
-                       MOC_ADDNODE(vx-1, vy, vz);
-       if( vx<MOC_RES-2 )
-               iffx-((float)vx)+MOC_THRESH > 1.0f)
-                       MOC_ADDNODE(vx+1, vy, vz);
-
-       if( vy>0 )
-               if( fy-((float)vy)-MOC_THRESH < 0.0f) 
-                       MOC_ADDNODE(vx, vy-1, vz);
-       if( vy<MOC_RES-2 )
-               if( fy-((float)vy)+MOC_THRESH > 1.0f) 
-                       MOC_ADDNODE(vx, vy+1, vz);
-
-       if( vz>0 )
-               if( fz-((float)vz)-MOC_THRESH < 0.0f) 
-                       MOC_ADDNODE(vx, vy, vz-1);
-       if( vz<MOC_RES-2 )
-               if( fz-((float)vz)+MOC_THRESH > 1.0f) 
-                       MOC_ADDNODE(vx, vy, vz+1);
-       
+       vx= (int)floorf(fx);
+       vy= (int)floorf(fy);
+       vz= (int)floorf(fz);
+
+       mesh_octree_add_node(basetable + MOC_INDEX(vx, vy, vz), index);
+
+       if (vx > 0)
+               if (fx-((float)vx)-MOC_THRESH < 0.0f)
+                       mesh_octree_add_node(basetable + MOC_INDEX(vx - 1, vy, vz), index);
+       if (vx < MOC_RES - 2)
+               if (fx-((float)vx)+MOC_THRESH > 1.0f)
+                       mesh_octree_add_node(basetable + MOC_INDEX(vx + 1, vy, vz), index);
+
+       if (vy > 0)
+               if (fy-((float)vy)-MOC_THRESH < 0.0f)
+                       mesh_octree_add_node(basetable + MOC_INDEX(vx, vy - 1, vz), index);
+       if (vy < MOC_RES - 2)
+               if (fy-((float)vy)+MOC_THRESH > 1.0f)
+                       mesh_octree_add_node(basetable + MOC_INDEX(vx, vy + 1, vz), index);
+
+       if (vz > 0)
+               if (fz-((float)vz)-MOC_THRESH < 0.0f)
+                       mesh_octree_add_node(basetable + MOC_INDEX(vx, vy, vz - 1), index);
+       if (vz <MOC_RES - 2)
+               if (fz-((float)vz)+MOC_THRESH > 1.0f)
+                       mesh_octree_add_node(basetable + MOC_INDEX(vx, vy, vz + 1), index);
+
 }
 
 static intptr_t mesh_octree_find_index(MocNode **bt, MVert *mvert, float *co)
@@ -817,7 +778,7 @@ intptr_t mesh_octree_table(Object *ob, BMEditMesh *em, float *co, char mode)
        if(mode=='u') {         /* use table */
                if(MeshOctree.table==NULL)
                        mesh_octree_table(ob, em, NULL, 's');
-          
+
                if(MeshOctree.table) {
                        Mesh *me= ob->data;
                        bt= MeshOctree.table + mesh_octree_get_base_offs(co, MeshOctree.offs, MeshOctree.div);
@@ -902,191 +863,23 @@ intptr_t mesh_octree_table(Object *ob, BMEditMesh *em, float *co, char mode)
        return 0;
 }
 
-
-/* ********************* MESH VERTEX MIRR TOPO LOOKUP *************** */
-
-#define MIRRHASH_TYPE int
-
-typedef struct MirrTopoPair {
-       long hash;
-       int vIndex;
-} MirrTopoPair;
-
-static int MirrTopo_long_sort(const void *l1, const void *l2)
-{
-       if(                     (MIRRHASH_TYPE)(intptr_t)l1 > (MIRRHASH_TYPE)(intptr_t)l2 ) return  1;
-       else if(        (MIRRHASH_TYPE)(intptr_t)l1 < (MIRRHASH_TYPE)(intptr_t)l2 ) return -1;
-       return 0;
-}
-
-static int MirrTopo_item_sort(const void *v1, const void *v2)
-{
-       if(                     ((MirrTopoPair *)v1)->hash > ((MirrTopoPair *)v2)->hash ) return  1;
-       else if(        ((MirrTopoPair *)v1)->hash < ((MirrTopoPair *)v2)->hash ) return -1;
-       return 0;
-}
-
-static long *mesh_topo_lookup = NULL;
-static int  mesh_topo_lookup_tot = -1;
-static int  mesh_topo_lookup_mode = -1;
+MirrTopoStore_t mesh_topo_store= {NULL, -1. -1, -1};
 
 /* mode is 's' start, or 'e' end, or 'u' use */
 /* if end, ob can be NULL */
-long mesh_mirrtopo_table(Object *ob, char mode)
+/* note, is supposed return -1 on error, which callers are currently checking for, but is not used so far */
+int mesh_mirrtopo_table(Object *ob, char mode)
 {
        if(mode=='u') {         /* use table */
-               Mesh *me= ob->data;
-               if(     (mesh_topo_lookup==NULL) ||
-                       (mesh_topo_lookup_mode != ob->mode) ||
-                       (me->edit_btmesh && me->edit_btmesh->bm->totvert != mesh_topo_lookup_tot) ||
-                       (me->edit_btmesh==NULL && me->totvert != mesh_topo_lookup_tot)
-               ) {
+               if (ED_mesh_mirrtopo_recalc_check(ob->data, ob->mode, &mesh_topo_store)) {
                        mesh_mirrtopo_table(ob, 's');
                }
-       } else if(mode=='s') { /* start table */
-               Mesh *me= ob->data;
-               MEdge *medge;
-               BMEditMesh *em= me->edit_btmesh;
-               BMEdge *eed;
-               BMIter iter;
-               MIRRHASH_TYPE *MirrTopoHash = NULL;
-               MIRRHASH_TYPE *MirrTopoHash_Prev = NULL;
-               MirrTopoPair *MirrTopoPairs;
-               int a, last, totvert;
-               int totUnique= -1, totUniqueOld= -1;
-
-               mesh_topo_lookup_mode= ob->mode;
-
-               /* reallocate if needed */
-               if (mesh_topo_lookup) {
-                       MEM_freeN(mesh_topo_lookup);
-                       mesh_topo_lookup = NULL;
-               }
-
-               if(em) {
-                       BM_ElemIndex_Ensure(em->bm, BM_VERT);
-
-                       totvert= em->bm->totvert;
-               }
-               else {
-                       totvert = me->totvert;
-               }
-
-               MirrTopoHash = MEM_callocN( totvert * sizeof(MIRRHASH_TYPE), "TopoMirr" );
-
-               /* Initialize the vert-edge-user counts used to detect unique topology */
-               if(em) {
-                       BM_ITER(eed, &iter, em->bm, BM_EDGES_OF_MESH, NULL) {
-                               MirrTopoHash[BM_GetIndex(eed->v1)]++;
-                               MirrTopoHash[BM_GetIndex(eed->v2)]++;
-                       }
-               } else {
-                       for(a=0, medge=me->medge; a<me->totedge; a++, medge++) {
-                               MirrTopoHash[medge->v1]++;
-                               MirrTopoHash[medge->v2]++;
-                       }
-               }
-
-               MirrTopoHash_Prev = MEM_dupallocN( MirrTopoHash );
-
-               totUniqueOld = -1;
-               while(1) {
-                       /* use the number of edges per vert to give verts unique topology IDs */
-
-                       if(em) {
-                               BM_ITER(eed, &iter, em->bm, BM_EDGES_OF_MESH, NULL) {
-                                       MirrTopoHash[BM_GetIndex(eed->v1)] += MirrTopoHash_Prev[BM_GetIndex(eed->v2)];
-                                       MirrTopoHash[BM_GetIndex(eed->v2)] += MirrTopoHash_Prev[BM_GetIndex(eed->v1)];
-                               }
-                       } else {
-                               for(a=0, medge=me->medge; a<me->totedge; a++, medge++) {
-                                       /* This can make really big numbers, wrapping around here is fine */
-                                       MirrTopoHash[medge->v1] += MirrTopoHash_Prev[medge->v2];
-                                       MirrTopoHash[medge->v2] += MirrTopoHash_Prev[medge->v1];
-                               }
-                       }
-                       memcpy(MirrTopoHash_Prev, MirrTopoHash, sizeof(MIRRHASH_TYPE) * totvert);
-
-                       /* sort so we can count unique values */
-                       qsort(MirrTopoHash_Prev, totvert, sizeof(MIRRHASH_TYPE), MirrTopo_long_sort);
-
-                       totUnique = 1; /* account for skiping the first value */
-                       for(a=1; a<totvert; a++) {
-                               if (MirrTopoHash_Prev[a-1] != MirrTopoHash_Prev[a]) {
-                                       totUnique++;
-                               }
-                       }
-
-                       if (totUnique <= totUniqueOld) {
-                               /* Finish searching for unique valus when 1 loop dosnt give a
-                                * higher number of unique values compared to the previous loop */
-                               break;
-                       } else {
-                               totUniqueOld = totUnique;
-                       }
-                       /* Copy the hash calculated this iter, so we can use them next time */
-                       memcpy(MirrTopoHash_Prev, MirrTopoHash, sizeof(MIRRHASH_TYPE) * totvert);
-               }
-
-               /* Hash/Index pairs are needed for sorting to find index pairs */
-               MirrTopoPairs= MEM_callocN( sizeof(MirrTopoPair) * totvert, "MirrTopoPairs");
-
-               /* since we are looping through verts, initialize these values here too */
-               mesh_topo_lookup = MEM_mallocN( totvert * sizeof(long), "mesh_topo_lookup" );
-
-               if(em) {
-                       EDBM_init_index_arrays(em,1,0,0);
-               }
-
-
-               for(a=0; a<totvert; a++) {
-                       MirrTopoPairs[a].hash= MirrTopoHash[a];
-                       MirrTopoPairs[a].vIndex = a;
-
-                       /* initialize lookup */
-                       mesh_topo_lookup[a] = -1;
-               }
-
-               qsort(MirrTopoPairs, totvert, sizeof(MirrTopoPair), MirrTopo_item_sort);
-
-               /* Since the loop starts at 2, we must define the last index where the hash's differ */
-               last = ((totvert >= 2) && (MirrTopoPairs[0].hash == MirrTopoPairs[1].hash)) ? 0 : 1;
-
-               /* Get the pairs out of the sorted hashes, note, totvert+1 means we can use the previous 2,
-                * but you cant ever access the last 'a' index of MirrTopoPairs */
-               for(a=2; a < totvert+1; a++) {
-                       /* printf("I %d %ld %d\n", (a-last), MirrTopoPairs[a  ].hash, MirrTopoPairs[a  ].vIndex ); */
-                       if ((a==totvert) || (MirrTopoPairs[a-1].hash != MirrTopoPairs[a].hash)) {
-                               if (a-last==2) {
-                                       if(em) {
-                                               mesh_topo_lookup[MirrTopoPairs[a-1].vIndex] =   (long)EDBM_get_vert_for_index(em, MirrTopoPairs[a-2].vIndex);
-                                               mesh_topo_lookup[MirrTopoPairs[a-2].vIndex] =   (long)EDBM_get_vert_for_index(em, MirrTopoPairs[a-1].vIndex);
-                                       } else {
-                                               mesh_topo_lookup[MirrTopoPairs[a-1].vIndex] =   MirrTopoPairs[a-2].vIndex;
-                                               mesh_topo_lookup[MirrTopoPairs[a-2].vIndex] =   MirrTopoPairs[a-1].vIndex;
-                                       }
-                               }
-                               last= a;
-                       }
-               }
-               if(em) {
-                       EDBM_free_index_arrays(em);
-               }
-
-               MEM_freeN( MirrTopoPairs );
-               MirrTopoPairs = NULL;
-
-               MEM_freeN( MirrTopoHash );
-               MEM_freeN( MirrTopoHash_Prev );
-
-               mesh_topo_lookup_tot = totvert;
-
-       } else if(mode=='e') { /* end table */
-               if (mesh_topo_lookup) {
-                       MEM_freeN(mesh_topo_lookup);
-               }
-               mesh_topo_lookup = NULL;
-               mesh_topo_lookup_tot= -1;
+       }
+       else if(mode=='s') { /* start table */
+               ED_mesh_mirrtopo_init(ob->data, ob->mode, &mesh_topo_store, FALSE);
+       }
+       else if(mode=='e') { /* end table */
+               ED_mesh_mirrtopo_free(&mesh_topo_store);
        }
        return 0;
 }
@@ -1110,7 +903,7 @@ static int mesh_get_x_mirror_vert_topo(Object *ob, int index)
        if (mesh_mirrtopo_table(ob, 'u')==-1)
                return -1;
 
-       return mesh_topo_lookup[index];
+       return mesh_topo_store.index_lookup[index];
 }
 
 int mesh_get_x_mirror_vert(Object *ob, int index)
@@ -1147,7 +940,7 @@ static BMVert *editbmesh_get_x_mirror_vert_spacial(Object *ob, BMEditMesh *em, f
 
 static BMVert *editbmesh_get_x_mirror_vert_topo(Object *ob, struct BMEditMesh *em, BMVert *eve, int index)
 {
-       long poinval;
+       intptr_t poinval;
        if (mesh_mirrtopo_table(ob, 'u')==-1)
                return NULL;
 
@@ -1167,7 +960,7 @@ static BMVert *editbmesh_get_x_mirror_vert_topo(Object *ob, struct BMEditMesh *e
                }
        }
 
-       poinval= mesh_topo_lookup[ index ];
+       poinval= mesh_topo_store.index_lookup[index];
 
        if(poinval != -1)
                return (BMVert *)(poinval);
@@ -1176,15 +969,15 @@ static BMVert *editbmesh_get_x_mirror_vert_topo(Object *ob, struct BMEditMesh *e
 
 BMVert *editbmesh_get_x_mirror_vert(Object *ob, struct BMEditMesh *em, BMVert *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 editbmesh_get_x_mirror_vert_topo(ob, em, eve, index);
-       } else {
+       }
+       else {
                return editbmesh_get_x_mirror_vert_spacial(ob, em, co);
        }
 }
 
-
+#if 0
 
 static float *editmesh_get_mirror_uv(BMEditMesh *em, int axis, float *uv, float *mirrCent, float *face_cent)
 {
@@ -1238,6 +1031,8 @@ static float *editmesh_get_mirror_uv(BMEditMesh *em, int axis, float *uv, float
        return NULL;
 }
 
+#endif
+
 static unsigned int mirror_facehash(const void *ptr)
 {
        const MFace *mf= ptr;
@@ -1284,6 +1079,7 @@ static int mirror_facecmp(const void *a, const void *b)
        return (mirror_facerotation((MFace*)a, (MFace*)b) == -1);
 }
 
+/* BMESH_TODO, convert to MPoly (functions above also) */
 int *mesh_get_x_mirror_faces(Object *ob, BMEditMesh *em)
 {
        Mesh *me= ob->data;
@@ -1315,8 +1111,8 @@ int *mesh_get_x_mirror_faces(Object *ob, BMEditMesh *em)
 
                /* make sure v4 is not 0 if a quad */
                if(mf->v4 && mirrormf.v4==0) {
-                       SWAP(int, mirrormf.v1, mirrormf.v3);
-                       SWAP(int, mirrormf.v2, mirrormf.v4);
+                       SWAP(unsigned int, mirrormf.v1, mirrormf.v3);
+                       SWAP(unsigned int, mirrormf.v2, mirrormf.v4);
                }
 
                hashmf= BLI_ghash_lookup(fhash, &mirrormf);