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