merge with/from trunk at r35190
[blender.git] / source / blender / blenkernel / intern / mesh.c
index 3c56cb4663b7c3c0de52e5708c4d1dd109497bb3..f6679315606d9820aa867a01e1638bdff0b36b28 100644 (file)
 #include "DNA_key_types.h"
 #include "DNA_meshdata_types.h"
 #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_DerivedMesh.h"
 #include "BKE_global.h"
 #include "BKE_mesh.h"
 #include "BKE_curve.h"
 /* -- */
 #include "BKE_object.h"
+#include "BKE_tessmesh.h"
+#include "BLI_edgehash.h"
+
+#include "BLI_blenlib.h"
+#include "BLI_editVert.h"
+#include "BLI_math.h"
+#include "BLI_cellalloc.h"
+#include "BLI_array.h"
+#include "BLI_edgehash.h"
+
+#include "bmesh.h"
+
+enum {
+       MESHCMP_DVERT_WEIGHTMISMATCH = 1,
+       MESHCMP_DVERT_GROUPMISMATCH,
+       MESHCMP_DVERT_TOTGROUPMISMATCH,
+       MESHCMP_LOOPCOLMISMATCH,
+       MESHCMP_LOOPUVMISMATCH,
+       MESHCMP_LOOPMISMATCH,
+       MESHCMP_POLYVERTMISMATCH,
+       MESHCMP_POLYMISMATCH,
+       MESHCMP_EDGEUNKNOWN,
+       MESHCMP_VERTCOMISMATCH,
+       MESHCMP_CDLAYERS_MISMATCH,
+};
+
+static char *cmpcode_to_str(int code)
+{
+       switch (code) {
+               case MESHCMP_DVERT_WEIGHTMISMATCH:
+                       return "Vertex Weight Mismatch";
+               case MESHCMP_DVERT_GROUPMISMATCH:
+                                       return "Vertex Group Mismatch";
+               case MESHCMP_DVERT_TOTGROUPMISMATCH:
+                                       return "Vertex Doesn't Belong To Same Number Of Groups";
+               case MESHCMP_LOOPCOLMISMATCH:
+                                       return "Vertex Color Mismatch";
+               case MESHCMP_LOOPUVMISMATCH:
+                                       return "UV Mismatch";
+               case MESHCMP_LOOPMISMATCH:
+                                       return "Loop Mismatch";
+               case MESHCMP_POLYVERTMISMATCH:
+                                       return "Loop Vert Mismatch In Poly Test";
+               case MESHCMP_POLYMISMATCH:
+                                       return "Loop Vert Mismatch";
+               case MESHCMP_EDGEUNKNOWN:
+                                       return "Edge Mismatch";
+               case MESHCMP_VERTCOMISMATCH:
+                                       return "Vertex Coordinate Mismatch";
+               case MESHCMP_CDLAYERS_MISMATCH:
+                                       "CustomData Layer Count Mismatch";
+               default:
+                               return "Mesh Comparison Code Unknown";
+               }
+}
+
+/*thresh is threshold for comparing vertices, uvs, vertex colors,
+  weights, etc.*/
+int customdata_compare(CustomData *c1, CustomData *c2, Mesh *m1, Mesh *m2, float thresh)
+{
+       CustomDataLayer *l1, *l2;
+       int i, i1=0, i2=0, tot, j;
+       
+       for (i=0; i<c1->totlayer; i++) {
+               if (ELEM7(c1->layers[i].type, CD_MVERT, CD_MEDGE, CD_MPOLY, 
+                                 CD_MLOOPUV, CD_MLOOPCOL, CD_MTEXPOLY, CD_MDEFORMVERT))                
+                       i1++;
+       }
+       
+       for (i=0; i<c2->totlayer; i++) {
+               if (ELEM7(c2->layers[i].type, CD_MVERT, CD_MEDGE, CD_MPOLY, 
+                                 CD_MLOOPUV, CD_MLOOPCOL, CD_MTEXPOLY, CD_MDEFORMVERT))                
+                       i2++;
+       }
+       
+       if (i1 != i2)
+               return MESHCMP_CDLAYERS_MISMATCH;
+       
+       l1 = c1->layers; l2 = c2->layers;
+       tot = i1;
+       i1 = 0; i2 = 0; 
+       for (i=0; i < tot; i++) {
+               while (i1 < c1->totlayer && !ELEM7(l1->type, CD_MVERT, CD_MEDGE, CD_MPOLY, 
+                                 CD_MLOOPUV, CD_MLOOPCOL, CD_MTEXPOLY, CD_MDEFORMVERT))
+                       i1++, l1++;
+
+               while (i2 < c2->totlayer && !ELEM7(l2->type, CD_MVERT, CD_MEDGE, CD_MPOLY, 
+                                 CD_MLOOPUV, CD_MLOOPCOL, CD_MTEXPOLY, CD_MDEFORMVERT))
+                       i2++, l2++;
+               
+               if (l1->type == CD_MVERT) {
+                       MVert *v1 = l1->data;
+                       MVert *v2 = l2->data;
+                       int vtot = m1->totvert;
+                       
+                       for (j=0; j<vtot; j++, v1++, v2++) {
+                               if (len_v3v3(v1->co, v2->co) > thresh)
+                                       return MESHCMP_VERTCOMISMATCH;
+                               /*I don't care about normals, let's just do coodinates*/
+                       }
+               }
+               
+               /*we're order-agnostic for edges here*/
+               if (l1->type == CD_MEDGE) {
+                       MEdge *e1 = l1->data;
+                       MEdge *e2 = l2->data;
+                       EdgeHash *eh = BLI_edgehash_new();
+                       int etot = m1->totedge;
+               
+                       for (j=0; j<etot; j++, e1++) {
+                               BLI_edgehash_insert(eh, e1->v1, e1->v2, e1);
+                       }
+                       
+                       for (j=0; j<etot; j++, e2++) {
+                               if (!BLI_edgehash_lookup(eh, e2->v1, e2->v2))
+                                       return MESHCMP_EDGEUNKNOWN;
+                       }
+                       BLI_edgehash_free(eh, NULL);
+               }
+               
+               if (l1->type == CD_MPOLY) {
+                       MPoly *p1 = l1->data;
+                       MPoly *p2 = l2->data;
+                       int ptot = m1->totpoly;
+               
+                       for (j=0; j<ptot; j++, p1++, p2++) {
+                               MLoop *lp1, *lp2;
+                               int k;
+                               
+                               if (p1->totloop != p2->totloop)
+                                       return MESHCMP_POLYMISMATCH;
+                               
+                               lp1 = m1->mloop + p1->loopstart;
+                               lp2 = m2->mloop + p2->loopstart;
+                               
+                               for (k=0; k<p1->totloop; k++, lp1++, lp2++) {
+                                       if (lp1->v != lp2->v)
+                                               return MESHCMP_POLYVERTMISMATCH;
+                               }
+                       }
+               }
+               if (l1->type == CD_MLOOP) {
+                       MLoop *lp1 = l1->data;
+                       MLoop *lp2 = l2->data;
+                       int ltot = m1->totloop;
+               
+                       for (j=0; j<ltot; j++, lp1++, lp2++) {
+                               if (lp1->v != lp2->v)
+                                       return MESHCMP_LOOPMISMATCH;
+                       }
+               }
+               if (l1->type == CD_MLOOPUV) {
+                       MLoopUV *lp1 = l1->data;
+                       MLoopUV *lp2 = l2->data;
+                       int ltot = m1->totloop;
+               
+                       for (j=0; j<ltot; j++, lp1++, lp2++) {
+                               if (len_v2v2(lp1->uv, lp2->uv) > thresh)
+                                       return MESHCMP_LOOPUVMISMATCH;
+                       }
+               }
+               
+               if (l1->type == CD_MLOOPCOL) {
+                       MLoopCol *lp1 = l1->data;
+                       MLoopCol *lp2 = l2->data;
+                       int ltot = m1->totloop;
+               
+                       for (j=0; j<ltot; j++, lp1++, lp2++) {
+                               if (ABS(lp1->r - lp2->r) > thresh || 
+                                   ABS(lp1->g - lp2->g) > thresh || 
+                                   ABS(lp1->b - lp2->b) > thresh || 
+                                   ABS(lp1->a - lp2->a) > thresh)
+                               {
+                                       return MESHCMP_LOOPCOLMISMATCH;
+                               }
+                       }
+               }
 
+               if (l1->type == CD_MDEFORMVERT) {
+                       MDeformVert *dv1 = l1->data;
+                       MDeformVert *dv2 = l2->data;
+                       int dvtot = m1->totvert;
+               
+                       for (j=0; j<dvtot; j++, dv1++, dv2++) {
+                               int k;
+                               MDeformWeight *dw1 = dv1->dw, *dw2=dv2->dw;
+                               
+                               if (dv1->totweight != dv2->totweight)
+                                       return MESHCMP_DVERT_TOTGROUPMISMATCH;
+                               
+                               for (k=0; k<dv1->totweight; k++, dw1++, dw2++) {
+                                       if (dw1->def_nr != dw2->def_nr)
+                                               return MESHCMP_DVERT_GROUPMISMATCH;
+                                       if (ABS(dw1->weight - dw2->weight) > thresh)
+                                               return MESHCMP_DVERT_WEIGHTMISMATCH;
+                               }
+                       }
+               }
+       }
+       
+       return 0;
+}
 
