99
99
#include "intern/bmesh_private.h" /* for element checking */
102
void BM_mesh_bm_from_me(BMesh *bm, Mesh *me, int set_key, int act_key_nr)
101
void BM_mesh_cd_flag_ensure(BMesh *bm, Mesh *mesh, const char cd_flag)
103
const char cd_flag_all = BM_mesh_cd_flag_from_bmesh(bm) | cd_flag;
104
BM_mesh_cd_flag_apply(bm, cd_flag_all);
106
mesh->cd_flag = cd_flag_all;
110
void BM_mesh_cd_flag_apply(BMesh *bm, const char cd_flag)
112
/* CustomData_bmesh_init_pool() must run first */
113
BLI_assert(bm->vdata.totlayer == 0 || bm->vdata.pool != NULL);
114
BLI_assert(bm->edata.totlayer == 0 || bm->edata.pool != NULL);
116
if (cd_flag & ME_CDFLAG_VERT_BWEIGHT) {
117
if (!CustomData_has_layer(&bm->vdata, CD_BWEIGHT)) {
118
BM_data_layer_add(bm, &bm->vdata, CD_BWEIGHT);
122
if (CustomData_has_layer(&bm->vdata, CD_BWEIGHT)) {
123
BM_data_layer_free(bm, &bm->vdata, CD_BWEIGHT);
127
if (cd_flag & ME_CDFLAG_EDGE_BWEIGHT) {
128
if (!CustomData_has_layer(&bm->edata, CD_BWEIGHT)) {
129
BM_data_layer_add(bm, &bm->edata, CD_BWEIGHT);
133
if (CustomData_has_layer(&bm->edata, CD_BWEIGHT)) {
134
BM_data_layer_free(bm, &bm->edata, CD_BWEIGHT);
138
if (cd_flag & ME_CDFLAG_EDGE_CREASE) {
139
if (!CustomData_has_layer(&bm->edata, CD_CREASE)) {
140
BM_data_layer_add(bm, &bm->edata, CD_CREASE);
144
if (CustomData_has_layer(&bm->edata, CD_CREASE)) {
145
BM_data_layer_free(bm, &bm->edata, CD_CREASE);
150
char BM_mesh_cd_flag_from_bmesh(BMesh *bm)
153
if (CustomData_has_layer(&bm->vdata, CD_BWEIGHT)) {
154
cd_flag |= ME_CDFLAG_VERT_BWEIGHT;
156
if (CustomData_has_layer(&bm->edata, CD_BWEIGHT)) {
157
cd_flag |= ME_CDFLAG_EDGE_BWEIGHT;
159
if (CustomData_has_layer(&bm->edata, CD_CREASE)) {
160
cd_flag |= ME_CDFLAG_EDGE_CREASE;
166
* \brief Mesh -> BMesh
168
* \warning This function doesn't calculate face normals.
170
void BM_mesh_bm_from_me(BMesh *bm, Mesh *me, bool set_key, int act_key_nr)
105
173
BLI_array_declare(verts);
152
223
CustomData_set_layer_name(&bm->ldata, CD_MLOOPUV, i, bm->pdata.layers[li].name);
155
if (!CustomData_has_layer(&bm->edata, CD_CREASE))
156
CustomData_add_layer(&bm->edata, CD_CREASE, CD_ASSIGN, NULL, 0);
158
if (!CustomData_has_layer(&bm->edata, CD_BWEIGHT))
159
CustomData_add_layer(&bm->edata, CD_BWEIGHT, CD_ASSIGN, NULL, 0);
161
if (!CustomData_has_layer(&bm->vdata, CD_BWEIGHT))
162
CustomData_add_layer(&bm->vdata, CD_BWEIGHT, CD_ASSIGN, NULL, 0);
164
226
if ((act_key_nr != 0) && (me->key != NULL)) {
165
227
actkey = BLI_findlink(&me->key->block, act_key_nr - 1);
205
267
CustomData_bmesh_init_pool(&bm->ldata, me->totloop, BM_LOOP);
206
268
CustomData_bmesh_init_pool(&bm->pdata, me->totpoly, BM_FACE);
270
BM_mesh_cd_flag_apply(bm, me->cd_flag);
272
cd_vert_bweight_offset = CustomData_get_offset(&bm->vdata, CD_BWEIGHT);
273
cd_edge_bweight_offset = CustomData_get_offset(&bm->edata, CD_BWEIGHT);
274
cd_edge_crease_offset = CustomData_get_offset(&bm->edata, CD_CREASE);
208
276
for (i = 0, mvert = me->mvert; i < me->totvert; i++, mvert++) {
209
v = BM_vert_create(bm, keyco && set_key ? keyco[i] : mvert->co, NULL);
277
v = BM_vert_create(bm, keyco && set_key ? keyco[i] : mvert->co, NULL, BM_CREATE_SKIP_CD);
210
278
BM_elem_index_set(v, i); /* set_ok */
216
284
/* this is necessary for selection counts to work properly */
217
285
if (mvert->flag & SELECT) {
218
BM_vert_select_set(bm, v, TRUE);
286
BM_vert_select_set(bm, v, true);
221
289
normal_short_to_float_v3(v->no, mvert->no);
223
BM_elem_float_data_set(&bm->vdata, v, CD_BWEIGHT, (float)mvert->bweight / 255.0f);
291
/* Copy Custom Data */
292
CustomData_to_bmesh_block(&me->vdata, &bm->vdata, i, &v->head.data, true);
225
/* Copy Custom Dat */
226
CustomData_to_bmesh_block(&me->vdata, &bm->vdata, i, &v->head.data);
294
if (cd_vert_bweight_offset != -1) BM_ELEM_CD_SET_FLOAT(v, cd_vert_bweight_offset, (float)mvert->bweight / 255.0f);
228
296
/* set shapekey data */
264
332
/* this is necessary for selection counts to work properly */
265
333
if (medge->flag & SELECT) {
266
BM_edge_select_set(bm, e, TRUE);
334
BM_edge_select_set(bm, e, true);
269
337
/* Copy Custom Data */
270
CustomData_to_bmesh_block(&me->edata, &bm->edata, i, &e->head.data);
272
BM_elem_float_data_set(&bm->edata, e, CD_CREASE, (float)medge->crease / 255.0f);
273
BM_elem_float_data_set(&bm->edata, e, CD_BWEIGHT, (float)medge->bweight / 255.0f);
338
CustomData_to_bmesh_block(&me->edata, &bm->edata, i, &e->head.data, true);
340
if (cd_edge_bweight_offset != -1) BM_ELEM_CD_SET_FLOAT(e, cd_edge_bweight_offset, (float)medge->bweight / 255.0f);
341
if (cd_edge_crease_offset != -1) BM_ELEM_CD_SET_FLOAT(e, cd_edge_crease_offset, (float)medge->crease / 255.0f);
276
345
bm->elem_index_dirty &= ~BM_EDGE; /* added in order, clear dirty flag */
278
347
mpoly = me->mpoly;
279
348
for (i = 0; i < me->totpoly; i++, mpoly++) {
282
352
BLI_array_empty(fedges);
283
353
BLI_array_empty(verts);
285
BLI_array_growitems(fedges, mpoly->totloop);
286
BLI_array_growitems(verts, mpoly->totloop);
355
BLI_array_grow_items(fedges, mpoly->totloop);
356
BLI_array_grow_items(verts, mpoly->totloop);
288
358
for (j = 0; j < mpoly->totloop; j++) {
289
359
ml = &me->mloop[mpoly->loopstart + j];
330
400
/* this is necessary for selection counts to work properly */
331
401
if (mpoly->flag & ME_FACE_SEL) {
332
BM_face_select_set(bm, f, TRUE);
402
BM_face_select_set(bm, f, true);
335
405
f->mat_nr = mpoly->mat_nr;
336
406
if (i == me->act_face) bm->act_face = f;
339
BM_ITER_ELEM_INDEX (l, &iter, f, BM_LOOPS_OF_FACE, j) {
408
j = mpoly->loopstart;
409
l_iter = l_first = BM_FACE_FIRST_LOOP(f);
340
411
/* Save index of correspsonding MLoop */
341
BM_elem_index_set(l, mpoly->loopstart + j); /* set_loop */
412
CustomData_to_bmesh_block(&me->ldata, &bm->ldata, j++, &l_iter->head.data, true);
413
} while ((l_iter = l_iter->next) != l_first);
344
415
/* Copy Custom Data */
345
CustomData_to_bmesh_block(&me->pdata, &bm->pdata, i, &f->head.data);
416
CustomData_to_bmesh_block(&me->pdata, &bm->pdata, i, &f->head.data, true);
348
419
bm->elem_index_dirty &= ~BM_FACE; /* added in order, clear dirty flag */
354
/* Copy over loop CustomData. Doing this in a separate loop isn't necessary
355
* but is an optimization, to avoid copying a bunch of interpolated customdata
356
* for each BMLoop (from previous BMLoops using the same edge), always followed
357
* by freeing the interpolated data and overwriting it with data from the Mesh. */
358
BM_ITER_MESH (f, &fiter, bm, BM_FACES_OF_MESH) {
359
BM_ITER_ELEM (l, &liter, f, BM_LOOPS_OF_FACE) {
360
int li = BM_elem_index_get(l);
361
CustomData_to_bmesh_block(&me->ldata, &bm->ldata, li, &l->head.data);
362
BM_elem_index_set(l, 0); /* set_loop */
367
421
if (me->mselect && me->totselect != 0) {
369
BMVert **vert_array = MEM_callocN(sizeof(BMVert *) * bm->totvert,
370
"Selection Conversion Vertex Pointer Array");
371
BMEdge **edge_array = MEM_callocN(sizeof(BMEdge *) * bm->totedge,
372
"Selection Conversion Edge Pointer Array");
373
BMFace **face_array = MEM_callocN(sizeof(BMFace *) * bm->totface,
374
"Selection Conversion Face Pointer Array");
423
BMVert **vert_array = MEM_mallocN(sizeof(BMVert *) * bm->totvert, "VSelConv");
424
BMEdge **edge_array = MEM_mallocN(sizeof(BMEdge *) * bm->totedge, "ESelConv");
425
BMFace **face_array = MEM_mallocN(sizeof(BMFace *) * bm->totface, "FSelConv");
382
BM_ITER_MESH_INDEX (vert, &iter, bm, BM_VERTS_OF_MESH, i) { vert_array[i] = vert; }
383
BM_ITER_MESH_INDEX (edge, &iter, bm, BM_EDGES_OF_MESH, i) { edge_array[i] = edge; }
384
BM_ITER_MESH_INDEX (face, &iter, bm, BM_FACES_OF_MESH, i) { face_array[i] = face; }
428
#pragma omp parallel sections if (bm->totvert + bm->totedge + bm->totface >= BM_OMP_LIMIT)
431
{ BM_iter_as_array(bm, BM_VERTS_OF_MESH, NULL, (void **)vert_array, bm->totvert); }
433
{ BM_iter_as_array(bm, BM_EDGES_OF_MESH, NULL, (void **)edge_array, bm->totedge); }
435
{ BM_iter_as_array(bm, BM_FACES_OF_MESH, NULL, (void **)face_array, bm->totface); }
386
438
for (i = 0, msel = me->mselect; i < me->totselect; i++, msel++) {
387
439
switch (msel->type) {
562
625
CustomData_add_layer(&me->ldata, CD_MLOOP, CD_ASSIGN, mloop, me->totloop);
563
626
CustomData_add_layer(&me->pdata, CD_MPOLY, CD_ASSIGN, mpoly, me->totpoly);
628
me->cd_flag = BM_mesh_cd_flag_from_bmesh(bm);
565
630
/* this is called again, 'dotess' arg is used there */
566
631
mesh_update_customdata_pointers(me, 0);
569
634
BM_ITER_MESH (v, &iter, bm, BM_VERTS_OF_MESH) {
570
float *bweight = CustomData_bmesh_get(&bm->vdata, v->head.data, CD_BWEIGHT);
572
mvert->bweight = bweight ? (char)((*bweight) * 255) : 0;
574
635
copy_v3_v3(mvert->co, v->co);
575
636
normal_float_to_short_v3(mvert->no, v->no);
593
656
BM_ITER_MESH (e, &iter, bm, BM_EDGES_OF_MESH) {
594
float *crease = CustomData_bmesh_get(&bm->edata, e->head.data, CD_CREASE);
595
float *bweight = CustomData_bmesh_get(&bm->edata, e->head.data, CD_BWEIGHT);
597
657
med->v1 = BM_elem_index_get(e->v1);
598
658
med->v2 = BM_elem_index_get(e->v2);
599
med->crease = crease ? (char)((*crease) * 255) : 0;
600
med->bweight = bweight ? (char)((*bweight) * 255) : 0;
602
660
med->flag = BM_edge_flag_to_mflag(e);
766
827
/* editing the base key should update others */
767
828
if ((me->key->type == KEY_RELATIVE) && /* only need offsets for relative shape keys */
768
(actkey != NULL) && /* unlikely, but the active key may not be valid if the
829
(actkey != NULL) && /* unlikely, but the active key may not be valid if the
769
830
* bmesh and the mesh are out of sync */
770
831
(oldverts != NULL)) /* not used here, but 'oldverts' is used later for applying 'ofs' */
772
int act_is_basis = FALSE;
833
bool act_is_basis = false;
774
835
/* find if this key is a basis for any others */
775
836
for (currkey = me->key->block.first; currkey; currkey = currkey->next) {
776
837
if (bm->shapenr - 1 == currkey->relative) {