From 010b310cac72fe3fddb50812c09f6959b756be3d Mon Sep 17 00:00:00 2001 From: cignoni Date: Mon, 7 Nov 2011 16:14:46 +0000 Subject: [PATCH] Added support of polylines (edges) in PLY support. Used the format suggested in the web description of the ply format ( http://paulbourke.net/dataformats/ply/ ) element edge 5 { five edges in object } property int vertex1 { index to first vertex of edge } property int vertex2 { index to second vertex } --- wrap/io_trimesh/export_ply.h | 28 +++++++++++++++++++++- wrap/io_trimesh/import_ply.h | 45 ++++++++++++++++++++++++++++++++---- wrap/io_trimesh/io_mask.h | 4 +++- 3 files changed, 71 insertions(+), 6 deletions(-) diff --git a/wrap/io_trimesh/export_ply.h b/wrap/io_trimesh/export_ply.h index 38a10717..cbe70691 100644 --- a/wrap/io_trimesh/export_ply.h +++ b/wrap/io_trimesh/export_ply.h @@ -172,6 +172,7 @@ typedef typename SaveMeshType::FaceType FaceType; typedef typename SaveMeshType::FacePointer FacePointer; typedef typename SaveMeshType::VertexIterator VertexIterator; typedef typename SaveMeshType::FaceIterator FaceIterator; +typedef typename SaveMeshType::EdgeIterator EdgeIterator; static int Save(SaveMeshType &m, const char * filename, bool binary=true) { @@ -361,7 +362,14 @@ static int Save(SaveMeshType &m, const char * filename, bool binary, PlyInfo &p for(i=0;i0) + fprintf(fpout, + "element edge %d\n" + "property int vertex1\n" + "property int vertex2\n" + ,m.en + ); fprintf(fpout, "end_header\n" ); // Salvataggio camera @@ -706,6 +714,24 @@ static int Save(SaveMeshType &m, const char * filename, bool binary, PlyInfo &p } } assert(fcnt==m.fn); + int eauxvv[2]; + int ecnt=0; + for(EdgeIterator ei=m.edge.begin();ei!=m.edge.end();++ei) + { + if( ! ei->IsD() ) + { + ++ecnt; + if(binary) + { + eauxvv[0]=indices[ei->cV(0)]; + eauxvv[1]=indices[ei->cV(1)]; + fwrite(vv,sizeof(int),2,fpout); + } + else // ***** ASCII ***** + fprintf(fpout,"%d %d \n", indices[ei->cV(0)], indices[ei->cV(1)]); + } + } + assert(ecnt==m.en); fclose(fpout); return 0; } diff --git a/wrap/io_trimesh/import_ply.h b/wrap/io_trimesh/import_ply.h index b9b12c45..d2fa634b 100644 --- a/wrap/io_trimesh/import_ply.h +++ b/wrap/io_trimesh/import_ply.h @@ -63,6 +63,7 @@ typedef typename OpenMeshType::VertexType VertexType; typedef typename OpenMeshType::FaceType FaceType; typedef typename OpenMeshType::VertexIterator VertexIterator; typedef typename OpenMeshType::FaceIterator FaceIterator; + typedef typename OpenMeshType::EdgeIterator EdgeIterator; //template int PlyType () { assert(0); return 0;} @@ -94,6 +95,11 @@ struct LoadPly_TristripAux unsigned char data[MAX_USER_DATA]; }; +struct LoadPly_EdgeAux +{ + int v1,v2; + unsigned char data[MAX_USER_DATA]; +}; // Yet another auxiliary data structure for loading some strange ply files // the original stanford range data... @@ -228,6 +234,16 @@ static const PropDescriptor &TristripDesc(int i) return qf[i]; } +static const PropDescriptor &EdgeDesc(int i) +{ + static const PropDescriptor qf[2]= + { + {"edge","vertex1", ply::T_INT, ply::T_INT, offsetof(LoadPly_EdgeAux,v1), 0,0,0,0,0 ,0}, + {"edge","vertex2", ply::T_INT, ply::T_INT, offsetof(LoadPly_EdgeAux,v2), 0,0,0,0,0 ,0}, + }; + return qf[i]; +} + // Descriptor for the Stanford Data Repository Range Maps. // In practice a grid with some invalid elements. Coords are saved only for good elements static const PropDescriptor &RangeDesc(int i) @@ -279,7 +295,7 @@ static const char *ErrorMsg(int error) ply_error_msg[ply::E_NOERROR ]="No errors"; ply_error_msg[ply::E_CANTOPEN ]="Can't open file"; ply_error_msg[ply::E_NOTHEADER ]="Header not found"; - ply_error_msg[ply::E_UNESPECTEDEOF ]="Eof in header"; + ply_error_msg[ply::E_UNESPECTEDEOF ]="Eof in header"; ply_error_msg[ply::E_NOFORMAT ]="Format not found"; ply_error_msg[ply::E_SYNTAX ]="Syntax error on header"; ply_error_msg[ply::E_PROPOUTOFELEMENT]="Property without element"; @@ -339,6 +355,7 @@ static int Open( OpenMeshType &m, const char * filename, PlyInfo &pi ) assert(filename!=0); std::vector index; LoadPly_FaceAux fa; + LoadPly_EdgeAux ea; LoadPly_TristripAux tsa; LoadPly_VertAux va; @@ -416,9 +433,11 @@ static int Open( OpenMeshType &m, const char * filename, PlyInfo &pi ) } // Descrittori facoltativi dei flags - + if(pf.AddToRead(EdgeDesc(0) )!= -1 && pf.AddToRead(EdgeDesc(1)) != -1 ) + pi.mask |= Mask::IOM_EDGEINDEX; + if(VertexType::HasFlags() && pf.AddToRead(VertDesc(3))!=-1 ) - pi.mask |= Mask::IOM_VERTFLAGS; + pi.mask |= Mask::IOM_VERTFLAGS; if( VertexType::HasNormal() ) { @@ -677,7 +696,25 @@ static int Open( OpenMeshType &m, const char * filename, PlyInfo &pi ) for(j=0,vi=m.vert.begin();j0) )/************************************************************/ + else if( !strcmp( pf.ElemName(i),"edge") && (n>0) )/******************** EDGE READING *******************************/ + { + assert( pi.mask & Mask::IOM_EDGEINDEX ); + EdgeIterator ei=Allocator::AddEdges(m,n); + pf.SetCurElement(i); + for(int j=0;j0) )/******************** FACE READING ****************************************/ { int j; diff --git a/wrap/io_trimesh/io_mask.h b/wrap/io_trimesh/io_mask.h index 1d3ee4ce..381f920b 100644 --- a/wrap/io_trimesh/io_mask.h +++ b/wrap/io_trimesh/io_mask.h @@ -72,7 +72,9 @@ enum { IOM_VERTQUALITY = 0x00008, IOM_VERTNORMAL = 0x00010, IOM_VERTTEXCOORD = 0x00020, - IOM_VERTRADIUS = 0x10000, + IOM_VERTRADIUS = 0x10000, + + IOM_EDGEINDEX = 0x80000, IOM_FACEINDEX = 0x00040, IOM_FACEFLAGS = 0x00080,