/*===========================================================================*\
* *
* OpenMesh *
* Copyright (C) 2001-2012 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 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" << std::endl;
if (_opt.is_binary()) {
_out << "format ";
if ( options_.check(Options::MSB) )
_out << "binary_big_endian ";
else
_out << "binary_little_endian ";
_out << "1.0" << std::endl;
} else
_out << "format ascii 1.0" << std::endl;
_out << "element vertex " << _be.n_vertices() << std::endl;
_out << "property float x" << std::endl;
_out << "property float y" << std::endl;
_out << "property float z" << std::endl;
if ( _opt.vertex_has_normal() ){
_out << "property float nx" << std::endl;
_out << "property float ny" << std::endl;
_out << "property float nz" << std::endl;
}
if ( _opt.vertex_has_texcoord() ){
_out << "property float u" << std::endl;
_out << "property float v" << std::endl;
}
if ( _opt.vertex_has_color() ){
if ( _opt.color_is_float() ) {
_out << "property float red" << std::endl;
_out << "property float green" << std::endl;
_out << "property float blue" << std::endl;
if ( _opt.color_has_alpha() )
_out << "property float alpha" << std::endl;
} else {
_out << "property uchar red" << std::endl;
_out << "property uchar green" << std::endl;
_out << "property uchar blue" << std::endl;
if ( _opt.color_has_alpha() )
_out << "property uchar alpha" << std::endl;
}
}
_out << "element face " << _be.n_faces() << std::endl;
_out << "property list uchar int vertex_indices" << std::endl;
_out << "end_header" << std::endl;
}
//-----------------------------------------------------------------------------
bool
_PLYWriter_::
write_ascii(std::ostream& _out, BaseExporter& _be, Options _opt) const
{
omlog() << "[PLYWriter] : write ascii file\n";
unsigned int i, j, 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=_be.n_vertices(); i vhandles;
write_header(_out, _be, _opt);
// vertex data (point, normals, texcoords)
for (i=0, nV=_be.n_vertices(); i vhandles;
for (i=0, nF=_be.n_faces(); i