Loading...
Searching...
No Matches
SR_binary_spec.hh
/* ========================================================================= *
* *
* OpenMesh *
* Copyright (c) 2001-2025, 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. *
* *
* ========================================================================= */
//=============================================================================
//
// Helper Functions for binary reading / writing
//
//=============================================================================
#ifndef OPENMESH_SR_BINARY_SPEC_HH
#define OPENMESH_SR_BINARY_SPEC_HH
//== INCLUDES =================================================================
#include <OpenMesh/Core/System/config.h>
// -------------------- STL
#include <iterator>
#include <string>
#if defined(OM_CC_GCC) && (OM_CC_VERSION < 30000)
# include <OpenMesh/Tools/Utils/NumLimitsT.hh>
#else
# include <limits>
#endif
#include <vector>
#include <stdexcept> // logic_error
#include <numeric> // accumulate
// -------------------- OpenMesh
#include <OpenMesh/Core/Geometry/VectorT.hh>
#include <OpenMesh/Core/Mesh/Status.hh>
#include <OpenMesh/Core/IO/SR_types.hh>
#include <OpenMesh/Core/IO/SR_rbo.hh>
#include <OpenMesh/Core/IO/SR_binary.hh>
#include <OpenMesh/Core/Utils/typename.hh>
#include <iostream>
//== NAMESPACES ===============================================================
namespace OpenMesh {
namespace IO {
//=============================================================================
#ifndef DOXY_IGNORE_THIS
//-----------------------------------------------------------------------------
// struct binary, helper for storing/restoring
#define SIMPLE_BINARY( T ) \
template <> struct binary< T > { \
typedef T value_type; \
static const bool is_streamable = true; \
static size_t size_of(const value_type&) { return sizeof(value_type); } \
static size_t size_of(void) { return sizeof(value_type); } \
static std::string type_identifier(void) { return #T; } \
static size_t store( std::ostream& _os, const value_type& _val, \
bool _swap=false) { \
value_type tmp = _val; \
if (_swap) reverse_byte_order(tmp); \
_os.write( (const char*)&tmp, sizeof(value_type) ); \
return _os.good() ? sizeof(value_type) : 0; \
} \
\
static size_t restore( std::istream& _is, value_type& _val, \
bool _swap=false) { \
_is.read( (char*)&_val, sizeof(value_type) ); \
if (_swap) reverse_byte_order(_val); \
return _is.good() ? sizeof(value_type) : 0; \
} \
}
SIMPLE_BINARY(bool);
//SIMPLE_BINARY(int);
// Why is this needed? Should not be used as not 32 bit compatible
//SIMPLE_BINARY(unsigned long);
SIMPLE_BINARY(float);
SIMPLE_BINARY(double);
SIMPLE_BINARY(long double);
SIMPLE_BINARY(char);
SIMPLE_BINARY(int8_t);
SIMPLE_BINARY(int16_t);
SIMPLE_BINARY(int32_t);
//SIMPLE_BINARY(int64_t); // TODO: This does not work. Find out why.
SIMPLE_BINARY(uint8_t);
SIMPLE_BINARY(uint16_t);
SIMPLE_BINARY(uint32_t);
SIMPLE_BINARY(uint64_t);
//handles
SIMPLE_BINARY(FaceHandle);
SIMPLE_BINARY(EdgeHandle);
SIMPLE_BINARY(HalfedgeHandle);
SIMPLE_BINARY(VertexHandle);
SIMPLE_BINARY(MeshHandle);
#undef SIMPLE_BINARY
// For unsigned long which is of size 64 bit on 64 bit
// architectures: convert into 32 bit unsigned integer value
// in order to stay compatible between 32/64 bit architectures.
// This allows cross reading BUT forbids storing unsigned longs
// as data type since higher order word (4 bytes) will be truncated.
// Does not work in case the data type that is to be stored
// exceeds the value range of unsigned int in size, which is improbable...
#define SIMPLE_BINARY( T ) \
template <> struct binary< T > { \
typedef T value_type; \
static const bool is_streamable = true; \
static size_t size_of(const value_type&) { return sizeof(value_type); } \
static size_t size_of(void) { return sizeof(value_type); } \
static std::string type_identifier(void) { return #T; } \
static size_t store( std::ostream& _os, const value_type& _val, \
bool _swap=false) { \
value_type tmp = _val; \
if (_swap) reverse_byte_order(tmp); \
/* Convert unsigned long to unsigned int for compatibility reasons */ \
unsigned int t1 = static_cast<unsigned int>(tmp); \
_os.write( (const char*)&t1, sizeof(unsigned int) ); \
return _os.good() ? sizeof(unsigned int) : 0; \
} \
\
static size_t restore( std::istream& _is, value_type& _val, \
bool _swap=false) { \
unsigned int t1; \
_is.read( (char*)&t1, sizeof(unsigned int) ); \
_val = t1; \
if (_swap) reverse_byte_order(_val); \
return _is.good() ? sizeof(unsigned int) : 0; \
} \
}
SIMPLE_BINARY(unsigned long);
#undef SIMPLE_BINARY
#define VECTORT_BINARY( T ) \
template <> struct binary< T > { \
typedef T value_type; \
static const bool is_streamable = true; \
static size_t size_of(void) { return sizeof(value_type); } \
static size_t size_of(const value_type&) { return size_of(); } \
static std::string type_identifier(void) { return #T; } \
static size_t store( std::ostream& _os, const value_type& _val, \
bool _swap=false) { \
value_type tmp = _val; \
size_t b = size_of(_val), N = value_type::size_; \
if (_swap) \
for (size_t i=0; i<N; ++i) \
reverse_byte_order( tmp[i] ); \
_os.write( (const char*)&tmp[0], b ); \
return _os.good() ? b : 0; \
} \
\
static size_t restore( std::istream& _is, value_type& _val, \
bool _swap=false) { \
size_t N=value_type::size_; \
size_t b = N * sizeof(value_type::value_type); \
_is.read( (char*)&_val[0], b ); \
if (_swap) for (size_t i=0; i<N; ++i) \
reverse_byte_order( _val[i] ); \
return _is.good() ? b : 0; \
} \
}
#define VECTORTS_BINARY( N ) \
VECTORT_BINARY( Vec##N##c ); \
VECTORT_BINARY( Vec##N##uc ); \
VECTORT_BINARY( Vec##N##s ); \
VECTORT_BINARY( Vec##N##us ); \
VECTORT_BINARY( Vec##N##i ); \
VECTORT_BINARY( Vec##N##ui ); \
VECTORT_BINARY( Vec##N##f ); \
VECTORT_BINARY( Vec##N##d );
VECTORTS_BINARY( 1 )
VECTORTS_BINARY( 2 )
VECTORTS_BINARY( 3 )
VECTORTS_BINARY( 4 )
VECTORTS_BINARY( 5 )
VECTORTS_BINARY( 6 )
#undef VECTORTS_BINARY
#undef VECTORT_BINARY
template <> struct binary< std::string > {
typedef std::string value_type;
typedef uint16_t length_t;
static const bool is_streamable = true;
static size_t size_of() { return UnknownSize; }
static size_t size_of(const value_type &_v)
{ return sizeof(length_t) + _v.size(); }
static std::string type_identifier(void) { return "std::string"; }
static
size_t store(std::ostream& _os, const value_type& _v, bool _swap=false)
{
#if defined(OM_CC_GCC) && (OM_CC_VERSION < 30000)
if (_v.size() < Utils::NumLimitsT<length_t>::max() )
#else
if (_v.size() < std::numeric_limits<length_t>::max() )
#endif
{
length_t len = length_t(_v.size());
size_t bytes = binary<length_t>::store( _os, len, _swap );
_os.write( _v.data(), len );
return _os.good() ? len+bytes : 0;
}
throw std::runtime_error("Cannot store string longer than 64Kb");
}
static
size_t restore(std::istream& _is, value_type& _val, bool _swap=false)
{
length_t len;
size_t bytes = binary<length_t>::restore( _is, len, _swap );
_val.resize(len);
_is.read( const_cast<char*>(_val.data()), len );
return _is.good() ? (len+bytes) : 0;
}
};
template <> struct binary<OpenMesh::Attributes::StatusInfo>
{
typedef OpenMesh::Attributes::StatusInfo value_type;
typedef value_type::value_type status_t;
static const bool is_streamable = true;
static size_t size_of() { return sizeof(status_t); }
static size_t size_of(const value_type&) { return size_of(); }
static std::string type_identifier(void) { return "StatusInfo";}
static size_t n_bytes(size_t _n_elem)
{ return _n_elem*sizeof(status_t); }
static
size_t store(std::ostream& _os, const value_type& _v, bool _swap=false)
{
status_t v=_v.bits();
return binary<status_t>::store(_os, v, _swap);
}
static
size_t restore( std::istream& _os, value_type& _v, bool _swap=false)
{
status_t v;
size_t b = binary<status_t>::restore(_os, v, _swap);
_v.set_bits(v);
return b;
}
};
//-----------------------------------------------------------------------------
// std::vector<T> specializations for struct binary<>
template <typename T>
struct FunctorStore {
FunctorStore( std::ostream& _os, bool _swap) : os_(_os), swap_(_swap) { }
size_t operator () ( size_t _v1, const T& _s2 )
{ return _v1+binary<T>::store(os_, _s2, swap_ ); }
std::ostream& os_;
bool swap_;
};
template <typename T>
struct FunctorRestore {
FunctorRestore( std::istream& _is, bool _swap) : is_(_is), swap_(_swap) { }
size_t operator () ( size_t _v1, T& _s2 )
{ return _v1+binary<T>::restore(is_, _s2, swap_ ); }
std::istream& is_;
bool swap_;
};
template <typename T>
struct binary< std::vector< T >, typename std::enable_if<std::is_default_constructible<T>::value>::type > {
typedef std::vector< T > value_type;
typedef typename value_type::value_type elem_type;
static const bool is_streamable = binary<T>::is_streamable;
static size_t size_of(bool /*_store_size*/ = true)
{ return IO::UnknownSize; }
static size_t size_of(const value_type& _v, bool _store_size = true)
{
if(binary<T>::size_of() != IO::UnknownSize)
{
unsigned int N = static_cast<unsigned int>(_v.size());
auto res = binary<T>::size_of()*_v.size() + (_store_size? sizeof(decltype(N)) : 0);
return res;
}
else
{
size_t size = 0;
for(auto v : _v)
size += binary<T>::size_of(v);
if(_store_size)
size += binary<unsigned int>::size_of();
return size;
}
}
static std::string type_identifier(void) { return "std::vector<" + binary<T>::type_identifier() + ">"; }
static
size_t store(std::ostream& _os, const value_type& _v, bool _swap=false, bool _store_size = true) {
size_t bytes=0;
if(_store_size)
{
unsigned int N = static_cast<unsigned int>(_v.size());
bytes += binary<unsigned int>::store( _os, N, _swap );
}
if (_swap)
bytes += std::accumulate( _v.begin(), _v.end(), static_cast<size_t>(0),
FunctorStore<elem_type>(_os,_swap) );
else
{
auto elem_size = binary<elem_type>::size_of();
if (elem_size != IO::UnknownSize && elem_size == sizeof(elem_type))
{
// size of all elements is known, equal, and densely packed in vector.
// Just store vector data
auto bytes_of_vec = size_of(_v, false);
bytes += bytes_of_vec;
if (_v.size() > 0)
_os.write( reinterpret_cast<const char*>(&_v[0]), bytes_of_vec);
}
else
{
// store individual elements
for (const auto& v : _v)
bytes += binary<elem_type>::store(_os, v, _swap);
}
}
return _os.good() ? bytes : 0;
}
static size_t restore(std::istream& _is, value_type& _v, bool _swap=false, bool _restore_size = true) {
size_t bytes=0;
if(_restore_size)
{
unsigned int size_of_vec;
bytes += binary<unsigned int>::restore(_is, size_of_vec, _swap);
_v.resize(size_of_vec);
}
if ( _swap)
bytes += std::accumulate( _v.begin(), _v.end(), size_t(0),
FunctorRestore<elem_type>(_is, _swap) );
else
{
auto elem_size = binary<elem_type>::size_of();
if (elem_size != IO::UnknownSize && elem_size == sizeof(elem_type))
{
// size of all elements is known, equal, and densely packed in vector.
// Just restore vector data
auto bytes_of_vec = size_of(_v, false);
bytes += bytes_of_vec;
if (_v.size() > 0)
_is.read( reinterpret_cast<char*>(&_v[0]), bytes_of_vec );
}
else
{
// restore individual elements
for (auto& v : _v)
bytes += binary<elem_type>::restore(_is, v, _swap);
}
}
return _is.good() ? bytes : 0;
}
};
#include <OpenMesh/Core/IO/SR_binary_vector_of_bool.hh>
// ----------------------------------------------------------------------------
#endif // DOXY_IGNORE_THIS
//=============================================================================
} // namespace IO
} // namespace OpenMesh
//=============================================================================
#endif // OPENMESH_SR_BINARY_SPEC_HH defined
//=============================================================================
NumLimitsT.hh
Temporary solution until std::numeric_limits is standard.
OpenMesh
Contains all the mesh ingredients like the polygonal mesh, the triangle mesh, different mesh kernels ...
Definition MeshItems.hh:59
OpenMesh::IO
This namespace contains functions for reading and writing polygonal meshes and a list of supported fi...
OpenMesh::IO::int8_t
signed char int8_t
Binary read a short from _is and perform byte swapping if _swap is true.
Definition SR_types.hh:80
OpenMesh::IO::int16_t
short int16_t
Binary read a short from _is and perform byte swapping if _swap is true.
Definition SR_types.hh:81
OpenMesh::IO::uint64_t
unsigned long long uint64_t
Binary read a short from _is and perform byte swapping if _swap is true.
Definition SR_types.hh:89
OpenMesh::IO::uint32_t
unsigned int uint32_t
Binary read a short from _is and perform byte swapping if _swap is true.
Definition SR_types.hh:85
OpenMesh::IO::uint16_t
unsigned short uint16_t
Binary read a short from _is and perform byte swapping if _swap is true.
Definition SR_types.hh:81
OpenMesh::IO::int32_t
int int32_t
Binary read a short from _is and perform byte swapping if _swap is true.
Definition SR_types.hh:85
OpenMesh::IO::uint8_t
unsigned char uint8_t
Binary read a short from _is and perform byte swapping if _swap is true.
Definition SR_types.hh:80
OpenMesh::IO::binary
The struct defines how to store and restore the type T.
Definition SR_binary.hh:97
OpenMesh::IO::binary::restore
static size_t restore(std::istream &, value_type &, bool=false, bool=true)
Restore a value of T and return the number of bytes read.
Definition SR_binary.hh:125
OpenMesh::IO::binary::is_streamable
static const bool is_streamable
Can we store T? Set this to true in your specialization.
Definition SR_binary.hh:101
OpenMesh::IO::binary::store
static size_t store(std::ostream &, const value_type &, bool=false, bool=true)
Store a value of T and return the number of bytes written.
Definition SR_binary.hh:113
OpenMesh::IO::binary::type_identifier
static std::string type_identifier(void)
A string that identifies the type of T.
Definition SR_binary.hh:109
OpenMesh::IO::binary::size_of
static size_t size_of(void)
What's the size of T? If it depends on the actual value (e.g. for vectors) return UnknownSize.
Definition SR_binary.hh:104
OpenMesh::Utils::NumLimitsT::max
static Scalar max()
Return the maximum absolte value a scalar type can store.
Definition NumLimitsT.hh:97