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