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
|
|
|
* *
|
2009-06-12 12:46:12 +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
|
|
|
|
|
|
|
|
/*===========================================================================*\
|
2009-06-12 12:46:12 +00:00
|
|
|
* *
|
2009-06-04 08:46:29 +00:00
|
|
|
* $Revision$ *
|
|
|
|
|
* $Date$ *
|
2009-02-06 13:37:46 +00:00
|
|
|
* *
|
|
|
|
|
\*===========================================================================*/
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
//== INCLUDES =================================================================
|
|
|
|
|
|
2015-08-21 12:44:24 +00:00
|
|
|
#include <fstream>
|
2009-02-06 13:37:46 +00:00
|
|
|
#include <OpenMesh/Core/System/config.h>
|
|
|
|
|
#include <OpenMesh/Core/System/omstream.hh>
|
|
|
|
|
#include <OpenMesh/Core/Utils/Endian.hh>
|
|
|
|
|
#include <OpenMesh/Core/IO/IOManager.hh>
|
|
|
|
|
#include <OpenMesh/Core/IO/BinaryHelper.hh>
|
|
|
|
|
#include <OpenMesh/Core/IO/writer/PLYWriter.hh>
|
2015-06-16 08:52:20 +00:00
|
|
|
#include <OpenMesh/Core/Utils/GenProg.hh>
|
2009-02-06 13:37:46 +00:00
|
|
|
|
|
|
|
|
#include <OpenMesh/Core/IO/SR_store.hh>
|
|
|
|
|
|
2015-06-09 08:58:41 +00:00
|
|
|
#include <iostream>
|
|
|
|
|
|
2009-02-06 13:37:46 +00:00
|
|
|
//=== NAMESPACES ==============================================================
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
namespace OpenMesh {
|
|
|
|
|
namespace IO {
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
//=== INSTANCIATE =============================================================
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// register the PLYLoader singleton with MeshLoader
|
|
|
|
|
_PLYWriter_ __PLYWriterInstance;
|
|
|
|
|
_PLYWriter_& PLYWriter() { return __PLYWriterInstance; }
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
//=== IMPLEMENTATION ==========================================================
|
|
|
|
|
|
|
|
|
|
|
2015-06-16 08:52:20 +00:00
|
|
|
_PLYWriter_::_PLYWriter_()
|
|
|
|
|
{
|
|
|
|
|
IOManager().register_module(this);
|
|
|
|
|
|
|
|
|
|
nameOfType_[Unsupported] = "";
|
|
|
|
|
nameOfType_[ValueTypeCHAR] = "char";
|
|
|
|
|
nameOfType_[ValueTypeUCHAR] = nameOfType_[ValueTypeUINT8] = "uchar";
|
|
|
|
|
nameOfType_[ValueTypeUSHORT] = "ushort";
|
|
|
|
|
nameOfType_[ValueTypeSHORT] = "short";
|
|
|
|
|
nameOfType_[ValueTypeUINT] = "uint";
|
|
|
|
|
nameOfType_[ValueTypeINT] = "int";
|
|
|
|
|
nameOfType_[ValueTypeFLOAT32] = nameOfType_[ValueTypeFLOAT] = "float";
|
|
|
|
|
nameOfType_[ValueTypeDOUBLE] = "double";
|
|
|
|
|
}
|
2009-02-06 13:37:46 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
bool
|
|
|
|
|
_PLYWriter_::
|
2012-10-15 10:28:14 +00:00
|
|
|
write(const std::string& _filename, BaseExporter& _be, Options _opt, std::streamsize _precision) const
|
2015-08-21 12:44:24 +00:00
|
|
|
{
|
|
|
|
|
|
|
|
|
|
// open file
|
|
|
|
|
std::ofstream out(_filename.c_str(), (_opt.check(Options::Binary) ? std::ios_base::binary | std::ios_base::out
|
|
|
|
|
: std::ios_base::out) );
|
|
|
|
|
return write(out, _be, _opt, _precision);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
bool
|
|
|
|
|
_PLYWriter_::
|
|
|
|
|
write(std::ostream& _os, BaseExporter& _be, Options _opt, std::streamsize _precision) const
|
2009-02-06 13:37:46 +00:00
|
|
|
{
|
|
|
|
|
// check exporter features
|
|
|
|
|
if ( !check( _be, _opt ) )
|
|
|
|
|
return false;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// check writer features
|
2010-11-23 17:36:12 +00:00
|
|
|
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;
|
|
|
|
|
}
|
2012-10-15 10:28:14 +00:00
|
|
|
|
2010-11-23 17:36:12 +00:00
|
|
|
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;
|
|
|
|
|
}
|
2009-02-06 13:37:46 +00:00
|
|
|
|
|
|
|
|
options_ = _opt;
|
|
|
|
|
|
2012-10-15 10:28:14 +00:00
|
|
|
|
2009-11-17 13:54:16 +00:00
|
|
|
if (!_os.good())
|
|
|
|
|
{
|
|
|
|
|
omerr() << "[PLYWriter] : cannot write to stream "
|
|
|
|
|
<< std::endl;
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
2012-10-15 14:46:52 +00:00
|
|
|
if (!_opt.check(Options::Binary))
|
|
|
|
|
_os.precision(_precision);
|
2012-10-15 10:28:14 +00:00
|
|
|
|
2009-11-17 13:54:16 +00:00
|
|
|
// write to file
|
|
|
|
|
bool result = (_opt.check(Options::Binary) ?
|
|
|
|
|
write_binary(_os, _be, _opt) :
|
|
|
|
|
write_ascii(_os, _be, _opt));
|
|
|
|
|
|
|
|
|
|
return result;
|
|
|
|
|
}
|
|
|
|
|
|
2015-06-16 08:52:20 +00:00
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
|
|
|
|
|
|
// helper function for casting a property
|
|
|
|
|
template<typename T>
|
|
|
|
|
const PropertyT<T>* castProperty(const BaseProperty* _prop)
|
|
|
|
|
{
|
|
|
|
|
return dynamic_cast< const PropertyT<T>* >(_prop);
|
|
|
|
|
}
|
2009-02-06 13:37:46 +00:00
|
|
|
|
|
|
|
|
//-----------------------------------------------------------------------------
|
2015-06-16 08:52:20 +00:00
|
|
|
std::vector<_PLYWriter_::CustomProperty> _PLYWriter_::writeCustomTypeHeader(std::ostream& _out, BaseKernel::const_prop_iterator _begin, BaseKernel::const_prop_iterator _end) const
|
|
|
|
|
{
|
|
|
|
|
std::vector<CustomProperty> customProps;
|
|
|
|
|
for (;_begin != _end; ++_begin)
|
|
|
|
|
{
|
|
|
|
|
BaseProperty* prop = *_begin;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// check, if property is persistant
|
2015-07-28 18:16:16 +00:00
|
|
|
if (!prop || !prop->persistent())
|
2015-06-16 08:52:20 +00:00
|
|
|
continue;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// identify type of property
|
|
|
|
|
CustomProperty cProp(prop);
|
|
|
|
|
size_t propSize = prop->element_size();
|
|
|
|
|
switch (propSize)
|
|
|
|
|
{
|
|
|
|
|
case 1:
|
|
|
|
|
{
|
|
|
|
|
assert_compile(sizeof(char) == 1);
|
|
|
|
|
//check, if prop is a char or unsigned char by dynamic_cast
|
|
|
|
|
//char, unsigned char and signed char are 3 distinct types
|
|
|
|
|
if (castProperty<signed char>(prop) != 0 || castProperty<char>(prop) != 0) //treat char as signed char
|
|
|
|
|
cProp.type = ValueTypeCHAR;
|
|
|
|
|
else if (castProperty<unsigned char>(prop) != 0)
|
|
|
|
|
cProp.type = ValueTypeUCHAR;
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
case 2:
|
|
|
|
|
{
|
|
|
|
|
assert_compile (sizeof(short) == 2);
|
|
|
|
|
if (castProperty<signed short>(prop) != 0)
|
|
|
|
|
cProp.type = ValueTypeSHORT;
|
|
|
|
|
else if (castProperty<unsigned short>(prop) != 0)
|
|
|
|
|
cProp.type = ValueTypeUSHORT;
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
case 4:
|
|
|
|
|
{
|
|
|
|
|
assert_compile (sizeof(int) == 4);
|
|
|
|
|
assert_compile (sizeof(float) == 4);
|
|
|
|
|
if (castProperty<signed int>(prop) != 0)
|
|
|
|
|
cProp.type = ValueTypeINT;
|
|
|
|
|
else if (castProperty<unsigned int>(prop) != 0)
|
|
|
|
|
cProp.type = ValueTypeUINT;
|
|
|
|
|
else if (castProperty<float>(prop) != 0)
|
|
|
|
|
cProp.type = ValueTypeFLOAT;
|
|
|
|
|
break;
|
2009-02-06 13:37:46 +00:00
|
|
|
|
2015-06-16 08:52:20 +00:00
|
|
|
}
|
|
|
|
|
case 8:
|
|
|
|
|
assert_compile (sizeof(double) == 8);
|
|
|
|
|
if (castProperty<double>(prop) != 0)
|
|
|
|
|
cProp.type = ValueTypeDOUBLE;
|
|
|
|
|
break;
|
|
|
|
|
default:
|
|
|
|
|
break;
|
|
|
|
|
}
|
2009-02-06 13:37:46 +00:00
|
|
|
|
2015-06-16 08:52:20 +00:00
|
|
|
if (cProp.type != Unsupported)
|
|
|
|
|
{
|
|
|
|
|
// property type was identified and it is persistant, write into header
|
|
|
|
|
customProps.push_back(cProp);
|
|
|
|
|
_out << "property " << nameOfType_[cProp.type] << " " << cProp.property->name() << "\n";
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return customProps;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
|
|
2015-08-28 09:20:26 +00:00
|
|
|
template<bool binary>
|
|
|
|
|
void _PLYWriter_::write_customProp(std::ostream& _out, const CustomProperty& _prop, size_t _index) const
|
2015-06-16 08:52:20 +00:00
|
|
|
{
|
|
|
|
|
if (_prop.type == ValueTypeCHAR)
|
2015-08-28 09:20:26 +00:00
|
|
|
writeProxy(_prop.type,_out, castProperty<signed char>(_prop.property)->data()[_index], OpenMesh::GenProg::Bool2Type<binary>());
|
2015-06-16 08:52:20 +00:00
|
|
|
else if (_prop.type == ValueTypeUCHAR || _prop.type == ValueTypeUINT8)
|
2015-08-28 09:20:26 +00:00
|
|
|
writeProxy(_prop.type,_out, castProperty<unsigned char>(_prop.property)->data()[_index], OpenMesh::GenProg::Bool2Type<binary>());
|
2015-06-16 08:52:20 +00:00
|
|
|
else if (_prop.type == ValueTypeSHORT)
|
2015-08-28 09:20:26 +00:00
|
|
|
writeProxy(_prop.type,_out, castProperty<signed short>(_prop.property)->data()[_index], OpenMesh::GenProg::Bool2Type<binary>());
|
2015-06-16 08:52:20 +00:00
|
|
|
else if (_prop.type == ValueTypeUSHORT)
|
2015-08-28 09:20:26 +00:00
|
|
|
writeProxy(_prop.type,_out, castProperty<unsigned short>(_prop.property)->data()[_index], OpenMesh::GenProg::Bool2Type<binary>());
|
2015-06-16 08:52:20 +00:00
|
|
|
else if (_prop.type == ValueTypeUINT)
|
2015-08-28 09:20:26 +00:00
|
|
|
writeProxy(_prop.type,_out, castProperty<unsigned int>(_prop.property)->data()[_index], OpenMesh::GenProg::Bool2Type<binary>());
|
2015-06-16 08:52:20 +00:00
|
|
|
else if (_prop.type == ValueTypeINT || _prop.type == ValueTypeINT32)
|
2015-08-28 09:20:26 +00:00
|
|
|
writeProxy(_prop.type,_out, castProperty<signed int>(_prop.property)->data()[_index], OpenMesh::GenProg::Bool2Type<binary>());
|
2015-06-16 08:52:20 +00:00
|
|
|
else if (_prop.type == ValueTypeFLOAT || _prop.type == ValueTypeFLOAT32)
|
2015-08-28 09:20:26 +00:00
|
|
|
writeProxy(_prop.type,_out, castProperty<float>(_prop.property)->data()[_index], OpenMesh::GenProg::Bool2Type<binary>());
|
2015-06-16 08:52:20 +00:00
|
|
|
else if (_prop.type == ValueTypeDOUBLE)
|
2015-08-28 09:20:26 +00:00
|
|
|
writeProxy(_prop.type,_out, castProperty<double>(_prop.property)->data()[_index], OpenMesh::GenProg::Bool2Type<binary>());
|
2015-06-16 08:52:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void _PLYWriter_::write_header(std::ostream& _out, BaseExporter& _be, Options& _opt, std::vector<CustomProperty>& _ovProps, std::vector<CustomProperty>& _ofProps) const {
|
2009-02-06 13:37:46 +00:00
|
|
|
//writing header
|
2015-01-19 17:37:47 +00:00
|
|
|
_out << "ply" << '\n';
|
2013-03-01 15:32:46 +00:00
|
|
|
|
|
|
|
|
if (_opt.is_binary()) {
|
|
|
|
|
_out << "format ";
|
|
|
|
|
if ( options_.check(Options::MSB) )
|
|
|
|
|
_out << "binary_big_endian ";
|
|
|
|
|
else
|
|
|
|
|
_out << "binary_little_endian ";
|
2015-01-19 17:37:47 +00:00
|
|
|
_out << "1.0" << '\n';
|
2013-03-01 15:32:46 +00:00
|
|
|
} else
|
2015-01-19 17:37:47 +00:00
|
|
|
_out << "format ascii 1.0" << '\n';
|
2013-03-01 15:32:46 +00:00
|
|
|
|
2015-01-19 17:37:47 +00:00
|
|
|
_out << "element vertex " << _be.n_vertices() << '\n';
|
2009-02-06 13:37:46 +00:00
|
|
|
|
2015-01-19 17:37:47 +00:00
|
|
|
_out << "property float x" << '\n';
|
|
|
|
|
_out << "property float y" << '\n';
|
|
|
|
|
_out << "property float z" << '\n';
|
2009-02-06 13:37:46 +00:00
|
|
|
|
2011-12-01 16:55:24 +00:00
|
|
|
if ( _opt.vertex_has_normal() ){
|
2015-01-19 17:37:47 +00:00
|
|
|
_out << "property float nx" << '\n';
|
|
|
|
|
_out << "property float ny" << '\n';
|
|
|
|
|
_out << "property float nz" << '\n';
|
2011-12-01 16:55:24 +00:00
|
|
|
}
|
|
|
|
|
|
2009-06-12 12:46:12 +00:00
|
|
|
if ( _opt.vertex_has_texcoord() ){
|
2015-01-19 17:37:47 +00:00
|
|
|
_out << "property float u" << '\n';
|
|
|
|
|
_out << "property float v" << '\n';
|
2009-06-12 12:46:12 +00:00
|
|
|
}
|
|
|
|
|
|
2009-02-06 13:37:46 +00:00
|
|
|
if ( _opt.vertex_has_color() ){
|
2013-03-01 15:32:46 +00:00
|
|
|
if ( _opt.color_is_float() ) {
|
2015-01-19 17:37:47 +00:00
|
|
|
_out << "property float red" << '\n';
|
|
|
|
|
_out << "property float green" << '\n';
|
|
|
|
|
_out << "property float blue" << '\n';
|
2013-03-01 15:32:46 +00:00
|
|
|
|
|
|
|
|
if ( _opt.color_has_alpha() )
|
2015-01-19 17:37:47 +00:00
|
|
|
_out << "property float alpha" << '\n';
|
2013-03-01 15:32:46 +00:00
|
|
|
} else {
|
2015-01-19 17:37:47 +00:00
|
|
|
_out << "property uchar red" << '\n';
|
|
|
|
|
_out << "property uchar green" << '\n';
|
|
|
|
|
_out << "property uchar blue" << '\n';
|
2013-03-01 15:32:46 +00:00
|
|
|
|
|
|
|
|
if ( _opt.color_has_alpha() )
|
2015-01-19 17:37:47 +00:00
|
|
|
_out << "property uchar alpha" << '\n';
|
2013-03-01 15:32:46 +00:00
|
|
|
}
|
2009-02-06 13:37:46 +00:00
|
|
|
}
|
|
|
|
|
|
2015-08-28 09:20:26 +00:00
|
|
|
_ovProps = writeCustomTypeHeader(_out, _be.kernel()->vprops_begin(), _be.kernel()->vprops_end());
|
2015-06-16 08:52:20 +00:00
|
|
|
|
2015-01-19 17:37:47 +00:00
|
|
|
_out << "element face " << _be.n_faces() << '\n';
|
|
|
|
|
_out << "property list uchar int vertex_indices" << '\n';
|
2015-06-16 08:52:20 +00:00
|
|
|
|
2015-08-28 09:20:26 +00:00
|
|
|
_ofProps = writeCustomTypeHeader(_out, _be.kernel()->fprops_begin(), _be.kernel()->fprops_end());
|
2015-06-16 08:52:20 +00:00
|
|
|
|
2015-01-19 17:37:47 +00:00
|
|
|
_out << "end_header" << '\n';
|
2013-03-01 15:32:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
bool
|
|
|
|
|
_PLYWriter_::
|
|
|
|
|
write_ascii(std::ostream& _out, BaseExporter& _be, Options _opt) const
|
|
|
|
|
{
|
2014-05-09 12:44:18 +00:00
|
|
|
|
2013-08-21 10:00:59 +00:00
|
|
|
unsigned int i, nV, nF;
|
2013-03-01 15:32:46 +00:00
|
|
|
Vec3f v, n;
|
|
|
|
|
OpenMesh::Vec3ui c;
|
|
|
|
|
OpenMesh::Vec4ui cA;
|
|
|
|
|
OpenMesh::Vec3f cf;
|
|
|
|
|
OpenMesh::Vec4f cAf;
|
|
|
|
|
OpenMesh::Vec2f t;
|
|
|
|
|
VertexHandle vh;
|
|
|
|
|
std::vector<VertexHandle> vhandles;
|
|
|
|
|
|
2015-06-16 08:52:20 +00:00
|
|
|
std::vector<CustomProperty> vProps;
|
|
|
|
|
std::vector<CustomProperty> fProps;
|
|
|
|
|
|
|
|
|
|
write_header(_out, _be, _opt, vProps, fProps);
|
2009-02-06 13:37:46 +00:00
|
|
|
|
2013-03-04 16:36:20 +00:00
|
|
|
if (_opt.color_is_float())
|
|
|
|
|
_out << std::fixed;
|
|
|
|
|
|
2009-02-06 13:37:46 +00:00
|
|
|
// vertex data (point, normals, colors, texcoords)
|
2013-07-23 14:03:45 +00:00
|
|
|
for (i=0, nV=int(_be.n_vertices()); i<nV; ++i)
|
2009-02-06 13:37:46 +00:00
|
|
|
{
|
|
|
|
|
vh = VertexHandle(i);
|
|
|
|
|
v = _be.point(vh);
|
|
|
|
|
|
|
|
|
|
//Vertex
|
|
|
|
|
_out << v[0] << " " << v[1] << " " << v[2];
|
|
|
|
|
|
2011-12-01 16:55:24 +00:00
|
|
|
// Vertex Normals
|
|
|
|
|
if ( _opt.vertex_has_normal() ){
|
|
|
|
|
n = _be.normal(vh);
|
|
|
|
|
_out << " " << n[0] << " " << n[1] << " " << n[2];
|
|
|
|
|
}
|
|
|
|
|
|
2009-06-12 12:46:12 +00:00
|
|
|
// Vertex TexCoords
|
|
|
|
|
if ( _opt.vertex_has_texcoord() ) {
|
|
|
|
|
t = _be.texcoord(vh);
|
|
|
|
|
_out << " " << t[0] << " " << t[1];
|
|
|
|
|
}
|
|
|
|
|
|
2009-02-06 13:37:46 +00:00
|
|
|
// VertexColor
|
|
|
|
|
if ( _opt.vertex_has_color() ) {
|
|
|
|
|
//with alpha
|
|
|
|
|
if ( _opt.color_has_alpha() ){
|
2013-03-01 15:32:46 +00:00
|
|
|
if (_opt.color_is_float()) {
|
|
|
|
|
cAf = _be.colorAf(vh);
|
|
|
|
|
_out << " " << cAf;
|
|
|
|
|
} else {
|
|
|
|
|
cA = _be.colorAi(vh);
|
|
|
|
|
_out << " " << cA;
|
|
|
|
|
}
|
2009-02-06 13:37:46 +00:00
|
|
|
}else{
|
|
|
|
|
//without alpha
|
2013-03-01 15:32:46 +00:00
|
|
|
if (_opt.color_is_float()) {
|
|
|
|
|
cf = _be.colorf(vh);
|
|
|
|
|
_out << " " << cf;
|
|
|
|
|
} else {
|
|
|
|
|
c = _be.colori(vh);
|
|
|
|
|
_out << " " << c;
|
|
|
|
|
}
|
2009-02-06 13:37:46 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2015-06-16 08:52:20 +00:00
|
|
|
|
|
|
|
|
// write custom properties for vertices
|
|
|
|
|
for (std::vector<CustomProperty>::iterator iter = vProps.begin(); iter < vProps.end(); ++iter)
|
2015-08-28 09:20:26 +00:00
|
|
|
write_customProp<false>(_out,*iter,i);
|
2015-06-16 08:52:20 +00:00
|
|
|
|
2009-02-06 13:37:46 +00:00
|
|
|
_out << "\n";
|
|
|
|
|
}
|
2009-06-12 12:46:12 +00:00
|
|
|
|
2009-02-06 13:37:46 +00:00
|
|
|
// faces (indices starting at 0)
|
2015-06-16 08:52:20 +00:00
|
|
|
for (i=0, nF=int(_be.n_faces()); i<nF; ++i)
|
2009-02-06 13:37:46 +00:00
|
|
|
{
|
2015-06-16 08:52:20 +00:00
|
|
|
// write vertex indices per face
|
|
|
|
|
nV = _be.get_vhandles(FaceHandle(i), vhandles);
|
|
|
|
|
_out << nV;
|
|
|
|
|
for (size_t j=0; j<vhandles.size(); ++j)
|
|
|
|
|
_out << " " << vhandles[j].idx();
|
|
|
|
|
|
|
|
|
|
// write custom props
|
|
|
|
|
for (std::vector<CustomProperty>::iterator iter = fProps.begin(); iter < fProps.end(); ++iter)
|
2015-08-28 09:20:26 +00:00
|
|
|
write_customProp<false>(_out,*iter,i);
|
2015-06-16 08:52:20 +00:00
|
|
|
_out << "\n";
|
2009-02-06 13:37:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
|
|
2009-11-17 13:54:16 +00:00
|
|
|
void _PLYWriter_::writeValue(ValueType _type, std::ostream& _out, int value) const {
|
2009-02-06 13:37:46 +00:00
|
|
|
|
|
|
|
|
uint32_t tmp32;
|
|
|
|
|
uint8_t tmp8;
|
|
|
|
|
|
|
|
|
|
switch (_type) {
|
|
|
|
|
case ValueTypeINT:
|
|
|
|
|
case ValueTypeINT32:
|
|
|
|
|
tmp32 = value;
|
|
|
|
|
store(_out, tmp32, options_.check(Options::MSB) );
|
|
|
|
|
break;
|
|
|
|
|
// case ValueTypeUINT8:
|
|
|
|
|
default :
|
|
|
|
|
tmp8 = value;
|
|
|
|
|
store(_out, tmp8, options_.check(Options::MSB) );
|
|
|
|
|
break;
|
|
|
|
|
// default :
|
|
|
|
|
// std::cerr << "unsupported conversion type to int: " << _type << std::endl;
|
|
|
|
|
// break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2009-11-17 13:54:16 +00:00
|
|
|
void _PLYWriter_::writeValue(ValueType _type, std::ostream& _out, unsigned int value) const {
|
2009-02-06 13:37:46 +00:00
|
|
|
|
|
|
|
|
uint32_t tmp32;
|
|
|
|
|
uint8_t tmp8;
|
|
|
|
|
|
|
|
|
|
switch (_type) {
|
|
|
|
|
case ValueTypeINT:
|
|
|
|
|
case ValueTypeINT32:
|
|
|
|
|
tmp32 = value;
|
|
|
|
|
store(_out, tmp32, options_.check(Options::MSB) );
|
|
|
|
|
break;
|
|
|
|
|
// case ValueTypeUINT8:
|
|
|
|
|
default :
|
|
|
|
|
tmp8 = value;
|
|
|
|
|
store(_out, tmp8, options_.check(Options::MSB) );
|
|
|
|
|
break;
|
|
|
|
|
// default :
|
|
|
|
|
// std::cerr << "unsupported conversion type to int: " << _type << std::endl;
|
|
|
|
|
// break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2009-11-17 13:54:16 +00:00
|
|
|
void _PLYWriter_::writeValue(ValueType _type, std::ostream& _out, float value) const {
|
2009-02-06 13:37:46 +00:00
|
|
|
|
|
|
|
|
float32_t tmp;
|
|
|
|
|
|
|
|
|
|
switch (_type) {
|
|
|
|
|
case ValueTypeFLOAT32:
|
|
|
|
|
case ValueTypeFLOAT:
|
|
|
|
|
tmp = value;
|
|
|
|
|
store( _out , tmp, options_.check(Options::MSB) );
|
|
|
|
|
break;
|
|
|
|
|
default :
|
|
|
|
|
std::cerr << "unsupported conversion type to float: " << _type << std::endl;
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2015-08-28 09:20:26 +00:00
|
|
|
void _PLYWriter_::writeValue(ValueType _type, std::ostream& _out, double value) const {
|
|
|
|
|
|
|
|
|
|
double_t tmp;
|
|
|
|
|
|
|
|
|
|
switch (_type) {
|
|
|
|
|
case ValueTypeDOUBLE:
|
|
|
|
|
tmp = value;
|
|
|
|
|
store( _out , tmp, options_.check(Options::MSB) );
|
|
|
|
|
break;
|
|
|
|
|
default :
|
|
|
|
|
std::cerr << "unsupported conversion type to float: " << _type << std::endl;
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void _PLYWriter_::writeValue(ValueType _type, std::ostream& _out, signed char value) const{
|
|
|
|
|
|
|
|
|
|
int8_t tmp;
|
|
|
|
|
|
|
|
|
|
switch (_type) {
|
|
|
|
|
case ValueTypeCHAR:
|
|
|
|
|
tmp = value;
|
|
|
|
|
store(_out, tmp, options_.check(Options::MSB) );
|
|
|
|
|
break;
|
|
|
|
|
default :
|
|
|
|
|
std::cerr << "unsupported conversion type to int: " << _type << std::endl;
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
void _PLYWriter_::writeValue(ValueType _type, std::ostream& _out, unsigned char value) const{
|
|
|
|
|
|
|
|
|
|
uint8_t tmp;
|
|
|
|
|
|
|
|
|
|
switch (_type) {
|
|
|
|
|
case ValueTypeUCHAR:
|
|
|
|
|
tmp = value;
|
|
|
|
|
store(_out, tmp, options_.check(Options::MSB) );
|
|
|
|
|
break;
|
|
|
|
|
default :
|
|
|
|
|
std::cerr << "unsupported conversion type to int: " << _type << std::endl;
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
void _PLYWriter_::writeValue(ValueType _type, std::ostream& _out, short value) const{
|
|
|
|
|
|
|
|
|
|
int16_t tmp;
|
|
|
|
|
|
|
|
|
|
switch (_type) {
|
|
|
|
|
case ValueTypeSHORT:
|
|
|
|
|
tmp = value;
|
|
|
|
|
store(_out, tmp, options_.check(Options::MSB) );
|
|
|
|
|
break;
|
|
|
|
|
default :
|
|
|
|
|
std::cerr << "unsupported conversion type to int: " << _type << std::endl;
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
void _PLYWriter_::writeValue(ValueType _type, std::ostream& _out, unsigned short value) const{
|
|
|
|
|
|
|
|
|
|
uint16_t tmp;
|
|
|
|
|
|
|
|
|
|
switch (_type) {
|
|
|
|
|
case ValueTypeUSHORT:
|
|
|
|
|
tmp = value;
|
|
|
|
|
store(_out, tmp, options_.check(Options::MSB) );
|
|
|
|
|
break;
|
|
|
|
|
default :
|
|
|
|
|
std::cerr << "unsupported conversion type to int: " << _type << std::endl;
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2009-06-12 12:46:12 +00:00
|
|
|
bool
|
2009-02-06 13:37:46 +00:00
|
|
|
_PLYWriter_::
|
2009-11-17 13:54:16 +00:00
|
|
|
write_binary(std::ostream& _out, BaseExporter& _be, Options _opt) const
|
2009-02-06 13:37:46 +00:00
|
|
|
{
|
2014-05-09 12:44:18 +00:00
|
|
|
|
2013-08-21 10:00:59 +00:00
|
|
|
unsigned int i, nV, nF;
|
2009-02-06 13:37:46 +00:00
|
|
|
Vec3f v, n;
|
|
|
|
|
Vec2f t;
|
2013-02-26 12:52:19 +00:00
|
|
|
OpenMesh::Vec4uc c;
|
2013-03-01 15:32:46 +00:00
|
|
|
OpenMesh::Vec4f cf;
|
2009-02-06 13:37:46 +00:00
|
|
|
VertexHandle vh;
|
|
|
|
|
std::vector<VertexHandle> vhandles;
|
|
|
|
|
|
2015-06-16 08:52:20 +00:00
|
|
|
// vProps and fProps will be empty, until custom properties are supported by the binary writer
|
|
|
|
|
std::vector<CustomProperty> vProps;
|
|
|
|
|
std::vector<CustomProperty> fProps;
|
|
|
|
|
|
|
|
|
|
write_header(_out, _be, _opt, vProps, fProps);
|
2009-06-12 12:46:12 +00:00
|
|
|
|
2009-02-06 13:37:46 +00:00
|
|
|
// vertex data (point, normals, texcoords)
|
2013-07-23 14:03:45 +00:00
|
|
|
for (i=0, nV=int(_be.n_vertices()); i<nV; ++i)
|
2009-02-06 13:37:46 +00:00
|
|
|
{
|
|
|
|
|
vh = VertexHandle(i);
|
|
|
|
|
v = _be.point(vh);
|
2009-06-12 12:46:12 +00:00
|
|
|
|
2009-02-06 13:37:46 +00:00
|
|
|
//vertex
|
|
|
|
|
writeValue(ValueTypeFLOAT, _out, v[0]);
|
|
|
|
|
writeValue(ValueTypeFLOAT, _out, v[1]);
|
|
|
|
|
writeValue(ValueTypeFLOAT, _out, v[2]);
|
2009-06-12 12:46:12 +00:00
|
|
|
|
2011-12-01 16:55:24 +00:00
|
|
|
// Vertex Normal
|
|
|
|
|
if ( _opt.vertex_has_normal() ){
|
|
|
|
|
n = _be.normal(vh);
|
|
|
|
|
writeValue(ValueTypeFLOAT, _out, n[0]);
|
|
|
|
|
writeValue(ValueTypeFLOAT, _out, n[1]);
|
|
|
|
|
writeValue(ValueTypeFLOAT, _out, n[2]);
|
|
|
|
|
}
|
|
|
|
|
|
2009-06-12 12:46:12 +00:00
|
|
|
// Vertex TexCoords
|
|
|
|
|
if ( _opt.vertex_has_texcoord() ) {
|
|
|
|
|
t = _be.texcoord(vh);
|
|
|
|
|
writeValue(ValueTypeFLOAT, _out, t[0]);
|
|
|
|
|
writeValue(ValueTypeFLOAT, _out, t[1]);
|
|
|
|
|
}
|
|
|
|
|
|
2009-02-06 13:37:46 +00:00
|
|
|
// vertex color
|
|
|
|
|
if ( _opt.vertex_has_color() ) {
|
2013-03-01 15:32:46 +00:00
|
|
|
if ( _opt.color_is_float() ) {
|
|
|
|
|
cf = _be.colorAf(vh);
|
|
|
|
|
writeValue(ValueTypeFLOAT, _out, cf[0]);
|
|
|
|
|
writeValue(ValueTypeFLOAT, _out, cf[1]);
|
|
|
|
|
writeValue(ValueTypeFLOAT, _out, cf[2]);
|
|
|
|
|
|
|
|
|
|
if ( _opt.color_has_alpha() )
|
|
|
|
|
writeValue(ValueTypeFLOAT, _out, cf[3]);
|
|
|
|
|
} else {
|
|
|
|
|
c = _be.colorA(vh);
|
|
|
|
|
writeValue(ValueTypeUCHAR, _out, (int)c[0]);
|
|
|
|
|
writeValue(ValueTypeUCHAR, _out, (int)c[1]);
|
|
|
|
|
writeValue(ValueTypeUCHAR, _out, (int)c[2]);
|
|
|
|
|
|
|
|
|
|
if ( _opt.color_has_alpha() )
|
|
|
|
|
writeValue(ValueTypeUCHAR, _out, (int)c[3]);
|
|
|
|
|
}
|
2009-02-06 13:37:46 +00:00
|
|
|
}
|
|
|
|
|
|
2015-08-28 09:20:26 +00:00
|
|
|
for (std::vector<CustomProperty>::iterator iter = vProps.begin(); iter < vProps.end(); ++iter)
|
|
|
|
|
write_customProp<true>(_out,*iter,i);
|
2009-02-06 13:37:46 +00:00
|
|
|
}
|
2015-08-28 09:20:26 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
for (i=0, nF=int(_be.n_faces()); i<nF; ++i)
|
2009-02-06 13:37:46 +00:00
|
|
|
{
|
2015-08-28 09:20:26 +00:00
|
|
|
//face
|
|
|
|
|
nV = _be.get_vhandles(FaceHandle(i), vhandles);
|
|
|
|
|
writeValue(ValueTypeUINT8, _out, nV);
|
|
|
|
|
for (size_t j=0; j<vhandles.size(); ++j)
|
|
|
|
|
writeValue(ValueTypeINT32, _out, vhandles[j].idx() );
|
|
|
|
|
|
|
|
|
|
for (std::vector<CustomProperty>::iterator iter = fProps.begin(); iter < fProps.end(); ++iter)
|
|
|
|
|
write_customProp<true>(_out,*iter,i);
|
2009-02-06 13:37:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// ----------------------------------------------------------------------------
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
size_t
|
|
|
|
|
_PLYWriter_::
|
|
|
|
|
binary_size(BaseExporter& _be, Options _opt) const
|
|
|
|
|
{
|
|
|
|
|
size_t header(0);
|
|
|
|
|
size_t data(0);
|
|
|
|
|
size_t _3floats(3*sizeof(float));
|
|
|
|
|
size_t _3ui(3*sizeof(unsigned int));
|
|
|
|
|
size_t _4ui(4*sizeof(unsigned int));
|
2009-06-12 12:46:12 +00:00
|
|
|
|
2009-02-06 13:37:46 +00:00
|
|
|
if ( !_opt.is_binary() )
|
|
|
|
|
return 0;
|
|
|
|
|
else
|
|
|
|
|
{
|
2014-05-30 10:15:12 +00:00
|
|
|
|
|
|
|
|
size_t _3longs(3*sizeof(long));
|
|
|
|
|
|
2009-02-06 13:37:46 +00:00
|
|
|
header += 11; // 'OFF BINARY\n'
|
|
|
|
|
header += _3longs; // #V #F #E
|
|
|
|
|
data += _be.n_vertices() * _3floats; // vertex data
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if ( _opt.vertex_has_normal() && _be.has_vertex_normals() )
|
|
|
|
|
{
|
|
|
|
|
header += 1; // N
|
|
|
|
|
data += _be.n_vertices() * _3floats;
|
|
|
|
|
}
|
2009-06-12 12:46:12 +00:00
|
|
|
|
2009-02-06 13:37:46 +00:00
|
|
|
if ( _opt.vertex_has_color() && _be.has_vertex_colors() )
|
|
|
|
|
{
|
|
|
|
|
header += 1; // C
|
|
|
|
|
data += _be.n_vertices() * _3floats;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if ( _opt.vertex_has_texcoord() && _be.has_vertex_texcoords() )
|
|
|
|
|
{
|
|
|
|
|
size_t _2floats(2*sizeof(float));
|
|
|
|
|
header += 2; // ST
|
|
|
|
|
data += _be.n_vertices() * _2floats;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// topology
|
|
|
|
|
if (_be.is_triangle_mesh())
|
|
|
|
|
{
|
|
|
|
|
data += _be.n_faces() * _4ui;
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
2013-08-21 10:00:59 +00:00
|
|
|
unsigned int i, nF;
|
2009-02-06 13:37:46 +00:00
|
|
|
std::vector<VertexHandle> vhandles;
|
|
|
|
|
|
2013-07-23 14:03:45 +00:00
|
|
|
for (i=0, nF=int(_be.n_faces()); i<nF; ++i)
|
2009-02-06 13:37:46 +00:00
|
|
|
{
|
2013-08-21 10:00:59 +00:00
|
|
|
data += _be.get_vhandles(FaceHandle(i), vhandles) * sizeof(unsigned int);
|
2009-02-06 13:37:46 +00:00
|
|
|
|
|
|
|
|
}
|
|
|
|
|
}
|
2009-06-12 12:46:12 +00:00
|
|
|
|
2009-02-06 13:37:46 +00:00
|
|
|
// face colors
|
|
|
|
|
if ( _opt.face_has_color() && _be.has_face_colors() ){
|
|
|
|
|
if ( _opt.color_has_alpha() )
|
|
|
|
|
data += _be.n_faces() * _4ui;
|
|
|
|
|
else
|
|
|
|
|
data += _be.n_faces() * _3ui;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return header+data;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
//=============================================================================
|
|
|
|
|
} // namespace IO
|
|
|
|
|
} // namespace OpenMesh
|
|
|
|
|
//=============================================================================
|