remove mesh and object arguments from bmesh operators, these are stored within the...
[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 = bm->ob;
48         Mesh *me = ob->data;
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]; */ /* UNUSED */ /* code below always overwrites */
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 *UNUSED(op))
351 {
352         BMO_CallOpf(bm, "bmesh_to_mesh");
353 }
354
355
356 static BMVert **bmesh_to_mesh_vertex_map(BMesh *bm, int ototvert)
357 {
358         BMVert **vertMap = NULL;
359         BMVert *eve;
360          int i= 0;
361         BMIter iter;
362
363         vertMap = MEM_callocN(sizeof(*vertMap)*ototvert, "vertMap");
364         if(CustomData_has_layer(&bm->vdata, CD_SHAPE_KEYINDEX)) {
365                 int *keyi;
366                 BM_ITER(eve, &iter, bm, BM_VERTS_OF_MESH, NULL) {
367                         keyi = CustomData_bmesh_get(&bm->vdata, eve->head.data, CD_SHAPE_KEYINDEX);
368                         if(keyi) {
369                                 if (*keyi != ORIGINDEX_NONE)
370                                         vertMap[*keyi] = eve;
371                         }
372                         else {
373                                 if(i < ototvert) {
374                                         vertMap[i] = eve;
375                                 }
376                         }
377                         i++;
378                 }
379         }
380         else {
381                 BM_ITER(eve, &iter, bm, BM_VERTS_OF_MESH, NULL) {
382                         if(i < ototvert) {
383                                 vertMap[i] = eve;
384                         }
385                         else {
386                                 break;
387                         }
388                         i++;
389                 }
390         }
391
392         return vertMap;
393 }
394
395 void bmesh_to_mesh_exec(BMesh *bm, BMOperator *op) {
396         Object *ob = bm->ob;
397         Mesh *me = ob->data;
398         MLoop *mloop;
399         KeyBlock *block;
400         MPoly *mpoly;
401         MVert *mvert, *oldverts;
402         MEdge *medge;
403         MFace *mface;
404         BMVert *v, *eve;
405         BMEdge *e;
406         BMLoop *l;
407         BMFace *f;
408         BMIter iter, liter;
409         float *facenors = NULL;
410         int i, j, *keyi, ototvert, totloop, totface, numTex, numCol;
411         int dotess = !BMO_Get_Int(op, "notesselation");
412
413         numTex = CustomData_number_of_layers(&bm->pdata, CD_MTEXPOLY);
414         numCol = CustomData_number_of_layers(&bm->ldata, CD_MLOOPCOL);
415         
416         ototvert = me->totvert;
417
418         /* new Vertex block */
419         if(bm->totvert==0) mvert= NULL;
420         else mvert= MEM_callocN(bm->totvert*sizeof(MVert), "loadeditbMesh vert");
421
422         /* new Edge block */
423         if(bm->totedge==0) medge= NULL;
424         else medge= MEM_callocN(bm->totedge*sizeof(MEdge), "loadeditbMesh edge");
425         
426         /*build ngon data*/
427         /* new Ngon Face block */
428         if(bm->totface==0) mpoly = NULL;
429         else mpoly= MEM_callocN(bm->totface*sizeof(MPoly), "loadeditbMesh poly");
430         
431         /*find number of loops to allocate*/
432         totloop = 0;
433         BM_ITER(f, &iter, bm, BM_FACES_OF_MESH, NULL) {
434                 totloop += f->len;
435         }
436
437         if (totloop==0) mloop = NULL;
438         else mloop = MEM_callocN(totloop*sizeof(MLoop), "loadeditbMesh loop");
439
440         /* lets save the old verts just in case we are actually working on
441          * a key ... we now do processing of the keys at the end */
442         oldverts= me->mvert;
443
444         /* don't free this yet */
445         CustomData_set_layer(&me->vdata, CD_MVERT, NULL);
446
447         /* free custom data */
448         CustomData_free(&me->vdata, me->totvert);
449         CustomData_free(&me->edata, me->totedge);
450         CustomData_free(&me->fdata, me->totface);
451         CustomData_free(&me->ldata, me->totloop);
452         CustomData_free(&me->pdata, me->totpoly);
453
454         /* add new custom data */
455         me->totvert= bm->totvert;
456         me->totedge= bm->totedge;
457         me->totloop= totloop;
458         me->totpoly= bm->totface;
459
460         CustomData_copy(&bm->vdata, &me->vdata, CD_MASK_MESH, CD_CALLOC, me->totvert);
461         CustomData_copy(&bm->edata, &me->edata, CD_MASK_MESH, CD_CALLOC, me->totedge);
462         CustomData_copy(&bm->ldata, &me->ldata, CD_MASK_MESH, CD_CALLOC, me->totloop);
463         CustomData_copy(&bm->pdata, &me->pdata, CD_MASK_MESH, CD_CALLOC, me->totpoly);
464
465         CustomData_add_layer(&me->vdata, CD_MVERT, CD_ASSIGN, mvert, me->totvert);
466         CustomData_add_layer(&me->edata, CD_MEDGE, CD_ASSIGN, medge, me->totedge);
467         CustomData_add_layer(&me->ldata, CD_MLOOP, CD_ASSIGN, mloop, me->totloop);
468         CustomData_add_layer(&me->pdata, CD_MPOLY, CD_ASSIGN, mpoly, me->totpoly);
469         
470         i = 0;
471         BM_ITER(v, &iter, bm, BM_VERTS_OF_MESH, NULL) {
472                 float *bweight = CustomData_bmesh_get(&bm->vdata, v->head.data, CD_BWEIGHT);
473
474                 mvert->bweight = bweight ? (char)((*bweight)*255) : 0;
475
476                 VECCOPY(mvert->co, v->co);
477
478                 mvert->no[0] = (short) (v->no[0]*32767.0f);
479                 mvert->no[1] = (short) (v->no[1]*32767.0f);
480                 mvert->no[2] = (short) (v->no[2]*32767.0f);
481                 
482                 mvert->flag = BMFlags_To_MEFlags(v);
483
484                 BM_SetIndex(v, i);
485
486                 /*copy over customdata*/
487                 CustomData_from_bmesh_block(&bm->vdata, &me->vdata, v->head.data, i);
488
489                 i++;
490                 mvert++;
491
492                 CHECK_ELEMENT(bm, v);
493         }
494
495         i = 0;
496         BM_ITER(e, &iter, bm, BM_EDGES_OF_MESH, NULL) {
497                 float *crease = CustomData_bmesh_get(&bm->edata, e->head.data, CD_CREASE);
498                 float *bweight = CustomData_bmesh_get(&bm->edata, e->head.data, CD_BWEIGHT);
499                 
500                 medge->v1 = BM_GetIndex(e->v1);
501                 medge->v2 = BM_GetIndex(e->v2);
502                 medge->crease = crease ? (char)((*crease)*255) : 0;
503                 medge->bweight = bweight ? (char)((*bweight)*255) : 0;
504                 
505                 medge->flag = BMFlags_To_MEFlags(e);
506                 
507                 BM_SetIndex(e, i);
508
509                 /*copy over customdata*/
510                 CustomData_from_bmesh_block(&bm->edata, &me->edata, e->head.data, i);
511
512                 i++;
513                 medge++;
514                 CHECK_ELEMENT(bm, e);
515         }
516
517         /*new scanfill tesselation code*/
518         if (dotess) {
519                 /*first counter number of faces we'll need*/
520                 totface = 0;
521                 BM_ITER(f, &iter, bm, BM_FACES_OF_MESH, NULL) {
522                         EditVert *eve, *lasteve = NULL, *firsteve = NULL;
523                         
524                         BLI_begin_edgefill();
525                         i = 0;
526                         BM_ITER(l, &liter, bm, BM_LOOPS_OF_FACE, f) {
527                                 eve = BLI_addfillvert(l->v->co);
528                                 eve->tmp.p = l;
529                                 
530                                 BM_SetIndex(l, i);
531
532                                 if (lasteve) {
533                                         BLI_addfilledge(lasteve, eve);
534                                 }
535
536                                 lasteve = eve;
537                                 if (!firsteve) firsteve = eve;
538
539                                 i++;
540                         }
541
542                         BLI_addfilledge(lasteve, firsteve);
543                         totface += BLI_edgefill(0);
544
545                         BLI_end_edgefill();
546                 }
547                 
548                 me->totface = totface;
549
550                 /* new tess face block */
551                 if(totface==0) mface= NULL;
552                 else {
553                         mface= MEM_callocN(totface*sizeof(MFace), "loadeditbMesh face");
554                         facenors = MEM_callocN(totface*sizeof(float)*3, "facenors");
555                 }
556
557                 CustomData_add_layer(&me->fdata, CD_MFACE, CD_ASSIGN, mface, me->totface);
558                 CustomData_add_layer(&me->fdata, CD_NORMAL, CD_ASSIGN, facenors, me->totface);
559                 CustomData_from_bmeshpoly(&me->fdata, &bm->pdata, &bm->ldata, totface);
560
561                 mesh_update_customdata_pointers(me);
562                 
563                 i = 0;
564                 BM_ITER(f, &iter, bm, BM_FACES_OF_MESH, NULL) {
565                         EditVert *eve, *lasteve = NULL, *firsteve = NULL;
566                         EditFace *efa;
567                         BMLoop *ls[3];
568                         
569                         BLI_begin_edgefill();
570                         BM_ITER(l, &liter, bm, BM_LOOPS_OF_FACE, f) {
571                                 eve = BLI_addfillvert(l->v->co);
572                                 eve->tmp.p = l;
573
574                                 if (lasteve) {
575                                         BLI_addfilledge(lasteve, eve);
576                                 }
577
578                                 lasteve = eve;
579                                 if (!firsteve) firsteve = eve;
580                         }
581
582                         BLI_addfilledge(lasteve, firsteve);
583                         BLI_edgefill(0);
584
585                         for (efa=fillfacebase.first; efa; efa=efa->next) {
586                                 ls[0] = efa->v1->tmp.p;
587                                 ls[1] = efa->v2->tmp.p;
588                                 ls[2] = efa->v3->tmp.p;
589                                 
590                                 /*ensure correct winding.  I believe this is
591                                   analogous to bubble sort on three elements.*/
592                                 if (BM_GetIndex(ls[0]) > BM_GetIndex(ls[1])) {
593                                         SWAP(BMLoop*, ls[0], ls[1]);
594                                 }
595                                 if (BM_GetIndex(ls[1]) > BM_GetIndex(ls[2])) {
596                                         SWAP(BMLoop*, ls[1], ls[2]);
597                                 }
598                                 if (BM_GetIndex(ls[0]) > BM_GetIndex(ls[1])) {
599                                         SWAP(BMLoop*, ls[0], ls[1]);
600                                 }
601
602                                 mface->mat_nr = f->mat_nr;
603                                 mface->flag = BMFlags_To_MEFlags(f);
604                                 
605                                 mface->v1 = BM_GetIndex(ls[0]->v);
606                                 mface->v2 = BM_GetIndex(ls[1]->v);
607                                 mface->v3 = BM_GetIndex(ls[2]->v);
608
609                                 test_index_face(mface, &me->fdata, i, 1);
610                                 
611                                 loops_to_corners(bm, me, i, f, ls, numTex, numCol);
612                                 VECCOPY(facenors, ls[0]->f->no);
613
614                                 mface++;
615                                 facenors += 3;
616                                 i++;
617                         }
618                         BLI_end_edgefill();
619                 }
620         }
621
622         i = 0;
623         j = 0;
624         BM_ITER(f, &iter, bm, BM_FACES_OF_MESH, NULL) {
625                 mpoly->loopstart = j;
626                 mpoly->totloop = f->len;
627                 mpoly->mat_nr = f->mat_nr;
628                 mpoly->flag = BMFlags_To_MEFlags(f);
629
630                 l = BMIter_New(&liter, bm, BM_LOOPS_OF_FACE, f);
631                 for ( ; l; l=BMIter_Step(&liter), j++, mloop++) {
632                         mloop->e = BM_GetIndex(l->e);
633                         mloop->v = BM_GetIndex(l->v);
634
635                         /*copy over customdata*/
636                         CustomData_from_bmesh_block(&bm->ldata, &me->ldata, l->head.data, j);
637                         CHECK_ELEMENT(bm, l);
638                         CHECK_ELEMENT(bm, l->e);
639                         CHECK_ELEMENT(bm, l->v);
640                 }
641                 
642                 if (f == bm->act_face) me->act_face = i;
643
644                 /*copy over customdata*/
645                 CustomData_from_bmesh_block(&bm->pdata, &me->pdata, f->head.data, i);
646
647                 i++;
648                 mpoly++;
649                 CHECK_ELEMENT(bm, f);
650         }
651
652         /* patch hook indices and vertex parents */
653         {
654                 Object *ob;
655                 ModifierData *md;
656                 BMVert **vertMap = NULL;
657                 int i,j;
658
659                 for (ob=G.main->object.first; ob; ob=ob->id.next) {
660                         if (ob->parent==bm->ob && ELEM(ob->partype, PARVERT1,PARVERT3)) {
661
662                                 if (vertMap == NULL) {
663                                         vertMap= bmesh_to_mesh_vertex_map(bm, ototvert);
664                                 }
665
666                                 if(ob->par1 < ototvert) {
667                                         eve = vertMap[ob->par1];
668                                         if(eve) ob->par1= BM_GetIndex(eve);
669                                 }
670                                 if(ob->par2 < ototvert) {
671                                         eve = vertMap[ob->par2];
672                                         if(eve) ob->par2= BM_GetIndex(eve);
673                                 }
674                                 if(ob->par3 < ototvert) {
675                                         eve = vertMap[ob->par3];
676                                         if(eve) ob->par3= BM_GetIndex(eve);
677                                 }
678                                 
679                         }
680                         if (ob->data==me) {
681                                 for (md=ob->modifiers.first; md; md=md->next) {
682                                         if (md->type==eModifierType_Hook) {
683                                                 HookModifierData *hmd = (HookModifierData*) md;
684
685                                                 if (vertMap == NULL) {
686                                                         vertMap= bmesh_to_mesh_vertex_map(bm, ototvert);
687                                                 }
688                                                 
689                                                 for (i=j=0; i<hmd->totindex; i++) {
690                                                         if(hmd->indexar[i] < ototvert) {
691                                                                 eve = vertMap[hmd->indexar[i]];
692                                                                 
693                                                                 if (eve) {
694                                                                         hmd->indexar[j++] = BM_GetIndex(eve);
695                                                                 }
696                                                         }
697                                                         else j++;
698                                                 }
699
700                                                 hmd->totindex = j;
701                                         }
702                                 }
703                         }
704                 }
705
706                 if (vertMap) MEM_freeN(vertMap);
707         }
708
709         mesh_update_customdata_pointers(me);
710
711         {
712                 BMEditSelection *selected;
713                 me->totselect = BLI_countlist(&(bm->selected));
714
715                 if(me->mselect) MEM_freeN(me->mselect);
716
717                 me->mselect = MEM_callocN(sizeof(MSelect) * me->totselect, "Mesh selection history");
718
719
720                 for(i = 0, selected = bm->selected.first; selected; i++, selected = selected->next){
721                         if(selected->type == BM_VERT){
722                                 me->mselect[i].type = ME_VSEL;
723
724                         }else if(selected->type == BM_EDGE){
725                                 me->mselect[i].type = ME_ESEL;
726
727                         }else if(selected->type == BM_FACE){
728                                 me->mselect[i].type = ME_FSEL;
729                         }
730
731                         me->mselect[i].index = BM_GetIndex(selected->data);
732                 }
733         }
734
735         if (me->key) {
736                 KeyBlock *actkey= BLI_findlink(&me->key->block, bm->shapenr-1);
737
738                 /*go through and find any shapekey customdata layers
739                   that might not have corrusponding KeyBlocks, and add them if
740                   necassary.*/
741                 j = 0;
742                 for (i=0; i<bm->vdata.totlayer; i++) {
743                         if (bm->vdata.layers[i].type != CD_SHAPEKEY)
744                                 continue;
745
746                         for (block=me->key->block.first; block; block=block->next) {
747                                 if (block->uid == bm->vdata.layers[i].uid)
748                                         break;
749                         }
750                         
751                         if (!block) {
752                                 block = MEM_callocN(sizeof(KeyBlock), "KeyBlock mesh_conv.c");
753                                 block->type = KEY_LINEAR;
754                                 block->slidermin = 0.0f;
755                                 block->slidermax = 1.0f;
756
757                                 BLI_addtail(&me->key->block, block);
758                                 me->key->totkey++;
759                         }
760
761                         j++;
762                 }
763
764                 for (block=me->key->block.first; block; block=block->next) {
765                         j = 0;
766
767                         for (i=0; i<bm->vdata.totlayer; i++) {
768                                 if (bm->vdata.layers[i].type != CD_SHAPEKEY)
769                                         continue;
770
771                                 if (block->uid == bm->vdata.layers[i].uid) {
772                                         float *fp, *co;
773
774                                         if (block->data)
775                                                 MEM_freeN(block->data);
776                                         block->data = fp = MEM_mallocN(sizeof(float)*3*bm->totvert, "shape key data");
777                                         block->totelem = bm->totvert;
778
779                                         BM_ITER(eve, &iter, bm, BM_VERTS_OF_MESH, NULL) {
780                                                 co = block==actkey ? eve->co : CustomData_bmesh_get_n(&bm->vdata, eve->head.data, CD_SHAPEKEY, j);
781                                                 
782                                                 VECCOPY(fp, co);
783                                                 fp += 3;
784                                         }
785                                         break;
786                                 }
787
788                                 j++;
789                         }
790
791                         /*if we didn't find a shapekey, tag the block to be reconstructed
792                           via the old method below*/
793                         if (j == CustomData_number_of_layers(&bm->vdata, CD_SHAPEKEY)) {
794                                 block->flag |= KEYBLOCK_MISSING;
795                         }
796                 }
797         }
798
799         /* old method of reconstructing keys via vertice's original key indices,
800            currently used if the new method above fails (which is theoretically
801            possible in certain cases of undo).*/
802         if(me->key) {
803                 float *fp, *newkey, *oldkey;
804                 KeyBlock *currkey;
805                 KeyBlock *actkey= BLI_findlink(&me->key->block, bm->shapenr-1);
806
807                 /* Lets reorder the key data so that things line up roughly
808                  * with the way things were before editmode */
809                 currkey = me->key->block.first;
810                 while(currkey) {
811                         if (!(currkey->flag & KEYBLOCK_MISSING)) {
812                                 currkey = currkey->next;
813                                 continue;
814                         }
815                         
816                         printf("warning: had to hackishly reconstruct shape key \"%s\","
817                                " it may not be correct anymore.\n", currkey->name);
818
819                         currkey->flag &= ~KEYBLOCK_MISSING;
820
821                         fp= newkey= MEM_callocN(me->key->elemsize*bm->totvert,  "currkey->data");
822                         oldkey = currkey->data;
823
824                         eve= BMIter_New(&iter, bm, BM_VERTS_OF_MESH, NULL);
825
826                         i = 0;
827                         mvert = me->mvert;
828                         while(eve) {
829                                 keyi = CustomData_bmesh_get(&bm->vdata, eve->head.data, CD_SHAPE_KEYINDEX);
830                                 if (!keyi) {
831                                         break;
832                                 }
833                                 if (*keyi >= 0 && *keyi < currkey->totelem) { // valid old vertex
834                                         if(currkey == actkey) {
835                                                 if(actkey == me->key->refkey) {
836                                                         VECCOPY(fp, mvert->co);
837                                                 }
838                                                 else {
839                                                         VECCOPY(fp, mvert->co);
840                                                         if(oldverts) {
841                                                                 VECCOPY(mvert->co, oldverts[*keyi].co);
842                                                         }
843                                                 }
844                                         }
845                                         else {
846                                                 if(oldkey) {
847                                                         VECCOPY(fp, oldkey + 3 * *keyi);
848                                                 }
849                                         }
850                                 }
851                                 else {
852                                         VECCOPY(fp, mvert->co);
853                                 }
854                                 fp+= 3;
855                                 ++i;
856                                 ++mvert;
857                                 eve= BMIter_Step(&iter);
858                         }
859                         currkey->totelem= bm->totvert;
860                         if(currkey->data) MEM_freeN(currkey->data);
861                         currkey->data = newkey;
862                         
863                         currkey= currkey->next;
864                 }
865         }
866
867         if(oldverts) MEM_freeN(oldverts);
868 }