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