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