-EditMesh *BKE_mesh_get_editmesh(Mesh *me)
+/*used for testing.  returns an error string the two meshes don't match*/
+char *mesh_cmp(Mesh *me1, Mesh *me2, float thresh)
 {
-       return me->edit_mesh;
+       int c;
+       
+       if (!me1 || !me2)
+               return "Requires two input meshes";
+       
+       if (me1->totvert != me2->totvert) 
+               return "Number of verts don't match";
+       
+       if (me1->totedge != me2->totedge)
+               return "Number of edges don't match";
+       
+       if (me1->totpoly != me2->totpoly)
+               return "Number of faces don't match";
+                               
+       if (me1->totloop !=me2->totloop)
+               return "Number of loops don't match";
+       
+       if ((c = customdata_compare(&me1->vdata, &me2->vdata, me1, me2, thresh)))
+               return cmpcode_to_str(c);
+
+       if ((c = customdata_compare(&me1->edata, &me2->edata, me1, me2, thresh)))
+               return cmpcode_to_str(c);
+
+       if ((c = customdata_compare(&me1->ldata, &me2->ldata, me1, me2, thresh)))
+               return cmpcode_to_str(c);
+
+       if ((c = customdata_compare(&me1->pdata, &me2->pdata, me1, me2, thresh)))
+               return cmpcode_to_str(c);
+       
+       return NULL;
 }
 
