From c9d0de49b9d5c42d8d1f04a9d712c0d7bb0a5673 Mon Sep 17 00:00:00 2001 From: Bastien Montagne Date: Thu, 15 Mar 2012 20:10:07 +0000 Subject: [PATCH] mesh_validate code for bmesh (i.e. polys/loops). MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Everything seems to work well (many tests making random changes over various meshes went good), but the code is a bit complex and hard to follow, due to the various possibilities of invalid poly/loop combinations… Code also makes more operations than previous tri/quad faces version (hence is a bit slower), but I don’t think we can do otherwise, it’s just the price for bmesh flexibility. ;) Note: added the py script I used to make the tests, under source/tests/... --- source/blender/blenkernel/BKE_mesh.h | 10 +- source/blender/blenkernel/intern/mesh.c | 123 +++- .../blender/blenkernel/intern/mesh_validate.c | 596 +++++++++++------- source/tests/bl_mesh_validate.py | 161 +++++ 4 files changed, 641 insertions(+), 249 deletions(-) create mode 100644 source/tests/bl_mesh_validate.py diff --git a/source/blender/blenkernel/BKE_mesh.h b/source/blender/blenkernel/BKE_mesh.h index 7771cc7db46..63d3a1f309a 100644 --- a/source/blender/blenkernel/BKE_mesh.h +++ b/source/blender/blenkernel/BKE_mesh.h @@ -160,7 +160,8 @@ void mesh_get_texspace(struct Mesh *me, float r_loc[3], float r_rot[3], float r_ /* if old, it converts mface->edcode to edge drawflags */ void make_edges(struct Mesh *me, int old); -void mesh_strip_loose_faces(struct Mesh *me); +void mesh_strip_loose_faces(struct Mesh *me); /* Needed for compatibility (some old read code). */ +void mesh_strip_loose_polysloops(struct Mesh *me); void mesh_strip_loose_edges(struct Mesh *me); /* Calculate vertex and face normals, face normals are returned in *faceNors_r if non-NULL @@ -268,11 +269,14 @@ int mesh_center_bounds(struct Mesh *me, float cent[3]); void mesh_translate(struct Mesh *me, float offset[3], int do_keys); /* mesh_validate.c */ +/* XXX Loop v/e are unsigned, so using max uint_32 value as invalid marker... */ +#define INVALID_LOOP_EDGE_MARKER 4294967295u int BKE_mesh_validate_arrays( - struct Mesh *me, + struct Mesh *me, struct MVert *mverts, unsigned int totvert, struct MEdge *medges, unsigned int totedge, - struct MFace *mfaces, unsigned int totface, + struct MLoop *mloops, unsigned int totloop, + struct MPoly *mpolys, unsigned int totpoly, struct MDeformVert *dverts, /* assume totvert length */ const short do_verbose, const short do_fixes); int BKE_mesh_validate(struct Mesh *me, int do_verbose); diff --git a/source/blender/blenkernel/intern/mesh.c b/source/blender/blenkernel/intern/mesh.c index 29b8ca2e2b3..a824cd11c6a 100644 --- a/source/blender/blenkernel/intern/mesh.c +++ b/source/blender/blenkernel/intern/mesh.c @@ -1016,38 +1016,131 @@ void make_edges(Mesh *me, int old) mesh_strip_loose_faces(me); } +/* We need to keep this for edge creation (for now?), and some old readfile code... */ void mesh_strip_loose_faces(Mesh *me) { - int a,b; + MFace *f; + int a, b; - for (a=b=0; atotface; a++) { - if (me->mface[a].v3) { - if (a!=b) { - memcpy(&me->mface[b],&me->mface[a],sizeof(me->mface[b])); + for (a = b = 0, f = me->mface; a < me->totface; a++, f++) { + if (f->v3) { + if (a != b) { + memcpy(&me->mface[b], f, sizeof(me->mface[b])); CustomData_copy_data(&me->fdata, &me->fdata, a, b, 1); - CustomData_free_elem(&me->fdata, a, 1); } b++; } } - me->totface = b; + if (a != b) { + CustomData_free_elem(&me->fdata, b, a - b); + me->totface = b; + } +} + +/* Works on both loops and polys! */ +/* Note: It won't try to guess which loops of an invalid poly to remove! + * this is the work of the caller, to mark those loops... + * See e.g. BKE_mesh_validate_arrays(). */ +void mesh_strip_loose_polysloops(Mesh *me) +{ + MPoly *p; + MLoop *l; + int a, b; + /* New loops idx! */ + int *new_idx = MEM_mallocN(sizeof(int) * me->totloop, "strip_loose_polysloops old2new idx mapping for polys."); + + for (a = b = 0, p = me->mpoly; a < me->totpoly; a++, p++) { + int invalid = FALSE; + int i = p->loopstart; + int stop = i + p->totloop; + + if (stop > me->totloop || stop < i) { + invalid = TRUE; + } + else { + l = &me->mloop[i]; + i = stop - i; + /* If one of the poly's loops is invalid, the whole poly is invalid! */ + for (; i--; l++) { + if (l->e == INVALID_LOOP_EDGE_MARKER) { + invalid = TRUE; + break; + } + } + } + + if (p->totloop >= 3 && !invalid) { + if (a != b) { + memcpy(&me->mpoly[b], p, sizeof(me->mpoly[b])); + CustomData_copy_data(&me->pdata, &me->pdata, a, b, 1); + } + b++; + } + } + if (a != b) { + CustomData_free_elem(&me->pdata, b, a - b); + me->totpoly = b; + } + + /* And now, get rid of invalid loops. */ + for (a = b = 0, l = me->mloop; a < me->totloop; a++, l++) { + if (l->e != INVALID_LOOP_EDGE_MARKER) { + if (a != b) { + memcpy(&me->mloop[b], l, sizeof(me->mloop[b])); + CustomData_copy_data(&me->ldata, &me->ldata, a, b, 1); + } + new_idx[a] = b; + b++; + } + else { + /* XXX Theorically, we should be able to not do this, as no remaining poly + * should use any stripped loop. But for security's sake... */ + new_idx[a] = -a; + } + } + if (a != b) { + CustomData_free_elem(&me->ldata, b, a - b); + me->totloop = b; + } + + /* And now, update polys' start loop index. */ + /* Note: At this point, there should never be any poly using a striped loop! */ + for (a = 0, p = me->mpoly; a < me->totpoly; a++, p++) { + p->loopstart = new_idx[p->loopstart]; + } } void mesh_strip_loose_edges(Mesh *me) { - int a,b; + MEdge *e; + MLoop *l; + int a, b; + unsigned int *new_idx = MEM_mallocN(sizeof(int) * me->totedge, "strip_loose_edges old2new idx mapping for loops."); - for (a=b=0; atotedge; a++) { - if (me->medge[a].v1!=me->medge[a].v2) { - if (a!=b) { - memcpy(&me->medge[b],&me->medge[a],sizeof(me->medge[b])); + for (a = b = 0, e = me->medge; a < me->totedge; a++, e++) { + if (e->v1 != e->v2) { + if (a != b) { + memcpy(&me->medge[b], e, sizeof(me->medge[b])); CustomData_copy_data(&me->edata, &me->edata, a, b, 1); - CustomData_free_elem(&me->edata, a, 1); } + new_idx[a] = b; b++; } + else { + new_idx[a] = INVALID_LOOP_EDGE_MARKER; + } + } + if (a != b) { + CustomData_free_elem(&me->edata, b, a - b); + me->totedge = b; + } + + /* And now, update loops' edge indices. */ + /* XXX We hope no loop was pointing to a striped edge! + * Else, its e will be set to INVALID_LOOP_EDGE_MARKER :/ */ + for (a = 0, l = me->mloop; a < me->totloop; a++, l++) { + l->e = new_idx[l->e]; } - me->totedge = b; } void mball_to_mesh(ListBase *lb, Mesh *me) @@ -2215,7 +2308,7 @@ int mesh_recalcTessellation(CustomData *fdata, CustomData *ldata, CustomData *pdata, MVert *mvert, int totface, int UNUSED(totloop), int totpoly, - /* when tessellating to recalcilate normals after + /* when tessellating to recalculate normals after * we can skip copying here */ const int do_face_nor_cpy) { diff --git a/source/blender/blenkernel/intern/mesh_validate.c b/source/blender/blenkernel/intern/mesh_validate.c index fbd7a5fb47b..0a10184c730 100644 --- a/source/blender/blenkernel/intern/mesh_validate.c +++ b/source/blender/blenkernel/intern/mesh_validate.c @@ -33,111 +33,91 @@ #include "DNA_mesh_types.h" #include "DNA_meshdata_types.h" +#include "DNA_object_types.h" #include "BLO_sys_types.h" -#include "BLI_utildefines.h" #include "BLI_edgehash.h" #include "BLI_math_base.h" +#include "BLI_utildefines.h" +#include "BKE_deform.h" +#include "BKE_depsgraph.h" #include "BKE_DerivedMesh.h" +#include "BKE_mesh.h" #include "MEM_guardedalloc.h" -#include "BKE_mesh.h" -#include "BKE_deform.h" - #define SELECT 1 -typedef union { - uint32_t verts[2]; - int64_t edval; -} EdgeUUID; +/* Used to detect polys (faces) using exactly the same vertices. */ +/* Used to detect loops used by no (disjoint) or more than one (intersect) polys. */ +typedef struct SortPoly { + int *verts; + int numverts; + int loopstart; + unsigned int index; + int invalid; /* Poly index. */ +} SortPoly; -typedef struct SortFace { -// unsigned int v[4]; - EdgeUUID es[4]; - unsigned int index; -} SortFace; - -static void edge_store_assign(uint32_t verts[2], const uint32_t v1, const uint32_t v2) +/* TODO check there is not some standard define of this somewhere! */ +static int int_cmp(const void *v1, const void *v2) { - if(v1 < v2) { - verts[0]= v1; - verts[1]= v2; - } - else { - verts[0]= v2; - verts[1]= v1; - } + return *(int*)v1 > *(int*)v2 ? 1 : *(int*)v1 < *(int*)v2 ? -1 : 0; } -static void edge_store_from_mface_quad(EdgeUUID es[4], MFace *mf) +static int search_poly_cmp(const void *v1, const void *v2) { - edge_store_assign(es[0].verts, mf->v1, mf->v2); - edge_store_assign(es[1].verts, mf->v2, mf->v3); - edge_store_assign(es[2].verts, mf->v3, mf->v4); - edge_store_assign(es[3].verts, mf->v4, mf->v1); + const SortPoly *sp1 = v1, *sp2 = v2; + const int max_idx = sp1->numverts > sp2->numverts ? sp2->numverts : sp1->numverts; + int idx = 0; + + /* Reject all invalid polys at end of list! */ + if (sp1->invalid || sp2->invalid) + return sp1->invalid && sp2->invalid ? 0 : sp1->invalid ? 1 : -1; + /* Else, sort on first non-egal verts (remember verts of valid polys are sorted). */ + while (idx < max_idx && sp1->verts[idx] == sp2->verts[idx]) + idx++; + return sp1->verts[idx] > sp2->verts[idx] ? 1 : sp1->verts[idx] < sp2->verts[idx] ? -1 : + sp1->numverts > sp2->numverts ? 1 : sp1->numverts < sp2->numverts ? -1 : 0; } -static void edge_store_from_mface_tri(EdgeUUID es[4], MFace *mf) +static int search_polyloop_cmp(const void *v1, const void *v2) { - edge_store_assign(es[0].verts, mf->v1, mf->v2); - edge_store_assign(es[1].verts, mf->v2, mf->v3); - edge_store_assign(es[2].verts, mf->v3, mf->v1); - es[3].verts[0] = es[3].verts[1] = UINT_MAX; -} - -static int int64_cmp(const void *v1, const void *v2) -{ - const int64_t x1= *(const int64_t *)v1; - const int64_t x2= *(const int64_t *)v2; - - if( x1 > x2 ) return 1; - else if( x1 < x2 ) return -1; - return 0; -} - -static int search_face_cmp(const void *v1, const void *v2) -{ - const SortFace *sfa= v1, *sfb= v2; - - if (sfa->es[0].edval > sfb->es[0].edval) return 1; - else if (sfa->es[0].edval < sfb->es[0].edval) return -1; - - else if (sfa->es[1].edval > sfb->es[1].edval) return 1; - else if (sfa->es[1].edval < sfb->es[1].edval) return -1; - - else if (sfa->es[2].edval > sfb->es[2].edval) return 1; - else if (sfa->es[2].edval < sfb->es[2].edval) return -1; - - else if (sfa->es[3].edval > sfb->es[3].edval) return 1; - else if (sfa->es[3].edval < sfb->es[3].edval) return -1; - else return 0; + const SortPoly *sp1 = v1, *sp2 = v2; + /* Reject all invalid polys at end of list! */ + if (sp1->invalid || sp2->invalid) + return sp1->invalid && sp2->invalid ? 0 : sp1->invalid ? 1 : -1; + /* Else, sort on loopstart. */ + return sp1->loopstart > sp2->loopstart ? 1 : sp1->loopstart < sp2->loopstart ? -1 : 0; } #define PRINT if(do_verbose) printf -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) +int BKE_mesh_validate_arrays(Mesh *mesh, + MVert *mverts, unsigned int totvert, + MEdge *medges, unsigned int totedge, + MLoop *mloops, unsigned int totloop, + MPoly *mpolys, unsigned int totpoly, + 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; } +# define REMOVE_EDGE_TAG(_me) { _me->v2 = _me->v1; do_edge_free = TRUE; } +# define IS_REMOVED_EDGE(_me) (_me->v2 == _me->v1) -// MVert *mv; - MEdge *med; - MFace *mf; - MFace *mf_prev; - MVert *mvert= mverts; - unsigned int i; +# define REMOVE_LOOP_TAG(_ml) { _ml->e = INVALID_LOOP_EDGE_MARKER; do_polyloop_free = TRUE; } +# define REMOVE_POLY_TAG(_mp) { _mp->totloop *= -1; do_polyloop_free = TRUE; } + + MVert *mv = mverts; + MEdge *me; + MLoop *ml; + MPoly *mp; + unsigned int i, j; + int *v; - short do_face_free= FALSE; short do_edge_free= FALSE; + short do_polyloop_free= FALSE; /* This regroups loops and polys! */ short verts_fixed= FALSE; short vert_weights_fixed= FALSE; @@ -146,180 +126,347 @@ int BKE_mesh_validate_arrays( Mesh *me, 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 && mesh == NULL)); - BLI_assert(!(do_fixes && me == NULL)); + PRINT("%s: verts(%u), edges(%u), loops(%u), polygons(%u)\n", + __func__, totvert, totedge, totloop, totpoly); - 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; + if(totedge == 0 && totpoly != 0) { + PRINT(" logical error, %u polygons and 0 edges\n", totpoly); + do_edge_recalc = do_fixes; } - for(i=1; ico[j])) { + if(!finite(mv->co[j])) { PRINT(" vertex %u: has invalid coordinate\n", i); if (do_fixes) { - zero_v3(mvert->co); + zero_v3(mv->co); verts_fixed= TRUE; } } - if(mvert->no[j]!=0) + if(mv->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; + mv->no[2]= SHRT_MAX; verts_fixed= TRUE; } } } - for(i=0, med= medges; iv1 == med->v2) { - PRINT(" edge %u: has matching verts, both %u\n", i, med->v1); + if(me->v1 == me->v2) { + PRINT(" edge %u: has matching verts, both %u\n", i, me->v1); remove= do_fixes; } - if(med->v1 >= totvert) { - PRINT(" edge %u: v1 index out of range, %u\n", i, med->v1); + if(me->v1 >= totvert) { + PRINT(" edge %u: v1 index out of range, %u\n", i, me->v1); remove= do_fixes; } - if(med->v2 >= totvert) { - PRINT(" edge %u: v2 index out of range, %u\n", i, med->v2); + if(me->v2 >= totvert) { + PRINT(" edge %u: v2 index out of range, %u\n", i, me->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))); + if(BLI_edgehash_haskey(edge_hash, me->v1, me->v2)) { + PRINT(" edge %u: is a duplicate of %d\n", i, + GET_INT_FROM_POINTER(BLI_edgehash_lookup(edge_hash, me->v1, me->v2))); remove= do_fixes; } if(remove == FALSE) { - BLI_edgehash_insert(edge_hash, med->v1, med->v2, SET_INT_IN_POINTER(i)); + BLI_edgehash_insert(edge_hash, me->v1, me->v2, SET_INT_IN_POINTER(i)); } else { - REMOVE_EDGE_TAG(med); + REMOVE_EDGE_TAG(me); } } - for(i=0, mf=mfaces, sf=sort_faces; i= 3 and loopstart+totloop < me.totloop). + * + * Loops must have: + * - a valid v value. + * - a valid e value (corresponding to the edge it defines with the next loop in poly). + * + * Also, loops not used by polys can be discarded. + * And "intersecting" loops (i.e. loops used by more than one poly) are invalid, + * so be sure to leave at most one poly/loop! + */ + { + SortPoly *sort_polys = MEM_callocN(sizeof(SortPoly) * totpoly, "mesh validate's sort_polys"); + SortPoly *prev_sp, *sp = sort_polys; + int prev_end; + for (i = 0, mp = mpolys; i < totpoly; i++, mp++, sp++) { + sp->index = i; - 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; + if (mp->loopstart < 0 || mp->totloop < 3) { + /* Invalid loop data. */ + PRINT(" poly %u is invalid (loopstart: %u, totloop: %u)\n", sp->index, mp->loopstart, mp->totloop); + sp->invalid = TRUE; } - } 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 (mp->loopstart + mp->totloop > totloop) { + /* Invalid loop data. */ + PRINT(" poly %u uses loops out of range (loopstart: %u, loopend: %u, max nbr of loops: %u)\n", + sp->index, mp->loopstart, mp->loopstart + mp->totloop - 1, totloop - 1); + sp->invalid = TRUE; } 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; } + /* Poly itself is valid, for now. */ + int v1, v2; /* v1 is prev loop vert idx, v2 is current loop one. */ + sp->invalid = FALSE; + sp->verts = v = MEM_mallocN(sizeof(int) * mp->totloop, "Vert idx of SortPoly"); + sp->numverts = mp->totloop; + sp->loopstart = mp->loopstart; - if(mf->v2 == mf->v3) { PRINT(" faceT %u: verts invalid, v2/v3 both %u\n", i, mf->v2); remove= do_fixes; } - } + /* Test all poly's loops' vert idx. */ + for (j = 0, ml = &mloops[sp->loopstart]; j < mp->totloop; j++, ml++, v++) { + if (ml->v >= totvert) { + /* Invalid vert idx. */ + PRINT(" loop %u has invalid vert reference (%u)\n", sp->loopstart + j, ml->v); + sp->invalid = TRUE; + } + *v = ml->v; + } - 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; } + if (sp->invalid) + continue; + + /* Test all poly's loops. */ + for (j = 0, ml = &mloops[sp->loopstart]; j < mp->totloop; j++, ml++) { + v1 = ml->v; + v2 = mloops[sp->loopstart + (j + 1) % mp->totloop].v; + if (!BLI_edgehash_haskey(edge_hash, v1, v2)) { + /* Edge not existing. */ + PRINT(" poly %u needs missing edge (%u, %u)\n", sp->index, v1, v2); + if (do_fixes) + do_edge_recalc = TRUE; + else + sp->invalid = TRUE; + } + else if (ml->e >= totedge) { + /* Invalid edge idx. + * We already know from previous text that a valid edge exists, use it (if allowed)! */ + if(do_fixes) { + int prev_e = ml->e; + ml->e = GET_INT_FROM_POINTER(BLI_edgehash_lookup(edge_hash, v1, v2)); + PRINT(" loop %u has invalid edge reference (%u), fixed using edge %u\n", + sp->loopstart + j, prev_e, ml->e); + } + else { + PRINT(" loop %u has invalid edge reference (%u)\n", sp->loopstart + j, ml->e); + sp->invalid = 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; } + me = &medges[ml->e]; + if (IS_REMOVED_EDGE(me) || !((me->v1 == v1 && me->v2 == v2) || (me->v1 == v2 && me->v2 == v1))) { + /* The pointed edge is invalid (tagged as removed, or vert idx mismatch), + * and we already know from previous test that a valid one exists, use it (if allowed)! */ + if(do_fixes) { + int prev_e = ml->e; + ml->e = GET_INT_FROM_POINTER(BLI_edgehash_lookup(edge_hash, v1, v2)); + PRINT(" poly %u has invalid edge reference (%u), fixed using edge %u\n", + sp->index, prev_e, ml->e); + } + else { + PRINT(" poly %u has invalid edge reference (%u)\n", sp->index, ml->e); + sp->invalid = TRUE; + } + } } } - sf->index = i; + /* Now check that that poly does not use a same vertex more than once! */ + if (!sp->invalid) { + int *prev_v = v = sp->verts; + j = sp->numverts; - if(mf->v4) { - edge_store_from_mface_quad(sf->es, mf); + qsort(sp->verts, j, sizeof(int), int_cmp); - qsort(sf->es, 4, sizeof(int64_t), int64_cmp); + for (j--, v++; j; j--, v++) { + if (*v != *prev_v) { + int dlt = v - prev_v; + if (dlt > 1) { + PRINT(" poly %u is invalid, it multi-uses vertex %u (%u times)\n", + sp->index, *prev_v, dlt); + sp->invalid = TRUE; + } + prev_v = v; + } + } + if (v - prev_v > 1) { /* Don’t forget final verts! */ + PRINT(" poly %u is invalid, it multi-uses vertex %u (%u times)\n", + sp->index, *prev_v, (int)(v - prev_v)); + sp->invalid = TRUE; + } } - 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); + /* Second check pass, testing polys using the same verts. */ + qsort(sort_polys, totpoly, sizeof(SortPoly), search_poly_cmp); + sp = prev_sp = sort_polys; + sp++; - sf= sort_faces; - sf_prev= sf; - sf++; - - for(i=1; ies, 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); + for (i = 1; i < totpoly; i++, sp++) { + int p1_nv = sp->numverts, p2_nv = prev_sp->numverts; + int *p1_v = sp->verts, *p2_v = prev_sp->verts; + short p1_sub = TRUE, p2_sub = TRUE; + if (sp->invalid) + break; + /* Test same polys. */ +#if 0 + /* NOTE: This performs a sub-set test. */ + /* XXX This (and the sort of verts list) is better than systematic + * search of all verts of one list into the other if lists have + * a fair amount of elements. + * Not sure however it's worth it in this case? + * But as we also need sorted vert list to check verts multi-used + * (in first pass of checks)... */ + /* XXX If we consider only "equal" polys (i.e. using exactly same set of verts) + * as invalid, better to replace this by a simple memory cmp... */ + while ((p1_nv && p2_nv) && (p1_sub || p2_sub)) { + if (*p1_v < *p2_v) { + if (p1_sub) + p1_sub = FALSE; + p1_nv--; + p1_v++; + } + else if (*p2_v < *p1_v) { + if (p2_sub) + p2_sub = FALSE; + p2_nv--; + p2_v++; } 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); + /* Equality, both next verts. */ + p1_nv--; + p2_nv--; + p1_v++; + p2_v++; } } + if (p1_nv && p1_sub) + p1_sub = FALSE; + else if (p2_nv && p2_sub) + p2_sub = FALSE; - remove= do_fixes; - } - else { - sf_prev= sf; + if (p1_sub && p2_sub) { + PRINT(" polys %u and %u use same vertices, considering poly %u as invalid.\n", + prev_sp->index, sp->index, sp->index); + sp->invalid = TRUE; + } + /* XXX In fact, these might be valid? :/ */ + else if (p1_sub) { + PRINT(" %u is a sub-poly of %u, considering it as invalid.\n", sp->index, prev_sp->index); + sp->invalid = TRUE; + } + else if (p2_sub) { + PRINT(" %u is a sub-poly of %u, considering it as invalid.\n", prev_sp->index, sp->index); + prev_sp->invalid = TRUE; + prev_sp = sp; /* sp is new reference poly. */ + } +#else + if (0) { + p1_sub += 0; + p2_sub += 0; + } + if((p1_nv == p2_nv) && (memcmp(p1_v, p2_v, p1_nv * sizeof(*p1_v)) == 0)) { + if (do_verbose) { + PRINT(" polys %u and %u use same vertices (%u", + prev_sp->index, sp->index, *p1_v); + for (j = 1; j < p1_nv; j++) + PRINT(", %u", p1_v[j]); + PRINT("), considering poly %u as invalid.\n", sp->index); + } + sp->invalid = TRUE; + } +#endif + else { + prev_sp = sp; + } } - if(remove) { - REMOVE_FACE_TAG(mf); + /* Third check pass, testing loops used by none or more than one poly. */ + qsort(sort_polys, totpoly, sizeof(SortPoly), search_polyloop_cmp); + sp = sort_polys; + prev_sp = NULL; + prev_end = 0; + for (i = 0; i < totpoly; i++, sp++) { + /* Free this now, we don't need it anymore, and avoid us another loop! */ + if (sp->verts) + MEM_freeN(sp->verts); + + /* Note above prev_sp: in following code, we make sure it is always valid poly (or NULL). */ + if (sp->invalid) { + if (do_fixes) { + REMOVE_POLY_TAG((&mpolys[sp->index])); + /* DO NOT REMOVE ITS LOOPS!!! + * As already invalid polys are at the end of the SortPoly list, the loops they + * were the only users have already been tagged as "to remove" during previous + * iterations, and we don’t want to remove some loops that may be used by + * another valid poly! */ + } + } + /* Test loops users. */ + else { + /* Unused loops. */ + if (prev_end < sp->loopstart) { + for (j = prev_end, ml = &mloops[prev_end]; j < sp->loopstart; j++, ml++) { + PRINT(" loop %u is unused.\n", j); + if (do_fixes) + REMOVE_LOOP_TAG(ml); + } + prev_end = sp->loopstart + sp->numverts; + prev_sp = sp; + } + /* Multi-used loops. */ + else if (prev_end > sp->loopstart) { + PRINT(" polys %u and %u share loops from %u to %u, considering poly %u as invalid.\n", + prev_sp->index, sp->index, sp->loopstart, prev_end, sp->index); + if (do_fixes) { + REMOVE_POLY_TAG((&mpolys[sp->index])); + /* DO NOT REMOVE ITS LOOPS!!! + * They might be used by some next, valid poly! + * Just not updating prev_end/prev_sp vars is enough to ensure the loops + * effectively no more needed will be marked as "to be removed"! */ + } + } + else { + prev_end = sp->loopstart + sp->numverts; + prev_sp = sp; + } + } } + /* We may have some remaining unused loops to get rid of! */ + if (prev_end < totloop) { + for (j = prev_end, ml = &mloops[prev_end]; j < totloop; j++, ml++) { + PRINT(" loop %u is unused.\n", j); + if (do_fixes) + REMOVE_LOOP_TAG(ml); + } + } + + MEM_freeN(sort_polys); } BLI_edgehash_free(edge_hash, NULL); - MEM_freeN(sort_faces); - /* fix deform verts */ if (dverts) { @@ -366,27 +513,28 @@ int BKE_mesh_validate_arrays( Mesh *me, } } - PRINT("BKE_mesh_validate: finished\n\n"); -# undef REMOVE_EDGE_TAG -# undef REMOVE_FACE_TAG +# undef REMOVE_EDGE_TAG +# undef IS_REMOVED_EDGE +# undef REMOVE_LOOP_TAG +# undef REMOVE_POLY_TAG - if(me) { - if(do_face_free) { - mesh_strip_loose_faces(me); + if(mesh) { + if(do_polyloop_free) { + mesh_strip_loose_polysloops(mesh); } if (do_edge_free) { - mesh_strip_loose_edges(me); + mesh_strip_loose_edges(mesh); } - if(do_fixes && do_edge_recalc) { - BKE_mesh_calc_edges(me, TRUE); + if(do_edge_recalc) { + BKE_mesh_calc_edges(mesh, TRUE); } } - return (verts_fixed || vert_weights_fixed || do_face_free || do_edge_free || do_edge_recalc); + return (verts_fixed || vert_weights_fixed || do_polyloop_free || do_edge_free || do_edge_recalc); } static int mesh_validate_customdata(CustomData *data, short do_verbose, const short do_fixes) @@ -417,16 +565,18 @@ static int mesh_validate_customdata(CustomData *data, short do_verbose, const sh #undef PRINT -static int BKE_mesh_validate_all_customdata(CustomData *vdata, CustomData *edata, CustomData *fdata, +static int BKE_mesh_validate_all_customdata(CustomData *vdata, CustomData *edata, + CustomData *ldata, CustomData *pdata, short do_verbose, const short do_fixes) { - int vfixed= 0, efixed= 0, ffixed= 0; + int vfixed= 0, efixed= 0, lfixed = 0, pfixed = 0; vfixed= mesh_validate_customdata(vdata, do_verbose, do_fixes); efixed= mesh_validate_customdata(edata, do_verbose, do_fixes); - ffixed= mesh_validate_customdata(fdata, do_verbose, do_fixes); + lfixed= mesh_validate_customdata(ldata, do_verbose, do_fixes); + pfixed= mesh_validate_customdata(pdata, do_verbose, do_fixes); - return vfixed || efixed || ffixed; + return vfixed || efixed || lfixed || pfixed; } int BKE_mesh_validate(Mesh *me, int do_verbose) @@ -437,35 +587,41 @@ int BKE_mesh_validate(Mesh *me, int do_verbose) printf("MESH: %s\n", me->id.name+2); } - layers_fixed= BKE_mesh_validate_all_customdata(&me->vdata, &me->edata, &me->fdata, do_verbose, TRUE); + layers_fixed= BKE_mesh_validate_all_customdata(&me->vdata, &me->edata, &me->ldata, &me->pdata, do_verbose, TRUE); arrays_fixed= BKE_mesh_validate_arrays(me, me->mvert, me->totvert, me->medge, me->totedge, - me->mface, me->totface, + me->mloop, me->totloop, + me->mpoly, me->totpoly, me->dvert, do_verbose, TRUE); - return layers_fixed || arrays_fixed; + if (layers_fixed || arrays_fixed) { + DAG_id_tag_update(&me->id, OB_RECALC_DATA); + return TRUE; + } + return FALSE; } int BKE_mesh_validate_dm(DerivedMesh *dm) { return BKE_mesh_validate_arrays(NULL, - dm->getVertArray(dm), dm->getNumVerts(dm), - dm->getEdgeArray(dm), dm->getNumEdges(dm), - dm->getTessFaceArray(dm), dm->getNumTessFaces(dm), - dm->getVertDataArray(dm, CD_MDEFORMVERT), - TRUE, FALSE); + dm->getVertArray(dm), dm->getNumVerts(dm), + dm->getEdgeArray(dm), dm->getNumEdges(dm), + dm->getLoopArray(dm), dm->getNumLoops(dm), + dm->getPolyArray(dm), dm->getNumPolys(dm), + dm->getVertDataArray(dm, CD_MDEFORMVERT), + TRUE, FALSE); } void BKE_mesh_calc_edges(Mesh *mesh, int update) { CustomData edata; EdgeHashIterator *ehi; - MFace *mf = mesh->mface; + MPoly *mp = mesh->mpoly; MEdge *med, *med_orig; EdgeHash *eh = BLI_edgehash_new(); - int i, totedge, totface = mesh->totface; + int i, totedge, totpoly = mesh->totpoly; int med_index; if(mesh->totedge==0) @@ -479,37 +635,15 @@ void BKE_mesh_calc_edges(Mesh *mesh, int update) BLI_edgehash_insert(eh, med->v1, med->v2, med); } - if(mesh->totpoly) { - /* mesh loops (bmesh only) */ - MPoly *mp= mesh->mpoly; - for(i=0; i < mesh->totpoly; i++, mp++) { - MLoop *l= &mesh->mloop[mp->loopstart]; - int j, l_prev= (l + (mp->totloop-1))->v; - for (j=0; j < mp->totloop; j++, l++) { - if (!BLI_edgehash_haskey(eh, l_prev, l->v)) { - BLI_edgehash_insert(eh, l_prev, l->v, NULL); - } - l_prev= l->v; - } - } - } - else { - /* regular faces (note, we could remove this for bmesh - campbell) */ - for (i = 0; i < totface; i++, mf++) { - if (!BLI_edgehash_haskey(eh, mf->v1, mf->v2)) - BLI_edgehash_insert(eh, mf->v1, mf->v2, NULL); - if (!BLI_edgehash_haskey(eh, mf->v2, mf->v3)) - BLI_edgehash_insert(eh, mf->v2, mf->v3, NULL); - - if (mf->v4) { - if (!BLI_edgehash_haskey(eh, mf->v3, mf->v4)) - BLI_edgehash_insert(eh, mf->v3, mf->v4, NULL); - if (!BLI_edgehash_haskey(eh, mf->v4, mf->v1)) - BLI_edgehash_insert(eh, mf->v4, mf->v1, NULL); - } else { - if (!BLI_edgehash_haskey(eh, mf->v3, mf->v1)) - BLI_edgehash_insert(eh, mf->v3, mf->v1, NULL); + /* mesh loops (bmesh only) */ + for(i=0; i < totpoly; i++, mp++) { + MLoop *l= &mesh->mloop[mp->loopstart]; + int j, l_prev= (l + (mp->totloop-1))->v; + for (j=0; j < mp->totloop; j++, l++) { + if (!BLI_edgehash_haskey(eh, l_prev, l->v)) { + BLI_edgehash_insert(eh, l_prev, l->v, NULL); } + l_prev= l->v; } } diff --git a/source/tests/bl_mesh_validate.py b/source/tests/bl_mesh_validate.py new file mode 100644 index 00000000000..a57a06d65e3 --- /dev/null +++ b/source/tests/bl_mesh_validate.py @@ -0,0 +1,161 @@ +# ##### BEGIN GPL LICENSE BLOCK ##### +# +# This program is free software; you can redistribute it and/or +# modify it under the terms of the GNU General Public License +# as published by the Free Software Foundation; either version 2 +# of the License, or (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with this program; if not, write to the Free Software Foundation, +# Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. +# +# ##### END GPL LICENSE BLOCK ##### + +# + +# Simple script to check mash validate code. +# XXX Should be extended with many more "wrong cases"! + +import bpy + +import sys +import random + + +MESHES = { + "test1": ( + ( + ( # Verts + (-1.0, -1.0, 0.0), + (-1.0, 0.0, 0.0), + (-1.0, 1.0, 0.0), + (0.0, -1.0, 0.0), + (0.0, 0.0, 0.0), + (0.0, 1.0, 0.0), + (1.0, -1.0, 0.0), + (1.0, 0.0, 0.0), + (1.5, 0.5, 0.0), + (1.0, 1.0, 0.0), + ), + ( # Edges + ), + ( # Loops + 0, 1, 4, 3, + 3, 4, 6, + 1, 2, 5, 4, + 3, 4, 6, + 4, 7, 6, + 4, 5, 9, 4, 8, 7, + ), + ( # Polygons + (0, 4), + (4, 3), + (7, 4), + (11, 3), + (14, 3), + (16, 6), + ), + ), + ), +} + + +BUILTINS = ( + "primitive_plane_add", + "primitive_cube_add", + "primitive_circle_add", + "primitive_uv_sphere_add", + "primitive_ico_sphere_add", + "primitive_cylinder_add", + "primitive_cone_add", + "primitive_grid_add", + "primitive_monkey_add", + "primitive_torus_add", + ) +BUILTINS_NBR = 4 +BUILTINS_NBRCHANGES = 5 + + +def test_meshes(): + for m in MESHES["test1"]: + bpy.ops.object.add(type="MESH") + data = bpy.context.active_object.data + + # Vertices. + data.vertices.add(len(m[0])) + for idx, v in enumerate(m[0]): + data.vertices[idx].co = v + # Edges. + data.edges.add(len(m[1])) + for idx, e in enumerate(m[1]): + data.edges[idx].vertices = e + # Loops. + data.loops.add(len(m[2])) + for idx, v in enumerate(m[2]): + data.loops[idx].vertex_index = v + # Polys. + data.polygons.add(len(m[3])) + for idx, l in enumerate(m[3]): + data.polygons[idx].loop_start = l[0] + data.polygons[idx].loop_total = l[1] + + while data.validate(verbose=True): + pass + + +def test_builtins(): + for x, func in enumerate(BUILTINS): + for y in range(BUILTINS_NBR): + getattr(bpy.ops.mesh, func)(location=(x * 2.5, y * 2.5, 0)) + data = bpy.context.active_object.data + try: + for n in range(BUILTINS_NBRCHANGES): + rnd = random.randint(1, 3) + if rnd == 1: + # Make fun with some edge. + e = random.randrange(0, len(data.edges)) + data.edges[e].vertices[random.randint(0, 1)] = \ + random.randrange(0, len(data.vertices) * 2) + elif rnd == 2: + # Make fun with some loop. + l = random.randrange(0, len(data.loops)) + if random.randint(0, 1): + data.loops[l].vertex_index = \ + random.randrange(0, len(data.vertices) * 2) + else: + data.loops[l].edge_index = \ + random.randrange(0, len(data.edges) * 2) + elif rnd == 3: + # Make fun with some poly. + p = random.randrange(0, len(data.polygons)) + if random.randint(0, 1): + data.polygons[p].loop_start = \ + random.randrange(0, len(data.loops)) + else: + data.polygons[p].loop_total = \ + random.randrange(0, 10) + except: + pass + + while data.validate(verbose=True): + pass + + +def main(): + test_builtins() + test_meshes() + + +if __name__ == "__main__": + # So a python error exits(1) + try: + main() + except: + import traceback + traceback.print_exc() + sys.exit(1)