merge with/from trunk at r35190
[blender.git] / source / blender / blenkernel / intern / mesh.c
index f205d8f9e9ad429b4c50f1029cabff7d5b2ad268..f6679315606d9820aa867a01e1638bdff0b36b28 100644 (file)
 #include "DNA_ipo_types.h"
 #include "DNA_customdata_types.h"
 
+#include "BLI_blenlib.h"
+#include "BLI_editVert.h"
+#include "BLI_math.h"
+#include "BLI_edgehash.h"
+#include "BLI_utildefines.h"
+#include "BLI_scanfill.h"
+
 #include "BKE_animsys.h"
 #include "BKE_main.h"
 #include "BKE_customdata.h"
 #include "BKE_displist.h"
 #include "BKE_library.h"
 #include "BKE_material.h"
+#include "BKE_modifier.h"
+#include "BKE_multires.h"
 #include "BKE_key.h"
 /* these 2 are only used by conversion functions */
 #include "BKE_curve.h"
 /* -- */
 #include "BKE_object.h"
-#include "BKE_utildefines.h"
 #include "BKE_tessmesh.h"
 #include "BLI_edgehash.h"
 
@@ -396,11 +404,11 @@ void unlink_mesh(Mesh *me)
 {
        int a;
        
-       if(me==0) return;
+       if(me==NULL) return;
        
        for(a=0; a<me->totcol; a++) {
                if(me->mat[a]) me->mat[a]->id.us--;
-               me->mat[a]= 0;
+               me->mat[a]= NULL;
        }
 
        if(me->key) {
@@ -408,9 +416,9 @@ void unlink_mesh(Mesh *me)
                if (me->key->id.us == 0 && me->key->ipo )
                        me->key->ipo->id.us--;
        }
-       me->key= 0;
+       me->key= NULL;
        
-       if(me->texcomesh) me->texcomesh= 0;
+       if(me->texcomesh) me->texcomesh= NULL;
 }
 
 
@@ -485,7 +493,7 @@ void free_dverts(MDeformVert *dvert, int totvert)
        MEM_freeN (dvert);
 }
 
-Mesh *add_mesh(char *name)
+Mesh *add_mesh(const char *name)
 {
        Mesh *me;
        
@@ -600,9 +608,9 @@ void make_local_tface(Mesh *me)
                                if(tface->tpage) {
                                        ima= tface->tpage;
                                        if(ima->id.lib) {
-                                               ima->id.lib= 0;
+                                               ima->id.lib= NULL;
                                                ima->id.flag= LIB_LOCAL;
-                                               new_id(0, (ID *)ima, 0);
+                                               new_id(NULL, (ID *)ima, NULL);
                                        }
                                }
                        }
@@ -623,11 +631,11 @@ void make_local_mesh(Mesh *me)
                * - mixed: make copy
                */
        
