45 #ifndef OPENMESH_IO_OMFORMAT_HH
46 #define OPENMESH_IO_OMFORMAT_HH
51 #include <OpenMesh/Core/System/config.h>
53 #include <OpenMesh/Core/IO/SR_store.hh>
54 #include <OpenMesh/Core/Utils/GenProg.hh>
55 #include <OpenMesh/Core/Utils/Endian.hh>
56 #include <OpenMesh/Core/Utils/vector_traits.hh>
59 #if defined(OM_CC_GCC) && (OM_GCC_VERSION < 30000)
61 # define OM_MISSING_HEADER_LIMITS 1
69 #ifndef DOXY_IGNORE_THIS
111 typedef unsigned char uchar;
132 size_t store( std::ostream& _os,
bool _swap )
const
134 _os.write( (
char*)
this, 4);
142 size_t restore( std::istream& _is,
bool _swap )
144 if (_is.read( (
char*)
this, 4 ).eof())
160 typedef uint32 esize_t;
165 Type_Texcoord = 0x02,
173 Entity_Vertex = 0x00,
177 Entity_Halfedge = 0x06,
178 Entity_Sentinel = 0x07
205 static const int SIZE_RESERVED = 1;
206 static const int SIZE_NAME = 1;
207 static const int SIZE_ENTITY = 3;
208 static const int SIZE_TYPE = 4;
210 static const int SIZE_SIGNED = 1;
211 static const int SIZE_FLOAT = 1;
212 static const int SIZE_DIM = 3;
213 static const int SIZE_BITS = 2;
215 static const int OFF_RESERVED = 0;
216 static const int OFF_NAME = SIZE_RESERVED + OFF_RESERVED;
217 static const int OFF_ENTITY = SIZE_NAME + OFF_NAME;
218 static const int OFF_TYPE = SIZE_ENTITY + OFF_ENTITY;
219 static const int OFF_SIGNED = SIZE_TYPE + OFF_TYPE;
220 static const int OFF_FLOAT = SIZE_SIGNED + OFF_SIGNED;
221 static const int OFF_DIM = SIZE_FLOAT + OFF_FLOAT;
222 static const int OFF_BITS = SIZE_DIM + OFF_DIM;
232 unsigned reserved_: SIZE_RESERVED;
233 unsigned name_ : SIZE_NAME;
234 unsigned entity_ : SIZE_ENTITY;
236 unsigned type_ : SIZE_TYPE;
238 unsigned signed_ : SIZE_SIGNED;
239 unsigned float_ : SIZE_FLOAT;
240 unsigned dim_ : SIZE_DIM;
241 unsigned bits_ : SIZE_BITS;
243 unsigned unused_ : 16;
247 class PropertyName :
public std::string
251 static const size_t size_max = 256;
255 explicit PropertyName(
const std::string& _name ) { *
this = _name; }
257 bool is_valid()
const {
return is_valid( size() ); }
259 static bool is_valid(
size_t _s ) {
return _s <= size_max; }
261 PropertyName& operator = (
const std::string& _rhs )
263 assert( is_valid( _rhs.size() ) );
265 if ( is_valid( _rhs.size() ) )
266 std::string::operator = ( _rhs );
269 omerr() <<
"Warning! Property name too long. Will be shortened!\n";
270 this->std::string::operator = ( _rhs.substr(0, size_max) );
284 inline size_t header_size(
void) {
return sizeof(Header); }
288 inline size_t chunk_header_size(
void ) {
return sizeof(uint16); }
292 inline size_t scalar_size(
const Chunk::Header& _hdr )
294 return _hdr.float_ ? (0x01 << _hdr.bits_) : (0x04 << _hdr.bits_);
299 inline size_t dimensions(
const Chunk::Header& _chdr) {
return _chdr.dim_+1; }
303 inline size_t vector_size(
const Chunk::Header& _chdr )
305 return dimensions(_chdr)*scalar_size(_chdr);
310 inline size_t chunk_data_size( Header& _hdr, Chunk::Header& _chunk_hdr )
313 switch( _chunk_hdr.entity_ )
315 case Chunk::Entity_Vertex: C = _hdr.n_vertices_;
break;
316 case Chunk::Entity_Face: C = _hdr.n_faces_;
break;
317 case Chunk::Entity_Halfedge: C = _hdr.n_edges_*2;
break;
318 case Chunk::Entity_Edge: C = _hdr.n_edges_;
break;
319 case Chunk::Entity_Mesh: C = 1;
break;
322 std::cerr <<
"Invalid value in _chunk_hdr.entity_\n";
327 return C * vector_size( _chunk_hdr );
330 inline size_t chunk_size( Header& _hdr, Chunk::Header& _chunk_hdr )
332 return chunk_header_size() + chunk_data_size( _hdr, _chunk_hdr );
337 uint16&
operator << (uint16& val,
const Chunk::Header& hdr);
338 Chunk::Header&
operator << (Chunk::Header& hdr,
const uint16 val);
343 template <
typename T>
bool is_float(
const T&)
345 #if defined(OM_MISSING_HEADER_LIMITS)
346 return !Utils::NumLimitsT<T>::is_integer();
348 return !std::numeric_limits<T>::is_integer;
352 template <
typename T>
bool is_double(
const T&)
357 template <>
inline bool is_double(
const double&)
362 template <
typename T>
bool is_integer(
const T)
364 #if defined(OM_MISSING_HEADER_LIMITS)
365 return Utils::NumLimitsT<T>::is_integer();
367 return std::numeric_limits<T>::is_integer;
371 template <
typename T>
bool is_signed(
const T&)
373 #if defined(OM_MISSING_HEADER_LIMITS)
374 return Utils::NumLimitsT<T>::is_signed();
376 return std::numeric_limits<T>::is_signed;
382 template <
typename VecType>
384 Chunk::Dim dim( VecType )
390 template <
typename VecType>
392 Chunk::Dim dim(
const Chunk::Header& _hdr )
394 return static_cast<Chunk::Dim
>( _hdr.dim_ );
398 Chunk::Integer_Size needed_bits(
size_t s );
402 template <
typename T> Chunk::Integer_Size integer_size(
const T&)
404 template <
typename T> Chunk::Integer_Size integer_size(
const T& d)
408 assert( is_integer(d) );
413 case 1:
return OMFormat::Chunk::Integer_8;
414 case 2:
return OMFormat::Chunk::Integer_16;
415 case 4:
return OMFormat::Chunk::Integer_32;
416 case 8:
return OMFormat::Chunk::Integer_64;
418 std::cerr <<
"Invalid value in integer_size\n";
422 return Chunk::Integer_Size(0);
428 template <
typename T> Chunk::Float_Size float_size(
const T&)
430 template <
typename T> Chunk::Float_Size float_size(
const T& d)
434 assert( is_float(d) );
439 case 4:
return OMFormat::Chunk::Float_32;
440 case 8:
return OMFormat::Chunk::Float_64;
441 case 16:
return OMFormat::Chunk::Float_128;
443 std::cerr <<
"Invalid value in float_size\n";
447 return Chunk::Float_Size(0);
451 template <
typename T>
453 unsigned int bits(
const T& val)
455 return is_integer(val)
456 ? (
static_cast<unsigned int>(integer_size(val)))
457 : (
static_cast<unsigned int>(float_size(val)));
462 inline uint8 mk_version(
const uint16 major,
const uint16 minor)
463 {
return (major & 0x07) << 5 | (minor & 0x1f); }
466 inline uint16 major_version(
const uint8 version)
467 {
return (version >> 5) & 0x07; }
470 inline uint16 minor_version(
const uint8 version)
471 {
return (version & 0x001f); }
476 std::string as_string(uint8 version);
478 const char *as_string(Chunk::Type t);
479 const char *as_string(Chunk::Entity e);
480 const char *as_string(Chunk::Dim d);
481 const char *as_string(Chunk::Integer_Size d);
482 const char *as_string(Chunk::Float_Size d);
484 std::ostream&
operator << ( std::ostream& _os,
const Header& _h );
485 std::ostream&
operator << ( std::ostream& _os,
const Chunk::Header& _c );
493 size_t store( std::ostream& _os,
const OMFormat::Header& _hdr,
bool _swap)
494 {
return _hdr.store( _os, _swap ); }
497 size_t restore( std::istream& _is, OMFormat::Header& _hdr,
bool _swap )
498 {
return _hdr.restore( _is, _swap ); }
505 store( std::ostream& _os,
const OMFormat::Chunk::Header& _hdr,
bool _swap)
507 OMFormat::uint16 val; val << _hdr;
513 restore( std::istream& _is, OMFormat::Chunk::Header& _hdr,
bool _swap )
515 OMFormat::uint16 val;
525 typedef GenProg::TrueType t_signed;
526 typedef GenProg::FalseType t_unsigned;
529 template<
typename T >
531 store( std::ostream& _os,
533 OMFormat::Chunk::Integer_Size _b,
538 template<
typename T >
540 store( std::ostream& _os,
542 OMFormat::Chunk::Integer_Size _b,
547 template<
typename T >
550 store( std::ostream& _os,
552 OMFormat::Chunk::Integer_Size _b,
555 assert( OMFormat::is_integer( _val ) );
557 if ( OMFormat::is_signed( _val ) )
558 return store( _os, _val, _b, _swap, t_signed() );
559 return store( _os, _val, _b, _swap, t_unsigned() );
563 template<
typename T >
inline
564 size_t restore( std::istream& _is,
566 OMFormat::Chunk::Integer_Size _b,
571 template<
typename T >
inline
572 size_t restore( std::istream& _is,
574 OMFormat::Chunk::Integer_Size _b,
579 template<
typename T >
582 restore( std::istream& _is,
584 OMFormat::Chunk::Integer_Size _b,
587 assert( OMFormat::is_integer( _val ) );
589 if ( OMFormat::is_signed( _val ) )
590 return restore( _is, _val, _b, _swap, t_signed() );
591 return restore( _is, _val, _b, _swap, t_unsigned() );
597 template <
typename VecT>
inline
598 size_t store( std::ostream& _os,
const VecT& _vec, GenProg::Int2Type<2>,
601 size_t bytes = store( _os, _vec[0], _swap );
602 bytes += store( _os, _vec[1], _swap );
606 template <
typename VecT>
inline
607 size_t store( std::ostream& _os,
const VecT& _vec, GenProg::Int2Type<3>,
610 size_t bytes = store( _os, _vec[0], _swap );
611 bytes += store( _os, _vec[1], _swap );
612 bytes += store( _os, _vec[2], _swap );
616 template <
typename VecT>
inline
617 size_t store( std::ostream& _os,
const VecT& _vec, GenProg::Int2Type<4>,
620 size_t bytes = store( _os, _vec[0], _swap );
621 bytes += store( _os, _vec[1], _swap );
622 bytes += store( _os, _vec[2], _swap );
623 bytes += store( _os, _vec[3], _swap );
627 template <
typename VecT>
inline
628 size_t store( std::ostream& _os,
const VecT& _vec, GenProg::Int2Type<1>,
631 return store( _os, _vec[0], _swap );
635 template <
typename VecT>
inline
636 size_t vector_store( std::ostream& _os,
const VecT& _vec,
bool _swap )
638 return store( _os, _vec,
644 template <
typename VecT>
647 restore( std::istream& _is, VecT& _vec, GenProg::Int2Type<2>,
650 size_t bytes = restore( _is, _vec[0], _swap );
651 bytes += restore( _is, _vec[1], _swap );
655 template <
typename VecT>
658 restore( std::istream& _is, VecT& _vec, GenProg::Int2Type<3>,
670 template <
typename VecT>
673 restore( std::istream& _is, VecT& _vec, GenProg::Int2Type<4>,
686 template <
typename VecT>
689 restore( std::istream& _is, VecT& _vec, GenProg::Int2Type<1>,
692 return restore( _is, _vec[0], _swap );
696 template <
typename VecT>
699 vector_restore( std::istream& _is, VecT& _vec,
bool _swap )
701 return restore( _is, _vec,
711 size_t store( std::ostream& _os,
const OMFormat::Chunk::PropertyName& _pn,
714 store( _os, _pn.size(), OMFormat::Chunk::Integer_8, _swap );
716 _os.write( _pn.c_str(), _pn.size() );
717 return _pn.size() + 1;
722 size_t restore( std::istream& _is, OMFormat::Chunk::PropertyName& _pn,
727 restore( _is, size, OMFormat::Chunk::Integer_8, _swap);
729 assert( OMFormat::Chunk::PropertyName::is_valid( size ) );
734 _is.read( buf, size );
747 #if defined(OM_MISSING_HEADER_LIMITS)
748 # undef OM_MISSING_HEADER_LIMITS
751 #if defined(OM_INCLUDE_TEMPLATES) && !defined(OPENMESH_IO_OMFORMAT_CC)
752 # define OPENMESH_IO_OMFORMAT_TEMPLATES
753 # include "OMFormatT_impl.hh"
This file provides the streams omlog, omout, and omerr.
Temporary solution until std::numeric_limits is standard.
Contains all the mesh ingredients like the polygonal mesh, the triangle mesh, different mesh kernels ...
Definition: MeshItems.hh:59
auto operator<<(std::ostream &os, const VectorT< Scalar, DIM > &_vec) -> typename std::enable_if< sizeof(decltype(os<< _vec[0])) >=0
output a vector by printing its space-separated compontens
signed char int8_t
Binary read a short from _is and perform byte swapping if _swap is true.
Definition: SR_types.hh:80
short int16_t
Binary read a short from _is and perform byte swapping if _swap is true.
Definition: SR_types.hh:81
double float64_t
Binary read a short from _is and perform byte swapping if _swap is true.
Definition: SR_types.hh:93
unsigned char uchar
Binary read a short from _is and perform byte swapping if _swap is true.
Definition: SR_types.hh:76
unsigned long long uint64_t
Binary read a short from _is and perform byte swapping if _swap is true.
Definition: SR_types.hh:89
unsigned int uint32_t
Binary read a short from _is and perform byte swapping if _swap is true.
Definition: SR_types.hh:85
long long int64_t
Binary read a short from _is and perform byte swapping if _swap is true.
Definition: SR_types.hh:89
unsigned short uint16_t
Binary read a short from _is and perform byte swapping if _swap is true.
Definition: SR_types.hh:81
float float32_t
Binary read a short from _is and perform byte swapping if _swap is true.
Definition: SR_types.hh:92
int int32_t
Binary read a short from _is and perform byte swapping if _swap is true.
Definition: SR_types.hh:85
unsigned char uint8_t
Binary read a short from _is and perform byte swapping if _swap is true.
Definition: SR_types.hh:80
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:126
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:118
T::value_type value_type
Type of the scalar value.
Definition: vector_traits.hh:94
static const size_t size_
size/dimension of the vector
Definition: vector_traits.hh:97
static size_t size()
size/dimension of the vector
Definition: vector_traits.hh:100