...
 
#include "OM2Format.hh"
namespace OpenMesh {
namespace IO {
namespace OM2Format {
namespace TypeInfo {
enum ElementType { Invalid, Vertex, Halfedge, Edge, Face, Mesh };
struct PropertyDeclaration {
std::string propertyName;
std::string typeName;
uint32_t element = ElementType::Invalid;
size_t store(std::ostream &os) {
return OpenMesh::IO::store(os, propertyName) +
OpenMesh::IO::store(os, typeName) +
OpenMesh::IO::store(os, element);
}
size_t restore(std::istream &is) {
return OpenMesh::IO::restore(is, propertyName) +
OpenMesh::IO::restore(is, typeName) +
OpenMesh::IO::restore(is, element);
}
};
struct PreambleHeader {
uint8_t magic[4] = {'O', 'M', '2', '\0'};
uint32_t version = 1;
int32_t options = 0;
uint32_t num_vertex_properties = 0;
uint32_t num_halfedge_properties = 0;
uint32_t num_edge_properties = 0;
uint32_t num_face_properties = 0;
uint32_t num_mesh_properties = 0;
};
bool checkMagic(const char *first4Bytes) {
return first4Bytes[0] == 'O' && first4Bytes[1] == 'M' &&
first4Bytes[2] == '2' && first4Bytes[3] == '\0';
}
void writePreamble(std::ostream &os, const OpenMesh::BaseKernel &mesh,
OpenMesh::IO::Options opts) {
auto savable = [&](OpenMesh::BaseProperty *prop) {
return (prop && prop->persistent() && !prop->name().empty() &&
prop->name()[1] != ':');
};
PreambleHeader header;
header.options = int(opts);
header.num_vertex_properties =
std::count_if(mesh.vprops_begin(), mesh.vprops_end(), savable);
header.num_halfedge_properties =
std::count_if(mesh.hprops_begin(), mesh.hprops_end(), savable);
header.num_edge_properties =
std::count_if(mesh.eprops_begin(), mesh.eprops_end(), savable);
header.num_face_properties =
std::count_if(mesh.fprops_begin(), mesh.fprops_end(), savable);
header.num_mesh_properties =
std::count_if(mesh.mprops_begin(), mesh.mprops_end(), savable);
os.write(reinterpret_cast<char *>(&header), sizeof(header));
auto const writeChunk = [&](OpenMesh::BaseProperty const &prop,
ElementType type) {
auto propType = get(prop);
if (!propType) {
std::cerr << "Could not store property " << prop.name()
<< " due to unregistered type." << std::endl;
return;
}
PropertyDeclaration decl;
decl.element = type;
decl.propertyName = prop.name();
decl.typeName = propType->name;
decl.store(os);
};
for (auto it = mesh.vprops_begin(); it != mesh.vprops_end(); it++) {
if (!savable(*it)) continue;
writeChunk(**it, ElementType::Vertex);
}
for (auto it = mesh.hprops_begin(); it != mesh.hprops_end(); it++) {
if (!savable(*it)) continue;
writeChunk(**it, ElementType::Halfedge);
}
for (auto it = mesh.eprops_begin(); it != mesh.eprops_end(); it++) {
if (!savable(*it)) continue;
writeChunk(**it, ElementType::Edge);
}
for (auto it = mesh.fprops_begin(); it != mesh.fprops_end(); it++) {
if (!savable(*it)) continue;
writeChunk(**it, ElementType::Face);
}
for (auto it = mesh.mprops_begin(); it != mesh.mprops_end(); it++) {
if (!savable(*it)) continue;
writeChunk(**it, ElementType::Mesh);
}
}
ExtendedOptions readPreamble(std::istream &is,
OpenMesh::BaseKernel &mesh) {
PreambleHeader header;
is.read(reinterpret_cast<char *>(&header), sizeof(header));
if (header.magic[0] != 'O' || header.magic[1] != 'M' ||
header.magic[2] != '2' || header.magic[3] != '\0') {
std::cerr << "File is missing magic number!" << std::endl;
return {0};
}
const auto props_total =
header.num_vertex_properties + header.num_halfedge_properties +
header.num_edge_properties + header.num_face_properties +
header.num_mesh_properties;
for (size_t i = 0; i < props_total; i++) {
PropertyDeclaration decl;
if (!decl.restore(is)) {
std::cerr << "Could not read property declaration!" << std::endl;
return {0};
}
auto type = get(decl.typeName);
switch (decl.element) {
case ElementType::Vertex:
type->addToVertex(decl.propertyName, mesh);
break;
case ElementType::Halfedge:
type->addToHalfedge(decl.propertyName, mesh);
break;
case ElementType::Edge:
type->addToEdge(decl.propertyName, mesh);
break;
case ElementType::Face:
type->addToFace(decl.propertyName, mesh);
break;
case ElementType::Mesh:
type->addToMesh(decl.propertyName, mesh);
break;
default:
std::cerr << "Invalid ElementType!" << decl.element << std::endl;
continue;
}
}
return {header.options};
}
std::vector<std::unique_ptr<BasePropertyType>> sTypes;
} // namespace TypeInfo
BasePropertyType::~BasePropertyType() {}
} // namespace OM2Format
} // namespace IO
} // namespace OpenMesh
This diff is collapsed.
/* ========================================================================= *
* *
* OpenMesh *
* Copyright (c) 2001-2015, RWTH-Aachen University *
* Department of 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$ *
* *
\*===========================================================================*/
//=============================================================================
//
// Helper Functions for binary reading / writing
//
//=============================================================================
//== INCLUDES =================================================================
#include <OpenMesh/Core/IO/OM2Format.hh>
//== NAMESPACES ===============================================================
namespace OpenMesh {
namespace IO {
namespace OMFormat {
//== IMPLEMENTATION ===========================================================
namespace TypeInfo {
template <class MeshItems, class Connectivity>
OpenMesh::IO::Options readPreambleAndBuiltins(
std::istream &is,
OpenMesh::AttribKernelT<MeshItems, Connectivity> &mesh) {
using Opts = OpenMesh::IO::Options;
Opts props = readPreamble(is, mesh);
if (props.vertex_has_normal()) mesh.request_vertex_normals();
if (props.vertex_has_color()) mesh.request_vertex_colors();
return props;
}
template <class MeshItems, class Connectivity>
OpenMesh::IO::Options writePreambleAndBuiltins(
std::ostream &os,
OpenMesh::AttribKernelT<MeshItems, Connectivity> const &mesh) {
using Opts = OpenMesh::IO::Options;
Opts opts;
if (mesh.has_vertex_normals()) opts += Opts::VertexNormal;
if (mesh.has_vertex_colors()) opts += Opts::VertexColor;
if (mesh.has_vertex_texcoords2D()) opts += Opts::VertexTexCoord;
writePreamble(os, mesh, opts);
return opts;
}
}
} // namespace OMFormat
// --------------------------------------------------------------------------
//=============================================================================
} // namespace IO
} // namespace OpenMesh
//=============================================================================
......@@ -194,6 +194,18 @@ public:
// post-processing
virtual void finish() {}
virtual void request_vertex_normals() = 0;
virtual void request_vertex_colors() = 0;
virtual void request_vertex_texcoords1D() = 0;
virtual void request_vertex_texcoords2D() = 0;
virtual void request_vertex_texcoords3D() = 0;
virtual void request_edge_colors() = 0;
virtual void request_face_normals() = 0;
virtual void request_face_colors() = 0;
virtual void request_halfedge_texcoords1D() = 0;
virtual void request_halfedge_texcoords2D() = 0;
virtual void request_halfedge_texcoords3D() = 0;
};
......
......@@ -391,6 +391,17 @@ public:
void finish() { }
void request_vertex_normals() { mesh_.request_vertex_normals(); }
void request_vertex_colors() { mesh_.request_vertex_colors(); }
void request_vertex_texcoords1D() { mesh_.request_vertex_texcoords1D(); }
void request_vertex_texcoords2D() { mesh_.request_vertex_texcoords2D(); }
void request_vertex_texcoords3D() { mesh_.request_vertex_texcoords3D(); }
void request_halfedge_texcoords1D() { mesh_.request_halfedge_texcoords1D(); }
void request_halfedge_texcoords2D() { mesh_.request_halfedge_texcoords2D(); }
void request_halfedge_texcoords3D() { mesh_.request_halfedge_texcoords3D(); }
void request_edge_colors() { mesh_.request_edge_colors(); }
void request_face_normals() { mesh_.request_face_normals(); }
void request_face_colors() { mesh_.request_face_colors(); }
private:
......
......@@ -94,7 +94,12 @@ can_u_read(const std::string& _filename) const
extension.begin(), tolower );
// locate extension in extension string
return (get_extensions().find(extension) != std::string::npos);
std::stringstream ss(get_extensions());
std::string candidate;
while (ss >> candidate)
if (candidate == extension)
return true;
return false;
}
......
/* ========================================================================= *
* *
* OpenMesh *
* Copyright (c) 2001-2015, RWTH-Aachen University *
* Department of 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 =================================================================
// STL
#include <fstream>
#include <istream>
#include <vector>
// OpenMesh
#include <OpenMesh/Core/IO/OM2Format.hh>
#include <OpenMesh/Core/IO/OMFormat.hh>
#include <OpenMesh/Core/IO/reader/OMReader.hh>
#include <OpenMesh/Core/IO/reader/OM2Reader.hh>
#include <OpenMesh/Core/System/config.h>
#include <OpenMesh/Core/System/omstream.hh>
#include <OpenMesh/Core/Utils/Endian.hh>
//=== NAMESPACES ==============================================================
namespace OpenMesh {
namespace IO {
//=== INSTANCIATE =============================================================
// register the OM2Reader singleton with MeshReader
_OM2Reader_ __OM2ReaderInstance;
_OM2Reader_ &OM2Reader() { return __OM2ReaderInstance; }
//=== IMPLEMENTATION ==========================================================
_OM2Reader_::_OM2Reader_() { IOManager().register_module(this); }
//-----------------------------------------------------------------------------
bool _OM2Reader_::read(const std::string &_filename, BaseImporter &_bi,
Options &_opt) {
// check whether importer can give us an OpenMesh BaseKernel
if (!_bi.kernel()) return false;
_opt += Options::Binary; // only binary format supported!
// Open file
std::ifstream ifs(_filename.c_str(), std::ios::binary);
/* Clear formatting flag skipws (Skip whitespaces). If set, operator>> will
* skip bytes set to whitespace chars (e.g. 0x20 bytes) in
* Property<bool>::restore.
*/
ifs.unsetf(std::ios::skipws);
if (!ifs.is_open() || !ifs.good()) {
omerr() << "[OM2Reader] : cannot not open file " << _filename
<< std::endl;
return false;
}
// Pass stream to read method, remember result
bool result = read(ifs, _bi, _opt);
// close input stream
ifs.close();
return result;
}
//-----------------------------------------------------------------------------
bool _OM2Reader_::read(std::istream &_is, BaseImporter &_bi, Options &_opt) {
// check whether importer can give us an OpenMesh BaseKernel
if (!_bi.kernel()) return false;
_opt += Options::Binary; // only binary format supported!
if (!_is.good()) {
omerr() << "[OMReader] : cannot read from stream " << std::endl;
return false;
}
// Pass stream to read method, remember result
bool result = read_binary(_is, _bi, _opt);
if (result) _opt += Options::Binary;
return result;
}
//-----------------------------------------------------------------------------
bool _OM2Reader_::read_ascii(std::istream & /* _is */, BaseImporter & /* _bi */,
Options & /* _opt */) const {
// not supported yet!
return false;
}
//-----------------------------------------------------------------------------
bool _OM2Reader_::read_binary(std::istream &_is, BaseImporter &_bi,
Options &_opt) const {
using Opts = OpenMesh::IO::Options;
OM2Format::TypeInfo::ExtendedOptions eopts = OM2Format::TypeInfo::readPreamble(_is, *_bi.kernel());
if (eopts.has_halfedge_texcoords1D()) _bi.request_halfedge_texcoords1D();
if (eopts.has_halfedge_texcoords2D()) _bi.request_halfedge_texcoords2D();
if (eopts.has_halfedge_texcoords3D()) _bi.request_halfedge_texcoords3D();
if (eopts.has_vertex_texcoords1D()) _bi.request_vertex_texcoords1D();
if (eopts.has_vertex_texcoords2D()) _bi.request_vertex_texcoords2D();
if (eopts.has_vertex_texcoords3D()) _bi.request_vertex_texcoords3D();
_opt = eopts.om();
if (_opt.vertex_has_normal() ) _bi.request_vertex_normals();
if (_opt.vertex_has_color() ) _bi.request_vertex_colors();
if (_opt.edge_has_color() ) _bi.request_edge_colors();
if (_opt.face_has_normal() ) _bi.request_face_normals();
if (_opt.face_has_color() ) _bi.request_face_colors();
return __OMReaderInstance.read(_is, _bi, _opt);
}
//-----------------------------------------------------------------------------
bool _OM2Reader_::can_u_read(const std::string &_filename) const {
// !!! Assuming BaseReader::can_u_parse( std::string& )
// does not call BaseReader::read_magic()!!!
if (this->BaseReader::can_u_read(_filename)) {
std::ifstream ifile(_filename.c_str());
if (ifile && can_u_read(ifile)) return true;
}
return false;
}
//-----------------------------------------------------------------------------
bool _OM2Reader_::can_u_read(std::istream &_is) const {
char tip[4];
_is.read(tip, 4);
for (char c : tip) _is.putback(c);
return OM2Format::TypeInfo::checkMagic(tip);
}
//-----------------------------------------------------------------------------
//=============================================================================
} // namespace IO
} // namespace OpenMesh
//=============================================================================
/* ========================================================================= *
* *
* OpenMesh *
* Copyright (c) 2001-2015, RWTH-Aachen University *
* Department of 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$ *
* *
\*===========================================================================*/
//=============================================================================
//
// Implements a reader module for OM2 (aka PROM) files
//
//=============================================================================
#ifndef __OM2READER_HH__
#define __OM2READER_HH__
//=== INCLUDES ================================================================
// OpenMesh
#include <OpenMesh/Core/System/config.h>
#include <OpenMesh/Core/Utils/SingletonT.hh>
#include <OpenMesh/Core/IO/OMFormat.hh>
#include <OpenMesh/Core/IO/IOManager.hh>
#include <OpenMesh/Core/IO/importer/BaseImporter.hh>
#include <OpenMesh/Core/IO/reader/BaseReader.hh>
// STD C++
#include <iosfwd>
#include <string>
//== NAMESPACES ===============================================================
namespace OpenMesh {
namespace IO {
//== IMPLEMENTATION ===========================================================
/**
Implementation of the OM format reader. This class is singleton'ed by
SingletonT to OMReader.
*/
class OPENMESHDLLEXPORT _OM2Reader_ : public BaseReader
{
public:
_OM2Reader_();
virtual ~_OM2Reader_() { }
std::string get_description() const { return "OpenMesh2 File Format"; }
std::string get_extensions() const { return "om2"; }
std::string get_magic() const { return "OM2"; }
bool read(const std::string& _filename,
BaseImporter& _bi,
Options& _opt );
//! Stream Reader for std::istream input in binary format
bool read(std::istream& _is,
BaseImporter& _bi,
Options& _opt );
virtual bool can_u_read(const std::string& _filename) const;
virtual bool can_u_read(std::istream& _is) const;
bool read_ascii(std::istream& _is, BaseImporter& _bi, Options& _opt) const;
bool read_binary(std::istream& _is, BaseImporter& _bi, Options& _opt) const;
private:
};
//== TYPE DEFINITION ==========================================================
/// Declare the single entity of the OM reader.
extern _OM2Reader_ __OM2ReaderInstance;
OPENMESHDLLEXPORT _OM2Reader_& OM2Reader();
//=============================================================================
} // namespace IO
} // namespace OpenMesh
//=============================================================================
#endif
//=============================================================================
/* ========================================================================= *
* *
* OpenMesh *
* Copyright (c) 2001-2015, RWTH-Aachen University *
* Department of 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 <OpenMesh/Core/System/config.h>
// -------------------- STL
#if defined( OM_CC_MIPS )
#include <time.h>
#include <string.h>
#else
#include <ctime>
#include <cstring>
#endif
#include <fstream>
#include <vector>
// -------------------- OpenMesh
#include <OpenMesh/Core/IO/OMFormat.hh>
#include <OpenMesh/Core/IO/exporter/BaseExporter.hh>
#include <OpenMesh/Core/IO/writer/OM2Writer.hh>
#include <OpenMesh/Core/IO/writer/OMWriter.hh>
//=== NAMESPACES ==============================================================
namespace OpenMesh {
namespace IO {
//=== INSTANCIATE =============================================================
// register the OMLoader singleton with MeshLoader
_OM2Writer_ __OM2WriterInstance;
_OM2Writer_& OM2Writer() { return __OM2WriterInstance; }
//=== IMPLEMENTATION ==========================================================
_OM2Writer_::
_OM2Writer_()
{
IOManager().register_module(this);
}
bool
_OM2Writer_::write(const std::string& _filename, BaseExporter& _be,
Options _opt, std::streamsize /*_precision*/) const
{
// check whether exporter can give us an OpenMesh BaseKernel
if (!_be.kernel()) return false;
_opt += Options::Binary; // only binary format supported
std::ofstream ofs(_filename.c_str(), std::ios::binary);
// check if file is open
if (!ofs.is_open())
{
omerr() << "[OMWriter] : cannot open file " << _filename << std::endl;
return false;
}
// call stream save method
bool rc = write(ofs, _be, _opt);
// close filestream
ofs.close();
// return success/failure notice
return rc;
}
//-----------------------------------------------------------------------------
bool
_OM2Writer_::write(std::ostream& _os, BaseExporter& _be, Options _opt, std::streamsize /*_precision*/) const
{
// std::clog << "[OMWriter]::write( stream )\n";
// check exporter features
if ( !check( _be, _opt ) )
{
omerr() << "[OMWriter]: exporter does not support wanted feature!\n";
return false;
}
// Maybe an ascii version will be implemented in the future.
// For now, support only a binary format
if ( !_opt.check( Options::Binary ) )
_opt += Options::Binary;
// Ignore LSB/MSB bit. Always store in LSB (little endian)
_opt += Options::LSB;
_opt -= Options::MSB;
return write_binary(_os, _be, _opt);
}
//-----------------------------------------------------------------------------
bool _OM2Writer_::write_binary(std::ostream& _os, BaseExporter& _be,
Options _opt) const
{
using Opts = OpenMesh::IO::Options;
if (_be.has_vertex_normals() ) _opt += Opts::VertexNormal;
if (_be.has_vertex_colors() ) _opt += Opts::VertexColor;
if (_be.has_vertex_texcoords() ) _opt += Opts::VertexTexCoord;
if (_be.has_edge_colors() ) _opt += Opts::EdgeColor;
if (_be.has_face_normals() ) _opt += Opts::FaceNormal;
if (_be.has_face_colors() ) _opt += Opts::FaceColor;
OM2Format::TypeInfo::writePreamble(_os, *_be.kernel(), _opt);
return __OMWriterInstance.write(_os, _be, _opt);
}
// ----------------------------------------------------------------------------
size_t _OM2Writer_::binary_size(BaseExporter& /* _be */, Options /* _opt */) const
{
// !!!TODO!!!
return -1;
}
//=============================================================================
} // namespace IO
} // namespace OpenMesh
//=============================================================================
/* ========================================================================= *
* *
* OpenMesh *
* Copyright (c) 2001-2015, RWTH-Aachen University *
* Department of 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$ *
* *
\*===========================================================================*/
//=============================================================================
//
// Implements a writer module for OM files
//
//=============================================================================
#ifndef __OM2WRITER_HH__
#define __OM2WRITER_HH__
//=== INCLUDES ================================================================
// STD C++
#include <iosfwd>
#include <string>
// OpenMesh
#include <OpenMesh/Core/IO/BinaryHelper.hh>
#include <OpenMesh/Core/System/config.h>
#include <OpenMesh/Core/Utils/SingletonT.hh>
#include <OpenMesh/Core/IO/OMFormat.hh>
#include <OpenMesh/Core/IO/OM2Format.hh>
#include <OpenMesh/Core/IO/IOManager.hh>
#include <OpenMesh/Core/IO/writer/BaseWriter.hh>
//== NAMESPACES ===============================================================
namespace OpenMesh {
namespace IO {
//=== FORWARDS ================================================================
class BaseExporter;
//=== IMPLEMENTATION ==========================================================
/**
* Implementation of the OM format writer. This class is singleton'ed by
* SingletonT to OMWriter.
*/
class OPENMESHDLLEXPORT _OM2Writer_ : public BaseWriter
{
public:
/// Constructor
_OM2Writer_();
/// Destructor
virtual ~_OM2Writer_() {};
std::string get_description() const
{ return "OpenMesh2 Format"; }
std::string get_extensions() const
{ return "om2"; }
bool write(std::ostream&, BaseExporter&, Options, std::streamsize _precision = 6) const;
size_t binary_size(BaseExporter& _be, Options _opt) const;
protected:
bool write(const std::string&, BaseExporter&, Options, std::streamsize _precision = 6) const;
bool write_binary(std::ostream&, BaseExporter&, Options) const;
size_t store_binary_custom_chunk( std::ostream&, const BaseProperty&,
OMFormat::Chunk::Entity, bool) const;
};
//== TYPE DEFINITION ==========================================================
/// Declare the single entity of the OM writer.
extern _OM2Writer_ __OM2WriterInstance;
OPENMESHDLLEXPORT _OM2Writer_& OM2Writer();
//=============================================================================
} // namespace IO
} // namespace OpenMesh
//=============================================================================
#endif
//=============================================================================