-void BKE_mesh_end_editmesh(Mesh *UNUSED(me), EditMesh *UNUSED(em))
+static void mesh_ensure_tesselation_customdata(Mesh *me)
 {
+       int tottex, totcol;
+
+       tottex = CustomData_number_of_layers(&me->fdata, CD_MTFACE);
+       totcol = CustomData_number_of_layers(&me->fdata, CD_MCOL);
+       
+       if (tottex != CustomData_number_of_layers(&me->pdata, CD_MTEXPOLY) ||
+           totcol != CustomData_number_of_layers(&me->ldata, CD_MLOOPCOL))
+       {
+               CustomData_free(&me->fdata, me->totface);
+               
+               me->mface = NULL;
+               me->mtface = NULL;
+               me->mcol = NULL;
+               me->totface = 0;
+
+               memset(&me->fdata, 0, sizeof(&me->fdata));
+
+               CustomData_from_bmeshpoly(&me->fdata, &me->pdata, &me->ldata, me->totface);
+               printf("Warning! Tesselation uvs or vcol data got out of sync, had to reset!\n");
+       }
 }
 
+/*this ensures grouped customdata (e.g. mtexpoly and mloopuv and mtface, or
+  mloopcol and mcol) have the same relative active/render/clone/mask indices.*/
+void mesh_update_linked_customdata(Mesh *me)
+{
+       int act;
+
+       if (me->edit_btmesh)
+               BMEdit_UpdateLinkedCustomData(me->edit_btmesh);
+
+       mesh_ensure_tesselation_customdata(me);
+
+       if (CustomData_has_layer(&me->pdata, CD_MTEXPOLY)) {
+               act = CustomData_get_active_layer(&me->pdata, CD_MTEXPOLY);
+               CustomData_set_layer_active(&me->ldata, CD_MLOOPUV, act);
+               CustomData_set_layer_active(&me->fdata, CD_MTFACE, act);
+
+               act = CustomData_get_render_layer(&me->pdata, CD_MTEXPOLY);
+               CustomData_set_layer_render(&me->ldata, CD_MLOOPUV, act);
+               CustomData_set_layer_render(&me->fdata, CD_MTFACE, act);
+
+               act = CustomData_get_clone_layer(&me->pdata, CD_MTEXPOLY);
+               CustomData_set_layer_clone(&me->ldata, CD_MLOOPUV, act);
+               CustomData_set_layer_clone(&me->fdata, CD_MTFACE, act);
+
+               act = CustomData_get_stencil_layer(&me->pdata, CD_MTEXPOLY);
+               CustomData_set_layer_stencil(&me->ldata, CD_MLOOPUV, act);
+               CustomData_set_layer_stencil(&me->fdata, CD_MTFACE, act);
+       }
+
+       if (CustomData_has_layer(&me->ldata, CD_MLOOPCOL)) {
+               act = CustomData_get_active_layer(&me->ldata, CD_MLOOPCOL);
+               CustomData_set_layer_active(&me->fdata, CD_MCOL, act);
+
+               act = CustomData_get_render_layer(&me->ldata, CD_MLOOPCOL);
+               CustomData_set_layer_render(&me->fdata, CD_MCOL, act);
+
+               act = CustomData_get_clone_layer(&me->ldata, CD_MLOOPCOL);
+               CustomData_set_layer_clone(&me->fdata, CD_MCOL, act);
+
+               act = CustomData_get_stencil_layer(&me->ldata, CD_MLOOPCOL);
+               CustomData_set_layer_stencil(&me->fdata, CD_MCOL, act);
+       }
+}
 
 void mesh_update_customdata_pointers(Mesh *me)
 {
+       mesh_update_linked_customdata(me);
+
        me->mvert = CustomData_get_layer(&me->vdata, CD_MVERT);
        me->dvert = CustomData_get_layer(&me->vdata, CD_MDEFORMVERT);
        me->msticky = CustomData_get_layer(&me->vdata, CD_MSTICKY);
@@ -87,6 +386,13 @@ void mesh_update_customdata_pointers(Mesh *me)
        me->mface = CustomData_get_layer(&me->fdata, CD_MFACE);
        me->mcol = CustomData_get_layer(&me->fdata, CD_MCOL);
        me->mtface = CustomData_get_layer(&me->fdata, CD_MTFACE);
+       
+       me->mpoly = CustomData_get_layer(&me->pdata, CD_MPOLY);
+       me->mloop = CustomData_get_layer(&me->ldata, CD_MLOOP);
+
+       me->mtpoly = CustomData_get_layer(&me->pdata, CD_MTEXPOLY);
+       me->mloopcol = CustomData_get_layer(&me->ldata, CD_MLOOPCOL);
+       me->mloopuv = CustomData_get_layer(&me->ldata, CD_MLOOPUV);
 }
 
 /* Note: unlinking is called when me->id.us is 0, question remains how
@@ -117,9 +423,10 @@ void unlink_mesh(Mesh *me)
 
 
 /* do not free mesh itself */
