/////////////////////////////////////////////////////////////////////////////// // // Copyright Leonardo Fischer 2011 - http://coderender.blogspot.com // // Distributed under the licence available in the accompanying file // LICENCE.txt. Please read it before use this code. // /////////////////////////////////////////////////////////////////////////////// #ifndef DCELStream_h #define DCELStream_h #include #include "Mesh.h" template class DCELStream { public: static void read(MeshT& mesh, std::istream &stream); /** Format: version 1 #this is a comment vc fc ec v ... # repeat v for vc times, one for each vertex f ... # repeat f for fc times, one for each face e ... # repeat e for ec times, one for each half edge end */ static void write(const MeshT& mesh, std::ostream &stream); template static void copyDcelData(const MeshT& source, MeshT2& target ) { const MeshT::Vertex* vertex = NULL; const unsigned int numVertices = source.getNumVertices(); const MeshT::Face* face = NULL; const unsigned int numFaces = source.getNumFaces(); const MeshT::HalfEdge* edge = NULL; const unsigned int numEdges = source.getNumHalfEdges(); target.clear(); target.getVertices().reserve( numVertices ); target.getFaces().reserve( numFaces ); target.getHalfEdges().reserve( numEdges ); for( unsigned int vertexId=0; vertexIdgetOrigin() ); ++vertexCount; } target.createTriangularFace( vid[0], vid[1], vid[2] ); // convert here the data from the source to target faces } /* for( unsigned int edgeId=0; edgeId void DCELStream::read(MeshT& mesh, std::istream &stream) { while( stream.good() ) { std::string str; stream >> str; if( str=="version" ) { unsigned int version; stream >> version; switch(version) { case 1: read_v1(mesh, stream); break; default: std::cerr << "Warning: unknown version " << version << std::endl; } } else if( str.at(0)=='#' ) { consumeComment(str, stream); } else { std::cerr << "Unknown line: " << str << std::endl; } } } template void DCELStream::write(const MeshT& mesh, std::ostream &stream) { const MeshT::Vertex* vertex = NULL; const unsigned int numVertices = mesh.getNumVertices(); const MeshT::Face* face = NULL; const unsigned int numFaces = mesh.getNumFaces(); const MeshT::HalfEdge* edge = NULL; const unsigned int numEdges = mesh.getNumHalfEdges(); stream << "version 1" << std::endl; stream << "vc " << numVertices << std::endl; stream << "fc " << numFaces << std::endl; stream << "ec " << numEdges << std::endl; for( unsigned int vertexId=0; vertexIdgetData() << std::endl; } for( unsigned int faceId=0; faceIdgetOrigin() ); ++vertexCount; } stream << vertexCount; for( unsigned int v=0; vgetData(); stream << std::endl; } for( unsigned int edgeId=0; edgeIdgetOrigin() ) << " " << mesh.getVertexId( edge->getTwin()->getOrigin() ) << " " << edge->getData() << std::endl; } stream << "end"; } template void DCELStream::read_v1(MeshT& mesh, std::istream &stream) { std::string str; bool reading = true; while( reading && stream.good() ) { stream >> str; if( str=="v" ) { MeshT::Vertex* vertex = mesh.getVertex( mesh.createVertex() ); stream >> (vertex->getData()); } else if( str=="f" ) { unsigned int vertexCount; stream >> vertexCount; unsigned int vid[3];//TODO generalize for( unsigned int i=0; i> (vid[i]); unsigned int fid = mesh.createTriangularFace( vid[0], vid[1], vid[2] ); MeshT::Face* face = mesh.getFace( fid ); stream >> face->getData(); } else if( str=="e" ) { unsigned int from, to; stream >> from >> to; MeshT::HalfEdge* edge = mesh.getHalfEdge( mesh.getVertex(from), mesh.getVertex(to) ); if( edge ) { stream >> edge->getData(); } else { std::cerr << '?'; } } else if( str=="vc" ) { unsigned int vc = -1; stream >> vc; mesh.getVertices().reserve( vc*5 ); } else if( str=="fc" ) { unsigned int fc = -1; stream >> fc; mesh.getFaces().reserve( fc*5 ); } else if( str=="ec" ) { unsigned int ec = -1; stream >> ec; mesh.getHalfEdges().reserve( ec*5 ); } else if( str=="end" ) { reading = false; } else if( str.at(0)=='#' ) { consumeComment(str, stream); } else { std::cerr << "Unknown line: " << str; } } mesh.manageUnhandledTriangles(); } template void DCELStream::consumeComment(const std::string& commentBegin, std::istream &stream) { char buffer[512]; stream.getline( buffer, sizeof(buffer) ); std::cerr << commentBegin << " " << buffer << std::endl; } #endif//DCELStream_h