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