Merge with blender svn -> svn merge -r 12064:12150 https://svn.blender.org/svnroot...
[blender-staging.git] / source / blender / src / editmesh.c
1 /**
2  * $Id$
3  *
4  * ***** BEGIN GPL/BL DUAL LICENSE BLOCK *****
5  *
6  * This program is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU General Public License
8  * as published by the Free Software Foundation; either version 2
9  * of the License, or (at your option) any later version. The Blender
10  * Foundation also sells licenses for use in proprietary software under
11  * the Blender License.  See http://www.blender.org/BL/ for information
12  * about this.
13  *
14  * This program is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17  * GNU General Public License for more details.
18  *
19  * You should have received a copy of the GNU General Public License
20  * along with this program; if not, write to the Free Software Foundation,
21  * Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
22  *
23  * The Original Code is Copyright (C) 2001-2002 by NaN Holding BV.
24  * All rights reserved.
25  *
26  * The Original Code is: all of this file.
27  *
28  * Contributor(s): none yet.
29  *
30  * ***** END GPL/BL DUAL LICENSE BLOCK *****
31  */
32
33
34 #include <stdlib.h>
35 #include <string.h>
36 #include <math.h>
37
38 #ifdef HAVE_CONFIG_H
39 #include <config.h>
40 #endif
41
42 #include "MEM_guardedalloc.h"
43
44 #include "PIL_time.h"
45
46 #include "DNA_customdata_types.h"
47 #include "DNA_mesh_types.h"
48 #include "DNA_meshdata_types.h"
49 #include "DNA_object_types.h"
50 #include "DNA_object_force.h"
51 #include "DNA_screen_types.h"
52 #include "DNA_key_types.h"
53 #include "DNA_scene_types.h"
54 #include "DNA_view3d_types.h"
55 #include "DNA_material_types.h"
56 #include "DNA_modifier_types.h"
57 #include "DNA_texture_types.h"
58 #include "DNA_userdef_types.h"
59
60 #include "BLI_blenlib.h"
61 #include "BLI_arithb.h"
62 #include "BLI_editVert.h"
63 #include "BLI_dynstr.h"
64 #include "BLI_rand.h"
65
66 #include "BKE_DerivedMesh.h"
67 #include "BKE_depsgraph.h"
68 #include "BKE_cloth.h"
69 #include "BKE_customdata.h"
70 #include "BKE_global.h"
71 #include "BKE_key.h"
72 #include "BKE_library.h"
73 #include "BKE_main.h"
74 #include "BKE_material.h"
75 #include "BKE_mesh.h"
76 #include "BKE_modifier.h"
77 #include "BKE_object.h"
78 #include "BKE_texture.h"
79 #include "BKE_utildefines.h"
80
81 #ifdef WITH_VERSE
82 #include "BKE_verse.h"
83 #endif
84
85 #include "BIF_editkey.h"
86 #include "BIF_editmesh.h"
87 #include "BIF_editmode_undo.h"
88 #include "BIF_interface.h"
89 #include "BIF_meshtools.h"
90 #include "BIF_mywindow.h"
91 #include "BIF_retopo.h"
92 #include "BIF_space.h"
93 #include "BIF_screen.h"
94 #include "BIF_toolbox.h"
95
96 #ifdef WITH_VERSE
97 #include "BIF_verse.h"
98 #endif
99
100 #include "BSE_view.h"
101 #include "BSE_edit.h"
102 #include "BSE_trans_types.h"
103
104 #include "BDR_drawobject.h"
105 #include "BDR_editobject.h"
106 #include "BDR_editface.h"
107 #include "BDR_vpaint.h"
108
109 #include "LBM_fluidsim.h"
110
111 #include "multires.h"
112 #include "mydevice.h"
113 #include "blendef.h"
114
115 /* own include */
116 #include "editmesh.h"
117
118 /* 
119
120 editmesh.c:
121 - add/alloc/free data
122 - hashtables
123 - enter/exit editmode
124
125 */
126
127
128 /* ***************** HASH ********************* */
129
130
131 #define EDHASHSIZE              (512*512)
132 #define EDHASH(a, b)    (a % EDHASHSIZE)
133
134
135 /* ************ ADD / REMOVE / FIND ****************** */
136
137 /* used to bypass normal calloc with fast one */
138 static void *(*callocvert)(size_t, size_t) = calloc;
139 static void *(*callocedge)(size_t, size_t) = calloc;
140 static void *(*callocface)(size_t, size_t) = calloc;
141
142 EditVert *addvertlist(float *vec, EditVert *example)
143 {
144         EditMesh *em = G.editMesh;
145         EditVert *eve;
146         static int hashnr= 0;
147
148         eve= callocvert(sizeof(EditVert), 1);
149         BLI_addtail(&em->verts, eve);
150         
151         if(vec) VECCOPY(eve->co, vec);
152
153         eve->hash= hashnr++;
154         if( hashnr>=EDHASHSIZE) hashnr= 0;
155
156         /* new verts get keyindex of -1 since they did not
157          * have a pre-editmode vertex order
158          */
159         eve->keyindex = -1;
160 #ifdef WITH_VERSE
161         createVerseVert(eve);
162 #endif
163
164         if(example)
165                 CustomData_em_copy_data(&em->vdata, &em->vdata, example->data, &eve->data);
166         else
167                 CustomData_em_set_default(&em->vdata, &eve->data);
168
169         return eve;
170 }
171
172 void free_editvert (EditVert *eve)
173 {
174 #ifdef WITH_VERSE
175         if(eve->vvert) {
176                 /* it prevents from removing all verse vertexes
177                  * during entering edit mode ... messy solution */
178                 if(G.editMesh->vnode)
179                         b_verse_send_vertex_delete(eve);
180                 else
181                         ((VerseVert*)eve->vvert)->vertex = NULL;
182         }
183 #endif
184
185         EM_remove_selection(eve, EDITVERT);
186         CustomData_em_free_block(&G.editMesh->vdata, &eve->data);
187         if(eve->fast==0)
188                 free(eve);
189 }
190
191
192 EditEdge *findedgelist(EditVert *v1, EditVert *v2)
193 {
194         EditVert *v3;
195         struct HashEdge *he;
196
197         /* swap ? */
198         if( v1 > v2) {
199                 v3= v2; 
200                 v2= v1; 
201                 v1= v3;
202         }
203         
204         if(G.editMesh->hashedgetab==NULL)
205                 G.editMesh->hashedgetab= MEM_callocN(EDHASHSIZE*sizeof(struct HashEdge), "hashedgetab");
206
207         he= G.editMesh->hashedgetab + EDHASH(v1->hash, v2->hash);
208         
209         while(he) {
210                 
211                 if(he->eed && he->eed->v1==v1 && he->eed->v2==v2) return he->eed;
212                 
213                 he= he->next;
214         }
215         return 0;
216 }
217
218 static void insert_hashedge(EditEdge *eed)
219 {
220         /* assuming that eed is not in the list yet, and that a find has been done before */
221         
222         struct HashEdge *first, *he;
223
224         first= G.editMesh->hashedgetab + EDHASH(eed->v1->hash, eed->v2->hash);
225
226         if( first->eed==0 ) {
227                 first->eed= eed;
228         }
229         else {
230                 he= &eed->hash; 
231                 he->eed= eed;
232                 he->next= first->next;
233                 first->next= he;
234         }
235 }
236
237 static void remove_hashedge(EditEdge *eed)
238 {
239         /* assuming eed is in the list */
240         
241         struct HashEdge *first, *he, *prev=NULL;
242
243         he=first= G.editMesh->hashedgetab + EDHASH(eed->v1->hash, eed->v2->hash);
244
245         while(he) {
246                 if(he->eed == eed) {
247                         /* remove from list */
248                         if(he==first) {
249                                 if(first->next) {
250                                         he= first->next;
251                                         first->eed= he->eed;
252                                         first->next= he->next;
253                                 }
254                                 else he->eed= 0;
255                         }
256                         else {
257                                 prev->next= he->next;
258                         }
259                         return;
260                 }
261                 prev= he;
262                 he= he->next;
263         }
264 }
265
266 EditEdge *addedgelist(EditVert *v1, EditVert *v2, EditEdge *example)
267 {
268         EditMesh *em = G.editMesh;
269         EditVert *v3;
270         EditEdge *eed;
271         int swap= 0;
272         
273         if(v1==v2) return NULL;
274         if(v1==NULL || v2==NULL) return NULL;
275
276         /* swap ? */
277         if(v1>v2) {
278                 v3= v2; 
279                 v2= v1; 
280                 v1= v3;
281                 swap= 1;
282         }
283         
284         /* find in hashlist */
285         eed= findedgelist(v1, v2);
286
287         if(eed==NULL) {
288         
289                 eed= (EditEdge *)callocedge(sizeof(EditEdge), 1);
290                 eed->v1= v1;
291                 eed->v2= v2;
292                 BLI_addtail(&em->edges, eed);
293                 eed->dir= swap;
294                 insert_hashedge(eed);
295                 
296                 /* copy edge data:
297                    rule is to do this with addedgelist call, before addfacelist */
298                 if(example) {
299                         eed->crease= example->crease;
300                         eed->sharp = example->sharp;
301                         eed->seam = example->seam;
302                         eed->h |= (example->h & EM_FGON);
303                 }
304         }
305
306         return eed;
307 }
308
309 void remedge(EditEdge *eed)
310 {
311         EditMesh *em = G.editMesh;
312
313         BLI_remlink(&em->edges, eed);
314         remove_hashedge(eed);
315 }
316
317 void free_editedge(EditEdge *eed)
318 {
319         EM_remove_selection(eed, EDITEDGE);
320         if(eed->fast==0){ 
321                 free(eed);
322         }
323 }
324
325 void free_editface(EditFace *efa)
326 {
327 #ifdef WITH_VERSE
328         if(efa->vface) {
329                 /* it prevents from removing all verse faces
330                  * during entering edit mode ... messy solution */
331                 if(G.editMesh->vnode)
332                         b_verse_send_face_delete(efa);
333                 else
334                         ((VerseFace*)efa->vface)->face = NULL;
335         }
336 #endif
337         EM_remove_selection(efa, EDITFACE);
338         
339         if (G.editMesh->act_face==efa)
340                 EM_set_actFace(NULL);
341                 
342         CustomData_em_free_block(&G.editMesh->fdata, &efa->data);
343         if(efa->fast==0)
344                 free(efa);
345 }
346
347 void free_vertlist(ListBase *edve) 
348 {
349         EditVert *eve, *next;
350
351         if (!edve) return;
352
353         eve= edve->first;
354         while(eve) {
355                 next= eve->next;
356                 free_editvert(eve);
357                 eve= next;
358         }
359         edve->first= edve->last= NULL;
360 }
361
362 void free_edgelist(ListBase *lb)
363 {
364         EditEdge *eed, *next;
365         
366         eed= lb->first;
367         while(eed) {
368                 next= eed->next;
369                 free_editedge(eed);
370                 eed= next;
371         }
372         lb->first= lb->last= NULL;
373 }
374
375 void free_facelist(ListBase *lb)
376 {
377         EditFace *efa, *next;
378         
379         efa= lb->first;
380         while(efa) {
381                 next= efa->next;
382                 free_editface(efa);
383                 efa= next;
384         }
385         lb->first= lb->last= NULL;
386 }
387
388 EditFace *addfacelist(EditVert *v1, EditVert *v2, EditVert *v3, EditVert *v4, EditFace *example, EditFace *exampleEdges)
389 {
390         EditMesh *em = G.editMesh;
391         EditFace *efa;
392         EditEdge *e1, *e2=0, *e3=0, *e4=0;
393
394         /* added sanity check... seems to happen for some tools, or for enter editmode for corrupted meshes */
395         if(v1==v4 || v2==v4 || v3==v4) v4= NULL;
396         
397         /* add face to list and do the edges */
398         if(exampleEdges) {
399                 e1= addedgelist(v1, v2, exampleEdges->e1);
400                 e2= addedgelist(v2, v3, exampleEdges->e2);
401                 if(v4) e3= addedgelist(v3, v4, exampleEdges->e3); 
402                 else e3= addedgelist(v3, v1, exampleEdges->e3);
403                 if(v4) e4= addedgelist(v4, v1, exampleEdges->e4);
404         }
405         else {
406                 e1= addedgelist(v1, v2, NULL);
407                 e2= addedgelist(v2, v3, NULL);
408                 if(v4) e3= addedgelist(v3, v4, NULL); 
409                 else e3= addedgelist(v3, v1, NULL);
410                 if(v4) e4= addedgelist(v4, v1, NULL);
411         }
412         
413         if(v1==v2 || v2==v3 || v1==v3) return NULL;
414         if(e2==0) return NULL;
415
416         efa= (EditFace *)callocface(sizeof(EditFace), 1);
417         efa->v1= v1;
418         efa->v2= v2;
419         efa->v3= v3;
420         efa->v4= v4;
421
422         efa->e1= e1;
423         efa->e2= e2;
424         efa->e3= e3;
425         efa->e4= e4;
426
427         if(example) {
428                 efa->mat_nr= example->mat_nr;
429                 efa->flag= example->flag;
430                 CustomData_em_copy_data(&em->fdata, &em->fdata, example->data, &efa->data);
431         }
432         else {
433                 if (G.obedit && G.obedit->actcol)
434                         efa->mat_nr= G.obedit->actcol-1;
435
436                 CustomData_em_set_default(&em->fdata, &efa->data);
437         }
438
439         BLI_addtail(&em->faces, efa);
440
441         if(efa->v4) {
442                 CalcNormFloat4(efa->v1->co, efa->v2->co, efa->v3->co, efa->v4->co, efa->n);
443                 CalcCent4f(efa->cent, efa->v1->co, efa->v2->co, efa->v3->co, efa->v4->co);
444         }
445         else {
446                 CalcNormFloat(efa->v1->co, efa->v2->co, efa->v3->co, efa->n);
447                 CalcCent3f(efa->cent, efa->v1->co, efa->v2->co, efa->v3->co);
448         }
449
450 #ifdef WITH_VERSE
451         createVerseFace(efa);
452 #endif
453
454         return efa;
455 }
456
457 /* ************************ end add/new/find ************  */
458
459 /* ************************ Edit{Vert,Edge,Face} utilss ***************************** */
460
461 /* some nice utility functions */
462
463 EditVert *editedge_getOtherVert(EditEdge *eed, EditVert *eve)
464 {
465         if (eve==eed->v1) {
466                 return eed->v2;
467         } else if (eve==eed->v2) {
468                 return eed->v1;
469         } else {
470                 return NULL;
471         }
472 }
473
474 EditVert *editedge_getSharedVert(EditEdge *eed, EditEdge *eed2) 
475 {
476         if (eed->v1==eed2->v1 || eed->v1==eed2->v2) {
477                 return eed->v1;
478         } else if (eed->v2==eed2->v1 || eed->v2==eed2->v2) {
479                 return eed->v2;
480         } else {
481                 return NULL;
482         }
483 }
484
485 int editedge_containsVert(EditEdge *eed, EditVert *eve) 
486 {
487         return (eed->v1==eve || eed->v2==eve);
488 }
489
490 int editface_containsVert(EditFace *efa, EditVert *eve) 
491 {
492         return (efa->v1==eve || efa->v2==eve || efa->v3==eve || (efa->v4 && efa->v4==eve));
493 }
494
495 int editface_containsEdge(EditFace *efa, EditEdge *eed) 
496 {
497         return (efa->e1==eed || efa->e2==eed || efa->e3==eed || (efa->e4 && efa->e4==eed));
498 }
499
500
501 /* ************************ stuct EditMesh manipulation ***************************** */
502
503 /* fake callocs for fastmalloc below */
504 static void *calloc_fastvert(size_t size, size_t nr)
505 {
506         EditVert *eve= G.editMesh->curvert++;
507         eve->fast= 1;
508         return eve;
509 }
510 static void *calloc_fastedge(size_t size, size_t nr)
511 {
512         EditEdge *eed= G.editMesh->curedge++;
513         eed->fast= 1;
514         return eed;
515 }
516 static void *calloc_fastface(size_t size, size_t nr)
517 {
518         EditFace *efa= G.editMesh->curface++;
519         efa->fast= 1;
520         return efa;
521 }
522
523 /* allocate 1 chunk for all vertices, edges, faces. These get tagged to
524    prevent it from being freed
525 */
526 static void init_editmesh_fastmalloc(EditMesh *em, int totvert, int totedge, int totface)
527 {
528         if(totvert) em->allverts= MEM_callocN(totvert*sizeof(EditVert), "allverts");
529         else em->allverts= NULL;
530         em->curvert= em->allverts;
531         
532         if(totedge==0) totedge= 4*totface;      // max possible
533
534         if(totedge) em->alledges= MEM_callocN(totedge*sizeof(EditEdge), "alledges");
535         else em->alledges= NULL;
536         em->curedge= em->alledges;
537         
538         if(totface) em->allfaces= MEM_callocN(totface*sizeof(EditFace), "allfaces");
539         else em->allfaces= NULL;
540         em->curface= em->allfaces;
541
542         callocvert= calloc_fastvert;
543         callocedge= calloc_fastedge;
544         callocface= calloc_fastface;
545 }
546
547 static void end_editmesh_fastmalloc(void)
548 {
549         callocvert= calloc;
550         callocedge= calloc;
551         callocface= calloc;
552 }
553
554 void free_editMesh(EditMesh *em)
555 {
556 #ifdef WITH_VERSE
557         struct VNode *vnode=NULL;
558 #endif
559         if(em==NULL) return;
560
561 #ifdef WITH_VERSE
562         if(em->vnode) {
563                 vnode = (VNode*)em->vnode;
564                 em->vnode = NULL;
565         }
566 #endif
567
568         if(em->verts.first) free_vertlist(&em->verts);
569         if(em->edges.first) free_edgelist(&em->edges);
570         if(em->faces.first) free_facelist(&em->faces);
571         if(em->selected.first) BLI_freelistN(&(em->selected));
572
573         CustomData_free(&em->vdata, 0);
574         CustomData_free(&em->fdata, 0);
575
576         if(em->derivedFinal) {
577                 if (em->derivedFinal!=em->derivedCage) {
578                         em->derivedFinal->needsFree= 1;
579                         em->derivedFinal->release(em->derivedFinal);
580                 }
581                 em->derivedFinal= NULL;
582         }
583         if(em->derivedCage) {
584                 em->derivedCage->needsFree= 1;
585                 em->derivedCage->release(em->derivedCage);
586                 em->derivedCage= NULL;
587         }
588
589 #ifdef WITH_VERSE
590         if(vnode) {
591                 em->vnode = (void*)vnode;
592         }
593 #endif
594
595         /* DEBUG: hashtabs are slowest part of enter/exit editmode. here a testprint */
596 #if 0
597         if(em->hashedgetab) {
598                 HashEdge *he, *hen;
599                 int a, used=0, max=0, nr;
600                 he= em->hashedgetab;
601                 for(a=0; a<EDHASHSIZE; a++, he++) {
602                         if(he->eed) used++;
603                         hen= he->next;
604                         nr= 0;
605                         while(hen) {
606                                 nr++;
607                                 hen= hen->next;
608                         }
609                         if(max<nr) max= nr;
610                 }
611                 printf("hastab used %d max %d\n", used, max);
612         }
613 #endif
614         if(em->hashedgetab) MEM_freeN(em->hashedgetab);
615         em->hashedgetab= NULL;
616         
617         if(em->allverts) MEM_freeN(em->allverts);
618         if(em->alledges) MEM_freeN(em->alledges);
619         if(em->allfaces) MEM_freeN(em->allfaces);
620         
621         em->allverts= em->curvert= NULL;
622         em->alledges= em->curedge= NULL;
623         em->allfaces= em->curface= NULL;
624         
625         mesh_octree_table(NULL, NULL, 'e');
626         
627         G.totvert= G.totface= 0;
628
629         if(em->retopo_paint_data) retopo_free_paint_data(em->retopo_paint_data);
630         em->retopo_paint_data= NULL;
631 }
632
633 /* on G.editMesh */
634 static void editMesh_set_hash(void)
635 {
636         EditEdge *eed;
637
638         G.editMesh->hashedgetab= NULL;
639         
640         for(eed=G.editMesh->edges.first; eed; eed= eed->next)  {
641                 if( findedgelist(eed->v1, eed->v2)==NULL )
642                         insert_hashedge(eed);
643         }
644
645 }
646
647
648 /* ************************ IN & OUT EDITMODE ***************************** */
649
650
651 static void edge_normal_compare(EditEdge *eed, EditFace *efa1)
652 {
653         EditFace *efa2;
654         float cent1[3], cent2[3];
655         float inp;
656         
657         efa2 = eed->tmp.f;
658         if(efa1==efa2) return;
659         
660         inp= efa1->n[0]*efa2->n[0] + efa1->n[1]*efa2->n[1] + efa1->n[2]*efa2->n[2];
661         if(inp<0.999 && inp >-0.999) eed->f2= 1;
662                 
663         if(efa1->v4) CalcCent4f(cent1, efa1->v1->co, efa1->v2->co, efa1->v3->co, efa1->v4->co);
664         else CalcCent3f(cent1, efa1->v1->co, efa1->v2->co, efa1->v3->co);
665         if(efa2->v4) CalcCent4f(cent2, efa2->v1->co, efa2->v2->co, efa2->v3->co, efa2->v4->co);
666         else CalcCent3f(cent2, efa2->v1->co, efa2->v2->co, efa2->v3->co);
667         
668         VecSubf(cent1, cent2, cent1);
669         Normalize(cent1);
670         inp= cent1[0]*efa1->n[0] + cent1[1]*efa1->n[1] + cent1[2]*efa1->n[2]; 
671
672         if(inp < -0.001 ) eed->f1= 1;
673 }
674
675 #if 0
676 typedef struct {
677         EditEdge *eed;
678         float noLen,no[3];
679         int adjCount;
680 } EdgeDrawFlagInfo;
681
682 static int edgeDrawFlagInfo_cmp(const void *av, const void *bv)
683 {
684         const EdgeDrawFlagInfo *a = av;
685         const EdgeDrawFlagInfo *b = bv;
686
687         if (a->noLen<b->noLen) return -1;
688         else if (a->noLen>b->noLen) return 1;
689         else return 0;
690 }
691 #endif
692
693 static void edge_drawflags(void)
694 {
695         EditMesh *em = G.editMesh;
696         EditVert *eve;
697         EditEdge *eed, *e1, *e2, *e3, *e4;
698         EditFace *efa;
699         
700         /* - count number of times edges are used in faces: 0 en 1 time means draw edge
701          * - edges more than 1 time used: in *tmp.f is pointer to first face
702          * - check all faces, when normal differs to much: draw (flag becomes 1)
703          */
704
705         /* later on: added flags for 'cylinder' and 'sphere' intersection tests in old
706            game engine (2.04)
707          */
708         
709         recalc_editnormals();
710         
711         /* init */
712         eve= em->verts.first;
713         while(eve) {
714                 eve->f1= 1;             /* during test it's set at zero */
715                 eve= eve->next;
716         }
717         eed= em->edges.first;
718         while(eed) {
719                 eed->f2= eed->f1= 0;
720                 eed->tmp.f = 0;
721                 eed= eed->next;
722         }
723
724         efa= em->faces.first;
725         while(efa) {
726                 e1= efa->e1;
727                 e2= efa->e2;
728                 e3= efa->e3;
729                 e4= efa->e4;
730                 if(e1->f2<4) e1->f2+= 1;
731                 if(e2->f2<4) e2->f2+= 1;
732                 if(e3->f2<4) e3->f2+= 1;
733                 if(e4 && e4->f2<4) e4->f2+= 1;
734                 
735                 if(e1->tmp.f == 0) e1->tmp.f = (void *) efa;
736                 if(e2->tmp.f == 0) e2->tmp.f = (void *) efa;
737                 if(e3->tmp.f ==0) e3->tmp.f = (void *) efa;
738                 if(e4 && (e4->tmp.f == 0)) e4->tmp.f = (void *) efa;
739                 
740                 efa= efa->next;
741         }
742
743         if(G.f & G_ALLEDGES) {
744                 efa= em->faces.first;
745                 while(efa) {
746                         if(efa->e1->f2>=2) efa->e1->f2= 1;
747                         if(efa->e2->f2>=2) efa->e2->f2= 1;
748                         if(efa->e3->f2>=2) efa->e3->f2= 1;
749                         if(efa->e4 && efa->e4->f2>=2) efa->e4->f2= 1;
750                         
751                         efa= efa->next;
752                 }               
753         }       
754         else {
755                 
756                 /* handle single-edges for 'test cylinder flag' (old engine) */
757                 
758                 eed= em->edges.first;
759                 while(eed) {
760                         if(eed->f2==1) eed->f1= 1;
761                         eed= eed->next;
762                 }
763
764                 /* all faces, all edges with flag==2: compare normal */
765                 efa= em->faces.first;
766                 while(efa) {
767                         if(efa->e1->f2==2) edge_normal_compare(efa->e1, efa);
768                         else efa->e1->f2= 1;
769                         if(efa->e2->f2==2) edge_normal_compare(efa->e2, efa);
770                         else efa->e2->f2= 1;
771                         if(efa->e3->f2==2) edge_normal_compare(efa->e3, efa);
772                         else efa->e3->f2= 1;
773                         if(efa->e4) {
774                                 if(efa->e4->f2==2) edge_normal_compare(efa->e4, efa);
775                                 else efa->e4->f2= 1;
776                         }
777                         efa= efa->next;
778                 }
779                 
780                 /* sphere collision flag */
781                 
782                 eed= em->edges.first;
783                 while(eed) {
784                         if(eed->f1!=1) {
785                                 eed->v1->f1= eed->v2->f1= 0;
786                         }
787                         eed= eed->next;
788                 }
789                 
790         }
791 }
792
793 /* turns Mesh into editmesh */
794 void make_editMesh()
795 {
796         Mesh *me= G.obedit->data;
797         EditMesh *em= G.editMesh;
798         MFace *mface;
799         MVert *mvert;
800         MSelect *mselect;
801         KeyBlock *actkey;
802         EditVert *eve, **evlist, *eve1, *eve2, *eve3, *eve4;
803         EditFace *efa;
804         EditEdge *eed;
805         EditSelection *ese;
806         int tot, a, eekadoodle= 0;
807
808 #ifdef WITH_VERSE
809         if(me->vnode){
810                 create_edit_mesh_from_geom_node(me->vnode);
811                 return;
812         }
813 #endif
814
815         /* because of reload */
816         free_editMesh(em);
817         
818         em->act_face = NULL;
819         G.totvert= tot= me->totvert;
820         G.totedge= me->totedge;
821         G.totface= me->totface;
822         
823         if(tot==0) {
824                 countall();
825                 return;
826         }
827         
828         /* initialize fastmalloc for editmesh */
829         init_editmesh_fastmalloc(em, me->totvert, me->totedge, me->totface);
830
831         actkey = ob_get_keyblock(G.obedit);
832         if(actkey) {
833                 strcpy(G.editModeTitleExtra, "(Key) ");
834                 key_to_mesh(actkey, me);
835                 tot= actkey->totelem;
836         }
837
838         /* make editverts */
839         CustomData_copy(&me->vdata, &em->vdata, CD_MASK_EDITMESH, CD_CALLOC, 0);
840         mvert= me->mvert;
841
842         evlist= (EditVert **)MEM_mallocN(tot*sizeof(void *),"evlist");
843         for(a=0; a<tot; a++, mvert++) {
844                 eve= addvertlist(mvert->co, NULL);
845                 evlist[a]= eve;
846                 
847                 // face select sets selection in next loop
848                 if( (FACESEL_PAINT_TEST)==0 )
849                         eve->f |= (mvert->flag & 1);
850                 
851                 if (mvert->flag & ME_HIDE) eve->h= 1;           
852                 eve->no[0]= mvert->no[0]/32767.0;
853                 eve->no[1]= mvert->no[1]/32767.0;
854                 eve->no[2]= mvert->no[2]/32767.0;
855
856                 /* lets overwrite the keyindex of the editvert
857                  * with the order it used to be in before
858                  * editmode
859                  */
860                 eve->keyindex = a;
861
862                 CustomData_to_em_block(&me->vdata, &em->vdata, a, &eve->data);
863         }
864
865         if(actkey && actkey->totelem!=me->totvert);
866         else {
867                 MEdge *medge= me->medge;
868                 
869                 CustomData_copy(&me->edata, &em->edata, CD_MASK_EDITMESH, CD_CALLOC, 0);
870                 /* make edges */
871                 for(a=0; a<me->totedge; a++, medge++) {
872                         eed= addedgelist(evlist[medge->v1], evlist[medge->v2], NULL);
873                         /* eed can be zero when v1 and v2 are identical, dxf import does this... */
874                         if(eed) {
875                                 eed->crease= ((float)medge->crease)/255.0;
876                                 
877                                 if(medge->flag & ME_SEAM) eed->seam= 1;
878                                 if(medge->flag & ME_SHARP) eed->sharp = 1;
879                                 if(medge->flag & SELECT) eed->f |= SELECT;
880                                 if(medge->flag & ME_FGON) eed->h= EM_FGON;      // 2 different defines!
881                                 if(medge->flag & ME_HIDE) eed->h |= 1;
882                                 if(G.scene->selectmode==SCE_SELECT_EDGE) 
883                                         EM_select_edge(eed, eed->f & SELECT);           // force edge selection to vertices, seems to be needed ...
884                                 CustomData_to_em_block(&me->edata,&em->edata, a, &eed->data);
885                         }
886                 }
887                 
888                 CustomData_copy(&me->fdata, &em->fdata, CD_MASK_EDITMESH, CD_CALLOC, 0);
889
890                 /* make faces */
891                 mface= me->mface;
892
893                 for(a=0; a<me->totface; a++, mface++) {
894                         eve1= evlist[mface->v1];
895                         eve2= evlist[mface->v2];
896                         if(!mface->v3) eekadoodle= 1;
897                         eve3= evlist[mface->v3];
898                         if(mface->v4) eve4= evlist[mface->v4]; else eve4= NULL;
899                         
900                         efa= addfacelist(eve1, eve2, eve3, eve4, NULL, NULL);
901
902                         if(efa) {
903                                 CustomData_to_em_block(&me->fdata, &em->fdata, a, &efa->data);
904
905                                 efa->mat_nr= mface->mat_nr;
906                                 efa->flag= mface->flag & ~ME_HIDE;
907                                 
908                                 /* select and hide face flag */
909                                 if(mface->flag & ME_FACE_SEL) {
910                                         efa->f |= SELECT;
911                                 }
912                                 if(mface->flag & ME_HIDE) efa->h= 1;
913
914                                 if((FACESEL_PAINT_TEST) && (efa->f & SELECT))
915                                         EM_select_face(efa, 1); /* flush down */
916                                 
917                                 if (a==me->act_face)
918                                         em->act_face = efa;
919                         }
920                 }
921         }
922         
923         if(eekadoodle)
924                 error("This Mesh has old style edgecodes, please put it in the bugtracker!");
925         
926         MEM_freeN(evlist);
927
928         end_editmesh_fastmalloc();      // resets global function pointers
929         
930         if(me->mselect){
931                 //restore editselections
932                 EM_init_index_arrays(1,1,1);
933                 mselect = me->mselect;
934                 
935                 for(a=0; a<me->totselect; a++, mselect++){
936                         /*check if recorded selection is still valid, if so copy into editmesh*/
937                         if( (mselect->type == EDITVERT && me->mvert[mselect->index].flag & SELECT) || (mselect->type == EDITEDGE && me->medge[mselect->index].flag & SELECT) || (mselect->type == EDITFACE && me->mface[mselect->index].flag & ME_FACE_SEL) ){
938                                 ese = MEM_callocN(sizeof(EditSelection), "Edit Selection");
939                                 ese->type = mselect->type;      
940                                 if(ese->type == EDITVERT) ese->data = EM_get_vert_for_index(mselect->index); else
941                                 if(ese->type == EDITEDGE) ese->data = EM_get_edge_for_index(mselect->index); else
942                                 if(ese->type == EDITFACE) ese->data = EM_get_face_for_index(mselect->index);
943                                 BLI_addtail(&(em->selected),ese);
944                         }
945                 }
946                 EM_free_index_arrays();
947         }
948         /* this creates coherent selections. also needed for older files */
949         EM_selectmode_set();
950         /* paranoia check to enforce hide rules */
951         EM_hide_reset();
952         /* sets helper flags which arent saved */
953         EM_fgon_flags();
954         
955         countall();
956         
957 }
958
959 /* makes Mesh out of editmesh */
960 void load_editMesh(void)
961 {
962         EditMesh *em = G.editMesh;
963         Mesh *me= G.obedit->data;
964         MVert *mvert, *oldverts;
965         MEdge *medge;
966         MFace *mface;
967         MSelect *mselect;
968         EditVert *eve;
969         EditFace *efa;
970         EditEdge *eed;
971         EditSelection *ese;
972         float *fp, *newkey, *oldkey, nor[3];
973         int i, a, ototvert, totedge=0;
974
975 #ifdef WITH_VERSE
976         if(em->vnode) {
977                 struct VNode *vnode = (VNode*)em->vnode;
978                 ((VGeomData*)vnode->data)->editmesh = NULL;
979                 em->vnode = NULL;
980         }
981 #endif
982
983         countall();
984
985         /* this one also tests of edges are not in faces: */
986         /* eed->f2==0: not in face, f2==1: draw it */
987         /* eed->f1 : flag for dynaface (cylindertest, old engine) */
988         /* eve->f1 : flag for dynaface (sphere test, old engine) */
989         /* eve->f2 : being used in vertexnormals */
990         edge_drawflags();
991         
992         eed= em->edges.first;
993         while(eed) {
994                 totedge++;
995                 eed= eed->next;
996         }
997         
998         /* new Vertex block */
999         if(G.totvert==0) mvert= NULL;
1000         else mvert= MEM_callocN(G.totvert*sizeof(MVert), "loadeditMesh vert");
1001
1002         /* new Edge block */
1003         if(totedge==0) medge= NULL;
1004         else medge= MEM_callocN(totedge*sizeof(MEdge), "loadeditMesh edge");
1005         
1006         /* new Face block */
1007         if(G.totface==0) mface= NULL;
1008         else mface= MEM_callocN(G.totface*sizeof(MFace), "loadeditMesh face");
1009
1010         /* lets save the old verts just in case we are actually working on
1011          * a key ... we now do processing of the keys at the end */
1012         oldverts= me->mvert;
1013         ototvert= me->totvert;
1014
1015         /* don't free this yet */
1016         CustomData_set_layer(&me->vdata, CD_MVERT, NULL);
1017
1018         /* free custom data */
1019         CustomData_free(&me->vdata, me->totvert);
1020         CustomData_free(&me->edata, me->totedge);
1021         CustomData_free(&me->fdata, me->totface);
1022
1023         /* add new custom data */
1024         me->totvert= G.totvert;
1025         me->totedge= totedge;
1026         me->totface= G.totface;
1027
1028         CustomData_copy(&em->vdata, &me->vdata, CD_MASK_MESH, CD_CALLOC, me->totvert);
1029         CustomData_copy(&em->edata, &me->edata, CD_MASK_MESH, CD_CALLOC, me->totedge);
1030         CustomData_copy(&em->fdata, &me->fdata, CD_MASK_MESH, CD_CALLOC, me->totface);
1031
1032         CustomData_add_layer(&me->vdata, CD_MVERT, CD_ASSIGN, mvert, me->totvert);
1033         CustomData_add_layer(&me->edata, CD_MEDGE, CD_ASSIGN, medge, me->totedge);
1034         CustomData_add_layer(&me->fdata, CD_MFACE, CD_ASSIGN, mface, me->totface);
1035         mesh_update_customdata_pointers(me);
1036
1037         /* the vertices, use ->tmp.l as counter */
1038         eve= em->verts.first;
1039         a= 0;
1040
1041         while(eve) {
1042                 VECCOPY(mvert->co, eve->co);
1043                 mvert->mat_nr= 255;  /* what was this for, halos? */
1044                 
1045                 /* vertex normal */
1046                 VECCOPY(nor, eve->no);
1047                 VecMulf(nor, 32767.0);
1048                 VECCOPY(mvert->no, nor);
1049
1050                 /* note: it used to remove me->dvert when it was not in use, cancelled
1051                    that... annoying when you have a fresh vgroup */
1052                 CustomData_from_em_block(&em->vdata, &me->vdata, eve->data, a);
1053
1054                 eve->tmp.l = a++;  /* counter */
1055                         
1056                 mvert->flag= 0;
1057                 if(eve->f1==1) mvert->flag |= ME_SPHERETEST;
1058                 mvert->flag |= (eve->f & SELECT);
1059                 if (eve->h) mvert->flag |= ME_HIDE;                     
1060
1061 #ifdef WITH_VERSE
1062                 if(eve->vvert) {
1063                         ((VerseVert*)eve->vvert)->vertex = NULL;
1064                         eve->vvert = NULL;
1065                 }
1066 #endif                  
1067                 eve= eve->next;
1068                 mvert++;
1069         }
1070
1071         /* the edges */
1072         a= 0;
1073         eed= em->edges.first;
1074         while(eed) {
1075                 medge->v1= (unsigned int) eed->v1->tmp.l;
1076                 medge->v2= (unsigned int) eed->v2->tmp.l;
1077                 
1078                 medge->flag= (eed->f & SELECT) | ME_EDGERENDER;
1079                 if(eed->f2<2) medge->flag |= ME_EDGEDRAW;
1080                 if(eed->f2==0) medge->flag |= ME_LOOSEEDGE;
1081                 if(eed->sharp) medge->flag |= ME_SHARP;
1082                 if(eed->seam) medge->flag |= ME_SEAM;
1083                 if(eed->h & EM_FGON) medge->flag |= ME_FGON;    // different defines yes
1084                 if(eed->h & 1) medge->flag |= ME_HIDE;
1085                 
1086                 medge->crease= (char)(255.0*eed->crease);
1087                 CustomData_from_em_block(&em->edata, &me->edata, eed->data, a);         
1088
1089                 eed->tmp.l = a++;
1090                 
1091                 medge++;
1092                 eed= eed->next;
1093         }
1094
1095         /* the faces */
1096         a = 0;
1097         efa= em->faces.first;
1098         i = 0;
1099         while(efa) {
1100                 mface= &((MFace *) me->mface)[i];
1101                 
1102                 mface->v1= (unsigned int) efa->v1->tmp.l;
1103                 mface->v2= (unsigned int) efa->v2->tmp.l;
1104                 mface->v3= (unsigned int) efa->v3->tmp.l;
1105                 if (efa->v4) mface->v4 = (unsigned int) efa->v4->tmp.l;
1106
1107                 mface->mat_nr= efa->mat_nr;
1108                 
1109                 mface->flag= efa->flag;
1110                 /* bit 0 of flag is already taken for smooth... */
1111                 
1112                 if(efa->h) {
1113                         mface->flag |= ME_HIDE;
1114                         mface->flag &= ~ME_FACE_SEL;
1115                 } else {
1116                         if(efa->f & 1) mface->flag |= ME_FACE_SEL;
1117                         else mface->flag &= ~ME_FACE_SEL;
1118                 }
1119                 
1120                 /* mat_nr in vertex */
1121                 if(me->totcol>1) {
1122                         mvert= me->mvert+mface->v1;
1123                         if(mvert->mat_nr == (char)255) mvert->mat_nr= mface->mat_nr;
1124                         mvert= me->mvert+mface->v2;
1125                         if(mvert->mat_nr == (char)255) mvert->mat_nr= mface->mat_nr;
1126                         mvert= me->mvert+mface->v3;
1127                         if(mvert->mat_nr == (char)255) mvert->mat_nr= mface->mat_nr;
1128                         if(mface->v4) {
1129                                 mvert= me->mvert+mface->v4;
1130                                 if(mvert->mat_nr == (char)255) mvert->mat_nr= mface->mat_nr;
1131                         }
1132                 }
1133                         
1134                 /* watch: efa->e1->f2==0 means loose edge */ 
1135                         
1136                 if(efa->e1->f2==1) {
1137                         efa->e1->f2= 2;
1138                 }                       
1139                 if(efa->e2->f2==1) {
1140                         efa->e2->f2= 2;
1141                 }
1142                 if(efa->e3->f2==1) {
1143                         efa->e3->f2= 2;
1144                 }
1145                 if(efa->e4 && efa->e4->f2==1) {
1146                         efa->e4->f2= 2;
1147                 }
1148
1149                 CustomData_from_em_block(&em->fdata, &me->fdata, efa->data, i);
1150
1151                 /* no index '0' at location 3 or 4 */
1152                 test_index_face(mface, &me->fdata, i, efa->v4?4:3);
1153                 
1154                 if (a==me->act_face)
1155                         EM_set_actFace(efa);
1156
1157 #ifdef WITH_VERSE
1158                 if(efa->vface) {
1159                         ((VerseFace*)efa->vface)->face = NULL;
1160                         efa->vface = NULL;
1161                 }
1162 #endif          
1163                 efa->tmp.l = a++;
1164                 i++;
1165                 efa= efa->next;
1166         }
1167
1168         /* patch hook indices and vertex parents */
1169         {
1170                 Object *ob;
1171                 ModifierData *md;
1172                 EditVert *eve, **vertMap = NULL;
1173                 int i,j;
1174
1175                 for (ob=G.main->object.first; ob; ob=ob->id.next) {
1176                         if (ob->parent==G.obedit && ELEM(ob->partype, PARVERT1,PARVERT3)) {
1177                                 
1178                                 /* duplicate code from below, make it function later...? */
1179                                 if (!vertMap) {
1180                                         vertMap = MEM_callocN(sizeof(*vertMap)*ototvert, "vertMap");
1181                                         
1182                                         for (eve=em->verts.first; eve; eve=eve->next) {
1183                                                 if (eve->keyindex!=-1)
1184                                                         vertMap[eve->keyindex] = eve;
1185                                         }
1186                                 }
1187                                 if(ob->par1 < ototvert) {
1188                                         eve = vertMap[ob->par1];
1189                                         if(eve) ob->par1= eve->tmp.l;
1190                                 }
1191                                 if(ob->par2 < ototvert) {
1192                                         eve = vertMap[ob->par2];
1193                                         if(eve) ob->par2= eve->tmp.l;
1194                                 }
1195                                 if(ob->par3 < ototvert) {
1196                                         eve = vertMap[ob->par3];
1197                                         if(eve) ob->par3= eve->tmp.l;
1198                                 }
1199                                 
1200                         }
1201                         if (ob->data==me) {
1202                                 for (md=ob->modifiers.first; md; md=md->next) {
1203                                         if (md->type==eModifierType_Hook) {
1204                                                 HookModifierData *hmd = (HookModifierData*) md;
1205
1206                                                 if (!vertMap) {
1207                                                         vertMap = MEM_callocN(sizeof(*vertMap)*ototvert, "vertMap");
1208
1209                                                         for (eve=em->verts.first; eve; eve=eve->next) {
1210                                                                 if (eve->keyindex!=-1)
1211                                                                         vertMap[eve->keyindex] = eve;
1212                                                         }
1213                                                 }
1214                                                 
1215                                                 for (i=j=0; i<hmd->totindex; i++) {
1216                                                         if(hmd->indexar[i] < ototvert) {
1217                                                                 eve = vertMap[hmd->indexar[i]];
1218                                                                 
1219                                                                 if (eve) {
1220                                                                         hmd->indexar[j++] = eve->tmp.l;
1221                                                                 }
1222                                                         }
1223                                                         else j++;
1224                                                 }
1225
1226                                                 hmd->totindex = j;
1227                                         }
1228                                 }
1229                         }
1230                 }
1231
1232                 if (vertMap) MEM_freeN(vertMap);
1233         }
1234
1235         /* are there keys? */
1236         if(me->key) {
1237                 KeyBlock *currkey, *actkey = ob_get_keyblock(G.obedit);
1238
1239                 /* Lets reorder the key data so that things line up roughly
1240                  * with the way things were before editmode */
1241                 currkey = me->key->block.first;
1242                 while(currkey) {
1243                         
1244                         fp= newkey= MEM_callocN(me->key->elemsize*G.totvert,  "currkey->data");
1245                         oldkey = currkey->data;
1246
1247                         eve= em->verts.first;
1248
1249                         i = 0;
1250                         mvert = me->mvert;
1251                         while(eve) {
1252                                 if (eve->keyindex >= 0 && eve->keyindex < currkey->totelem) { // valid old vertex
1253                                         if(currkey == actkey) {
1254                                                 if (actkey == me->key->refkey) {
1255                                                         VECCOPY(fp, mvert->co);
1256                                                 }
1257                                                 else {
1258                                                         VECCOPY(fp, mvert->co);
1259                                                         if(oldverts) {
1260                                                                 VECCOPY(mvert->co, oldverts[eve->keyindex].co);
1261                                                         }
1262                                                 }
1263                                         }
1264                                         else {
1265                                                 if(oldkey) {
1266                                                         VECCOPY(fp, oldkey + 3 * eve->keyindex);
1267                                                 }
1268                                         }
1269                                 }
1270                                 else {
1271                                         VECCOPY(fp, mvert->co);
1272                                 }
1273                                 fp+= 3;
1274                                 ++i;
1275                                 ++mvert;
1276                                 eve= eve->next;
1277                         }
1278                         currkey->totelem= G.totvert;
1279                         if(currkey->data) MEM_freeN(currkey->data);
1280                         currkey->data = newkey;
1281                         
1282                         currkey= currkey->next;
1283                 }
1284         }
1285
1286         if(oldverts) MEM_freeN(oldverts);
1287         
1288         i = 0;
1289         for(ese=em->selected.first; ese; ese=ese->next) i++;
1290         me->totselect = i;
1291         if(i==0) mselect= NULL;
1292         else mselect= MEM_callocN(i*sizeof(MSelect), "loadeditMesh selections");
1293         
1294         if(me->mselect) MEM_freeN(me->mselect);
1295         me->mselect= mselect;
1296         
1297         for(ese=em->selected.first; ese; ese=ese->next){
1298                 mselect->type = ese->type;
1299                 if(ese->type == EDITVERT) mselect->index = ((EditVert*)ese->data)->tmp.l;
1300                 else if(ese->type == EDITEDGE) mselect->index = ((EditEdge*)ese->data)->tmp.l;
1301                 else if(ese->type == EDITFACE) mselect->index = ((EditFace*)ese->data)->tmp.l;
1302                 mselect++;
1303         }
1304         
1305         /* to be sure: clear ->tmp.l pointers */
1306         eve= em->verts.first;
1307         while(eve) {
1308                 eve->tmp.l = 0;
1309                 eve= eve->next;
1310         }
1311         
1312         eed= em->edges.first;
1313         while(eed) { 
1314                 eed->tmp.l = 0;
1315                 eed= eed->next;
1316         }
1317         
1318         efa= em->faces.first;
1319         while(efa) {
1320                 efa->tmp.l = 0;
1321                 efa= efa->next;
1322         }
1323         
1324         /* remake softbody of all users */
1325         if(me->id.us>1) {
1326                 Base *base;
1327                 for(base= G.scene->base.first; base; base= base->next) {
1328                         if(base->object->data==me) {                            
1329                                 if(modifiers_isClothEnabled(base->object)) {
1330                                         cloth_free_modifier(modifiers_isClothEnabled(base->object));
1331                                 }
1332                                 
1333                                 base->object->softflag |= OB_SB_REDO;
1334                                 base->object->recalc |= OB_RECALC_DATA;
1335                         }
1336                 }
1337         }
1338
1339         mesh_calc_normals(me->mvert, me->totvert, me->mface, me->totface, NULL);
1340 }
1341
1342 void remake_editMesh(void)
1343 {
1344         make_editMesh();
1345         allqueue(REDRAWVIEW3D, 0);
1346         DAG_object_flush_update(G.scene, G.obedit, OB_RECALC_DATA);
1347         BIF_undo_push("Undo all changes");
1348 }
1349
1350 /* ***************              (partial exit editmode) *************/
1351
1352
1353 void separatemenu(void)
1354 {
1355         short event;
1356
1357         if(G.editMesh->verts.first==NULL) return;
1358            
1359         event = pupmenu("Separate %t|Selected%x1|All Loose Parts%x2|By Material%x3");
1360         
1361         if (event==0) return;
1362         waitcursor(1);
1363         
1364         switch (event) {
1365         case 1: 
1366                 separate_mesh();                    
1367                 break;
1368         case 2:                     
1369                 separate_mesh_loose();              
1370                 break;
1371         case 3:
1372                 separate_material();
1373                 break;
1374         }
1375         waitcursor(0);
1376 }
1377
1378 void separate_material(void)
1379 {
1380         EditMesh *em = G.editMesh;
1381         unsigned char curr_mat;
1382         Mesh *me;
1383         
1384         if(multires_test()) return;
1385         
1386         me= get_mesh(G.obedit);
1387         if(me->key) {
1388                 error("Can't separate with vertex keys");
1389                 return;
1390         }
1391
1392         if(G.obedit && em) {
1393                 if(G.obedit->type == OB_MESH) {
1394                         for (curr_mat = 1; curr_mat < G.obedit->totcol; ++curr_mat) {
1395                                 /* clear selection, we're going to use that to select material group */
1396                                 EM_clear_flag_all(SELECT);
1397                                 /* select the material */
1398                                 editmesh_select_by_material(curr_mat);
1399                                 /* and now separate */
1400                                 separate_mesh();
1401                         }
1402                 }
1403         }
1404         
1405         countall();
1406         allqueue(REDRAWVIEW3D, 0);
1407         DAG_object_flush_update(G.scene, G.obedit, OB_RECALC_DATA);
1408
1409 }
1410
1411 void separate_mesh(void)
1412 {
1413         EditMesh *em = G.editMesh;
1414         EditMesh emcopy;
1415         EditVert *eve, *v1;
1416         EditEdge *eed, *e1;
1417         EditFace *efa, *vl1;
1418         Object *oldob;
1419         Mesh *me, *men;
1420         Base *base, *oldbase;
1421         ListBase edve, eded, edvl;
1422 #ifdef WITH_VERSE
1423         struct VNode *vnode = NULL;
1424 #endif
1425         
1426         TEST_EDITMESH
1427         if(multires_test()) return;
1428
1429         waitcursor(1);
1430         
1431         me= get_mesh(G.obedit);
1432         if(me->key) {
1433                 error("Can't separate with vertex keys");
1434                 return;
1435         }
1436         
1437         /* blender crashes in derivedmesh drawing if I don't do this... but why? 
1438                 Anyhoo, this function is horrible anyway (ton) 
1439                 the fluidsimFlag also has to be reset btw. (n_t) */
1440         if(G.obedit->fluidsimSettings) {
1441                 fluidsimSettingsFree(G.obedit->fluidsimSettings);
1442                 G.obedit->fluidsimSettings = NULL;
1443                 G.obedit->fluidsimFlag = 0;
1444         }
1445         
1446         if(em->selected.first) BLI_freelistN(&(em->selected)); /* clear the selection order */
1447                 
1448         EM_selectmode_set();    // enforce full consistant selection flags 
1449         
1450         /* we are going to abuse the system as follows:
1451          * 1. add a duplicate object: this will be the new one, we remember old pointer
1452          * 2: then do a split if needed.
1453          * 3. put apart: all NOT selected verts, edges, faces
1454          * 4. call load_editMesh(): this will be the new object
1455          * 5. freelist and get back old verts, edges, facs
1456          */
1457         
1458         /* make only obedit selected */
1459         base= FIRSTBASE;
1460         while(base) {
1461                 if(base->lay & G.vd->lay) {
1462                         if(base->object==G.obedit) base->flag |= SELECT;
1463                         else base->flag &= ~SELECT;
1464                 }
1465                 base= base->next;
1466         }
1467         
1468 #ifdef WITH_VERSE
1469         if(G.editMesh->vnode) {
1470                 vnode = G.editMesh->vnode;
1471                 G.editMesh->vnode = NULL;
1472         }
1473 #endif
1474         /* no test for split, split doesn't split when a loose part is selected */
1475         /* SPLIT: first make duplicate */
1476         adduplicateflag(SELECT);
1477
1478 #ifdef WITH_VERSE
1479         if(vnode) {
1480                 G.editMesh->vnode = vnode;
1481         }
1482 #endif
1483         /* SPLIT: old faces have 3x flag 128 set, delete these ones */
1484         delfaceflag(128);
1485         
1486         /* since we do tricky things with verts/edges/faces, this makes sure all is selected coherent */
1487         EM_selectmode_set();
1488         
1489         /* set apart: everything that is not selected */
1490         edve.first= edve.last= eded.first= eded.last= edvl.first= edvl.last= 0;
1491         eve= em->verts.first;
1492         while(eve) {
1493                 v1= eve->next;
1494                 if((eve->f & SELECT)==0) {
1495                         BLI_remlink(&em->verts, eve);
1496                         BLI_addtail(&edve, eve);
1497 #ifdef WITH_VERSE
1498                         if(eve->vvert) {
1499                                 ((VerseVert*)eve->vvert)->vertex = NULL;
1500                                 eve->vvert = NULL;
1501                         }
1502 #endif
1503                 }
1504                 
1505                 eve= v1;
1506         }
1507         eed= em->edges.first;
1508         while(eed) {
1509                 e1= eed->next;
1510                 if((eed->f & SELECT)==0) {
1511                         BLI_remlink(&em->edges, eed);
1512                         BLI_addtail(&eded, eed);
1513                 }
1514                 eed= e1;
1515         }
1516         efa= em->faces.first;
1517         while(efa) {
1518                 vl1= efa->next;
1519                 if((efa->f & SELECT)==0) {
1520                         BLI_remlink(&em->faces, efa);
1521                         BLI_addtail(&edvl, efa);
1522 #ifdef WITH_VERSE
1523                         if(efa->vface) {
1524                                 ((VerseFace*)efa->vface)->face = NULL;
1525                                 efa->vface = NULL;
1526                         }
1527 #endif
1528                 }
1529                 efa= vl1;
1530         }
1531         
1532         oldob= G.obedit;
1533         oldbase= BASACT;
1534         
1535 #ifdef WITH_VERSE
1536         if(G.obedit->vnode) {
1537                 vnode = G.obedit->vnode;
1538                 G.obedit->vnode = NULL;
1539         }
1540 #endif
1541         adduplicate(1, 0); /* notrans and a linked duplicate */
1542         
1543 #ifdef WITH_VERSE
1544         if(vnode) {
1545                 G.obedit->vnode = vnode;
1546         }
1547 #endif
1548         
1549         G.obedit= BASACT->object;       /* basact was set in adduplicate()  */
1550         
1551         men= copy_mesh(me);
1552         set_mesh(G.obedit, men);
1553         /* because new mesh is a copy: reduce user count */
1554         men->id.us--;
1555         
1556         load_editMesh();
1557         
1558         BASACT->flag &= ~SELECT;
1559         
1560         /* we cannot free the original buffer... */
1561         emcopy= *G.editMesh;
1562         emcopy.allverts= NULL;
1563         emcopy.alledges= NULL;
1564         emcopy.allfaces= NULL;
1565         emcopy.derivedFinal= emcopy.derivedCage= NULL;
1566         memset(&emcopy.vdata, 0, sizeof(emcopy.vdata));
1567         memset(&emcopy.fdata, 0, sizeof(emcopy.fdata));
1568         free_editMesh(&emcopy);
1569         
1570         em->verts= edve;
1571         em->edges= eded;
1572         em->faces= edvl;
1573         
1574         /* hashedges are freed now, make new! */
1575         editMesh_set_hash();
1576
1577         DAG_object_flush_update(G.scene, G.obedit, OB_RECALC_DATA);     
1578         G.obedit= oldob;
1579         BASACT= oldbase;
1580         BASACT->flag |= SELECT;
1581         
1582         waitcursor(0);
1583
1584         countall();
1585         allqueue(REDRAWVIEW3D, 0);
1586         DAG_object_flush_update(G.scene, G.obedit, OB_RECALC_DATA);     
1587
1588 }
1589
1590 void separate_mesh_loose(void)
1591 {
1592         EditMesh *em = G.editMesh;
1593         EditMesh emcopy;
1594         EditVert *eve, *v1;
1595         EditEdge *eed, *e1;
1596         EditFace *efa, *vl1;
1597         Object *oldob=NULL;
1598         Mesh *me, *men;
1599         Base *base, *oldbase;
1600         ListBase edve, eded, edvl;
1601         int vertsep=0;  
1602         short done=0, check=1;
1603 #ifdef WITH_VERSE
1604         struct VNode *vnode = NULL;
1605 #endif
1606                         
1607         me= get_mesh(G.obedit);
1608 #ifdef WITH_VERSE
1609         if(me->vnode) {
1610                 error("Can't separate a mesh shared at verse server");
1611                 return;
1612         }
1613 #endif
1614         if(me->key) {
1615                 error("Can't separate a mesh with vertex keys");
1616                 return;
1617         }
1618         
1619         /* same problem as in separate_mesh above (n_t) */
1620         if(G.obedit->fluidsimSettings) {
1621                 fluidsimSettingsFree(G.obedit->fluidsimSettings);
1622                 G.obedit->fluidsimSettings = NULL;
1623                 G.obedit->fluidsimFlag = 0;
1624         }
1625
1626         TEST_EDITMESH
1627         if(multires_test()) return;
1628         waitcursor(1);  
1629         
1630         /* we are going to abuse the system as follows:
1631          * 1. add a duplicate object: this will be the new one, we remember old pointer
1632          * 2: then do a split if needed.
1633          * 3. put apart: all NOT selected verts, edges, faces
1634          * 4. call load_editMesh(): this will be the new object
1635          * 5. freelist and get back old verts, edges, facs
1636          */
1637                         
1638         while(!done){           
1639                 vertsep=check=1;
1640                 
1641                 countall();
1642                 
1643                 /* make only obedit selected */
1644                 base= FIRSTBASE;
1645                 while(base) {
1646                         if(base->lay & G.vd->lay) {
1647                                 if(base->object==G.obedit) base->flag |= SELECT;
1648                                 else base->flag &= ~SELECT;
1649                         }
1650                         base= base->next;
1651                 }               
1652                 
1653                 /*--------- Select connected-----------*/               
1654                 
1655                 EM_clear_flag_all(SELECT);
1656
1657                 /* Select a random vert to start with */
1658                 eve= em->verts.first;
1659                 eve->f |= SELECT;
1660                 
1661                 while(check==1) {
1662                         check= 0;                       
1663                         eed= em->edges.first;                   
1664                         while(eed) {                            
1665                                 if(eed->h==0) {
1666                                         if(eed->v1->f & SELECT) {
1667                                                 if( (eed->v2->f & SELECT)==0 ) {
1668                                                         eed->v2->f |= SELECT;
1669                                                         vertsep++;
1670                                                         check= 1;
1671                                                 }
1672                                         }
1673                                         else if(eed->v2->f & SELECT) {
1674                                                 if( (eed->v1->f & SELECT)==0 ) {
1675                                                         eed->v1->f |= SELECT;
1676                                                         vertsep++;
1677                                                         check= SELECT;
1678                                                 }
1679                                         }
1680                                 }
1681                                 eed= eed->next;                         
1682                         }
1683                 }               
1684                 /*----------End of select connected--------*/
1685                 
1686                 
1687                 /* If the amount of vertices that is about to be split == the total amount 
1688                    of verts in the mesh, it means that there is only 1 unconnected object, so we don't have to separate
1689                 */
1690                 if(G.totvert==vertsep) done=1;                          
1691                 else{                   
1692                         /* No splitting: select connected goes fine */
1693                         
1694                         EM_select_flush();      // from verts->edges->faces
1695
1696                         /* set apart: everything that is not selected */
1697                         edve.first= edve.last= eded.first= eded.last= edvl.first= edvl.last= 0;
1698                         eve= em->verts.first;
1699                         while(eve) {
1700                                 v1= eve->next;
1701                                 if((eve->f & SELECT)==0) {
1702                                         BLI_remlink(&em->verts, eve);
1703                                         BLI_addtail(&edve, eve);
1704 #ifdef WITH_VERSE
1705                                         if(eve->vvert) {
1706                                                 b_verse_send_vertex_delete(eve);
1707                                         }
1708 #endif
1709                                 }
1710                                 eve= v1;
1711                         }
1712                         eed= em->edges.first;
1713                         while(eed) {
1714                                 e1= eed->next;
1715                                 if( (eed->f & SELECT)==0 ) {
1716                                         BLI_remlink(&em->edges, eed);
1717                                         BLI_addtail(&eded, eed);
1718                                 }
1719                                 eed= e1;
1720                         }
1721                         efa= em->faces.first;
1722                         while(efa) {
1723                                 vl1= efa->next;
1724                                 if( (efa->f & SELECT)==0 ) {
1725                                         BLI_remlink(&em->faces, efa);
1726                                         BLI_addtail(&edvl, efa);
1727 #ifdef WITH_VERSE
1728                                         if(efa->vface) {
1729                                                 b_verse_send_face_delete(efa);
1730                                         }
1731 #endif
1732                                 }
1733                                 efa= vl1;
1734                         }
1735                         
1736                         oldob= G.obedit;
1737                         oldbase= BASACT;
1738                         
1739 #ifdef WITH_VERSE
1740                         if(G.obedit->vnode) {
1741                                 vnode = G.obedit->vnode;
1742                                 G.obedit->vnode = NULL;
1743                         }
1744 #endif
1745                         adduplicate(1, 0); /* notrans and a linked duplicate*/
1746 #ifdef WITH_VERSE
1747                         if(vnode) {
1748                                 G.obedit->vnode = vnode;
1749                         }
1750 #endif
1751                         
1752                         G.obedit= BASACT->object;       /* basact was set in adduplicate()  */
1753
1754                         men= copy_mesh(me);
1755                         set_mesh(G.obedit, men);
1756                         /* because new mesh is a copy: reduce user count */
1757                         men->id.us--;
1758                         
1759                         load_editMesh();
1760                         
1761                         BASACT->flag &= ~SELECT;
1762                         
1763                         /* we cannot free the original buffer... */
1764                         emcopy= *G.editMesh;
1765                         emcopy.allverts= NULL;
1766                         emcopy.alledges= NULL;
1767                         emcopy.allfaces= NULL;
1768                         emcopy.derivedFinal= emcopy.derivedCage= NULL;
1769                         memset(&emcopy.vdata, 0, sizeof(emcopy.vdata));
1770                         memset(&emcopy.fdata, 0, sizeof(emcopy.fdata));
1771                         free_editMesh(&emcopy);
1772                         
1773                         em->verts= edve;
1774                         em->edges= eded;
1775                         em->faces= edvl;
1776                         
1777                         /* hashedges are freed now, make new! */
1778                         editMesh_set_hash();
1779                         
1780                         G.obedit= oldob;
1781                         BASACT= oldbase;
1782                         BASACT->flag |= SELECT; 
1783                                         
1784                 }               
1785         }
1786         
1787         /* unselect the vertices that we (ab)used for the separation*/
1788         EM_clear_flag_all(SELECT);
1789                 
1790         waitcursor(0);
1791         countall();
1792         allqueue(REDRAWVIEW3D, 0);
1793         DAG_object_flush_update(G.scene, G.obedit, OB_RECALC_DATA);     
1794 }
1795
1796 /* ******************************************** */
1797
1798 /* *************** UNDO ***************************** */
1799 /* new mesh undo, based on pushing editmesh data itself */
1800 /* reuses same code as for global and curve undo... unify that (ton) */
1801
1802 /* only one 'hack', to save memory it doesn't store the first push, but does a remake editmesh */
1803
1804 /* a compressed version of editmesh data */
1805
1806 typedef struct EditVertC
1807 {
1808         float no[3];
1809         float co[3];
1810         unsigned char f, h;
1811         int keyindex;
1812 } EditVertC;
1813
1814 typedef struct EditEdgeC
1815 {
1816         int v1, v2;
1817         unsigned char f, h, seam, sharp, pad;
1818         short crease, fgoni;
1819 } EditEdgeC;
1820
1821 typedef struct EditFaceC
1822 {
1823         int v1, v2, v3, v4;
1824         unsigned char mat_nr, flag, f, h, fgonf;
1825         short pad1;
1826 } EditFaceC;
1827
1828 typedef struct EditSelectionC{
1829         short type;
1830         int index;
1831 }EditSelectionC;
1832
1833 typedef struct EM_MultiresUndo {
1834         int users;
1835         Multires *mr;
1836 } EM_MultiresUndo;
1837
1838 typedef struct UndoMesh {
1839         EditVertC *verts;
1840         EditEdgeC *edges;
1841         EditFaceC *faces;
1842         EditSelectionC *selected;
1843         int totvert, totedge, totface, totsel;
1844         short selectmode;
1845         RetopoPaintData *retopo_paint_data;
1846         char retopo_mode;
1847         CustomData vdata, edata, fdata;
1848         EM_MultiresUndo *mru;
1849 } UndoMesh;
1850
1851 /* for callbacks */
1852
1853 static void free_undoMesh(void *umv)
1854 {
1855         UndoMesh *um= umv;
1856         
1857         if(um->verts) MEM_freeN(um->verts);
1858         if(um->edges) MEM_freeN(um->edges);
1859         if(um->faces) MEM_freeN(um->faces);
1860         if(um->selected) MEM_freeN(um->selected);
1861         if(um->retopo_paint_data) retopo_free_paint_data(um->retopo_paint_data);
1862         CustomData_free(&um->vdata, um->totvert);
1863         CustomData_free(&um->edata, um->totedge);
1864         CustomData_free(&um->fdata, um->totface);
1865         if(um->mru) {
1866                 --um->mru->users;
1867                 if(um->mru->users==0) {
1868                         multires_free(um->mru->mr);
1869                         um->mru->mr= NULL;
1870                         MEM_freeN(um->mru);
1871                 }
1872         }
1873         MEM_freeN(um);
1874 }
1875
1876 static void *editMesh_to_undoMesh(void)
1877 {
1878         EditMesh *em= G.editMesh;
1879         UndoMesh *um;
1880         EditVert *eve;
1881         EditEdge *eed;
1882         EditFace *efa;
1883         EditSelection *ese;
1884         EditVertC *evec=NULL;
1885         EditEdgeC *eedc=NULL;
1886         EditFaceC *efac=NULL;
1887         EditSelectionC *esec=NULL;
1888         int a;
1889         
1890         um= MEM_callocN(sizeof(UndoMesh), "undomesh");
1891         
1892         um->selectmode = G.scene->selectmode;
1893         
1894         for(eve=em->verts.first; eve; eve= eve->next) um->totvert++;
1895         for(eed=em->edges.first; eed; eed= eed->next) um->totedge++;
1896         for(efa=em->faces.first; efa; efa= efa->next) um->totface++;
1897         for(ese=em->selected.first; ese; ese=ese->next) um->totsel++; 
1898         /* malloc blocks */
1899         
1900         if(um->totvert) evec= um->verts= MEM_callocN(um->totvert*sizeof(EditVertC), "allvertsC");
1901         if(um->totedge) eedc= um->edges= MEM_callocN(um->totedge*sizeof(EditEdgeC), "alledgesC");
1902         if(um->totface) efac= um->faces= MEM_callocN(um->totface*sizeof(EditFaceC), "allfacesC");
1903         if(um->totsel) esec= um->selected= MEM_callocN(um->totsel*sizeof(EditSelectionC), "allselections");
1904
1905         if(um->totvert) CustomData_copy(&em->vdata, &um->vdata, CD_MASK_EDITMESH, CD_CALLOC, um->totvert);
1906         if(um->totedge) CustomData_copy(&em->edata, &um->edata, CD_MASK_EDITMESH, CD_CALLOC, um->totedge);
1907         if(um->totface) CustomData_copy(&em->fdata, &um->fdata, CD_MASK_EDITMESH, CD_CALLOC, um->totface);
1908         
1909         /* now copy vertices */
1910         a = 0;
1911         for(eve=em->verts.first; eve; eve= eve->next, evec++, a++) {
1912                 VECCOPY(evec->co, eve->co);
1913                 VECCOPY(evec->no, eve->no);
1914
1915                 evec->f= eve->f;
1916                 evec->h= eve->h;
1917                 evec->keyindex= eve->keyindex;
1918                 eve->tmp.l = a; /*store index*/
1919
1920                 CustomData_from_em_block(&em->vdata, &um->vdata, eve->data, a);
1921         }
1922         
1923         /* copy edges */
1924         a = 0;
1925         for(eed=em->edges.first; eed; eed= eed->next, eedc++, a++)  {
1926                 eedc->v1= (int)eed->v1->tmp.l;
1927                 eedc->v2= (int)eed->v2->tmp.l;
1928                 eedc->f= eed->f;
1929                 eedc->h= eed->h;
1930                 eedc->seam= eed->seam;
1931                 eedc->sharp= eed->sharp;
1932                 eedc->crease= (short)(eed->crease*255.0);
1933                 eedc->fgoni= eed->fgoni;
1934                 eed->tmp.l = a; /*store index*/
1935                 CustomData_from_em_block(&em->edata, &um->edata, eed->data, a);
1936         
1937         }
1938         
1939         /* copy faces */
1940         a = 0;
1941         for(efa=em->faces.first; efa; efa= efa->next, efac++, a++) {
1942                 efac->v1= (int)efa->v1->tmp.l;
1943                 efac->v2= (int)efa->v2->tmp.l;
1944                 efac->v3= (int)efa->v3->tmp.l;
1945                 if(efa->v4) efac->v4= (int)efa->v4->tmp.l;
1946                 else efac->v4= -1;
1947                 
1948                 efac->mat_nr= efa->mat_nr;
1949                 efac->flag= efa->flag;
1950                 efac->f= efa->f;
1951                 efac->h= efa->h;
1952                 efac->fgonf= efa->fgonf;
1953
1954                 efa->tmp.l = a; /*store index*/
1955
1956                 CustomData_from_em_block(&em->fdata, &um->fdata, efa->data, a);
1957         }
1958         
1959         a = 0;
1960         for(ese=em->selected.first; ese; ese=ese->next, esec++){
1961                 esec->type = ese->type;
1962                 if(ese->type == EDITVERT) a = esec->index = ((EditVert*)ese->data)->tmp.l; 
1963                 else if(ese->type == EDITEDGE) a = esec->index = ((EditEdge*)ese->data)->tmp.l; 
1964                 else if(ese->type == EDITFACE) a = esec->index = ((EditFace*)ese->data)->tmp.l;
1965         }
1966
1967         um->retopo_paint_data= retopo_paint_data_copy(em->retopo_paint_data);
1968         um->retopo_mode= G.scene->toolsettings->retopo_mode;
1969         
1970         {
1971                 Multires *mr= get_mesh(G.obedit)->mr;
1972                 UndoMesh *prev= undo_editmode_get_prev(G.obedit);
1973                 
1974                 um->mru= NULL;
1975                 
1976                 if(mr) {
1977                         if(prev && prev->mru && prev->mru->mr && prev->mru->mr->current == mr->current) {
1978                                 um->mru= prev->mru;
1979                                 ++um->mru->users;
1980                         }
1981                         else {
1982                                 um->mru= MEM_callocN(sizeof(EM_MultiresUndo), "EM_MultiresUndo");
1983                                 um->mru->users= 1;
1984                                 um->mru->mr= multires_copy(mr);
1985                         }
1986                 }
1987         }
1988         
1989         return um;
1990 }
1991
1992 static void undoMesh_to_editMesh(void *umv)
1993 {
1994         UndoMesh *um= (UndoMesh*)umv;
1995         EditMesh *em= G.editMesh;
1996         EditVert *eve, **evar=NULL;
1997         EditEdge *eed;
1998         EditFace *efa;
1999         EditSelection *ese;
2000         EditVertC *evec;
2001         EditEdgeC *eedc;
2002         EditFaceC *efac;
2003         EditSelectionC *esec;
2004         int a=0;
2005
2006 #ifdef WITH_VERSE
2007         struct VNode *vnode = G.editMesh->vnode;
2008         if(vnode) {
2009                 /* send delete command to all verse vertexes and verse face ...
2010                  * verse mesh will be recreated from new edit mesh */
2011                 destroy_versemesh(vnode);
2012         }
2013 #endif  
2014         G.scene->selectmode = um->selectmode;
2015         
2016         free_editMesh(G.editMesh);
2017         
2018         /* malloc blocks */
2019         memset(em, 0, sizeof(EditMesh));
2020                 
2021         init_editmesh_fastmalloc(em, um->totvert, um->totedge, um->totface);
2022
2023 #ifdef WITH_VERSE
2024         G.editMesh->vnode = vnode;
2025 #endif
2026
2027         CustomData_free(&em->vdata, 0);
2028         CustomData_free(&em->edata, 0);
2029         CustomData_free(&em->fdata, 0);
2030
2031         CustomData_copy(&um->vdata, &em->vdata, CD_MASK_EDITMESH, CD_CALLOC, 0);
2032         CustomData_copy(&um->edata, &em->edata, CD_MASK_EDITMESH, CD_CALLOC, 0);
2033         CustomData_copy(&um->fdata, &em->fdata, CD_MASK_EDITMESH, CD_CALLOC, 0);
2034
2035         /* now copy vertices */
2036
2037         if(um->totvert) evar= MEM_mallocN(um->totvert*sizeof(EditVert *), "vertex ar");
2038         for(a=0, evec= um->verts; a<um->totvert; a++, evec++) {
2039                 eve= addvertlist(evec->co, NULL);
2040                 evar[a]= eve;
2041
2042                 VECCOPY(eve->no, evec->no);
2043                 eve->f= evec->f;
2044                 eve->h= evec->h;
2045                 eve->keyindex= evec->keyindex;
2046
2047                 CustomData_to_em_block(&um->vdata, &em->vdata, a, &eve->data);
2048         }
2049
2050         /* copy edges */
2051         for(a=0, eedc= um->edges; a<um->totedge; a++, eedc++) {
2052                 eed= addedgelist(evar[eedc->v1], evar[eedc->v2], NULL);
2053
2054                 eed->f= eedc->f;
2055                 eed->h= eedc->h;
2056                 eed->seam= eedc->seam;
2057                 eed->sharp= eedc->sharp;
2058                 eed->fgoni= eedc->fgoni;
2059                 eed->crease= ((float)eedc->crease)/255.0;
2060                 CustomData_to_em_block(&um->edata, &em->edata, a, &eed->data);
2061         }
2062         
2063         /* copy faces */
2064         for(a=0, efac= um->faces; a<um->totface; a++, efac++) {
2065                 if(efac->v4 != -1)
2066                         efa= addfacelist(evar[efac->v1], evar[efac->v2], evar[efac->v3], evar[efac->v4], NULL, NULL);
2067                 else 
2068                         efa= addfacelist(evar[efac->v1], evar[efac->v2], evar[efac->v3], NULL, NULL ,NULL);
2069
2070                 efa->mat_nr= efac->mat_nr;
2071                 efa->flag= efac->flag;
2072                 efa->f= efac->f;
2073                 efa->h= efac->h;
2074                 efa->fgonf= efac->fgonf;
2075                 
2076                 CustomData_to_em_block(&um->fdata, &em->fdata, a, &efa->data);
2077         }
2078         
2079         end_editmesh_fastmalloc();
2080         if(evar) MEM_freeN(evar);
2081         
2082         G.totvert = um->totvert;
2083         G.totedge = um->totedge;
2084         G.totface = um->totface;
2085         /*restore stored editselections*/
2086         if(um->totsel){
2087                 EM_init_index_arrays(1,1,1);
2088                 for(a=0, esec= um->selected; a<um->totsel; a++, esec++){
2089                         ese = MEM_callocN(sizeof(EditSelection), "Edit Selection");
2090                         ese->type = esec->type;
2091                         if(ese->type == EDITVERT) ese->data = EM_get_vert_for_index(esec->index); else
2092                         if(ese->type == EDITEDGE) ese->data = EM_get_edge_for_index(esec->index); else
2093                         if(ese->type == EDITFACE) ese->data = EM_get_face_for_index(esec->index);
2094                         BLI_addtail(&(em->selected),ese);
2095                 }
2096                 EM_free_index_arrays();
2097         }
2098
2099         retopo_free_paint();
2100         em->retopo_paint_data= retopo_paint_data_copy(um->retopo_paint_data);
2101         G.scene->toolsettings->retopo_mode= um->retopo_mode;
2102         if(G.scene->toolsettings->retopo_mode) {
2103                 if(G.vd->depths) G.vd->depths->damaged= 1;
2104                 retopo_queue_updates(G.vd);
2105                 retopo_paint_view_update(G.vd);
2106         }
2107         
2108         {
2109                 Mesh *me= get_mesh(G.obedit);
2110                 multires_free(me->mr);
2111                 me->mr= NULL;
2112                 if(um->mru && um->mru->mr) me->mr= multires_copy(um->mru->mr);
2113         }
2114 }
2115
2116
2117 /* and this is all the undo system needs to know */
2118 void undo_push_mesh(char *name)
2119 {
2120         undo_editmode_push(name, free_undoMesh, undoMesh_to_editMesh, editMesh_to_undoMesh);
2121 }
2122
2123
2124
2125 /* *************** END UNDO *************/
2126
2127 static EditVert **g_em_vert_array = NULL;
2128 static EditEdge **g_em_edge_array = NULL;
2129 static EditFace **g_em_face_array = NULL;
2130
2131 void EM_init_index_arrays(int forVert, int forEdge, int forFace)
2132 {
2133         EditVert *eve;
2134         EditEdge *eed;
2135         EditFace *efa;
2136         int i;
2137
2138         if (forVert) {
2139                 g_em_vert_array = MEM_mallocN(sizeof(*g_em_vert_array)*G.totvert, "em_v_arr");
2140
2141                 for (i=0,eve=G.editMesh->verts.first; eve; i++,eve=eve->next)
2142                         g_em_vert_array[i] = eve;
2143         }
2144
2145         if (forEdge) {
2146                 g_em_edge_array = MEM_mallocN(sizeof(*g_em_edge_array)*G.totedge, "em_e_arr");
2147
2148                 for (i=0,eed=G.editMesh->edges.first; eed; i++,eed=eed->next)
2149                         g_em_edge_array[i] = eed;
2150         }
2151
2152         if (forFace) {
2153                 g_em_face_array = MEM_mallocN(sizeof(*g_em_face_array)*G.totface, "em_f_arr");
2154
2155                 for (i=0,efa=G.editMesh->faces.first; efa; i++,efa=efa->next)
2156                         g_em_face_array[i] = efa;
2157         }
2158 }
2159
2160 void EM_free_index_arrays(void)
2161 {
2162         if (g_em_vert_array) MEM_freeN(g_em_vert_array);
2163         if (g_em_edge_array) MEM_freeN(g_em_edge_array);
2164         if (g_em_face_array) MEM_freeN(g_em_face_array);
2165         g_em_vert_array = NULL;
2166         g_em_edge_array = NULL;
2167         g_em_face_array = NULL;
2168 }
2169
2170 EditVert *EM_get_vert_for_index(int index)
2171 {
2172         return g_em_vert_array?g_em_vert_array[index]:NULL;
2173 }
2174
2175 EditEdge *EM_get_edge_for_index(int index)
2176 {
2177         return g_em_edge_array?g_em_edge_array[index]:NULL;
2178 }
2179
2180 EditFace *EM_get_face_for_index(int index)
2181 {
2182         return g_em_face_array?g_em_face_array[index]:NULL;
2183 }
2184
2185 /* can we edit UV's for this mesh?*/
2186 int EM_texFaceCheck(void)
2187 {
2188         /* some of these checks could be a touch overkill */
2189         if (    (G.obedit) &&
2190                         (G.obedit->type == OB_MESH) &&
2191                         (G.editMesh) &&
2192                         (G.editMesh->faces.first) &&
2193                         (CustomData_has_layer(&G.editMesh->fdata, CD_MTFACE)))
2194                 return 1;
2195         return 0;
2196 }
2197
2198 /* can we edit colors for this mesh?*/
2199 int EM_vertColorCheck(void)
2200 {
2201         /* some of these checks could be a touch overkill */
2202         if (    (G.obedit) &&
2203                         (G.obedit->type == OB_MESH) &&
2204                         (G.editMesh) &&
2205                         (G.editMesh->faces.first) &&
2206                         (CustomData_has_layer(&G.editMesh->fdata, CD_MCOL)))
2207                 return 1;
2208         return 0;
2209 }
2210