int BKE_mesh_validate_arrays(Mesh *me, MVert *UNUSED(mverts), unsigned int totvert, MEdge *medges, unsigned int totedge, MFace *mfaces, unsigned int totface, const short do_verbose, const short do_fixes) { # define PRINT if(do_verbose) printf # define REMOVE_EDGE_TAG(_med) { _med->v2= _med->v1; do_edge_free= 1; } # define REMOVE_FACE_TAG(_mf) { _mf->v3=0; do_face_free= 1; } // MVert *mv; MEdge *med; MFace *mf; MFace *mf_prev; unsigned int i; int do_face_free= FALSE; int do_edge_free= FALSE; int do_edge_recalc= FALSE; EdgeHash *edge_hash = BLI_edgehash_new(); SortFace *sort_faces= MEM_callocN(sizeof(SortFace) * totface, "search faces"); SortFace *sf; SortFace *sf_prev; unsigned int totsortface= 0; BLI_assert(!(do_fixes && me == NULL)); PRINT("ED_mesh_validate: verts(%u), edges(%u), faces(%u)\n", totvert, totedge, totface); if(totedge == 0 && totface != 0) { PRINT(" locical error, %u faces and 0 edges\n", totface); do_edge_recalc= TRUE; } for(i=0, med= medges; i<totedge; i++, med++) { int remove= FALSE; if(med->v1 == med->v2) { PRINT(" edge %u: has matching verts, both %u\n", i, med->v1); remove= do_fixes; } if(med->v1 >= totvert) { PRINT(" edge %u: v1 index out of range, %u\n", i, med->v1); remove= do_fixes; } if(med->v2 >= totvert) { PRINT(" edge %u: v2 index out of range, %u\n", i, med->v2); remove= do_fixes; } if(BLI_edgehash_haskey(edge_hash, med->v1, med->v2)) { PRINT(" edge %u: is a duplicate of, %d\n", i, GET_INT_FROM_POINTER(BLI_edgehash_lookup(edge_hash, med->v1, med->v2))); remove= do_fixes; } if(remove == FALSE){ BLI_edgehash_insert(edge_hash, med->v1, med->v2, SET_INT_IN_POINTER(i)); } else { REMOVE_EDGE_TAG(med); } } for(i=0, mf=mfaces, sf=sort_faces; i<totface; i++, mf++) { int remove= FALSE; int fidx; unsigned int fv[4]; fidx = mf->v4 ? 3:2; do { fv[fidx]= *(&(mf->v1) + fidx); if(fv[fidx] >= totvert) { PRINT(" face %u: 'v%d' index out of range, %u\n", i, fidx + 1, fv[fidx]); remove= do_fixes; } } while (fidx--); if(remove == FALSE) { if(mf->v4) { if(mf->v1 == mf->v2) { PRINT(" face %u: verts invalid, v1/v2 both %u\n", i, mf->v1); remove= do_fixes; } if(mf->v1 == mf->v3) { PRINT(" face %u: verts invalid, v1/v3 both %u\n", i, mf->v1); remove= do_fixes; } if(mf->v1 == mf->v4) { PRINT(" face %u: verts invalid, v1/v4 both %u\n", i, mf->v1); remove= do_fixes; } if(mf->v2 == mf->v3) { PRINT(" face %u: verts invalid, v2/v3 both %u\n", i, mf->v2); remove= do_fixes; } if(mf->v2 == mf->v4) { PRINT(" face %u: verts invalid, v2/v4 both %u\n", i, mf->v2); remove= do_fixes; } if(mf->v3 == mf->v4) { PRINT(" face %u: verts invalid, v3/v4 both %u\n", i, mf->v3); remove= do_fixes; } } else { if(mf->v1 == mf->v2) { PRINT(" faceT %u: verts invalid, v1/v2 both %u\n", i, mf->v1); remove= do_fixes; } if(mf->v1 == mf->v3) { PRINT(" faceT %u: verts invalid, v1/v3 both %u\n", i, mf->v1); remove= do_fixes; } if(mf->v2 == mf->v3) { PRINT(" faceT %u: verts invalid, v2/v3 both %u\n", i, mf->v2); remove= do_fixes; } } if(remove == FALSE) { if(totedge) { if(mf->v4) { if(!BLI_edgehash_haskey(edge_hash, mf->v1, mf->v2)) { PRINT(" face %u: edge v1/v2 (%u,%u) is missing egde data\n", i, mf->v1, mf->v2); do_edge_recalc= TRUE; } if(!BLI_edgehash_haskey(edge_hash, mf->v2, mf->v3)) { PRINT(" face %u: edge v2/v3 (%u,%u) is missing egde data\n", i, mf->v2, mf->v3); do_edge_recalc= TRUE; } if(!BLI_edgehash_haskey(edge_hash, mf->v3, mf->v4)) { PRINT(" face %u: edge v3/v4 (%u,%u) is missing egde data\n", i, mf->v3, mf->v4); do_edge_recalc= TRUE; } if(!BLI_edgehash_haskey(edge_hash, mf->v4, mf->v1)) { PRINT(" face %u: edge v4/v1 (%u,%u) is missing egde data\n", i, mf->v4, mf->v1); do_edge_recalc= TRUE; } } else { if(!BLI_edgehash_haskey(edge_hash, mf->v1, mf->v2)) { PRINT(" face %u: edge v1/v2 (%u,%u) is missing egde data\n", i, mf->v1, mf->v2); do_edge_recalc= TRUE; } if(!BLI_edgehash_haskey(edge_hash, mf->v2, mf->v3)) { PRINT(" face %u: edge v2/v3 (%u,%u) is missing egde data\n", i, mf->v2, mf->v3); do_edge_recalc= TRUE; } if(!BLI_edgehash_haskey(edge_hash, mf->v3, mf->v1)) { PRINT(" face %u: edge v3/v1 (%u,%u) is missing egde data\n", i, mf->v3, mf->v1); do_edge_recalc= TRUE; } } } sf->index = i; if(mf->v4) { edge_store_from_mface_quad(sf->es, mf); qsort(sf->es, 4, sizeof(int64_t), int64_cmp); } else { edge_store_from_mface_tri(sf->es, mf); qsort(sf->es, 3, sizeof(int64_t), int64_cmp); } totsortface++; sf++; } } if(remove) { REMOVE_FACE_TAG(mf); } } qsort(sort_faces, totsortface, sizeof(SortFace), search_face_cmp); sf= sort_faces; sf_prev= sf; sf++; for(i=1; i<totsortface; i++, sf++) { int remove= FALSE; /* on a valid mesh, code below will never run */ if(memcmp(sf->es, sf_prev->es, sizeof(sf_prev->es)) == 0) { mf= mfaces + sf->index; if(do_verbose) { mf_prev= mfaces + sf_prev->index; if(mf->v4) { PRINT(" face %u & %u: are duplicates (%u,%u,%u,%u) (%u,%u,%u,%u)\n", sf->index, sf_prev->index, mf->v1, mf->v2, mf->v3, mf->v4, mf_prev->v1, mf_prev->v2, mf_prev->v3, mf_prev->v4); } else { PRINT(" face %u & %u: are duplicates (%u,%u,%u) (%u,%u,%u)\n", sf->index, sf_prev->index, mf->v1, mf->v2, mf->v3, mf_prev->v1, mf_prev->v2, mf_prev->v3); } } remove= do_fixes; } else { sf_prev= sf; } if(remove) { REMOVE_FACE_TAG(mf); } } BLI_edgehash_free(edge_hash, NULL); MEM_freeN(sort_faces); PRINT("BKE_mesh_validate: finished\n\n"); # undef PRINT # undef REMOVE_EDGE_TAG # undef REMOVE_FACE_TAG if(me) { if(do_face_free) { mesh_strip_loose_faces(me); } if (do_edge_free) { mesh_strip_loose_edges(me); } if(do_fixes && do_edge_recalc) { BKE_mesh_calc_edges(me, TRUE); } } return (do_face_free || do_edge_free || do_edge_recalc); }
int BKE_mesh_validate_arrays( Mesh *me, MVert *mverts, unsigned int totvert, MEdge *medges, unsigned int totedge, MFace *mfaces, unsigned int totface, MDeformVert *dverts, /* assume totvert length */ const short do_verbose, const short do_fixes) { # define REMOVE_EDGE_TAG(_med) { _med->v2= _med->v1; do_edge_free= 1; } # define REMOVE_FACE_TAG(_mf) { _mf->v3=0; do_face_free= 1; } // MVert *mv; MEdge *med; MFace *mf; MFace *mf_prev; MVert *mvert= mverts; unsigned int i; short do_face_free= FALSE; short do_edge_free= FALSE; short verts_fixed= FALSE; short vert_weights_fixed= FALSE; int do_edge_recalc= FALSE; EdgeHash *edge_hash = BLI_edgehash_new(); SortFace *sort_faces= MEM_callocN(sizeof(SortFace) * totface, "search faces"); SortFace *sf; SortFace *sf_prev; unsigned int totsortface= 0; BLI_assert(!(do_fixes && me == NULL)); PRINT("%s: verts(%u), edges(%u), faces(%u)\n", __func__, totvert, totedge, totface); if(totedge == 0 && totface != 0) { PRINT(" locical error, %u faces and 0 edges\n", totface); do_edge_recalc= TRUE; } for(i=1; i<totvert; i++, mvert++) { int j; int fix_normal= TRUE; for(j=0; j<3; j++) { if(!finite(mvert->co[j])) { PRINT(" vertex %u: has invalid coordinate\n", i); if (do_fixes) { zero_v3(mvert->co); verts_fixed= TRUE; } } if(mvert->no[j]!=0) fix_normal= FALSE; } if(fix_normal) { PRINT(" vertex %u: has zero normal, assuming Z-up normal\n", i); if (do_fixes) { mvert->no[2]= SHRT_MAX; verts_fixed= TRUE; } } } for(i=0, med= medges; i<totedge; i++, med++) { int remove= FALSE; if(med->v1 == med->v2) { PRINT(" edge %u: has matching verts, both %u\n", i, med->v1); remove= do_fixes; } if(med->v1 >= totvert) { PRINT(" edge %u: v1 index out of range, %u\n", i, med->v1); remove= do_fixes; } if(med->v2 >= totvert) { PRINT(" edge %u: v2 index out of range, %u\n", i, med->v2); remove= do_fixes; } if(BLI_edgehash_haskey(edge_hash, med->v1, med->v2)) { PRINT(" edge %u: is a duplicate of, %d\n", i, GET_INT_FROM_POINTER(BLI_edgehash_lookup(edge_hash, med->v1, med->v2))); remove= do_fixes; } if(remove == FALSE){ BLI_edgehash_insert(edge_hash, med->v1, med->v2, SET_INT_IN_POINTER(i)); } else { REMOVE_EDGE_TAG(med); } } for(i=0, mf=mfaces, sf=sort_faces; i<totface; i++, mf++) { int remove= FALSE; int fidx; unsigned int fv[4]; fidx = mf->v4 ? 3:2; do { fv[fidx]= *(&(mf->v1) + fidx); if(fv[fidx] >= totvert) { PRINT(" face %u: 'v%d' index out of range, %u\n", i, fidx + 1, fv[fidx]); remove= do_fixes; } } while (fidx--); if(remove == FALSE) { if(mf->v4) { if(mf->v1 == mf->v2) { PRINT(" face %u: verts invalid, v1/v2 both %u\n", i, mf->v1); remove= do_fixes; } if(mf->v1 == mf->v3) { PRINT(" face %u: verts invalid, v1/v3 both %u\n", i, mf->v1); remove= do_fixes; } if(mf->v1 == mf->v4) { PRINT(" face %u: verts invalid, v1/v4 both %u\n", i, mf->v1); remove= do_fixes; } if(mf->v2 == mf->v3) { PRINT(" face %u: verts invalid, v2/v3 both %u\n", i, mf->v2); remove= do_fixes; } if(mf->v2 == mf->v4) { PRINT(" face %u: verts invalid, v2/v4 both %u\n", i, mf->v2); remove= do_fixes; } if(mf->v3 == mf->v4) { PRINT(" face %u: verts invalid, v3/v4 both %u\n", i, mf->v3); remove= do_fixes; } } else { if(mf->v1 == mf->v2) { PRINT(" faceT %u: verts invalid, v1/v2 both %u\n", i, mf->v1); remove= do_fixes; } if(mf->v1 == mf->v3) { PRINT(" faceT %u: verts invalid, v1/v3 both %u\n", i, mf->v1); remove= do_fixes; } if(mf->v2 == mf->v3) { PRINT(" faceT %u: verts invalid, v2/v3 both %u\n", i, mf->v2); remove= do_fixes; } } if(remove == FALSE) { if(totedge) { if(mf->v4) { if(!BLI_edgehash_haskey(edge_hash, mf->v1, mf->v2)) { PRINT(" face %u: edge v1/v2 (%u,%u) is missing egde data\n", i, mf->v1, mf->v2); do_edge_recalc= TRUE; } if(!BLI_edgehash_haskey(edge_hash, mf->v2, mf->v3)) { PRINT(" face %u: edge v2/v3 (%u,%u) is missing egde data\n", i, mf->v2, mf->v3); do_edge_recalc= TRUE; } if(!BLI_edgehash_haskey(edge_hash, mf->v3, mf->v4)) { PRINT(" face %u: edge v3/v4 (%u,%u) is missing egde data\n", i, mf->v3, mf->v4); do_edge_recalc= TRUE; } if(!BLI_edgehash_haskey(edge_hash, mf->v4, mf->v1)) { PRINT(" face %u: edge v4/v1 (%u,%u) is missing egde data\n", i, mf->v4, mf->v1); do_edge_recalc= TRUE; } } else { if(!BLI_edgehash_haskey(edge_hash, mf->v1, mf->v2)) { PRINT(" face %u: edge v1/v2 (%u,%u) is missing egde data\n", i, mf->v1, mf->v2); do_edge_recalc= TRUE; } if(!BLI_edgehash_haskey(edge_hash, mf->v2, mf->v3)) { PRINT(" face %u: edge v2/v3 (%u,%u) is missing egde data\n", i, mf->v2, mf->v3); do_edge_recalc= TRUE; } if(!BLI_edgehash_haskey(edge_hash, mf->v3, mf->v1)) { PRINT(" face %u: edge v3/v1 (%u,%u) is missing egde data\n", i, mf->v3, mf->v1); do_edge_recalc= TRUE; } } } sf->index = i; if(mf->v4) { edge_store_from_mface_quad(sf->es, mf); qsort(sf->es, 4, sizeof(int64_t), int64_cmp); } else { edge_store_from_mface_tri(sf->es, mf); qsort(sf->es, 3, sizeof(int64_t), int64_cmp); } totsortface++; sf++; } } if(remove) { REMOVE_FACE_TAG(mf); } } qsort(sort_faces, totsortface, sizeof(SortFace), search_face_cmp); sf= sort_faces; sf_prev= sf; sf++; for(i=1; i<totsortface; i++, sf++) { int remove= FALSE; /* on a valid mesh, code below will never run */ if(memcmp(sf->es, sf_prev->es, sizeof(sf_prev->es)) == 0) { mf= mfaces + sf->index; if(do_verbose) { mf_prev= mfaces + sf_prev->index; if(mf->v4) { PRINT(" face %u & %u: are duplicates (%u,%u,%u,%u) (%u,%u,%u,%u)\n", sf->index, sf_prev->index, mf->v1, mf->v2, mf->v3, mf->v4, mf_prev->v1, mf_prev->v2, mf_prev->v3, mf_prev->v4); } else { PRINT(" face %u & %u: are duplicates (%u,%u,%u) (%u,%u,%u)\n", sf->index, sf_prev->index, mf->v1, mf->v2, mf->v3, mf_prev->v1, mf_prev->v2, mf_prev->v3); } } remove= do_fixes; } else { sf_prev= sf; } if(remove) { REMOVE_FACE_TAG(mf); } } BLI_edgehash_free(edge_hash, NULL); MEM_freeN(sort_faces); /* fix deform verts */ if (dverts) { MDeformVert *dv; for(i=0, dv= dverts; i<totvert; i++, dv++) { MDeformWeight *dw; unsigned int j; for(j=0, dw= dv->dw; j < dv->totweight; j++, dw++) { /* note, greater then max defgroups is accounted for in our code, but not < 0 */ if (!finite(dw->weight)) { PRINT(" vertex deform %u, group %d has weight: %f\n", i, dw->def_nr, dw->weight); if (do_fixes) { dw->weight= 0.0f; vert_weights_fixed= TRUE; } } else if (dw->weight < 0.0f || dw->weight > 1.0f) { PRINT(" vertex deform %u, group %d has weight: %f\n", i, dw->def_nr, dw->weight); if (do_fixes) { CLAMP(dw->weight, 0.0f, 1.0f); vert_weights_fixed= TRUE; } } if (dw->def_nr < 0) { PRINT(" vertex deform %u, has invalid group %d\n", i, dw->def_nr); if (do_fixes) { defvert_remove_group(dv, dw); if (dv->dw) { /* re-allocated, the new values compensate for stepping * within the for loop and may not be valid */ j--; dw= dv->dw + j; vert_weights_fixed= TRUE; } else { /* all freed */ break; } } } } } } PRINT("BKE_mesh_validate: finished\n\n"); # undef REMOVE_EDGE_TAG # undef REMOVE_FACE_TAG if(me) { if(do_face_free) { mesh_strip_loose_faces(me); } if (do_edge_free) { mesh_strip_loose_edges(me); } if(do_fixes && do_edge_recalc) { BKE_mesh_calc_edges(me, TRUE); } } return (verts_fixed || vert_weights_fixed || do_face_free || do_edge_free || do_edge_recalc); }