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 e74066cf52c6f27a93f93cb9f8049213f894707e..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,
  *
  * 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.
  *
  * The Original Code is Copyright (C) 2001-2002 by NaN Holding BV.
  * All rights reserved.
@@ -41,7 +41,9 @@
 #include "MEM_guardedalloc.h"
 
 #include "DNA_ID.h"
 #include "MEM_guardedalloc.h"
 
 #include "DNA_ID.h"
+#include "DNA_anim_types.h"
 #include "DNA_curve_types.h"
 #include "DNA_curve_types.h"
+#include "DNA_scene_types.h"
 #include "DNA_material_types.h"
 #include "DNA_object_types.h"
 #include "DNA_image_types.h"
 #include "DNA_material_types.h"
 #include "DNA_object_types.h"
 #include "DNA_image_types.h"
@@ -50,6 +52,7 @@
 #include "DNA_meshdata_types.h"
 #include "DNA_ipo_types.h"
 
 #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_customdata.h"
 #include "BKE_depsgraph.h"
 #include "BKE_main.h"
 
 #include "BLI_blenlib.h"
 #include "BLI_editVert.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"
 
 
 #include "bmesh.h"
 
-EditMesh *BKE_mesh_get_editmesh(Mesh *me)
+static void mesh_ensure_tesselation_customdata(Mesh *me)
 {
 {
-       return bmesh_to_editmesh(me->edit_btmesh->bm);
+       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)
 {
 {
-       BM_Free_Mesh(me->edit_btmesh->bm);
-       me->edit_btmesh->bm = editmesh_to_bmesh(em);
-       BMEdit_RecalcTesselation(me->edit_btmesh);
-}
+       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)
 {
 
 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);
        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);
