Fix for convert_exec()
[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., 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 static void mfaces_strip_loose(MFace *mface, int *totface)
567 {
568         int a,b;
569
570         for (a=b=0; a<*totface; a++) {
571                 if (mface[a].v3) {
572                         if (a!=b) {
573                                 memcpy(&mface[b],&mface[a],sizeof(mface[b]));
574                         }
575                         b++;
576                 }
577         }
578
579         *totface= b;
580 }
581
582 /* Create edges based on known verts and faces */
583 static void make_edges_mdata(MVert *allvert, MFace *allface, int totvert, int totface,
584         int old, MEdge **alledge, int *_totedge)
585 {
586         MFace *mface;
587         MEdge *medge;
588         struct edgesort *edsort, *ed;
589         int a, totedge=0, final=0;
590
591         /* we put all edges in array, sort them, and detect doubles that way */
592
593         for(a= totface, mface= allface; a>0; a--, mface++) {
594                 if(mface->v4) totedge+=4;
595                 else if(mface->v3) totedge+=3;
596                 else totedge+=1;
597         }
598
599         if(totedge==0) {
600                 /* flag that mesh has edges */
601                 (*alledge)= MEM_callocN(0, "make mesh edges");
602                 (*_totedge) = 0;
603                 return;
604         }
605
606         ed= edsort= MEM_mallocN(totedge*sizeof(struct edgesort), "edgesort");
607
608         for(a= totface, mface= allface; a>0; a--, mface++) {
609                 to_edgesort(ed++, mface->v1, mface->v2, !mface->v3, mface->edcode & ME_V1V2);
610                 if(mface->v4) {
611                         to_edgesort(ed++, mface->v2, mface->v3, 0, mface->edcode & ME_V2V3);
612                         to_edgesort(ed++, mface->v3, mface->v4, 0, mface->edcode & ME_V3V4);
613                         to_edgesort(ed++, mface->v4, mface->v1, 0, mface->edcode & ME_V4V1);
614                 }
615                 else if(mface->v3) {
616                         to_edgesort(ed++, mface->v2, mface->v3, 0, mface->edcode & ME_V2V3);
617                         to_edgesort(ed++, mface->v3, mface->v1, 0, mface->edcode & ME_V3V1);
618                 }
619         }
620
621         qsort(edsort, totedge, sizeof(struct edgesort), vergedgesort);
622
623         /* count final amount */
624         for(a=totedge, ed=edsort; a>1; a--, ed++) {
625                 /* edge is unique when it differs from next edge, or is last */
626                 if(ed->v1 != (ed+1)->v1 || ed->v2 != (ed+1)->v2) final++;
627         }
628         final++;
629
630         (*alledge)= medge= MEM_callocN(sizeof (MEdge) * final, "make_edges mdge");
631         (*_totedge)= final;
632
633         for(a=totedge, ed=edsort; a>1; a--, ed++) {
634                 /* edge is unique when it differs from next edge, or is last */
635                 if(ed->v1 != (ed+1)->v1 || ed->v2 != (ed+1)->v2) {
636                         medge->v1= ed->v1;
637                         medge->v2= ed->v2;
638                         if(old==0 || ed->is_draw) medge->flag= ME_EDGEDRAW|ME_EDGERENDER;
639                         if(ed->is_loose) medge->flag|= ME_LOOSEEDGE;
640                         medge++;
641                 }
642                 else {
643                         /* equal edge, we merge the drawflag */
644                         (ed+1)->is_draw |= ed->is_draw;
645                 }
646         }
647         /* last edge */
648         medge->v1= ed->v1;
649         medge->v2= ed->v2;
650         medge->flag= ME_EDGEDRAW;
651         if(ed->is_loose) medge->flag|= ME_LOOSEEDGE;
652         medge->flag |= ME_EDGERENDER;
653
654         MEM_freeN(edsort);
655 }
656
657 void make_edges(Mesh *me, int old)
658 {
659         MEdge *medge;
660         int totedge=0;
661
662         make_edges_mdata(me->mvert, me->mface, me->totvert, me->totface, old, &medge, &totedge);
663         if(totedge==0) {
664                 /* flag that mesh has edges */
665                 me->medge = medge;
666                 me->totedge = 0;
667                 return;
668         }
669
670         medge= CustomData_add_layer(&me->edata, CD_MEDGE, CD_ASSIGN, medge, totedge);
671         me->medge= medge;
672         me->totedge= totedge;
673
674         mesh_strip_loose_faces(me);
675 }
676
677 void mesh_strip_loose_faces(Mesh *me)
678 {
679         int a,b;
680
681         for (a=b=0; a<me->totface; a++) {
682                 if (me->mface[a].v3) {
683                         if (a!=b) {
684                                 memcpy(&me->mface[b],&me->mface[a],sizeof(me->mface[b]));
685                                 CustomData_copy_data(&me->fdata, &me->fdata, a, b, 1);
686                                 CustomData_free_elem(&me->fdata, a, 1);
687                         }
688                         b++;
689                 }
690         }
691         me->totface = b;
692 }
693
694 void mball_to_mesh(ListBase *lb, Mesh *me)
695 {
696         DispList *dl;
697         MVert *mvert;
698         MFace *mface;
699         float *nors, *verts;
700         int a, *index;
701         
702         dl= lb->first;
703         if(dl==0) return;
704
705         if(dl->type==DL_INDEX4) {
706                 me->flag= ME_NOPUNOFLIP;
707                 me->totvert= dl->nr;
708                 me->totface= dl->parts;
709                 
710                 mvert= CustomData_add_layer(&me->vdata, CD_MVERT, CD_CALLOC, NULL, dl->nr);
711                 mface= CustomData_add_layer(&me->fdata, CD_MFACE, CD_CALLOC, NULL, dl->parts);
712                 me->mvert= mvert;
713                 me->mface= mface;
714
715                 a= dl->nr;
716                 nors= dl->nors;
717                 verts= dl->verts;
718                 while(a--) {
719                         VECCOPY(mvert->co, verts);
720                         mvert->no[0]= (short int)(nors[0]*32767.0);
721                         mvert->no[1]= (short int)(nors[1]*32767.0);
722                         mvert->no[2]= (short int)(nors[2]*32767.0);
723                         mvert++;
724                         nors+= 3;
725                         verts+= 3;
726                 }
727                 
728                 a= dl->parts;
729                 index= dl->index;
730                 while(a--) {
731                         mface->v1= index[0];
732                         mface->v2= index[1];
733                         mface->v3= index[2];
734                         mface->v4= index[3];
735                         mface->flag= ME_SMOOTH;
736
737                         test_index_face(mface, NULL, 0, (mface->v3==mface->v4)? 3: 4);
738
739                         mface++;
740                         index+= 4;
741                 }
742
743                 make_edges(me, 0);      // all edges
744         }       
745 }
746
747 /* Initialize mverts, medges and, faces for converting nurbs to mesh and derived mesh */
748 /* return non-zero on error */
749 int nurbs_to_mdata(Object *ob, MVert **allvert, int *_totvert,
750         MEdge **alledge, int *_totedge, MFace **allface, int *_totface)
751 {
752         DispList *dl;
753         Curve *cu;
754         MVert *mvert;
755         MFace *mface;
756         float *data;
757         int a, b, ofs, vertcount, startvert, totvert=0, totvlak=0;
758         int p1, p2, p3, p4, *index;
759
760         cu= ob->data;
761
762         /* count */
763         dl= cu->disp.first;
764         while(dl) {
765                 if(dl->type==DL_SEGM) {
766                         totvert+= dl->parts*dl->nr;
767                         totvlak+= dl->parts*(dl->nr-1);
768                 }
769                 else if(dl->type==DL_POLY) {
770                         /* cyclic polys are filled. except when 3D */
771                         if(cu->flag & CU_3D) {
772                                 totvert+= dl->parts*dl->nr;
773                                 totvlak+= dl->parts*dl->nr;
774                         }
775                 }
776                 else if(dl->type==DL_SURF) {
777                         totvert+= dl->parts*dl->nr;
778                         totvlak+= (dl->parts-1+((dl->flag & DL_CYCL_V)==2))*(dl->nr-1+(dl->flag & DL_CYCL_U));
779                 }
780                 else if(dl->type==DL_INDEX3) {
781                         totvert+= dl->nr;
782                         totvlak+= dl->parts;
783                 }
784                 dl= dl->next;
785         }
786
787         if(totvert==0) {
788                 /* error("can't convert"); */
789                 /* Make Sure you check ob->data is a curve */
790                 return -1;
791         }
792
793         *allvert= mvert= MEM_callocN(sizeof (MVert) * totvert, "nurbs_init mvert");
794         *allface= mface= MEM_callocN(sizeof (MVert) * totvert, "nurbs_init 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         *_totvert= totvert;
935         *_totface= totvlak;
936
937         make_edges_mdata(*allvert, *allface, totvert, totvlak, 0, alledge, _totedge);
938         mfaces_strip_loose(*allface, _totface);
939
940         return 0;
941 }
942
943 /* this may fail replacing ob->data, be sure to check ob->type */
944 void nurbs_to_mesh(Object *ob)
945 {
946         Object *ob1;
947         DerivedMesh *dm= ob->derivedFinal;
948         Mesh *me;
949         Curve *cu;
950         MVert *allvert= NULL;
951         MEdge *alledge= NULL;
952         MFace *allface= NULL;
953         int totvert, totedge, totface;
954
955         cu= ob->data;
956
957         if (dm == NULL) {
958                 if (nurbs_to_mdata (ob, &allvert, &totvert, &alledge, &totedge, &allface, &totface) != 0) {
959                         /* Error initializing */
960                         return;
961                 }
962
963                 /* make mesh */
964                 me= add_mesh("Mesh");
965                 me->totvert= totvert;
966                 me->totface= totface;
967                 me->totedge= totedge;
968
969                 me->mvert= CustomData_add_layer(&me->vdata, CD_MVERT, CD_ASSIGN, allvert, me->totvert);
970                 me->mface= CustomData_add_layer(&me->fdata, CD_MFACE, CD_ASSIGN, allface, me->totface);
971                 me->medge= CustomData_add_layer(&me->edata, CD_MEDGE, CD_ASSIGN, alledge, me->totedge);
972
973                 mesh_calc_normals(me->mvert, me->totvert, me->mface, me->totface, NULL);
974         } else {
975                 me= add_mesh("Mesh");
976                 DM_to_mesh(dm, me);
977         }
978
979         me->totcol= cu->totcol;
980         me->mat= cu->mat;
981         cu->mat= 0;
982         cu->totcol= 0;
983
984         if(ob->data) {
985                 free_libblock(&G.main->curve, ob->data);
986         }
987         ob->data= me;
988         ob->type= OB_MESH;
989
990         /* other users */
991         ob1= G.main->object.first;
992         while(ob1) {
993                 if(ob1->data==cu) {
994                         ob1->type= OB_MESH;
995                 
996                         ob1->data= ob->data;
997                         id_us_plus((ID *)ob->data);
998                 }
999                 ob1= ob1->id.next;
1000         }
1001 }
1002
1003 typedef struct EdgeLink {
1004         Link *next, *prev;
1005         void *edge;
1006 } EdgeLink;
1007
1008 typedef struct VertLink {
1009         Link *next, *prev;
1010         int index;
1011 } VertLink;
1012
1013 static void prependPolyLineVert(ListBase *lb, int index)
1014 {
1015         VertLink *vl= MEM_callocN(sizeof(VertLink), "VertLink");
1016         vl->index = index;
1017         BLI_addhead(lb, vl);
1018 }
1019
1020 static void appendPolyLineVert(ListBase *lb, int index)
1021 {
1022         VertLink *vl= MEM_callocN(sizeof(VertLink), "VertLink");
1023         vl->index = index;
1024         BLI_addtail(lb, vl);
1025 }
1026
1027 void mesh_to_curve(Scene *scene, Object *ob)
1028 {
1029         /* make new mesh data from the original copy */
1030         DerivedMesh *dm= mesh_get_derived_final(scene, ob, CD_MASK_MESH);
1031
1032         MVert *mverts= dm->getVertArray(dm);
1033         MEdge *med, *medge= dm->getEdgeArray(dm);
1034         MFace *mf,  *mface= dm->getFaceArray(dm);
1035
1036         int totedge = dm->getNumEdges(dm);
1037         int totface = dm->getNumFaces(dm);
1038         int totedges = 0;
1039         int i, needsFree = 0;
1040
1041         /* only to detect edge polylines */
1042         EdgeHash *eh = BLI_edgehash_new();
1043         EdgeHash *eh_edge = BLI_edgehash_new();
1044
1045
1046         ListBase edges = {NULL, NULL};
1047
1048         /* create edges from all faces (so as to find edges not in any faces) */
1049         mf= mface;
1050         for (i = 0; i < totface; i++, mf++) {
1051                 if (!BLI_edgehash_haskey(eh, mf->v1, mf->v2))
1052                         BLI_edgehash_insert(eh, mf->v1, mf->v2, NULL);
1053                 if (!BLI_edgehash_haskey(eh, mf->v2, mf->v3))
1054                         BLI_edgehash_insert(eh, mf->v2, mf->v3, NULL);
1055
1056                 if (mf->v4) {
1057                         if (!BLI_edgehash_haskey(eh, mf->v3, mf->v4))
1058                                 BLI_edgehash_insert(eh, mf->v3, mf->v4, NULL);
1059                         if (!BLI_edgehash_haskey(eh, mf->v4, mf->v1))
1060                                 BLI_edgehash_insert(eh, mf->v4, mf->v1, NULL);
1061                 } else {
1062                         if (!BLI_edgehash_haskey(eh, mf->v3, mf->v1))
1063                                 BLI_edgehash_insert(eh, mf->v3, mf->v1, NULL);
1064                 }
1065         }
1066
1067         med= medge;
1068         for(i=0; i<totedge; i++, med++) {
1069                 if (!BLI_edgehash_haskey(eh, med->v1, med->v2)) {
1070                         EdgeLink *edl= MEM_callocN(sizeof(EdgeLink), "EdgeLink");
1071
1072                         BLI_edgehash_insert(eh_edge, med->v1, med->v2, NULL);
1073                         edl->edge= med;
1074
1075                         BLI_addtail(&edges, edl);       totedges++;
1076                 }
1077         }
1078         BLI_edgehash_free(eh_edge, NULL);
1079         BLI_edgehash_free(eh, NULL);
1080
1081         if(edges.first) {
1082                 Curve *cu = add_curve(ob->id.name+2, OB_CURVE);
1083                 cu->flag |= CU_3D;
1084
1085                 while(edges.first) {
1086                         /* each iteration find a polyline and add this as a nurbs poly spline */
1087
1088                         ListBase polyline = {NULL, NULL}; /* store a list of VertLink's */
1089                         int closed = FALSE;
1090                         int totpoly= 0;
1091                         MEdge *med_current= ((EdgeLink *)edges.last)->edge;
1092                         int startVert= med_current->v1;
1093                         int endVert= med_current->v2;
1094                         int ok= TRUE;
1095
1096                         appendPolyLineVert(&polyline, startVert);       totpoly++;
1097                         appendPolyLineVert(&polyline, endVert);         totpoly++;
1098                         BLI_freelinkN(&edges, edges.last);                      totedges--;
1099
1100                         while(ok) { /* while connected edges are found... */
1101                                 ok = FALSE;
1102                                 i= totedges;
1103                                 while(i) {
1104                                         EdgeLink *edl;
1105
1106                                         i-=1;
1107                                         edl= BLI_findlink(&edges, i);
1108                                         med= edl->edge;
1109
1110                                         if(med->v1==endVert) {
1111                                                 endVert = med->v2;
1112                                                 appendPolyLineVert(&polyline, med->v2); totpoly++;
1113                                                 BLI_freelinkN(&edges, edl);                             totedges--;
1114                                                 ok= TRUE;
1115                                         }
1116                                         else if(med->v2==endVert) {
1117                                                 endVert = med->v1;
1118                                                 appendPolyLineVert(&polyline, endVert); totpoly++;
1119                                                 BLI_freelinkN(&edges, edl);                             totedges--;
1120                                                 ok= TRUE;
1121                                         }
1122                                         else if(med->v1==startVert) {
1123                                                 startVert = med->v2;
1124                                                 prependPolyLineVert(&polyline, startVert);      totpoly++;
1125                                                 BLI_freelinkN(&edges, edl);                                     totedges--;
1126                                                 ok= TRUE;
1127                                         }
1128                                         else if(med->v2==startVert) {
1129                                                 startVert = med->v1;
1130                                                 prependPolyLineVert(&polyline, startVert);      totpoly++;
1131                                                 BLI_freelinkN(&edges, edl);                                     totedges--;
1132                                                 ok= TRUE;
1133                                         }
1134                                 }
1135                         }
1136
1137                         /* Now we have a polyline, make into a curve */
1138                         if(startVert==endVert) {
1139                                 BLI_freelinkN(&polyline, polyline.last);
1140                                 totpoly--;
1141                                 closed = TRUE;
1142                         }
1143
1144                         /* --- nurbs --- */
1145                         {
1146                                 Nurb *nu;
1147                                 BPoint *bp;
1148                                 VertLink *vl;
1149
1150                                 /* create new 'nurb' within the curve */
1151                                 nu = (Nurb *)MEM_callocN(sizeof(Nurb), "MeshNurb");
1152
1153                                 nu->pntsu= totpoly;
1154                                 nu->pntsv= 1;
1155                                 nu->orderu= 4;
1156                                 nu->flagu= 2 | (closed ? CU_CYCLIC:0);  /* endpoint */
1157                                 nu->resolu= 12;
1158
1159                                 nu->bp= (BPoint *)MEM_callocN(sizeof(BPoint)*totpoly, "bpoints");
1160
1161                                 /* add points */
1162                                 vl= polyline.first;
1163                                 for (i=0, bp=nu->bp; i < totpoly; i++, bp++, vl=(VertLink *)vl->next) {
1164                                         copy_v3_v3(bp->vec, mverts[vl->index].co);
1165                                         bp->f1= SELECT;
1166                                         bp->radius = bp->weight = 1.0;
1167                                 }
1168                                 BLI_freelistN(&polyline);
1169
1170                                 /* add nurb to curve */
1171                                 BLI_addtail(&cu->nurb, nu);
1172                         }
1173                         /* --- done with nurbs --- */
1174                 }
1175
1176                 ((Mesh *)ob->data)->id.us--;
1177                 ob->data= cu;
1178                 ob->type= OB_CURVE;
1179
1180                 /* curve objects can't contain DM in usual cases, we could free memory */
1181                 needsFree= 1;
1182         }
1183
1184         dm->needsFree = needsFree;
1185         dm->release(dm);
1186
1187         if (needsFree) {
1188                 ob->derivedFinal = NULL;
1189         }
1190 }
1191
1192 void mesh_delete_material_index(Mesh *me, int index)
1193 {
1194         int i;
1195
1196         for (i=0; i<me->totface; i++) {
1197                 MFace *mf = &((MFace*) me->mface)[i];
1198                 if (mf->mat_nr && mf->mat_nr>=index) 
1199                         mf->mat_nr--;
1200         }
1201 }
1202
1203 void mesh_set_smooth_flag(Object *meshOb, int enableSmooth) 
1204 {
1205         Mesh *me = meshOb->data;
1206         int i;
1207
1208         for (i=0; i<me->totface; i++) {
1209                 MFace *mf = &((MFace*) me->mface)[i];
1210
1211                 if (enableSmooth) {
1212                         mf->flag |= ME_SMOOTH;
1213                 } else {
1214                         mf->flag &= ~ME_SMOOTH;
1215                 }
1216         }
1217
1218 // XXX do this in caller        DAG_id_flush_update(&me->id, OB_RECALC_DATA);
1219 }
1220
1221 void mesh_calc_normals(MVert *mverts, int numVerts, MFace *mfaces, int numFaces, float **faceNors_r) 
1222 {
1223         float (*tnorms)[3]= MEM_callocN(numVerts*sizeof(*tnorms), "tnorms");
1224         float *fnors= MEM_mallocN(sizeof(*fnors)*3*numFaces, "meshnormals");
1225         int i;
1226
1227         for (i=0; i<numFaces; i++) {
1228                 MFace *mf= &mfaces[i];
1229                 float *f_no= &fnors[i*3];
1230
1231                 if (mf->v4)
1232                         normal_quad_v3( f_no,mverts[mf->v1].co, mverts[mf->v2].co, mverts[mf->v3].co, mverts[mf->v4].co);
1233                 else
1234                         normal_tri_v3( f_no,mverts[mf->v1].co, mverts[mf->v2].co, mverts[mf->v3].co);
1235                 
1236                 add_v3_v3v3(tnorms[mf->v1], tnorms[mf->v1], f_no);
1237                 add_v3_v3v3(tnorms[mf->v2], tnorms[mf->v2], f_no);
1238                 add_v3_v3v3(tnorms[mf->v3], tnorms[mf->v3], f_no);
1239                 if (mf->v4)
1240                         add_v3_v3v3(tnorms[mf->v4], tnorms[mf->v4], f_no);
1241         }
1242         for (i=0; i<numVerts; i++) {
1243                 MVert *mv= &mverts[i];
1244                 float *no= tnorms[i];
1245                 
1246                 if (normalize_v3(no)==0.0) {
1247                         VECCOPY(no, mv->co);
1248                         normalize_v3(no);
1249                 }
1250
1251                 mv->no[0]= (short)(no[0]*32767.0);
1252                 mv->no[1]= (short)(no[1]*32767.0);
1253                 mv->no[2]= (short)(no[2]*32767.0);
1254         }
1255         
1256         MEM_freeN(tnorms);
1257
1258         if (faceNors_r) {
1259                 *faceNors_r = fnors;
1260         } else {
1261                 MEM_freeN(fnors);
1262         }
1263 }
1264
1265 float (*mesh_getVertexCos(Mesh *me, int *numVerts_r))[3]
1266 {
1267         int i, numVerts = me->totvert;
1268         float (*cos)[3] = MEM_mallocN(sizeof(*cos)*numVerts, "vertexcos1");
1269         
1270         if (numVerts_r) *numVerts_r = numVerts;
1271         for (i=0; i<numVerts; i++)
1272                 VECCOPY(cos[i], me->mvert[i].co);
1273         
1274         return cos;
1275 }
1276
1277 UvVertMap *make_uv_vert_map(struct MFace *mface, struct MTFace *tface, unsigned int totface, unsigned int totvert, int selected, float *limit)
1278 {
1279         UvVertMap *vmap;
1280         UvMapVert *buf;
1281         MFace *mf;
1282         MTFace *tf;
1283         unsigned int a;
1284         int     i, totuv, nverts;
1285
1286         totuv = 0;
1287
1288         /* generate UvMapVert array */
1289         mf= mface;
1290         tf= tface;
1291         for(a=0; a<totface; a++, mf++, tf++)
1292                 if(!selected || (!(mf->flag & ME_HIDE) && (mf->flag & ME_FACE_SEL)))
1293                         totuv += (mf->v4)? 4: 3;
1294                 
1295         if(totuv==0)
1296                 return NULL;
1297         
1298         vmap= (UvVertMap*)MEM_callocN(sizeof(*vmap), "UvVertMap");
1299         if (!vmap)
1300                 return NULL;
1301
1302         vmap->vert= (UvMapVert**)MEM_callocN(sizeof(*vmap->vert)*totvert, "UvMapVert*");
1303         buf= vmap->buf= (UvMapVert*)MEM_callocN(sizeof(*vmap->buf)*totuv, "UvMapVert");
1304
1305         if (!vmap->vert || !vmap->buf) {
1306                 free_uv_vert_map(vmap);
1307                 return NULL;
1308         }
1309
1310         mf= mface;
1311         tf= tface;
1312         for(a=0; a<totface; a++, mf++, tf++) {
1313                 if(!selected || (!(mf->flag & ME_HIDE) && (mf->flag & ME_FACE_SEL))) {
1314                         nverts= (mf->v4)? 4: 3;
1315
1316                         for(i=0; i<nverts; i++) {
1317                                 buf->tfindex= i;
1318                                 buf->f= a;
1319                                 buf->separate = 0;
1320                                 buf->next= vmap->vert[*(&mf->v1 + i)];
1321                                 vmap->vert[*(&mf->v1 + i)]= buf;
1322                                 buf++;
1323                         }
1324                 }
1325         }
1326         
1327         /* sort individual uvs for each vert */
1328         tf= tface;
1329         for(a=0; a<totvert; a++) {
1330                 UvMapVert *newvlist= NULL, *vlist=vmap->vert[a];
1331                 UvMapVert *iterv, *v, *lastv, *next;
1332                 float *uv, *uv2, uvdiff[2];
1333
1334                 while(vlist) {
1335                         v= vlist;
1336                         vlist= vlist->next;
1337                         v->next= newvlist;
1338                         newvlist= v;
1339
1340                         uv= (tf+v->f)->uv[v->tfindex];
1341                         lastv= NULL;
1342                         iterv= vlist;
1343
1344                         while(iterv) {
1345                                 next= iterv->next;
1346
1347                                 uv2= (tf+iterv->f)->uv[iterv->tfindex];
1348                                 sub_v2_v2v2(uvdiff, uv2, uv);
1349
1350
1351                                 if(fabs(uv[0]-uv2[0]) < limit[0] && fabs(uv[1]-uv2[1]) < limit[1]) {
1352                                         if(lastv) lastv->next= next;
1353                                         else vlist= next;
1354                                         iterv->next= newvlist;
1355                                         newvlist= iterv;
1356                                 }
1357                                 else
1358                                         lastv=iterv;
1359
1360                                 iterv= next;
1361                         }
1362
1363                         newvlist->separate = 1;
1364                 }
1365
1366                 vmap->vert[a]= newvlist;
1367         }
1368         
1369         return vmap;
1370 }
1371
1372 UvMapVert *get_uv_map_vert(UvVertMap *vmap, unsigned int v)
1373 {
1374         return vmap->vert[v];
1375 }
1376
1377 void free_uv_vert_map(UvVertMap *vmap)
1378 {
1379         if (vmap) {
1380                 if (vmap->vert) MEM_freeN(vmap->vert);
1381                 if (vmap->buf) MEM_freeN(vmap->buf);
1382                 MEM_freeN(vmap);
1383         }
1384 }
1385
1386 /* Generates a map where the key is the vertex and the value is a list
1387    of faces that use that vertex as a corner. The lists are allocated
1388    from one memory pool. */
1389 void create_vert_face_map(ListBase **map, IndexNode **mem, const MFace *mface, const int totvert, const int totface)
1390 {
1391         int i,j;
1392         IndexNode *node = NULL;
1393         
1394         (*map) = MEM_callocN(sizeof(ListBase) * totvert, "vert face map");
1395         (*mem) = MEM_callocN(sizeof(IndexNode) * totface*4, "vert face map mem");
1396         node = *mem;
1397         
1398         /* Find the users */
1399         for(i = 0; i < totface; ++i){
1400                 for(j = 0; j < (mface[i].v4?4:3); ++j, ++node) {
1401                         node->index = i;
1402                         BLI_addtail(&(*map)[((unsigned int*)(&mface[i]))[j]], node);
1403                 }
1404         }
1405 }
1406
1407 /* Generates a map where the key is the vertex and the value is a list
1408    of edges that use that vertex as an endpoint. The lists are allocated
1409    from one memory pool. */
1410 void create_vert_edge_map(ListBase **map, IndexNode **mem, const MEdge *medge, const int totvert, const int totedge)
1411 {
1412         int i, j;
1413         IndexNode *node = NULL;
1414  
1415         (*map) = MEM_callocN(sizeof(ListBase) * totvert, "vert edge map");
1416         (*mem) = MEM_callocN(sizeof(IndexNode) * totedge * 2, "vert edge map mem");
1417         node = *mem;
1418        
1419         /* Find the users */
1420         for(i = 0; i < totedge; ++i){
1421                 for(j = 0; j < 2; ++j, ++node) {
1422                         node->index = i;
1423                         BLI_addtail(&(*map)[((unsigned int*)(&medge[i].v1))[j]], node);
1424                 }
1425         }
1426 }
1427
1428 /* Partial Mesh Visibility */
1429 PartialVisibility *mesh_pmv_copy(PartialVisibility *pmv)
1430 {
1431         PartialVisibility *n= MEM_dupallocN(pmv);
1432         n->vert_map= MEM_dupallocN(pmv->vert_map);
1433         n->edge_map= MEM_dupallocN(pmv->edge_map);
1434         n->old_edges= MEM_dupallocN(pmv->old_edges);
1435         n->old_faces= MEM_dupallocN(pmv->old_faces);
1436         return n;
1437 }
1438
1439 void mesh_pmv_free(PartialVisibility *pv)
1440 {
1441         MEM_freeN(pv->vert_map);
1442         MEM_freeN(pv->edge_map);
1443         MEM_freeN(pv->old_faces);
1444         MEM_freeN(pv->old_edges);
1445         MEM_freeN(pv);
1446 }
1447
1448 void mesh_pmv_revert(Object *ob, Mesh *me)
1449 {
1450         if(me->pv) {
1451                 unsigned i;
1452                 MVert *nve, *old_verts;
1453                 
1454                 /* Reorder vertices */
1455                 nve= me->mvert;
1456                 old_verts = MEM_mallocN(sizeof(MVert)*me->pv->totvert,"PMV revert verts");
1457                 for(i=0; i<me->pv->totvert; ++i)
1458                         old_verts[i]= nve[me->pv->vert_map[i]];
1459
1460                 /* Restore verts, edges and faces */
1461                 CustomData_free_layer_active(&me->vdata, CD_MVERT, me->totvert);
1462                 CustomData_free_layer_active(&me->edata, CD_MEDGE, me->totedge);
1463                 CustomData_free_layer_active(&me->fdata, CD_MFACE, me->totface);
1464
1465                 CustomData_add_layer(&me->vdata, CD_MVERT, CD_ASSIGN, old_verts, me->pv->totvert);
1466                 CustomData_add_layer(&me->edata, CD_MEDGE, CD_ASSIGN, me->pv->old_edges, me->pv->totedge);
1467                 CustomData_add_layer(&me->fdata, CD_MFACE, CD_ASSIGN, me->pv->old_faces, me->pv->totface);
1468                 mesh_update_customdata_pointers(me);
1469
1470                 me->totvert= me->pv->totvert;
1471                 me->totedge= me->pv->totedge;
1472                 me->totface= me->pv->totface;
1473
1474                 me->pv->old_edges= NULL;
1475                 me->pv->old_faces= NULL;
1476
1477                 /* Free maps */
1478                 MEM_freeN(me->pv->edge_map);
1479                 me->pv->edge_map= NULL;
1480                 MEM_freeN(me->pv->vert_map);
1481                 me->pv->vert_map= NULL;
1482
1483 // XXX do this in caller                DAG_id_flush_update(&me->id, OB_RECALC_DATA);
1484         }
1485 }
1486
1487 void mesh_pmv_off(Object *ob, Mesh *me)
1488 {
1489         if(ob && me->pv) {
1490                 mesh_pmv_revert(ob, me);
1491                 MEM_freeN(me->pv);
1492                 me->pv= NULL;
1493         }
1494 }