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