-void free_mesh(Mesh *me)
+void free_mesh(Mesh *me, int unlink)
 {
-       unlink_mesh(me);
+       if (unlink)
+               unlink_mesh(me);
 
        if(me->pv) {
                if(me->pv->vert_map) MEM_freeN(me->pv->vert_map);
@@ -135,7 +442,9 @@ void free_mesh(Mesh *me)
        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);
+
        if(me->adt) {
                BKE_free_animdata(&me->id);
                me->adt= NULL;
@@ -145,7 +454,7 @@ void free_mesh(Mesh *me)
        
        if(me->bb) MEM_freeN(me->bb);
        if(me->mselect) MEM_freeN(me->mselect);
-       if(me->edit_mesh) MEM_freeN(me->edit_mesh);
+       if(me->edit_btmesh) MEM_freeN(me->edit_btmesh);
 }
 
 void copy_dverts(MDeformVert *dst, MDeformVert *src, int copycount)
@@ -160,7 +469,7 @@ void copy_dverts(MDeformVert *dst, MDeformVert *src, int copycount)
        
        for (i=0; i<copycount; i++){
                if (src[i].dw){
-                       dst[i].dw = MEM_callocN (sizeof(MDeformWeight)*src[i].totweight, "copy_deformWeight");
+                       dst[i].dw = BLI_cellalloc_calloc (sizeof(MDeformWeight)*src[i].totweight, "copy_deformWeight");
                        memcpy (dst[i].dw, src[i].dw, sizeof (MDeformWeight)*src[i].totweight);
                }
        }
@@ -179,7 +488,7 @@ void free_dverts(MDeformVert *dvert, int totvert)
 
        /* Free any special data from the verts */
        for (i=0; i<totvert; i++){
-               if (dvert[i].dw) MEM_freeN (dvert[i].dw);
+               if (dvert[i].dw) BLI_cellalloc_free (dvert[i].dw);
        }
        MEM_freeN (dvert);
 }
