/*===========================================================================*\ * * * OpenMesh * * Copyright (c) 2001-2015, RWTH-Aachen University * * Department for Computer Graphics and Multimedia * * All rights reserved. * * www.openmesh.org * * * *---------------------------------------------------------------------------* * This file is part of OpenMesh. * *---------------------------------------------------------------------------* * * * Redistribution and use in source and binary forms, with or without * * modification, are permitted provided that the following conditions * * are met: * * * * 1. Redistributions of source code must retain the above copyright notice, * * this list of conditions and the following disclaimer. * * * * 2. Redistributions in binary form must reproduce the above copyright * * notice, this list of conditions and the following disclaimer in the * * documentation and/or other materials provided with the distribution. * * * * 3. Neither the name of the copyright holder nor the names of its * * contributors may be used to endorse or promote products derived from * * this software without specific prior written permission. * * * * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS * * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED * * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A * * PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER * * OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, * * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, * * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR * * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF * * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING * * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS * * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. * * * /*===========================================================================*\ /*===========================================================================*\ * * * $Revision$ * * $Date$ * * * \*===========================================================================*/ //== INCLUDES ================================================================= #include #include #include #include #include #include #include //=== NAMESPACES ============================================================== namespace OpenMesh { namespace IO { //=== INSTANCIATE ============================================================= // register the PLYLoader singleton with MeshLoader _PLYWriter_ __PLYWriterInstance; _PLYWriter_& PLYWriter() { return __PLYWriterInstance; } //=== IMPLEMENTATION ========================================================== _PLYWriter_::_PLYWriter_() { IOManager().register_module(this); } //----------------------------------------------------------------------------- bool _PLYWriter_:: 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) ) { // Face normals are not supported // Uncheck these options and output message that // they are not written out even though they were requested _opt.unset(Options::FaceNormal); omerr() << "[PLYWriter] : Warning: Face normals are not supported and thus not exported! " << std::endl; } if ( _opt.check(Options::FaceColor) ) { // Face normals are not supported // Uncheck these options and output message that // they are not written out even though they were requested _opt.unset(Options::FaceColor); omerr() << "[PLYWriter] : Warning: Face colors are not supported and thus not exported! " << std::endl; } options_ = _opt; // 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() << "[PLYWriter] : cannot open file " << _filename << std::endl; return false; } 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 _PLYWriter_:: 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) || _opt.check(Options::FaceColor) ) // not supported yet return false; options_ = _opt; if (!_os.good()) { omerr() << "[PLYWriter] : cannot write to stream " << std::endl; return false; } 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; } //----------------------------------------------------------------------------- void _PLYWriter_::write_header(std::ostream& _out, BaseExporter& _be, Options& _opt) const { //writing header _out << "ply" << '\n'; if (_opt.is_binary()) { _out << "format "; if ( options_.check(Options::MSB) ) _out << "binary_big_endian "; else _out << "binary_little_endian "; _out << "1.0" << '\n'; } else _out << "format ascii 1.0" << '\n'; _out << "element vertex " << _be.n_vertices() << '\n'; _out << "property float x" << '\n'; _out << "property float y" << '\n'; _out << "property float z" << '\n'; if ( _opt.vertex_has_normal() ){ _out << "property float nx" << '\n'; _out << "property float ny" << '\n'; _out << "property float nz" << '\n'; } if ( _opt.vertex_has_texcoord() ){ _out << "property float u" << '\n'; _out << "property float v" << '\n'; } if ( _opt.vertex_has_color() ){ if ( _opt.color_is_float() ) { _out << "property float red" << '\n'; _out << "property float green" << '\n'; _out << "property float blue" << '\n'; if ( _opt.color_has_alpha() ) _out << "property float alpha" << '\n'; } else { _out << "property uchar red" << '\n'; _out << "property uchar green" << '\n'; _out << "property uchar blue" << '\n'; if ( _opt.color_has_alpha() ) _out << "property uchar alpha" << '\n'; } } _out << "element face " << _be.n_faces() << '\n'; _out << "property list uchar int vertex_indices" << '\n'; _out << "end_header" << '\n'; } //----------------------------------------------------------------------------- bool _PLYWriter_:: write_ascii(std::ostream& _out, BaseExporter& _be, Options _opt) const { unsigned int i, nV, nF; Vec3f v, n; OpenMesh::Vec3ui c; OpenMesh::Vec4ui cA; OpenMesh::Vec3f cf; OpenMesh::Vec4f cAf; OpenMesh::Vec2f t; VertexHandle vh; std::vector vhandles; write_header(_out, _be, _opt); if (_opt.color_is_float()) _out << std::fixed; // vertex data (point, normals, colors, texcoords) for (i=0, nV=int(_be.n_vertices()); i vhandles; write_header(_out, _be, _opt); // vertex data (point, normals, texcoords) for (i=0, nV=int(_be.n_vertices()); i vhandles; for (i=0, nF=int(_be.n_faces()); i