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