2015-04-28 11:54:17 +00:00
|
|
|
/* ========================================================================= *
|
2009-02-06 13:37:46 +00:00
|
|
|
* *
|
|
|
|
|
* OpenMesh *
|
2015-04-28 11:33:32 +00:00
|
|
|
* Copyright (c) 2001-2015, RWTH-Aachen University *
|
2015-04-28 13:07:46 +00:00
|
|
|
* Department of Computer Graphics and Multimedia *
|
2015-04-28 11:33:32 +00:00
|
|
|
* All rights reserved. *
|
|
|
|
|
* www.openmesh.org *
|
2009-02-06 13:37:46 +00:00
|
|
|
* *
|
2012-10-15 10:28:14 +00:00
|
|
|
*---------------------------------------------------------------------------*
|
2015-04-28 11:33:32 +00:00
|
|
|
* This file is part of OpenMesh. *
|
|
|
|
|
*---------------------------------------------------------------------------*
|
2009-06-04 08:46:29 +00:00
|
|
|
* *
|
2015-04-28 11:33:32 +00:00
|
|
|
* 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. *
|
2015-04-28 11:54:17 +00:00
|
|
|
* *
|
|
|
|
|
* ========================================================================= */
|
2009-06-04 08:46:29 +00:00
|
|
|
|
|
|
|
|
/*===========================================================================*\
|
2012-10-15 10:28:14 +00:00
|
|
|
* *
|
2009-06-04 08:46:29 +00:00
|
|
|
* $Revision$ *
|
|
|
|
|
* $Date$ *
|
2009-02-06 13:37:46 +00:00
|
|
|
* *
|
|
|
|
|
\*===========================================================================*/
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
//== INCLUDES =================================================================
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
//STL
|
|
|
|
|
#include <fstream>
|
2013-07-23 13:46:43 +00:00
|
|
|
#include <limits>
|
2009-02-06 13:37:46 +00:00
|
|
|
|
|
|
|
|
// OpenMesh
|
|
|
|
|
#include <OpenMesh/Core/IO/BinaryHelper.hh>
|
|
|
|
|
#include <OpenMesh/Core/IO/writer/OBJWriter.hh>
|
|
|
|
|
#include <OpenMesh/Core/IO/IOManager.hh>
|
|
|
|
|
#include <OpenMesh/Core/Utils/color_cast.hh>
|
|
|
|
|
|
|
|
|
|
//=== NAMESPACES ==============================================================
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
namespace OpenMesh {
|
|
|
|
|
namespace IO {
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
//=== INSTANCIATE =============================================================
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// register the OBJLoader singleton with MeshLoader
|
|
|
|
|
_OBJWriter_ __OBJWriterinstance;
|
|
|
|
|
_OBJWriter_& OBJWriter() { return __OBJWriterinstance; }
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
//=== IMPLEMENTATION ==========================================================
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
_OBJWriter_::_OBJWriter_() { IOManager().register_module(this); }
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
bool
|
|
|
|
|
_OBJWriter_::
|
2012-10-15 10:28:14 +00:00
|
|
|
write(const std::string& _filename, BaseExporter& _be, Options _opt, std::streamsize _precision) const
|
2009-02-06 13:37:46 +00:00
|
|
|
{
|
|
|
|
|
std::fstream out(_filename.c_str(), std::ios_base::out );
|
|
|
|
|
|
|
|
|
|
if (!out)
|
|
|
|
|
{
|
|
|
|
|
omerr() << "[OBJWriter] : cannot open file "
|
|
|
|
|
<< _filename << std::endl;
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
2012-10-15 10:28:14 +00:00
|
|
|
out.precision(_precision);
|
|
|
|
|
|
2009-02-06 13:37:46 +00:00
|
|
|
{
|
|
|
|
|
#if defined(WIN32)
|
|
|
|
|
std::string::size_type dot = _filename.find_last_of("\\/");
|
|
|
|
|
#else
|
|
|
|
|
std::string::size_type dot = _filename.rfind("/");
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
if (dot == std::string::npos){
|
|
|
|
|
path_ = "./";
|
|
|
|
|
objName_ = _filename;
|
|
|
|
|
}else{
|
|
|
|
|
path_ = _filename.substr(0,dot+1);
|
|
|
|
|
objName_ = _filename.substr(dot+1);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//remove the file extension
|
2016-04-05 15:43:32 +02:00
|
|
|
dot = objName_.find_last_of(".");
|
2009-02-06 13:37:46 +00:00
|
|
|
|
|
|
|
|
if(dot != std::string::npos)
|
2016-02-11 14:49:44 +01:00
|
|
|
objName_ = objName_.substr(0,dot);
|
2009-02-06 13:37:46 +00:00
|
|
|
}
|
|
|
|
|
|
2013-07-30 08:43:40 +00:00
|
|
|
bool result = write(out, _be, _opt, _precision);
|
2009-02-06 13:37:46 +00:00
|
|
|
|
|
|
|
|
out.close();
|
|
|
|
|
return result;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
|
|
2013-07-23 13:46:43 +00:00
|
|
|
size_t _OBJWriter_::getMaterial(OpenMesh::Vec3f _color) const
|
2009-02-06 13:37:46 +00:00
|
|
|
{
|
2013-07-23 13:46:43 +00:00
|
|
|
for (size_t i=0; i < material_.size(); i++)
|
2009-02-06 13:37:46 +00:00
|
|
|
if(material_[i] == _color)
|
|
|
|
|
return i;
|
|
|
|
|
|
|
|
|
|
//not found add new material
|
|
|
|
|
material_.push_back( _color );
|
|
|
|
|
return material_.size()-1;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
|
|
2013-07-23 13:46:43 +00:00
|
|
|
size_t _OBJWriter_::getMaterial(OpenMesh::Vec4f _color) const
|
2009-02-06 13:37:46 +00:00
|
|
|
{
|
2013-07-23 13:46:43 +00:00
|
|
|
for (size_t i=0; i < materialA_.size(); i++)
|
2009-02-06 13:37:46 +00:00
|
|
|
if(materialA_[i] == _color)
|
|
|
|
|
return i;
|
|
|
|
|
|
|
|
|
|
//not found add new material
|
|
|
|
|
materialA_.push_back( _color );
|
|
|
|
|
return materialA_.size()-1;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
|
|
|
|
|
|
bool
|
|
|
|
|
_OBJWriter_::
|
2009-11-17 13:54:16 +00:00
|
|
|
writeMaterial(std::ostream& _out, BaseExporter& _be, Options _opt) const
|
2009-02-06 13:37:46 +00:00
|
|
|
{
|
|
|
|
|
OpenMesh::Vec3f c;
|
|
|
|
|
OpenMesh::Vec4f cA;
|
|
|
|
|
|
|
|
|
|
material_.clear();
|
|
|
|
|
materialA_.clear();
|
|
|
|
|
|
|
|
|
|
//iterate over faces
|
2013-07-23 13:46:43 +00:00
|
|
|
for (size_t i=0, nF=_be.n_faces(); i<nF; ++i)
|
2009-02-06 13:37:46 +00:00
|
|
|
{
|
|
|
|
|
//color with alpha
|
|
|
|
|
if ( _opt.color_has_alpha() ){
|
2013-07-23 13:46:43 +00:00
|
|
|
cA = color_cast<OpenMesh::Vec4f> (_be.colorA( FaceHandle(int(i)) ));
|
2009-02-06 13:37:46 +00:00
|
|
|
getMaterial(cA);
|
|
|
|
|
}else{
|
|
|
|
|
//and without alpha
|
2013-07-23 13:46:43 +00:00
|
|
|
c = color_cast<OpenMesh::Vec3f> (_be.color( FaceHandle(int(i)) ));
|
2009-02-06 13:37:46 +00:00
|
|
|
getMaterial(c);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//write the materials
|
|
|
|
|
if ( _opt.color_has_alpha() )
|
2013-07-23 13:46:43 +00:00
|
|
|
for (size_t i=0; i < materialA_.size(); i++){
|
2015-01-19 17:37:47 +00:00
|
|
|
_out << "newmtl " << "mat" << i << '\n';
|
|
|
|
|
_out << "Ka 0.5000 0.5000 0.5000" << '\n';
|
2016-02-11 14:49:44 +01:00
|
|
|
_out << "Kd " << materialA_[i][0] << ' ' << materialA_[i][1] << ' ' << materialA_[i][2] << '\n';
|
2015-01-19 17:37:47 +00:00
|
|
|
_out << "Tr " << materialA_[i][3] << '\n';
|
|
|
|
|
_out << "illum 1" << '\n';
|
2009-02-06 13:37:46 +00:00
|
|
|
}
|
|
|
|
|
else
|
2013-07-23 13:46:43 +00:00
|
|
|
for (size_t i=0; i < material_.size(); i++){
|
2015-01-19 17:37:47 +00:00
|
|
|
_out << "newmtl " << "mat" << i << '\n';
|
|
|
|
|
_out << "Ka 0.5000 0.5000 0.5000" << '\n';
|
2016-02-11 14:49:44 +01:00
|
|
|
_out << "Kd " << material_[i][0] << ' ' << material_[i][1] << ' ' << material_[i][2] << '\n';
|
2015-01-19 17:37:47 +00:00
|
|
|
_out << "illum 1" << '\n';
|
2009-02-06 13:37:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
bool
|
|
|
|
|
_OBJWriter_::
|
2012-10-15 10:28:14 +00:00
|
|
|
write(std::ostream& _out, BaseExporter& _be, Options _opt, std::streamsize _precision) const
|
2009-02-06 13:37:46 +00:00
|
|
|
{
|
2013-07-24 06:36:26 +00:00
|
|
|
unsigned int idx;
|
|
|
|
|
size_t i, j,nV, nF;
|
2009-02-06 13:37:46 +00:00
|
|
|
Vec3f v, n;
|
|
|
|
|
Vec2f t;
|
|
|
|
|
VertexHandle vh;
|
|
|
|
|
std::vector<VertexHandle> vhandles;
|
|
|
|
|
bool useMatrial = false;
|
|
|
|
|
OpenMesh::Vec3f c;
|
|
|
|
|
OpenMesh::Vec4f cA;
|
|
|
|
|
|
|
|
|
|
omlog() << "[OBJWriter] : write file\n";
|
|
|
|
|
|
2012-10-15 10:28:14 +00:00
|
|
|
_out.precision(_precision);
|
2009-02-06 13:37:46 +00:00
|
|
|
|
|
|
|
|
// check exporter features
|
|
|
|
|
if (!check( _be, _opt))
|
|
|
|
|
return false;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// check writer features
|
|
|
|
|
if ( _opt.check(Options::Binary) || // not supported by format
|
2014-07-25 09:13:33 +00:00
|
|
|
_opt.check(Options::FaceNormal))
|
2009-02-06 13:37:46 +00:00
|
|
|
return false;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
//create material file if needed
|
|
|
|
|
if ( _opt.check(Options::FaceColor) ){
|
|
|
|
|
|
|
|
|
|
std::string matFile = path_ + objName_ + ".mat";
|
|
|
|
|
|
|
|
|
|
std::fstream matStream(matFile.c_str(), std::ios_base::out );
|
|
|
|
|
|
2014-02-28 07:15:27 +00:00
|
|
|
if (!matStream)
|
2009-02-06 13:37:46 +00:00
|
|
|
{
|
|
|
|
|
omerr() << "[OBJWriter] : cannot write material file " << matFile << std::endl;
|
|
|
|
|
|
|
|
|
|
}else{
|
|
|
|
|
useMatrial = writeMaterial(matStream, _be, _opt);
|
|
|
|
|
|
|
|
|
|
matStream.close();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// header
|
|
|
|
|
_out << "# " << _be.n_vertices() << " vertices, ";
|
2015-01-19 17:37:47 +00:00
|
|
|
_out << _be.n_faces() << " faces" << '\n';
|
2009-02-06 13:37:46 +00:00
|
|
|
|
|
|
|
|
// material file
|
|
|
|
|
if (useMatrial && _opt.check(Options::FaceColor) )
|
2015-01-19 17:37:47 +00:00
|
|
|
_out << "mtllib " << objName_ << ".mat" << '\n';
|
2009-02-06 13:37:46 +00:00
|
|
|
|
|
|
|
|
// vertex data (point, normals, texcoords)
|
|
|
|
|
for (i=0, nV=_be.n_vertices(); i<nV; ++i)
|
|
|
|
|
{
|
2013-07-24 06:36:26 +00:00
|
|
|
vh = VertexHandle(int(i));
|
2009-02-06 13:37:46 +00:00
|
|
|
v = _be.point(vh);
|
|
|
|
|
n = _be.normal(vh);
|
|
|
|
|
t = _be.texcoord(vh);
|
|
|
|
|
|
2015-01-19 17:37:47 +00:00
|
|
|
_out << "v " << v[0] <<" "<< v[1] <<" "<< v[2] << '\n';
|
2009-02-06 13:37:46 +00:00
|
|
|
|
|
|
|
|
if (_opt.check(Options::VertexNormal))
|
2015-01-19 17:37:47 +00:00
|
|
|
_out << "vn " << n[0] <<" "<< n[1] <<" "<< n[2] << '\n';
|
2009-02-06 13:37:46 +00:00
|
|
|
|
|
|
|
|
if (_opt.check(Options::VertexTexCoord))
|
2015-01-19 17:37:47 +00:00
|
|
|
_out << "vt " << t[0] <<" "<< t[1] << '\n';
|
2009-02-06 13:37:46 +00:00
|
|
|
}
|
|
|
|
|
|
2013-07-23 13:46:43 +00:00
|
|
|
size_t lastMat = std::numeric_limits<std::size_t>::max();
|
2009-02-06 13:37:46 +00:00
|
|
|
|
2013-05-16 11:11:41 +00:00
|
|
|
// we do not want to write seperators if we only write vertex indices
|
|
|
|
|
bool onlyVertices = !_opt.check(Options::VertexTexCoord)
|
|
|
|
|
&& !_opt.check(Options::VertexNormal);
|
|
|
|
|
|
2009-02-06 13:37:46 +00:00
|
|
|
// faces (indices starting at 1 not 0)
|
|
|
|
|
for (i=0, nF=_be.n_faces(); i<nF; ++i)
|
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
if (useMatrial && _opt.check(Options::FaceColor) ){
|
2013-07-23 13:46:43 +00:00
|
|
|
size_t material = std::numeric_limits<std::size_t>::max();
|
2009-04-09 07:11:30 +00:00
|
|
|
|
2009-02-06 13:37:46 +00:00
|
|
|
//color with alpha
|
|
|
|
|
if ( _opt.color_has_alpha() ){
|
2013-07-23 13:46:43 +00:00
|
|
|
cA = color_cast<OpenMesh::Vec4f> (_be.colorA( FaceHandle(int(i)) ));
|
2009-04-09 07:11:30 +00:00
|
|
|
material = getMaterial(cA);
|
|
|
|
|
} else{
|
2009-02-06 13:37:46 +00:00
|
|
|
//and without alpha
|
2013-07-23 13:46:43 +00:00
|
|
|
c = color_cast<OpenMesh::Vec3f> (_be.color( FaceHandle(int(i)) ));
|
2009-04-09 07:11:30 +00:00
|
|
|
material = getMaterial(c);
|
2009-02-06 13:37:46 +00:00
|
|
|
}
|
|
|
|
|
|
2009-10-23 13:15:03 +00:00
|
|
|
// if we are ina a new material block, specify in the file which material to use
|
2009-04-09 07:11:30 +00:00
|
|
|
if(lastMat != material) {
|
2015-01-19 17:37:47 +00:00
|
|
|
_out << "usemtl mat" << material << '\n';
|
2013-08-21 12:03:51 +00:00
|
|
|
lastMat = material;
|
2009-04-09 07:11:30 +00:00
|
|
|
}
|
2009-02-06 13:37:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
_out << "f";
|
|
|
|
|
|
2013-07-24 06:36:26 +00:00
|
|
|
_be.get_vhandles(FaceHandle(int(i)), vhandles);
|
2009-02-06 13:37:46 +00:00
|
|
|
|
|
|
|
|
for (j=0; j< vhandles.size(); ++j)
|
|
|
|
|
{
|
2012-10-15 10:28:14 +00:00
|
|
|
|
2009-10-23 13:15:03 +00:00
|
|
|
// Write vertex index
|
2009-02-06 13:37:46 +00:00
|
|
|
idx = vhandles[j].idx() + 1;
|
|
|
|
|
_out << " " << idx;
|
|
|
|
|
|
2013-05-16 11:11:41 +00:00
|
|
|
if (!onlyVertices) {
|
|
|
|
|
// write separator
|
|
|
|
|
_out << "/" ;
|
2012-10-15 10:28:14 +00:00
|
|
|
|
2013-05-16 11:11:41 +00:00
|
|
|
// write vertex texture coordinate index
|
|
|
|
|
if (_opt.check(Options::VertexTexCoord))
|
|
|
|
|
_out << idx;
|
2009-02-06 13:37:46 +00:00
|
|
|
|
2013-05-16 11:11:41 +00:00
|
|
|
// write vertex normal index
|
|
|
|
|
if ( _opt.check(Options::VertexNormal) ) {
|
|
|
|
|
// write separator
|
|
|
|
|
_out << "/" ;
|
|
|
|
|
_out << idx;
|
|
|
|
|
}
|
|
|
|
|
}
|
2009-02-06 13:37:46 +00:00
|
|
|
}
|
|
|
|
|
|
2015-01-19 17:37:47 +00:00
|
|
|
_out << '\n';
|
2009-02-06 13:37:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
material_.clear();
|
|
|
|
|
materialA_.clear();
|
|
|
|
|
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
//=============================================================================
|
|
|
|
|
} // namespace IO
|
|
|
|
|
} // namespace OpenMesh
|
|
|
|
|
//=============================================================================
|