@@ -204,6 +513,7 @@ Mesh *copy_mesh(Mesh *me)
 {
        Mesh *men;
        MTFace *tface;
+       MTexPoly *txface;
        int a, i;
        
        men= copy_libblock(me);
@@ -217,6 +527,8 @@ Mesh *copy_mesh(Mesh *me)
        CustomData_copy(&me->vdata, &men->vdata, CD_MASK_MESH, CD_DUPLICATE, men->totvert);
        CustomData_copy(&me->edata, &men->edata, CD_MASK_MESH, CD_DUPLICATE, men->totedge);
        CustomData_copy(&me->fdata, &men->fdata, CD_MASK_MESH, CD_DUPLICATE, men->totface);
+       CustomData_copy(&me->ldata, &men->ldata, CD_MASK_MESH, CD_DUPLICATE, men->totloop);
+       CustomData_copy(&me->pdata, &men->pdata, CD_MASK_MESH, CD_DUPLICATE, men->totpoly);
        mesh_update_customdata_pointers(men);
 
        /* ensure indirect linked data becomes lib-extern */
@@ -229,7 +541,17 @@ Mesh *copy_mesh(Mesh *me)
                                        id_lib_extern((ID*)tface->tpage);
                }
        }
-       
+
+       for(i=0; i<me->pdata.totlayer; i++) {
+               if(me->pdata.layers[i].type == CD_MTEXPOLY) {
+                       txface= (MTexPoly*)me->pdata.layers[i].data;
+
+                       for(a=0; a<me->totpoly; a++, txface++)
+                               if(txface->tpage)
+                                       id_lib_extern((ID*)txface->tpage);
+               }
+       }
+
        men->mselect= NULL;
 
        men->bb= MEM_dupallocN(men->bb);
@@ -240,12 +562,43 @@ Mesh *copy_mesh(Mesh *me)
        return men;
 }
 
+BMesh *BKE_mesh_to_bmesh(Mesh *me, Object *ob)
+{
+       BMesh *bm;
+       int allocsize[4] = {512,512,2048,512};
+
+       bm = BM_Make_Mesh(allocsize);
+
+       BMO_CallOpf(bm, "mesh_to_bmesh mesh=%p object=%p", me, ob);
+
+       return bm;
+}
+
 void make_local_tface(Mesh *me)
 {
        MTFace *tface;
+       MTexPoly *txface;
        Image *ima;
        int a, i;
        
+       for(i=0; i<me->pdata.totlayer; i++) {
+               if(me->pdata.layers[i].type == CD_MTEXPOLY) {
+                       txface= (MTexPoly*)me->fdata.layers[i].data;
+                       
+                       for(a=0; a<me->totpoly; a++, txface++) {
+                               /* special case: ima always local immediately */
+                               if(txface->tpage) {
+                                       ima= txface->tpage;
+                                       if(ima->id.lib) {
+                                               ima->id.lib= 0;
+                                               ima->id.flag= LIB_LOCAL;
+                                               new_id(0, (ID *)ima, 0);
+                                       }
+                               }
+                       }
+               }
+       }
+
        for(i=0; i<me->fdata.totlayer; i++) {
                if(me->fdata.layers[i].type == CD_MTFACE) {
                        tface= (MTFace*)me->fdata.layers[i].data;
@@ -263,6 +616,7 @@ void make_local_tface(Mesh *me)
                        }
                }
        }
