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