From 89266673a14f7d06979a0131f4fe1163aafcc14e Mon Sep 17 00:00:00 2001 From: cnr-isti-vclab Date: Thu, 8 Jun 2006 16:20:52 +0000 Subject: [PATCH] fixed some bugs --- wrap/io_trimesh/import_ptx.h | 375 ++++++++++++++++++++++++++++++++--- 1 file changed, 351 insertions(+), 24 deletions(-) diff --git a/wrap/io_trimesh/import_ptx.h b/wrap/io_trimesh/import_ptx.h index 81885386..e63d506e 100644 --- a/wrap/io_trimesh/import_ptx.h +++ b/wrap/io_trimesh/import_ptx.h @@ -57,10 +57,30 @@ namespace vcg { typedef typename OpenMeshType::VertexIterator VertexIterator; typedef typename OpenMeshType::FaceIterator FaceIterator; + struct RANGEMAP_INFO + { + fpos_t pos; + int vn; + int fn; + }; + typedef typename std::vector< RANGEMAP_INFO > RANGEMAP_INFO_TABLE; + + struct PTX_HEAD_INFO + { + int vn; + int fn; + RANGEMAP_INFO_TABLE rmapInfo; + }; // skip a mesh - static bool skipmesh(FILE* fp) + + static bool skipmesh(FILE* fp, CallBackPos *cb=NULL) + { + PTX_HEAD_INFO tab; + return skipmesh(fp, cb); + } + static bool skipmesh(FILE* fp, PTX_HEAD_INFO & tab, CallBackPos *cb=NULL) { int colnum; int rownum; @@ -68,18 +88,23 @@ namespace vcg { char linebuf; if(feof(fp)) return false; + RANGEMAP_INFO ptxInfo; + fgetpos(fp, &ptxInfo.pos ); // getting mesh size; fscanf(fp,"%i\n",&colnum); fscanf(fp,"%i\n",&rownum); + + ptxInfo.vn = rownum*colnum; + ptxInfo.fn = (rownum-1) * (colnum-1) * 2; + char tmp[255]; + sprintf(tmp, "PTX Mesh analysis... mesh %i vert %i face %i", (int)tab.rmapInfo.size(), ptxInfo.vn, ptxInfo.fn); + if ( ( colnum <=0 ) || ( rownum <=0 ) ) return false; - //printf("\n %i x %i \n", rownum, colnum); - //printf(" expect V %i F %i\n",(rownum*colnum),((rownum-1)*(colnum-1)*2)); - if(feof(fp)) return false; - + if(cb) cb( rand()%100, tmp); skiplines = (colnum * rownum) + 8; // have to skip (col * row) lines plus 8 lines for the header for(int ii=0; ii0 ) for (int i=0; i!=meshNumber; ++i) skipmesh(fp, ptxHead, cb); + if (!readPTX( m, fp, mask, meshNumber, cb)) + { + m.Clear(); + return false; + } + clearBadVertex(m, mask, cb); + return true; + } + + static void clearBadVertex(OpenMeshType &m, int mask, CallBackPos *cb=NULL) + { + if(cb) cb(40,"PTX Mesh Loading - remove bad vertex!"); + for(VertexIterator vi = m.vert.begin(); vi != m.vert.end(); vi++) + { + if((*vi).P() == Point3f(0.0, 0.0, 0.0)) + { + (*vi).SetD(); + m.vn--; + } + } + + if(cb) cb(60,"PTX Mesh Loading - remove bad face!"); + bool onlypoints = ((mask & PTX_ONLY_POINTS) != 0); + if(! onlypoints) + { + for(OpenMeshType::FaceIterator fi = m.face.begin(); fi != m.face.end(); fi++) + { + if( ((*fi).V(0)->IsD()) || ((*fi).V(1)->IsD()) || ((*fi).V(2)->IsD()) ) + { + (*fi).SetD(); + m.fn--; + } + } + + // eliminate high angle triangles + int angle = 90; + + //printf(" culling by angle \n"); + float limit = cos( angle*3.14159265358979323846/180.0 ); + Point3f raggio; + + if(cb) cb(85,"PTX Mesh Loading - remove bad face!"); + vcg::tri::UpdateNormals::PerFaceNormalized(m); + for(OpenMeshType::FaceIterator fi = m.face.begin(); fi != m.face.end(); fi++) + if(!(*fi).IsD()) + { + raggio = -((*fi).V(0)->P() + (*fi).V(1)->P() + (*fi).V(2)->P()) / 3.0; + raggio.Normalize(); + if((raggio * (*fi).N()) < limit) + { + (*fi).SetD(); + m.fn--; + } + } + + } + /*if(cb) cb(60,"PTX Mesh Loading RemoveDuplicateVertex"); + tri::Clean::RemoveDuplicateVertex(m); + + if (!onlypoints) + { + if(cb) cb(60,"PTX Mesh Loading RemoveUnreferencedVertex"); + tri::Clean::RemoveUnreferencedVertex(m); + }*/ + if(cb) cb(100,"PTX Mesh Loading finish!"); + + + } //if numMesh == -1 load all mesh - static bool Open( OpenMeshType &m, const char * filename, int numMesh = -1, int mask = PTX_ONLY_POINTS, CallBackPos *cb=NULL) + static bool Open( OpenMeshType &m, const char * filename, int mask = PTX_ONLY_POINTS, CallBackPos *cb=NULL) { FILE *fp; fp = fopen(filename, "rb"); if(fp == NULL) return false; m.Clear(); + m.vn=0; + m.fn=0; + int vn=0; + int fn=0; + //PTX_HEAD_INFO tab; + //tab.clear(); + //while ( skipmesh( fp, tab, cb ) ) {} + /*if ( (vn<=0) && (fn<=0) ) return false; + //VertexIterator vi = Allocator::AddVertices(m,vn); + //OpenMeshType::FaceIterator fi= Allocator::AddFaces(m,fn); + + VertexIterator vi = Allocator::AddVertices(m, tab[20].vn); + FaceIterator fi = Allocator::AddFaces(m, tab[20].fn); + readPTX( m, fp, vi, fi, tab[20], mask, 20, cb); + fclose(fp); + /* return true; if ( numMesh>0 ) - for (int i=0; i!=numMesh; ++i) if (!skipmesh(fp)) return false; + for (int i=0; i!=numMesh; ++i) if (!skipmesh(fp, vn, fn, tab)) return false; int mn=0; if ( numMesh == -1 ) @@ -122,7 +258,7 @@ namespace vcg { } fclose(fp); - +*/ // now i delete all points in (0,0,0) that are unsampled points for(VertexIterator vi = m.vert.begin(); vi != m.vert.end(); vi++) { @@ -148,9 +284,8 @@ namespace vcg { } // eliminate high angle triangles - /*int angle =85; - if(angle != 90) - { + int angle = 90; + printf(" culling by angle \n"); float limit = cos( angle*3.14159265358979323846/180.0 ); Point3f raggio; @@ -168,16 +303,15 @@ namespace vcg { } } - }*/ } - if(cb) cb(60,"PTX Mesh Loading RemoveDuplicateVertex"); + /*if(cb) cb(60,"PTX Mesh Loading RemoveDuplicateVertex"); tri::Clean::RemoveDuplicateVertex(m); if (!onlypoints) { if(cb) cb(60,"PTX Mesh Loading RemoveUnreferencedVertex"); tri::Clean::RemoveUnreferencedVertex(m); - } + }*/ if(cb) cb(100,"PTX Mesh Loading finish!"); return true; } @@ -185,6 +319,7 @@ namespace vcg { + static bool readPTX( OpenMeshType &m, FILE *fp, int mask, int mn, CallBackPos *cb=NULL) { int colnum; @@ -248,7 +383,7 @@ namespace vcg { int vn = rownum*colnum; VertexIterator vi = Allocator::AddVertices(m,vn); - + m.vn = vn; // parse the first line.... if(hascolor) { @@ -264,6 +399,201 @@ namespace vcg { if (computeBbox) m.bbox.SetNull(); + //addthefirstpoint + (*vi).P()[0]=xx; + (*vi).P()[1]=yy; + (*vi).P()[2]=zz; + (*vi).P() = currtrasf * (*vi).P(); + if (computeBbox) m.bbox.Add( (*vi).P() ); + if(hascolor && savecolor) + { + (*vi).C()[0]=rr; + (*vi).C()[1]=gg; + (*vi).C()[2]=bb; + } + vi++; + + // now for each line until end of mesh (row*col)-1 + for(ii=0; ii<((rownum*colnum)-1); ii++) + { + char tmp[255]; + sprintf(tmp, "PTX Mesh Loading... mesh %i", mn); + if(cb) cb((ii*total)/vn, tmp); + + // read the stream + if(hascolor) fscanf(fp,"%f %f %f %f %f %f %f", &xx, &yy, &zz, &rf, &rr, &gg, &bb); + else fscanf(fp,"%f %f %f %f", &xx, &yy, &zz, &rf); + + // add the point + (*vi).P()[0]=xx; + (*vi).P()[1]=yy; + (*vi).P()[2]=zz; + (*vi).P() = currtrasf * (*vi).P(); + if (computeBbox) m.bbox.Add( (*vi).P() ); + if(hascolor && savecolor) + { + (*vi).C()[0]=rr; + (*vi).C()[1]=gg; + (*vi).C()[2]=bb; + } + vi++; + } + + if(! onlypoints) + { + // now i can triangulate + int trinum = (rownum-1) * (colnum-1) * 2; + OpenMeshType::FaceIterator fi= Allocator::AddFaces(m,trinum); + m.fn = trinum; + + int v0i,v1i,v2i, t; + t=0; + for(int rit=0; rit::AddVertices(m,vn); + //m.vn += vn; + // parse the first line.... + if(hascolor) + { + printf("\n hascolor "); + sscanf(linebuf,"%f %f %f %f %f %f %f", &xx, &yy, &zz, &rf, &rr, &gg, &bb); + } + else + { + printf("\n no color "); + sscanf(linebuf,"%f %f %f %f", &xx, &yy, &zz, &rf); + } + + //if (computeBbox) m.bbox.SetNull(); + + //addthefirstpoint (*vi).P()[0]=xx; (*vi).P()[1]=yy; @@ -311,10 +641,6 @@ namespace vcg { } - - m.vn = m.vert.size(); - m.fn = 0; - if(! onlypoints) { @@ -322,10 +648,10 @@ namespace vcg { int trinum = (rownum-1) * (colnum-1) * 2; - OpenMeshType::FaceIterator fi= Allocator::AddFaces(m,trinum); + //OpenMeshType::FaceIterator fi= Allocator::AddFaces(m,trinum); - - m.fn = 0; + // m.fn += trinum; + int v0i,v1i,v2i, t; for(int rit=0; rit