Revision: 34730
          
http://projects.blender.org/scm/viewvc.php?view=rev&root=bf-blender&revision=34730
Author:   campbellbarton
Date:     2011-02-09 01:27:46 +0000 (Wed, 09 Feb 2011)
Log Message:
-----------
mesh validation function to report errors and help debug bad data generated by 
modifiers.

detects...
- invalid vertex range for edges/faces
- duplicate indices in edge/face
- duplicate edges/faces in mesh
- missing edges data in faces

At the moment it doesn't correct errors, but eventually it will do this.

Modified Paths:
--------------
    trunk/blender/source/blender/editors/include/ED_mesh.h
    trunk/blender/source/blender/editors/mesh/CMakeLists.txt
    trunk/blender/source/blender/makesrna/intern/rna_mesh_api.c

Added Paths:
-----------
    trunk/blender/source/blender/editors/mesh/mesh_validate.c

Modified: trunk/blender/source/blender/editors/include/ED_mesh.h
===================================================================
--- trunk/blender/source/blender/editors/include/ED_mesh.h      2011-02-09 
01:21:03 UTC (rev 34729)
+++ trunk/blender/source/blender/editors/include/ED_mesh.h      2011-02-09 
01:27:46 UTC (rev 34730)
@@ -51,6 +51,9 @@
 struct MDeformVert;
 struct Scene;
 struct Mesh;
+struct MFace;
+struct MEdge;
+struct MVert;
 struct MCol;
 struct UvVertMap;
 struct UvMapVert;
@@ -79,6 +82,10 @@
 #define B_FRACTAL                      0x2000
 #define B_SPHERE                       0x4000
 
+/* mesh_validate.c */
+void ED_mesh_validate_arrays(struct MVert *mverts, int totvert, struct MEdge 
*medges, int totedge, struct MFace *mfaces, int totface);
+void ED_mesh_validate(struct Mesh *me);
+
 /* meshtools.c */
 
 intptr_t       mesh_octree_table(struct Object *ob, struct EditMesh *em, float 
*co, char mode);

Modified: trunk/blender/source/blender/editors/mesh/CMakeLists.txt
===================================================================
--- trunk/blender/source/blender/editors/mesh/CMakeLists.txt    2011-02-09 
01:21:03 UTC (rev 34729)
+++ trunk/blender/source/blender/editors/mesh/CMakeLists.txt    2011-02-09 
01:27:46 UTC (rev 34730)
@@ -42,6 +42,7 @@
        loopcut.c
        mesh_data.c
        mesh_ops.c
+       mesh_validate.c
        meshtools.c
 
        mesh_intern.h

Added: trunk/blender/source/blender/editors/mesh/mesh_validate.c
===================================================================
--- trunk/blender/source/blender/editors/mesh/mesh_validate.c                   
        (rev 0)
