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