Cleanup: reduce code indentation and simplify some code flow

This commit is contained in:
Germano Cavalcante
2020-06-29 09:33:40 -03:00
parent 2476c47d75
commit 85cde12bff

View File

@@ -1070,98 +1070,94 @@ static void create_trans_vert_customdata_layer(BMVert *v,
void trans_mesh_customdata_correction_init(TransInfo *t, TransDataContainer *tc)
{
if (!(t->settings->uvcalc_flag & UVCALC_TRANSFORM_CORRECT)) {
return;
}
if (tc->custom.type.data) {
/* Custom data correction has initiated before. */
BLI_assert(tc->custom.type.free_cb == trans_mesh_customdata_free_cb);
return;
}
int i;
BMEditMesh *em = BKE_editmesh_from_object(tc->obedit);
BMesh *bm = em->bm;
bool use_origfaces;
int cd_loop_mdisp_offset;
if (bm->shapenr > 1) {
/* Don't do this at all for non-basis shape keys, too easy to
* accidentally break uv maps or vertex colors then */
/* create copies of faces for customdata projection. */
return;
}
const bool has_layer_math = CustomData_has_math(&bm->ldata);
const int cd_loop_mdisp_offset = CustomData_get_offset(&bm->ldata, CD_MDISPS);
if (!has_layer_math && (cd_loop_mdisp_offset == -1)) {
return;
}
/* create copies of faces for customdata projection */
bmesh_edit_begin(bm, BMO_OPTYPE_FLAG_UNTAN_MULTIRES);
struct GHash *origfaces = BLI_ghash_ptr_new(__func__);
struct BMesh *bm_origfaces = BM_mesh_create(&bm_mesh_allocsize_default,
&((struct BMeshCreateParams){
.use_toolflags = false,
}));
/* we need to have matching customdata */
BM_mesh_copy_init_customdata(bm_origfaces, bm, NULL);
int *layer_math_map = NULL;
int layer_math_map_len = 0;
{
const bool has_layer_math = CustomData_has_math(&bm->ldata);
cd_loop_mdisp_offset = CustomData_get_offset(&bm->ldata, CD_MDISPS);
if ((t->settings->uvcalc_flag & UVCALC_TRANSFORM_CORRECT) &&
/* don't do this at all for non-basis shape keys, too easy to
* accidentally break uv maps or vertex colors then */
(bm->shapenr <= 1) && (has_layer_math || (cd_loop_mdisp_offset != -1))) {
use_origfaces = true;
}
else {
use_origfaces = false;
cd_loop_mdisp_offset = -1;
/* TODO: We don't need `sod->layer_math_map` when there are no loops linked
* to one of the sliding vertices. */
if (has_layer_math) {
/* over alloc, only 'math' layers are indexed */
layer_math_map = MEM_mallocN(bm->ldata.totlayer * sizeof(int), __func__);
for (int i = 0; i < bm->ldata.totlayer; i++) {
if (CustomData_layer_has_math(&bm->ldata, i)) {
layer_math_map[layer_math_map_len++] = i;
}
}
BLI_assert(layer_math_map_len != 0);
}
}
if (use_origfaces) {
/* create copies of faces for customdata projection */
bmesh_edit_begin(bm, BMO_OPTYPE_FLAG_UNTAN_MULTIRES);
struct TransCustomDataLayer *tcld = MEM_mallocN(sizeof(*tcld), __func__);
int data_len = tc->data_len + tc->data_mirror_len;
struct GHash *origfaces = BLI_ghash_ptr_new(__func__);
struct BMesh *bm_origfaces = BM_mesh_create(&bm_mesh_allocsize_default,
&((struct BMeshCreateParams){
.use_toolflags = false,
}));
tcld->bm = bm;
tcld->origfaces = origfaces;
tcld->bm_origfaces = bm_origfaces;
tcld->cd_loop_mdisp_offset = cd_loop_mdisp_offset;
tcld->layer_math_map = layer_math_map;
tcld->layer_math_map_num = layer_math_map_len;
tcld->arena = BLI_memarena_new(BLI_MEMARENA_STD_BUFSIZE, __func__);
tcld->origverts = BLI_ghash_ptr_new_ex(__func__, data_len);
tcld->data = BLI_memarena_alloc(tcld->arena, data_len * sizeof(*tcld->data));
tcld->data_len = data_len;
/* we need to have matching customdata */
BM_mesh_copy_init_customdata(bm_origfaces, bm, NULL);
{
/* Setup Verts. */
struct TransCustomDataLayerVert *tcld_vert_iter = &tcld->data[0];
int *layer_math_map = NULL;
int layer_index_dst = 0;
{
/* TODO: We don't need `sod->layer_math_map` when there are no loops linked
* to one of the sliding vertices. */
if (CustomData_has_math(&bm->ldata)) {
/* over alloc, only 'math' layers are indexed */
layer_math_map = MEM_mallocN(bm->ldata.totlayer * sizeof(int), __func__);
for (i = 0; i < bm->ldata.totlayer; i++) {
if (CustomData_layer_has_math(&bm->ldata, i)) {
layer_math_map[layer_index_dst++] = i;
}
}
BLI_assert(layer_index_dst != 0);
}
}
struct TransCustomDataLayer *tcld;
tc->custom.type.data = tcld = MEM_mallocN(sizeof(*tcld), __func__);
tc->custom.type.free_cb = trans_mesh_customdata_free_cb;
tcld->bm = bm;
tcld->origfaces = origfaces;
tcld->bm_origfaces = bm_origfaces;
tcld->cd_loop_mdisp_offset = cd_loop_mdisp_offset;
tcld->layer_math_map = layer_math_map;
tcld->layer_math_map_num = layer_index_dst;
tcld->arena = BLI_memarena_new(BLI_MEMARENA_STD_BUFSIZE, __func__);
int data_len = tc->data_len + tc->data_mirror_len;
struct GHash *origverts = BLI_ghash_ptr_new_ex(__func__, data_len);
tcld->origverts = origverts;
struct TransCustomDataLayerVert *tcld_vert, *tcld_vert_iter;
tcld_vert = BLI_memarena_alloc(tcld->arena, data_len * sizeof(*tcld_vert));
tcld_vert_iter = &tcld_vert[0];
TransData *tob;
for (i = tc->data_len, tob = tc->data; i--; tob++, tcld_vert_iter++) {
TransData *tob = tc->data;
for (int i = tc->data_len; i--; tob++, tcld_vert_iter++) {
BMVert *v = tob->extra;
create_trans_vert_customdata_layer(v, tcld, tcld_vert_iter);
}
TransDataMirror *td_mirror = tc->data_mirror;
for (i = tc->data_mirror_len; i--; td_mirror++, tcld_vert_iter++) {
for (int i = tc->data_mirror_len; i--; td_mirror++, tcld_vert_iter++) {
BMVert *v = td_mirror->extra;
create_trans_vert_customdata_layer(v, tcld, tcld_vert_iter);
}
tcld->data = tcld_vert;
tcld->data_len = data_len;
}
tc->custom.type.data = tcld;
tc->custom.type.free_cb = trans_mesh_customdata_free_cb;
}
/**
@@ -1180,19 +1176,17 @@ static void trans_mesh_customdata_correction_apply_vert(struct TransCustomDataLa
BMesh *bm = tcld->bm;
BMVert *v = tcld_vert->v;
const float *co_orig_3d = tcld_vert->co_orig_3d;
struct LinkNode **cd_loop_groups = tcld_vert->cd_loop_groups;
BMIter liter;
int j, l_num;
float *loop_weights;
const bool is_moved = (len_squared_v3v3(v->co, co_orig_3d) > FLT_EPSILON);
const bool do_loop_weight = tcld->layer_math_map_num && is_moved;
const bool do_loop_mdisps = is_final && is_moved && (tcld->cd_loop_mdisp_offset != -1);
const bool do_loop_weight = is_moved && tcld->layer_math_map_num;
const float *v_proj_axis = v->no;
/* original (l->prev, l, l->next) projections for each loop ('l' remains unchanged) */
float v_proj[3][3];
if (do_loop_weight || do_loop_mdisps) {
if (do_loop_weight) {
project_plane_normalized_v3_v3v3(v_proj[1], co_orig_3d, v_proj_axis);
}
@@ -1257,6 +1251,7 @@ static void trans_mesh_customdata_correction_apply_vert(struct TransCustomDataLa
}
}
struct LinkNode **cd_loop_groups = tcld_vert->cd_loop_groups;
if (tcld->layer_math_map_num && cd_loop_groups) {
if (do_loop_weight) {
for (j = 0; j < tcld->layer_math_map_num; j++) {
@@ -1276,6 +1271,7 @@ static void trans_mesh_customdata_correction_apply_vert(struct TransCustomDataLa
* Interpolate from every other loop (not ideal)
* However values will only be taken from loops which overlap other mdisps.
* */
const bool do_loop_mdisps = is_moved && is_final && (tcld->cd_loop_mdisp_offset != -1);
if (do_loop_mdisps) {
float(*faces_center)[3] = BLI_array_alloca(faces_center, l_num);
BMLoop *l;