50 #ifndef OPENMESH_IO_OMFORMAT_HH 51 #define OPENMESH_IO_OMFORMAT_HH 56 #include <OpenMesh/Core/System/config.h> 58 #include <OpenMesh/Core/IO/SR_store.hh> 59 #include <OpenMesh/Core/Utils/GenProg.hh> 60 #include <OpenMesh/Core/Utils/Endian.hh> 61 #include <OpenMesh/Core/Utils/vector_traits.hh> 64 #if defined(OM_CC_GCC) && (OM_GCC_VERSION < 30000) 66 # define OM_MISSING_HEADER_LIMITS 1 74 #ifndef DOXY_IGNORE_THIS 116 typedef unsigned char uchar;
137 size_t store( std::ostream& _os,
bool _swap )
const 139 _os.write( (
char*)
this, 4);
141 bytes += binary<uint32_t>::store( _os, n_vertices_, _swap );
142 bytes += binary<uint32_t>::store( _os, n_faces_, _swap );
143 bytes += binary<uint32_t>::store( _os, n_edges_, _swap );
147 size_t restore( std::istream& _is,
bool _swap )
149 if (_is.read( (
char*)
this, 4 ).eof())
153 bytes += binary<uint32_t>::restore( _is, n_vertices_, _swap );
154 bytes += binary<uint32_t>::restore( _is, n_faces_, _swap );
155 bytes += binary<uint32_t>::restore( _is, n_edges_, _swap );
165 typedef uint32 esize_t;
170 Type_Texcoord = 0x02,
178 Entity_Vertex = 0x00,
182 Entity_Halfedge = 0x06,
183 Entity_Sentinel = 0x07
210 static const int SIZE_RESERVED = 1;
211 static const int SIZE_NAME = 1;
212 static const int SIZE_ENTITY = 3;
213 static const int SIZE_TYPE = 4;
215 static const int SIZE_SIGNED = 1;
216 static const int SIZE_FLOAT = 1;
217 static const int SIZE_DIM = 3;
218 static const int SIZE_BITS = 2;
220 static const int OFF_RESERVED = 0;
221 static const int OFF_NAME = SIZE_RESERVED + OFF_RESERVED;
222 static const int OFF_ENTITY = SIZE_NAME + OFF_NAME;
223 static const int OFF_TYPE = SIZE_ENTITY + OFF_ENTITY;
224 static const int OFF_SIGNED = SIZE_TYPE + OFF_TYPE;
225 static const int OFF_FLOAT = SIZE_SIGNED + OFF_SIGNED;
226 static const int OFF_DIM = SIZE_FLOAT + OFF_FLOAT;
227 static const int OFF_BITS = SIZE_DIM + OFF_DIM;
237 unsigned reserved_: SIZE_RESERVED;
238 unsigned name_ : SIZE_NAME;
239 unsigned entity_ : SIZE_ENTITY;
241 unsigned type_ : SIZE_TYPE;
243 unsigned signed_ : SIZE_SIGNED;
244 unsigned float_ : SIZE_FLOAT;
245 unsigned dim_ : SIZE_DIM;
246 unsigned bits_ : SIZE_BITS;
248 unsigned unused_ : 16;
252 class PropertyName :
public std::string
256 static const size_t size_max = 256;
260 PropertyName(
const std::string& _name ) { *
this = _name; }
262 bool is_valid()
const {
return is_valid( size() ); }
264 static bool is_valid(
size_t _s ) {
return _s <= size_max; }
266 PropertyName& operator = (
const std::string& _rhs )
268 assert( is_valid( _rhs.size() ) );
270 if ( is_valid( _rhs.size() ) )
271 std::string::operator = ( _rhs );
274 omerr() <<
"Warning! Property name too long. Will be shortened!\n";
275 this->std::string::operator = ( _rhs.substr(0, size_max) );
290 inline size_t header_size(
void) {
return sizeof(Header); }
294 inline size_t chunk_header_size(
void ) {
return sizeof(uint16); }
298 inline size_t scalar_size(
const Chunk::Header& _hdr )
300 return _hdr.float_ ? (0x01 << _hdr.bits_) : (0x04 << _hdr.bits_);
305 inline size_t dimensions(
const Chunk::Header& _chdr) {
return _chdr.dim_+1; }
309 inline size_t vector_size(
const Chunk::Header& _chdr )
311 return dimensions(_chdr)*scalar_size(_chdr);
316 inline size_t chunk_data_size( Header& _hdr, Chunk::Header& _chunk_hdr )
319 switch( _chunk_hdr.entity_ )
321 case Chunk::Entity_Vertex: C = _hdr.n_vertices_;
break;
322 case Chunk::Entity_Face: C = _hdr.n_faces_;
break;
323 case Chunk::Entity_Halfedge: C = _hdr.n_edges_*2;
break;
324 case Chunk::Entity_Edge: C = _hdr.n_edges_;
break;
325 case Chunk::Entity_Mesh: C = 1;
break;
328 std::cerr <<
"Invalid value in _chunk_hdr.entity_\n";
333 return C * vector_size( _chunk_hdr );
336 inline size_t chunk_size( Header& _hdr, Chunk::Header& _chunk_hdr )
338 return chunk_header_size() + chunk_data_size( _hdr, _chunk_hdr );
343 uint16&
operator << (uint16& val,
const Chunk::Header& hdr);
344 Chunk::Header&
operator << (Chunk::Header& hdr,
const uint16 val);
349 template <
typename T>
bool is_float(
const T&)
351 #if defined(OM_MISSING_HEADER_LIMITS) 352 return !Utils::NumLimitsT<T>::is_integer();
354 return !std::numeric_limits<T>::is_integer;
358 template <
typename T>
bool is_integer(
const T)
360 #if defined(OM_MISSING_HEADER_LIMITS) 361 return Utils::NumLimitsT<T>::is_integer();
363 return std::numeric_limits<T>::is_integer;
367 template <
typename T>
bool is_signed(
const T&)
369 #if defined(OM_MISSING_HEADER_LIMITS) 370 return Utils::NumLimitsT<T>::is_signed();
372 return std::numeric_limits<T>::is_signed;
378 template <
typename VecType>
380 Chunk::Dim dim( VecType )
386 template <
typename VecType>
388 Chunk::Dim dim(
const Chunk::Header& _hdr )
390 return static_cast<Chunk::Dim
>( _hdr.dim_ );
394 Chunk::Integer_Size needed_bits(
size_t s );
398 template <
typename T> Chunk::Integer_Size integer_size(
const T&)
400 template <
typename T> Chunk::Integer_Size integer_size(
const T& d)
404 assert( is_integer(d) );
409 case 1:
return OMFormat::Chunk::Integer_8;
410 case 2:
return OMFormat::Chunk::Integer_16;
411 case 4:
return OMFormat::Chunk::Integer_32;
412 case 8:
return OMFormat::Chunk::Integer_64;
414 std::cerr <<
"Invalid value in integer_size\n";
418 return Chunk::Integer_Size(0);
424 template <
typename T> Chunk::Float_Size float_size(
const T&)
426 template <
typename T> Chunk::Float_Size float_size(
const T& d)
430 assert( is_float(d) );
435 case 4:
return OMFormat::Chunk::Float_32;
436 case 8:
return OMFormat::Chunk::Float_64;
437 case 16:
return OMFormat::Chunk::Float_128;
439 std::cerr <<
"Invalid value in float_size\n";
443 return Chunk::Float_Size(0);
447 template <
typename T>
449 unsigned int bits(
const T& val)
451 return is_integer(val)
452 ? (
static_cast<unsigned int>(integer_size(val)))
453 : (
static_cast<unsigned int>(float_size(val)));
458 inline uint8 mk_version(
const uint16 major,
const uint16 minor)
459 {
return (major & 0x07) << 5 | (minor & 0x1f); }
462 inline uint16 major_version(
const uint8 version)
463 {
return (version >> 5) & 0x07; }
466 inline uint16 minor_version(
const uint8 version)
467 {
return (version & 0x001f); }
472 const char *as_string(Chunk::Type t);
473 const char *as_string(Chunk::Entity e);
474 const char *as_string(Chunk::Dim d);
475 const char *as_string(Chunk::Integer_Size d);
476 const char *as_string(Chunk::Float_Size d);
478 std::ostream&
operator << ( std::ostream& _os,
const Header& _h );
479 std::ostream&
operator << ( std::ostream& _os,
const Chunk::Header& _c );
487 size_t store( std::ostream& _os,
const OMFormat::Header& _hdr,
bool _swap)
488 {
return _hdr.store( _os, _swap ); }
491 size_t restore( std::istream& _is, OMFormat::Header& _hdr,
bool _swap )
492 {
return _hdr.restore( _is, _swap ); }
499 store( std::ostream& _os,
const OMFormat::Chunk::Header& _hdr,
bool _swap)
501 OMFormat::uint16 val; val << _hdr;
502 return binary<uint16_t>::store( _os, val, _swap );
507 restore( std::istream& _is, OMFormat::Chunk::Header& _hdr,
bool _swap )
509 OMFormat::uint16 val;
510 size_t bytes = binary<uint16_t>::restore( _is, val, _swap );
519 typedef GenProg::TrueType t_signed;
520 typedef GenProg::FalseType t_unsigned;
523 template<
typename T >
525 store( std::ostream& _os,
527 OMFormat::Chunk::Integer_Size _b,
532 template<
typename T >
534 store( std::ostream& _os,
536 OMFormat::Chunk::Integer_Size _b,
541 template<
typename T >
544 store( std::ostream& _os,
546 OMFormat::Chunk::Integer_Size _b,
549 assert( OMFormat::is_integer( _val ) );
551 if ( OMFormat::is_signed( _val ) )
552 return store( _os, _val, _b, _swap, t_signed() );
553 return store( _os, _val, _b, _swap, t_unsigned() );
557 template<
typename T >
inline 558 size_t restore( std::istream& _is,
560 OMFormat::Chunk::Integer_Size _b,
565 template<
typename T >
inline 566 size_t restore( std::istream& _is,
568 OMFormat::Chunk::Integer_Size _b,
573 template<
typename T >
576 restore( std::istream& _is,
578 OMFormat::Chunk::Integer_Size _b,
581 assert( OMFormat::is_integer( _val ) );
583 if ( OMFormat::is_signed( _val ) )
584 return restore( _is, _val, _b, _swap, t_signed() );
585 return restore( _is, _val, _b, _swap, t_unsigned() );
591 template <
typename VecT>
inline 592 size_t store( std::ostream& _os,
const VecT& _vec, GenProg::Int2Type<2>,
595 size_t bytes = store( _os, _vec[0], _swap );
596 bytes += store( _os, _vec[1], _swap );
600 template <
typename VecT>
inline 601 size_t store( std::ostream& _os,
const VecT& _vec, GenProg::Int2Type<3>,
604 size_t bytes = store( _os, _vec[0], _swap );
605 bytes += store( _os, _vec[1], _swap );
606 bytes += store( _os, _vec[2], _swap );
610 template <
typename VecT>
inline 611 size_t store( std::ostream& _os,
const VecT& _vec, GenProg::Int2Type<4>,
614 size_t bytes = store( _os, _vec[0], _swap );
615 bytes += store( _os, _vec[1], _swap );
616 bytes += store( _os, _vec[2], _swap );
617 bytes += store( _os, _vec[3], _swap );
621 template <
typename VecT>
inline 622 size_t store( std::ostream& _os,
const VecT& _vec, GenProg::Int2Type<1>,
625 return store( _os, _vec[0], _swap );
629 template <
typename VecT>
inline 630 size_t vector_store( std::ostream& _os,
const VecT& _vec,
bool _swap )
632 return store( _os, _vec,
638 template <
typename VecT>
641 restore( std::istream& _is, VecT& _vec, GenProg::Int2Type<2>,
644 size_t bytes = restore( _is, _vec[0], _swap );
645 bytes += restore( _is, _vec[1], _swap );
649 template <
typename VecT>
652 restore( std::istream& _is, VecT& _vec, GenProg::Int2Type<3>,
658 bytes = binary<scalar_type>::restore( _is, _vec[0], _swap );
659 bytes += binary<scalar_type>::restore( _is, _vec[1], _swap );
660 bytes += binary<scalar_type>::restore( _is, _vec[2], _swap );
664 template <
typename VecT>
667 restore( std::istream& _is, VecT& _vec, GenProg::Int2Type<4>,
673 bytes = binary<scalar_type>::restore( _is, _vec[0], _swap );
674 bytes += binary<scalar_type>::restore( _is, _vec[1], _swap );
675 bytes += binary<scalar_type>::restore( _is, _vec[2], _swap );
676 bytes += binary<scalar_type>::restore( _is, _vec[3], _swap );
680 template <
typename VecT>
683 restore( std::istream& _is, VecT& _vec, GenProg::Int2Type<1>,
686 return restore( _is, _vec[0], _swap );
690 template <
typename VecT>
693 vector_restore( std::istream& _is, VecT& _vec,
bool _swap )
695 return restore( _is, _vec,
705 size_t store( std::ostream& _os,
const OMFormat::Chunk::PropertyName& _pn,
708 store( _os, _pn.size(), OMFormat::Chunk::Integer_8, _swap );
710 _os.write( _pn.c_str(), _pn.size() );
711 return _pn.size() + 1;
716 size_t restore( std::istream& _is, OMFormat::Chunk::PropertyName& _pn,
721 restore( _is, size, OMFormat::Chunk::Integer_8, _swap);
723 assert( OMFormat::Chunk::PropertyName::is_valid( size ) );
728 _is.read( buf, size );
741 #if defined(OM_MISSING_HEADER_LIMITS) 742 # undef OM_MISSING_HEADER_LIMITS 745 #if defined(OM_INCLUDE_TEMPLATES) && !defined(OPENMESH_IO_OMFORMAT_CC) 746 # define OPENMESH_IO_OMFORMAT_TEMPLATES 747 # include "OMFormatT.cc" unsigned char uchar
Binary read a short from _is and perform byte swapping if _swap is true.
Definition: SR_types.hh:81
This file provides the streams omlog, omout, and omerr.
Temporary solution until std::numeric_limits is standard.
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 size_t size()
size/dimension of the vector
Definition: vector_traits.hh:105
short int16_t
Binary read a short from _is and perform byte swapping if _swap is true.
Definition: SR_types.hh:86
T::value_type value_type
Type of the scalar value.
Definition: vector_traits.hh:99
int int32_t
Binary read a short from _is and perform byte swapping if _swap is true.
Definition: SR_types.hh:90
long long int64_t
Binary read a short from _is and perform byte swapping if _swap is true.
Definition: SR_types.hh:94
Contains all the mesh ingredients like the polygonal mesh, the triangle mesh, different mesh kernels ...
Definition: MeshItems.hh:64
float float32_t
Binary read a short from _is and perform byte swapping if _swap is true.
Definition: SR_types.hh:97
unsigned int uint32_t
Binary read a short from _is and perform byte swapping if _swap is true.
Definition: SR_types.hh:90
unsigned char uint8_t
Binary read a short from _is and perform byte swapping if _swap is true.
Definition: SR_types.hh:85
signed char int8_t
Binary read a short from _is and perform byte swapping if _swap is true.
Definition: SR_types.hh:85
unsigned long long uint64_t
Binary read a short from _is and perform byte swapping if _swap is true.
Definition: SR_types.hh:94
static const size_t size_
size/dimension of the vector
Definition: vector_traits.hh:102
double float64_t
Binary read a short from _is and perform byte swapping if _swap is true.
Definition: SR_types.hh:98
unsigned short uint16_t
Binary read a short from _is and perform byte swapping if _swap is true.
Definition: SR_types.hh:86