45 #ifndef OPENMESH_IO_OMFORMAT_HH 46 #define OPENMESH_IO_OMFORMAT_HH 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);
136 bytes += binary<uint32_t>::store( _os, n_vertices_, _swap );
137 bytes += binary<uint32_t>::store( _os, n_faces_, _swap );
138 bytes += binary<uint32_t>::store( _os, n_edges_, _swap );
142 size_t restore( std::istream& _is,
bool _swap )
144 if (_is.read( (
char*)
this, 4 ).eof())
148 bytes += binary<uint32_t>::restore( _is, n_vertices_, _swap );
149 bytes += binary<uint32_t>::restore( _is, n_faces_, _swap );
150 bytes += binary<uint32_t>::restore( _is, n_edges_, _swap );
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 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) );
285 inline size_t header_size(
void) {
return sizeof(Header); }
289 inline size_t chunk_header_size(
void ) {
return sizeof(uint16); }
293 inline size_t scalar_size(
const Chunk::Header& _hdr )
295 return _hdr.float_ ? (0x01 << _hdr.bits_) : (0x04 << _hdr.bits_);
300 inline size_t dimensions(
const Chunk::Header& _chdr) {
return _chdr.dim_+1; }
304 inline size_t vector_size(
const Chunk::Header& _chdr )
306 return dimensions(_chdr)*scalar_size(_chdr);
311 inline size_t chunk_data_size( Header& _hdr, Chunk::Header& _chunk_hdr )
314 switch( _chunk_hdr.entity_ )
316 case Chunk::Entity_Vertex: C = _hdr.n_vertices_;
break;
317 case Chunk::Entity_Face: C = _hdr.n_faces_;
break;
318 case Chunk::Entity_Halfedge: C = _hdr.n_edges_*2;
break;
319 case Chunk::Entity_Edge: C = _hdr.n_edges_;
break;
320 case Chunk::Entity_Mesh: C = 1;
break;
323 std::cerr <<
"Invalid value in _chunk_hdr.entity_\n";
328 return C * vector_size( _chunk_hdr );
331 inline size_t chunk_size( Header& _hdr, Chunk::Header& _chunk_hdr )
333 return chunk_header_size() + chunk_data_size( _hdr, _chunk_hdr );
338 uint16&
operator << (uint16& val,
const Chunk::Header& hdr);
339 Chunk::Header&
operator << (Chunk::Header& hdr,
const uint16 val);
344 template <
typename T>
bool is_float(
const T&)
346 #if defined(OM_MISSING_HEADER_LIMITS) 347 return !Utils::NumLimitsT<T>::is_integer();
349 return !std::numeric_limits<T>::is_integer;
353 template <
typename T>
bool is_integer(
const T)
355 #if defined(OM_MISSING_HEADER_LIMITS) 356 return Utils::NumLimitsT<T>::is_integer();
358 return std::numeric_limits<T>::is_integer;
362 template <
typename T>
bool is_signed(
const T&)
364 #if defined(OM_MISSING_HEADER_LIMITS) 365 return Utils::NumLimitsT<T>::is_signed();
367 return std::numeric_limits<T>::is_signed;
373 template <
typename VecType>
375 Chunk::Dim dim( VecType )
381 template <
typename VecType>
383 Chunk::Dim dim(
const Chunk::Header& _hdr )
385 return static_cast<Chunk::Dim
>( _hdr.dim_ );
389 Chunk::Integer_Size needed_bits(
size_t s );
393 template <
typename T> Chunk::Integer_Size integer_size(
const T&)
395 template <
typename T> Chunk::Integer_Size integer_size(
const T& d)
399 assert( is_integer(d) );
404 case 1:
return OMFormat::Chunk::Integer_8;
405 case 2:
return OMFormat::Chunk::Integer_16;
406 case 4:
return OMFormat::Chunk::Integer_32;
407 case 8:
return OMFormat::Chunk::Integer_64;
409 std::cerr <<
"Invalid value in integer_size\n";
413 return Chunk::Integer_Size(0);
419 template <
typename T> Chunk::Float_Size float_size(
const T&)
421 template <
typename T> Chunk::Float_Size float_size(
const T& d)
425 assert( is_float(d) );
430 case 4:
return OMFormat::Chunk::Float_32;
431 case 8:
return OMFormat::Chunk::Float_64;
432 case 16:
return OMFormat::Chunk::Float_128;
434 std::cerr <<
"Invalid value in float_size\n";
438 return Chunk::Float_Size(0);
442 template <
typename T>
444 unsigned int bits(
const T& val)
446 return is_integer(val)
447 ? (
static_cast<unsigned int>(integer_size(val)))
448 : (
static_cast<unsigned int>(float_size(val)));
453 inline uint8 mk_version(
const uint16 major,
const uint16 minor)
454 {
return (major & 0x07) << 5 | (minor & 0x1f); }
457 inline uint16 major_version(
const uint8 version)
458 {
return (version >> 5) & 0x07; }
461 inline uint16 minor_version(
const uint8 version)
462 {
return (version & 0x001f); }
467 std::string as_string(uint8 version);
469 const char *as_string(Chunk::Type t);
470 const char *as_string(Chunk::Entity e);
471 const char *as_string(Chunk::Dim d);
472 const char *as_string(Chunk::Integer_Size d);
473 const char *as_string(Chunk::Float_Size d);
475 std::ostream&
operator << ( std::ostream& _os,
const Header& _h );
476 std::ostream&
operator << ( std::ostream& _os,
const Chunk::Header& _c );
484 size_t store( std::ostream& _os,
const OMFormat::Header& _hdr,
bool _swap)
485 {
return _hdr.store( _os, _swap ); }
488 size_t restore( std::istream& _is, OMFormat::Header& _hdr,
bool _swap )
489 {
return _hdr.restore( _is, _swap ); }
496 store( std::ostream& _os,
const OMFormat::Chunk::Header& _hdr,
bool _swap)
498 OMFormat::uint16 val; val << _hdr;
499 return binary<uint16_t>::store( _os, val, _swap );
504 restore( std::istream& _is, OMFormat::Chunk::Header& _hdr,
bool _swap )
506 OMFormat::uint16 val;
507 size_t bytes = binary<uint16_t>::restore( _is, val, _swap );
516 typedef GenProg::TrueType t_signed;
517 typedef GenProg::FalseType t_unsigned;
520 template<
typename T >
522 store( std::ostream& _os,
524 OMFormat::Chunk::Integer_Size _b,
529 template<
typename T >
531 store( std::ostream& _os,
533 OMFormat::Chunk::Integer_Size _b,
538 template<
typename T >
541 store( std::ostream& _os,
543 OMFormat::Chunk::Integer_Size _b,
546 assert( OMFormat::is_integer( _val ) );
548 if ( OMFormat::is_signed( _val ) )
549 return store( _os, _val, _b, _swap, t_signed() );
550 return store( _os, _val, _b, _swap, t_unsigned() );
554 template<
typename T >
inline 555 size_t restore( std::istream& _is,
557 OMFormat::Chunk::Integer_Size _b,
562 template<
typename T >
inline 563 size_t restore( std::istream& _is,
565 OMFormat::Chunk::Integer_Size _b,
570 template<
typename T >
573 restore( std::istream& _is,
575 OMFormat::Chunk::Integer_Size _b,
578 assert( OMFormat::is_integer( _val ) );
580 if ( OMFormat::is_signed( _val ) )
581 return restore( _is, _val, _b, _swap, t_signed() );
582 return restore( _is, _val, _b, _swap, t_unsigned() );
588 template <
typename VecT>
inline 589 size_t store( std::ostream& _os,
const VecT& _vec, GenProg::Int2Type<2>,
592 size_t bytes = store( _os, _vec[0], _swap );
593 bytes += store( _os, _vec[1], _swap );
597 template <
typename VecT>
inline 598 size_t store( std::ostream& _os,
const VecT& _vec, GenProg::Int2Type<3>,
601 size_t bytes = store( _os, _vec[0], _swap );
602 bytes += store( _os, _vec[1], _swap );
603 bytes += store( _os, _vec[2], _swap );
607 template <
typename VecT>
inline 608 size_t store( std::ostream& _os,
const VecT& _vec, GenProg::Int2Type<4>,
611 size_t bytes = store( _os, _vec[0], _swap );
612 bytes += store( _os, _vec[1], _swap );
613 bytes += store( _os, _vec[2], _swap );
614 bytes += store( _os, _vec[3], _swap );
618 template <
typename VecT>
inline 619 size_t store( std::ostream& _os,
const VecT& _vec, GenProg::Int2Type<1>,
622 return store( _os, _vec[0], _swap );
626 template <
typename VecT>
inline 627 size_t vector_store( std::ostream& _os,
const VecT& _vec,
bool _swap )
629 return store( _os, _vec,
635 template <
typename VecT>
638 restore( std::istream& _is, VecT& _vec, GenProg::Int2Type<2>,
641 size_t bytes = restore( _is, _vec[0], _swap );
642 bytes += restore( _is, _vec[1], _swap );
646 template <
typename VecT>
649 restore( std::istream& _is, VecT& _vec, GenProg::Int2Type<3>,
655 bytes = binary<scalar_type>::restore( _is, _vec[0], _swap );
656 bytes += binary<scalar_type>::restore( _is, _vec[1], _swap );
657 bytes += binary<scalar_type>::restore( _is, _vec[2], _swap );
661 template <
typename VecT>
664 restore( std::istream& _is, VecT& _vec, GenProg::Int2Type<4>,
670 bytes = binary<scalar_type>::restore( _is, _vec[0], _swap );
671 bytes += binary<scalar_type>::restore( _is, _vec[1], _swap );
672 bytes += binary<scalar_type>::restore( _is, _vec[2], _swap );
673 bytes += binary<scalar_type>::restore( _is, _vec[3], _swap );
677 template <
typename VecT>
680 restore( std::istream& _is, VecT& _vec, GenProg::Int2Type<1>,
683 return restore( _is, _vec[0], _swap );
687 template <
typename VecT>
690 vector_restore( std::istream& _is, VecT& _vec,
bool _swap )
692 return restore( _is, _vec,
702 size_t store( std::ostream& _os,
const OMFormat::Chunk::PropertyName& _pn,
705 store( _os, _pn.size(), OMFormat::Chunk::Integer_8, _swap );
707 _os.write( _pn.c_str(), _pn.size() );
708 return _pn.size() + 1;
713 size_t restore( std::istream& _is, OMFormat::Chunk::PropertyName& _pn,
718 restore( _is, size, OMFormat::Chunk::Integer_8, _swap);
720 assert( OMFormat::Chunk::PropertyName::is_valid( size ) );
725 _is.read( buf, size );
738 #if defined(OM_MISSING_HEADER_LIMITS) 739 # undef OM_MISSING_HEADER_LIMITS 742 #if defined(OM_INCLUDE_TEMPLATES) && !defined(OPENMESH_IO_OMFORMAT_CC) 743 # define OPENMESH_IO_OMFORMAT_TEMPLATES 744 # include "OMFormatT_impl.hh" 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
static const size_t size_
size/dimension of the vector
unsigned long long uint64_t
static size_t size()
size/dimension of the vector
T::value_type value_type
Type of the scalar value.