merge with trunk at r27259 and commit of a patch by anthony jones to fix msvc (though...
[blender-staging.git] / source / blender / blenkernel / intern / mesh.c
index 6ef557ca8796c1e557c092652de11719c757572c..c7b769417937b4f66b8cc19450e186b1fc7e297f 100644 (file)
@@ -19,7 +19,7 @@
  *
  * You should have received a copy of the GNU General Public License
  * along with this program; if not, write to the Free Software Foundation,
- * Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
+ * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  *
  * The Original Code is Copyright (C) 2001-2002 by NaN Holding BV.
  * All rights reserved.
@@ -41,6 +41,7 @@
 #include "MEM_guardedalloc.h"
 
 #include "DNA_ID.h"
+#include "DNA_anim_types.h"
 #include "DNA_curve_types.h"
 #include "DNA_scene_types.h"
 #include "DNA_material_types.h"
@@ -51,6 +52,7 @@
 #include "DNA_meshdata_types.h"
 #include "DNA_ipo_types.h"
 
+#include "BKE_animsys.h"
 #include "BKE_customdata.h"
 #include "BKE_depsgraph.h"
 #include "BKE_main.h"
 /* -- */
 #include "BKE_object.h"
 #include "BKE_utildefines.h"
+#include "BKE_tessmesh.h"
 
 #include "BLI_blenlib.h"
 #include "BLI_editVert.h"
-#include "BLI_arithb.h"
+#include "BLI_math.h"
+#include "BLI_cellalloc.h"
+#include "BLI_array.h"
 #include "BLI_edgehash.h"
 
+#include "bmesh.h"
 
-EditMesh *BKE_mesh_get_editmesh(Mesh *me)
+static void mesh_ensure_tesselation_customdata(Mesh *me)
 {
-       return me->edit_mesh;
+       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");
+       }
 }
 
-void BKE_mesh_end_editmesh(Mesh *me, EditMesh *em)
+/*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);
@@ -95,6 +161,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
@@ -125,9 +198,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);
@@ -143,12 +217,19 @@ 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;
+       }
+       
        if(me->mat) MEM_freeN(me->mat);
        
        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)
@@ -163,7 +244,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);
                }
        }
@@ -182,7 +263,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);
 }
@@ -207,6 +288,7 @@ Mesh *copy_mesh(Mesh *me)
 {
        Mesh *men;
        MTFace *tface;
+       MTexPoly *txface;
        int a, i;
        
        men= copy_libblock(me);
@@ -220,6 +302,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 */
@@ -232,7 +316,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);
@@ -243,12 +337,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;
@@ -266,6 +391,7 @@ void make_local_tface(Mesh *me)
                        }
                }
        }
+
 }
 
 void make_local_mesh(Mesh *me)
@@ -956,10 +1082,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;
 
@@ -1086,7 +1212,7 @@ void mesh_to_curve(Scene *scene, Object *ob)
                                /* add points */
                                vl= polyline.first;
                                for (i=0, bp=nu->bp; i < totpoly; i++, bp++, vl=(VertLink *)vl->next) {
-                                       VecCopyf(bp->vec, mverts[vl->index].co);
+                                       copy_v3_v3(bp->vec, mverts[vl->index].co);
                                        bp->f1= SELECT;
                                        bp->radius = bp->weight = 1.0;
                                }
@@ -1146,23 +1272,23 @@ void mesh_calc_normals(MVert *mverts, int numVerts, MFace *mfaces, int numFaces,
                float *f_no= &fnors[i*3];
 
                if (mf->v4)
-                       CalcNormFloat4(mverts[mf->v1].co, mverts[mf->v2].co, mverts[mf->v3].co, mverts[mf->v4].co, f_no);
+                       normal_quad_v3( f_no,mverts[mf->v1].co, mverts[mf->v2].co, mverts[mf->v3].co, mverts[mf->v4].co);
                else
-                       CalcNormFloat(mverts[mf->v1].co, mverts[mf->v2].co, mverts[mf->v3].co, f_no);
+                       normal_tri_v3( f_no,mverts[mf->v1].co, mverts[mf->v2].co, mverts[mf->v3].co);
                
-               VecAddf(tnorms[mf->v1], tnorms[mf->v1], f_no);
-               VecAddf(tnorms[mf->v2], tnorms[mf->v2], f_no);
-               VecAddf(tnorms[mf->v3], tnorms[mf->v3], f_no);
+               add_v3_v3v3(tnorms[mf->v1], tnorms[mf->v1], f_no);
+               add_v3_v3v3(tnorms[mf->v2], tnorms[mf->v2], f_no);
+               add_v3_v3v3(tnorms[mf->v3], tnorms[mf->v3], f_no);
                if (mf->v4)
-                       VecAddf(tnorms[mf->v4], tnorms[mf->v4], f_no);
+                       add_v3_v3v3(tnorms[mf->v4], tnorms[mf->v4], f_no);
        }
        for (i=0; i<numVerts; i++) {
                MVert *mv= &mverts[i];
                float *no= tnorms[i];
                
-               if (Normalize(no)==0.0) {
+               if (normalize_v3(no)==0.0) {
                        VECCOPY(no, mv->co);
-                       Normalize(no);
+                       normalize_v3(no);
                }
 
                mv->no[0]= (short)(no[0]*32767.0);
@@ -1262,7 +1388,7 @@ UvVertMap *make_uv_vert_map(struct MFace *mface, struct MTFace *tface, unsigned
                                next= iterv->next;
 
                                uv2= (tf+iterv->f)->uv[iterv->tfindex];
-                               Vec2Subf(uvdiff, uv2, uv);
+                               sub_v2_v2v2(uvdiff, uv2, uv);
 
 
                                if(fabs(uv[0]-uv2[0]) < limit[0] && fabs(uv[1]-uv2[1]) < limit[1]) {
@@ -1409,3 +1535,275 @@ void mesh_pmv_off(Object *ob, Mesh *me)
                me->pv= NULL;
        }
 }
+
+static void mesh_loops_to_corners(CustomData *fdata, CustomData *ldata, 
+                          CustomData *pdata, int lindex[3], int findex, 
+                          int polyindex, int numTex, int numCol) 
+{
+       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<3; 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<3; 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<3; 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[3], *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++) {
+               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)
+                               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, 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;
+                       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++) {
+               /*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);
+               }
+
+               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, origIndex[i], numTex, numCol);
+       }
+
+       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;
+       }
+}