From efa67fbcfc316110219e7acca1c9b1c192493aec Mon Sep 17 00:00:00 2001 From: Mike Kremer Date: Tue, 17 Nov 2009 13:54:16 +0000 Subject: [PATCH] Commit of the debugged version of Alex' improvement to OpenMesh. File i/o is now done via istream/ostream instances such that direct buffer writing to files (serialization) will also be possible in future releases. Code is tested. git-svn-id: http://www.openmesh.org/svnrepo/OpenMesh/trunk@221 fdac6126-5c0c-442c-9429-916003d36597 --- src/OpenMesh/Core/IO/IOManager.cc | 53 ++++++++ src/OpenMesh/Core/IO/IOManager.hh | 22 ++- src/OpenMesh/Core/IO/MeshIO.hh | 33 +++++ src/OpenMesh/Core/IO/reader/BaseReader.hh | 8 ++ src/OpenMesh/Core/IO/reader/OBJReader.cc | 13 +- src/OpenMesh/Core/IO/reader/OBJReader.hh | 14 +- src/OpenMesh/Core/IO/reader/OFFReader.cc | 105 +++++++------- src/OpenMesh/Core/IO/reader/OFFReader.hh | 35 +++-- src/OpenMesh/Core/IO/reader/OMReader.cc | 158 ++++++++++++---------- src/OpenMesh/Core/IO/reader/OMReader.hh | 6 +- src/OpenMesh/Core/IO/reader/PLYReader.cc | 23 +++- src/OpenMesh/Core/IO/reader/PLYReader.hh | 15 +- src/OpenMesh/Core/IO/reader/STLReader.cc | 67 +++++---- src/OpenMesh/Core/IO/reader/STLReader.hh | 21 +-- src/OpenMesh/Core/IO/writer/BaseWriter.hh | 21 +-- src/OpenMesh/Core/IO/writer/OBJWriter.cc | 4 +- src/OpenMesh/Core/IO/writer/OBJWriter.hh | 6 +- src/OpenMesh/Core/IO/writer/OFFWriter.cc | 53 +++++++- src/OpenMesh/Core/IO/writer/OFFWriter.hh | 12 +- src/OpenMesh/Core/IO/writer/OMWriter.hh | 2 + src/OpenMesh/Core/IO/writer/PLYWriter.cc | 44 +++++- src/OpenMesh/Core/IO/writer/PLYWriter.hh | 12 +- src/OpenMesh/Core/IO/writer/STLWriter.cc | 11 ++ src/OpenMesh/Core/IO/writer/STLWriter.hh | 2 + 24 files changed, 505 insertions(+), 235 deletions(-) diff --git a/src/OpenMesh/Core/IO/IOManager.cc b/src/OpenMesh/Core/IO/IOManager.cc index 83a2e278..da56f811 100644 --- a/src/OpenMesh/Core/IO/IOManager.cc +++ b/src/OpenMesh/Core/IO/IOManager.cc @@ -102,6 +102,31 @@ read(const std::string& _filename, BaseImporter& _bi, Options& _opt) //----------------------------------------------------------------------------- +bool +_IOManager_:: +read(std::istream& _is, const std::string& _ext, BaseImporter& _bi, Options& _opt) +{ + std::set::const_iterator it = reader_modules_.begin(); + std::set::const_iterator it_end = reader_modules_.end(); + + // Try all registered modules + for(; it != it_end; ++it) + if ((*it)->BaseReader::can_u_read(_ext)) //Use the extension check only (no file existence) + { + _bi.prepare(); + bool ok = (*it)->read(_is, _bi, _opt); + _bi.finish(); + return ok; + } + + // All modules failed to read + return false; +} + + +//----------------------------------------------------------------------------- + + bool _IOManager_:: write(const std::string& _filename, BaseExporter& _be, Options _opt) @@ -128,6 +153,34 @@ write(const std::string& _filename, BaseExporter& _be, Options _opt) return false; } +//----------------------------------------------------------------------------- + + +bool +_IOManager_:: +write(std::ostream& _os,const std::string &_ext, BaseExporter& _be, Options _opt) +{ + std::set::const_iterator it = writer_modules_.begin(); + std::set::const_iterator it_end = writer_modules_.end(); + + if ( it == it_end ) + { + omerr() << "[OpenMesh::IO::_IOManager_] No writing modules available!\n"; + return false; + } + + // Try all registered modules + for(; it != it_end; ++it) + { + if ((*it)->BaseWriter::can_u_write(_ext)) //Restrict test to the extension check + { + return (*it)->write(_os, _be, _opt); + } + } + + // All modules failed to save + return false; +} //----------------------------------------------------------------------------- diff --git a/src/OpenMesh/Core/IO/IOManager.hh b/src/OpenMesh/Core/IO/IOManager.hh index de7d6c5b..224d1b08 100644 --- a/src/OpenMesh/Core/IO/IOManager.hh +++ b/src/OpenMesh/Core/IO/IOManager.hh @@ -118,6 +118,16 @@ public: BaseImporter& _bi, Options& _opt); +/** + Read a mesh from open std::istream _is. The target data structure is specified + by the given BaseImporter. The \c sread method consecutively queries all + of its reader modules. True is returned upon success, false if all + reader modules failed to use _is. + */ + bool read(std::istream& _filename, + const std::string& _ext, + BaseImporter& _bi, + Options& _opt); /** Write a mesh to file _filename. The source data structure is specified @@ -129,7 +139,17 @@ public: bool write(const std::string& _filename, BaseExporter& _be, Options _opt=Options::Default); - + +/** Write a mesh to open std::ostream _os. The source data structure is specified + by the given BaseExporter. The \c save method consecutively queries all + of its writer modules. True is returned upon success, false if all + writer modules failed to write the requested format. + Options is determined by _filename's extension. + */ + bool write(std::ostream& _filename, + const std::string& _ext, + BaseExporter& _be, + Options _opt=Options::Default); /// Returns true if the format is supported by one of the reader modules. diff --git a/src/OpenMesh/Core/IO/MeshIO.hh b/src/OpenMesh/Core/IO/MeshIO.hh index 0c9d6d5c..cafba927 100644 --- a/src/OpenMesh/Core/IO/MeshIO.hh +++ b/src/OpenMesh/Core/IO/MeshIO.hh @@ -107,6 +107,23 @@ read_mesh(Mesh& _mesh, } +/** Read a mesh from file open std::istream. The file format is determined by + parameter _ext. _ext has to include ".[format]" in order to work properly */ +template +bool +read_mesh(Mesh& _mesh, + std::istream& _is, + const std::string& _ext, + Options& _opt, + bool _clear = true) +{ + if (_clear) _mesh.clear(); + ImporterT importer(_mesh); + return IOManager().read(_is,_ext, importer, _opt); +} + + + //----------------------------------------------------------------------------- @@ -124,6 +141,22 @@ bool write_mesh(const Mesh& _mesh, const std::string& _filename, //----------------------------------------------------------------------------- +/** Write a mesh to an open std::ostream. The file format is determined + by _ext. */ +template +bool write_mesh(const Mesh& _mesh, + std::ostream& _os, + const std::string& _ext, + Options _opt = Options::Default) +{ + ExporterT exporter(_mesh); + return IOManager().write(_os,_ext, exporter, _opt); +} + + +//----------------------------------------------------------------------------- + + template size_t binary_size(const Mesh& _mesh, const std::string& _format, Options _opt = Options::Default) diff --git a/src/OpenMesh/Core/IO/reader/BaseReader.hh b/src/OpenMesh/Core/IO/reader/BaseReader.hh index 0fb96da1..f33153f1 100644 --- a/src/OpenMesh/Core/IO/reader/BaseReader.hh +++ b/src/OpenMesh/Core/IO/reader/BaseReader.hh @@ -110,6 +110,14 @@ public: virtual bool read(const std::string& _filename, BaseImporter& _bi, Options& _opt) = 0; + + /** Reads a mesh given by a std::stream. This method usually uses the same stream reading method + that read uses. Options can be passed via _opt. After execution _opt contains the Options + that were available + */ + virtual bool read(std::istream& _is, + BaseImporter& _bi, + Options& _opt) = 0; /// Returns true if reader can parse _filename (checks extension) diff --git a/src/OpenMesh/Core/IO/reader/OBJReader.cc b/src/OpenMesh/Core/IO/reader/OBJReader.cc index 52692a18..004b5375 100644 --- a/src/OpenMesh/Core/IO/reader/OBJReader.cc +++ b/src/OpenMesh/Core/IO/reader/OBJReader.cc @@ -4,10 +4,10 @@ * Copyright (C) 2001-2009 by Computer Graphics Group, RWTH Aachen * * www.openmesh.org * * * - *---------------------------------------------------------------------------* + *---------------------------------------------------------------------------* * This file is part of OpenMesh. * * * - * OpenMesh is free software: you can redistribute it and/or modify * + * OpenMesh is free software: you can redistribute it and/or modify * * it under the terms of the GNU Lesser General Public License as * * published by the Free Software Foundation, either version 3 of * * the License, or (at your option) any later version with the * @@ -30,10 +30,10 @@ * License along with OpenMesh. If not, * * see . * * * -\*===========================================================================*/ +\*===========================================================================*/ /*===========================================================================*\ - * * + * * * $Revision$ * * $Date$ * * * @@ -112,7 +112,7 @@ read(const std::string& _filename, BaseImporter& _bi, Options& _opt) { std::fstream in( _filename.c_str(), std::ios_base::in ); - if (!in) + if (!in.is_open() || !in.good()) { omerr() << "[OBJReader] : cannot not open file " << _filename @@ -137,7 +137,6 @@ read(const std::string& _filename, BaseImporter& _bi, Options& _opt) return result; } - //----------------------------------------------------------------------------- bool @@ -264,7 +263,7 @@ read_material(std::fstream& _in) bool _OBJReader_:: -read(std::fstream& _in, BaseImporter& _bi, Options& _opt) +read(std::istream& _in, BaseImporter& _bi, Options& _opt) { omlog() << "[OBJReader] : read file\n"; diff --git a/src/OpenMesh/Core/IO/reader/OBJReader.hh b/src/OpenMesh/Core/IO/reader/OBJReader.hh index 3407d91b..5543df1b 100644 --- a/src/OpenMesh/Core/IO/reader/OBJReader.hh +++ b/src/OpenMesh/Core/IO/reader/OBJReader.hh @@ -4,10 +4,10 @@ * Copyright (C) 2001-2009 by Computer Graphics Group, RWTH Aachen * * www.openmesh.org * * * - *---------------------------------------------------------------------------* + *---------------------------------------------------------------------------* * This file is part of OpenMesh. * * * - * OpenMesh is free software: you can redistribute it and/or modify * + * OpenMesh is free software: you can redistribute it and/or modify * * it under the terms of the GNU Lesser General Public License as * * published by the Free Software Foundation, either version 3 of * * the License, or (at your option) any later version with the * @@ -30,10 +30,10 @@ * License along with OpenMesh. If not, * * see . * * * -\*===========================================================================*/ +\*===========================================================================*/ /*===========================================================================*\ - * * + * * * $Revision$ * * $Date$ * * * @@ -94,6 +94,10 @@ public: BaseImporter& _bi, Options& _opt); + bool read(std::istream& _in, + BaseImporter& _bi, + Options& _opt); + private: #ifndef DOXY_IGNORE_THIS @@ -163,8 +167,6 @@ private: private: - bool read(std::fstream& _in, BaseImporter& _bi, Options& _opt); - std::string path_; }; diff --git a/src/OpenMesh/Core/IO/reader/OFFReader.cc b/src/OpenMesh/Core/IO/reader/OFFReader.cc index 182e93f6..5f015389 100644 --- a/src/OpenMesh/Core/IO/reader/OFFReader.cc +++ b/src/OpenMesh/Core/IO/reader/OFFReader.cc @@ -4,10 +4,10 @@ * Copyright (C) 2001-2009 by Computer Graphics Group, RWTH Aachen * * www.openmesh.org * * * - *---------------------------------------------------------------------------* + *---------------------------------------------------------------------------* * This file is part of OpenMesh. * * * - * OpenMesh is free software: you can redistribute it and/or modify * + * OpenMesh is free software: you can redistribute it and/or modify * * it under the terms of the GNU Lesser General Public License as * * published by the Free Software Foundation, either version 3 of * * the License, or (at your option) any later version with the * @@ -30,10 +30,10 @@ * License along with OpenMesh. If not, * * see . * * * -\*===========================================================================*/ +\*===========================================================================*/ /*===========================================================================*\ - * * + * * * $Revision$ * * $Date$ * * * @@ -57,6 +57,8 @@ #include //STL +#include +#include #include #include @@ -84,48 +86,55 @@ _OFFReader_& OFFReader() { return __OFFReaderInstance; } -_OFFReader_::_OFFReader_() -{ - IOManager().register_module(this); +_OFFReader_::_OFFReader_() +{ + IOManager().register_module(this); } //----------------------------------------------------------------------------- -bool -_OFFReader_::read(const std::string& _filename, BaseImporter& _bi, +bool +_OFFReader_::read(const std::string& _filename, BaseImporter& _bi, Options& _opt) { - std::fstream in(_filename.c_str(), (options_.is_binary() ? std::ios_base::binary | std::ios_base::in - : std::ios_base::in) ); + std::ifstream ifile(_filename.c_str(), (options_.is_binary() ? std::ios::binary | std::ios::in + : std::ios::in) ); - if (!in) + if (!ifile.is_open() || !ifile.good()) { - omerr() << "[OFFReader] : cannot not open file " + omerr() << "[OFFReader] : cannot not open file " << _filename << std::endl; + return false; } + assert(ifile); - bool result = read(in, _bi, _opt); - + bool result = read(ifile, _bi, _opt); - in.close(); + ifile.close(); return result; } - //----------------------------------------------------------------------------- -bool -_OFFReader_::read(std::fstream& _in, BaseImporter& _bi, Options& _opt ) const +bool +_OFFReader_::read(std::istream& _in, BaseImporter& _bi, Options& _opt ) { + if (!_in.good()) + { + omerr() << "[OMReader] : cannot not use stream " + << std::endl; + return false; + } + // filter relevant options for reading bool swap = _opt.check( Options::Swap ); - + userOptions_ = _opt; @@ -152,13 +161,13 @@ _OFFReader_::read(std::fstream& _in, BaseImporter& _bi, Options& _opt ) const //----------------------------------------------------------------------------- -bool -_OFFReader_::read_ascii(std::fstream& _in, BaseImporter& _bi) const +bool +_OFFReader_::read_ascii(std::istream& _in, BaseImporter& _bi) const { omlog() << "[OFFReader] : read ascii file\n"; - + unsigned int i, j, k, l, idx; unsigned int nV, nF, dummy; OpenMesh::Vec3f v, n; @@ -186,9 +195,9 @@ omlog() << "[OFFReader] : read ascii file\n"; { // Always read VERTEX _in >> v[0]; _in >> v[1]; _in >> v[2]; - + vh = _bi.add_vertex(v); - + //perhaps read NORMAL if ( options_.vertex_has_normal() ){ @@ -236,7 +245,7 @@ omlog() << "[OFFReader] : read ascii file\n"; _bi.set_color( vh, color_cast(c4f) ); break; - default: + default: std::cerr << "Error in file format (colorType = " << colorType << ")\n"; } } @@ -266,7 +275,7 @@ omlog() << "[OFFReader] : read ascii file\n"; vhandles[1] = VertexHandle(k); vhandles[2] = VertexHandle(l); } - else + else { vhandles.clear(); for (j=0; j(c4f) ); break; - default: + default: std::cerr << "Error in file format (colorType = " << colorType << ")\n"; } } @@ -369,7 +378,7 @@ int _OFFReader_::getColorType(std::string& _line, bool _texCoordsAvailable) cons //get first item found = _line.find(" "); std::string c1 = _line.substr (0,found); - + if (c1.find(".") != std::string::npos){ if (count == 3) count = 5; @@ -380,32 +389,32 @@ int _OFFReader_::getColorType(std::string& _line, bool _texCoordsAvailable) cons return count; } -void _OFFReader_::readValue(std::fstream& _in, float& _value) const{ +void _OFFReader_::readValue(std::istream& _in, float& _value) const{ float32_t tmp; restore( _in , tmp, false ); //assuming LSB byte order _value = tmp; } -void _OFFReader_::readValue(std::fstream& _in, int& _value) const{ +void _OFFReader_::readValue(std::istream& _in, int& _value) const{ uint32_t tmp; restore( _in , tmp, false ); //assuming LSB byte order _value = tmp; } -void _OFFReader_::readValue(std::fstream& _in, unsigned int& _value) const{ +void _OFFReader_::readValue(std::istream& _in, unsigned int& _value) const{ uint32_t tmp; restore( _in , tmp, false ); //assuming LSB byte order _value = tmp; } -bool -_OFFReader_::read_binary(std::fstream& _in, BaseImporter& _bi, bool /*_swap*/) const +bool +_OFFReader_::read_binary(std::istream& _in, BaseImporter& _bi, bool /*_swap*/) const { omlog() << "[OFFReader] : read binary file\n"; - + unsigned int i, j, k, l, idx; unsigned int nV, nF, dummy; OpenMesh::Vec3f v, n; @@ -426,16 +435,16 @@ _OFFReader_::read_binary(std::fstream& _in, BaseImporter& _bi, bool /*_swap*/) c _bi.reserve(nV, 3*nV, nF); - // read vertices: coord [hcoord] [normal] [color] [texcoord] + // read vertices: coord [hcoord] [normal] [color] [texcoord] for (i=0; i .. [color spec] // So far color spec is unsupported! @@ -494,7 +503,7 @@ _OFFReader_::read_binary(std::fstream& _in, BaseImporter& _bi, bool /*_swap*/) c vhandles[1] = VertexHandle(k); vhandles[2] = VertexHandle(l); } - else + else { vhandles.clear(); for (j=0; j 0 ) && ( p[0] == 'n') ) { vertexDimensionTooHigh = true; ++p; --remainingChars; } - + if ( ( remainingChars < 3 ) || (!(p[0] == 'O' && p[1] == 'F' && p[2] == 'F') ) ) return false; diff --git a/src/OpenMesh/Core/IO/reader/OFFReader.hh b/src/OpenMesh/Core/IO/reader/OFFReader.hh index 81c580bd..a66b0435 100644 --- a/src/OpenMesh/Core/IO/reader/OFFReader.hh +++ b/src/OpenMesh/Core/IO/reader/OFFReader.hh @@ -4,10 +4,10 @@ * Copyright (C) 2001-2009 by Computer Graphics Group, RWTH Aachen * * www.openmesh.org * * * - *---------------------------------------------------------------------------* + *---------------------------------------------------------------------------* * This file is part of OpenMesh. * * * - * OpenMesh is free software: you can redistribute it and/or modify * + * OpenMesh is free software: you can redistribute it and/or modify * * it under the terms of the GNU Lesser General Public License as * * published by the Free Software Foundation, either version 3 of * * the License, or (at your option) any later version with the * @@ -30,10 +30,10 @@ * License along with OpenMesh. If not, * * see . * * * -\*===========================================================================*/ +\*===========================================================================*/ /*===========================================================================*\ - * * + * * * $Revision$ * * $Date$ * * * @@ -83,8 +83,8 @@ class BaseImporter; //== IMPLEMENTATION =========================================================== -/** - Implementation of the OFF format reader. This class is singleton'ed by +/** + Implementation of the OFF format reader. This class is singleton'ed by SingletonT to OFFReader. By passing Options to the read function you can manipulate the reading behavoir. @@ -123,29 +123,28 @@ public: std::string get_description() const { return "Object File Format"; } std::string get_extensions() const { return "off"; } std::string get_magic() const { return "OFF"; } - - bool read(const std::string& _filename, - BaseImporter& _bi, + + bool read(const std::string& _filename, + BaseImporter& _bi, Options& _opt); bool can_u_read(const std::string& _filename) const; - + bool read(std::istream& _in, BaseImporter& _bi, Options& _opt ); private: bool can_u_read(std::istream& _is) const; - bool read(std::fstream& _in, BaseImporter& _bi, Options& _opt ) const; - bool read_ascii(std::fstream& _in, BaseImporter& _bi) const; - bool read_binary(std::fstream& _in, BaseImporter& _bi, bool swap) const; - - void readValue(std::fstream& _in, float& _value) const; - void readValue(std::fstream& _in, int& _value) const; - void readValue(std::fstream& _in, unsigned int& _value) const; + bool read_ascii(std::istream& _in, BaseImporter& _bi) const; + bool read_binary(std::istream& _in, BaseImporter& _bi, bool swap) const; + + void readValue(std::istream& _in, float& _value) const; + void readValue(std::istream& _in, int& _value) const; + void readValue(std::istream& _in, unsigned int& _value) const; int getColorType(std::string & _line, bool _texCoordsAvailable) const; - + //available options for reading mutable Options options_; //options that the user wants to read diff --git a/src/OpenMesh/Core/IO/reader/OMReader.cc b/src/OpenMesh/Core/IO/reader/OMReader.cc index c9eb46a7..ccc688f7 100644 --- a/src/OpenMesh/Core/IO/reader/OMReader.cc +++ b/src/OpenMesh/Core/IO/reader/OMReader.cc @@ -4,10 +4,10 @@ * Copyright (C) 2001-2009 by Computer Graphics Group, RWTH Aachen * * www.openmesh.org * * * - *---------------------------------------------------------------------------* + *---------------------------------------------------------------------------* * This file is part of OpenMesh. * * * - * OpenMesh is free software: you can redistribute it and/or modify * + * OpenMesh is free software: you can redistribute it and/or modify * * it under the terms of the GNU Lesser General Public License as * * published by the Free Software Foundation, either version 3 of * * the License, or (at your option) any later version with the * @@ -30,10 +30,10 @@ * License along with OpenMesh. If not, * * see . * * * -\*===========================================================================*/ +\*===========================================================================*/ /*===========================================================================*\ - * * + * * * $Revision$ * * $Date$ * * * @@ -75,15 +75,15 @@ _OMReader_& OMReader() { return __OMReaderInstance; } _OMReader_::_OMReader_() { - IOManager().register_module(this); + IOManager().register_module(this); } //----------------------------------------------------------------------------- -bool -_OMReader_::read(const std::string& _filename, +bool +_OMReader_::read(const std::string& _filename, BaseImporter& _bi, Options& _opt) { @@ -95,42 +95,58 @@ _OMReader_::read(const std::string& _filename, // Open file std::ifstream ifs( _filename.c_str(), std::ios::binary ); - if (!ifs.is_open()) + if (!ifs.is_open() || !ifs.good()) { - omerr() << "[OMReader] : cannot not open file " + omerr() << "[OMReader] : cannot not open file " << _filename << std::endl; return false; } - + // Pass stream to read method, remember result bool result = read(ifs, _bi, _opt); - + // close input stream ifs.close(); - + return result; } - //----------------------------------------------------------------------------- -bool -_OMReader_::read(std::istream& _is, BaseImporter& _bi, Options& _opt ) const +bool +_OMReader_::read(std::istream& _is, + BaseImporter& _bi, + Options& _opt) { - // currently only binary file format is supported - _opt += Options::Binary; + // check whether importer can give us an OpenMesh BaseKernel + if (!_bi.kernel()) return false; + + + _opt += Options::Binary; // only binary format supported! + + if (!_is.good()) + { + omerr() << "[OMReader] : cannot read from stream " + << std::endl; + return false; + } + + // Pass stream to read method, remember result + bool result = read_binary(_is, _bi, _opt); + + if(result) _opt += Options::Binary; - return read_binary( _is, _bi, _opt ); + return result; } //----------------------------------------------------------------------------- -bool -_OMReader_::read_ascii( std::istream& /* _is */, BaseImporter& /* _bi */, +bool +_OMReader_::read_ascii( std::istream& /* _is */, BaseImporter& /* _bi */, Options& /* _opt */) const { // not supported yet! @@ -140,14 +156,14 @@ _OMReader_::read_ascii( std::istream& /* _is */, BaseImporter& /* _bi */, //----------------------------------------------------------------------------- -bool -_OMReader_::read_binary( std::istream& _is, BaseImporter& _bi, +bool +_OMReader_::read_binary( std::istream& _is, BaseImporter& _bi, Options& _opt) const { bool swap = _opt.check(Options::Swap) || (Endian::local() == Endian::MSB); // intialize byte counter - bytes_ = 0; + bytes_ = 0; bytes_ += restore( _is, header_, swap ); @@ -172,7 +188,7 @@ _OMReader_::read_binary( std::istream& _is, BaseImporter& _bi, data_bytes = bytes_; switch( chunk_header_.entity_ ) { - case OMFormat::Chunk::Entity_Vertex: + case OMFormat::Chunk::Entity_Vertex: if (!read_binary_vertex_chunk( _is, _bi, _opt, swap )) return false; break; @@ -214,13 +230,13 @@ _OMReader_::can_u_read(const std::string& _filename) const { std::ifstream ifile( _filename.c_str() ); if ( ifile && can_u_read( ifile ) ) - return true; + return true; } return false; } //----------------------------------------------------------------------------- - + bool _OMReader_::can_u_read(std::istream& _is) const { @@ -231,7 +247,7 @@ _OMReader_::can_u_read(std::istream& _is) const while( evt.size() < 4 ) evt.push_back( static_cast(_is.get()) ); - // put back all read characters + // put back all read characters std::vector::reverse_iterator it = evt.rbegin(); while (it != evt.rend() ) _is.putback( *it++ ); @@ -254,14 +270,14 @@ _OMReader_::can_u_read(std::istream& _is) const default: // ? return false; } - + // 4th characters encodes the version return supports( hdr->version_ ); } //----------------------------------------------------------------------------- -bool +bool _OMReader_::supports( const OMFormat::uint8 /* version */ ) const { return true; @@ -270,16 +286,16 @@ _OMReader_::supports( const OMFormat::uint8 /* version */ ) const //----------------------------------------------------------------------------- -bool -_OMReader_::read_binary_vertex_chunk( std::istream &_is, - BaseImporter &_bi, +bool +_OMReader_::read_binary_vertex_chunk( std::istream &_is, + BaseImporter &_bi, Options &_opt, bool _swap) const { using OMFormat::Chunk; assert( chunk_header_.entity_ == Chunk::Entity_Vertex ); - + OpenMesh::Vec3f v3f; OpenMesh::Vec2f v2f; OpenMesh::Vec3uc v3uc; // rgb @@ -290,7 +306,7 @@ _OMReader_::read_binary_vertex_chunk( std::istream &_is, switch (chunk_header_.type_) { case Chunk::Type_Pos: - assert( OMFormat::dimensions(chunk_header_) + assert( OMFormat::dimensions(chunk_header_) == size_t(OpenMesh::Vec3f::dim()) ); for (; vidx < header_.n_vertices_ && !_is.eof(); ++vidx) @@ -300,9 +316,9 @@ _OMReader_::read_binary_vertex_chunk( std::istream &_is, } break; - + case Chunk::Type_Normal: - assert( OMFormat::dimensions(chunk_header_) + assert( OMFormat::dimensions(chunk_header_) == size_t(OpenMesh::Vec3f::dim()) ); _opt += Options::VertexNormal; @@ -313,9 +329,9 @@ _OMReader_::read_binary_vertex_chunk( std::istream &_is, } break; - + case Chunk::Type_Texcoord: - assert( OMFormat::dimensions(chunk_header_) + assert( OMFormat::dimensions(chunk_header_) == size_t(OpenMesh::Vec2f::dim()) ); _opt += Options::VertexTexCoord; @@ -324,14 +340,14 @@ _OMReader_::read_binary_vertex_chunk( std::istream &_is, bytes_ += vector_restore( _is, v2f, _swap ); _bi.set_texcoord(VertexHandle(vidx), v2f); } - break; + break; case Chunk::Type_Color: assert( OMFormat::dimensions(chunk_header_) == 3 ); _opt += Options::VertexColor; - + for (; vidx < header_.n_vertices_ && !_is.eof(); ++vidx) { bytes_ += vector_restore( _is, v3uc, _swap ); @@ -340,8 +356,8 @@ _OMReader_::read_binary_vertex_chunk( std::istream &_is, break; case Chunk::Type_Custom: - - bytes_ += + + bytes_ += restore_binary_custom_data( _is, _bi.kernel()->_get_vprop( property_name_ ), header_.n_vertices_, @@ -354,7 +370,7 @@ _OMReader_::read_binary_vertex_chunk( std::istream &_is, default: // skip unknown chunks { omerr() << "Unknown chunk type ignored!\n"; - size_t size_of = header_.n_vertices_ + size_t size_of = header_.n_vertices_ * OMFormat::vector_size(chunk_header_); _is.ignore( size_of ); bytes_ += size_of; @@ -369,8 +385,8 @@ _OMReader_::read_binary_vertex_chunk( std::istream &_is, //----------------------------------------------------------------------------- bool -_OMReader_::read_binary_face_chunk( std::istream &_is, - BaseImporter &_bi, +_OMReader_::read_binary_face_chunk( std::istream &_is, + BaseImporter &_bi, Options &_opt, bool _swap ) const { @@ -395,7 +411,7 @@ _OMReader_::read_binary_face_chunk( std::istream &_is, case 'T': nV = 3; break; case 'Q': nV = 4; break; } - + for (; fidx < header_.n_faces_; ++fidx) { if ( header_.mesh_ == 'P' ) @@ -403,8 +419,8 @@ _OMReader_::read_binary_face_chunk( std::istream &_is, vhandles.clear(); for (size_t j=0; j_get_fprop( property_name_ ), header_.n_faces_, @@ -454,13 +470,13 @@ _OMReader_::read_binary_face_chunk( std::istream &_is, fidx = header_.n_faces_; break; - + default: // skip unknown chunks { omerr() << "Unknown chunk type ignore!\n"; size_t size_of = OMFormat::chunk_data_size(header_, chunk_header_); _is.ignore( size_of ); - bytes_ += size_of; + bytes_ += size_of; } } return fidx == header_.n_faces_; @@ -470,8 +486,8 @@ _OMReader_::read_binary_face_chunk( std::istream &_is, //----------------------------------------------------------------------------- bool -_OMReader_::read_binary_edge_chunk( std::istream &_is, - BaseImporter &_bi, +_OMReader_::read_binary_edge_chunk( std::istream &_is, + BaseImporter &_bi, Options &/*_opt */, bool _swap ) const { @@ -483,9 +499,9 @@ _OMReader_::read_binary_edge_chunk( std::istream &_is, switch( chunk_header_.type_ ) { - case Chunk::Type_Custom: + case Chunk::Type_Custom: - bytes_ += + bytes_ += restore_binary_custom_data( _is, _bi.kernel()->_get_eprop( property_name_ ), header_.n_edges_, @@ -507,8 +523,8 @@ _OMReader_::read_binary_edge_chunk( std::istream &_is, //----------------------------------------------------------------------------- bool -_OMReader_::read_binary_halfedge_chunk( std::istream &_is, - BaseImporter &_bi, +_OMReader_::read_binary_halfedge_chunk( std::istream &_is, + BaseImporter &_bi, Options &/* _opt */, bool _swap ) const { @@ -522,11 +538,11 @@ _OMReader_::read_binary_halfedge_chunk( std::istream &_is, { case Chunk::Type_Custom: - bytes_ += + bytes_ += restore_binary_custom_data( _is, _bi.kernel()->_get_hprop( property_name_ ), 2*header_.n_edges_, - _swap ); + _swap ); break; default: @@ -544,8 +560,8 @@ _OMReader_::read_binary_halfedge_chunk( std::istream &_is, //----------------------------------------------------------------------------- bool -_OMReader_::read_binary_mesh_chunk( std::istream &_is, - BaseImporter &_bi, +_OMReader_::read_binary_mesh_chunk( std::istream &_is, + BaseImporter &_bi, Options & /* _opt */, bool _swap ) const { @@ -558,13 +574,13 @@ _OMReader_::read_binary_mesh_chunk( std::istream &_is, switch( chunk_header_.type_ ) { case Chunk::Type_Custom: - - bytes_ += + + bytes_ += restore_binary_custom_data( _is, _bi.kernel()->_get_mprop( property_name_ ), 1, _swap ); - + break; default: @@ -581,10 +597,10 @@ _OMReader_::read_binary_mesh_chunk( std::istream &_is, //----------------------------------------------------------------------------- -size_t +size_t _OMReader_::restore_binary_custom_data( std::istream& _is, BaseProperty* _bp, size_t _n_elem, bool _swap) const -{ +{ assert( !_bp || (_bp->name() == property_name_) ); using OMFormat::Chunk; @@ -605,7 +621,7 @@ _OMReader_::restore_binary_custom_data( std::istream& _is, BaseProperty* _bp, if ( ((n_bytes == BaseProperty::UnknownSize) || (n_bytes == block_size)) && (_bp->element_size() == BaseProperty::UnknownSize || - (_n_elem * _bp->element_size() == block_size) ) ) + (_n_elem * _bp->element_size() == block_size) ) ) { #if defined(OM_DEBUG) bytes += (b=_bp->restore( _is, _swap )); @@ -621,7 +637,7 @@ _OMReader_::restore_binary_custom_data( std::istream& _is, BaseProperty* _bp, } else { - omerr() << "Warning! Property " << _bp->name() << " not loaded: " + omerr() << "Warning! Property " << _bp->name() << " not loaded: " << "Mismatching data sizes!n"; } } diff --git a/src/OpenMesh/Core/IO/reader/OMReader.hh b/src/OpenMesh/Core/IO/reader/OMReader.hh index ca329d55..1154ace8 100644 --- a/src/OpenMesh/Core/IO/reader/OMReader.hh +++ b/src/OpenMesh/Core/IO/reader/OMReader.hh @@ -95,6 +95,11 @@ public: BaseImporter& _bi, Options& _opt ); +//! Stream Reader for std::istream input in binary format + bool read(std::istream& _is, + BaseImporter& _bi, + Options& _opt ); + virtual bool can_u_read(const std::string& _filename) const; virtual bool can_u_read(std::istream& _is) const; @@ -103,7 +108,6 @@ private: bool supports( const OMFormat::uint8 version ) const; - bool read(std::istream& _is, BaseImporter& _bi, Options& _opt ) const; bool read_ascii(std::istream& _is, BaseImporter& _bi, Options& _opt) const; bool read_binary(std::istream& _is, BaseImporter& _bi, Options& _opt) const; diff --git a/src/OpenMesh/Core/IO/reader/PLYReader.cc b/src/OpenMesh/Core/IO/reader/PLYReader.cc index 9050d410..74341952 100644 --- a/src/OpenMesh/Core/IO/reader/PLYReader.cc +++ b/src/OpenMesh/Core/IO/reader/PLYReader.cc @@ -87,10 +87,11 @@ _PLYReader_::_PLYReader_() { bool _PLYReader_::read(const std::string& _filename, BaseImporter& _bi, Options& _opt) { + std::fstream in(_filename.c_str(), (options_.is_binary() ? std::ios_base::binary | std::ios_base::in : std::ios_base::in)); - if (!in) { + if (!in.is_open() || !in.good()) { omerr() << "[PLYReader] : cannot not open file " << _filename << std::endl; return false; } @@ -104,7 +105,13 @@ bool _PLYReader_::read(const std::string& _filename, BaseImporter& _bi, Options& //----------------------------------------------------------------------------- -bool _PLYReader_::read(std::fstream& _in, BaseImporter& _bi, Options& _opt) const { +bool _PLYReader_::read(std::istream& _in, BaseImporter& _bi, Options& _opt) { + + if (!_in.good()) { + omerr() << "[PLYReader] : cannot not use stream" << std::endl; + return false; + } + // filter relevant options for reading bool swap = _opt.check(Options::Swap); @@ -137,9 +144,11 @@ bool _PLYReader_::read(std::fstream& _in, BaseImporter& _bi, Options& _opt) cons } + + //----------------------------------------------------------------------------- -bool _PLYReader_::read_ascii(std::fstream& _in, BaseImporter& _bi) const { +bool _PLYReader_::read_ascii(std::istream& _in, BaseImporter& _bi) const { omlog() << "[PLYReader] : read ascii file\n"; @@ -269,7 +278,7 @@ bool _PLYReader_::read_ascii(std::fstream& _in, BaseImporter& _bi) const { //----------------------------------------------------------------------------- -bool _PLYReader_::read_binary(std::fstream& _in, BaseImporter& _bi, bool /*_swap*/) const { +bool _PLYReader_::read_binary(std::istream& _in, BaseImporter& _bi, bool /*_swap*/) const { omlog() << "[PLYReader] : read binary file format\n"; @@ -394,7 +403,7 @@ bool _PLYReader_::read_binary(std::fstream& _in, BaseImporter& _bi, bool /*_swap //----------------------------------------------------------------------------- -void _PLYReader_::readValue(ValueType _type, std::fstream& _in, float& _value) const { +void _PLYReader_::readValue(ValueType _type, std::istream& _in, float& _value) const { switch (_type) { case ValueTypeFLOAT32: @@ -410,7 +419,7 @@ void _PLYReader_::readValue(ValueType _type, std::fstream& _in, float& _value) c } } -void _PLYReader_::readValue(ValueType _type, std::fstream& _in, unsigned int& _value) const { +void _PLYReader_::readValue(ValueType _type, std::istream& _in, unsigned int& _value) const { int32_t tmp_int32_t; uint8_t tmp_uchar; @@ -432,7 +441,7 @@ void _PLYReader_::readValue(ValueType _type, std::fstream& _in, unsigned int& _v } } -void _PLYReader_::readValue(ValueType _type, std::fstream& _in, int& _value) const { +void _PLYReader_::readValue(ValueType _type, std::istream& _in, int& _value) const { int32_t tmp_int32_t; uint8_t tmp_uchar; diff --git a/src/OpenMesh/Core/IO/reader/PLYReader.hh b/src/OpenMesh/Core/IO/reader/PLYReader.hh index feef96ef..077d6317 100644 --- a/src/OpenMesh/Core/IO/reader/PLYReader.hh +++ b/src/OpenMesh/Core/IO/reader/PLYReader.hh @@ -103,6 +103,10 @@ public: BaseImporter& _bi, Options& _opt); + bool read(std::istream& _is, + BaseImporter& _bi, + Options& _opt); + bool can_u_read(const std::string& _filename) const; enum ValueType { @@ -116,15 +120,14 @@ private: bool can_u_read(std::istream& _is) const; - bool read(std::fstream& _in, BaseImporter& _bi, Options& _opt ) const; - bool read_ascii(std::fstream& _in, BaseImporter& _bi) const; - bool read_binary(std::fstream& _in, BaseImporter& _bi, bool swap) const; + bool read_ascii(std::istream& _in, BaseImporter& _bi) const; + bool read_binary(std::istream& _in, BaseImporter& _bi, bool swap) const; float readToFloatValue(ValueType _type , std::fstream& _in) const; - void readValue(ValueType _type , std::fstream& _in, float& _value) const; - void readValue(ValueType _type , std::fstream& _in, unsigned int& _value) const; - void readValue(ValueType _type , std::fstream& _in, int& _value) const; + void readValue(ValueType _type , std::istream& _in, float& _value) const; + void readValue(ValueType _type , std::istream& _in, unsigned int& _value) const; + void readValue(ValueType _type , std::istream& _in, int& _value) const; //available options for reading mutable Options options_; diff --git a/src/OpenMesh/Core/IO/reader/STLReader.cc b/src/OpenMesh/Core/IO/reader/STLReader.cc index e98d49c9..ad1dd2a2 100644 --- a/src/OpenMesh/Core/IO/reader/STLReader.cc +++ b/src/OpenMesh/Core/IO/reader/STLReader.cc @@ -4,10 +4,10 @@ * Copyright (C) 2001-2009 by Computer Graphics Group, RWTH Aachen * * www.openmesh.org * * * - *---------------------------------------------------------------------------* + *---------------------------------------------------------------------------* * This file is part of OpenMesh. * * * - * OpenMesh is free software: you can redistribute it and/or modify * + * OpenMesh is free software: you can redistribute it and/or modify * * it under the terms of the GNU Lesser General Public License as * * published by the Free Software Foundation, either version 3 of * * the License, or (at your option) any later version with the * @@ -30,10 +30,10 @@ * License along with OpenMesh. If not, * * see . * * * -\*===========================================================================*/ +\*===========================================================================*/ /*===========================================================================*\ - * * + * * * $Revision$ * * $Date$ * * * @@ -76,17 +76,17 @@ _STLReader_& STLReader() { return __STLReaderInstance; } _STLReader_:: -_STLReader_() +_STLReader_() : eps_(FLT_MIN) { - IOManager().register_module(this); + IOManager().register_module(this); } //----------------------------------------------------------------------------- -bool +bool _STLReader_:: read(const std::string& _filename, BaseImporter& _bi, Options& _opt) { @@ -118,15 +118,15 @@ read(const std::string& _filename, BaseImporter& _bi, Options& _opt) _opt -= Options::Binary; break; } - + case STLB: { result = read_stlb(_filename, _bi); _opt += Options::Binary; break; } - - default: + + default: { result = false; break; @@ -137,6 +137,17 @@ read(const std::string& _filename, BaseImporter& _bi, Options& _opt) return result; } +bool +_STLReader_::read(std::istream& _is, + BaseImporter& _bi, + Options& _opt) +{ + + omerr() << "[OMReader] : STL Streams are not supported " << std::endl; + + return false; +} + //----------------------------------------------------------------------------- @@ -151,9 +162,9 @@ public: bool operator()( const Vec3f& _v0, const Vec3f& _v1 ) const { - if (fabs(_v0[0] - _v1[0]) <= eps_) + if (fabs(_v0[0] - _v1[0]) <= eps_) { - if (fabs(_v0[1] - _v1[1]) <= eps_) + if (fabs(_v0[1] - _v1[1]) <= eps_) { return (_v0[2] < _v1[2] - eps_); } @@ -172,17 +183,16 @@ private: //----------------------------------------------------------------------------- -bool +bool _STLReader_:: read_stla(const std::string& _filename, BaseImporter& _bi) const { omlog() << "[STLReader] : read ascii file\n"; - FILE* in = fopen(_filename.c_str(), "r"); if (!in) { - omerr() << "[STLReader] : cannot not open file " + omerr() << "[STLReader] : cannot not open file " << _filename << std::endl; return false; @@ -200,15 +210,15 @@ read_stla(const std::string& _filename, BaseImporter& _bi) const std::map vMap(comp); std::map::iterator vMapIt; - + while (in && !feof(in) && fgets(line, 100, in)) { for (p=line; isspace(*p) && *p!='\0'; ++p) {}; // skip white-space if ((strncmp(p, "outer", 5) == 0) || (strncmp(p, "OUTER", 5) == 0)) - { + { vhandles.clear(); - + for (i=0; i<3; ++i) { fgets(line, 100, in); @@ -223,7 +233,7 @@ read_stla(const std::string& _filename, BaseImporter& _bi) const vhandles.push_back(VertexHandle(cur_idx)); vMap[v] = VertexHandle(cur_idx++); } - else + else // Yes : get index from map vhandles.push_back(vMapIt->second); } @@ -248,17 +258,16 @@ read_stla(const std::string& _filename, BaseImporter& _bi) const //----------------------------------------------------------------------------- -bool +bool _STLReader_:: read_stlb(const std::string& _filename, BaseImporter& _bi) const { omlog() << "[STLReader] : read binary file\n"; - FILE* in = fopen(_filename.c_str(), "rb"); if (!in) { - omerr() << "[STLReader] : cannot not open file " + omerr() << "[STLReader] : cannot not open file " << _filename << std::endl; return false; @@ -275,7 +284,7 @@ read_stlb(const std::string& _filename, BaseImporter& _bi) const std::map vMap; std::map::iterator vMapIt; - + // check size of types if ((sizeof(float) != 4) || (sizeof(int) != 4)) { omerr() << "[STLReader] : wrong type size\n"; @@ -286,11 +295,11 @@ read_stlb(const std::string& _filename, BaseImporter& _bi) const union { unsigned int i; unsigned char c[4]; } endian_test; endian_test.i = 1; swapFlag = (endian_test.c[3] == 1); - + // read number of triangles fread(dummy, 1, 80, in); nT = read_int(in, swapFlag); - + // read triangles while (nT) { @@ -314,18 +323,18 @@ read_stlb(const std::string& _filename, BaseImporter& _bi) const vhandles.push_back(VertexHandle(cur_idx)); vMap[v] = VertexHandle(cur_idx++); } - else + else // Yes : get index from map vhandles.push_back(vMapIt->second); } - + // Add face only if it is not degenerated if ((vhandles[0] != vhandles[1]) && (vhandles[0] != vhandles[2]) && (vhandles[1] != vhandles[2])) _bi.add_face(vhandles); - + fread(dummy, 1, 2, in); --nT; } @@ -355,7 +364,7 @@ check_stl_type(const std::string& _filename) const endian_test.i = 1; bool swapFlag = (endian_test.c[3] == 1); - + // read number of triangles char dummy[100]; fread(dummy, 1, 80, in); diff --git a/src/OpenMesh/Core/IO/reader/STLReader.hh b/src/OpenMesh/Core/IO/reader/STLReader.hh index 2cc5edde..c6c05432 100644 --- a/src/OpenMesh/Core/IO/reader/STLReader.hh +++ b/src/OpenMesh/Core/IO/reader/STLReader.hh @@ -4,10 +4,10 @@ * Copyright (C) 2001-2009 by Computer Graphics Group, RWTH Aachen * * www.openmesh.org * * * - *---------------------------------------------------------------------------* + *---------------------------------------------------------------------------* * This file is part of OpenMesh. * * * - * OpenMesh is free software: you can redistribute it and/or modify * + * OpenMesh is free software: you can redistribute it and/or modify * * it under the terms of the GNU Lesser General Public License as * * published by the Free Software Foundation, either version 3 of * * the License, or (at your option) any later version with the * @@ -30,10 +30,10 @@ * License along with OpenMesh. If not, * * see . * * * -\*===========================================================================*/ +\*===========================================================================*/ /*===========================================================================*\ - * * + * * * $Revision$ * * $Date$ * * * @@ -79,8 +79,8 @@ class BaseImporter; //== IMPLEMENTATION =========================================================== -/** - Implementation of the STL format reader. This class is singleton'ed by +/** + Implementation of the STL format reader. This class is singleton'ed by SingletonT to STLReader. */ class _STLReader_ : public BaseReader @@ -94,14 +94,17 @@ public: virtual ~_STLReader_() {}; - std::string get_description() const + std::string get_description() const { return "Stereolithography Interface Format"; } std::string get_extensions() const { return "stl stla stlb"; } - bool read(const std::string& _filename, + bool read(const std::string& _filename, BaseImporter& _bi, Options& _opt); + bool read(std::istream& _in, + BaseImporter& _bi, + Options& _opt); /** Set the threshold to be used for considering two point to be equal. Can be used to merge small gaps */ @@ -111,7 +114,7 @@ public: float epsilon() const { return eps_; } - + private: enum STL_Type { STLA, STLB, NONE }; diff --git a/src/OpenMesh/Core/IO/writer/BaseWriter.hh b/src/OpenMesh/Core/IO/writer/BaseWriter.hh index 6c17e9c4..c7214c78 100644 --- a/src/OpenMesh/Core/IO/writer/BaseWriter.hh +++ b/src/OpenMesh/Core/IO/writer/BaseWriter.hh @@ -4,10 +4,10 @@ * Copyright (C) 2001-2009 by Computer Graphics Group, RWTH Aachen * * www.openmesh.org * * * - *---------------------------------------------------------------------------* + *---------------------------------------------------------------------------* * This file is part of OpenMesh. * * * - * OpenMesh is free software: you can redistribute it and/or modify * + * OpenMesh is free software: you can redistribute it and/or modify * * it under the terms of the GNU Lesser General Public License as * * published by the Free Software Foundation, either version 3 of * * the License, or (at your option) any later version with the * @@ -30,10 +30,10 @@ * License along with OpenMesh. If not, * * see . * * * -\*===========================================================================*/ +\*===========================================================================*/ /*===========================================================================*\ - * * + * * * $Revision$ * * $Date$ * * * @@ -86,10 +86,10 @@ public: /// Destructor virtual ~BaseWriter() {}; - + /// Return short description of the supported file format. virtual std::string get_description() const = 0; - + /// Return file format's extension. virtual std::string get_extensions() const = 0; @@ -97,9 +97,14 @@ public: virtual bool can_u_write(const std::string& _filename) const; /// Write to file _filename. Data source specified by BaseExporter _be. - virtual bool write(const std::string& _filename, + virtual bool write(const std::string& _filename, + BaseExporter& _be, + Options _opt) const = 0; + + /// Write to std::ostream _os. Data source specified by BaseExporter _be. + virtual bool write(std::ostream& _os, BaseExporter& _be, - Options _opt) const = 0; + Options _opt) const = 0; /// Returns expected size of file if binary format is supported else 0. virtual size_t binary_size(BaseExporter&, Options) const { return 0; } diff --git a/src/OpenMesh/Core/IO/writer/OBJWriter.cc b/src/OpenMesh/Core/IO/writer/OBJWriter.cc index 96a23037..912c2102 100644 --- a/src/OpenMesh/Core/IO/writer/OBJWriter.cc +++ b/src/OpenMesh/Core/IO/writer/OBJWriter.cc @@ -149,7 +149,7 @@ int _OBJWriter_::getMaterial(OpenMesh::Vec4f _color) const bool _OBJWriter_:: -writeMaterial(std::fstream& _out, BaseExporter& _be, Options _opt) const +writeMaterial(std::ostream& _out, BaseExporter& _be, Options _opt) const { OpenMesh::Vec3f c; OpenMesh::Vec4f cA; @@ -196,7 +196,7 @@ writeMaterial(std::fstream& _out, BaseExporter& _be, Options _opt) const bool _OBJWriter_:: -write(std::fstream& _out, BaseExporter& _be, Options _opt) const +write(std::ostream& _out, BaseExporter& _be, Options _opt) const { unsigned int i, j, nV, nF, idx; Vec3f v, n; diff --git a/src/OpenMesh/Core/IO/writer/OBJWriter.hh b/src/OpenMesh/Core/IO/writer/OBJWriter.hh index 53b9908f..b291512e 100644 --- a/src/OpenMesh/Core/IO/writer/OBJWriter.hh +++ b/src/OpenMesh/Core/IO/writer/OBJWriter.hh @@ -90,6 +90,8 @@ public: std::string get_extensions() const { return "obj"; } bool write(const std::string&, BaseExporter&, Options) const; + + bool write(std::ostream&, BaseExporter&, Options) const; size_t binary_size(BaseExporter&, Options) const { return 0; } @@ -105,9 +107,9 @@ private: int getMaterial(OpenMesh::Vec4f _color) const; - bool writeMaterial(std::fstream& _out, BaseExporter&, Options) const; + bool writeMaterial(std::ostream& _out, BaseExporter&, Options) const; + - bool write(std::fstream& _out, BaseExporter&, Options) const; }; diff --git a/src/OpenMesh/Core/IO/writer/OFFWriter.cc b/src/OpenMesh/Core/IO/writer/OFFWriter.cc index 040f4756..c04caab6 100644 --- a/src/OpenMesh/Core/IO/writer/OFFWriter.cc +++ b/src/OpenMesh/Core/IO/writer/OFFWriter.cc @@ -121,13 +121,56 @@ write(const std::string& _filename, BaseExporter& _be, Options _opt) const return result; } +//----------------------------------------------------------------------------- + + +bool +_OFFWriter_:: +write(std::ostream& _os, BaseExporter& _be, Options _opt) const +{ + // check exporter features + if ( !check( _be, _opt ) ) + return false; + + + // check writer features + if ( _opt.check(Options::FaceNormal) ) // not supported by format + return false; + + + if (!_os.good()) + { + omerr() << "[OFFWriter] : cannot write to stream " + << std::endl; + return false; + } + + + // write header line + if (_opt.check(Options::VertexTexCoord)) _os << "ST"; + if (_opt.check(Options::VertexColor) || _opt.check(Options::FaceColor)) _os << "C"; + if (_opt.check(Options::VertexNormal)) _os << "N"; + _os << "OFF"; + if (_opt.check(Options::Binary)) _os << " BINARY"; + _os << "\n"; + + + // write to file + bool result = (_opt.check(Options::Binary) ? + write_binary(_os, _be, _opt) : + write_ascii(_os, _be, _opt)); + + + // return result + return result; +} //----------------------------------------------------------------------------- bool _OFFWriter_:: -write_ascii(std::fstream& _out, BaseExporter& _be, Options _opt) const +write_ascii(std::ostream& _out, BaseExporter& _be, Options _opt) const { omlog() << "[OFFWriter] : write ascii file\n"; @@ -244,19 +287,19 @@ write_ascii(std::fstream& _out, BaseExporter& _be, Options _opt) const //----------------------------------------------------------------------------- -void _OFFWriter_::writeValue(std::fstream& _out, int value) const { +void _OFFWriter_::writeValue(std::ostream& _out, int value) const { uint32_t tmp = value; store(_out, tmp, false); } -void _OFFWriter_::writeValue(std::fstream& _out, unsigned int value) const { +void _OFFWriter_::writeValue(std::ostream& _out, unsigned int value) const { uint32_t tmp = value; store(_out, tmp, false); } -void _OFFWriter_::writeValue(std::fstream& _out, float value) const { +void _OFFWriter_::writeValue(std::ostream& _out, float value) const { float32_t tmp = value; store(_out, tmp, false); @@ -264,7 +307,7 @@ void _OFFWriter_::writeValue(std::fstream& _out, float value) const { bool _OFFWriter_:: -write_binary(std::fstream& _out, BaseExporter& _be, Options _opt) const +write_binary(std::ostream& _out, BaseExporter& _be, Options _opt) const { omlog() << "[OFFWriter] : write ascii file\n"; diff --git a/src/OpenMesh/Core/IO/writer/OFFWriter.hh b/src/OpenMesh/Core/IO/writer/OFFWriter.hh index 5c816f3d..3fedd7b7 100644 --- a/src/OpenMesh/Core/IO/writer/OFFWriter.hh +++ b/src/OpenMesh/Core/IO/writer/OFFWriter.hh @@ -101,17 +101,19 @@ public: std::string get_extensions() const { return "off"; } bool write(const std::string&, BaseExporter&, Options) const; + + bool write(std::ostream&, BaseExporter&, Options) const; size_t binary_size(BaseExporter& _be, Options _opt) const; protected: - void writeValue(std::fstream& _out, int value) const; - void writeValue(std::fstream& _out, unsigned int value) const; - void writeValue(std::fstream& _out, float value) const; + void writeValue(std::ostream& _out, int value) const; + void writeValue(std::ostream& _out, unsigned int value) const; + void writeValue(std::ostream& _out, float value) const; - bool write_ascii(std::fstream& _in, BaseExporter&, Options) const; - bool write_binary(std::fstream& _in, BaseExporter&, Options) const; + bool write_ascii(std::ostream& _in, BaseExporter&, Options) const; + bool write_binary(std::ostream& _in, BaseExporter&, Options) const; }; diff --git a/src/OpenMesh/Core/IO/writer/OMWriter.hh b/src/OpenMesh/Core/IO/writer/OMWriter.hh index 9525d30a..7da3ebbe 100644 --- a/src/OpenMesh/Core/IO/writer/OMWriter.hh +++ b/src/OpenMesh/Core/IO/writer/OMWriter.hh @@ -103,6 +103,7 @@ public: { return "om"; } bool write(std::ostream&, BaseExporter&, Options) const; + size_t binary_size(BaseExporter& _be, Options _opt) const; @@ -116,6 +117,7 @@ protected: bool write(const std::string&, BaseExporter&, Options) const; bool write_binary(std::ostream&, BaseExporter&, Options) const; + size_t store_binary_custom_chunk( std::ostream&, const BaseProperty&, OMFormat::Chunk::Entity, bool) const; diff --git a/src/OpenMesh/Core/IO/writer/PLYWriter.cc b/src/OpenMesh/Core/IO/writer/PLYWriter.cc index 618c1223..37a42ed0 100644 --- a/src/OpenMesh/Core/IO/writer/PLYWriter.cc +++ b/src/OpenMesh/Core/IO/writer/PLYWriter.cc @@ -112,13 +112,47 @@ write(const std::string& _filename, BaseExporter& _be, Options _opt) const return result; } +//----------------------------------------------------------------------------- + + +bool +_PLYWriter_:: +write(std::ostream& _os, BaseExporter& _be, Options _opt) const +{ + // check exporter features + if ( !check( _be, _opt ) ) + return false; + + + // check writer features + if ( _opt.check(Options::FaceNormal) || _opt.check(Options::FaceColor) ) // not supported yet + return false; + + options_ = _opt; + + + if (!_os.good()) + { + omerr() << "[PLYWriter] : cannot write to stream " + << std::endl; + return false; + } + + // write to file + bool result = (_opt.check(Options::Binary) ? + write_binary(_os, _be, _opt) : + write_ascii(_os, _be, _opt)); + + return result; +} + //----------------------------------------------------------------------------- bool _PLYWriter_:: -write_ascii(std::fstream& _out, BaseExporter& _be, Options _opt) const +write_ascii(std::ostream& _out, BaseExporter& _be, Options _opt) const { omlog() << "[PLYWriter] : write ascii file\n"; @@ -248,7 +282,7 @@ write_ascii(std::fstream& _out, BaseExporter& _be, Options _opt) const //----------------------------------------------------------------------------- -void _PLYWriter_::writeValue(ValueType _type, std::fstream& _out, int value) const { +void _PLYWriter_::writeValue(ValueType _type, std::ostream& _out, int value) const { uint32_t tmp32; uint8_t tmp8; @@ -270,7 +304,7 @@ default : } } -void _PLYWriter_::writeValue(ValueType _type, std::fstream& _out, unsigned int value) const { +void _PLYWriter_::writeValue(ValueType _type, std::ostream& _out, unsigned int value) const { uint32_t tmp32; uint8_t tmp8; @@ -292,7 +326,7 @@ default : } } -void _PLYWriter_::writeValue(ValueType _type, std::fstream& _out, float value) const { +void _PLYWriter_::writeValue(ValueType _type, std::ostream& _out, float value) const { float32_t tmp; @@ -310,7 +344,7 @@ void _PLYWriter_::writeValue(ValueType _type, std::fstream& _out, float value) c bool _PLYWriter_:: -write_binary(std::fstream& _out, BaseExporter& _be, Options _opt) const +write_binary(std::ostream& _out, BaseExporter& _be, Options _opt) const { omlog() << "[PLYWriter] : write binary file\n"; diff --git a/src/OpenMesh/Core/IO/writer/PLYWriter.hh b/src/OpenMesh/Core/IO/writer/PLYWriter.hh index 0bc553f4..2ed2bfc6 100644 --- a/src/OpenMesh/Core/IO/writer/PLYWriter.hh +++ b/src/OpenMesh/Core/IO/writer/PLYWriter.hh @@ -97,6 +97,8 @@ public: bool write(const std::string&, BaseExporter&, Options) const; + bool write(std::ostream&, BaseExporter&, Options) const; + size_t binary_size(BaseExporter& _be, Options _opt) const; enum ValueType { @@ -110,12 +112,12 @@ private: mutable Options options_; protected: - void writeValue(ValueType _type, std::fstream& _out, int value) const; - void writeValue(ValueType _type, std::fstream& _out, unsigned int value) const; - void writeValue(ValueType _type, std::fstream& _out, float value) const; + void writeValue(ValueType _type, std::ostream& _out, int value) const; + void writeValue(ValueType _type, std::ostream& _out, unsigned int value) const; + void writeValue(ValueType _type, std::ostream& _out, float value) const; - bool write_ascii(std::fstream& _in, BaseExporter&, Options) const; - bool write_binary(std::fstream& _in, BaseExporter&, Options) const; + bool write_ascii(std::ostream& _in, BaseExporter&, Options) const; + bool write_binary(std::ostream& _in, BaseExporter&, Options) const; }; diff --git a/src/OpenMesh/Core/IO/writer/STLWriter.cc b/src/OpenMesh/Core/IO/writer/STLWriter.cc index eaf7ee95..b2be7ab9 100644 --- a/src/OpenMesh/Core/IO/writer/STLWriter.cc +++ b/src/OpenMesh/Core/IO/writer/STLWriter.cc @@ -113,6 +113,17 @@ write(const std::string& _filename, BaseExporter& _be, Options _opt) const return false; } +//----------------------------------------------------------------------------- + + +bool +_STLWriter_:: +write(std::ostream& _os, BaseExporter& _be, Options _opt) const +{ + omerr() << "[STLWriter] : STL Streams are not supported " << std::endl; +} + + //----------------------------------------------------------------------------- diff --git a/src/OpenMesh/Core/IO/writer/STLWriter.hh b/src/OpenMesh/Core/IO/writer/STLWriter.hh index a011f7e1..966179c9 100644 --- a/src/OpenMesh/Core/IO/writer/STLWriter.hh +++ b/src/OpenMesh/Core/IO/writer/STLWriter.hh @@ -95,6 +95,8 @@ public: bool write(const std::string&, BaseExporter&, Options) const; + bool write(std::ostream&, BaseExporter&, Options) const; + size_t binary_size(BaseExporter&, Options) const; private: -- GitLab