/*===========================================================================*\ * * * OpenMesh * * Copyright (C) 2001-2014 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 * * 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 * * following exceptions: * * * * If other files instantiate templates or use macros * * or inline functions from this file, or you compile this file and * * link it with other files to produce an executable, this file does * * not by itself cause the resulting executable to be covered by the * * GNU Lesser General Public License. This exception does not however * * invalidate any other reasons why the executable file might be * * covered by the GNU Lesser General Public License. * * * * OpenMesh is distributed in the hope that it will be useful, * * but WITHOUT ANY WARRANTY; without even the implied warranty of * * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * * GNU Lesser General Public License for more details. * * * * You should have received a copy of the GNU LesserGeneral Public * * License along with OpenMesh. If not, * * see . * * * \*===========================================================================*/ /*===========================================================================*\ * * * $Revision$ * * $Date$ * * * \*===========================================================================*/ //== INCLUDES ================================================================= #include #include #include #include #include #include #include //=== NAMESPACES ============================================================== namespace OpenMesh { namespace IO { //=== INSTANCIATE ============================================================= // register the OFFLoader singleton with MeshLoader _OFFWriter_ __OFFWriterInstance; _OFFWriter_& OFFWriter() { return __OFFWriterInstance; } //=== IMPLEMENTATION ========================================================== _OFFWriter_::_OFFWriter_() { IOManager().register_module(this); } //----------------------------------------------------------------------------- bool _OFFWriter_:: write(const std::string& _filename, BaseExporter& _be, Options _opt, std::streamsize _precision) const { // check exporter features if ( !check( _be, _opt ) ) return false; // check writer features if ( _opt.check(Options::FaceNormal) ) // not supported by format return false; // open file std::fstream out(_filename.c_str(), (_opt.check(Options::Binary) ? std::ios_base::binary | std::ios_base::out : std::ios_base::out) ); if (!out) { omerr() << "[OFFWriter] : cannot open file " << _filename << std::endl; return false; } // write header line if (_opt.check(Options::VertexTexCoord)) out << "ST"; if (_opt.check(Options::VertexColor) || _opt.check(Options::FaceColor)) out << "C"; if (_opt.check(Options::VertexNormal)) out << "N"; out << "OFF"; if (_opt.check(Options::Binary)) out << " BINARY"; out << "\n"; if (!_opt.check(Options::Binary)) out.precision(_precision); // write to file bool result = (_opt.check(Options::Binary) ? write_binary(out, _be, _opt) : write_ascii(out, _be, _opt)); // return result out.close(); return result; } //----------------------------------------------------------------------------- bool _OFFWriter_:: write(std::ostream& _os, BaseExporter& _be, Options _opt, std::streamsize _precision) 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"; if (!_opt.check(Options::Binary)) _os.precision(_precision); // 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::ostream& _out, BaseExporter& _be, Options _opt) const { omlog() << "[OFFWriter] : write ascii file\n"; unsigned int i, nV, nF; Vec3f v, n; Vec2f t; OpenMesh::Vec3i c; OpenMesh::Vec4i cA; OpenMesh::Vec3f cf; OpenMesh::Vec4f cAf; VertexHandle vh; std::vector vhandles; // #vertices, #faces _out << _be.n_vertices() << " "; _out << _be.n_faces() << " "; _out << 0 << "\n"; if (_opt.color_is_float()) _out << std::fixed; // vertex data (point, normals, colors, texcoords) for (i=0, nV=int(_be.n_vertices()); i vhandles; // #vertices, #faces writeValue(_out, (uint)_be.n_vertices() ); writeValue(_out, (uint) _be.n_faces() ); writeValue(_out, 0 ); // vertex data (point, normals, texcoords) for (i=0, nV=int(_be.n_vertices()); i vhandles; for (i=0, nF=int(_be.n_faces()); i