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