Mesh Animation + Depsgraph Tweaks:
[blender.git] / source / blender / blenkernel / intern / mesh.c
1
2 /*  mesh.c
3  *
4  *  
5  * 
6  * $Id$
7  *
8  * ***** BEGIN GPL LICENSE BLOCK *****
9  *
10  * This program is free software; you can redistribute it and/or
11  * modify it under the terms of the GNU General Public License
12  * as published by the Free Software Foundation; either version 2
13  * of the License, or (at your option) any later version.
14  *
15  * This program is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU General Public License for more details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with this program; if not, write to the Free Software Foundation,
22  * Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
23  *
24  * The Original Code is Copyright (C) 2001-2002 by NaN Holding BV.
25  * All rights reserved.
26  *
27  * Contributor(s): Blender Foundation
28  *
29  * ***** END GPL LICENSE BLOCK *****
30  */
31
32 #ifdef HAVE_CONFIG_H
33 #include <config.h>
34 #endif
35
36 #include <stdlib.h>
37 #include <string.h>
38 #include <stdio.h>
39 #include <math.h>
40
41 #include "MEM_guardedalloc.h"
42
43 #include "DNA_ID.h"
44 #include "DNA_curve_types.h"
45 #include "DNA_scene_types.h"
46 #include "DNA_material_types.h"
47 #include "DNA_object_types.h"
48 #include "DNA_image_types.h"
49 #include "DNA_key_types.h"
50 #include "DNA_mesh_types.h"
51 #include "DNA_meshdata_types.h"
52 #include "DNA_ipo_types.h"
53
54 #include "BKE_customdata.h"
55 #include "BKE_depsgraph.h"
56 #include "BKE_main.h"
57 #include "BKE_DerivedMesh.h"
58 #include "BKE_global.h"
59 #include "BKE_mesh.h"
60 #include "BKE_subsurf.h"
61 #include "BKE_displist.h"
62 #include "BKE_library.h"
63 #include "BKE_material.h"
64 #include "BKE_key.h"
65 /* these 2 are only used by conversion functions */
66 #include "BKE_curve.h"
67 /* -- */
68 #include "BKE_object.h"
69 #include "BKE_utildefines.h"
70
71 #include "BLI_blenlib.h"
72 #include "BLI_editVert.h"
73 #include "BLI_math.h"
74 #include "BLI_edgehash.h"
75
76
77 EditMesh *BKE_mesh_get_editmesh(Mesh *me)
78 {
79         return me->edit_mesh;
80 }
81
82 void BKE_mesh_end_editmesh(Mesh *me, EditMesh *em)
83 {
84 }
85
86
87 void mesh_update_customdata_pointers(Mesh *me)
88 {
89         me->mvert = CustomData_get_layer(&me->vdata, CD_MVERT);
90         me->dvert = CustomData_get_layer(&me->vdata, CD_MDEFORMVERT);
91         me->msticky = CustomData_get_layer(&me->vdata, CD_MSTICKY);
92
93         me->medge = CustomData_get_layer(&me->edata, CD_MEDGE);
94
95         me->mface = CustomData_get_layer(&me->fdata, CD_MFACE);
96         me->mcol = CustomData_get_layer(&me->fdata, CD_MCOL);
97         me->mtface = CustomData_get_layer(&me->fdata, CD_MTFACE);
98 }
99
100 /* Note: unlinking is called when me->id.us is 0, question remains how
101  * much unlinking of Library data in Mesh should be done... probably
102  * we need a more generic method, like the expand() functions in
103  * readfile.c */
104
105 void unlink_mesh(Mesh *me)
106 {
107         int a;
108         
109         if(me==0) return;
110         
111         for(a=0; a<me->totcol; a++) {
112                 if(me->mat[a]) me->mat[a]->id.us--;
113                 me->mat[a]= 0;
114         }
115
116         if(me->key) {
117                 me->key->id.us--;
118                 if (me->key->id.us == 0 && me->key->ipo )
119                         me->key->ipo->id.us--;
120         }
121         me->key= 0;
122         
123         if(me->texcomesh) me->texcomesh= 0;
124 }
125
126
127 /* do not free mesh itself */
128 void free_mesh(Mesh *me)
129 {
130         unlink_mesh(me);
131
132         if(me->pv) {
133                 if(me->pv->vert_map) MEM_freeN(me->pv->vert_map);
134                 if(me->pv->edge_map) MEM_freeN(me->pv->edge_map);
135                 if(me->pv->old_faces) MEM_freeN(me->pv->old_faces);
136                 if(me->pv->old_edges) MEM_freeN(me->pv->old_edges);
137                 me->totvert= me->pv->totvert;
138                 me->totedge= me->pv->totedge;
139                 me->totface= me->pv->totface;
140                 MEM_freeN(me->pv);
141         }
142
143         CustomData_free(&me->vdata, me->totvert);
144         CustomData_free(&me->edata, me->totedge);
145         CustomData_free(&me->fdata, me->totface);
146         
147         if(me->adt) {
148                 BKE_free_animdata(&me->id);
149                 me->adt= NULL;
150         }
151         
152         if(me->mat) MEM_freeN(me->mat);
153         
154         if(me->bb) MEM_freeN(me->bb);
155         if(me->mselect) MEM_freeN(me->mselect);
156         if(me->edit_mesh) MEM_freeN(me->edit_mesh);
157 }
158
159 void copy_dverts(MDeformVert *dst, MDeformVert *src, int copycount)
160 {
161         /* Assumes dst is already set up */
162         int i;
163
164         if (!src || !dst)
165                 return;
166
167         memcpy (dst, src, copycount * sizeof(MDeformVert));
168         
169         for (i=0; i<copycount; i++){
170                 if (src[i].dw){
171                         dst[i].dw = MEM_callocN (sizeof(MDeformWeight)*src[i].totweight, "copy_deformWeight");
172                         memcpy (dst[i].dw, src[i].dw, sizeof (MDeformWeight)*src[i].totweight);
173                 }
174         }
175
176 }
177
178 void free_dverts(MDeformVert *dvert, int totvert)
179 {
180         /* Instead of freeing the verts directly,
181         call this function to delete any special
182         vert data */
183         int     i;
184
185         if (!dvert)
186                 return;
187
188         /* Free any special data from the verts */
189         for (i=0; i<totvert; i++){
190                 if (dvert[i].dw) MEM_freeN (dvert[i].dw);
191         }
192         MEM_freeN (dvert);
193 }
194
195 Mesh *add_mesh(char *name)
196 {
197         Mesh *me;
198         
199         me= alloc_libblock(&G.main->mesh, ID_ME, name);
200         
201         me->size[0]= me->size[1]= me->size[2]= 1.0;
202         me->smoothresh= 30;
203         me->texflag= AUTOSPACE;
204         me->flag= ME_TWOSIDED;
205         me->bb= unit_boundbox();
206         me->drawflag= ME_DRAWEDGES|ME_DRAWFACES|ME_DRAWCREASES;
207         
208         return me;
209 }
210
211 Mesh *copy_mesh(Mesh *me)
212 {
213         Mesh *men;
214         MTFace *tface;
215         int a, i;
216         
217         men= copy_libblock(me);
218         
219         men->mat= MEM_dupallocN(me->mat);
220         for(a=0; a<men->totcol; a++) {
221                 id_us_plus((ID *)men->mat[a]);
222         }
223         id_us_plus((ID *)men->texcomesh);
224
225         CustomData_copy(&me->vdata, &men->vdata, CD_MASK_MESH, CD_DUPLICATE, men->totvert);
226         CustomData_copy(&me->edata, &men->edata, CD_MASK_MESH, CD_DUPLICATE, men->totedge);
227         CustomData_copy(&me->fdata, &men->fdata, CD_MASK_MESH, CD_DUPLICATE, men->totface);
228         mesh_update_customdata_pointers(men);
229
230         /* ensure indirect linked data becomes lib-extern */
231         for(i=0; i<me->fdata.totlayer; i++) {
232                 if(me->fdata.layers[i].type == CD_MTFACE) {
233                         tface= (MTFace*)me->fdata.layers[i].data;
234
235                         for(a=0; a<me->totface; a++, tface++)
236                                 if(tface->tpage)
237                                         id_lib_extern((ID*)tface->tpage);
238                 }
239         }
240         
241         men->mselect= NULL;
242
243         men->bb= MEM_dupallocN(men->bb);
244         
245         men->key= copy_key(me->key);
246         if(men->key) men->key->from= (ID *)men;
247
248         return men;
249 }
250
251 void make_local_tface(Mesh *me)
252 {
253         MTFace *tface;
254         Image *ima;
255         int a, i;
256         
257         for(i=0; i<me->fdata.totlayer; i++) {
258                 if(me->fdata.layers[i].type == CD_MTFACE) {
259                         tface= (MTFace*)me->fdata.layers[i].data;
260                         
261                         for(a=0; a<me->totface; a++, tface++) {
262                                 /* special case: ima always local immediately */
263                                 if(tface->tpage) {
264                                         ima= tface->tpage;
265                                         if(ima->id.lib) {
266                                                 ima->id.lib= 0;
267                                                 ima->id.flag= LIB_LOCAL;
268                                                 new_id(0, (ID *)ima, 0);
269                                         }
270                                 }
271                         }
272                 }
273         }
274 }
275
276 void make_local_mesh(Mesh *me)
277 {
278         Object *ob;
279         Mesh *men;
280         int local=0, lib=0;
281
282         /* - only lib users: do nothing
283             * - only local users: set flag
284             * - mixed: make copy
285             */
286         
287         if(me->id.lib==0) return;
288         if(me->id.us==1) {
289                 me->id.lib= 0;
290                 me->id.flag= LIB_LOCAL;
291                 new_id(0, (ID *)me, 0);
292                 
293                 if(me->mtface) make_local_tface(me);
294                 
295                 return;
296         }
297         
298         ob= G.main->object.first;
299         while(ob) {
300                 if( me==get_mesh(ob) ) {
301                         if(ob->id.lib) lib= 1;
302                         else local= 1;
303                 }
304                 ob= ob->id.next;
305         }
306         
307         if(local && lib==0) {
308                 me->id.lib= 0;
309                 me->id.flag= LIB_LOCAL;
310                 new_id(0, (ID *)me, 0);
311                 
312                 if(me->mtface) make_local_tface(me);
313                 
314         }
315         else if(local && lib) {
316                 men= copy_mesh(me);
317                 men->id.us= 0;
318                 
319                 ob= G.main->object.first;
320                 while(ob) {
321                         if( me==get_mesh(ob) ) {                                
322                                 if(ob->id.lib==0) {
323                                         set_mesh(ob, men);
324                                 }
325                         }
326                         ob= ob->id.next;
327                 }
328         }
329 }
330
331 void boundbox_mesh(Mesh *me, float *loc, float *size)
332 {
333         MVert *mvert;
334         BoundBox *bb;
335         float min[3], max[3];
336         float mloc[3], msize[3];
337         int a;
338         
339         if(me->bb==0) me->bb= MEM_callocN(sizeof(BoundBox), "boundbox");
340         bb= me->bb;
341         
342         INIT_MINMAX(min, max);
343
344         if (!loc) loc= mloc;
345         if (!size) size= msize;
346         
347         mvert= me->mvert;
348         for(a=0; a<me->totvert; a++, mvert++) {
349                 DO_MINMAX(mvert->co, min, max);
350         }
351
352         if(!me->totvert) {
353                 min[0] = min[1] = min[2] = -1.0f;
354                 max[0] = max[1] = max[2] = 1.0f;
355         }
356
357         loc[0]= (min[0]+max[0])/2.0f;
358         loc[1]= (min[1]+max[1])/2.0f;
359         loc[2]= (min[2]+max[2])/2.0f;
360                 
361         size[0]= (max[0]-min[0])/2.0f;
362         size[1]= (max[1]-min[1])/2.0f;
363         size[2]= (max[2]-min[2])/2.0f;
364         
365         boundbox_set_from_min_max(bb, min, max);
366 }
367
368 void tex_space_mesh(Mesh *me)
369 {
370         float loc[3], size[3];
371         int a;
372
373         boundbox_mesh(me, loc, size);
374
375         if(me->texflag & AUTOSPACE) {
376                 for (a=0; a<3; a++) {
377                         if(size[a]==0.0) size[a]= 1.0;
378                         else if(size[a]>0.0 && size[a]<0.00001) size[a]= 0.00001;
379                         else if(size[a]<0.0 && size[a]> -0.00001) size[a]= -0.00001;
380                 }
381
382                 VECCOPY(me->loc, loc);
383                 VECCOPY(me->size, size);
384                 me->rot[0]= me->rot[1]= me->rot[2]= 0.0;
385         }
386 }
387
388 BoundBox *mesh_get_bb(Object *ob)
389 {
390         Mesh *me= ob->data;
391
392         if(ob->bb)
393                 return ob->bb;
394
395         if (!me->bb)
396                 tex_space_mesh(me);
397
398         return me->bb;
399 }
400
401 void mesh_get_texspace(Mesh *me, float *loc_r, float *rot_r, float *size_r)
402 {
403         if (!me->bb) {
404                 tex_space_mesh(me);
405         }
406
407         if (loc_r) VECCOPY(loc_r, me->loc);
408         if (rot_r) VECCOPY(rot_r, me->rot);
409         if (size_r) VECCOPY(size_r, me->size);
410 }
411
412 float *get_mesh_orco_verts(Object *ob)
413 {
414         Mesh *me = ob->data;
415         MVert *mvert = NULL;
416         Mesh *tme = me->texcomesh?me->texcomesh:me;
417         int a, totvert;
418         float (*vcos)[3] = NULL;
419
420         /* Get appropriate vertex coordinates */
421         vcos = MEM_callocN(sizeof(*vcos)*me->totvert, "orco mesh");
422         mvert = tme->mvert;
423         totvert = MIN2(tme->totvert, me->totvert);
424
425         for(a=0; a<totvert; a++, mvert++) {
426                 vcos[a][0]= mvert->co[0];
427                 vcos[a][1]= mvert->co[1];
428                 vcos[a][2]= mvert->co[2];
429         }
430
431         return (float*)vcos;
432 }
433
434 void transform_mesh_orco_verts(Mesh *me, float (*orco)[3], int totvert, int invert)
435 {
436         float loc[3], size[3];
437         int a;
438
439         mesh_get_texspace(me->texcomesh?me->texcomesh:me, loc, NULL, size);
440
441         if(invert) {
442                 for(a=0; a<totvert; a++) {
443                         float *co = orco[a];
444                         co[0] = co[0]*size[0] + loc[0];
445                         co[1] = co[1]*size[1] + loc[1];
446                         co[2] = co[2]*size[2] + loc[2];
447                 }
448         }
449         else {
450                 for(a=0; a<totvert; a++) {
451                         float *co = orco[a];
452                         co[0] = (co[0]-loc[0])/size[0];
453                         co[1] = (co[1]-loc[1])/size[1];
454                         co[2] = (co[2]-loc[2])/size[2];
455                 }
456         }
457 }
458
459 /* rotates the vertices of a face in case v[2] or v[3] (vertex index) is = 0.
460    this is necessary to make the if(mface->v4) check for quads work */
461 int test_index_face(MFace *mface, CustomData *fdata, int mfindex, int nr)
462 {
463         /* first test if the face is legal */
464         if(mface->v3 && mface->v3==mface->v4) {
465                 mface->v4= 0;
466                 nr--;
467         }
468         if(mface->v2 && mface->v2==mface->v3) {
469                 mface->v3= mface->v4;
470                 mface->v4= 0;
471                 nr--;
472         }
473         if(mface->v1==mface->v2) {
474                 mface->v2= mface->v3;
475                 mface->v3= mface->v4;
476                 mface->v4= 0;
477                 nr--;
478         }
479
480         /* prevent a zero at wrong index location */
481         if(nr==3) {
482                 if(mface->v3==0) {
483                         static int corner_indices[4] = {1, 2, 0, 3};
484
485                         SWAP(int, mface->v1, mface->v2);
486                         SWAP(int, mface->v2, mface->v3);
487
488                         if(fdata)
489                                 CustomData_swap(fdata, mfindex, corner_indices);
490                 }
491         }
492         else if(nr==4) {
493                 if(mface->v3==0 || mface->v4==0) {
494                         static int corner_indices[4] = {2, 3, 0, 1};
495
496                         SWAP(int, mface->v1, mface->v3);
497                         SWAP(int, mface->v2, mface->v4);
498
499                         if(fdata)
500                                 CustomData_swap(fdata, mfindex, corner_indices);
501                 }
502         }
503
504         return nr;
505 }
506
507 Mesh *get_mesh(Object *ob)
508 {
509         
510         if(ob==0) return 0;
511         if(ob->type==OB_MESH) return ob->data;
512         else return 0;
513 }
514
515 void set_mesh(Object *ob, Mesh *me)
516 {
517         Mesh *old=0;
518         
519         if(ob==0) return;
520         
521         if(ob->type==OB_MESH) {
522                 old= ob->data;
523                 if (old)
524                         old->id.us--;
525                 ob->data= me;
526                 id_us_plus((ID *)me);
527         }
528         
529         test_object_materials((ID *)me);
530 }
531
532 /* ************** make edges in a Mesh, for outside of editmode */
533
534 struct edgesort {
535         int v1, v2;
536         short is_loose, is_draw;
537 };
538
539 /* edges have to be added with lowest index first for sorting */
540 static void to_edgesort(struct edgesort *ed, int v1, int v2, short is_loose, short is_draw)
541 {
542         if(v1<v2) {
543                 ed->v1= v1; ed->v2= v2;
544         }
545         else {
546                 ed->v1= v2; ed->v2= v1;
547         }
548         ed->is_loose= is_loose;
549         ed->is_draw= is_draw;
550 }
551
552 static int vergedgesort(const void *v1, const void *v2)
553 {
554         const struct edgesort *x1=v1, *x2=v2;
555
556         if( x1->v1 > x2->v1) return 1;
557         else if( x1->v1 < x2->v1) return -1;
558         else if( x1->v2 > x2->v2) return 1;
559         else if( x1->v2 < x2->v2) return -1;
560         
561         return 0;
562 }
563
564 void make_edges(Mesh *me, int old)
565 {
566         MFace *mface;
567         MEdge *medge;
568         struct edgesort *edsort, *ed;
569         int a, totedge=0, final=0;
570         
571         /* we put all edges in array, sort them, and detect doubles that way */
572         
573         for(a= me->totface, mface= me->mface; a>0; a--, mface++) {
574                 if(mface->v4) totedge+=4;
575                 else if(mface->v3) totedge+=3;
576                 else totedge+=1;
577         }
578         
579         if(totedge==0) {
580                 /* flag that mesh has edges */
581                 me->medge = MEM_callocN(0, "make mesh edges");
582                 me->totedge = 0;
583                 return;
584         }
585         
586         ed= edsort= MEM_mallocN(totedge*sizeof(struct edgesort), "edgesort");
587         
588         for(a= me->totface, mface= me->mface; a>0; a--, mface++) {
589                 to_edgesort(ed++, mface->v1, mface->v2, !mface->v3, mface->edcode & ME_V1V2);
590                 if(mface->v4) {
591                         to_edgesort(ed++, mface->v2, mface->v3, 0, mface->edcode & ME_V2V3);
592                         to_edgesort(ed++, mface->v3, mface->v4, 0, mface->edcode & ME_V3V4);
593                         to_edgesort(ed++, mface->v4, mface->v1, 0, mface->edcode & ME_V4V1);
594                 }
595                 else if(mface->v3) {
596                         to_edgesort(ed++, mface->v2, mface->v3, 0, mface->edcode & ME_V2V3);
597                         to_edgesort(ed++, mface->v3, mface->v1, 0, mface->edcode & ME_V3V1);
598                 }
599         }
600         
601         qsort(edsort, totedge, sizeof(struct edgesort), vergedgesort);
602         
603         /* count final amount */
604         for(a=totedge, ed=edsort; a>1; a--, ed++) {
605                 /* edge is unique when it differs from next edge, or is last */
606                 if(ed->v1 != (ed+1)->v1 || ed->v2 != (ed+1)->v2) final++;
607         }
608         final++;
609         
610
611         medge= CustomData_add_layer(&me->edata, CD_MEDGE, CD_CALLOC, NULL, final);
612         me->medge= medge;
613         me->totedge= final;
614         
615         for(a=totedge, ed=edsort; a>1; a--, ed++) {
616                 /* edge is unique when it differs from next edge, or is last */
617                 if(ed->v1 != (ed+1)->v1 || ed->v2 != (ed+1)->v2) {
618                         medge->v1= ed->v1;
619                         medge->v2= ed->v2;
620                         if(old==0 || ed->is_draw) medge->flag= ME_EDGEDRAW|ME_EDGERENDER;
621                         if(ed->is_loose) medge->flag|= ME_LOOSEEDGE;
622                         medge++;
623                 }
624                 else {
625                         /* equal edge, we merge the drawflag */
626                         (ed+1)->is_draw |= ed->is_draw;
627                 }
628         }
629         /* last edge */
630         medge->v1= ed->v1;
631         medge->v2= ed->v2;
632         medge->flag= ME_EDGEDRAW;
633         if(ed->is_loose) medge->flag|= ME_LOOSEEDGE;
634         medge->flag |= ME_EDGERENDER;
635
636         MEM_freeN(edsort);
637
638         mesh_strip_loose_faces(me);
639 }
640
641 void mesh_strip_loose_faces(Mesh *me)
642 {
643         int a,b;
644
645         for (a=b=0; a<me->totface; a++) {
646                 if (me->mface[a].v3) {
647                         if (a!=b) {
648                                 memcpy(&me->mface[b],&me->mface[a],sizeof(me->mface[b]));
649                                 CustomData_copy_data(&me->fdata, &me->fdata, a, b, 1);
650                                 CustomData_free_elem(&me->fdata, a, 1);
651                         }
652                         b++;
653                 }
654         }
655         me->totface = b;
656 }
657
658
659 void mball_to_mesh(ListBase *lb, Mesh *me)
660 {
661         DispList *dl;
662         MVert *mvert;
663         MFace *mface;
664         float *nors, *verts;
665         int a, *index;
666         
667         dl= lb->first;
668         if(dl==0) return;
669
670         if(dl->type==DL_INDEX4) {
671                 me->flag= ME_NOPUNOFLIP;
672                 me->totvert= dl->nr;
673                 me->totface= dl->parts;
674                 
675                 mvert= CustomData_add_layer(&me->vdata, CD_MVERT, CD_CALLOC, NULL, dl->nr);
676                 mface= CustomData_add_layer(&me->fdata, CD_MFACE, CD_CALLOC, NULL, dl->parts);
677                 me->mvert= mvert;
678                 me->mface= mface;
679
680                 a= dl->nr;
681                 nors= dl->nors;
682                 verts= dl->verts;
683                 while(a--) {
684                         VECCOPY(mvert->co, verts);
685                         mvert->no[0]= (short int)(nors[0]*32767.0);
686                         mvert->no[1]= (short int)(nors[1]*32767.0);
687                         mvert->no[2]= (short int)(nors[2]*32767.0);
688                         mvert++;
689                         nors+= 3;
690                         verts+= 3;
691                 }
692                 
693                 a= dl->parts;
694                 index= dl->index;
695                 while(a--) {
696                         mface->v1= index[0];
697                         mface->v2= index[1];
698                         mface->v3= index[2];
699                         mface->v4= index[3];
700                         mface->flag= ME_SMOOTH;
701
702                         test_index_face(mface, NULL, 0, (mface->v3==mface->v4)? 3: 4);
703
704                         mface++;
705                         index+= 4;
706                 }
707
708                 make_edges(me, 0);      // all edges
709         }       
710 }
711
712 /* this may fail replacing ob->data, be sure to check ob->type */
713 void nurbs_to_mesh(Object *ob)
714 {
715         Object *ob1;
716         DispList *dl;
717         Mesh *me;
718         Curve *cu;
719         MVert *mvert;
720         MFace *mface;
721         float *data;
722         int a, b, ofs, vertcount, startvert, totvert=0, totvlak=0;
723         int p1, p2, p3, p4, *index;
724
725         cu= ob->data;
726
727         /* count */
728         dl= cu->disp.first;
729         while(dl) {
730                 if(dl->type==DL_SEGM) {
731                         totvert+= dl->parts*dl->nr;
732                         totvlak+= dl->parts*(dl->nr-1);
733                 }
734                 else if(dl->type==DL_POLY) {
735                         /* cyclic polys are filled. except when 3D */
736                         if(cu->flag & CU_3D) {
737                                 totvert+= dl->parts*dl->nr;
738                                 totvlak+= dl->parts*dl->nr;
739                         }
740                 }
741                 else if(dl->type==DL_SURF) {
742                         totvert+= dl->parts*dl->nr;
743                         totvlak+= (dl->parts-1+((dl->flag & DL_CYCL_V)==2))*(dl->nr-1+(dl->flag & DL_CYCL_U));
744                 }
745                 else if(dl->type==DL_INDEX3) {
746                         totvert+= dl->nr;
747                         totvlak+= dl->parts;
748                 }
749                 dl= dl->next;
750         }
751         if(totvert==0) {
752                 /* error("can't convert"); */
753                 /* Make Sure you check ob->data is a curve */
754                 return;
755         }
756
757         /* make mesh */
758         me= add_mesh("Mesh");
759         me->totvert= totvert;
760         me->totface= totvlak;
761
762         me->totcol= cu->totcol;
763         me->mat= cu->mat;
764         cu->mat= 0;
765         cu->totcol= 0;
766
767         mvert= CustomData_add_layer(&me->vdata, CD_MVERT, CD_CALLOC, NULL, me->totvert);
768         mface= CustomData_add_layer(&me->fdata, CD_MFACE, CD_CALLOC, NULL, me->totface);
769         me->mvert= mvert;
770         me->mface= mface;
771
772         /* verts and faces */
773         vertcount= 0;
774
775         dl= cu->disp.first;
776         while(dl) {
777                 int smooth= dl->rt & CU_SMOOTH ? 1 : 0;
778                 
779                 if(dl->type==DL_SEGM) {
780                         startvert= vertcount;
781                         a= dl->parts*dl->nr;
782                         data= dl->verts;
783                         while(a--) {
784                                 VECCOPY(mvert->co, data);
785                                 data+=3;
786                                 vertcount++;
787                                 mvert++;
788                         }
789
790                         for(a=0; a<dl->parts; a++) {
791                                 ofs= a*dl->nr;
792                                 for(b=1; b<dl->nr; b++) {
793                                         mface->v1= startvert+ofs+b-1;
794                                         mface->v2= startvert+ofs+b;
795                                         if(smooth) mface->flag |= ME_SMOOTH;
796                                         mface++;
797                                 }
798                         }
799
800                 }
801                 else if(dl->type==DL_POLY) {
802                         /* 3d polys are not filled */
803                         if(cu->flag & CU_3D) {
804                                 startvert= vertcount;
805                                 a= dl->parts*dl->nr;
806                                 data= dl->verts;
807                                 while(a--) {
808                                         VECCOPY(mvert->co, data);
809                                         data+=3;
810                                         vertcount++;
811                                         mvert++;
812                                 }
813         
814                                 for(a=0; a<dl->parts; a++) {
815                                         ofs= a*dl->nr;
816                                         for(b=0; b<dl->nr; b++) {
817                                                 mface->v1= startvert+ofs+b;
818                                                 if(b==dl->nr-1) mface->v2= startvert+ofs;
819                                                 else mface->v2= startvert+ofs+b+1;
820                                                 if(smooth) mface->flag |= ME_SMOOTH;
821                                                 mface++;
822                                         }
823                                 }
824                         }
825                 }
826                 else if(dl->type==DL_INDEX3) {
827                         startvert= vertcount;
828                         a= dl->nr;
829                         data= dl->verts;
830                         while(a--) {
831                                 VECCOPY(mvert->co, data);
832                                 data+=3;
833                                 vertcount++;
834                                 mvert++;
835                         }
836
837                         a= dl->parts;
838                         index= dl->index;
839                         while(a--) {
840                                 mface->v1= startvert+index[0];
841                                 mface->v2= startvert+index[2];
842                                 mface->v3= startvert+index[1];
843                                 mface->v4= 0;
844                                 test_index_face(mface, NULL, 0, 3);
845                                 
846                                 if(smooth) mface->flag |= ME_SMOOTH;
847                                 mface++;
848                                 index+= 3;
849                         }
850         
851         
852                 }
853                 else if(dl->type==DL_SURF) {
854                         startvert= vertcount;
855                         a= dl->parts*dl->nr;
856                         data= dl->verts;
857                         while(a--) {
858                                 VECCOPY(mvert->co, data);
859                                 data+=3;
860                                 vertcount++;
861                                 mvert++;
862                         }
863
864                         for(a=0; a<dl->parts; a++) {
865
866                                 if( (dl->flag & DL_CYCL_V)==0 && a==dl->parts-1) break;
867
868                                 if(dl->flag & DL_CYCL_U) {                      /* p2 -> p1 -> */
869                                         p1= startvert+ dl->nr*a;        /* p4 -> p3 -> */
870                                         p2= p1+ dl->nr-1;               /* -----> next row */
871                                         p3= p1+ dl->nr;
872                                         p4= p2+ dl->nr;
873                                         b= 0;
874                                 }
875                                 else {
876                                         p2= startvert+ dl->nr*a;
877                                         p1= p2+1;
878                                         p4= p2+ dl->nr;
879                                         p3= p1+ dl->nr;
880                                         b= 1;
881                                 }
882                                 if( (dl->flag & DL_CYCL_V) && a==dl->parts-1) {
883                                         p3-= dl->parts*dl->nr;
884                                         p4-= dl->parts*dl->nr;
885                                 }
886
887                                 for(; b<dl->nr; b++) {
888                                         mface->v1= p1;
889                                         mface->v2= p3;
890                                         mface->v3= p4;
891                                         mface->v4= p2;
892                                         mface->mat_nr= (unsigned char)dl->col;
893                                         test_index_face(mface, NULL, 0, 4);
894                                         
895                                         if(smooth) mface->flag |= ME_SMOOTH;
896                                         mface++;
897
898                                         p4= p3; 
899                                         p3++;
900                                         p2= p1; 
901                                         p1++;
902                                 }
903                         }
904
905                 }
906
907                 dl= dl->next;
908         }
909
910         make_edges(me, 0);      // all edges
911         mesh_calc_normals(me->mvert, me->totvert, me->mface, me->totface, NULL);
912
913         if(ob->data) {
914                 free_libblock(&G.main->curve, ob->data);
915         }
916         ob->data= me;
917         ob->type= OB_MESH;
918         
919         /* other users */
920         ob1= G.main->object.first;
921         while(ob1) {
922                 if(ob1->data==cu) {
923                         ob1->type= OB_MESH;
924                 
925                         ob1->data= ob->data;
926                         id_us_plus((ID *)ob->data);
927                 }
928                 ob1= ob1->id.next;
929         }
930
931 }
932
933 typedef struct EdgeLink {
934         Link *next, *prev;
935         void *edge;
936 } EdgeLink;
937
938 typedef struct VertLink {
939         Link *next, *prev;
940         int index;
941 } VertLink;
942
943 static void prependPolyLineVert(ListBase *lb, int index)
944 {
945         VertLink *vl= MEM_callocN(sizeof(VertLink), "VertLink");
946         vl->index = index;
947         BLI_addhead(lb, vl);
948 }
949
950 static void appendPolyLineVert(ListBase *lb, int index)
951 {
952         VertLink *vl= MEM_callocN(sizeof(VertLink), "VertLink");
953         vl->index = index;
954         BLI_addtail(lb, vl);
955 }
956
957 void mesh_to_curve(Scene *scene, Object *ob)
958 {
959         /* make new mesh data from the original copy */
960         DerivedMesh *dm= mesh_get_derived_final(scene, ob, CD_MASK_MESH);
961
962         MVert *mverts= dm->getVertArray(dm);
963         MEdge *med, *medge= dm->getEdgeArray(dm);
964         MFace *mf,  *mface= dm->getFaceArray(dm);
965
966         int totedge = dm->getNumEdges(dm);
967         int totface = dm->getNumFaces(dm);
968         int totedges = 0;
969         int i;
970
971         /* only to detect edge polylines */
972         EdgeHash *eh = BLI_edgehash_new();
973         EdgeHash *eh_edge = BLI_edgehash_new();
974
975
976         ListBase edges = {NULL, NULL};
977
978         /* create edges from all faces (so as to find edges not in any faces) */
979         mf= mface;
980         for (i = 0; i < totface; i++, mf++) {
981                 if (!BLI_edgehash_haskey(eh, mf->v1, mf->v2))
982                         BLI_edgehash_insert(eh, mf->v1, mf->v2, NULL);
983                 if (!BLI_edgehash_haskey(eh, mf->v2, mf->v3))
984                         BLI_edgehash_insert(eh, mf->v2, mf->v3, NULL);
985
986                 if (mf->v4) {
987                         if (!BLI_edgehash_haskey(eh, mf->v3, mf->v4))
988                                 BLI_edgehash_insert(eh, mf->v3, mf->v4, NULL);
989                         if (!BLI_edgehash_haskey(eh, mf->v4, mf->v1))
990                                 BLI_edgehash_insert(eh, mf->v4, mf->v1, NULL);
991                 } else {
992                         if (!BLI_edgehash_haskey(eh, mf->v3, mf->v1))
993                                 BLI_edgehash_insert(eh, mf->v3, mf->v1, NULL);
994                 }
995         }
996
997         med= medge;
998         for(i=0; i<totedge; i++, med++) {
999                 if (!BLI_edgehash_haskey(eh, med->v1, med->v2)) {
1000                         EdgeLink *edl= MEM_callocN(sizeof(EdgeLink), "EdgeLink");
1001
1002                         BLI_edgehash_insert(eh_edge, med->v1, med->v2, NULL);
1003                         edl->edge= med;
1004
1005                         BLI_addtail(&edges, edl);       totedges++;
1006                 }
1007         }
1008         BLI_edgehash_free(eh_edge, NULL);
1009         BLI_edgehash_free(eh, NULL);
1010
1011         if(edges.first) {
1012                 Curve *cu = add_curve(ob->id.name+2, OB_CURVE);
1013                 cu->flag |= CU_3D;
1014
1015                 while(edges.first) {
1016                         /* each iteration find a polyline and add this as a nurbs poly spline */
1017
1018                         ListBase polyline = {NULL, NULL}; /* store a list of VertLink's */
1019                         int closed = FALSE;
1020                         int totpoly= 0;
1021                         MEdge *med_current= ((EdgeLink *)edges.last)->edge;
1022                         int startVert= med_current->v1;
1023                         int endVert= med_current->v2;
1024                         int ok= TRUE;
1025
1026                         appendPolyLineVert(&polyline, startVert);       totpoly++;
1027                         appendPolyLineVert(&polyline, endVert);         totpoly++;
1028                         BLI_freelinkN(&edges, edges.last);                      totedges--;
1029
1030                         while(ok) { /* while connected edges are found... */
1031                                 ok = FALSE;
1032                                 i= totedges;
1033                                 while(i) {
1034                                         EdgeLink *edl;
1035
1036                                         i-=1;
1037                                         edl= BLI_findlink(&edges, i);
1038                                         med= edl->edge;
1039
1040                                         if(med->v1==endVert) {
1041                                                 endVert = med->v2;
1042                                                 appendPolyLineVert(&polyline, med->v2); totpoly++;
1043                                                 BLI_freelinkN(&edges, edl);                             totedges--;
1044                                                 ok= TRUE;
1045                                         }
1046                                         else if(med->v2==endVert) {
1047                                                 endVert = med->v1;
1048                                                 appendPolyLineVert(&polyline, endVert); totpoly++;
1049                                                 BLI_freelinkN(&edges, edl);                             totedges--;
1050                                                 ok= TRUE;
1051                                         }
1052                                         else if(med->v1==startVert) {
1053                                                 startVert = med->v2;
1054                                                 prependPolyLineVert(&polyline, startVert);      totpoly++;
1055                                                 BLI_freelinkN(&edges, edl);                                     totedges--;
1056                                                 ok= TRUE;
1057                                         }
1058                                         else if(med->v2==startVert) {
1059                                                 startVert = med->v1;
1060                                                 prependPolyLineVert(&polyline, startVert);      totpoly++;
1061                                                 BLI_freelinkN(&edges, edl);                                     totedges--;
1062                                                 ok= TRUE;
1063                                         }
1064                                 }
1065                         }
1066
1067                         /* Now we have a polyline, make into a curve */
1068                         if(startVert==endVert) {
1069                                 BLI_freelinkN(&polyline, polyline.last);
1070                                 totpoly--;
1071                                 closed = TRUE;
1072                         }
1073
1074                         /* --- nurbs --- */
1075                         {
1076                                 Nurb *nu;
1077                                 BPoint *bp;
1078                                 VertLink *vl;
1079
1080                                 /* create new 'nurb' within the curve */
1081                                 nu = (Nurb *)MEM_callocN(sizeof(Nurb), "MeshNurb");
1082
1083                                 nu->pntsu= totpoly;
1084                                 nu->pntsv= 1;
1085                                 nu->orderu= 4;
1086                                 nu->flagu= 2 | (closed ? CU_CYCLIC:0);  /* endpoint */
1087                                 nu->resolu= 12;
1088
1089                                 nu->bp= (BPoint *)MEM_callocN(sizeof(BPoint)*totpoly, "bpoints");
1090
1091                                 /* add points */
1092                                 vl= polyline.first;
1093                                 for (i=0, bp=nu->bp; i < totpoly; i++, bp++, vl=(VertLink *)vl->next) {
1094                                         copy_v3_v3(bp->vec, mverts[vl->index].co);
1095                                         bp->f1= SELECT;
1096                                         bp->radius = bp->weight = 1.0;
1097                                 }
1098                                 BLI_freelistN(&polyline);
1099
1100                                 /* add nurb to curve */
1101                                 BLI_addtail(&cu->nurb, nu);
1102                         }
1103                         /* --- done with nurbs --- */
1104                 }
1105
1106                 ((Mesh *)ob->data)->id.us--;
1107                 ob->data= cu;
1108                 ob->type= OB_CURVE;
1109         }
1110
1111         dm->release(dm);
1112 }
1113
1114 void mesh_delete_material_index(Mesh *me, int index)
1115 {
1116         int i;
1117
1118         for (i=0; i<me->totface; i++) {
1119                 MFace *mf = &((MFace*) me->mface)[i];
1120                 if (mf->mat_nr && mf->mat_nr>=index) 
1121                         mf->mat_nr--;
1122         }
1123 }
1124
1125 void mesh_set_smooth_flag(Object *meshOb, int enableSmooth) 
1126 {
1127         Mesh *me = meshOb->data;
1128         int i;
1129
1130         for (i=0; i<me->totface; i++) {
1131                 MFace *mf = &((MFace*) me->mface)[i];
1132
1133                 if (enableSmooth) {
1134                         mf->flag |= ME_SMOOTH;
1135                 } else {
1136                         mf->flag &= ~ME_SMOOTH;
1137                 }
1138         }
1139
1140 // XXX do this in caller        DAG_id_flush_update(&me->id, OB_RECALC_DATA);
1141 }
1142
1143 void mesh_calc_normals(MVert *mverts, int numVerts, MFace *mfaces, int numFaces, float **faceNors_r) 
1144 {
1145         float (*tnorms)[3]= MEM_callocN(numVerts*sizeof(*tnorms), "tnorms");
1146         float *fnors= MEM_mallocN(sizeof(*fnors)*3*numFaces, "meshnormals");
1147         int i;
1148
1149         for (i=0; i<numFaces; i++) {
1150                 MFace *mf= &mfaces[i];
1151                 float *f_no= &fnors[i*3];
1152
1153                 if (mf->v4)
1154                         normal_quad_v3( f_no,mverts[mf->v1].co, mverts[mf->v2].co, mverts[mf->v3].co, mverts[mf->v4].co);
1155                 else
1156                         normal_tri_v3( f_no,mverts[mf->v1].co, mverts[mf->v2].co, mverts[mf->v3].co);
1157                 
1158                 add_v3_v3v3(tnorms[mf->v1], tnorms[mf->v1], f_no);
1159                 add_v3_v3v3(tnorms[mf->v2], tnorms[mf->v2], f_no);
1160                 add_v3_v3v3(tnorms[mf->v3], tnorms[mf->v3], f_no);
1161                 if (mf->v4)
1162                         add_v3_v3v3(tnorms[mf->v4], tnorms[mf->v4], f_no);
1163         }
1164         for (i=0; i<numVerts; i++) {
1165                 MVert *mv= &mverts[i];
1166                 float *no= tnorms[i];
1167                 
1168                 if (normalize_v3(no)==0.0) {
1169                         VECCOPY(no, mv->co);
1170                         normalize_v3(no);
1171                 }
1172
1173                 mv->no[0]= (short)(no[0]*32767.0);
1174                 mv->no[1]= (short)(no[1]*32767.0);
1175                 mv->no[2]= (short)(no[2]*32767.0);
1176         }
1177         
1178         MEM_freeN(tnorms);
1179
1180         if (faceNors_r) {
1181                 *faceNors_r = fnors;
1182         } else {
1183                 MEM_freeN(fnors);
1184         }
1185 }
1186
1187 float (*mesh_getVertexCos(Mesh *me, int *numVerts_r))[3]
1188 {
1189         int i, numVerts = me->totvert;
1190         float (*cos)[3] = MEM_mallocN(sizeof(*cos)*numVerts, "vertexcos1");
1191         
1192         if (numVerts_r) *numVerts_r = numVerts;
1193         for (i=0; i<numVerts; i++)
1194                 VECCOPY(cos[i], me->mvert[i].co);
1195         
1196         return cos;
1197 }
1198
1199 UvVertMap *make_uv_vert_map(struct MFace *mface, struct MTFace *tface, unsigned int totface, unsigned int totvert, int selected, float *limit)
1200 {
1201         UvVertMap *vmap;
1202         UvMapVert *buf;
1203         MFace *mf;
1204         MTFace *tf;
1205         unsigned int a;
1206         int     i, totuv, nverts;
1207
1208         totuv = 0;
1209
1210         /* generate UvMapVert array */
1211         mf= mface;
1212         tf= tface;
1213         for(a=0; a<totface; a++, mf++, tf++)
1214                 if(!selected || (!(mf->flag & ME_HIDE) && (mf->flag & ME_FACE_SEL)))
1215                         totuv += (mf->v4)? 4: 3;
1216                 
1217         if(totuv==0)
1218                 return NULL;
1219         
1220         vmap= (UvVertMap*)MEM_callocN(sizeof(*vmap), "UvVertMap");
1221         if (!vmap)
1222                 return NULL;
1223
1224         vmap->vert= (UvMapVert**)MEM_callocN(sizeof(*vmap->vert)*totvert, "UvMapVert*");
1225         buf= vmap->buf= (UvMapVert*)MEM_callocN(sizeof(*vmap->buf)*totuv, "UvMapVert");
1226
1227         if (!vmap->vert || !vmap->buf) {
1228                 free_uv_vert_map(vmap);
1229                 return NULL;
1230         }
1231
1232         mf= mface;
1233         tf= tface;
1234         for(a=0; a<totface; a++, mf++, tf++) {
1235                 if(!selected || (!(mf->flag & ME_HIDE) && (mf->flag & ME_FACE_SEL))) {
1236                         nverts= (mf->v4)? 4: 3;
1237
1238                         for(i=0; i<nverts; i++) {
1239                                 buf->tfindex= i;
1240                                 buf->f= a;
1241                                 buf->separate = 0;
1242                                 buf->next= vmap->vert[*(&mf->v1 + i)];
1243                                 vmap->vert[*(&mf->v1 + i)]= buf;
1244                                 buf++;
1245                         }
1246                 }
1247         }
1248         
1249         /* sort individual uvs for each vert */
1250         tf= tface;
1251         for(a=0; a<totvert; a++) {
1252                 UvMapVert *newvlist= NULL, *vlist=vmap->vert[a];
1253                 UvMapVert *iterv, *v, *lastv, *next;
1254                 float *uv, *uv2, uvdiff[2];
1255
1256                 while(vlist) {
1257                         v= vlist;
1258                         vlist= vlist->next;
1259                         v->next= newvlist;
1260                         newvlist= v;
1261
1262                         uv= (tf+v->f)->uv[v->tfindex];
1263                         lastv= NULL;
1264                         iterv= vlist;
1265
1266                         while(iterv) {
1267                                 next= iterv->next;
1268
1269                                 uv2= (tf+iterv->f)->uv[iterv->tfindex];
1270                                 sub_v2_v2v2(uvdiff, uv2, uv);
1271
1272
1273                                 if(fabs(uv[0]-uv2[0]) < limit[0] && fabs(uv[1]-uv2[1]) < limit[1]) {
1274                                         if(lastv) lastv->next= next;
1275                                         else vlist= next;
1276                                         iterv->next= newvlist;
1277                                         newvlist= iterv;
1278                                 }
1279                                 else
1280                                         lastv=iterv;
1281
1282                                 iterv= next;
1283                         }
1284
1285                         newvlist->separate = 1;
1286                 }
1287
1288                 vmap->vert[a]= newvlist;
1289         }
1290         
1291         return vmap;
1292 }
1293
1294 UvMapVert *get_uv_map_vert(UvVertMap *vmap, unsigned int v)
1295 {
1296         return vmap->vert[v];
1297 }
1298
1299 void free_uv_vert_map(UvVertMap *vmap)
1300 {
1301         if (vmap) {
1302                 if (vmap->vert) MEM_freeN(vmap->vert);
1303                 if (vmap->buf) MEM_freeN(vmap->buf);
1304                 MEM_freeN(vmap);
1305         }
1306 }
1307
1308 /* Generates a map where the key is the vertex and the value is a list
1309    of faces that use that vertex as a corner. The lists are allocated
1310    from one memory pool. */
1311 void create_vert_face_map(ListBase **map, IndexNode **mem, const MFace *mface, const int totvert, const int totface)
1312 {
1313         int i,j;
1314         IndexNode *node = NULL;
1315         
1316         (*map) = MEM_callocN(sizeof(ListBase) * totvert, "vert face map");
1317         (*mem) = MEM_callocN(sizeof(IndexNode) * totface*4, "vert face map mem");
1318         node = *mem;
1319         
1320         /* Find the users */
1321         for(i = 0; i < totface; ++i){
1322                 for(j = 0; j < (mface[i].v4?4:3); ++j, ++node) {
1323                         node->index = i;
1324                         BLI_addtail(&(*map)[((unsigned int*)(&mface[i]))[j]], node);
1325                 }
1326         }
1327 }
1328
1329 /* Generates a map where the key is the vertex and the value is a list
1330    of edges that use that vertex as an endpoint. The lists are allocated
1331    from one memory pool. */
1332 void create_vert_edge_map(ListBase **map, IndexNode **mem, const MEdge *medge, const int totvert, const int totedge)
1333 {
1334         int i, j;
1335         IndexNode *node = NULL;
1336  
1337         (*map) = MEM_callocN(sizeof(ListBase) * totvert, "vert edge map");
1338         (*mem) = MEM_callocN(sizeof(IndexNode) * totedge * 2, "vert edge map mem");
1339         node = *mem;
1340        
1341         /* Find the users */
1342         for(i = 0; i < totedge; ++i){
1343                 for(j = 0; j < 2; ++j, ++node) {
1344                         node->index = i;
1345                         BLI_addtail(&(*map)[((unsigned int*)(&medge[i].v1))[j]], node);
1346                 }
1347         }
1348 }
1349
1350 /* Partial Mesh Visibility */
1351 PartialVisibility *mesh_pmv_copy(PartialVisibility *pmv)
1352 {
1353         PartialVisibility *n= MEM_dupallocN(pmv);
1354         n->vert_map= MEM_dupallocN(pmv->vert_map);
1355         n->edge_map= MEM_dupallocN(pmv->edge_map);
1356         n->old_edges= MEM_dupallocN(pmv->old_edges);
1357         n->old_faces= MEM_dupallocN(pmv->old_faces);
1358         return n;
1359 }
1360
1361 void mesh_pmv_free(PartialVisibility *pv)
1362 {
1363         MEM_freeN(pv->vert_map);
1364         MEM_freeN(pv->edge_map);
1365         MEM_freeN(pv->old_faces);
1366         MEM_freeN(pv->old_edges);
1367         MEM_freeN(pv);
1368 }
1369
1370 void mesh_pmv_revert(Object *ob, Mesh *me)
1371 {
1372         if(me->pv) {
1373                 unsigned i;
1374                 MVert *nve, *old_verts;
1375                 
1376                 /* Reorder vertices */
1377                 nve= me->mvert;
1378                 old_verts = MEM_mallocN(sizeof(MVert)*me->pv->totvert,"PMV revert verts");
1379                 for(i=0; i<me->pv->totvert; ++i)
1380                         old_verts[i]= nve[me->pv->vert_map[i]];
1381
1382                 /* Restore verts, edges and faces */
1383                 CustomData_free_layer_active(&me->vdata, CD_MVERT, me->totvert);
1384                 CustomData_free_layer_active(&me->edata, CD_MEDGE, me->totedge);
1385                 CustomData_free_layer_active(&me->fdata, CD_MFACE, me->totface);
1386
1387                 CustomData_add_layer(&me->vdata, CD_MVERT, CD_ASSIGN, old_verts, me->pv->totvert);
1388                 CustomData_add_layer(&me->edata, CD_MEDGE, CD_ASSIGN, me->pv->old_edges, me->pv->totedge);
1389                 CustomData_add_layer(&me->fdata, CD_MFACE, CD_ASSIGN, me->pv->old_faces, me->pv->totface);
1390                 mesh_update_customdata_pointers(me);
1391
1392                 me->totvert= me->pv->totvert;
1393                 me->totedge= me->pv->totedge;
1394                 me->totface= me->pv->totface;
1395
1396                 me->pv->old_edges= NULL;
1397                 me->pv->old_faces= NULL;
1398
1399                 /* Free maps */
1400                 MEM_freeN(me->pv->edge_map);
1401                 me->pv->edge_map= NULL;
1402                 MEM_freeN(me->pv->vert_map);
1403                 me->pv->vert_map= NULL;
1404
1405 // XXX do this in caller                DAG_id_flush_update(&me->id, OB_RECALC_DATA);
1406         }
1407 }
1408
1409 void mesh_pmv_off(Object *ob, Mesh *me)
1410 {
1411         if(ob && me->pv) {
1412                 mesh_pmv_revert(ob, me);
1413                 MEM_freeN(me->pv);
1414                 me->pv= NULL;
1415         }
1416 }