@@ -135,9 +198,10 @@ void unlink_mesh(Mesh *me)
 
 
 /* do not free mesh itself */
 
 
 /* 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);
 
        if(me->pv) {
                if(me->pv->vert_map) MEM_freeN(me->pv->vert_map);
@@ -156,6 +220,11 @@ void free_mesh(Mesh *me)
        CustomData_free(&me->ldata, me->totloop);
        CustomData_free(&me->pdata, me->totpoly);
 
        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->mat) MEM_freeN(me->mat);
        
        if(me->bb) MEM_freeN(me->bb);
@@ -175,7 +244,7 @@ void copy_dverts(MDeformVert *dst, MDeformVert *src, int copycount)
        
        for (i=0; i<copycount; i++){
                if (src[i].dw){
        
        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);
                }
        }
                        memcpy (dst[i].dw, src[i].dw, sizeof (MDeformWeight)*src[i].totweight);
                }
        }
@@ -194,7 +263,7 @@ void free_dverts(MDeformVert *dvert, int totvert)
 
        /* Free any special data from the verts */
        for (i=0; i<totvert; i++){
 
        /* 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);
 }
        }
        MEM_freeN (dvert);
 }
@@ -268,14 +337,14 @@ Mesh *copy_mesh(Mesh *me)
        return men;
 }
 
        return men;
 }
 
-BMesh *BKE_mesh_to_bmesh(Mesh *me)
+BMesh *BKE_mesh_to_bmesh(Mesh *me, Object *ob)
 {
        BMesh *bm;
        int allocsize[4] = {512,512,2048,512};
 
        bm = BM_Make_Mesh(allocsize);
 
 {
        BMesh *bm;
        int allocsize[4] = {512,512,2048,512};
 
        bm = BM_Make_Mesh(allocsize);
 
-       BMO_CallOpf(bm, "mesh_to_bmesh mesh=%p", me);
+       BMO_CallOpf(bm, "mesh_to_bmesh mesh=%p object=%p", me, ob);
 
        return bm;
 }
 
        return bm;
 }
@@ -419,35 +488,12 @@ void boundbox_mesh(Mesh *me, float *loc, float *size)
 
 void tex_space_mesh(Mesh *me)
 {
 
 void tex_space_mesh(Mesh *me)
 {
-       KeyBlock *kb;
-       float *fp, loc[3], size[3], min[3], max[3];
+       float loc[3], size[3];
        int a;
 
        boundbox_mesh(me, loc, size);
 
        if(me->texflag & AUTOSPACE) {
        int a;
 
        boundbox_mesh(me, loc, size);
 
        if(me->texflag & AUTOSPACE) {
-               if(me->key) {
-                       kb= me->key->refkey;
-                       if (kb) {
-                               
-                               INIT_MINMAX(min, max);
-                               
-                               fp= kb->data;
-                               for(a=0; a<kb->totelem; a++, fp+=3) {   
-                                       DO_MINMAX(fp, min, max);
-                               }
-                               if(kb->totelem) {
-                                       loc[0]= (min[0]+max[0])/2.0f; loc[1]= (min[1]+max[1])/2.0f; loc[2]= (min[2]+max[2])/2.0f;
-                                       size[0]= (max[0]-min[0])/2.0f; size[1]= (max[1]-min[1])/2.0f; size[2]= (max[2]-min[2])/2.0f;
-                               }
-                               else {
-                                       loc[0]= loc[1]= loc[2]= 0.0;
-                                       size[0]= size[1]= size[2]= 0.0;
-                               }
-                               
-                       }
-               }
-
                for (a=0; a<3; a++) {
                        if(size[a]==0.0) size[a]= 1.0;
                        else if(size[a]>0.0 && size[a]<0.00001) size[a]= 0.00001;
                for (a=0; a<3; a++) {
                        if(size[a]==0.0) size[a]= 1.0;
                        else if(size[a]>0.0 && size[a]<0.00001) size[a]= 0.00001;
@@ -487,26 +533,20 @@ void mesh_get_texspace(Mesh *me, float *loc_r, float *rot_r, float *size_r)
 float *get_mesh_orco_verts(Object *ob)
 {
        Mesh *me = ob->data;
 float *get_mesh_orco_verts(Object *ob)
 {
        Mesh *me = ob->data;
+       MVert *mvert = NULL;
+       Mesh *tme = me->texcomesh?me->texcomesh:me;
        int a, totvert;
        float (*vcos)[3] = NULL;
 
        /* Get appropriate vertex coordinates */
        int a, totvert;
        float (*vcos)[3] = NULL;
 
        /* Get appropriate vertex coordinates */
-       if(me->key && me->texcomesh==0 && me->key->refkey) {
-               vcos= mesh_getRefKeyCos(me, &totvert);
-       }
-       else {
-               MVert *mvert = NULL;            
-               Mesh *tme = me->texcomesh?me->texcomesh:me;
-
-               vcos = MEM_callocN(sizeof(*vcos)*me->totvert, "orco mesh");
-               mvert = tme->mvert;
-               totvert = MIN2(tme->totvert, me->totvert);
-
-               for(a=0; a<totvert; a++, mvert++) {
-                       vcos[a][0]= mvert->co[0];
-                       vcos[a][1]= mvert->co[1];
-                       vcos[a][2]= mvert->co[2];
-               }
+       vcos = MEM_callocN(sizeof(*vcos)*me->totvert, "orco mesh");
+       mvert = tme->mvert;
+       totvert = MIN2(tme->totvert, me->totvert);
+
+       for(a=0; a<totvert; a++, mvert++) {
+               vcos[a][0]= mvert->co[0];
+               vcos[a][1]= mvert->co[1];
+               vcos[a][2]= mvert->co[2];
        }
 
        return (float*)vcos;
        }
 
        return (float*)vcos;
@@ -601,7 +641,8 @@ void set_mesh(Object *ob, Mesh *me)
        
        if(ob->type==OB_MESH) {
                old= ob->data;
        
        if(ob->type==OB_MESH) {
                old= ob->data;
-               old->id.us--;
+               if (old)
+                       old->id.us--;
                ob->data= me;
                id_us_plus((ID *)me);
        }
                ob->data= me;
                id_us_plus((ID *)me);
        }
@@ -1010,6 +1051,187 @@ void nurbs_to_mesh(Object *ob)
 
 }
 
 
 }
 
