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