+
 }
 
 void make_local_mesh(Mesh *me)
@@ -1076,10 +1430,10 @@ void mesh_to_curve(Scene *scene, Object *ob)
 
        MVert *mverts= dm->getVertArray(dm);
        MEdge *med, *medge= dm->getEdgeArray(dm);
-       MFace *mf,  *mface= dm->getFaceArray(dm);
+       MFace *mf,  *mface= dm->getTessFaceArray(dm);
 
        int totedge = dm->getNumEdges(dm);
-       int totface = dm->getNumFaces(dm);
+       int totface = dm->getNumTessFaces(dm);
        int totedges = 0;
        int i, needsFree = 0;
 
@@ -1536,6 +1890,285 @@ void mesh_pmv_off(Mesh *me)
        }
 }
 
+static void mesh_loops_to_corners(CustomData *fdata, CustomData *ldata, 
+                          CustomData *pdata, int lindex[4], int findex, 
+                          int polyindex, int numTex, int numCol, int tot) 
+{
+       MTFace *texface;
+       MTexPoly *texpoly;
+       MCol *mcol;
+       MLoopCol *mloopcol;
+       MLoopUV *mloopuv;
+       int i, j, hasWCol = CustomData_has_layer(ldata, CD_WEIGHT_MLOOPCOL);
+
+       for(i=0; i < numTex; i++){
+               texface = CustomData_get_n(fdata, CD_MTFACE, findex, i);
+               texpoly = CustomData_get_n(pdata, CD_MTEXPOLY, polyindex, i);
+               
+               texface->tpage = texpoly->tpage;
+               texface->flag = texpoly->flag;
+               texface->transp = texpoly->transp;
+               texface->mode = texpoly->mode;
+               texface->tile = texpoly->tile;
+               texface->unwrap = texpoly->unwrap;
+
+               for (j=0; j<tot; j++) {
+                       mloopuv = CustomData_get_n(ldata, CD_MLOOPUV, lindex[j], i);
+                       texface->uv[j][0] = mloopuv->uv[0];
+                       texface->uv[j][1] = mloopuv->uv[1];
+               }
+       }
+
+       for(i=0; i < numCol; i++){
+               mcol = CustomData_get_n(fdata, CD_MCOL, findex, i);
+
+               for (j=0; j<tot; j++) {
+                       mloopcol = CustomData_get_n(ldata, CD_MLOOPCOL, lindex[j], i);
+                       mcol[j].r = mloopcol->r;
+                       mcol[j].g = mloopcol->g;
+                       mcol[j].b = mloopcol->b;
+                       mcol[j].a = mloopcol->a;
+               }
+       }
+
+       if (hasWCol) {
+               mcol = CustomData_get(fdata,  findex, CD_WEIGHT_MCOL);
+
+               for (j=0; j<tot; j++) {
+                       mloopcol = CustomData_get(ldata, lindex[j], CD_WEIGHT_MLOOPCOL);
+                       mcol[j].r = mloopcol->r;
+                       mcol[j].g = mloopcol->g;
+                       mcol[j].b = mloopcol->b;
+                       mcol[j].a = mloopcol->a;
+               }
+       }
+}
+
+/*
+  this function recreates a tesselation.
+  returns number of tesselation faces.
+
+  use_poly_origindex sets whether or not the tesselation faces' origindex
+  layer should point to original poly indices or real poly indices.
+
+  use_face_origindex sets the tesselation faces' origindex layer
+  to point to the tesselation faces themselves, not the polys.
+
+  if both of the above are 0, it'll use the indices of the mpolys of the MPoly
+  data in pdata, and ignore the origindex layer altogether.
+ */
+int mesh_recalcTesselation(CustomData *fdata, 
+                           CustomData *ldata, CustomData *pdata,
+                           MVert *mvert, int totface, int totloop, 
+                           int totpoly, int use_poly_origindex, 
+                          int use_face_origindex)
+{
+       MPoly *mp, *mpoly;
+       MLoop *ml, *mloop;
+       MFace *mf = NULL, *mface;
+       BLI_array_declare(mf);
+       EditVert *v, *lastv, *firstv;
+       EditFace *f;
+       BLI_array_declare(origIndex);
+       int i, j, k, lindex[4], *origIndex = NULL, *polyorigIndex;
+       int numTex, numCol;
+
+       mpoly = CustomData_get_layer(pdata, CD_MPOLY);
+       mloop = CustomData_get_layer(ldata, CD_MLOOP);
+
+       numTex = CustomData_number_of_layers(ldata, CD_MLOOPUV);
+       numCol = CustomData_number_of_layers(ldata, CD_MLOOPCOL);
+       
+       k = 0;
+       mp = mpoly;
+       polyorigIndex = use_poly_origindex? CustomData_get_layer(pdata, CD_ORIGINDEX) : NULL;
+       for (i=0; i<totpoly; i++, mp++) {
+               if (mp->totloop > 2) {          
+                       ml = mloop + mp->loopstart;
+
+                       firstv = NULL;
+                       lastv = NULL;
+                       for (j=0; j<mp->totloop; j++, ml++) {
+                               v = BLI_addfillvert(mvert[ml->v].co);
+                               if (polyorigIndex && use_poly_origindex)
+                                       v->tmp.l = polyorigIndex[i];
+                               else
+                                       v->tmp.l = i;
+       
+                               v->keyindex = mp->loopstart + j;
+       
+                               if (lastv)
+                                       BLI_addfilledge(lastv, v);
+       
+                               if (!firstv)
+                                       firstv = v;
+                               lastv = v;
+                       }
+                       BLI_addfilledge(lastv, firstv);
+                       
+                       BLI_edgefill(0);
+                       for (f=fillfacebase.first; f; f=f->next) {
+                               BLI_array_growone(mf);
+                               BLI_array_growone(origIndex);
+       
+                               /*these are loop indices, they'll be transformed
+                                 into vert indices later.*/
+                               mf[k].v1 = f->v1->keyindex;
+                               mf[k].v2 = f->v2->keyindex;
+                               mf[k].v3 = f->v3->keyindex;
+                               
+                               /*put poly index in mf->v4*/
+                               mf[k].v4 = f->v1->tmp.l;
+                               
+                               mf[k].mat_nr = mp->mat_nr;
+                               mf[k].flag = mp->flag;
+                               origIndex[k] = use_face_origindex ? k : f->v1->tmp.l;
+       
+                               k++;
+                       }
+       
+                       BLI_end_edgefill();
+               }
+       }
+
+       CustomData_free(fdata, totface);
+       memset(fdata, 0, sizeof(CustomData));
+       totface = k;
+       
+       CustomData_add_layer(fdata, CD_MFACE, CD_ASSIGN, mf, totface);
+       CustomData_add_layer(fdata, CD_ORIGINDEX, CD_ASSIGN, origIndex, totface);
+       CustomData_from_bmeshpoly(fdata, pdata, ldata, totface);
+
+       mface = mf;
+       for (i=0; i<totface; i++, mf++) {
+               /*sort loop indices to ensure winding is correct*/
+               if (mf->v1 > mf->v2) SWAP(int, mf->v1, mf->v2);
+               if (mf->v2 > mf->v3) SWAP(int, mf->v2, mf->v3);
+               if (mf->v1 > mf->v2) SWAP(int, mf->v1, mf->v2);
+
+               if (mf->v1 > mf->v2) SWAP(int, mf->v1, mf->v2);
+               if (mf->v2 > mf->v3) SWAP(int, mf->v2, mf->v3);
+               if (mf->v1 > mf->v2) SWAP(int, mf->v1, mf->v2);
+       
+               lindex[0] = mf->v1;
+               lindex[1] = mf->v2;
+               lindex[2] = mf->v3;
+
+               /*transform loop indices to vert indices*/
+               mf->v1 = mloop[mf->v1].v;
+               mf->v2 = mloop[mf->v2].v;
+               mf->v3 = mloop[mf->v3].v;
+
+               mesh_loops_to_corners(fdata, ldata, pdata,
+                       lindex, i, mf->v4, numTex, numCol, 3);
+               
+               mf->v4 = 0;
+       }
+
+       return totface;
+}
+
+/*
+ * COMPUTE POLY NORMAL
+ *
+ * Computes the normal of a planar 
+ * polygon See Graphics Gems for 
+ * computing newell normal.
+ *
+*/
+static void mesh_calc_ngon_normal(MPoly *mpoly, MLoop *loopstart, 
+                                 MVert *mvert, float *normal)
+{
+
+       MVert *v1, *v2, *v3;
+       double u[3],  v[3], w[3];
+       double n[3] = {0.0, 0.0, 0.0}, l;
+       int i, s=0;
+
+       for(i = 0; i < mpoly->totloop; i++){
+               v1 = mvert + loopstart[i].v;
+               v2 = mvert + loopstart[(i+1)%mpoly->totloop].v;
+               v3 = mvert + loopstart[(i+2)%mpoly->totloop].v;
+               
+               VECCOPY(u, v1->co);
+               VECCOPY(v, v2->co);
+               VECCOPY(w, v3->co);
+
+               /*this fixes some weird numerical error*/
+               if (i==0) {
+                       u[0] += 0.0001f;
+                       u[1] += 0.0001f;
+                       u[2] += 0.0001f;
+               }
+               
+               /* newell's method
+               
+               so thats?:
+               (a[1] - b[1]) * (a[2] + b[2]);
+               a[1]*b[2] - b[1]*a[2] - b[1]*b[2] + a[1]*a[2]
+
+               odd.  half of that is the cross product. . .what's the
+               other half?
+
+               also could be like a[1]*(b[2] + a[2]) - b[1]*(a[2] - b[2])
+               */
+
+               n[0] += (u[1] - v[1]) * (u[2] + v[2]);
+               n[1] += (u[2] - v[2]) * (u[0] + v[0]);
+               n[2] += (u[0] - v[0]) * (u[1] + v[1]);
+       }
+       
+       l = n[0]*n[0]+n[1]*n[1]+n[2]*n[2];
+       l = sqrt(l);
+
+       if (l == 0.0) {
+               normal[0] = 0.0f;
+               normal[1] = 0.0f;
+               normal[2] = 1.0f;
+
+               return;
+       } else l = 1.0f / l;
+
+       n[0] *= l;
+       n[1] *= l;
+       n[2] *= l;
+       
+       normal[0] = (float) n[0];
+       normal[1] = (float) n[1];
+       normal[2] = (float) n[2];
+
+}
+
+void mesh_calc_poly_normal(MPoly *mpoly, MLoop *loopstart, 
+                           MVert *mvarray, float *no)
+{
+       if(mpoly->totloop > 4) {
+               mesh_calc_ngon_normal(mpoly, loopstart, mvarray, no);
+       }
+       else if(mpoly->totloop == 3){
+               MVert *v1, *v2, *v3;
+
+               v1 = mvarray + (loopstart++)->v;
+               v2 = mvarray + (loopstart++)->v;
+               v3 = mvarray + loopstart->v;
+               normal_tri_v3( no,v1->co, v2->co, v3->co);
+       }
+       else if(mpoly->totloop == 4){
+               MVert *v1, *v2, *v3, *v4;
+
+               v1 = mvarray + (loopstart++)->v;
+               v2 = mvarray + (loopstart++)->v;
+               v3 = mvarray + (loopstart++)->v;
+               v4 = mvarray + loopstart->v;
+               normal_quad_v3( no,v1->co, v2->co, v3->co, v4->co);
+       }
+       else{ /*horrible, two sided face!*/
+               no[0] = 0.0;
+               no[1] = 0.0;
+               no[2] = 1.0;
+       }
+}
+
 /* basic vertex data functions */
 int minmax_mesh(Mesh *me, float min[3], float max[3])
 {