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