-       if(me->id.lib==0) return;
+       if(me->id.lib==NULL) return;
        if(me->id.us==1) {
-               me->id.lib= 0;
+               me->id.lib= NULL;
                me->id.flag= LIB_LOCAL;
-               new_id(0, (ID *)me, 0);
+               new_id(NULL, (ID *)me, NULL);
                
                if(me->mtface) make_local_tface(me);
                
@@ -644,9 +652,9 @@ void make_local_mesh(Mesh *me)
        }
        
        if(local && lib==0) {
-               me->id.lib= 0;
+               me->id.lib= NULL;
                me->id.flag= LIB_LOCAL;
-               new_id(0, (ID *)me, 0);
+               new_id(NULL, (ID *)me, NULL);
                
                if(me->mtface) make_local_tface(me);
                
@@ -658,7 +666,7 @@ void make_local_mesh(Mesh *me)
                ob= bmain->object.first;
                while(ob) {
                        if( me==get_mesh(ob) ) {                                
-                               if(ob->id.lib==0) {
+                               if(ob->id.lib==NULL) {
                                        set_mesh(ob, men);
                                }
                        }
@@ -673,7 +681,7 @@ void boundbox_mesh(Mesh *me, float *loc, float *size)
        float min[3], max[3];
        float mloc[3], msize[3];
        
-       if(me->bb==0) me->bb= MEM_callocN(sizeof(BoundBox), "boundbox");
+       if(me->bb==NULL) me->bb= MEM_callocN(sizeof(BoundBox), "boundbox");
        bb= me->bb;
 
        if (!loc) loc= mloc;
@@ -786,11 +794,11 @@ void transform_mesh_orco_verts(Mesh *me, float (*orco)[3], int totvert, int inve
 int test_index_face(MFace *mface, CustomData *fdata, int mfindex, int nr)
 {
        /* first test if the face is legal */
-       if(mface->v3 && mface->v3==mface->v4) {
+       if((mface->v3 || nr==4) && mface->v3==mface->v4) {
                mface->v4= 0;
                nr--;
        }
-       if(mface->v2 && mface->v2==mface->v3) {
+       if((mface->v2 || mface->v4) && mface->v2==mface->v3) {
                mface->v3= mface->v4;
                mface->v4= 0;
                nr--;
@@ -802,6 +810,32 @@ int test_index_face(MFace *mface, CustomData *fdata, int mfindex, int nr)
                nr--;
        }
 
+       /* check corrupt cases, bowtie geometry, cant handle these because edge data wont exist so just return 0 */
+       if(nr==3) {
+               if(
+               /* real edges */
+                       mface->v1==mface->v2 ||
+                       mface->v2==mface->v3 ||
+                       mface->v3==mface->v1
+               ) {
+                       return 0;
+               }
+       }
+       else if(nr==4) {
+               if(
+               /* real edges */
+                       mface->v1==mface->v2 ||
+                       mface->v2==mface->v3 ||
+                       mface->v3==mface->v4 ||
+                       mface->v4==mface->v1 ||
+               /* across the face */
+                       mface->v1==mface->v3 ||
+                       mface->v2==mface->v4
+               ) {
+                       return 0;
+               }
+       }
+
        /* prevent a zero at wrong index location */
        if(nr==3) {
                if(mface->v3==0) {
@@ -832,16 +866,18 @@ int test_index_face(MFace *mface, CustomData *fdata, int mfindex, int nr)
 Mesh *get_mesh(Object *ob)
 {
        
-       if(ob==0) return 0;
+       if(ob==NULL) return NULL;
        if(ob->type==OB_MESH) return ob->data;
-       else return 0;
+       else return NULL;
 }
 
 void set_mesh(Object *ob, Mesh *me)
 {
-       Mesh *old=0;
+       Mesh *old=NULL;
+
+       multires_force_update(ob);
        
-       if(ob==0) return;
+       if(ob==NULL) return;
        
        if(ob->type==OB_MESH) {
                old= ob->data;
@@ -852,6 +888,8 @@ void set_mesh(Object *ob, Mesh *me)
        }
        
        test_object_materials((ID *)me);
+
+       test_object_modifiers(ob);
 }
 
 /* ************** make edges in a Mesh, for outside of editmode */
@@ -903,7 +941,7 @@ static void mfaces_strip_loose(MFace *mface, int *totface)
 }
 
 /* Create edges based on known verts and faces */
-static void make_edges_mdata(MVert *allvert, MFace *allface, int totvert, int totface,
+static void make_edges_mdata(MVert *UNUSED(allvert), MFace *allface, int UNUSED(totvert), int totface,
        int old, MEdge **alledge, int *_totedge)
 {
        MFace *mface;
@@ -1020,6 +1058,23 @@ void mesh_strip_loose_faces(Mesh *me)
        me->totface = b;
 }
 
+void mesh_strip_loose_edges(Mesh *me)
+{
+       int a,b;
+
+       for (a=b=0; a<me->totedge; a++) {
+               if (me->medge[a].v1!=me->medge[a].v2) {
+                       if (a!=b) {
+                               memcpy(&me->medge[b],&me->medge[a],sizeof(me->medge[b]));
+                               CustomData_copy_data(&me->edata, &me->edata, a, b, 1);
+                               CustomData_free_elem(&me->edata, a, 1);
+                       }
+                       b++;
+               }
+       }
+       me->totedge = b;
+}
+
 void mball_to_mesh(ListBase *lb, Mesh *me)
 {
        DispList *dl;
@@ -1029,7 +1084,7 @@ void mball_to_mesh(ListBase *lb, Mesh *me)
        int a, *index;
        
        dl= lb->first;
-       if(dl==0) return;
+       if(dl==NULL) return;
 
        if(dl->type==DL_INDEX4) {
                me->totvert= dl->nr;
@@ -1077,7 +1132,7 @@ void mball_to_mesh(ListBase *lb, Mesh *me)
 int nurbs_to_mdata(Object *ob, MVert **allvert, int *totvert,
        MEdge **alledge, int *totedge, MFace **allface, int *totface)
 {
-       return nurbs_to_mdata_customdb(ob, &((Curve *)ob->data)->disp,
+       return nurbs_to_mdata_customdb(ob, &ob->disp,
                allvert, totvert, alledge, totedge, allface, totface);
 }
 
@@ -1093,9 +1148,13 @@ int nurbs_to_mdata_customdb(Object *ob, ListBase *dispbase, MVert **allvert, int
        float *data;
        int a, b, ofs, vertcount, startvert, totvert=0, totvlak=0;
        int p1, p2, p3, p4, *index;
+       int conv_polys= 0;
 
        cu= ob->data;
 
+       conv_polys|= cu->flag & CU_3D;          /* 2d polys are filled with DL_INDEX3 displists */
+       conv_polys|= ob->type == OB_SURF;       /* surf polys are never filled */
+
        /* count */
        dl= dispbase->first;
        while(dl) {
@@ -1104,8 +1163,10 @@ int nurbs_to_mdata_customdb(Object *ob, ListBase *dispbase, MVert **allvert, int
                        totvlak+= dl->parts*(dl->nr-1);
                }
                else if(dl->type==DL_POLY) {
-                       totvert+= dl->parts*dl->nr;
-                       totvlak+= dl->parts*dl->nr;
+                       if(conv_polys) {
+                               totvert+= dl->parts*dl->nr;
+                               totvlak+= dl->parts*dl->nr;
+                       }
                }
                else if(dl->type==DL_SURF) {
                        totvert+= dl->parts*dl->nr;
@@ -1125,7 +1186,7 @@ int nurbs_to_mdata_customdb(Object *ob, ListBase *dispbase, MVert **allvert, int
        }
 
        *allvert= mvert= MEM_callocN(sizeof (MVert) * totvert, "nurbs_init mvert");
-       *allface= mface= MEM_callocN(sizeof (MVert) * totvert, "nurbs_init mface");
+       *allface= mface= MEM_callocN(sizeof (MVert) * totvlak, "nurbs_init mface");
 
        /* verts and faces */
        vertcount= 0;
@@ -1157,24 +1218,26 @@ int nurbs_to_mdata_customdb(Object *ob, ListBase *dispbase, MVert **allvert, int
 
                }
                else if(dl->type==DL_POLY) {
-                       startvert= vertcount;
-                       a= dl->parts*dl->nr;
-                       data= dl->verts;
-                       while(a--) {
-                               VECCOPY(mvert->co, data);
-                               data+=3;
-                               vertcount++;
-                               mvert++;
-                       }
+                       if(conv_polys) {
+                               startvert= vertcount;
+                               a= dl->parts*dl->nr;
+                               data= dl->verts;
+                               while(a--) {
+                                       VECCOPY(mvert->co, data);
+                                       data+=3;
+                                       vertcount++;
+                                       mvert++;
+                               }
 
-                       for(a=0; a<dl->parts; a++) {
-                               ofs= a*dl->nr;
-                               for(b=0; b<dl->nr; b++) {
-                                       mface->v1= startvert+ofs+b;
-                                       if(b==dl->nr-1) mface->v2= startvert+ofs;
-                                       else mface->v2= startvert+ofs+b+1;
-                                       if(smooth) mface->flag |= ME_SMOOTH;
-                                       mface++;
+                               for(a=0; a<dl->parts; a++) {
+                                       ofs= a*dl->nr;
+                                       for(b=0; b<dl->nr; b++) {
+                                               mface->v1= startvert+ofs+b;
+                                               if(b==dl->nr-1) mface->v2= startvert+ofs;
+                                               else mface->v2= startvert+ofs+b+1;
+                                               if(smooth) mface->flag |= ME_SMOOTH;
+                                               mface++;
+                                       }
                                }
                        }
                }
@@ -1196,6 +1259,7 @@ int nurbs_to_mdata_customdb(Object *ob, ListBase *dispbase, MVert **allvert, int
                                mface->v2= startvert+index[2];
                                mface->v3= startvert+index[1];
                                mface->v4= 0;
+                               mface->mat_nr= (unsigned char)dl->col;
                                test_index_face(mface, NULL, 0, 3);
 
                                if(smooth) mface->flag |= ME_SMOOTH;
@@ -1313,7 +1377,7 @@ void nurbs_to_mesh(Object *ob)
 
        tex_space_mesh(me);
 
-       cu->mat= 0;
+       cu->mat= NULL;
        cu->totcol= 0;
 
        if(ob->data) {
@@ -1555,14 +1619,12 @@ void mesh_set_smooth_flag(Object *meshOb, int enableSmooth)
                        mf->flag &= ~ME_SMOOTH;
                }
        }
-
-// XXX do this in caller       DAG_id_flush_update(&me->id, OB_RECALC_DATA);
 }
 
 void mesh_calc_normals(MVert *mverts, int numVerts, MFace *mfaces, int numFaces, float **faceNors_r) 
 {
        float (*tnorms)[3]= MEM_callocN(numVerts*sizeof(*tnorms), "tnorms");
-       float *fnors= MEM_mallocN(sizeof(*fnors)*3*numFaces, "meshnormals");
+       float *fnors= MEM_callocN(sizeof(*fnors)*3*numFaces, "meshnormals");
        int i;
 
        for (i=0; i<numFaces; i++) {
@@ -1782,7 +1844,7 @@ void mesh_pmv_free(PartialVisibility *pv)
        MEM_freeN(pv);
 }
 
-void mesh_pmv_revert(Object *ob, Mesh *me)
+void mesh_pmv_revert(Mesh *me)
 {
        if(me->pv) {
                unsigned i;
@@ -1816,15 +1878,13 @@ void mesh_pmv_revert(Object *ob, Mesh *me)
                me->pv->edge_map= NULL;
                MEM_freeN(me->pv->vert_map);
                me->pv->vert_map= NULL;
-
-// XXX do this in caller               DAG_id_flush_update(&me->id, OB_RECALC_DATA);
        }
 }
 
-void mesh_pmv_off(Object *ob, Mesh *me)
+void mesh_pmv_off(Mesh *me)
 {
-       if(ob && me->pv) {
-               mesh_pmv_revert(ob, me);
+       if(me->pv) {
+               mesh_pmv_revert(me);
                MEM_freeN(me->pv);
                me->pv= NULL;
        }
@@ -1946,7 +2006,7 @@ int mesh_recalcTesselation(CustomData *fdata,
                        }
                        BLI_addfilledge(lastv, firstv);
                        
-                       BLI_edgefill(0, 0);
+                       BLI_edgefill(0);
                        for (f=fillfacebase.first; f; f=f->next) {
                                BLI_array_growone(mf);
                                BLI_array_growone(origIndex);
@@ -2129,7 +2189,10 @@ int mesh_center_median(Mesh *me, float cent[3])
        for(mvert= me->mvert; i--; mvert++) {
                add_v3_v3(cent, mvert->co);
        }
-       mul_v3_fl(cent, 1.0f/(float)me->totvert);
+       /* otherwise we get NAN for 0 verts */
+       if(me->totvert) {
+               mul_v3_fl(cent, 1.0f/(float)me->totvert);
+       }
 
        return (me->totvert != 0);
 }