diff options
Diffstat (limited to 'source/blender/blenkernel/intern/mesh_validate.c')
-rw-r--r-- | source/blender/blenkernel/intern/mesh_validate.c | 154 |
1 files changed, 25 insertions, 129 deletions
diff --git a/source/blender/blenkernel/intern/mesh_validate.c b/source/blender/blenkernel/intern/mesh_validate.c index 338420641cf..b3c53df2d5f 100644 --- a/source/blender/blenkernel/intern/mesh_validate.c +++ b/source/blender/blenkernel/intern/mesh_validate.c @@ -52,6 +52,7 @@ static CLG_LogRef LOG = {"bke.mesh"}; +/* -------------------------------------------------------------------- */ /** \name Internal functions * \{ */ @@ -161,16 +162,16 @@ static int int_cmp(const void *v1, const void *v2) static int search_poly_cmp(const void *v1, const void *v2) { - const SortPoly *sp1 = v1, *sp2 = v2; - const int max_idx = sp1->numverts > sp2->numverts ? sp2->numverts : sp1->numverts; - int idx; + const SortPoly *sp1 = v1; + const SortPoly *sp2 = v2; /* Reject all invalid polys at end of list! */ if (sp1->invalid || sp2->invalid) { return sp1->invalid ? (sp2->invalid ? 0 : 1) : -1; } /* Else, sort on first non-equal verts (remember verts of valid polys are sorted). */ - for (idx = 0; idx < max_idx; idx++) { + const int max_idx = sp1->numverts > sp2->numverts ? sp2->numverts : sp1->numverts; + for (int idx = 0; idx < max_idx; idx++) { const int v1_i = sp1->verts[idx]; const int v2_i = sp2->verts[idx]; if (v1_i != v2_i) { @@ -182,7 +183,8 @@ static int search_poly_cmp(const void *v1, const void *v2) static int search_polyloop_cmp(const void *v1, const void *v2) { - const SortPoly *sp1 = v1, *sp2 = v2; + const SortPoly *sp1 = v1; + const SortPoly *sp2 = v2; /* Reject all invalid polys at end of list! */ if (sp1->invalid || sp2->invalid) { @@ -198,8 +200,10 @@ static int search_polyloop_cmp(const void *v1, const void *v2) * \{ */ #define PRINT_MSG(...) \ - if (do_verbose) \ - CLOG_INFO(&LOG, 1, __VA_ARGS__) + if (do_verbose) { \ + CLOG_INFO(&LOG, 1, __VA_ARGS__); \ + } \ + ((void)0) #define PRINT_ERR(...) \ do { \ @@ -319,6 +323,7 @@ bool BKE_mesh_validate_arrays(Mesh *mesh, if (mv->no[j] != 0) { fix_normal = false; + break; } } @@ -697,7 +702,7 @@ bool BKE_mesh_validate_arrays(Mesh *mesh, /* Test same polys. */ if ((p1_nv == p2_nv) && (memcmp(p1_v, p2_v, p1_nv * sizeof(*p1_v)) == 0)) { if (do_verbose) { - // TODO: convert list to string + /* TODO: convert list to string */ PRINT_ERR("\tPolys %u and %u use same vertices (%d", prev_sp->index, sp->index, *p1_v); for (j = 1; j < p1_nv; j++) { PRINT_ERR(", %d", p1_v[j]); @@ -989,7 +994,6 @@ bool BKE_mesh_validate_all_customdata(CustomData *vdata, { bool is_valid = true; bool is_change_v, is_change_e, is_change_l, is_change_p; - int tot_uvloop, tot_vcolloop; CustomData_MeshMasks mask = {0}; if (check_meshmask) { mask = CD_MASK_MESH; @@ -1004,8 +1008,8 @@ bool BKE_mesh_validate_all_customdata(CustomData *vdata, is_valid &= mesh_validate_customdata( pdata, mask.pmask, totpoly, do_verbose, do_fixes, &is_change_p); - tot_uvloop = CustomData_number_of_layers(ldata, CD_MLOOPUV); - tot_vcolloop = CustomData_number_of_layers(ldata, CD_MLOOPCOL); + const int tot_uvloop = CustomData_number_of_layers(ldata, CD_MLOOPUV); + const int tot_vcolloop = CustomData_number_of_layers(ldata, CD_MLOOPCOL); if (tot_uvloop > MAX_MTFACE) { PRINT_ERR( "\tMore UV layers than %d allowed, %d last ones won't be available for render, shaders, " @@ -1523,111 +1527,6 @@ void BKE_mesh_calc_edges_legacy(Mesh *me, const bool use_old) BKE_mesh_strip_loose_faces(me); } -/** - * Calculate edges from polygons - * - * \param mesh: The mesh to add edges into - * \param update: When true create new edges co-exist - */ -void BKE_mesh_calc_edges(Mesh *mesh, bool update, const bool select) -{ - CustomData edata; - EdgeHashIterator *ehi; - MPoly *mp; - MEdge *med, *med_orig; - EdgeHash *eh; - unsigned int eh_reserve; - int i, totedge, totpoly = mesh->totpoly; - int med_index; - /* select for newly created meshes which are selected [#25595] */ - const short ed_flag = (ME_EDGEDRAW | ME_EDGERENDER) | (select ? SELECT : 0); - - if (mesh->totedge == 0) { - update = false; - } - - eh_reserve = max_ii(update ? mesh->totedge : 0, BLI_EDGEHASH_SIZE_GUESS_FROM_POLYS(totpoly)); - eh = BLI_edgehash_new_ex(__func__, eh_reserve); - - if (update) { - /* assume existing edges are valid - * useful when adding more faces and generating edges from them */ - med = mesh->medge; - for (i = 0; i < mesh->totedge; i++, med++) { - BLI_edgehash_insert(eh, med->v1, med->v2, med); - } - } - - /* mesh loops (bmesh only) */ - for (mp = mesh->mpoly, i = 0; i < totpoly; mp++, i++) { - MLoop *l = &mesh->mloop[mp->loopstart]; - int j, v_prev = (l + (mp->totloop - 1))->v; - for (j = 0; j < mp->totloop; j++, l++) { - if (v_prev != l->v) { - void **val_p; - if (!BLI_edgehash_ensure_p(eh, v_prev, l->v, &val_p)) { - *val_p = NULL; - } - } - v_prev = l->v; - } - } - - totedge = BLI_edgehash_len(eh); - - /* write new edges into a temporary CustomData */ - CustomData_reset(&edata); - CustomData_add_layer(&edata, CD_MEDGE, CD_CALLOC, NULL, totedge); - - med = CustomData_get_layer(&edata, CD_MEDGE); - for (ehi = BLI_edgehashIterator_new(eh), i = 0; BLI_edgehashIterator_isDone(ehi) == false; - BLI_edgehashIterator_step(ehi), ++i, ++med) { - if (update && (med_orig = BLI_edgehashIterator_getValue(ehi))) { - *med = *med_orig; /* copy from the original */ - } - else { - BLI_edgehashIterator_getKey(ehi, &med->v1, &med->v2); - med->flag = ed_flag; - } - - /* store the new edge index in the hash value */ - BLI_edgehashIterator_setValue(ehi, POINTER_FROM_INT(i)); - } - BLI_edgehashIterator_free(ehi); - - if (mesh->totpoly) { - /* second pass, iterate through all loops again and assign - * the newly created edges to them. */ - for (mp = mesh->mpoly, i = 0; i < mesh->totpoly; mp++, i++) { - MLoop *l = &mesh->mloop[mp->loopstart]; - MLoop *l_prev = (l + (mp->totloop - 1)); - int j; - for (j = 0; j < mp->totloop; j++, l++) { - /* Lookup hashed edge index, if it's valid. */ - if (l_prev->v != l->v) { - med_index = POINTER_AS_INT(BLI_edgehash_lookup(eh, l_prev->v, l->v)); - } - else { - /* This is an invalid edge; normally this does not happen in Blender, but it can be part - * of an imported mesh with invalid geometry. See T76514. */ - med_index = 0; - } - l_prev->e = med_index; - l_prev = l; - } - } - } - - /* free old CustomData and assign new one */ - CustomData_free(&mesh->edata, mesh->totedge); - mesh->edata = edata; - mesh->totedge = totedge; - - mesh->medge = CustomData_get_layer(&mesh->edata, CD_MEDGE); - - BLI_edgehash_free(eh, NULL); -} - void BKE_mesh_calc_edges_loose(Mesh *mesh) { MEdge *med = mesh->medge; @@ -1654,16 +1553,11 @@ void BKE_mesh_calc_edges_loose(Mesh *mesh) void BKE_mesh_calc_edges_tessface(Mesh *mesh) { - CustomData edgeData; - EdgeSetIterator *ehi; - MFace *mf = mesh->mface; - MEdge *med; - EdgeSet *eh; - int i, *index, numEdges, numFaces = mesh->totface; + const int numFaces = mesh->totface; + EdgeSet *eh = BLI_edgeset_new_ex(__func__, BLI_EDGEHASH_SIZE_GUESS_FROM_POLYS(numFaces)); - eh = BLI_edgeset_new_ex(__func__, BLI_EDGEHASH_SIZE_GUESS_FROM_POLYS(numFaces)); - - for (i = 0; i < numFaces; i++, mf++) { + MFace *mf = mesh->mface; + for (int i = 0; i < numFaces; i++, mf++) { BLI_edgeset_add(eh, mf->v1, mf->v2); BLI_edgeset_add(eh, mf->v2, mf->v3); @@ -1676,17 +1570,19 @@ void BKE_mesh_calc_edges_tessface(Mesh *mesh) } } - numEdges = BLI_edgeset_len(eh); + const int numEdges = BLI_edgeset_len(eh); /* write new edges into a temporary CustomData */ + CustomData edgeData; CustomData_reset(&edgeData); CustomData_add_layer(&edgeData, CD_MEDGE, CD_CALLOC, NULL, numEdges); CustomData_add_layer(&edgeData, CD_ORIGINDEX, CD_CALLOC, NULL, numEdges); - med = CustomData_get_layer(&edgeData, CD_MEDGE); - index = CustomData_get_layer(&edgeData, CD_ORIGINDEX); + MEdge *med = CustomData_get_layer(&edgeData, CD_MEDGE); + int *index = CustomData_get_layer(&edgeData, CD_ORIGINDEX); - for (ehi = BLI_edgesetIterator_new(eh), i = 0; BLI_edgesetIterator_isDone(ehi) == false; + EdgeSetIterator *ehi = BLI_edgesetIterator_new(eh); + for (int i = 0; BLI_edgesetIterator_isDone(ehi) == false; BLI_edgesetIterator_step(ehi), i++, med++, index++) { BLI_edgesetIterator_getKey(ehi, &med->v1, &med->v2); |