+++ trunk/blender/source/blender/editors/mesh/mesh_validate.c   2011-02-09 
01:27:46 UTC (rev 34730)
@@ -0,0 +1,252 @@
+/**
+ * $Id: mesh_ops.c 34569 2011-01-30 16:19:08Z ton $
+ *
+ * ***** 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.
+ *
+ * The Original Code is Copyright (C) 2011 Blender Foundation.
+ * All rights reserved.
+ *
+ * ***** END GPL LICENSE BLOCK *****
+ */
+
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <limits.h>
+
+#include "DNA_mesh_types.h"
+#include "DNA_meshdata_types.h"
+
+#include "BLI_utildefines.h"
+#include "BLI_edgehash.h"
+
+#include "MEM_guardedalloc.h"
+
+#include "ED_mesh.h"
+
+typedef struct SearchFace {
+       unsigned int v[4];
+       unsigned int index;
+} SearchFace;
+
+static int uint_cmp(const void *v1, const void *v2)
+{
+       const unsigned int x1= GET_INT_FROM_POINTER(v1), x2= 
GET_INT_FROM_POINTER(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 SearchFace *sfa= v1, *sfb= v2;
+
+       if              (sfa->v[0] > sfb->v[0]) return 1;
+       else if (sfa->v[0] < sfb->v[0]) return -1;
+
+       if              (sfa->v[1] > sfb->v[1]) return 1;
+       else if (sfa->v[1] < sfb->v[1]) return -1;
+
+       if              (sfa->v[2] > sfb->v[2]) return 1;
+       else if (sfa->v[2] < sfb->v[2]) return -1;
+
+       if              (sfa->v[3] > sfb->v[3]) return 1;
+       else if (sfa->v[3] < sfb->v[3]) return -1;
+
+       return 0;
+}
+
+void ED_mesh_validate_arrays(MVert *UNUSED(mverts), int totvert, MEdge 
*medges, int totedge, MFace *mfaces, int totface)
+{
+//     MVert *mv;
+       MEdge *med;
+       MFace *mf;
+       int i;
+
+       EdgeHash *edge_hash = BLI_edgehash_new();
+
+       SearchFace *search_faces= MEM_callocN(sizeof(SearchFace) * totface, 
"search faces");
+       SearchFace *sf;
+       SearchFace *sf_prev;
+
+       printf("ED_mesh_validate: verts(%d), edges(%d), faces(%d)\n", totvert, 
totedge, totface);
+
+       if(totedge==0 && totface != 0) {
+               printf("    locical error, %d faces and 0 edges\n", totface);
+       }
+
+       for(i=0, med=medges; i<totedge; i++, med++) {
+               if(med->v1 == med->v2) {
+                       printf("    edge %d: has matching verts, both %d\n", i, 
med->v1);
+               }
+               if(med->v1 < 0 || med->v1 >= totvert) {
+                       printf("    edge %d: v1 index out of range, %d\n", i, 
med->v1);
+               }
+               if(med->v2 < 0 || med->v2 >= totvert) {
+                       printf("    edge %d: v2 index out of range, %d\n", i, 
med->v2);
+               }
+
+               if(BLI_edgehash_haskey(edge_hash, med->v1, med->v2)) {
+                       printf("    edge %d: is a duplicate of, %d\n", i, 
GET_INT_FROM_POINTER(BLI_edgehash_lookup(edge_hash, med->v1, med->v2)));
+               }
+
+               BLI_edgehash_insert(edge_hash, med->v1, med->v2, 
SET_INT_IN_POINTER(i));
+       }
+
+       for(i=0, mf=mfaces; i<totface; i++, mf++) {
+               unsigned int fverts[4];
+               // unsigned int fedges[4];
+               int fidx;
+
+               fidx = mf->v4 ? 3:2;
+               do {
+                       fverts[fidx]= *(&mf->v1 + fidx);
+                       if(fverts[fidx] < 0 || fverts[fidx] >= totvert) {
+                               printf("    face %d: 'v%d' index out of range, 
%d\n", i, fidx + 1, fverts[fidx]);
+                       }
+               } while (fidx--);
+
+               if(mf->v4) {
+                       if(mf->v1 == mf->v2) printf("    face %d: verts 
invalid, v1/v2 both %d\n", i, mf->v1);
+                       if(mf->v1 == mf->v3) printf("    face %d: verts 
invalid, v1/v3 both %d\n", i, mf->v1);
+                       if(mf->v1 == mf->v4) printf("    face %d: verts 
invalid, v1/v4 both %d\n", i, mf->v1);
+
+                       if(mf->v2 == mf->v3) printf("    face %d: verts 
invalid, v2/v3 both %d\n", i, mf->v2);
+                       if(mf->v2 == mf->v4) printf("    face %d: verts 
invalid, v2/v4 both %d\n", i, mf->v2);
+
+                       if(mf->v3 == mf->v4) printf("    face %d: verts 
invalid, v3/v4 both %d\n", i, mf->v3);
+
+                       if(totedge) {
+                               if(!BLI_edgehash_haskey(edge_hash, mf->v1, 
mf->v2)) printf("    face %d: edge v1/v2 (%d,%d) is missing egde data\n", i, 
mf->v1, mf->v2);
+                               if(!BLI_edgehash_haskey(edge_hash, mf->v2, 
mf->v3)) printf("    face %d: edge v2/v3 (%d,%d) is missing egde data\n", i, 
mf->v2, mf->v3);
+                               if(!BLI_edgehash_haskey(edge_hash, mf->v3, 
mf->v4)) printf("    face %d: edge v3/v4 (%d,%d) is missing egde data\n", i, 
mf->v3, mf->v4);
+                               if(!BLI_edgehash_haskey(edge_hash, mf->v4, 
mf->v1)) printf("    face %d: edge v4/v1 (%d,%d) is missing egde data\n", i, 
mf->v4, mf->v1);
+                       }
+                       /* TODO, avoid double lookop */
+                       /*
+                       fedges[0]= 
GET_INT_FROM_POINTER(BLI_edgehash_lookup(edge_hash, mf->v1, mf->v2));
+                       fedges[1]= 
GET_INT_FROM_POINTER(BLI_edgehash_lookup(edge_hash, mf->v2, mf->v3));
+                       fedges[2]= 
GET_INT_FROM_POINTER(BLI_edgehash_lookup(edge_hash, mf->v3, mf->v4));
+                       fedges[3]= 
GET_INT_FROM_POINTER(BLI_edgehash_lookup(edge_hash, mf->v4, mf->v1));
+                       */
+                       qsort(fverts, 4, sizeof(int), uint_cmp);
+               }
+               else {
+                       if(mf->v1 == mf->v2) printf("    face %d: verts 
invalid, v1/v2 both %d\n", i, mf->v1);
+                       if(mf->v1 == mf->v3) printf("    face %d: verts 
invalid, v1/v3 both %d\n", i, mf->v1);
+
+                       if(mf->v2 == mf->v3) printf("    face %d: verts 
invalid, v2/v3 both %d\n", i, mf->v2);
+
+                       if(totedge) {
+                               if(!BLI_edgehash_haskey(edge_hash, mf->v1, 
mf->v2)) printf("    face %d: edge v1/v2 (%d,%d) is missing egde data\n", i, 
mf->v1, mf->v2);
+                               if(!BLI_edgehash_haskey(edge_hash, mf->v2, 
mf->v3)) printf("    face %d: edge v2/v3 (%d,%d) is missing egde data\n", i, 
mf->v2, mf->v3);
+                               if(!BLI_edgehash_haskey(edge_hash, mf->v3, 
mf->v1)) printf("    face %d: edge v3/v1 (%d,%d) is missing egde data\n", i, 
mf->v3, mf->v1);
+                       }
+                       /* TODO, avoid double lookop */
+                       /*
+                       fedges[0]= 
GET_INT_FROM_POINTER(BLI_edgehash_lookup(edge_hash, mf->v1, mf->v2));
+                       fedges[1]= 
GET_INT_FROM_POINTER(BLI_edgehash_lookup(edge_hash, mf->v2, mf->v3));
+                       fedges[2]= 
GET_INT_FROM_POINTER(BLI_edgehash_lookup(edge_hash, mf->v3, mf->v1));
+                       */
+                       qsort(fverts, 3, sizeof(int), uint_cmp);
+               }
+
+               search_faces[i].index = i;
+
+               if(mf->v4) {
+                       search_faces[i].v[0] = fverts[0];
+                       search_faces[i].v[1] = fverts[1];
+                       search_faces[i].v[2] = fverts[2];
+                       search_faces[i].v[3] = fverts[3];
+               }
+               else {
+                       search_faces[i].v[0] = fverts[0];
+                       search_faces[i].v[1] = fverts[1];
+                       search_faces[i].v[2] = fverts[2];
+                       search_faces[i].v[3] = UINT_MAX;
+               }
+       }
+
+       qsort(search_faces, totface, sizeof(SearchFace), search_face_cmp);
+
+       sf= search_faces;
+       sf_prev= sf;
+       sf++;
+
+       for(i=1; i<totface; i++, sf++, sf_prev++) {
+               /* on a valid mesh, code below will never run */
+               if(memcmp(sf->v, sf_prev->v, sizeof(sf_prev->v)) == 0) {
+                       /* slow, could be smarter here */
+                       MFace *mf= mfaces + sf->index;
+                       MFace *mf_prev= mfaces + sf_prev->index;
+                       int size_expect, size_found;
+
+                       EdgeHash *eh_tmp= BLI_edgehash_new();
+                       if(mf->v4) {
+                               BLI_edgehash_insert(eh_tmp, mf->v1, mf->v2, 
NULL);
+                               BLI_edgehash_insert(eh_tmp, mf->v2, mf->v3, 
NULL);
+                               BLI_edgehash_insert(eh_tmp, mf->v3, mf->v4, 
NULL);
+                               BLI_edgehash_insert(eh_tmp, mf->v4, mf->v1, 
NULL);
+
+                               BLI_edgehash_insert(eh_tmp, mf_prev->v1, 
mf_prev->v2, NULL);
+                               BLI_edgehash_insert(eh_tmp, mf_prev->v2, 
mf_prev->v3, NULL);
+                               BLI_edgehash_insert(eh_tmp, mf_prev->v3, 
mf_prev->v4, NULL);
+                               BLI_edgehash_insert(eh_tmp, mf_prev->v4, 
mf_prev->v1, NULL);
+
+                               size_expect= 4;
+                       }
+                       else {
+                               BLI_edgehash_insert(eh_tmp, mf->v1, mf->v2, 
NULL);
+                               BLI_edgehash_insert(eh_tmp, mf->v2, mf->v3, 
NULL);
+                               BLI_edgehash_insert(eh_tmp, mf->v3, mf->v1, 
NULL);
+
+                               BLI_edgehash_insert(eh_tmp, mf_prev->v1, 
mf_prev->v2, NULL);
+                               BLI_edgehash_insert(eh_tmp, mf_prev->v2, 
mf_prev->v3, NULL);
+                               BLI_edgehash_insert(eh_tmp, mf_prev->v3, 
mf_prev->v1, NULL);
+
+                               size_expect= 3;
+                       }
+
+                       size_found= BLI_edgehash_size(eh_tmp);
+                       BLI_edgehash_free(eh_tmp, NULL);
+
+                       if(size_found != size_expect) {
+                               printf("    face %d & %d: are duplicates ", 
sf->index, sf_prev->index);
+                               if(mf->v4) {
+                                       printf("(%d,%d,%d,%d) ", mf->v1, 
mf->v2, mf->v3, mf->v4);
+                                       printf("(%d,%d,%d,%d)\n", mf_prev->v1, 
mf_prev->v2, mf_prev->v3, mf_prev->v4);
+                               }
+                               else {
+                                       printf("(%d,%d,%d) ", mf->v1, mf->v2, 
mf->v3);
+                                       printf("(%d,%d,%d)\n", mf_prev->v1, 
mf_prev->v2, mf_prev->v3);
+                               }
+                       }
+               }
+       }
+
+       BLI_edgehash_free(edge_hash, NULL);
+       MEM_freeN(search_faces);
+
+       printf("ED_mesh_validate: finished\n\n");
+}
+
+void ED_mesh_validate(Mesh *me)
+{
+       printf("MESH: %s\n", me->id.name+2);

@@ Diff output truncated at 10240 characters. @@
_______________________________________________
Bf-blender-cvs mailing list
[email protected]
http://lists.blender.org/mailman/listinfo/bf-blender-cvs

Reply via email to