Quiet annoying warning:
[blender.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 {
48         Object *ob = BMO_Get_Pnt(op, "object");
49         Mesh *me = BMO_Get_Pnt(op, "mesh");
50         MVert *mvert;
51         BLI_array_declare(verts);
52         MEdge *medge;
53         MLoop *ml;
54         MPoly *mpoly;
55         KeyBlock *actkey, *block;
56         BMVert *v, **vt=NULL, **verts = NULL;
57         BMEdge *e, **fedges=NULL, **et = NULL;
58         BMFace *f;
59         BMLoop *l;
60         BLI_array_declare(fedges);
61         float (*keyco)[3]= NULL;
62         int *keyi;
63         int set_key = BMO_Get_Int(op, "set_shapekey");
64         int totuv, i, j, allocsize[4] = {512, 512, 2048, 512};
65
66         if (!me || !me->totvert) return; /*sanity check*/
67         
68         vt = MEM_mallocN(sizeof(void**)*me->totvert, "mesh to bmesh vtable");
69
70         CustomData_copy(&me->vdata, &bm->vdata, CD_MASK_BMESH, CD_CALLOC, 0);
71         CustomData_copy(&me->edata, &bm->edata, CD_MASK_BMESH, CD_CALLOC, 0);
72         CustomData_copy(&me->ldata, &bm->ldata, CD_MASK_BMESH, CD_CALLOC, 0);
73         CustomData_copy(&me->pdata, &bm->pdata, CD_MASK_BMESH, CD_CALLOC, 0);
74         
75         /*make sure uv layer names are consistent*/
76         totuv = CustomData_number_of_layers(&bm->pdata, CD_MTEXPOLY);
77         for (i=0; i<totuv; i++) {
78                 int li = CustomData_get_layer_index_n(&bm->pdata, CD_MTEXPOLY, i);
79                 CustomData_set_layer_name(&bm->ldata, CD_MLOOPUV, i, bm->pdata.layers[li].name);
80         }
81         
82         if (!CustomData_has_layer(&bm->edata, CD_CREASE))
83                 CustomData_add_layer(&bm->edata, CD_CREASE, CD_ASSIGN, NULL, 0);
84
85         if (!CustomData_has_layer(&bm->edata, CD_BWEIGHT))
86                 CustomData_add_layer(&bm->edata, CD_BWEIGHT, CD_ASSIGN, NULL, 0);
87
88         if (!CustomData_has_layer(&bm->vdata, CD_BWEIGHT))
89                 CustomData_add_layer(&bm->vdata, CD_BWEIGHT, CD_ASSIGN, NULL, 0);
90
91
92         if (me->key && ob->shapenr > me->key->totkey) {
93                 ob->shapenr = me->key->totkey-1;
94         }
95
96         actkey = ob_get_keyblock(ob);
97         if(actkey && actkey->totelem == me->totvert) {
98                 CustomData_add_layer(&bm->vdata, CD_SHAPE_KEYINDEX, CD_ASSIGN, NULL, 0);
99                 
100                 /*check if we need to generate unique ids for the shapekeys.
101                   this also exists in the file reading code, but is here for
102                   a sanity check*/
103                 if (!me->key->uidgen) {
104                         fprintf(stderr, "%s had to generate shape key uid's in a situation we shouldn't need to! (bmesh internal error)\n", __func__);
105                         me->key->uidgen = 1;
106                         for (block=me->key->block.first; block; block=block->next) {
107                                 block->uid = me->key->uidgen++;
108                         }
109                 }
110
111                 keyco= actkey->data;
112                 bm->shapenr= ob->shapenr;
113                 for (i=0, block=me->key->block.first; block; block=block->next, i++) {
114                         CustomData_add_layer_named(&bm->vdata, CD_SHAPEKEY, 
115                                          CD_ASSIGN, NULL, 0, block->name);
116                         
117                         j = CustomData_get_layer_index_n(&bm->vdata, CD_SHAPEKEY, i);
118                         bm->vdata.layers[j].uid = block->uid;
119                 }
120         } else if (actkey) {
121                 printf("shapekey<->mesh mismatch!\n");
122         }
123         
124         CustomData_bmesh_init_pool(&bm->vdata, allocsize[0]);
125         CustomData_bmesh_init_pool(&bm->edata, allocsize[1]);
126         CustomData_bmesh_init_pool(&bm->ldata, allocsize[2]);
127         CustomData_bmesh_init_pool(&bm->pdata, allocsize[3]);
128
129         for (i=0, mvert = me->mvert; i<me->totvert; i++, mvert++) {
130                 v = BM_Make_Vert(bm, keyco&&set_key ? keyco[i] : mvert->co, NULL);
131                 BM_SetIndex(v, i); /* set_ok */
132                 vt[i] = v;
133
134                 /*transfer flags*/
135                 v->head.hflag = BM_Vert_Flag_From_MEFlag(mvert->flag);
136
137                 /*this is necassary for selection counts to work properly*/
138                 if (BM_TestHFlag(v, BM_SELECT)) BM_Select_Vert(bm, v, 1);
139
140                 normal_short_to_float_v3(v->no, mvert->no);
141
142                 BM_SetCDf(&bm->vdata, v, CD_BWEIGHT, (float)mvert->bweight / 255.0f);
143
144                 /*Copy Custom Data*/
145                 CustomData_to_bmesh_block(&me->vdata, &bm->vdata, i, &v->head.data);
146
147                 /*set shapekey data*/
148                 if (me->key) {
149                         /*set shape key original index*/
150                         keyi = CustomData_bmesh_get(&bm->vdata, v->head.data, CD_SHAPE_KEYINDEX);
151                         if (keyi) {
152                                 *keyi = i;
153                         }
154                         
155                         for (block=me->key->block.first, j=0; block; block=block->next, j++) {
156                                 float *co = CustomData_bmesh_get_n(&bm->vdata, v->head.data, 
157                                                                    CD_SHAPEKEY, j);
158                                 if (co)
159                                         copy_v3_v3(co, ((float*)block->data)+3*i);
160                         }
161                 }
162         }
163
164         bm->elem_index_dirty &= ~BM_VERT; /* added in order, clear dirty flag */
165
166         if (!me->totedge) {
167                 MEM_freeN(vt);
168                 return;
169         }
170
171         et = MEM_mallocN(sizeof(void**)*me->totedge, "mesh to bmesh etable");
172
173         medge = me->medge;
174         for (i=0; i<me->totedge; i++, medge++) {
175                 e = BM_Make_Edge(bm, vt[medge->v1], vt[medge->v2], NULL, 0);
176                 BM_SetIndex(e, i); /* set_ok */
177                 et[i] = e;
178
179                 /*transfer flags*/
180                 e->head.hflag = BM_Edge_Flag_From_MEFlag(medge->flag);
181
182                 /*this is necassary for selection counts to work properly*/
183                 if (BM_TestHFlag(e, BM_SELECT)) BM_Select(bm, e, 1);
184                 
185                 /*Copy Custom Data*/
186                 CustomData_to_bmesh_block(&me->edata, &bm->edata, i, &e->head.data);
187                 
188                 BM_SetCDf(&bm->edata, e, CD_CREASE, (float)medge->crease / 255.0f);
189                 BM_SetCDf(&bm->edata, e, CD_BWEIGHT, (float)medge->bweight / 255.0f);
190         }
191
192         bm->elem_index_dirty &= ~BM_EDGE; /* added in order, clear dirty flag */
193         
194         if (!me->totpoly) {
195                 MEM_freeN(vt);
196                 MEM_freeN(et);
197                 return;
198         }
199
200         mpoly = me->mpoly;
201         for (i=0; i<me->totpoly; i++, mpoly++) {
202                 BMVert *v1 /* , *v2 */ /* UNUSED */;
203                 BMIter iter;
204
205                 BLI_array_empty(fedges);
206                 BLI_array_empty(verts);
207                 for (j=0; j<mpoly->totloop; j++) {
208                         ml = &me->mloop[mpoly->loopstart+j];
209                         v = vt[ml->v];
210                         e = et[ml->e];
211
212                         BLI_array_growone(fedges);
213                         BLI_array_growone(verts);
214
215                         fedges[j] = e;
216                         verts[j] = v;
217                 }
218                 
219                 v1 = vt[me->mloop[mpoly->loopstart].v];
220                 /* v2 = vt[me->mloop[mpoly->loopstart+1].v]; */ /* UNUSED */ /* code below always overwrites */
221
222                 if (v1 == fedges[0]->v1) {
223                         /* v2 = fedges[0]->v2; */ /* UNUSED */
224                 }
225                 else {
226                         v1 = fedges[0]->v2;
227                         /* v2 = fedges[0]->v1; */ /* UNUSED */
228                 }
229         
230                 f = BM_Make_Face(bm, verts, fedges, mpoly->totloop, 0);
231
232                 if (!f) {
233                         printf("%s: Warning! Bad face in mesh"
234                                " \"%s\" at index %d!, skipping\n",
235                                __func__, me->id.name+2, i);
236                         continue;
237                 }
238
239                 /* dont use 'i' since we may have skipped the face */
240                 BM_SetIndex(f, bm->totface-1); /* set_ok */
241
242                 /*transfer flags*/
243                 f->head.hflag = BM_Face_Flag_From_MEFlag(mpoly->flag);
244
245                 /*this is necassary for selection counts to work properly*/
246                 if (BM_TestHFlag(f, BM_SELECT)) BM_Select(bm, f, 1);
247
248                 f->mat_nr = mpoly->mat_nr;
249                 if (i == me->act_face) bm->act_face = f;
250
251                 j = 0;
252                 BM_ITER_INDEX(l, &iter, bm, BM_LOOPS_OF_FACE, f, j) {
253                         /* Save index of correspsonding MLoop */
254                         BM_SetIndex(l, mpoly->loopstart+j); /* set_loop */
255                 }
256
257                 /*Copy Custom Data*/
258                 CustomData_to_bmesh_block(&me->pdata, &bm->pdata, i, &f->head.data);
259         }
260
261         bm->elem_index_dirty &= ~BM_FACE; /* added in order, clear dirty flag */
262
263         {
264                 BMIter fiter;
265                 BMIter liter;
266                 
267                 /* Copy over loop CustomData. Doing this in a separate loop isn't necessary
268                    but is an optimization, to avoid copying a bunch of interpolated customdata
269                    for each BMLoop (from previous BMLoops using the same edge), always followed
270                    by freeing the interpolated data and overwriting it with data from the Mesh. */
271                 BM_ITER(f, &fiter, bm, BM_FACES_OF_MESH, NULL) {
272                         BM_ITER(l, &liter, bm, BM_LOOPS_OF_FACE, f) {
273                                 int li = BM_GetIndex(l);
274                                 CustomData_to_bmesh_block(&me->ldata, &bm->ldata, li, &l->head.data);
275                                 BM_SetIndex(l, 0); /* set_loop */
276                         }
277                 }
278         }
279
280         {
281                 BMIter iter;
282                 BMVert *vertex;
283                 BMEdge *edge;
284                 BMFace *face;
285                 BMVert **vertex_array = MEM_callocN(sizeof(BMVert *) * bm->totvert,
286                                                   "Selection Conversion Vertex Pointer Array");
287                 BMEdge **edge_array = MEM_callocN(sizeof(BMEdge *) * bm->totedge,
288                                                 "Selection Conversion Edge Pointer Array");
289                 BMFace **face_array = MEM_callocN(sizeof(BMFace *) * bm->totface,
290                                                 "Selection Conversion Face Pointer Array");
291
292                 for(i = 0, vertex = BMIter_New(&iter, bm, BM_VERTS_OF_MESH, NULL);
293                     vertex; i++, vertex = BMIter_Step(&iter)){
294                         vertex_array[i] = vertex;
295                 }
296
297                 for(i = 0, edge = BMIter_New(&iter, bm, BM_EDGES_OF_MESH, NULL);
298                     edge; i++, edge = BMIter_Step(&iter)){
299                         edge_array[i] = edge;
300                 }
301
302                 for(i = 0, face = BMIter_New(&iter, bm, BM_FACES_OF_MESH, NULL);
303                     face; i++, face = BMIter_Step(&iter)){
304                         face_array[i] = face;
305                 }
306
307                 if(me->mselect) {
308                         for(i = 0; i < me->totselect; i++){
309                                 if(me->mselect[i].type == ME_VSEL){
310                                         BM_store_selection(bm, vertex_array[me->mselect[i].index]);
311                                 }else if(me->mselect[i].type == ME_ESEL){
312                                         BM_store_selection(bm, edge_array[me->mselect[i].index]);
313                                 }else if(me->mselect[i].type == ME_FSEL){
314                                         BM_store_selection(bm, face_array[me->mselect[i].index]);
315                                 }
316                         }
317                 }
318                 else {
319                         me->totselect= 0;
320                 }
321
322                 MEM_freeN(vertex_array);
323                 MEM_freeN(edge_array);
324                 MEM_freeN(face_array);
325         }
326
327         BLI_array_free(fedges);
328         BLI_array_free(verts);
329         
330         MEM_freeN(vt);
331         MEM_freeN(et);
332 }
333
334
335 static void loops_to_corners(BMesh *bm, Mesh *me, int findex,
336                              BMFace *f, BMLoop *ls[3], int numTex, int numCol) 
337 {
338         BMLoop *l;
339         MTFace *texface;
340         MTexPoly *texpoly;
341         MCol *mcol;
342         MLoopCol *mloopcol;
343         MLoopUV *mloopuv;
344         int i, j;
345
346         for(i=0; i < numTex; i++) {
347                 texface = CustomData_get_n(&me->fdata, CD_MTFACE, findex, i);
348                 texpoly = CustomData_bmesh_get_n(&bm->pdata, f->head.data, CD_MTEXPOLY, i);
349                 
350                 texface->tpage = texpoly->tpage;
351                 texface->flag = texpoly->flag;
352                 texface->transp = texpoly->transp;
353                 texface->mode = texpoly->mode;
354                 texface->tile = texpoly->tile;
355                 texface->unwrap = texpoly->unwrap;
356
357                 for (j=0; j<3; j++) {
358                         l = ls[j];
359                         mloopuv = CustomData_bmesh_get_n(&bm->ldata, l->head.data, CD_MLOOPUV, i);
360                         texface->uv[j][0] = mloopuv->uv[0];
361                         texface->uv[j][1] = mloopuv->uv[1];
362                 }
363         }
364
365         for(i=0; i < numCol; i++){
366                 mcol = CustomData_get_n(&me->fdata, CD_MCOL, findex, i);
367
368                 for (j=0; j<3; j++) {
369                         l = ls[j];
370                         mloopcol = CustomData_bmesh_get_n(&bm->ldata, l->head.data, CD_MLOOPCOL, i);
371                         mcol[j].r = mloopcol->r;
372                         mcol[j].g = mloopcol->g;
373                         mcol[j].b = mloopcol->b;
374                         mcol[j].a = mloopcol->a;
375                 }
376         }
377 }
378
379 void object_load_bmesh_exec(BMesh *bm, BMOperator *op)
380 {
381         Object *ob = BMO_Get_Pnt(op, "object");
382         /* Scene *scene = BMO_Get_Pnt(op, "scene"); */
383         Mesh *me = ob->data;
384
385         BMO_CallOpf(bm, "bmesh_to_mesh mesh=%p object=%p", me, ob);
386 }
387
388
389 static BMVert **bmesh_to_mesh_vertex_map(BMesh *bm, int ototvert)
390 {
391         BMVert **vertMap = NULL;
392         BMVert *eve;
393         int index;
394         int i= 0;
395         BMIter iter;
396
397         /* caller needs to ensure this */
398         BLI_assert(ototvert > 0);
399
400         vertMap = MEM_callocN(sizeof(*vertMap)*ototvert, "vertMap");
401         if(CustomData_has_layer(&bm->vdata, CD_SHAPE_KEYINDEX)) {
402                 int *keyi;
403                 BM_ITER(eve, &iter, bm, BM_VERTS_OF_MESH, NULL) {
404                         keyi = CustomData_bmesh_get(&bm->vdata, eve->head.data, CD_SHAPE_KEYINDEX);
405                         if(keyi) {
406                                 if (((index= *keyi) != ORIGINDEX_NONE) && (index < ototvert)) {
407                                         vertMap[index] = eve;
408                                 }
409                         }
410                         else {
411                                 if(i < ototvert) {
412                                         vertMap[i] = eve;
413                                 }
414                         }
415                         i++;
416                 }
417         }
418         else {
419                 BM_ITER(eve, &iter, bm, BM_VERTS_OF_MESH, NULL) {
420                         if(i < ototvert) {
421                                 vertMap[i] = eve;
422                         }
423                         else {
424                                 break;
425                         }
426                         i++;
427                 }
428         }
429
430         return vertMap;
431 }
432
433 BM_INLINE void bmesh_quick_edgedraw_flag(MEdge *med, BMEdge *e)
434 {
435         /* this is a cheap way to set the edge draw, its not precise and will
436          * pick the first 2 faces an edge uses */
437
438
439         if ( /* (med->flag & ME_EDGEDRAW) && */ /* assume to be true */
440              (e->l && (e->l != e->l->radial_next)) &&
441              (dot_v3v3(e->l->f->no, e->l->radial_next->f->no) > 0.998f))
442         {
443                 med->flag &= ~ME_EDGEDRAW;
444         }
445 }
446
447
448 void bmesh_to_mesh_exec(BMesh *bm, BMOperator *op)
449 {
450         Mesh *me = BMO_Get_Pnt(op, "mesh");
451         /* Object *ob = BMO_Get_Pnt(op, "object"); */
452         MLoop *mloop;
453         MPoly *mpoly;
454         MVert *mvert, *oldverts;
455         MEdge *med, *medge;
456         MFace *mface;
457         BMVert *v, *eve;
458         BMEdge *e;
459         BMLoop *l;
460         BMFace *f;
461         BMIter iter, liter; float *facenors = NULL;
462         int i, j, *keyi, ototvert, totloop, totface, numTex, numCol, *polyindex = NULL;
463         int dotess = !BMO_Get_Int(op, "notesselation");
464
465         numTex = CustomData_number_of_layers(&bm->pdata, CD_MTEXPOLY);
466         numCol = CustomData_number_of_layers(&bm->ldata, CD_MLOOPCOL);
467         
468         ototvert = me->totvert;
469
470         /* new Vertex block */
471         if(bm->totvert==0) mvert= NULL;
472         else mvert= MEM_callocN(bm->totvert*sizeof(MVert), "loadeditbMesh vert");
473
474         /* new Edge block */
475         if(bm->totedge==0) medge= NULL;
476         else medge= MEM_callocN(bm->totedge*sizeof(MEdge), "loadeditbMesh edge");
477         
478         /*build ngon data*/
479         /* new Ngon Face block */
480         if(bm->totface==0) mpoly = NULL;
481         else mpoly= MEM_callocN(bm->totface*sizeof(MPoly), "loadeditbMesh poly");
482         
483         /*find number of loops to allocate*/
484         totloop = 0;
485         BM_ITER(f, &iter, bm, BM_FACES_OF_MESH, NULL) {
486                 totloop += f->len;
487         }
488
489         if (totloop==0) mloop = NULL;
490         else mloop = MEM_callocN(totloop*sizeof(MLoop), "loadeditbMesh loop");
491
492         /* lets save the old verts just in case we are actually working on
493          * a key ... we now do processing of the keys at the end */
494         oldverts= me->mvert;
495
496         /* don't free this yet */
497         CustomData_set_layer(&me->vdata, CD_MVERT, NULL);
498
499         /* free custom data */
500         CustomData_free(&me->vdata, me->totvert);
501         CustomData_free(&me->edata, me->totedge);
502         CustomData_free(&me->fdata, me->totface);
503         CustomData_free(&me->ldata, me->totloop);
504         CustomData_free(&me->pdata, me->totpoly);
505
506         /* add new custom data */
507         me->totvert= bm->totvert;
508         me->totedge= bm->totedge;
509         me->totloop= totloop;
510         me->totpoly= bm->totface;
511         /* will be overwritten with a valid value if 'dotess' is set, otherwise we
512          * end up with 'me->totface' and me->mface == NULL which can crash [#28625]
513          */
514         me->totface= 0;
515
516         CustomData_copy(&bm->vdata, &me->vdata, CD_MASK_MESH, CD_CALLOC, me->totvert);
517         CustomData_copy(&bm->edata, &me->edata, CD_MASK_MESH, CD_CALLOC, me->totedge);
518         CustomData_copy(&bm->ldata, &me->ldata, CD_MASK_MESH, CD_CALLOC, me->totloop);
519         CustomData_copy(&bm->pdata, &me->pdata, CD_MASK_MESH, CD_CALLOC, me->totpoly);
520
521         CustomData_add_layer(&me->vdata, CD_MVERT, CD_ASSIGN, mvert, me->totvert);
522         CustomData_add_layer(&me->edata, CD_MEDGE, CD_ASSIGN, medge, me->totedge);
523         CustomData_add_layer(&me->ldata, CD_MLOOP, CD_ASSIGN, mloop, me->totloop);
524         CustomData_add_layer(&me->pdata, CD_MPOLY, CD_ASSIGN, mpoly, me->totpoly);
525         
526         i = 0;
527         BM_ITER(v, &iter, bm, BM_VERTS_OF_MESH, NULL) {
528                 float *bweight = CustomData_bmesh_get(&bm->vdata, v->head.data, CD_BWEIGHT);
529
530                 mvert->bweight = bweight ? (char)((*bweight)*255) : 0;
531
532                 copy_v3_v3(mvert->co, v->co);
533                 normal_float_to_short_v3(mvert->no, v->no);
534                 
535                 mvert->flag = BM_Vert_Flag_To_MEFlag(v);
536
537                 BM_SetIndex(v, i); /* set_inline */
538
539                 /*copy over customdata*/
540                 CustomData_from_bmesh_block(&bm->vdata, &me->vdata, v->head.data, i);
541
542                 i++;
543                 mvert++;
544
545                 BM_CHECK_ELEMENT(bm, v);
546         }
547         bm->elem_index_dirty &= ~BM_VERT;
548
549         med= medge;
550         i = 0;
551         BM_ITER(e, &iter, bm, BM_EDGES_OF_MESH, NULL) {
552                 float *crease = CustomData_bmesh_get(&bm->edata, e->head.data, CD_CREASE);
553                 float *bweight = CustomData_bmesh_get(&bm->edata, e->head.data, CD_BWEIGHT);
554                 
555                 med->v1 = BM_GetIndex(e->v1);
556                 med->v2 = BM_GetIndex(e->v2);
557                 med->crease = crease ? (char)((*crease)*255) : 0;
558                 med->bweight = bweight ? (char)((*bweight)*255) : 0;
559                 
560                 med->flag = BM_Edge_Flag_To_MEFlag(e);
561                 
562                 BM_SetIndex(e, i); /* set_inline */
563
564                 /*copy over customdata*/
565                 CustomData_from_bmesh_block(&bm->edata, &me->edata, e->head.data, i);
566
567                 bmesh_quick_edgedraw_flag(med, e);
568
569                 i++;
570                 med++;
571                 BM_CHECK_ELEMENT(bm, e);
572         }
573         bm->elem_index_dirty &= ~BM_EDGE;
574
575         /*new scanfill tesselation code*/
576         if (dotess) {
577                 /*first counter number of faces we'll need*/
578                 totface = 0;
579                 BM_ITER(f, &iter, bm, BM_FACES_OF_MESH, NULL) {
580                         EditVert *eve, *lasteve = NULL, *firsteve = NULL;
581                         
582                         BLI_begin_edgefill();
583                         i = 0;
584                         BM_ITER(l, &liter, bm, BM_LOOPS_OF_FACE, f) {
585                                 eve = BLI_addfillvert(l->v->co);
586                                 eve->tmp.p = l;
587                                 
588                                 BM_SetIndex(l, i); /* set_loop */
589
590                                 if (lasteve) {
591                                         BLI_addfilledge(lasteve, eve);
592                                 }
593
594                                 lasteve = eve;
595                                 if (!firsteve) firsteve = eve;
596
597                                 i++;
598                         }
599
600                         BLI_addfilledge(lasteve, firsteve);
601                         totface += BLI_edgefill(0);
602
603                         BLI_end_edgefill();
604                 }
605                 
606                 me->totface = totface;
607
608                 /* new tess face block */
609                 if(totface==0) mface= NULL;
610                 else {
611                         mface= MEM_callocN(totface*sizeof(MFace), "loadeditbMesh face");
612                         facenors = MEM_callocN(totface*sizeof(float)*3, "facenors");
613                         polyindex = MEM_callocN(totface*sizeof(int), "polyindex");
614                 }
615
616                 CustomData_add_layer(&me->fdata, CD_MFACE, CD_ASSIGN, mface, me->totface);
617                 CustomData_add_layer(&me->fdata, CD_POLYINDEX, CD_ASSIGN, polyindex, me->totface);
618                 CustomData_add_layer(&me->fdata, CD_NORMAL, CD_ASSIGN, facenors, me->totface);
619                 CustomData_from_bmeshpoly(&me->fdata, &bm->pdata, &bm->ldata, totface);
620
621                 mesh_update_customdata_pointers(me, TRUE);
622                 
623                 i = 0;
624                 BM_ITER_INDEX(f, &iter, bm, BM_FACES_OF_MESH, NULL, j) {
625                         EditVert *eve, *lasteve = NULL, *firsteve = NULL;
626                         EditFace *efa;
627                         BMLoop *ls[3];
628                         
629                         BLI_begin_edgefill();
630                         BM_ITER(l, &liter, bm, BM_LOOPS_OF_FACE, f) {
631                                 eve = BLI_addfillvert(l->v->co);
632                                 eve->tmp.p = l;
633
634                                 if (lasteve) {
635                                         BLI_addfilledge(lasteve, eve);
636                                 }
637
638                                 lasteve = eve;
639                                 if (!firsteve) firsteve = eve;
640                         }
641
642                         BLI_addfilledge(lasteve, firsteve);
643                         BLI_edgefill(0);
644
645                         for (efa=fillfacebase.first; efa; efa=efa->next) {
646                                 ls[0] = efa->v1->tmp.p;
647                                 ls[1] = efa->v2->tmp.p;
648                                 ls[2] = efa->v3->tmp.p;
649                                 
650                                 /*ensure correct winding.  I believe this is
651                                   analogous to bubble sort on three elements.*/
652                                 if (BM_GetIndex(ls[0]) > BM_GetIndex(ls[1])) {
653                                         SWAP(BMLoop*, ls[0], ls[1]);
654                                 }
655                                 if (BM_GetIndex(ls[1]) > BM_GetIndex(ls[2])) {
656                                         SWAP(BMLoop*, ls[1], ls[2]);
657                                 }
658                                 if (BM_GetIndex(ls[0]) > BM_GetIndex(ls[1])) {
659                                         SWAP(BMLoop*, ls[0], ls[1]);
660                                 }
661
662                                 mface->mat_nr = f->mat_nr;
663                                 mface->flag = BM_Face_Flag_To_MEFlag(f);
664                                 
665                                 mface->v1 = BM_GetIndex(ls[0]->v);
666                                 mface->v2 = BM_GetIndex(ls[1]->v);
667                                 mface->v3 = BM_GetIndex(ls[2]->v);
668
669                                 test_index_face(mface, &me->fdata, i, 1);
670                                 
671                                 loops_to_corners(bm, me, i, f, ls, numTex, numCol);
672                                 copy_v3_v3(facenors, ls[0]->f->no);
673
674                                 *polyindex = j;
675
676                                 mface++;
677                                 facenors += 3;
678                                 polyindex++;
679                                 i++;
680                         }
681                         BLI_end_edgefill();
682                 }
683         }
684
685         i = 0;
686         j = 0;
687         BM_ITER(f, &iter, bm, BM_FACES_OF_MESH, NULL) {
688                 mpoly->loopstart = j;
689                 mpoly->totloop = f->len;
690                 mpoly->mat_nr = f->mat_nr;
691                 mpoly->flag = BM_Face_Flag_To_MEFlag(f);
692
693                 l = BMIter_New(&liter, bm, BM_LOOPS_OF_FACE, f);
694                 for ( ; l; l=BMIter_Step(&liter), j++, mloop++) {
695                         mloop->e = BM_GetIndex(l->e);
696                         mloop->v = BM_GetIndex(l->v);
697
698                         /*copy over customdata*/
699                         CustomData_from_bmesh_block(&bm->ldata, &me->ldata, l->head.data, j);
700                         BM_CHECK_ELEMENT(bm, l);
701                         BM_CHECK_ELEMENT(bm, l->e);
702                         BM_CHECK_ELEMENT(bm, l->v);
703                 }
704                 
705                 if (f == bm->act_face) me->act_face = i;
706
707                 /*copy over customdata*/
708                 CustomData_from_bmesh_block(&bm->pdata, &me->pdata, f->head.data, i);
709
710                 i++;
711                 mpoly++;
712                 BM_CHECK_ELEMENT(bm, f);
713         }
714
715         /* patch hook indices and vertex parents */
716         if (ototvert > 0) {
717                 Object *ob;
718                 ModifierData *md;
719                 BMVert **vertMap = NULL;
720                 int i,j;
721
722                 for (ob=G.main->object.first; ob; ob=ob->id.next) {
723                         if (ob->parent==bm->ob && ELEM(ob->partype, PARVERT1,PARVERT3)) {
724
725                                 if (vertMap == NULL) {
726                                         vertMap= bmesh_to_mesh_vertex_map(bm, ototvert);
727                                 }
728
729                                 if(ob->par1 < ototvert) {
730                                         eve = vertMap[ob->par1];
731                                         if(eve) ob->par1= BM_GetIndex(eve);
732                                 }
733                                 if(ob->par2 < ototvert) {
734                                         eve = vertMap[ob->par2];
735                                         if(eve) ob->par2= BM_GetIndex(eve);
736                                 }
737                                 if(ob->par3 < ototvert) {
738                                         eve = vertMap[ob->par3];
739                                         if(eve) ob->par3= BM_GetIndex(eve);
740                                 }
741                                 
742                         }
743                         if (ob->data==me) {
744                                 for (md=ob->modifiers.first; md; md=md->next) {
745                                         if (md->type==eModifierType_Hook) {
746                                                 HookModifierData *hmd = (HookModifierData*) md;
747
748                                                 if (vertMap == NULL) {
749                                                         vertMap= bmesh_to_mesh_vertex_map(bm, ototvert);
750                                                 }
751                                                 
752                                                 for (i=j=0; i<hmd->totindex; i++) {
753                                                         if(hmd->indexar[i] < ototvert) {
754                                                                 eve = vertMap[hmd->indexar[i]];
755                                                                 
756                                                                 if (eve) {
757                                                                         hmd->indexar[j++] = BM_GetIndex(eve);
758                                                                 }
759                                                         }
760                                                         else j++;
761                                                 }
762
763                                                 hmd->totindex = j;
764                                         }
765                                 }
766                         }
767                 }
768
769                 if (vertMap) MEM_freeN(vertMap);
770         }
771
772         mesh_update_customdata_pointers(me, dotess);
773
774         {
775                 BMEditSelection *selected;
776                 me->totselect = BLI_countlist(&(bm->selected));
777
778                 if(me->mselect) MEM_freeN(me->mselect);
779
780                 me->mselect = MEM_callocN(sizeof(MSelect) * me->totselect, "Mesh selection history");
781
782
783                 for(i = 0, selected = bm->selected.first; selected; i++, selected = selected->next){
784                         if(selected->htype == BM_VERT){
785                                 me->mselect[i].type = ME_VSEL;
786
787                         }else if(selected->htype == BM_EDGE){
788                                 me->mselect[i].type = ME_ESEL;
789
790                         }else if(selected->htype == BM_FACE){
791                                 me->mselect[i].type = ME_FSEL;
792                         }
793
794                         me->mselect[i].index = BM_GetIndex(selected->data);
795                 }
796         }
797
798         /* see comment below, this logic is in twice */
799
800         if (me->key) {
801                 KeyBlock *currkey;
802                 KeyBlock *actkey= BLI_findlink(&me->key->block, bm->shapenr-1);
803
804                 float (*ofs)[3] = NULL;
805
806                 /*go through and find any shapekey customdata layers
807                   that might not have corrusponding KeyBlocks, and add them if
808                   necassary.*/
809                 j = 0;
810                 for (i=0; i<bm->vdata.totlayer; i++) {
811                         if (bm->vdata.layers[i].type != CD_SHAPEKEY)
812                                 continue;
813
814                         for (currkey=me->key->block.first; currkey; currkey=currkey->next) {
815                                 if (currkey->uid == bm->vdata.layers[i].uid)
816                                         break;
817                         }
818                         
819                         if (!currkey) {
820                                 currkey = MEM_callocN(sizeof(KeyBlock), "KeyBlock mesh_conv.c");
821                                 currkey->type = KEY_LINEAR;
822                                 currkey->slidermin = 0.0f;
823                                 currkey->slidermax = 1.0f;
824
825                                 BLI_addtail(&me->key->block, currkey);
826                                 me->key->totkey++;
827                         }
828
829                         j++;
830                 }
831
832
833                 /* editing the base key should update others */
834                 if(me->key->type==KEY_RELATIVE && oldverts) {
835                         int act_is_basis = 0;
836                         /* find if this key is a basis for any others */
837                         for(currkey = me->key->block.first; currkey; currkey= currkey->next) {
838                                 if(bm->shapenr-1 == currkey->relative) {
839                                         act_is_basis = 1;
840                                         break;
841                                 }
842                         }
843
844                         if(act_is_basis) { /* active key is a base */
845                                 float (*fp)[3]= actkey->data;
846                                 int *keyi;
847                                 i=0;
848                                 ofs= MEM_callocN(sizeof(float) * 3 * bm->totvert,  "currkey->data");
849                                 mvert = me->mvert;
850                                 BM_ITER(eve, &iter, bm, BM_VERTS_OF_MESH, NULL) {
851                                         keyi = CustomData_bmesh_get(&bm->vdata, eve->head.data, CD_SHAPE_KEYINDEX);
852                                         if(keyi && *keyi != ORIGINDEX_NONE) {
853                                                 sub_v3_v3v3(ofs[i], mvert->co, fp[*keyi]);
854                                         }
855                                         i++;
856                                         mvert++;
857                                 }
858                         }
859                 }
860
861
862                 for (currkey=me->key->block.first; currkey; currkey=currkey->next) {
863                         j = 0;
864
865                         for (i=0; i<bm->vdata.totlayer; i++) {
866                                 if (bm->vdata.layers[i].type != CD_SHAPEKEY)
867                                         continue;
868
869                                 if (currkey->uid == bm->vdata.layers[i].uid) {
870                                         int apply_offset = (ofs && (currkey != actkey) && (bm->shapenr-1 == currkey->relative));
871                                         float *fp, *co;
872                                         float (*ofs_pt)[3] = ofs;
873
874                                         if (currkey->data)
875                                                 MEM_freeN(currkey->data);
876                                         currkey->data = fp = MEM_mallocN(sizeof(float)*3*bm->totvert, "shape key data");
877                                         currkey->totelem = bm->totvert;
878
879                                         BM_ITER(eve, &iter, bm, BM_VERTS_OF_MESH, NULL) {
880                                                 co = currkey==actkey ? eve->co : CustomData_bmesh_get_n(&bm->vdata, eve->head.data, CD_SHAPEKEY, j);
881                                                 
882                                                 copy_v3_v3(fp, co);
883
884                                                 /* propagate edited basis offsets to other shapes */
885                                                 if(apply_offset) {
886                                                         add_v3_v3(fp, *ofs_pt++);
887                                                 }
888
889                                                 fp += 3;
890                                         }
891                                         break;
892                                 }
893
894                                 j++;
895                         }
896
897                         /*if we didn't find a shapekey, tag the block to be reconstructed
898                           via the old method below*/
899                         if (j == CustomData_number_of_layers(&bm->vdata, CD_SHAPEKEY)) {
900                                 currkey->flag |= KEYBLOCK_MISSING;
901                         }
902                 }
903
904                 if(ofs) MEM_freeN(ofs);
905         }
906
907         /* XXX, code below is from trunk and a duplicate functionality
908          * to the block above.
909          * We should use one or the other, having both means we have to maintain
910          * both and keep them working the same way which is a hassle - campbell */
911
912         /* old method of reconstructing keys via vertice's original key indices,
913            currently used if the new method above fails (which is theoretically
914            possible in certain cases of undo).*/
915         if(me->key) {
916                 float *fp, *newkey, *oldkey;
917                 KeyBlock *currkey;
918                 KeyBlock *actkey= BLI_findlink(&me->key->block, bm->shapenr-1);
919
920                 float (*ofs)[3] = NULL;
921
922                 /* editing the base key should update others */
923                 if(me->key->type==KEY_RELATIVE && oldverts) {
924                         int act_is_basis = 0;
925                         /* find if this key is a basis for any others */
926                         for(currkey = me->key->block.first; currkey; currkey= currkey->next) {
927                                 if(bm->shapenr-1 == currkey->relative) {
928                                         act_is_basis = 1;
929                                         break;
930                                 }
931                         }
932
933                         if(act_is_basis) { /* active key is a base */
934                                 float (*fp)[3]= actkey->data;
935                                 int *keyi;
936                                 i=0;
937                                 ofs= MEM_callocN(sizeof(float) * 3 * bm->totvert,  "currkey->data");
938                                 mvert = me->mvert;
939                                 BM_ITER(eve, &iter, bm, BM_VERTS_OF_MESH, NULL) {
940                                         keyi = CustomData_bmesh_get(&bm->vdata, eve->head.data, CD_SHAPE_KEYINDEX);
941                                         if(keyi && *keyi != ORIGINDEX_NONE) {
942                                                 sub_v3_v3v3(ofs[i], mvert->co, fp[*keyi]);
943                                         }
944                                         i++;
945                                         mvert++;
946                                 }
947                         }
948                 }
949
950                 /* Lets reorder the key data so that things line up roughly
951                  * with the way things were before editmode */
952                 currkey = me->key->block.first;
953                 while(currkey) {
954                         int apply_offset = (ofs && (currkey != actkey) && (bm->shapenr-1 == currkey->relative));
955
956                         if (!(currkey->flag & KEYBLOCK_MISSING)) {
957                                 currkey = currkey->next;
958                                 continue;
959                         }
960                         
961                         printf("warning: had to hackishly reconstruct shape key \"%s\","
962                                " it may not be correct anymore.\n", currkey->name);
963
964                         currkey->flag &= ~KEYBLOCK_MISSING;
965
966                         fp= newkey= MEM_callocN(me->key->elemsize*bm->totvert,  "currkey->data");
967                         oldkey = currkey->data;
968
969                         eve= BMIter_New(&iter, bm, BM_VERTS_OF_MESH, NULL);
970
971                         i = 0;
972                         mvert = me->mvert;
973                         while(eve) {
974                                 keyi = CustomData_bmesh_get(&bm->vdata, eve->head.data, CD_SHAPE_KEYINDEX);
975                                 if (!keyi) {
976                                         break;
977                                 }
978                                 if (*keyi >= 0 && *keyi < currkey->totelem) { // valid old vertex
979                                         if(currkey == actkey) {
980                                                 if(actkey == me->key->refkey) {
981                                                         copy_v3_v3(fp, mvert->co);
982                                                 }
983                                                 else {
984                                                         copy_v3_v3(fp, mvert->co);
985                                                         if(oldverts) {
986                                                                 copy_v3_v3(mvert->co, oldverts[*keyi].co);
987                                                         }
988                                                 }
989                                         }
990                                         else {
991                                                 if(oldkey) {
992                                                         copy_v3_v3(fp, oldkey + 3 * *keyi);
993                                                 }
994                                         }
995                                 }
996                                 else {
997                                         copy_v3_v3(fp, mvert->co);
998                                 }
999
1000                                 /* propagate edited basis offsets to other shapes */
1001                                 if(apply_offset) {
1002                                         add_v3_v3(fp, ofs[i]);
1003                                 }
1004
1005                                 fp+= 3;
1006                                 ++i;
1007                                 ++mvert;
1008                                 eve= BMIter_Step(&iter);
1009                         }
1010                         currkey->totelem= bm->totvert;
1011                         if(currkey->data) MEM_freeN(currkey->data);
1012                         currkey->data = newkey;
1013                         
1014                         currkey= currkey->next;
1015                 }
1016
1017                 if(ofs) MEM_freeN(ofs);
1018         }
1019
1020         if(oldverts) MEM_freeN(oldverts);
1021 }