Blender  V2.59
mesh_validate.c
Go to the documentation of this file.
00001 /*
00002  * $Id: mesh_validate.c 36320 2011-04-25 06:44:43Z campbellbarton $
00003  *
00004  * ***** BEGIN GPL LICENSE BLOCK *****
00005  *
00006  * This program is free software; you can redistribute it and/or
00007  * modify it under the terms of the GNU General Public License
00008  * as published by the Free Software Foundation; either version 2
00009  * of the License, or (at your option) any later version.
00010  *
00011  * This program is distributed in the hope that it will be useful,
00012  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00013  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00014  * GNU General Public License for more details.
00015  *
00016  * You should have received a copy of the GNU General Public License
00017  * along with this program; if not, write to the Free Software Foundation,
00018  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
00019  *
00020  * The Original Code is Copyright (C) 2011 Blender Foundation.
00021  * All rights reserved.
00022  *
00023  * ***** END GPL LICENSE BLOCK *****
00024  */
00025 
00031 #include <stdio.h>
00032 #include <stdlib.h>
00033 #include <string.h>
00034 #include <limits.h>
00035 
00036 #include "DNA_mesh_types.h"
00037 #include "DNA_meshdata_types.h"
00038 
00039 #include "BLO_sys_types.h"
00040 
00041 #include "BLI_utildefines.h"
00042 #include "BLI_edgehash.h"
00043 
00044 #include "BKE_DerivedMesh.h"
00045 
00046 #include "MEM_guardedalloc.h"
00047 
00048 #include "BKE_mesh.h"
00049 
00050 #define SELECT 1
00051 
00052 typedef union {
00053         uint32_t verts[2];
00054         int64_t edval;
00055 } EdgeUUID;
00056 
00057 typedef struct SortFace {
00058 //      unsigned int    v[4];
00059         EdgeUUID                es[4];
00060         unsigned int    index;
00061 } SortFace;
00062 
00063 static void edge_store_assign(uint32_t verts[2],  const uint32_t v1, const uint32_t v2)
00064 {
00065         if(v1 < v2) {
00066                 verts[0]= v1;
00067                 verts[1]= v2;
00068         }
00069         else {
00070                 verts[0]= v2;
00071                 verts[1]= v1;
00072         }
00073 }
00074 
00075 static void edge_store_from_mface_quad(EdgeUUID es[4], MFace *mf)
00076 {
00077         edge_store_assign(es[0].verts, mf->v1, mf->v2);
00078         edge_store_assign(es[1].verts, mf->v2, mf->v3);
00079         edge_store_assign(es[2].verts, mf->v3, mf->v4);
00080         edge_store_assign(es[3].verts, mf->v4, mf->v1);
00081 }
00082 
00083 static void edge_store_from_mface_tri(EdgeUUID es[3], MFace *mf)
00084 {
00085         edge_store_assign(es[0].verts, mf->v1, mf->v2);
00086         edge_store_assign(es[1].verts, mf->v2, mf->v3);
00087         edge_store_assign(es[2].verts, mf->v3, mf->v1);
00088         es[3].verts[0] = es[3].verts[1] = UINT_MAX;
00089 }
00090 
00091 static int int64_cmp(const void *v1, const void *v2)
00092 {
00093         const int64_t x1= *(const int64_t *)v1;
00094         const int64_t x2= *(const int64_t *)v2;
00095 
00096         if( x1 > x2 ) return 1;
00097         else if( x1 < x2 ) return -1;
00098         return 0;
00099 }
00100 
00101 static int search_face_cmp(const void *v1, const void *v2)
00102 {
00103         const SortFace *sfa= v1, *sfb= v2;
00104 
00105         if      (sfa->es[0].edval > sfb->es[0].edval) return 1;
00106         else if (sfa->es[0].edval < sfb->es[0].edval) return -1;
00107 
00108         else if (sfa->es[1].edval > sfb->es[1].edval) return 1;
00109         else if (sfa->es[1].edval < sfb->es[1].edval) return -1;
00110 
00111         else if (sfa->es[2].edval > sfb->es[2].edval) return 1;
00112         else if (sfa->es[2].edval < sfb->es[2].edval) return -1;
00113 
00114         else if (sfa->es[3].edval > sfb->es[3].edval) return 1;
00115         else if (sfa->es[3].edval < sfb->es[3].edval) return -1;
00116         else                                                                              return 0;
00117 
00118 }
00119 
00120 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)
00121 {
00122 #       define PRINT if(do_verbose) printf
00123 #       define REMOVE_EDGE_TAG(_med) { _med->v2= _med->v1; do_edge_free= 1; }
00124 #       define REMOVE_FACE_TAG(_mf) { _mf->v3=0; do_face_free= 1; }
00125 
00126 //      MVert *mv;
00127         MEdge *med;
00128         MFace *mf;
00129         MFace *mf_prev;
00130         unsigned int i;
00131 
00132         int do_face_free= FALSE;
00133         int do_edge_free= FALSE;
00134 
00135         int do_edge_recalc= FALSE;
00136 
00137         EdgeHash *edge_hash = BLI_edgehash_new();
00138 
00139         SortFace *sort_faces= MEM_callocN(sizeof(SortFace) * totface, "search faces");
00140         SortFace *sf;
00141         SortFace *sf_prev;
00142         unsigned int totsortface= 0;
00143 
00144         BLI_assert(!(do_fixes && me == NULL));
00145 
00146         PRINT("ED_mesh_validate: verts(%d), edges(%d), faces(%d)\n", totvert, totedge, totface);
00147 
00148         if(totedge == 0 && totface != 0) {
00149                 PRINT("    locical error, %d faces and 0 edges\n", totface);
00150                 do_edge_recalc= TRUE;
00151         }
00152 
00153         for(i=0, med= medges; i<totedge; i++, med++) {
00154                 int remove= FALSE;
00155                 if(med->v1 == med->v2) {
00156                         PRINT("    edge %d: has matching verts, both %d\n", i, med->v1);
00157                         remove= do_fixes;
00158                 }
00159                 if(med->v1 >= totvert) {
00160                         PRINT("    edge %d: v1 index out of range, %d\n", i, med->v1);
00161                         remove= do_fixes;
00162                 }
00163                 if(med->v2 >= totvert) {
00164                         PRINT("    edge %d: v2 index out of range, %d\n", i, med->v2);
00165                         remove= do_fixes;
00166                 }
00167 
00168                 if(BLI_edgehash_haskey(edge_hash, med->v1, med->v2)) {
00169                         PRINT("    edge %d: is a duplicate of, %d\n", i, GET_INT_FROM_POINTER(BLI_edgehash_lookup(edge_hash, med->v1, med->v2)));
00170                         remove= do_fixes;
00171                 }
00172 
00173                 if(remove == FALSE){
00174                         BLI_edgehash_insert(edge_hash, med->v1, med->v2, SET_INT_IN_POINTER(i));
00175                 }
00176                 else {
00177                         REMOVE_EDGE_TAG(med);
00178                 }
00179         }
00180 
00181         for(i=0, mf=mfaces, sf=sort_faces; i<totface; i++, mf++) {
00182                 int remove= FALSE;
00183                 int fidx;
00184                 unsigned int fv[4];
00185 
00186                 fidx = mf->v4 ? 3:2;
00187                 do {
00188                         fv[fidx]= *(&(mf->v1) + fidx);
00189                         if(fv[fidx] >= totvert) {
00190                                 PRINT("    face %d: 'v%d' index out of range, %d\n", i, fidx + 1, fv[fidx]);
00191                                 remove= do_fixes;
00192                         }
00193                 } while (fidx--);
00194 
00195                 if(remove == FALSE) {
00196                         if(mf->v4) {
00197                                 if(mf->v1 == mf->v2) { PRINT("    face %d: verts invalid, v1/v2 both %d\n", i, mf->v1); remove= do_fixes; }
00198                                 if(mf->v1 == mf->v3) { PRINT("    face %d: verts invalid, v1/v3 both %d\n", i, mf->v1); remove= do_fixes;  }
00199                                 if(mf->v1 == mf->v4) { PRINT("    face %d: verts invalid, v1/v4 both %d\n", i, mf->v1); remove= do_fixes;  }
00200 
00201                                 if(mf->v2 == mf->v3) { PRINT("    face %d: verts invalid, v2/v3 both %d\n", i, mf->v2); remove= do_fixes;  }
00202                                 if(mf->v2 == mf->v4) { PRINT("    face %d: verts invalid, v2/v4 both %d\n", i, mf->v2); remove= do_fixes;  }
00203 
00204                                 if(mf->v3 == mf->v4) { PRINT("    face %d: verts invalid, v3/v4 both %d\n", i, mf->v3); remove= do_fixes;  }
00205                         }
00206                         else {
00207                                 if(mf->v1 == mf->v2) { PRINT("    faceT %d: verts invalid, v1/v2 both %d\n", i, mf->v1); remove= do_fixes; }
00208                                 if(mf->v1 == mf->v3) { PRINT("    faceT %d: verts invalid, v1/v3 both %d\n", i, mf->v1); remove= do_fixes; }
00209 
00210                                 if(mf->v2 == mf->v3) { PRINT("    faceT %d: verts invalid, v2/v3 both %d\n", i, mf->v2); remove= do_fixes; }
00211                         }
00212 
00213                         if(remove == FALSE) {
00214                                 if(totedge) {
00215                                         if(mf->v4) {
00216                                                 if(!BLI_edgehash_haskey(edge_hash, mf->v1, mf->v2)) { PRINT("    face %d: edge v1/v2 (%d,%d) is missing egde data\n", i, mf->v1, mf->v2); do_edge_recalc= TRUE; }
00217                                                 if(!BLI_edgehash_haskey(edge_hash, mf->v2, mf->v3)) { PRINT("    face %d: edge v2/v3 (%d,%d) is missing egde data\n", i, mf->v2, mf->v3); do_edge_recalc= TRUE; }
00218                                                 if(!BLI_edgehash_haskey(edge_hash, mf->v3, mf->v4)) { PRINT("    face %d: edge v3/v4 (%d,%d) is missing egde data\n", i, mf->v3, mf->v4); do_edge_recalc= TRUE; }
00219                                                 if(!BLI_edgehash_haskey(edge_hash, mf->v4, mf->v1)) { PRINT("    face %d: edge v4/v1 (%d,%d) is missing egde data\n", i, mf->v4, mf->v1); do_edge_recalc= TRUE; }
00220                                         }
00221                                         else {
00222                                                 if(!BLI_edgehash_haskey(edge_hash, mf->v1, mf->v2)) { PRINT("    face %d: edge v1/v2 (%d,%d) is missing egde data\n", i, mf->v1, mf->v2); do_edge_recalc= TRUE; }
00223                                                 if(!BLI_edgehash_haskey(edge_hash, mf->v2, mf->v3)) { PRINT("    face %d: edge v2/v3 (%d,%d) is missing egde data\n", i, mf->v2, mf->v3); do_edge_recalc= TRUE; }
00224                                                 if(!BLI_edgehash_haskey(edge_hash, mf->v3, mf->v1)) { PRINT("    face %d: edge v3/v1 (%d,%d) is missing egde data\n", i, mf->v3, mf->v1); do_edge_recalc= TRUE; }
00225                                         }
00226                                 }
00227 
00228                                 sf->index = i;
00229 
00230                                 if(mf->v4) {
00231                                         edge_store_from_mface_quad(sf->es, mf);
00232 
00233                                         qsort(sf->es, 4, sizeof(int64_t), int64_cmp);
00234                                 }
00235                                 else {
00236                                         edge_store_from_mface_tri(sf->es, mf);
00237                                         qsort(sf->es, 3, sizeof(int64_t), int64_cmp);
00238                                 }
00239 
00240                                 totsortface++;
00241                                 sf++;
00242                         }
00243                 }
00244                 if(remove) {
00245                         REMOVE_FACE_TAG(mf);
00246                 }
00247         }
00248 
00249         qsort(sort_faces, totsortface, sizeof(SortFace), search_face_cmp);
00250 
00251         sf= sort_faces;
00252         sf_prev= sf;
00253         sf++;
00254 
00255         for(i=1; i<totsortface; i++, sf++) {
00256                 int remove= FALSE;
00257                 /* on a valid mesh, code below will never run */
00258                 if(memcmp(sf->es, sf_prev->es, sizeof(sf_prev->es)) == 0) {
00259                         mf= mfaces + sf->index;
00260 
00261                         if(do_verbose) {
00262                                 mf_prev= mfaces + sf_prev->index;
00263                                 if(mf->v4) {
00264                                         PRINT("    face %d & %d: are duplicates (%d,%d,%d,%d) (%d,%d,%d,%d)\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);
00265                                 }
00266                                 else {
00267                                         PRINT("    face %d & %d: are duplicates (%d,%d,%d) (%d,%d,%d)\n", sf->index, sf_prev->index, mf->v1, mf->v2, mf->v3, mf_prev->v1, mf_prev->v2, mf_prev->v3);
00268                                 }
00269                         }
00270 
00271                         remove= do_fixes;
00272                 }
00273                 else {
00274                         sf_prev= sf;
00275                 }
00276 
00277                 if(remove) {
00278                         REMOVE_FACE_TAG(mf);
00279                 }
00280         }
00281 
00282         BLI_edgehash_free(edge_hash, NULL);
00283         MEM_freeN(sort_faces);
00284 
00285         PRINT("BKE_mesh_validate: finished\n\n");
00286 
00287 #        undef PRINT
00288 #        undef REMOVE_EDGE_TAG
00289 #        undef REMOVE_FACE_TAG
00290 
00291         if(me) {
00292                 if(do_face_free) {
00293                         mesh_strip_loose_faces(me);
00294                 }
00295 
00296                 if (do_edge_free) {
00297                         mesh_strip_loose_edges(me);
00298                 }
00299 
00300                 if(do_fixes && do_edge_recalc) {
00301                         BKE_mesh_calc_edges(me, TRUE);
00302                 }
00303         }
00304 
00305         return (do_face_free || do_edge_free || do_edge_recalc);
00306 }
00307 
00308 int BKE_mesh_validate(Mesh *me, int do_verbose)
00309 {
00310         if(do_verbose) {
00311                 printf("MESH: %s\n", me->id.name+2);
00312         }
00313         return BKE_mesh_validate_arrays(me, me->mvert, me->totvert, me->medge, me->totedge, me->mface, me->totface, do_verbose, TRUE);
00314 }
00315 
00316 int BKE_mesh_validate_dm(DerivedMesh *dm)
00317 {
00318         return BKE_mesh_validate_arrays(NULL, dm->getVertArray(dm), dm->getNumVerts(dm), dm->getEdgeArray(dm), dm->getNumEdges(dm), dm->getFaceArray(dm), dm->getNumFaces(dm), TRUE, FALSE);
00319 }
00320 
00321 void BKE_mesh_calc_edges(Mesh *mesh, int update)
00322 {
00323         CustomData edata;
00324         EdgeHashIterator *ehi;
00325         MFace *mf = mesh->mface;
00326         MEdge *med, *med_orig;
00327         EdgeHash *eh = BLI_edgehash_new();
00328         int i, totedge, totface = mesh->totface;
00329 
00330         if(mesh->totedge==0)
00331                 update= 0;
00332 
00333         if(update) {
00334                 /* assume existing edges are valid
00335                  * useful when adding more faces and generating edges from them */
00336                 med= mesh->medge;
00337                 for(i= 0; i<mesh->totedge; i++, med++)
00338                         BLI_edgehash_insert(eh, med->v1, med->v2, med);
00339         }
00340 
00341         for (i = 0; i < totface; i++, mf++) {
00342                 if (!BLI_edgehash_haskey(eh, mf->v1, mf->v2))
00343                         BLI_edgehash_insert(eh, mf->v1, mf->v2, NULL);
00344                 if (!BLI_edgehash_haskey(eh, mf->v2, mf->v3))
00345                         BLI_edgehash_insert(eh, mf->v2, mf->v3, NULL);
00346 
00347                 if (mf->v4) {
00348                         if (!BLI_edgehash_haskey(eh, mf->v3, mf->v4))
00349                                 BLI_edgehash_insert(eh, mf->v3, mf->v4, NULL);
00350                         if (!BLI_edgehash_haskey(eh, mf->v4, mf->v1))
00351                                 BLI_edgehash_insert(eh, mf->v4, mf->v1, NULL);
00352                 } else {
00353                         if (!BLI_edgehash_haskey(eh, mf->v3, mf->v1))
00354                                 BLI_edgehash_insert(eh, mf->v3, mf->v1, NULL);
00355                 }
00356         }
00357 
00358         totedge = BLI_edgehash_size(eh);
00359 
00360         /* write new edges into a temporary CustomData */
00361         memset(&edata, 0, sizeof(edata));
00362         CustomData_add_layer(&edata, CD_MEDGE, CD_CALLOC, NULL, totedge);
00363 
00364         ehi = BLI_edgehashIterator_new(eh);
00365         med = CustomData_get_layer(&edata, CD_MEDGE);
00366         for(i = 0; !BLI_edgehashIterator_isDone(ehi);
00367                 BLI_edgehashIterator_step(ehi), ++i, ++med) {
00368 
00369                 if(update && (med_orig=BLI_edgehashIterator_getValue(ehi))) {
00370                         *med= *med_orig; /* copy from the original */
00371                 } else {
00372                         BLI_edgehashIterator_getKey(ehi, (int*)&med->v1, (int*)&med->v2);
00373                         med->flag = ME_EDGEDRAW|ME_EDGERENDER|SELECT; /* select for newly created meshes which are selected [#25595] */
00374                 }
00375         }
00376         BLI_edgehashIterator_free(ehi);
00377 
00378         /* free old CustomData and assign new one */
00379         CustomData_free(&mesh->edata, mesh->totedge);
00380         mesh->edata = edata;
00381         mesh->totedge = totedge;
00382 
00383         mesh->medge = CustomData_get_layer(&mesh->edata, CD_MEDGE);
00384 
00385         BLI_edgehash_free(eh, NULL);
00386 }