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