Style Cleanup: whitespace and some formatting.
[blender.git] / source / blender / bmesh / operators / mesh_conv.c
1 /*
2  * ***** BEGIN GPL LICENSE BLOCK *****
3  *
4  * This program is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU General Public License
6  * as published by the Free Software Foundation; either version 2
7  * of the License, or (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software Foundation,
16  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17  *
18  * Contributor(s): Joseph Eagar.
19  *
20  * ***** END GPL LICENSE BLOCK *****
21  */
22
23 #include <string.h>
24
25 #include "MEM_guardedalloc.h"
26
27 #include "DNA_listBase.h"
28 #include "DNA_customdata_types.h"
29 #include "DNA_mesh_types.h"
30 #include "DNA_meshdata_types.h"
31 #include "DNA_modifier_types.h"
32 #include "DNA_key_types.h"
33 #include "DNA_object_types.h"
34 #include "DNA_scene_types.h"
35
36 #include "BKE_customdata.h"
37 #include "BKE_mesh.h"
38 #include "BKE_global.h"
39 #include "BKE_DerivedMesh.h"
40 #include "BKE_cdderivedmesh.h"
41 #include "BKE_key.h"
42 #include "BKE_main.h"
43
44 #include "BLI_utildefines.h"
45 #include "BLI_listbase.h"
46 #include "BLI_math.h"
47 #include "BLI_edgehash.h"
48 #include "BLI_editVert.h"
49 #include "BLI_scanfill.h"
50 #include "BLI_array.h"
51 #include "BLI_utildefines.h"
52
53 #include "ED_mesh.h"
54
55 #include "mesh_intern.h"
56 #include "bmesh.h"
57 #include "bmesh_private.h"
58
59 /*
60  * MESH CONV.C
61  *
62  * This file contains functions
63  * for converting a Mesh
64  * into a Bmesh, and back again.
65  *
66  */
67
68 void mesh_to_bmesh_exec(BMesh *bm, BMOperator *op)
69 {
70         Object *ob = BMO_Get_Pnt(op, "object");
71         Mesh *me = BMO_Get_Pnt(op, "mesh");
72         MVert *mvert;
73         BLI_array_declare(verts);
74         MEdge *medge;
75         MLoop *ml;
76         MPoly *mpoly;
77         KeyBlock *actkey, *block;
78         BMVert *v, **vt = NULL, **verts = NULL;
79         BMEdge *e, **fedges = NULL, **et = NULL;
80         BMFace *f;
81         BMLoop *l;
82         BLI_array_declare(fedges);
83         float (*keyco)[3] = NULL;
84         int *keyi;
85         int set_key = BMO_Get_Int(op, "set_shapekey");
86         int totuv, i, j, allocsize[4] = {512, 512, 2048, 512};
87
88         if (!me || !me->totvert) {
89                 return; /* sanity check */
90         }
91         
92         vt = MEM_mallocN(sizeof(void **) * me->totvert, "mesh to bmesh vtable");
93
94         CustomData_copy(&me->vdata, &bm->vdata, CD_MASK_BMESH, CD_CALLOC, 0);
95         CustomData_copy(&me->edata, &bm->edata, CD_MASK_BMESH, CD_CALLOC, 0);
96         CustomData_copy(&me->ldata, &bm->ldata, CD_MASK_BMESH, CD_CALLOC, 0);
97         CustomData_copy(&me->pdata, &bm->pdata, CD_MASK_BMESH, CD_CALLOC, 0);
98         
99         /* make sure uv layer names are consisten */
100         totuv = CustomData_number_of_layers(&bm->pdata, CD_MTEXPOLY);
101         for (i = 0; i < totuv; i++) {
102                 int li = CustomData_get_layer_index_n(&bm->pdata, CD_MTEXPOLY, i);
103                 CustomData_set_layer_name(&bm->ldata, CD_MLOOPUV, i, bm->pdata.layers[li].name);
104         }
105         
106         if (!CustomData_has_layer(&bm->edata, CD_CREASE))
107                 CustomData_add_layer(&bm->edata, CD_CREASE, CD_ASSIGN, NULL, 0);
108
109         if (!CustomData_has_layer(&bm->edata, CD_BWEIGHT))
110                 CustomData_add_layer(&bm->edata, CD_BWEIGHT, CD_ASSIGN, NULL, 0);
111
112         if (!CustomData_has_layer(&bm->vdata, CD_BWEIGHT))
113                 CustomData_add_layer(&bm->vdata, CD_BWEIGHT, CD_ASSIGN, NULL, 0);
114
115
116         if (me->key && ob->shapenr > me->key->totkey) {
117                 ob->shapenr = me->key->totkey - 1;
118         }
119
120         actkey = ob_get_keyblock(ob);
121         if (actkey && actkey->totelem == me->totvert) {
122                 CustomData_add_layer(&bm->vdata, CD_SHAPE_KEYINDEX, CD_ASSIGN, NULL, 0);
123                 
124                 /* check if we need to generate unique ids for the shapekeys.
125                  * this also exists in the file reading code, but is here for
126                  * a sanity chec */
127                 if (!me->key->uidgen) {
128                         fprintf(stderr,
129                                 "%s had to generate shape key uid's in a situation we shouldn't need to! "
130                                 "(bmesh internal error)\n",
131                                 __func__);
132
133                         me->key->uidgen = 1;
134                         for (block = me->key->block.first; block; block = block->next) {
135                                 block->uid = me->key->uidgen++;
136                         }
137                 }
138
139                 keyco = actkey->data;
140                 bm->shapenr = ob->shapenr;
141                 for (i = 0, block = me->key->block.first; block; block = block->next, i++) {
142                         CustomData_add_layer_named(&bm->vdata, CD_SHAPEKEY,
143                                                    CD_ASSIGN, NULL, 0, block->name);
144                         
145                         j = CustomData_get_layer_index_n(&bm->vdata, CD_SHAPEKEY, i);
146                         bm->vdata.layers[j].uid = block->uid;
147                 }
148         }
149         else if (actkey) {
150                 printf("shapekey<->mesh mismatch!\n");
151         }
152         
153         CustomData_bmesh_init_pool(&bm->vdata, allocsize[0]);
154         CustomData_bmesh_init_pool(&bm->edata, allocsize[1]);
155         CustomData_bmesh_init_pool(&bm->ldata, allocsize[2]);
156         CustomData_bmesh_init_pool(&bm->pdata, allocsize[3]);
157
158         for (i = 0, mvert = me->mvert; i < me->totvert; i++, mvert++) {
159                 v = BM_Make_Vert(bm, keyco && set_key ? keyco[i] : mvert->co, NULL);
160                 BM_SetIndex(v, i); /* set_ok */
161                 vt[i] = v;
162
163                 /* transfer flag */
164                 v->head.hflag = BM_Vert_Flag_From_MEFlag(mvert->flag);
165
166                 /* this is necassary for selection counts to work properl */
167                 if (BM_TestHFlag(v, BM_SELECT)) BM_Select_Vert(bm, v, TRUE);
168
169                 normal_short_to_float_v3(v->no, mvert->no);
170
171                 BM_SetCDf(&bm->vdata, v, CD_BWEIGHT, (float)mvert->bweight / 255.0f);
172
173                 /* Copy Custom Dat */
174                 CustomData_to_bmesh_block(&me->vdata, &bm->vdata, i, &v->head.data);
175
176                 /* set shapekey dat */
177                 if (me->key) {
178                         /* set shape key original inde */
179                         keyi = CustomData_bmesh_get(&bm->vdata, v->head.data, CD_SHAPE_KEYINDEX);
180                         if (keyi) {
181                                 *keyi = i;
182                         }
183                         
184                         for (block = me->key->block.first, j = 0; block; block = block->next, j++) {
185                                 float *co = CustomData_bmesh_get_n(&bm->vdata, v->head.data, CD_SHAPEKEY, j);
186
187                                 if (co) {
188                                         copy_v3_v3(co, ((float *)block->data) + 3 * i);
189                                 }
190                         }
191                 }
192         }
193
194         bm->elem_index_dirty &= ~BM_VERT; /* added in order, clear dirty flag */
195
196         if (!me->totedge) {
197                 MEM_freeN(vt);
198                 return;
199         }
200
201         et = MEM_mallocN(sizeof(void **) * me->totedge, "mesh to bmesh etable");
202
203         medge = me->medge;
204         for (i = 0; i < me->totedge; i++, medge++) {
205                 e = BM_Make_Edge(bm, vt[medge->v1], vt[medge->v2], NULL, 0);
206                 BM_SetIndex(e, i); /* set_ok */
207                 et[i] = e;
208
209                 /* transfer flags */
210                 e->head.hflag = BM_Edge_Flag_From_MEFlag(medge->flag);
211
212                 /* this is necassary for selection counts to work properly */
213                 if (BM_TestHFlag(e, BM_SELECT)) BM_Select(bm, e, TRUE);
214                 
215                 /* Copy Custom Dat */
216                 CustomData_to_bmesh_block(&me->edata, &bm->edata, i, &e->head.data);
217                 
218                 BM_SetCDf(&bm->edata, e, CD_CREASE, (float)medge->crease / 255.0f);
219                 BM_SetCDf(&bm->edata, e, CD_BWEIGHT, (float)medge->bweight / 255.0f);
220         }
221
222         bm->elem_index_dirty &= ~BM_EDGE; /* added in order, clear dirty flag */
223         
224         if (!me->totpoly) {
225                 MEM_freeN(vt);
226                 MEM_freeN(et);
227                 return;
228         }
229
230         mpoly = me->mpoly;
231         for (i = 0; i < me->totpoly; i++, mpoly++) {
232                 BMIter iter;
233
234                 BLI_array_empty(fedges);
235                 BLI_array_empty(verts);
236
237                 BLI_array_growitems(fedges, mpoly->totloop);
238                 BLI_array_growitems(verts, mpoly->totloop);
239
240                 for (j = 0; j < mpoly->totloop; j++) {
241                         ml = &me->mloop[mpoly->loopstart + j];
242                         v = vt[ml->v];
243                         e = et[ml->e];
244
245                         fedges[j] = e;
246                         verts[j] = v;
247                 }
248                 
249                 /* not sure what this block is supposed to do,
250                  * but its unused. so commenting - campbell */
251 #if 0
252                 {
253                         BMVert *v1, *v2;
254                         v1 = vt[me->mloop[mpoly->loopstart].v];
255                         v2 = vt[me->mloop[mpoly->loopstart + 1].v];
256
257                         if (v1 == fedges[0]->v1) {
258                                 v2 = fedges[0]->v2;
259                         }
260                         else {
261                                 v1 = fedges[0]->v2;
262                                 v2 = fedges[0]->v1;
263                         }
264                 }
265 #endif
266
267                 f = BM_Make_Face(bm, verts, fedges, mpoly->totloop, 0);
268
269                 if (!f) {
270                         printf("%s: Warning! Bad face in mesh"
271                                " \"%s\" at index %d!, skipping\n",
272                                __func__, me->id.name + 2, i);
273                         continue;
274                 }
275
276                 /* dont use 'i' since we may have skipped the face */
277                 BM_SetIndex(f, bm->totface - 1); /* set_ok */
278
279                 /* transfer flag */
280                 f->head.hflag = BM_Face_Flag_From_MEFlag(mpoly->flag);
281
282                 /* this is necassary for selection counts to work properl */
283                 if (BM_TestHFlag(f, BM_SELECT)) BM_Select(bm, f, TRUE);
284
285                 f->mat_nr = mpoly->mat_nr;
286                 if (i == me->act_face) bm->act_face = f;
287
288                 j = 0;
289                 BM_ITER_INDEX(l, &iter, bm, BM_LOOPS_OF_FACE, f, j) {
290                         /* Save index of correspsonding MLoop */
291                         BM_SetIndex(l, mpoly->loopstart + j); /* set_loop */
292                 }
293
294                 /* Copy Custom Dat */
295                 CustomData_to_bmesh_block(&me->pdata, &bm->pdata, i, &f->head.data);
296         }
297
298         bm->elem_index_dirty &= ~BM_FACE; /* added in order, clear dirty flag */
299
300         {
301                 BMIter fiter;
302                 BMIter liter;
303                 
304                 /* Copy over loop CustomData. Doing this in a separate loop isn't necessary
305                  * but is an optimization, to avoid copying a bunch of interpolated customdata
306                  * for each BMLoop (from previous BMLoops using the same edge), always followed
307                  * by freeing the interpolated data and overwriting it with data from the Mesh. */
308                 BM_ITER(f, &fiter, bm, BM_FACES_OF_MESH, NULL) {
309                         BM_ITER(l, &liter, bm, BM_LOOPS_OF_FACE, f) {
310                                 int li = BM_GetIndex(l);
311                                 CustomData_to_bmesh_block(&me->ldata, &bm->ldata, li, &l->head.data);
312                                 BM_SetIndex(l, 0); /* set_loop */
313                         }
314                 }
315         }
316
317         if (me->mselect && me->totselect != 0) {
318                 BMIter iter;
319                 BMVert *vertex;
320                 BMEdge *edge;
321                 BMFace *face;
322                 BMVert **vertex_array = MEM_callocN(sizeof(BMVert *) * bm->totvert,
323                                                   "Selection Conversion Vertex Pointer Array");
324                 BMEdge **edge_array = MEM_callocN(sizeof(BMEdge *) * bm->totedge,
325                                                 "Selection Conversion Edge Pointer Array");
326                 BMFace **face_array = MEM_callocN(sizeof(BMFace *) * bm->totface,
327                                                 "Selection Conversion Face Pointer Array");
328
329                 for (i = 0, vertex = BMIter_New(&iter, bm, BM_VERTS_OF_MESH, NULL);
330                     vertex; i++, vertex = BMIter_Step(&iter))
331                 {
332                         vertex_array[i] = vertex;
333                 }
334
335                 for (i = 0, edge = BMIter_New(&iter, bm, BM_EDGES_OF_MESH, NULL);
336                     edge; i++, edge = BMIter_Step(&iter))
337                 {
338                         edge_array[i] = edge;
339                 }
340
341                 for (i = 0, face = BMIter_New(&iter, bm, BM_FACES_OF_MESH, NULL);
342                     face; i++, face = BMIter_Step(&iter))
343                 {
344                         face_array[i] = face;
345                 }
346
347                 if (me->mselect) {
348                         for (i = 0; i < me->totselect; i++) {
349                                 if (me->mselect[i].type == ME_VSEL) {
350                                         BM_store_selection(bm, vertex_array[me->mselect[i].index]);
351                                 }
352                                 else if (me->mselect[i].type == ME_ESEL) {
353                                         BM_store_selection(bm, edge_array[me->mselect[i].index]);
354                                 }
355                                 else if (me->mselect[i].type == ME_FSEL) {
356                                         BM_store_selection(bm, face_array[me->mselect[i].index]);
357                                 }
358                         }
359                 }
360                 else {
361                         me->totselect = 0;
362                 }
363
364                 MEM_freeN(vertex_array);
365                 MEM_freeN(edge_array);
366                 MEM_freeN(face_array);
367         }
368         else {
369                 me->totselect = 0;
370                 if (me->mselect) {
371                         MEM_freeN(me->mselect);
372                         me->mselect = NULL;
373                 }
374         }
375
376         BLI_array_free(fedges);
377         BLI_array_free(verts);
378         
379         MEM_freeN(vt);
380         MEM_freeN(et);
381 }
382
383 void object_load_bmesh_exec(BMesh *bm, BMOperator *op)
384 {
385         Object *ob = BMO_Get_Pnt(op, "object");
386         /* Scene *scene = BMO_Get_Pnt(op, "scene"); */
387         Mesh *me = ob->data;
388
389         BMO_CallOpf(bm, "bmesh_to_mesh mesh=%p object=%p notesselation=%i", me, ob, TRUE);
390 }
391
392
393 static BMVert **bmesh_to_mesh_vertex_map(BMesh *bm, int ototvert)
394 {
395         BMVert **vertMap = NULL;
396         BMVert *eve;
397         int index;
398         int i = 0;
399         BMIter iter;
400
401         /* caller needs to ensure this */
402         BLI_assert(ototvert > 0);
403
404         vertMap = MEM_callocN(sizeof(*vertMap)*ototvert, "vertMap");
405         if (CustomData_has_layer(&bm->vdata, CD_SHAPE_KEYINDEX)) {
406                 int *keyi;
407                 BM_ITER(eve, &iter, bm, BM_VERTS_OF_MESH, NULL) {
408                         keyi = CustomData_bmesh_get(&bm->vdata, eve->head.data, CD_SHAPE_KEYINDEX);
409                         if (keyi) {
410                                 if (((index = *keyi) != ORIGINDEX_NONE) && (index < ototvert)) {
411                                         vertMap[index] = eve;
412                                 }
413                         }
414                         else {
415                                 if (i < ototvert) {
416                                         vertMap[i] = eve;
417                                 }
418                         }
419                         i++;
420                 }
421         }
422         else {
423                 BM_ITER(eve, &iter, bm, BM_VERTS_OF_MESH, NULL) {
424                         if (i < ototvert) {
425                                 vertMap[i] = eve;
426                         }
427                         else {
428                                 break;
429                         }
430                         i++;
431                 }
432         }
433
434         return vertMap;
435 }
436
437 BM_INLINE void bmesh_quick_edgedraw_flag(MEdge *med, BMEdge *e)
438 {
439         /* this is a cheap way to set the edge draw, its not precise and will
440          * pick the first 2 faces an edge uses */
441
442
443         if ( /* (med->flag & ME_EDGEDRAW) && */ /* assume to be true */
444              (e->l && (e->l != e->l->radial_next)) &&
445              (dot_v3v3(e->l->f->no, e->l->radial_next->f->no) > 0.998f))
446         {
447                 med->flag &= ~ME_EDGEDRAW;
448         }
449 }
450
451
452 void bmesh_to_mesh_exec(BMesh *bm, BMOperator *op)
453 {
454         Mesh *me = BMO_Get_Pnt(op, "mesh");
455         /* Object *ob = BMO_Get_Pnt(op, "object"); */
456         MLoop *mloop;
457         MPoly *mpoly;
458         MVert *mvert, *oldverts;
459         MEdge *med, *medge;
460         BMVert *v, *eve;
461         BMEdge *e;
462         BMLoop *l;
463         BMFace *f;
464         BMIter iter, liter;
465         int i, j, *keyi, ototvert, totloop;
466         int dotess = !BMO_Get_Int(op, "notesselation");
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 dat */
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 allocat */
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         /* this is called again, 'dotess' arg is used there */
527         mesh_update_customdata_pointers(me, 0);
528         
529         i = 0;
530         BM_ITER(v, &iter, bm, BM_VERTS_OF_MESH, NULL) {
531                 float *bweight = CustomData_bmesh_get(&bm->vdata, v->head.data, CD_BWEIGHT);
532
533                 mvert->bweight = bweight ? (char)((*bweight) * 255) : 0;
534
535                 copy_v3_v3(mvert->co, v->co);
536                 normal_float_to_short_v3(mvert->no, v->no);
537                 
538                 mvert->flag = BM_Vert_Flag_To_MEFlag(v);
539
540                 BM_SetIndex(v, i); /* set_inline */
541
542                 /* copy over customdat */
543                 CustomData_from_bmesh_block(&bm->vdata, &me->vdata, v->head.data, i);
544
545                 i++;
546                 mvert++;
547
548                 BM_CHECK_ELEMENT(bm, v);
549         }
550         bm->elem_index_dirty &= ~BM_VERT;
551
552         med = medge;
553         i = 0;
554         BM_ITER(e, &iter, bm, BM_EDGES_OF_MESH, NULL) {
555                 float *crease = CustomData_bmesh_get(&bm->edata, e->head.data, CD_CREASE);
556                 float *bweight = CustomData_bmesh_get(&bm->edata, e->head.data, CD_BWEIGHT);
557                 
558                 med->v1 = BM_GetIndex(e->v1);
559                 med->v2 = BM_GetIndex(e->v2);
560                 med->crease = crease ? (char)((*crease) * 255) : 0;
561                 med->bweight = bweight ? (char)((*bweight) * 255) : 0;
562                 
563                 med->flag = BM_Edge_Flag_To_MEFlag(e);
564                 
565                 BM_SetIndex(e, i); /* set_inline */
566
567                 /* copy over customdat */
568                 CustomData_from_bmesh_block(&bm->edata, &me->edata, e->head.data, i);
569
570                 bmesh_quick_edgedraw_flag(med, e);
571
572                 i++;
573                 med++;
574                 BM_CHECK_ELEMENT(bm, e);
575         }
576         bm->elem_index_dirty &= ~BM_EDGE;
577
578         i = 0;
579         j = 0;
580         BM_ITER(f, &iter, bm, BM_FACES_OF_MESH, NULL) {
581                 mpoly->loopstart = j;
582                 mpoly->totloop = f->len;
583                 mpoly->mat_nr = f->mat_nr;
584                 mpoly->flag = BM_Face_Flag_To_MEFlag(f);
585
586                 l = BMIter_New(&liter, bm, BM_LOOPS_OF_FACE, f);
587                 for ( ; l; l = BMIter_Step(&liter), j++, mloop++) {
588                         mloop->e = BM_GetIndex(l->e);
589                         mloop->v = BM_GetIndex(l->v);
590
591                         /* copy over customdat */
592                         CustomData_from_bmesh_block(&bm->ldata, &me->ldata, l->head.data, j);
593                         BM_CHECK_ELEMENT(bm, l);
594                         BM_CHECK_ELEMENT(bm, l->e);
595                         BM_CHECK_ELEMENT(bm, l->v);
596                 }
597                 
598                 if (f == bm->act_face) me->act_face = i;
599
600                 /* copy over customdat */
601                 CustomData_from_bmesh_block(&bm->pdata, &me->pdata, f->head.data, i);
602
603                 i++;
604                 mpoly++;
605                 BM_CHECK_ELEMENT(bm, f);
606         }
607
608         /* patch hook indices and vertex parents */
609         if (ototvert > 0) {
610                 Object *ob;
611                 ModifierData *md;
612                 BMVert **vertMap = NULL;
613                 int i, j;
614
615                 for (ob = G.main->object.first; ob; ob = ob->id.next) {
616                         if (ob->parent == bm->ob && ELEM(ob->partype, PARVERT1, PARVERT3)) {
617
618                                 if (vertMap == NULL) {
619                                         vertMap = bmesh_to_mesh_vertex_map(bm, ototvert);
620                                 }
621
622                                 if (ob->par1 < ototvert) {
623                                         eve = vertMap[ob->par1];
624                                         if (eve) ob->par1 = BM_GetIndex(eve);
625                                 }
626                                 if (ob->par2 < ototvert) {
627                                         eve = vertMap[ob->par2];
628                                         if (eve) ob->par2 = BM_GetIndex(eve);
629                                 }
630                                 if (ob->par3 < ototvert) {
631                                         eve = vertMap[ob->par3];
632                                         if (eve) ob->par3 = BM_GetIndex(eve);
633                                 }
634                                 
635                         }
636                         if (ob->data == me) {
637                                 for (md = ob->modifiers.first; md; md = md->next) {
638                                         if (md->type == eModifierType_Hook) {
639                                                 HookModifierData *hmd = (HookModifierData *) md;
640
641                                                 if (vertMap == NULL) {
642                                                         vertMap = bmesh_to_mesh_vertex_map(bm, ototvert);
643                                                 }
644                                                 
645                                                 for (i = j = 0; i < hmd->totindex; i++) {
646                                                         if (hmd->indexar[i] < ototvert) {
647                                                                 eve = vertMap[hmd->indexar[i]];
648                                                                 
649                                                                 if (eve) {
650                                                                         hmd->indexar[j++] = BM_GetIndex(eve);
651                                                                 }
652                                                         }
653                                                         else j++;
654                                                 }
655
656                                                 hmd->totindex = j;
657                                         }
658                                 }
659                         }
660                 }
661
662                 if (vertMap) MEM_freeN(vertMap);
663         }
664
665         if (dotess) {
666                 BKE_mesh_calc_tessface(me);
667         }
668
669         mesh_update_customdata_pointers(me, dotess);
670
671         {
672                 BMEditSelection *selected;
673                 me->totselect = BLI_countlist(&(bm->selected));
674
675                 if (me->mselect) MEM_freeN(me->mselect);
676
677                 me->mselect = MEM_callocN(sizeof(MSelect) * me->totselect, "Mesh selection history");
678
679
680                 for (i = 0, selected = bm->selected.first; selected; i++, selected = selected->next) {
681                         if (selected->htype == BM_VERT) {
682                                 me->mselect[i].type = ME_VSEL;
683
684                         }
685                         else if (selected->htype == BM_EDGE) {
686                                 me->mselect[i].type = ME_ESEL;
687
688                         }
689                         else if (selected->htype == BM_FACE) {
690                                 me->mselect[i].type = ME_FSEL;
691                         }
692
693                         me->mselect[i].index = BM_GetIndex(selected->data);
694                 }
695         }
696
697         /* see comment below, this logic is in twice */
698
699         if (me->key) {
700                 KeyBlock *currkey;
701                 KeyBlock *actkey = BLI_findlink(&me->key->block, bm->shapenr - 1);
702
703                 float (*ofs)[3] = NULL;
704
705                 /* go through and find any shapekey customdata layers
706                  * that might not have corrusponding KeyBlocks, and add them if
707                  * necassary */
708                 j = 0;
709                 for (i = 0; i < bm->vdata.totlayer; i++) {
710                         if (bm->vdata.layers[i].type != CD_SHAPEKEY)
711                                 continue;
712
713                         for (currkey = me->key->block.first; currkey; currkey = currkey->next) {
714                                 if (currkey->uid == bm->vdata.layers[i].uid)
715                                         break;
716                         }
717                         
718                         if (!currkey) {
719                                 currkey = MEM_callocN(sizeof(KeyBlock), "KeyBlock mesh_conv.c");
720                                 currkey->type = KEY_LINEAR;
721                                 currkey->slidermin = 0.0f;
722                                 currkey->slidermax = 1.0f;
723
724                                 BLI_addtail(&me->key->block, currkey);
725                                 me->key->totkey++;
726                         }
727
728                         j++;
729                 }
730
731
732                 /* editing the base key should update others */
733                 if (me->key->type == KEY_RELATIVE && oldverts) {
734                         int act_is_basis = 0;
735                         /* find if this key is a basis for any others */
736                         for (currkey = me->key->block.first; currkey; currkey = currkey->next) {
737                                 if (bm->shapenr - 1 == currkey->relative) {
738                                         act_is_basis = 1;
739                                         break;
740                                 }
741                         }
742
743                         if (act_is_basis) { /* active key is a base */
744                                 float (*fp)[3] = actkey->data;
745                                 int *keyi;
746                                 i = 0;
747                                 ofs = MEM_callocN(sizeof(float) * 3 * bm->totvert,  "currkey->data");
748                                 mvert = me->mvert;
749                                 BM_ITER(eve, &iter, bm, BM_VERTS_OF_MESH, NULL) {
750                                         keyi = CustomData_bmesh_get(&bm->vdata, eve->head.data, CD_SHAPE_KEYINDEX);
751                                         if (keyi && *keyi != ORIGINDEX_NONE) {
752                                                 sub_v3_v3v3(ofs[i], mvert->co, fp[*keyi]);
753                                         }
754                                         i++;
755                                         mvert++;
756                                 }
757                         }
758                 }
759
760
761                 for (currkey = me->key->block.first; currkey; currkey = currkey->next) {
762                         j = 0;
763
764                         for (i = 0; i < bm->vdata.totlayer; i++) {
765                                 if (bm->vdata.layers[i].type != CD_SHAPEKEY)
766                                         continue;
767
768                                 if (currkey->uid == bm->vdata.layers[i].uid) {
769                                         int apply_offset = (ofs && (currkey != actkey) && (bm->shapenr - 1 == currkey->relative));
770                                         float *fp, *co;
771                                         float (*ofs_pt)[3] = ofs;
772
773                                         if (currkey->data)
774                                                 MEM_freeN(currkey->data);
775                                         currkey->data = fp = MEM_mallocN(sizeof(float) * 3 * bm->totvert, "shape key data");
776                                         currkey->totelem = bm->totvert;
777
778                                         BM_ITER(eve, &iter, bm, BM_VERTS_OF_MESH, NULL) {
779                                                 co = currkey == actkey ? eve->co : CustomData_bmesh_get_n(&bm->vdata, eve->head.data, CD_SHAPEKEY, j);
780                                                 
781                                                 copy_v3_v3(fp, co);
782
783                                                 /* propagate edited basis offsets to other shapes */
784                                                 if (apply_offset) {
785                                                         add_v3_v3(fp, *ofs_pt++);
786                                                 }
787
788                                                 fp += 3;
789                                         }
790                                         break;
791                                 }
792
793                                 j++;
794                         }
795
796                         /* if we didn't find a shapekey, tag the block to be reconstructed
797                          * via the old method below */
798                         if (j == CustomData_number_of_layers(&bm->vdata, CD_SHAPEKEY)) {
799                                 currkey->flag |= KEYBLOCK_MISSING;
800                         }
801                 }
802
803                 if (ofs) MEM_freeN(ofs);
804         }
805
806         /* XXX, code below is from trunk and a duplicate functionality
807          * to the block above.
808          * We should use one or the other, having both means we have to maintain
809          * both and keep them working the same way which is a hassle - campbell */
810
811         /* old method of reconstructing keys via vertice's original key indices,
812          * currently used if the new method above fails (which is theoretically
813          * possible in certain cases of undo) */
814         if (me->key) {
815                 float *fp, *newkey, *oldkey;
816                 KeyBlock *currkey;
817                 KeyBlock *actkey = BLI_findlink(&me->key->block, bm->shapenr - 1);
818
819                 float (*ofs)[3] = NULL;
820
821                 /* editing the base key should update others */
822                 if (me->key->type == KEY_RELATIVE && oldverts) {
823                         int act_is_basis = 0;
824                         /* find if this key is a basis for any others */
825                         for (currkey = me->key->block.first; currkey; currkey = currkey->next) {
826                                 if (bm->shapenr - 1 == currkey->relative) {
827                                         act_is_basis = 1;
828                                         break;
829                                 }
830                         }
831
832                         if (act_is_basis) { /* active key is a base */
833                                 float (*fp)[3] = actkey->data;
834                                 int *keyi;
835                                 i = 0;
836                                 ofs = MEM_callocN(sizeof(float) * 3 * bm->totvert,  "currkey->data");
837                                 mvert = me->mvert;
838                                 BM_ITER(eve, &iter, bm, BM_VERTS_OF_MESH, NULL) {
839                                         keyi = CustomData_bmesh_get(&bm->vdata, eve->head.data, CD_SHAPE_KEYINDEX);
840                                         if (keyi && *keyi != ORIGINDEX_NONE) {
841                                                 sub_v3_v3v3(ofs[i], mvert->co, fp[*keyi]);
842                                         }
843                                         i++;
844                                         mvert++;
845                                 }
846                         }
847                 }
848
849                 /* Lets reorder the key data so that things line up roughly
850                  * with the way things were before editmode */
851                 currkey = me->key->block.first;
852                 while (currkey) {
853                         int apply_offset = (ofs && (currkey != actkey) && (bm->shapenr - 1 == currkey->relative));
854
855                         if (!(currkey->flag & KEYBLOCK_MISSING)) {
856                                 currkey = currkey->next;
857                                 continue;
858                         }
859                         
860                         printf("warning: had to hackishly reconstruct shape key \"%s\","
861                                " it may not be correct anymore.\n", currkey->name);
862
863                         currkey->flag &= ~KEYBLOCK_MISSING;
864
865                         fp = newkey = MEM_callocN(me->key->elemsize * bm->totvert,  "currkey->data");
866                         oldkey = currkey->data;
867
868                         eve = BMIter_New(&iter, bm, BM_VERTS_OF_MESH, NULL);
869
870                         i = 0;
871                         mvert = me->mvert;
872                         while (eve) {
873                                 keyi = CustomData_bmesh_get(&bm->vdata, eve->head.data, CD_SHAPE_KEYINDEX);
874                                 if (!keyi) {
875                                         break;
876                                 }
877                                 if (*keyi >= 0 && *keyi < currkey->totelem) { // valid old vertex
878                                         if (currkey == actkey) {
879                                                 if (actkey == me->key->refkey) {
880                                                         copy_v3_v3(fp, mvert->co);
881                                                 }
882                                                 else {
883                                                         copy_v3_v3(fp, mvert->co);
884                                                         if (oldverts) {
885                                                                 copy_v3_v3(mvert->co, oldverts[*keyi].co);
886                                                         }
887                                                 }
888                                         }
889                                         else {
890                                                 if (oldkey) {
891                                                         copy_v3_v3(fp, oldkey + 3 * *keyi);
892                                                 }
893                                         }
894                                 }
895                                 else {
896                                         copy_v3_v3(fp, mvert->co);
897                                 }
898
899                                 /* propagate edited basis offsets to other shapes */
900                                 if (apply_offset) {
901                                         add_v3_v3(fp, ofs[i]);
902                                 }
903
904                                 fp+= 3;
905                                 ++i;
906                                 ++mvert;
907                                 eve = BMIter_Step(&iter);
908                         }
909                         currkey->totelem = bm->totvert;
910                         if (currkey->data) MEM_freeN(currkey->data);
911                         currkey->data = newkey;
912                         
913                         currkey = currkey->next;
914                 }
915
916                 if (ofs) MEM_freeN(ofs);
917         }
918
919         if (oldverts) MEM_freeN(oldverts);
920 }