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