From 45b736926ab41fd5255c38fae3b14fed48c47b40 Mon Sep 17 00:00:00 2001 From: cignoni Date: Tue, 6 Nov 2012 21:54:13 +0000 Subject: [PATCH] Corrected triangulation bug in off file. --- wrap/io_trimesh/import_off.h | 156 ++++++++++++++++++++--------------- 1 file changed, 89 insertions(+), 67 deletions(-) diff --git a/wrap/io_trimesh/import_off.h b/wrap/io_trimesh/import_off.h index 2f3df900..2609ae37 100644 --- a/wrap/io_trimesh/import_off.h +++ b/wrap/io_trimesh/import_off.h @@ -369,81 +369,103 @@ namespace vcg unsigned int f0=0; - // Initial call to the QuadTriangulate with an empty vector to just reset the static set of existing diagonals - std::vector qtmp; - BitQuad::QuadTriangulate(qtmp); + // Initial call to the QuadTriangulate with an empty vector to just reset the static set of existing diagonals + std::vector qtmp; + BitQuad::QuadTriangulate(qtmp); for (unsigned int f=0; f < nFaces; f++) { - f0 = f; - if (stream.fail()) - return InvalidFile; + f0 = f; + if (stream.fail()) + return InvalidFile; - if(cb && (f%1000)==0) - cb(50+f*50/nFaces,"Face Loading"); + if(cb && (f%1000)==0) + cb(50+f*50/nFaces,"Face Loading"); - TokenizeNextLine(stream, tokens); - int vert_per_face = atoi(tokens[0].c_str()); - if(vert_per_face < 3) - return ErrorDegenerateFace; - k = 1; - if (vert_per_face == 3) + TokenizeNextLine(stream, tokens); + int vert_per_face = atoi(tokens[0].c_str()); + if(vert_per_face < 3) + return ErrorDegenerateFace; + k = 1; + if (vert_per_face == 3) + { + for (int j = 0; j < 3; j++) { - for (int j = 0; j < 3; j++) - { - if (k == tokens.size()) // if EOL // Go to next line when needed - { - TokenizeNextLine(stream, tokens); - if (tokens.size() == 0) return InvalidFile; // if EOF - k = 0; - } - - mesh.face[f].V(j) = &(mesh.vert[ atoi(tokens[k].c_str()) ]); - k++; - } + if (k == tokens.size()) // if EOL // Go to next line when needed + { + TokenizeNextLine(stream, tokens); + if (tokens.size() == 0) return InvalidFile; // if EOF + k = 0; + } + + mesh.face[f].V(j) = &(mesh.vert[ atoi(tokens[k].c_str()) ]); + k++; } - else + } + else + { + // The face must be triangulated + unsigned int trigs = vert_per_face-3; // number of extra faces to add + nFaces += trigs; + Allocator::AddFaces(mesh, trigs); + std::vector vertIndices(vert_per_face); + std::vector polygonVect(vert_per_face); // vec of polygon loops used for the triangulation of polygonal face + for (int j=0; j < vert_per_face; j++) { - // The face must be triangulate - unsigned int trigs = vert_per_face-3; // number of extra faces to add - nFaces += trigs; - Allocator::AddFaces(mesh, trigs); - std::vector vertIndices(vert_per_face); - for (int j=0; j < vert_per_face; j++) - { - if (k == tokens.size()) // if EOL // Go to next line when needed - { - TokenizeNextLine(stream, tokens); - if (tokens.size() == 0) return InvalidFile; // if EOF - k = 0; - } - vertIndices[j] = atoi(tokens[k].c_str()); - k++; - } - if(vert_per_face==4) - { // To well triangulate use the bitquad support function that reorders vertex for a simple fan - std::vector q(4); - for(int qqi=0;qqi<4;++qqi) - q[qqi]=& mesh.vert[vertIndices[qqi]]; - BitQuad::QuadTriangulate(q); - for(int qqi=0;qqi<4;++qqi) - vertIndices[qqi] = q[qqi]- & mesh.vert[0]; - } - // standard fan triangulation (we hope the polygon is convex...) - for (int j=0; j<=vert_per_face-3; j++) - { - mesh.face[f+j].V(0) = &(mesh.vert[ vertIndices[0 ] ]); - mesh.face[f+j].V(1) = &(mesh.vert[ vertIndices[1+j] ]); - mesh.face[f+j].V(2) = &(mesh.vert[ vertIndices[2+j] ]); - if (tri::HasPerFaceFlags(mesh)) { - // tag internal polygonal edges as "faux" - if (j>0) mesh.face[f+j].SetF(0); - if (j q(4); + for(int qqi=0;qqi<4;++qqi) + q[qqi]=& mesh.vert[vertIndices[qqi]]; + BitQuad::QuadTriangulate(q); + for(int qqi=0;qqi<4;++qqi) + vertIndices[qqi] = q[qqi]- & mesh.vert[0]; + // build a two face fan + for (int j=0; j<2; j++) + { + mesh.face[f+j].V(0) = &(mesh.vert[ vertIndices[0 ] ]); + mesh.face[f+j].V(1) = &(mesh.vert[ vertIndices[1+j] ]); + mesh.face[f+j].V(2) = &(mesh.vert[ vertIndices[2+j] ]); + if (tri::HasPerFaceFlags(mesh)) { + // tag internal polygonal edges as "faux" + if (j>0) mesh.face[f+j].SetF(0); + if (j indexTriangulatedVect; + // TessellatePlanarPolygon3(polygonVect,indexTriangulatedVect); + std::vector< std::vector > loopVect; + loopVect.push_back(polygonVect); +#ifdef __gl_h_ + //qDebug("OK: using opengl tessellation for a polygon of %i vertices",vertexesPerFace); + vcg::glu_tesselator::tesselate(loopVect, indexTriangulatedVect); +#else + //qDebug("Warning: using fan tessellation for a polygon of %i vertices",vertexesPerFace); + InternalFanTessellator(loopVect, indexTriangulatedVect); +#endif + for (int j=0; j