Cleanup: replace short with boolean for zero area array

Also remove redundant fabsf on the area of a quad/tri &
reduce indentation using continue in for loop.
This commit is contained in:
Campbell Barton 2021-08-06 01:42:01 +10:00
parent ff2265f0a9
commit 04c24bec07
2 changed files with 172 additions and 168 deletions

View File

@ -44,7 +44,7 @@ struct BLaplacianSystem {
int numEdges; /* Number of edges. */
int numFaces; /* Number of faces. */
int numVerts; /* Number of verts. */
short *zerola; /* Is zero area or length. */
bool *zerola; /* Is zero area or length. */
/* Pointers to data. */
BMesh *bm;
@ -98,7 +98,7 @@ static void memset_laplacian_system(LaplacianSystem *sys, int val)
memset(sys->ring_areas, val, sizeof(float) * sys->numVerts);
memset(sys->vlengths, val, sizeof(float) * sys->numVerts);
memset(sys->vweights, val, sizeof(float) * sys->numVerts);
memset(sys->zerola, val, sizeof(short) * sys->numVerts);
memset(sys->zerola, val, sizeof(bool) * sys->numVerts);
}
static LaplacianSystem *init_laplacian_system(int a_numEdges, int a_numFaces, int a_numVerts)
@ -139,7 +139,7 @@ static LaplacianSystem *init_laplacian_system(int a_numEdges, int a_numFaces, in
return NULL;
}
sys->zerola = MEM_callocN(sizeof(short) * sys->numVerts, "ModLaplSmoothZeloa");
sys->zerola = MEM_callocN(sizeof(bool) * sys->numVerts, "ModLaplSmoothZeloa");
if (!sys->zerola) {
delete_laplacian_system(sys);
return NULL;
@ -181,104 +181,107 @@ static void init_laplacian_matrix(LaplacianSystem *sys)
BMVert *vf[4];
BM_ITER_MESH_INDEX (e, &eiter, sys->bm, BM_EDGES_OF_MESH, i) {
if (!BM_elem_flag_test(e, BM_ELEM_SELECT) && BM_edge_is_boundary(e)) {
v1 = e->v1->co;
v2 = e->v2->co;
idv1 = BM_elem_index_get(e->v1);
idv2 = BM_elem_index_get(e->v2);
if (BM_elem_flag_test(e, BM_ELEM_SELECT) || !BM_edge_is_boundary(e)) {
continue;
}
w1 = len_v3v3(v1, v2);
if (w1 > sys->min_area) {
w1 = 1.0f / w1;
sys->eweights[i] = w1;
sys->vlengths[idv1] += w1;
sys->vlengths[idv2] += w1;
}
else {
sys->zerola[idv1] = 1;
sys->zerola[idv2] = 1;
}
v1 = e->v1->co;
v2 = e->v2->co;
idv1 = BM_elem_index_get(e->v1);
idv2 = BM_elem_index_get(e->v2);
w1 = len_v3v3(v1, v2);
if (w1 > sys->min_area) {
w1 = 1.0f / w1;
sys->eweights[i] = w1;
sys->vlengths[idv1] += w1;
sys->vlengths[idv2] += w1;
}
else {
sys->zerola[idv1] = true;
sys->zerola[idv2] = true;
}
}
BM_ITER_MESH_INDEX (f, &fiter, sys->bm, BM_FACES_OF_MESH, i) {
if (BM_elem_flag_test(f, BM_ELEM_SELECT)) {
if (!BM_elem_flag_test(f, BM_ELEM_SELECT)) {
continue;
}
BM_ITER_ELEM_INDEX (vn, &vi, f, BM_VERTS_OF_FACE, j) {
vf[j] = vn;
}
has_4_vert = (j == 4) ? 1 : 0;
idv1 = BM_elem_index_get(vf[0]);
idv2 = BM_elem_index_get(vf[1]);
idv3 = BM_elem_index_get(vf[2]);
idv4 = has_4_vert ? BM_elem_index_get(vf[3]) : 0;
BM_ITER_ELEM_INDEX (vn, &vi, f, BM_VERTS_OF_FACE, j) {
vf[j] = vn;
}
has_4_vert = (j == 4) ? 1 : 0;
idv1 = BM_elem_index_get(vf[0]);
idv2 = BM_elem_index_get(vf[1]);
idv3 = BM_elem_index_get(vf[2]);
idv4 = has_4_vert ? BM_elem_index_get(vf[3]) : 0;
v1 = vf[0]->co;
v2 = vf[1]->co;
v3 = vf[2]->co;
v4 = has_4_vert ? vf[3]->co : NULL;
v1 = vf[0]->co;
v2 = vf[1]->co;
v3 = vf[2]->co;
v4 = has_4_vert ? vf[3]->co : NULL;
if (has_4_vert) {
areaf = area_quad_v3(v1, v2, v3, v4);
}
else {
areaf = area_tri_v3(v1, v2, v3);
}
if (areaf < sys->min_area) {
sys->zerola[idv1] = true;
sys->zerola[idv2] = true;
sys->zerola[idv3] = true;
if (has_4_vert) {
areaf = area_quad_v3(v1, v2, v3, v4);
sys->zerola[idv4] = true;
}
else {
areaf = area_tri_v3(v1, v2, v3);
}
sys->ring_areas[idv1] += areaf;
sys->ring_areas[idv2] += areaf;
sys->ring_areas[idv3] += areaf;
if (has_4_vert) {
sys->ring_areas[idv4] += areaf;
}
if (has_4_vert) {
idv[0] = idv1;
idv[1] = idv2;
idv[2] = idv3;
idv[3] = idv4;
for (j = 0; j < 4; j++) {
idv1 = idv[j];
idv2 = idv[(j + 1) % 4];
idv3 = idv[(j + 2) % 4];
idv4 = idv[(j + 3) % 4];
v1 = vf[j]->co;
v2 = vf[(j + 1) % 4]->co;
v3 = vf[(j + 2) % 4]->co;
v4 = vf[(j + 3) % 4]->co;
w2 = cotangent_tri_weight_v3(v4, v1, v2) + cotangent_tri_weight_v3(v3, v1, v2);
w3 = cotangent_tri_weight_v3(v2, v3, v1) + cotangent_tri_weight_v3(v4, v1, v3);
w4 = cotangent_tri_weight_v3(v2, v4, v1) + cotangent_tri_weight_v3(v3, v4, v1);
sys->vweights[idv1] += (w2 + w3 + w4) / 4.0f;
}
}
else {
w1 = cotangent_tri_weight_v3(v1, v2, v3);
w2 = cotangent_tri_weight_v3(v2, v3, v1);
w3 = cotangent_tri_weight_v3(v3, v1, v2);
if (fabsf(areaf) < sys->min_area) {
sys->zerola[idv1] = 1;
sys->zerola[idv2] = 1;
sys->zerola[idv3] = 1;
if (has_4_vert) {
sys->zerola[idv4] = 1;
}
}
sys->fweights[i][0] += w1;
sys->fweights[i][1] += w2;
sys->fweights[i][2] += w3;
sys->ring_areas[idv1] += areaf;
sys->ring_areas[idv2] += areaf;
sys->ring_areas[idv3] += areaf;
if (has_4_vert) {
sys->ring_areas[idv4] += areaf;
}
if (has_4_vert) {
idv[0] = idv1;
idv[1] = idv2;
idv[2] = idv3;
idv[3] = idv4;
for (j = 0; j < 4; j++) {
idv1 = idv[j];
idv2 = idv[(j + 1) % 4];
idv3 = idv[(j + 2) % 4];
idv4 = idv[(j + 3) % 4];
v1 = vf[j]->co;
v2 = vf[(j + 1) % 4]->co;
v3 = vf[(j + 2) % 4]->co;
v4 = vf[(j + 3) % 4]->co;
w2 = cotangent_tri_weight_v3(v4, v1, v2) + cotangent_tri_weight_v3(v3, v1, v2);
w3 = cotangent_tri_weight_v3(v2, v3, v1) + cotangent_tri_weight_v3(v4, v1, v3);
w4 = cotangent_tri_weight_v3(v2, v4, v1) + cotangent_tri_weight_v3(v3, v4, v1);
sys->vweights[idv1] += (w2 + w3 + w4) / 4.0f;
}
}
else {
w1 = cotangent_tri_weight_v3(v1, v2, v3);
w2 = cotangent_tri_weight_v3(v2, v3, v1);
w3 = cotangent_tri_weight_v3(v3, v1, v2);
sys->fweights[i][0] += w1;
sys->fweights[i][1] += w2;
sys->fweights[i][2] += w3;
sys->vweights[idv1] += w2 + w3;
sys->vweights[idv2] += w1 + w3;
sys->vweights[idv3] += w1 + w2;
}
sys->vweights[idv1] += w2 + w3;
sys->vweights[idv2] += w1 + w3;
sys->vweights[idv3] += w1 + w2;
}
}
}
@ -300,80 +303,81 @@ static void fill_laplacian_matrix(LaplacianSystem *sys)
BMVert *vf[4];
BM_ITER_MESH_INDEX (f, &fiter, sys->bm, BM_FACES_OF_MESH, i) {
if (BM_elem_flag_test(f, BM_ELEM_SELECT)) {
BM_ITER_ELEM_INDEX (vn, &vi, f, BM_VERTS_OF_FACE, j) {
vf[j] = vn;
}
has_4_vert = (j == 4) ? 1 : 0;
if (has_4_vert) {
idv[0] = BM_elem_index_get(vf[0]);
idv[1] = BM_elem_index_get(vf[1]);
idv[2] = BM_elem_index_get(vf[2]);
idv[3] = BM_elem_index_get(vf[3]);
for (j = 0; j < 4; j++) {
idv1 = idv[j];
idv2 = idv[(j + 1) % 4];
idv3 = idv[(j + 2) % 4];
idv4 = idv[(j + 3) % 4];
if (!BM_elem_flag_test(f, BM_ELEM_SELECT)) {
continue;
}
v1 = vf[j]->co;
v2 = vf[(j + 1) % 4]->co;
v3 = vf[(j + 2) % 4]->co;
v4 = vf[(j + 3) % 4]->co;
BM_ITER_ELEM_INDEX (vn, &vi, f, BM_VERTS_OF_FACE, j) {
vf[j] = vn;
}
has_4_vert = (j == 4) ? 1 : 0;
if (has_4_vert) {
idv[0] = BM_elem_index_get(vf[0]);
idv[1] = BM_elem_index_get(vf[1]);
idv[2] = BM_elem_index_get(vf[2]);
idv[3] = BM_elem_index_get(vf[3]);
for (j = 0; j < 4; j++) {
idv1 = idv[j];
idv2 = idv[(j + 1) % 4];
idv3 = idv[(j + 2) % 4];
idv4 = idv[(j + 3) % 4];
w2 = cotangent_tri_weight_v3(v4, v1, v2) + cotangent_tri_weight_v3(v3, v1, v2);
w3 = cotangent_tri_weight_v3(v2, v3, v1) + cotangent_tri_weight_v3(v4, v1, v3);
w4 = cotangent_tri_weight_v3(v2, v4, v1) + cotangent_tri_weight_v3(v3, v4, v1);
v1 = vf[j]->co;
v2 = vf[(j + 1) % 4]->co;
v3 = vf[(j + 2) % 4]->co;
v4 = vf[(j + 3) % 4]->co;
w2 = w2 / 4.0f;
w3 = w3 / 4.0f;
w4 = w4 / 4.0f;
w2 = cotangent_tri_weight_v3(v4, v1, v2) + cotangent_tri_weight_v3(v3, v1, v2);
w3 = cotangent_tri_weight_v3(v2, v3, v1) + cotangent_tri_weight_v3(v4, v1, v3);
w4 = cotangent_tri_weight_v3(v2, v4, v1) + cotangent_tri_weight_v3(v3, v4, v1);
if (!vert_is_boundary(vf[j]) && sys->zerola[idv1] == 0) {
EIG_linear_solver_matrix_add(sys->context, idv1, idv2, w2 * sys->vweights[idv1]);
EIG_linear_solver_matrix_add(sys->context, idv1, idv3, w3 * sys->vweights[idv1]);
EIG_linear_solver_matrix_add(sys->context, idv1, idv4, w4 * sys->vweights[idv1]);
}
w2 = w2 / 4.0f;
w3 = w3 / 4.0f;
w4 = w4 / 4.0f;
if (!vert_is_boundary(vf[j]) && sys->zerola[idv1] == false) {
EIG_linear_solver_matrix_add(sys->context, idv1, idv2, w2 * sys->vweights[idv1]);
EIG_linear_solver_matrix_add(sys->context, idv1, idv3, w3 * sys->vweights[idv1]);
EIG_linear_solver_matrix_add(sys->context, idv1, idv4, w4 * sys->vweights[idv1]);
}
}
else {
idv1 = BM_elem_index_get(vf[0]);
idv2 = BM_elem_index_get(vf[1]);
idv3 = BM_elem_index_get(vf[2]);
/* Is ring if number of faces == number of edges around vertice. */
if (!vert_is_boundary(vf[0]) && sys->zerola[idv1] == 0) {
EIG_linear_solver_matrix_add(
sys->context, idv1, idv2, sys->fweights[i][2] * sys->vweights[idv1]);
EIG_linear_solver_matrix_add(
sys->context, idv1, idv3, sys->fweights[i][1] * sys->vweights[idv1]);
}
if (!vert_is_boundary(vf[1]) && sys->zerola[idv2] == 0) {
EIG_linear_solver_matrix_add(
sys->context, idv2, idv1, sys->fweights[i][2] * sys->vweights[idv2]);
EIG_linear_solver_matrix_add(
sys->context, idv2, idv3, sys->fweights[i][0] * sys->vweights[idv2]);
}
if (!vert_is_boundary(vf[2]) && sys->zerola[idv3] == 0) {
EIG_linear_solver_matrix_add(
sys->context, idv3, idv1, sys->fweights[i][1] * sys->vweights[idv3]);
EIG_linear_solver_matrix_add(
sys->context, idv3, idv2, sys->fweights[i][0] * sys->vweights[idv3]);
}
}
else {
idv1 = BM_elem_index_get(vf[0]);
idv2 = BM_elem_index_get(vf[1]);
idv3 = BM_elem_index_get(vf[2]);
/* Is ring if number of faces == number of edges around vertice. */
if (!vert_is_boundary(vf[0]) && sys->zerola[idv1] == false) {
EIG_linear_solver_matrix_add(
sys->context, idv1, idv2, sys->fweights[i][2] * sys->vweights[idv1]);
EIG_linear_solver_matrix_add(
sys->context, idv1, idv3, sys->fweights[i][1] * sys->vweights[idv1]);
}
if (!vert_is_boundary(vf[1]) && sys->zerola[idv2] == false) {
EIG_linear_solver_matrix_add(
sys->context, idv2, idv1, sys->fweights[i][2] * sys->vweights[idv2]);
EIG_linear_solver_matrix_add(
sys->context, idv2, idv3, sys->fweights[i][0] * sys->vweights[idv2]);
}
if (!vert_is_boundary(vf[2]) && sys->zerola[idv3] == false) {
EIG_linear_solver_matrix_add(
sys->context, idv3, idv1, sys->fweights[i][1] * sys->vweights[idv3]);
EIG_linear_solver_matrix_add(
sys->context, idv3, idv2, sys->fweights[i][0] * sys->vweights[idv3]);
}
}
}
BM_ITER_MESH_INDEX (e, &eiter, sys->bm, BM_EDGES_OF_MESH, i) {
if (!BM_elem_flag_test(e, BM_ELEM_SELECT) && BM_edge_is_boundary(e)) {
v1 = e->v1->co;
v2 = e->v2->co;
idv1 = BM_elem_index_get(e->v1);
idv2 = BM_elem_index_get(e->v2);
if (sys->zerola[idv1] == 0 && sys->zerola[idv2] == 0) {
EIG_linear_solver_matrix_add(
sys->context, idv1, idv2, sys->eweights[i] * sys->vlengths[idv1]);
EIG_linear_solver_matrix_add(
sys->context, idv2, idv1, sys->eweights[i] * sys->vlengths[idv2]);
}
if (BM_elem_flag_test(e, BM_ELEM_SELECT) || !BM_edge_is_boundary(e)) {
continue;
}
idv1 = BM_elem_index_get(e->v1);
idv2 = BM_elem_index_get(e->v2);
if (sys->zerola[idv1] == false && sys->zerola[idv2] == false) {
EIG_linear_solver_matrix_add(
sys->context, idv1, idv2, sys->eweights[i] * sys->vlengths[idv1]);
EIG_linear_solver_matrix_add(
sys->context, idv2, idv1, sys->eweights[i] * sys->vlengths[idv2]);
}
}
}
@ -448,8 +452,8 @@ static void validate_solution(
lene = len_v3v3(ve1, ve2);
if (lene > leni * SMOOTH_LAPLACIAN_MAX_EDGE_PERCENTAGE ||
lene < leni * SMOOTH_LAPLACIAN_MIN_EDGE_PERCENTAGE) {
sys->zerola[idv1] = 1;
sys->zerola[idv2] = 1;
sys->zerola[idv1] = true;
sys->zerola[idv2] = true;
}
}
@ -458,7 +462,7 @@ static void validate_solution(
}
BMO_ITER (v, &siter, sys->op->slots_in, "verts", BM_VERT) {
m_vertex_id = BM_elem_index_get(v);
if (sys->zerola[m_vertex_id] == 0) {
if (sys->zerola[m_vertex_id] == false) {
if (usex) {
v->co[0] = EIG_linear_solver_variable_get(sys->context, 0, m_vertex_id);
}
@ -528,7 +532,7 @@ void bmo_smooth_laplacian_vert_exec(BMesh *bm, BMOperator *op)
EIG_linear_solver_right_hand_side_add(sys->context, 1, m_vertex_id, v->co[1]);
EIG_linear_solver_right_hand_side_add(sys->context, 2, m_vertex_id, v->co[2]);
i = m_vertex_id;
if ((sys->zerola[i] == 0) &&
if ((sys->zerola[i] == false) &&
/* Non zero check is to account for vertices that aren't connected to a selected face.
* Without this wire edges become `nan`, see T89214. */
(sys->ring_areas[i] != 0.0f)) {

View File

@ -66,7 +66,7 @@ struct BLaplacianSystem {
int numVerts; /* Number of verts. */
short *numNeFa; /* Number of neighbors faces around vertice. */
short *numNeEd; /* Number of neighbors Edges around vertice. */
short *zerola; /* Is zero area or length. */
bool *zerola; /* Is zero area or length. */
/* Pointers to data. */
float (*vertexCos)[3];
@ -130,7 +130,7 @@ static void memset_laplacian_system(LaplacianSystem *sys, int val)
memset(sys->ring_areas, val, sizeof(float) * sys->numVerts);
memset(sys->vlengths, val, sizeof(float) * sys->numVerts);
memset(sys->vweights, val, sizeof(float) * sys->numVerts);
memset(sys->zerola, val, sizeof(short) * sys->numVerts);
memset(sys->zerola, val, sizeof(bool) * sys->numVerts);
}
static LaplacianSystem *init_laplacian_system(int a_numEdges,
@ -152,7 +152,7 @@ static LaplacianSystem *init_laplacian_system(int a_numEdges,
sys->ring_areas = MEM_calloc_arrayN(sys->numVerts, sizeof(float), __func__);
sys->vlengths = MEM_calloc_arrayN(sys->numVerts, sizeof(float), __func__);
sys->vweights = MEM_calloc_arrayN(sys->numVerts, sizeof(float), __func__);
sys->zerola = MEM_calloc_arrayN(sys->numVerts, sizeof(short), __func__);
sys->zerola = MEM_calloc_arrayN(sys->numVerts, sizeof(bool), __func__);
return sys;
}
@ -225,8 +225,8 @@ static void init_laplacian_matrix(LaplacianSystem *sys)
sys->numNeEd[idv2] = sys->numNeEd[idv2] + 1;
w1 = len_v3v3(v1, v2);
if (w1 < sys->min_area) {
sys->zerola[idv1] = 1;
sys->zerola[idv2] = 1;
sys->zerola[idv1] = true;
sys->zerola[idv2] = true;
}
else {
w1 = 1.0f / w1;
@ -253,7 +253,7 @@ static void init_laplacian_matrix(LaplacianSystem *sys)
areaf = area_tri_v3(v_prev, v_curr, v_next);
if (areaf < sys->min_area) {
sys->zerola[l_curr->v] = 1;
sys->zerola[l_curr->v] = true;
}
sys->ring_areas[l_prev->v] += areaf;
@ -300,7 +300,7 @@ static void fill_laplacian_matrix(LaplacianSystem *sys)
const uint l_curr_index = l_curr - sys->mloop;
/* Is ring if number of faces == number of edges around vertice. */
if (sys->numNeEd[l_curr->v] == sys->numNeFa[l_curr->v] && sys->zerola[l_curr->v] == 0) {
if (sys->numNeEd[l_curr->v] == sys->numNeFa[l_curr->v] && sys->zerola[l_curr->v] == false) {
EIG_linear_solver_matrix_add(sys->context,
l_curr->v,
l_next->v,
@ -310,7 +310,7 @@ static void fill_laplacian_matrix(LaplacianSystem *sys)
l_prev->v,
sys->fweights[l_curr_index][1] * sys->vweights[l_curr->v]);
}
if (sys->numNeEd[l_next->v] == sys->numNeFa[l_next->v] && sys->zerola[l_next->v] == 0) {
if (sys->numNeEd[l_next->v] == sys->numNeFa[l_next->v] && sys->zerola[l_next->v] == false) {
EIG_linear_solver_matrix_add(sys->context,
l_next->v,
l_curr->v,
@ -320,7 +320,7 @@ static void fill_laplacian_matrix(LaplacianSystem *sys)
l_prev->v,
sys->fweights[l_curr_index][0] * sys->vweights[l_next->v]);
}
if (sys->numNeEd[l_prev->v] == sys->numNeFa[l_prev->v] && sys->zerola[l_prev->v] == 0) {
if (sys->numNeEd[l_prev->v] == sys->numNeFa[l_prev->v] && sys->zerola[l_prev->v] == false) {
EIG_linear_solver_matrix_add(sys->context,
l_prev->v,
l_curr->v,
@ -338,7 +338,7 @@ static void fill_laplacian_matrix(LaplacianSystem *sys)
idv2 = sys->medges[i].v2;
/* Is boundary */
if (sys->numNeEd[idv1] != sys->numNeFa[idv1] && sys->numNeEd[idv2] != sys->numNeFa[idv2] &&
sys->zerola[idv1] == 0 && sys->zerola[idv2] == 0) {
sys->zerola[idv1] == false && sys->zerola[idv2] == false) {
EIG_linear_solver_matrix_add(
sys->context, idv1, idv2, sys->eweights[i] * sys->vlengths[idv1]);
EIG_linear_solver_matrix_add(
@ -358,7 +358,7 @@ static void validate_solution(LaplacianSystem *sys, short flag, float lambda, fl
sys->vert_centroid, sys->vertexCos, sys->mpoly, sys->numPolys, sys->mloop);
}
for (i = 0; i < sys->numVerts; i++) {
if (sys->zerola[i] == 0) {
if (sys->zerola[i] == false) {
lam = sys->numNeEd[i] == sys->numNeFa[i] ? (lambda >= 0.0f ? 1.0f : -1.0f) :
(lambda_border >= 0.0f ? 1.0f : -1.0f);
if (flag & MOD_LAPLACIANSMOOTH_X) {
@ -442,7 +442,7 @@ static void laplaciansmoothModifier_do(
wpaint = 1.0f;
}
if (sys->zerola[i] == 0) {
if (sys->zerola[i] == false) {
if (smd->flag & MOD_LAPLACIANSMOOTH_NORMALIZED) {
w = sys->vweights[i];
sys->vweights[i] = (w == 0.0f) ? 0.0f : -fabsf(smd->lambda) * wpaint / w;