+typedef struct EdgeLink {
+       Link *next, *prev;
+       void *edge;
+} EdgeLink;
+
+typedef struct VertLink {
+       Link *next, *prev;
+       int index;
+} VertLink;
+
+static void prependPolyLineVert(ListBase *lb, int index)
+{
+       VertLink *vl= MEM_callocN(sizeof(VertLink), "VertLink");
+       vl->index = index;
+       BLI_addhead(lb, vl);
+}
+
+static void appendPolyLineVert(ListBase *lb, int index)
+{
+       VertLink *vl= MEM_callocN(sizeof(VertLink), "VertLink");
+       vl->index = index;
+       BLI_addtail(lb, vl);
+}
+
+void mesh_to_curve(Scene *scene, Object *ob)
+{
+       /* make new mesh data from the original copy */
+       DerivedMesh *dm= mesh_get_derived_final(scene, ob, CD_MASK_MESH);
+
+       MVert *mverts= dm->getVertArray(dm);
+       MEdge *med, *medge= dm->getEdgeArray(dm);
+       MFace *mf,  *mface= dm->getTessFaceArray(dm);
+
+       int totedge = dm->getNumEdges(dm);
+       int totface = dm->getNumTessFaces(dm);
+       int totedges = 0;
+       int i;
+
+       /* only to detect edge polylines */
+       EdgeHash *eh = BLI_edgehash_new();
+       EdgeHash *eh_edge = BLI_edgehash_new();
+
+
+       ListBase edges = {NULL, NULL};
+
+       /* create edges from all faces (so as to find edges not in any faces) */
+       mf= mface;
+       for (i = 0; i < totface; i++, mf++) {
+               if (!BLI_edgehash_haskey(eh, mf->v1, mf->v2))
+                       BLI_edgehash_insert(eh, mf->v1, mf->v2, NULL);
+               if (!BLI_edgehash_haskey(eh, mf->v2, mf->v3))
+                       BLI_edgehash_insert(eh, mf->v2, mf->v3, NULL);
+
+               if (mf->v4) {
+                       if (!BLI_edgehash_haskey(eh, mf->v3, mf->v4))
+                               BLI_edgehash_insert(eh, mf->v3, mf->v4, NULL);
+                       if (!BLI_edgehash_haskey(eh, mf->v4, mf->v1))
+                               BLI_edgehash_insert(eh, mf->v4, mf->v1, NULL);
+               } else {
+                       if (!BLI_edgehash_haskey(eh, mf->v3, mf->v1))
+                               BLI_edgehash_insert(eh, mf->v3, mf->v1, NULL);
+               }
+       }
+
+       med= medge;
+       for(i=0; i<totedge; i++, med++) {
+               if (!BLI_edgehash_haskey(eh, med->v1, med->v2)) {
+                       EdgeLink *edl= MEM_callocN(sizeof(EdgeLink), "EdgeLink");
+
+                       BLI_edgehash_insert(eh_edge, med->v1, med->v2, NULL);
+                       edl->edge= med;
+
+                       BLI_addtail(&edges, edl);       totedges++;
+               }
+       }
+       BLI_edgehash_free(eh_edge, NULL);
+       BLI_edgehash_free(eh, NULL);
+
+       if(edges.first) {
+               Curve *cu = add_curve(ob->id.name+2, OB_CURVE);
+               cu->flag |= CU_3D;
+
+               while(edges.first) {
+                       /* each iteration find a polyline and add this as a nurbs poly spline */
+
+                       ListBase polyline = {NULL, NULL}; /* store a list of VertLink's */
+                       int closed = FALSE;
+                       int totpoly= 0;
+                       MEdge *med_current= ((EdgeLink *)edges.last)->edge;
+                       int startVert= med_current->v1;
+                       int endVert= med_current->v2;
+                       int ok= TRUE;
+
+                       appendPolyLineVert(&polyline, startVert);       totpoly++;
+                       appendPolyLineVert(&polyline, endVert);         totpoly++;
+                       BLI_freelinkN(&edges, edges.last);                      totedges--;
+
+                       while(ok) { /* while connected edges are found... */
+                               ok = FALSE;
+                               i= totedges;
+                               while(i) {
+                                       EdgeLink *edl;
+
+                                       i-=1;
+                                       edl= BLI_findlink(&edges, i);
+                                       med= edl->edge;
+
+                                       if(med->v1==endVert) {
+                                               endVert = med->v2;
+                                               appendPolyLineVert(&polyline, med->v2); totpoly++;
+                                               BLI_freelinkN(&edges, edl);                             totedges--;
+                                               ok= TRUE;
+                                       }
+                                       else if(med->v2==endVert) {
+                                               endVert = med->v1;
+                                               appendPolyLineVert(&polyline, endVert); totpoly++;
+                                               BLI_freelinkN(&edges, edl);                             totedges--;
+                                               ok= TRUE;
+                                       }
+                                       else if(med->v1==startVert) {
+                                               startVert = med->v2;
+                                               prependPolyLineVert(&polyline, startVert);      totpoly++;
+                                               BLI_freelinkN(&edges, edl);                                     totedges--;
+                                               ok= TRUE;
+                                       }
+                                       else if(med->v2==startVert) {
+                                               startVert = med->v1;
+                                               prependPolyLineVert(&polyline, startVert);      totpoly++;
+                                               BLI_freelinkN(&edges, edl);                                     totedges--;
+                                               ok= TRUE;
+                                       }
+                               }
+                       }
+
+                       /* Now we have a polyline, make into a curve */
+                       if(startVert==endVert) {
+                               BLI_freelinkN(&polyline, polyline.last);
+                               totpoly--;
+                               closed = TRUE;
+                       }
+
+                       /* --- nurbs --- */
+                       {
+                               Nurb *nu;
+                               BPoint *bp;
+                               VertLink *vl;
+
+                               /* create new 'nurb' within the curve */
+                               nu = (Nurb *)MEM_callocN(sizeof(Nurb), "MeshNurb");
+
+                               nu->pntsu= totpoly;
+                               nu->pntsv= 1;
+                               nu->orderu= 4;
+                               nu->flagu= 2 | (closed ? CU_CYCLIC:0);  /* endpoint */
+                               nu->resolu= 12;
+
+                               nu->bp= (BPoint *)MEM_callocN(sizeof(BPoint)*totpoly, "bpoints");
+
+                               /* add points */
+                               vl= polyline.first;
+                               for (i=0, bp=nu->bp; i < totpoly; i++, bp++, vl=(VertLink *)vl->next) {
+                                       copy_v3_v3(bp->vec, mverts[vl->index].co);
+                                       bp->f1= SELECT;
+                                       bp->radius = bp->weight = 1.0;
+                               }
+                               BLI_freelistN(&polyline);
+
+                               /* add nurb to curve */
+                               BLI_addtail(&cu->nurb, nu);
+                       }
+                       /* --- done with nurbs --- */
+               }
+
+               ((Mesh *)ob->data)->id.us--;
+               ob->data= cu;
+               ob->type= OB_CURVE;
+       }
+
+       dm->release(dm);
+}
+
 void mesh_delete_material_index(Mesh *me, int index)
 {
        int i;
 void mesh_delete_material_index(Mesh *me, int index)
 {
        int i;
@@ -1036,7 +1258,7 @@ void mesh_set_smooth_flag(Object *meshOb, int enableSmooth)
                }
        }
 
                }
        }
 
