svn merge -r39877:39878 https://svn.blender.org/svnroot/bf-blender/trunk/blender...
[blender-staging.git] / source / blender / bmesh / operators / mesh_conv.c
1 #include <string.h>
2
3 #include "MEM_guardedalloc.h"
4
5 #include "DNA_listBase.h"
6 #include "DNA_customdata_types.h"
7 #include "DNA_mesh_types.h"
8 #include "DNA_meshdata_types.h"
9 #include "DNA_modifier_types.h"
10 #include "DNA_key_types.h"
11 #include "DNA_object_types.h"
12 #include "DNA_scene_types.h"
13
14 #include "BKE_customdata.h" 
15 #include "BKE_mesh.h"
16 #include "BKE_global.h"
17 #include "BKE_DerivedMesh.h"
18 #include "BKE_cdderivedmesh.h"
19 #include "BKE_key.h"
20 #include "BKE_main.h"
21
22 #include "BLI_utildefines.h"
23 #include "BLI_math.h"
24 #include "BLI_blenlib.h"
25 #include "BLI_edgehash.h"
26 #include "BLI_editVert.h"
27 #include "BLI_scanfill.h"
28 #include "BLI_array.h"
29 #include "BLI_utildefines.h"
30
31 #include "ED_mesh.h"
32
33 #include "mesh_intern.h"
34 #include "bmesh.h"
35 #include "bmesh_private.h"
36
37 /*
38  * MESH CONV.C
39  *
40  * This file contains functions
41  * for converting a Mesh
42  * into a Bmesh, and back again.
43  *
44 */
45
46 void mesh_to_bmesh_exec(BMesh *bm, BMOperator *op) {
47         Object *ob = BMO_Get_Pnt(op, "object");
48         Mesh *me = BMO_Get_Pnt(op, "mesh");
49         MVert *mvert;
50         BLI_array_declare(verts);
51         MEdge *medge;
52         MLoop *ml;
53         MPoly *mpoly;
54         KeyBlock *actkey, *block;
55         BMVert *v, **vt=NULL, **verts = NULL;
56         BMEdge *e, **fedges=NULL, **et = NULL;
57         BMFace *f;
58         BLI_array_declare(fedges);
59         float (*keyco)[3]= NULL;
60         int *keyi;
61         int set_key = BMO_Get_Int(op, "set_shapekey");
62         int totuv, i, j, li, allocsize[4] = {512, 512, 2048, 512};
63
64         if (!me || !me->totvert) return; /*sanity check*/
65         
66         vt = MEM_mallocN(sizeof(void**)*me->totvert, "mesh to bmesh vtable");
67
68         CustomData_copy(&me->vdata, &bm->vdata, CD_MASK_BMESH, CD_CALLOC, 0);
69         CustomData_copy(&me->edata, &bm->edata, CD_MASK_BMESH, CD_CALLOC, 0);
70         CustomData_copy(&me->ldata, &bm->ldata, CD_MASK_BMESH, CD_CALLOC, 0);
71         CustomData_copy(&me->pdata, &bm->pdata, CD_MASK_BMESH, CD_CALLOC, 0);
72         
73         /*make sure uv layer names are consistent*/
74         totuv = CustomData_number_of_layers(&bm->pdata, CD_MTEXPOLY);
75         for (i=0; i<totuv; i++) {
76                 int li = CustomData_get_layer_index_n(&bm->pdata, CD_MTEXPOLY, i);
77                 CustomData_set_layer_name(&bm->ldata, CD_MLOOPUV, i, bm->pdata.layers[li].name);
78         }
79         
80         if (!CustomData_has_layer(&bm->edata, CD_CREASE))
81                 CustomData_add_layer(&bm->edata, CD_CREASE, CD_ASSIGN, NULL, 0);
82
83         if (!CustomData_has_layer(&bm->edata, CD_BWEIGHT))
84                 CustomData_add_layer(&bm->edata, CD_BWEIGHT, CD_ASSIGN, NULL, 0);
85
86         if (!CustomData_has_layer(&bm->vdata, CD_BWEIGHT))
87                 CustomData_add_layer(&bm->vdata, CD_BWEIGHT, CD_ASSIGN, NULL, 0);
88
89
90         if (me->key && ob->shapenr > me->key->totkey) {
91                 ob->shapenr = me->key->totkey-1;
92         }
93
94         actkey = ob_get_keyblock(ob);
95         if(actkey && actkey->totelem == me->totvert) {
96                 CustomData_add_layer(&bm->vdata, CD_SHAPE_KEYINDEX, CD_ASSIGN, NULL, 0);
97                 
98                 /*check if we need to generate unique ids for the shapekeys.
99                   this also exists in the file reading code, but is here for
100                   a sanity check*/
101                 if (!me->key->uidgen) {
102                         printf("yeek! had to generate shape key uid's in a situation we shouldn't need to!\n");
103                         me->key->uidgen = 1;
104                         for (block=me->key->block.first; block; block=block->next) {
105                                 block->uid = me->key->uidgen++;
106                         }
107                 }
108
109                 keyco= actkey->data;
110                 bm->shapenr= ob->shapenr;
111                 for (i=0, block=me->key->block.first; block; block=block->next, i++) {
112                         CustomData_add_layer_named(&bm->vdata, CD_SHAPEKEY, 
113                                          CD_ASSIGN, NULL, 0, block->name);
114                         
115                         j = CustomData_get_layer_index_n(&bm->vdata, CD_SHAPEKEY, i);
116                         bm->vdata.layers[j].uid = block->uid;
117                 }
118         } else if (actkey) {
119                 printf("shapekey<->mesh mismatch!\n");
120         }
121         
122         CustomData_bmesh_init_pool(&bm->vdata, allocsize[0]);
123         CustomData_bmesh_init_pool(&bm->edata, allocsize[1]);
124         CustomData_bmesh_init_pool(&bm->ldata, allocsize[2]);
125         CustomData_bmesh_init_pool(&bm->pdata, allocsize[3]);
126
127         for (i=0, mvert = me->mvert; i<me->totvert; i++, mvert++) {
128                 v = BM_Make_Vert(bm, keyco&&set_key ? keyco[i] : mvert->co, NULL);
129                 normal_short_to_float_v3(v->no, mvert->no);
130
131                 vt[i] = v;
132                 BM_SetIndex(v, i);
133
134                 /*this is necassary for selection counts to work properly*/
135                 if(v->head.flag & BM_SELECT) BM_Select_Vert(bm, v, 1);
136
137                 /*transfer flags*/
138                 v->head.flag = MEFlags_To_BMFlags(mvert->flag, BM_VERT);
139                 BM_SetCDf(&bm->vdata, v, CD_BWEIGHT, (float)mvert->bweight / 255.0f);
140
141                 /*Copy Custom Data*/
142                 CustomData_to_bmesh_block(&me->vdata, &bm->vdata, i, &v->head.data);
143
144                 /*set shapekey data*/
145                 if (me->key) {
146                         /*set shape key original index*/
147                         keyi = CustomData_bmesh_get(&bm->vdata, v->head.data, CD_SHAPE_KEYINDEX);
148                         if (keyi) {
149                                 *keyi = i;
150                         }
151                         
152                         for (block=me->key->block.first, j=0; block; block=block->next, j++) {
153                                 float *co = CustomData_bmesh_get_n(&bm->vdata, v->head.data, 
154                                                                    CD_SHAPEKEY, j);
155                                 if (co)
156                                         VECCOPY(co, ((float*)block->data)+3*i);
157                         }
158                 }
159         }
160
161         if (!me->totedge) {
162                 MEM_freeN(vt);
163                 return;
164         }
165
166         et = MEM_mallocN(sizeof(void**)*me->totedge, "mesh to bmesh etable");
167
168         medge = me->medge;
169         for (i=0; i<me->totedge; i++, medge++) {
170                 e = BM_Make_Edge(bm, vt[medge->v1], vt[medge->v2], NULL, 0);
171                 et[i] = e;
172                 
173                 /*Copy Custom Data*/
174                 CustomData_to_bmesh_block(&me->edata, &bm->edata, i, &e->head.data);
175                 
176                 BM_SetCDf(&bm->edata, e, CD_CREASE, (float)medge->crease / 255.0f);
177                 BM_SetCDf(&bm->edata, e, CD_BWEIGHT, (float)medge->bweight / 255.0f);
178
179                 /*this is necassary for selection counts to work properly*/
180                 if (e->head.flag & BM_SELECT) BM_Select(bm, e, 1);
181
182                 /*transfer flags*/
183                 e->head.flag = MEFlags_To_BMFlags(medge->flag, BM_EDGE);
184         }
185         
186         if (!me->totpoly) {
187                 MEM_freeN(vt);
188                 MEM_freeN(et);
189                 return;
190         }
191
192         mpoly = me->mpoly;
193         li = 0;
194         for (i=0; i<me->totpoly; i++, mpoly++) {
195                 BMVert *v1, *v2;
196                 BMIter iter;
197                 BMLoop *l;
198
199                 BLI_array_empty(fedges);
200                 BLI_array_empty(verts);
201                 for (j=0; j<mpoly->totloop; j++) {
202                         ml = &me->mloop[mpoly->loopstart+j];
203                         v = vt[ml->v];
204                         e = et[ml->e];
205
206                         BLI_array_growone(fedges);
207                         BLI_array_growone(verts);
208
209                         fedges[j] = e;
210                         verts[j] = v;
211                 }
212                 
213                 v1 = vt[me->mloop[mpoly->loopstart].v];
214                 v2 = vt[me->mloop[mpoly->loopstart+1].v];
215
216                 if (v1 == fedges[0]->v1) v2 = fedges[0]->v2;
217                 else {
218                         v1 = fedges[0]->v2;
219                         v2 = fedges[0]->v1;
220                 }
221         
222                 f = BM_Make_Face(bm, verts, fedges, mpoly->totloop);
223
224                 if (!f) {
225                         printf("Warning! Bad face in mesh"
226                                " \"%s\" at index %d!\n", me->id.name+2, i);
227                         continue;
228                 }
229
230                 /*this is necassary for selection counts to work properly*/
231                 if (f->head.flag & BM_SELECT) BM_Select(bm, f, 1);
232
233                 /*transfer flags*/
234                 f->head.flag = MEFlags_To_BMFlags(mpoly->flag, BM_FACE);
235
236                 f->mat_nr = mpoly->mat_nr;
237                 if (i == me->act_face) bm->act_face = f;
238
239                 /*Copy over loop customdata*/
240                 j = 0;
241                 BM_ITER(l, &iter, bm, BM_LOOPS_OF_FACE, f) {
242                         CustomData_to_bmesh_block(&me->ldata, &bm->ldata, mpoly->loopstart+j, &l->head.data);
243                         li++;
244                         j++;
245                 }
246
247                 /*Copy Custom Data*/
248                 CustomData_to_bmesh_block(&me->pdata, &bm->pdata, i, &f->head.data);
249         }
250
251         {
252                 BMIter iter;
253                 BMVert *vertex;
254                 BMEdge *edge;
255                 BMFace *face;
256                 BMVert **vertex_array = MEM_callocN(sizeof(BMVert *) * bm->totvert,
257                                                   "Selection Conversion Vertex Pointer Array");
258                 BMEdge **edge_array = MEM_callocN(sizeof(BMEdge *) * bm->totedge,
259                                                 "Selection Conversion Edge Pointer Array");
260                 BMFace **face_array = MEM_callocN(sizeof(BMFace *) * bm->totface,
261                                                 "Selection Conversion Face Pointer Array");
262
263                 for(i = 0, vertex = BMIter_New(&iter, bm, BM_VERTS_OF_MESH, NULL);
264                     vertex; i++, vertex = BMIter_Step(&iter)){
265                         vertex_array[i] = vertex;
266                 }
267
268                 for(i = 0, edge = BMIter_New(&iter, bm, BM_EDGES_OF_MESH, NULL);
269                     edge; i++, edge = BMIter_Step(&iter)){
270                         edge_array[i] = edge;
271                 }
272
273                 for(i = 0, face = BMIter_New(&iter, bm, BM_FACES_OF_MESH, NULL);
274                     face; i++, face = BMIter_Step(&iter)){
275                         face_array[i] = face;
276                 }
277
278                 if(me->mselect) {
279                         for(i = 0; i < me->totselect; i++){
280                                 if(me->mselect[i].type == ME_VSEL){
281                                         BM_store_selection(bm, vertex_array[me->mselect[i].index]);
282                                 }else if(me->mselect[i].type == ME_ESEL){
283                                         BM_store_selection(bm, edge_array[me->mselect[i].index]);
284                                 }else if(me->mselect[i].type == ME_FSEL){
285                                         BM_store_selection(bm, face_array[me->mselect[i].index]);
286                                 }
287                         }
288                 }
289                 else {
290                         me->totselect= 0;
291                 }
292
293                 MEM_freeN(vertex_array);
294                 MEM_freeN(edge_array);
295                 MEM_freeN(face_array);
296         }
297
298         BLI_array_free(fedges);
299         BLI_array_free(verts);
300         
301         MEM_freeN(vt);
302         MEM_freeN(et);
303 }
304
305
306 static void loops_to_corners(BMesh *bm, Mesh *me, int findex,
307                              BMFace *f, BMLoop *ls[3], int numTex, int numCol) 
308 {
309         BMLoop *l;
310         MTFace *texface;
311         MTexPoly *texpoly;
312         MCol *mcol;
313         MLoopCol *mloopcol;
314         MLoopUV *mloopuv;
315         int i, j;
316
317         for(i=0; i < numTex; i++){
318                 texface = CustomData_get_n(&me->fdata, CD_MTFACE, findex, i);
319                 texpoly = CustomData_bmesh_get_n(&bm->pdata, f->head.data, CD_MTEXPOLY, i);
320                 
321                 texface->tpage = texpoly->tpage;
322                 texface->flag = texpoly->flag;
323                 texface->transp = texpoly->transp;
324                 texface->mode = texpoly->mode;
325                 texface->tile = texpoly->tile;
326                 texface->unwrap = texpoly->unwrap;
327
328                 for (j=0; j<3; j++) {
329                         l = ls[j];
330                         mloopuv = CustomData_bmesh_get_n(&bm->ldata, l->head.data, CD_MLOOPUV, i);
331                         texface->uv[j][0] = mloopuv->uv[0];
332                         texface->uv[j][1] = mloopuv->uv[1];
333                 }
334         }
335
336         for(i=0; i < numCol; i++){
337                 mcol = CustomData_get_n(&me->fdata, CD_MCOL, findex, i);
338
339                 for (j=0; j<3; j++) {
340                         l = ls[j];
341                         mloopcol = CustomData_bmesh_get_n(&bm->ldata, l->head.data, CD_MLOOPCOL, i);
342                         mcol[j].r = mloopcol->r;
343                         mcol[j].g = mloopcol->g;
344                         mcol[j].b = mloopcol->b;
345                         mcol[j].a = mloopcol->a;
346                 }
347         }
348 }
349
350 void object_load_bmesh_exec(BMesh *bm, BMOperator *op) {
351         Object *ob = BMO_Get_Pnt(op, "object");
352         /* Scene *scene = BMO_Get_Pnt(op, "scene"); */
353         Mesh *me = ob->data;
354
355         BMO_CallOpf(bm, "bmesh_to_mesh mesh=%p object=%p", me, ob);
356 }
357
358 void bmesh_to_mesh_exec(BMesh *bm, BMOperator *op) {
359         Mesh *me = BMO_Get_Pnt(op, "mesh");
360         /* Object *ob = BMO_Get_Pnt(op, "object"); */
361         MLoop *mloop;
362         KeyBlock *block;
363         MPoly *mpoly;
364         MVert *mvert, *oldverts;
365         MEdge *medge;
366         MFace *mface;
367         BMVert *v, *eve;
368         BMEdge *e;
369         BMLoop *l;
370         BMFace *f;
371         BMIter iter, liter;
372         float *facenors = NULL;
373         int i, j, *keyi, ototvert, totloop, totface, numTex, numCol;
374         int dotess = !BMO_Get_Int(op, "notesselation");
375
376         numTex = CustomData_number_of_layers(&bm->pdata, CD_MTEXPOLY);
377         numCol = CustomData_number_of_layers(&bm->ldata, CD_MLOOPCOL);
378         
379         ototvert = me->totvert;
380
381         /* new Vertex block */
382         if(bm->totvert==0) mvert= NULL;
383         else mvert= MEM_callocN(bm->totvert*sizeof(MVert), "loadeditbMesh vert");
384
385         /* new Edge block */
386         if(bm->totedge==0) medge= NULL;
387         else medge= MEM_callocN(bm->totedge*sizeof(MEdge), "loadeditbMesh edge");
388         
389         /*build ngon data*/
390         /* new Ngon Face block */
391         if(bm->totface==0) mpoly = NULL;
392         else mpoly= MEM_callocN(bm->totface*sizeof(MPoly), "loadeditbMesh poly");
393         
394         /*find number of loops to allocate*/
395         totloop = 0;
396         BM_ITER(f, &iter, bm, BM_FACES_OF_MESH, NULL) {
397                 totloop += f->len;
398         }
399
400         if (totloop==0) mloop = NULL;
401         else mloop = MEM_callocN(totloop*sizeof(MLoop), "loadeditbMesh loop");
402
403         /* lets save the old verts just in case we are actually working on
404          * a key ... we now do processing of the keys at the end */
405         oldverts= me->mvert;
406
407         /* don't free this yet */
408         CustomData_set_layer(&me->vdata, CD_MVERT, NULL);
409
410         /* free custom data */
411         CustomData_free(&me->vdata, me->totvert);
412         CustomData_free(&me->edata, me->totedge);
413         CustomData_free(&me->fdata, me->totface);
414         CustomData_free(&me->ldata, me->totloop);
415         CustomData_free(&me->pdata, me->totpoly);
416
417         /* add new custom data */
418         me->totvert= bm->totvert;
419         me->totedge= bm->totedge;
420         me->totloop= totloop;
421         me->totpoly= bm->totface;
422
423         CustomData_copy(&bm->vdata, &me->vdata, CD_MASK_MESH, CD_CALLOC, me->totvert);
424         CustomData_copy(&bm->edata, &me->edata, CD_MASK_MESH, CD_CALLOC, me->totedge);
425         CustomData_copy(&bm->ldata, &me->ldata, CD_MASK_MESH, CD_CALLOC, me->totloop);
426         CustomData_copy(&bm->pdata, &me->pdata, CD_MASK_MESH, CD_CALLOC, me->totpoly);
427
428         CustomData_add_layer(&me->vdata, CD_MVERT, CD_ASSIGN, mvert, me->totvert);
429         CustomData_add_layer(&me->edata, CD_MEDGE, CD_ASSIGN, medge, me->totedge);
430         CustomData_add_layer(&me->ldata, CD_MLOOP, CD_ASSIGN, mloop, me->totloop);
431         CustomData_add_layer(&me->pdata, CD_MPOLY, CD_ASSIGN, mpoly, me->totpoly);
432         
433         i = 0;
434         BM_ITER(v, &iter, bm, BM_VERTS_OF_MESH, NULL) {
435                 float *bweight = CustomData_bmesh_get(&bm->vdata, v->head.data, CD_BWEIGHT);
436
437                 mvert->bweight = bweight ? (char)((*bweight)*255) : 0;
438
439                 VECCOPY(mvert->co, v->co);
440
441                 mvert->no[0] = (short) (v->no[0]*32767.0f);
442                 mvert->no[1] = (short) (v->no[1]*32767.0f);
443                 mvert->no[2] = (short) (v->no[2]*32767.0f);
444                 
445                 mvert->flag = BMFlags_To_MEFlags(v);
446
447                 BM_SetIndex(v, i);
448
449                 /*copy over customdata*/
450                 CustomData_from_bmesh_block(&bm->vdata, &me->vdata, v->head.data, i);
451
452                 i++;
453                 mvert++;
454
455                 CHECK_ELEMENT(bm, v);
456         }
457
458         i = 0;
459         BM_ITER(e, &iter, bm, BM_EDGES_OF_MESH, NULL) {
460                 float *crease = CustomData_bmesh_get(&bm->edata, e->head.data, CD_CREASE);
461                 float *bweight = CustomData_bmesh_get(&bm->edata, e->head.data, CD_BWEIGHT);
462                 
463                 medge->v1 = BM_GetIndex(e->v1);
464                 medge->v2 = BM_GetIndex(e->v2);
465                 medge->crease = crease ? (char)((*crease)*255) : 0;
466                 medge->bweight = bweight ? (char)((*bweight)*255) : 0;
467                 
468                 medge->flag = BMFlags_To_MEFlags(e);
469                 
470                 BM_SetIndex(e, i);
471
472                 /*copy over customdata*/
473                 CustomData_from_bmesh_block(&bm->edata, &me->edata, e->head.data, i);
474
475                 i++;
476                 medge++;
477                 CHECK_ELEMENT(bm, e);
478         }
479
480         /*new scanfill tesselation code*/
481         if (dotess) {
482                 /*first counter number of faces we'll need*/
483                 totface = 0;
484                 BM_ITER(f, &iter, bm, BM_FACES_OF_MESH, NULL) {
485                         EditVert *eve, *lasteve = NULL, *firsteve = NULL;
486                         
487                         BLI_begin_edgefill();
488                         i = 0;
489                         BM_ITER(l, &liter, bm, BM_LOOPS_OF_FACE, f) {
490                                 eve = BLI_addfillvert(l->v->co);
491                                 eve->tmp.p = l;
492                                 
493                                 BM_SetIndex(l, i);
494
495                                 if (lasteve) {
496                                         BLI_addfilledge(lasteve, eve);
497                                 }
498
499                                 lasteve = eve;
500                                 if (!firsteve) firsteve = eve;
501
502                                 i++;
503                         }
504
505                         BLI_addfilledge(lasteve, firsteve);
506                         totface += BLI_edgefill(0);
507
508                         BLI_end_edgefill();
509                 }
510                 
511                 me->totface = totface;
512
513                 /* new tess face block */
514                 if(totface==0) mface= NULL;
515                 else {
516                         mface= MEM_callocN(totface*sizeof(MFace), "loadeditbMesh face");
517                         facenors = MEM_callocN(totface*sizeof(float)*3, "facenors");
518                 }
519
520                 CustomData_add_layer(&me->fdata, CD_MFACE, CD_ASSIGN, mface, me->totface);
521                 CustomData_add_layer(&me->fdata, CD_NORMAL, CD_ASSIGN, facenors, me->totface);
522                 CustomData_from_bmeshpoly(&me->fdata, &bm->pdata, &bm->ldata, totface);
523
524                 mesh_update_customdata_pointers(me);
525                 
526                 i = 0;
527                 BM_ITER(f, &iter, bm, BM_FACES_OF_MESH, NULL) {
528                         EditVert *eve, *lasteve = NULL, *firsteve = NULL;
529                         EditFace *efa;
530                         BMLoop *ls[3];
531                         
532                         BLI_begin_edgefill();
533                         BM_ITER(l, &liter, bm, BM_LOOPS_OF_FACE, f) {
534                                 eve = BLI_addfillvert(l->v->co);
535                                 eve->tmp.p = l;
536
537                                 if (lasteve) {
538                                         BLI_addfilledge(lasteve, eve);
539                                 }
540
541                                 lasteve = eve;
542                                 if (!firsteve) firsteve = eve;
543                         }
544
545                         BLI_addfilledge(lasteve, firsteve);
546                         BLI_edgefill(0);
547
548                         for (efa=fillfacebase.first; efa; efa=efa->next) {
549                                 ls[0] = efa->v1->tmp.p;
550                                 ls[1] = efa->v2->tmp.p;
551                                 ls[2] = efa->v3->tmp.p;
552                                 
553                                 /*ensure correct winding.  I believe this is
554                                   analogous to bubble sort on three elements.*/
555                                 if (BM_GetIndex(ls[0]) > BM_GetIndex(ls[1])) {
556                                         SWAP(BMLoop*, ls[0], ls[1]);
557                                 }
558                                 if (BM_GetIndex(ls[1]) > BM_GetIndex(ls[2])) {
559                                         SWAP(BMLoop*, ls[1], ls[2]);
560                                 }
561                                 if (BM_GetIndex(ls[0]) > BM_GetIndex(ls[1])) {
562                                         SWAP(BMLoop*, ls[0], ls[1]);
563                                 }
564
565                                 mface->mat_nr = f->mat_nr;
566                                 mface->flag = BMFlags_To_MEFlags(f);
567                                 
568                                 mface->v1 = BM_GetIndex(ls[0]->v);
569                                 mface->v2 = BM_GetIndex(ls[1]->v);
570                                 mface->v3 = BM_GetIndex(ls[2]->v);
571
572                                 test_index_face(mface, &me->fdata, i, 1);
573                                 
574                                 loops_to_corners(bm, me, i, f, ls, numTex, numCol);
575                                 VECCOPY(facenors, ls[0]->f->no);
576
577                                 mface++;
578                                 facenors += 3;
579                                 i++;
580                         }
581                         BLI_end_edgefill();
582                 }
583         }
584
585         i = 0;
586         j = 0;
587         BM_ITER(f, &iter, bm, BM_FACES_OF_MESH, NULL) {
588                 mpoly->loopstart = j;
589                 mpoly->totloop = f->len;
590                 mpoly->mat_nr = f->mat_nr;
591                 mpoly->flag = BMFlags_To_MEFlags(f);
592
593                 l = BMIter_New(&liter, bm, BM_LOOPS_OF_FACE, f);
594                 for ( ; l; l=BMIter_Step(&liter), j++, mloop++) {
595                         mloop->e = BM_GetIndex(l->e);
596                         mloop->v = BM_GetIndex(l->v);
597
598                         /*copy over customdata*/
599                         CustomData_from_bmesh_block(&bm->ldata, &me->ldata, l->head.data, j);
600                         CHECK_ELEMENT(bm, l);
601                         CHECK_ELEMENT(bm, l->e);
602                         CHECK_ELEMENT(bm, l->v);
603                 }
604                 
605                 if (f == bm->act_face) me->act_face = i;
606
607                 /*copy over customdata*/
608                 CustomData_from_bmesh_block(&bm->pdata, &me->pdata, f->head.data, i);
609
610                 i++;
611                 mpoly++;
612                 CHECK_ELEMENT(bm, f);
613         }
614
615         /* patch hook indices and vertex parents */
616         {
617                 Object *ob;
618                 ModifierData *md;
619                 BMVert **vertMap = NULL;
620                 int i,j;
621
622                 for (ob=G.main->object.first; ob; ob=ob->id.next) {
623                         if (ob->parent==bm->ob && ELEM(ob->partype, PARVERT1,PARVERT3)) {
624                                 
625                                 /* duplicate code from below, make it function later...? */
626                                 if (!vertMap) {
627                                         vertMap = MEM_callocN(sizeof(*vertMap)*ototvert, "vertMap");
628                                         
629                                         BM_ITER(eve, &iter, bm, BM_VERTS_OF_MESH, NULL) {
630                                                 keyi = CustomData_bmesh_get(&bm->vdata, eve->head.data, CD_SHAPE_KEYINDEX);
631                                                 if (*keyi != ORIGINDEX_NONE)
632                                                         vertMap[*keyi] = eve;
633                                         }
634                                 }
635                                 if(ob->par1 < ototvert) {
636                                         eve = vertMap[ob->par1];
637                                         if(eve) ob->par1= BM_GetIndex(eve);
638                                 }
639                                 if(ob->par2 < ototvert) {
640                                         eve = vertMap[ob->par2];
641                                         if(eve) ob->par2= BM_GetIndex(eve);
642                                 }
643                                 if(ob->par3 < ototvert) {
644                                         eve = vertMap[ob->par3];
645                                         if(eve) ob->par3= BM_GetIndex(eve);
646                                 }
647                                 
648                         }
649                         if (ob->data==me) {
650                                 for (md=ob->modifiers.first; md; md=md->next) {
651                                         if (md->type==eModifierType_Hook) {
652                                                 HookModifierData *hmd = (HookModifierData*) md;
653
654                                                 if (!vertMap) {
655                                                         vertMap = MEM_callocN(sizeof(*vertMap)*ototvert, "vertMap");
656                                                         if(CustomData_has_layer(&bm->vdata, CD_SHAPE_KEYINDEX)) {
657                                                                 i= 0;
658                                                                 BM_ITER(eve, &iter, bm, BM_VERTS_OF_MESH, NULL) {
659                                                                         keyi = CustomData_bmesh_get(&bm->vdata, eve->head.data, CD_SHAPE_KEYINDEX);
660                                                                         if(keyi) {
661                                                                                 if (*keyi != ORIGINDEX_NONE)
662                                                                                         vertMap[*keyi] = eve;
663                                                                         }
664                                                                         else {
665                                                                                 vertMap[i] = eve;
666                                                                         }
667                                                                         i++;
668                                                                 }
669                                                         }
670                                                         else {
671                                                                 i= 0;
672                                                                 BM_ITER(eve, &iter, bm, BM_VERTS_OF_MESH, NULL) {
673                                                                         vertMap[i] = eve;
674                                                                         i++;
675                                                                 }
676                                                         }
677                                                 }
678                                                 
679                                                 for (i=j=0; i<hmd->totindex; i++) {
680                                                         if(hmd->indexar[i] < ototvert) {
681                                                                 eve = vertMap[hmd->indexar[i]];
682                                                                 
683                                                                 if (eve) {
684                                                                         hmd->indexar[j++] = BM_GetIndex(eve);
685                                                                 }
686                                                         }
687                                                         else j++;
688                                                 }
689
690                                                 hmd->totindex = j;
691                                         }
692                                 }
693                         }
694                 }
695
696                 if (vertMap) MEM_freeN(vertMap);
697         }
698
699         mesh_update_customdata_pointers(me);
700
701         {
702                 BMEditSelection *selected;
703                 me->totselect = BLI_countlist(&(bm->selected));
704
705                 if(me->mselect) MEM_freeN(me->mselect);
706
707                 me->mselect = MEM_callocN(sizeof(MSelect) * me->totselect, "Mesh selection history");
708
709
710                 for(i = 0, selected = bm->selected.first; selected; i++, selected = selected->next){
711                         if(selected->type == BM_VERT){
712                                 me->mselect[i].type = ME_VSEL;
713
714                         }else if(selected->type == BM_EDGE){
715                                 me->mselect[i].type = ME_ESEL;
716
717                         }else if(selected->type == BM_FACE){
718                                 me->mselect[i].type = ME_FSEL;
719                         }
720
721                         me->mselect[i].index = BM_GetIndex(selected->data);
722                 }
723         }
724
725         if (me->key) {
726                 KeyBlock *actkey= BLI_findlink(&me->key->block, bm->shapenr-1);
727
728                 /*go through and find any shapekey customdata layers
729                   that might not have corrusponding KeyBlocks, and add them if
730                   necassary.*/
731                 j = 0;
732                 for (i=0; i<bm->vdata.totlayer; i++) {
733                         if (bm->vdata.layers[i].type != CD_SHAPEKEY)
734                                 continue;
735
736                         for (block=me->key->block.first; block; block=block->next) {
737                                 if (block->uid == bm->vdata.layers[i].uid)
738                                         break;
739                         }
740                         
741                         if (!block) {
742                                 block = MEM_callocN(sizeof(KeyBlock), "KeyBlock mesh_conv.c");
743                                 block->type = KEY_LINEAR;
744                                 block->slidermin = 0.0f;
745                                 block->slidermax = 1.0f;
746
747                                 BLI_addtail(&me->key->block, block);
748                                 me->key->totkey++;
749                         }
750
751                         j++;
752                 }
753
754                 for (block=me->key->block.first; block; block=block->next) {
755                         j = 0;
756
757                         for (i=0; i<bm->vdata.totlayer; i++) {
758                                 if (bm->vdata.layers[i].type != CD_SHAPEKEY)
759                                         continue;
760
761                                 if (block->uid == bm->vdata.layers[i].uid) {
762                                         float *fp, *co;
763
764                                         if (block->data)
765                                                 MEM_freeN(block->data);
766                                         block->data = fp = MEM_mallocN(sizeof(float)*3*bm->totvert, "shape key data");
767                                         block->totelem = bm->totvert;
768
769                                         BM_ITER(eve, &iter, bm, BM_VERTS_OF_MESH, NULL) {
770                                                 co = block==actkey ? eve->co : CustomData_bmesh_get_n(&bm->vdata, eve->head.data, CD_SHAPEKEY, j);
771                                                 
772                                                 VECCOPY(fp, co);
773                                                 fp += 3;
774                                         }
775                                         break;
776                                 }
777
778                                 j++;
779                         }
780
781                         /*if we didn't find a shapekey, tag the block to be reconstructed
782                           via the old method below*/
783                         if (j == CustomData_number_of_layers(&bm->vdata, CD_SHAPEKEY)) {
784                                 block->flag |= KEYBLOCK_MISSING;
785                         }
786                 }
787         }
788
789         /* old method of reconstructing keys via vertice's original key indices,
790            currently used if the new method above fails (which is theoretically
791            possible in certain cases of undo).*/
792         if(me->key) {
793                 float *fp, *newkey, *oldkey;
794                 KeyBlock *currkey;
795                 KeyBlock *actkey= BLI_findlink(&me->key->block, bm->shapenr-1);
796
797                 /* Lets reorder the key data so that things line up roughly
798                  * with the way things were before editmode */
799                 currkey = me->key->block.first;
800                 while(currkey) {
801                         if (!(currkey->flag & KEYBLOCK_MISSING)) {
802                                 currkey = currkey->next;
803                                 continue;
804                         }
805                         
806                         printf("warning: had to hackishly reconstruct shape key \"%s\","
807                                " it may not be correct anymore.\n", currkey->name);
808
809                         currkey->flag &= ~KEYBLOCK_MISSING;
810
811                         fp= newkey= MEM_callocN(me->key->elemsize*bm->totvert,  "currkey->data");
812                         oldkey = currkey->data;
813
814                         eve= BMIter_New(&iter, bm, BM_VERTS_OF_MESH, NULL);
815
816                         i = 0;
817                         mvert = me->mvert;
818                         while(eve) {
819                                 keyi = CustomData_bmesh_get(&bm->vdata, eve->head.data, CD_SHAPE_KEYINDEX);
820                                 if (!keyi) {
821                                         break;
822                                 }
823                                 if (*keyi >= 0 && *keyi < currkey->totelem) { // valid old vertex
824                                         if(currkey == actkey) {
825                                                 if(actkey == me->key->refkey) {
826                                                         VECCOPY(fp, mvert->co);
827                                                 }
828                                                 else {
829                                                         VECCOPY(fp, mvert->co);
830                                                         if(oldverts) {
831                                                                 VECCOPY(mvert->co, oldverts[*keyi].co);
832                                                         }
833                                                 }
834                                         }
835                                         else {
836                                                 if(oldkey) {
837                                                         VECCOPY(fp, oldkey + 3 * *keyi);
838                                                 }
839                                         }
840                                 }
841                                 else {
842                                         VECCOPY(fp, mvert->co);
843                                 }
844                                 fp+= 3;
845                                 ++i;
846                                 ++mvert;
847                                 eve= BMIter_Step(&iter);
848                         }
849                         currkey->totelem= bm->totvert;
850                         if(currkey->data) MEM_freeN(currkey->data);
851                         currkey->data = newkey;
852                         
853                         currkey= currkey->next;
854                 }
855         }
856
857         if(oldverts) MEM_freeN(oldverts);
858 }