-// XXX do this in caller       DAG_object_flush_update(scene, meshOb, OB_RECALC_DATA);
+// 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) 
 }
 
 void mesh_calc_normals(MVert *mverts, int numVerts, MFace *mfaces, int numFaces, float **faceNors_r) 
@@ -1050,23 +1272,23 @@ void mesh_calc_normals(MVert *mverts, int numVerts, MFace *mfaces, int numFaces,
                float *f_no= &fnors[i*3];
 
                if (mf->v4)
                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
                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)
                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];
                
        }
        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);
                        VECCOPY(no, mv->co);
-                       Normalize(no);
+                       normalize_v3(no);
                }
 
                mv->no[0]= (short)(no[0]*32767.0);
                }
 
                mv->no[0]= (short)(no[0]*32767.0);
@@ -1095,29 +1317,6 @@ float (*mesh_getVertexCos(Mesh *me, int *numVerts_r))[3]
        return cos;
 }
 
        return cos;
 }
 
-float (*mesh_getRefKeyCos(Mesh *me, int *numVerts_r))[3]
-{
-       KeyBlock *kb;
-       float (*cos)[3] = NULL;
-       int totvert;
-       
-       if(me->key && me->key->refkey) {
-               if(numVerts_r) *numVerts_r= me->totvert;
-               
-               kb= me->key->refkey;
-               
-               /* prevent accessing invalid memory */
-               if (me->totvert > kb->totelem)          cos= MEM_callocN(sizeof(*cos)*me->totvert, "vertexcos1");
-               else                                                            cos= MEM_mallocN(sizeof(*cos)*me->totvert, "vertexcos1");
-               
-               totvert= MIN2(kb->totelem, me->totvert);
-
-               memcpy(cos, kb->data, sizeof(*cos)*totvert);
-       }
-
-       return cos;
-}
-
 UvVertMap *make_uv_vert_map(struct MFace *mface, struct MTFace *tface, unsigned int totface, unsigned int totvert, int selected, float *limit)
 {
        UvVertMap *vmap;
 UvVertMap *make_uv_vert_map(struct MFace *mface, struct MTFace *tface, unsigned int totface, unsigned int totvert, int selected, float *limit)
 {
        UvVertMap *vmap;
@@ -1189,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];
                                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]) {
 
 
                                if(fabs(uv[0]-uv2[0]) < limit[0] && fabs(uv[1]-uv2[1]) < limit[1]) {
@@ -1324,7 +1523,7 @@ void mesh_pmv_revert(Object *ob, Mesh *me)
                MEM_freeN(me->pv->vert_map);
                me->pv->vert_map= NULL;
 
                MEM_freeN(me->pv->vert_map);
                me->pv->vert_map= NULL;
 
-// XXX do this in caller               DAG_object_flush_update(scene, ob, OB_RECALC_DATA);
+// XXX do this in caller               DAG_id_flush_update(&me->id, OB_RECALC_DATA);
        }
 }
 
        }
 }
 
@@ -1336,3 +1535,275 @@ void mesh_pmv_off(Object *ob, Mesh *me)
                me->pv= NULL;
        }
 }
                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;
+       }
+}