OpenMesh
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
OMFormat.hh
1 /*===========================================================================*\
2  * *
3  * OpenMesh *
4  * Copyright (C) 2001-2012 by Computer Graphics Group, RWTH Aachen *
5  * www.openmesh.org *
6  * *
7  *---------------------------------------------------------------------------*
8  * This file is part of OpenMesh. *
9  * *
10  * OpenMesh is free software: you can redistribute it and/or modify *
11  * it under the terms of the GNU Lesser General Public License as *
12  * published by the Free Software Foundation, either version 3 of *
13  * the License, or (at your option) any later version with the *
14  * following exceptions: *
15  * *
16  * If other files instantiate templates or use macros *
17  * or inline functions from this file, or you compile this file and *
18  * link it with other files to produce an executable, this file does *
19  * not by itself cause the resulting executable to be covered by the *
20  * GNU Lesser General Public License. This exception does not however *
21  * invalidate any other reasons why the executable file might be *
22  * covered by the GNU Lesser General Public License. *
23  * *
24  * OpenMesh is distributed in the hope that it will be useful, *
25  * but WITHOUT ANY WARRANTY; without even the implied warranty of *
26  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
27  * GNU Lesser General Public License for more details. *
28  * *
29  * You should have received a copy of the GNU LesserGeneral Public *
30  * License along with OpenMesh. If not, *
31  * see <http://www.gnu.org/licenses/>. *
32  * *
33 \*===========================================================================*/
34 
35 /*===========================================================================*\
36  * *
37  * $Revision: 736 $ *
38  * $Date: 2012-10-08 09:30:49 +0200 (Mo, 08 Okt 2012) $ *
39  * *
40 \*===========================================================================*/
41 
42 
43 #ifndef OPENMESH_IO_OMFORMAT_HH
44 #define OPENMESH_IO_OMFORMAT_HH
45 
46 
47 //=== INCLUDES ================================================================
48 
49 #include <OpenMesh/Core/System/config.h>
51 #include <OpenMesh/Core/IO/SR_store.hh>
52 #include <OpenMesh/Core/Utils/GenProg.hh>
53 #include <OpenMesh/Core/Utils/Endian.hh>
54 #include <OpenMesh/Core/Utils/vector_traits.hh>
55 // --------------------
56 #include <iostream>
57 #if defined(OM_CC_GCC) && (OM_GCC_VERSION < 30000)
59 # define OM_MISSING_HEADER_LIMITS 1
60 #else
61 # include <limits>
62 #endif
63 
64 
65 //== NAMESPACES ==============================================================
66 
67 #ifndef DOXY_IGNORE_THIS
68 namespace OpenMesh {
69 namespace IO {
70 namespace OMFormat {
71 
72 
73 //=== IMPLEMENTATION ==========================================================
74 
75 
79 
80 //-----------------------------------------------------------------------------
81 
82  // <:Header>
83  // <:Comment>
84  // Chunk 0
85  // <:ChunkHeader>
86  // <:Comment>
87  // data
88  // Chunk 1
89  // <:ChunkHeader>
90  // <:Comment>
91  // data
92  // .
93  // .
94  // .
95  // Chunk N
96 
97  //
98  // NOTICE!
99  //
100  // The usage of data types who differ in size
101  // on different pc architectures (32/64 bit) and/or
102  // operating systems, e.g. (unsigned) long, size_t,
103  // is not recommended because of inconsistencies
104  // in case of cross writing and reading.
105  //
106  // Basic types that are supported are:
107 
108 
109  typedef unsigned char uchar;
110  typedef uint8_t uint8;
111  typedef uint16_t uint16;
112  typedef uint32_t uint32;
113  typedef uint64_t uint64;
114  typedef int8_t int8;
115  typedef int16_t int16;
116  typedef int32_t int32;
117  typedef int64_t int64;
118  typedef float32_t float32;
119  typedef float64_t float64;
120 
121  struct Header
122  {
123  uchar magic_[2]; // OM
124  uchar mesh_; // [T]riangles, [Q]uads, [P]olygonals
125  uint8 version_;
126  uint32 n_vertices_;
127  uint32 n_faces_;
128  uint32 n_edges_;
129 
130  size_t store( std::ostream& _os, bool _swap ) const
131  {
132  _os.write( (char*)this, 4); // magic_, mesh_, version_
133  size_t bytes = 4;
134  bytes += binary<uint32_t>::store( _os, n_vertices_, _swap );
135  bytes += binary<uint32_t>::store( _os, n_faces_, _swap );
136  bytes += binary<uint32_t>::store( _os, n_edges_, _swap );
137  return bytes;
138  }
139 
140  size_t restore( std::istream& _is, bool _swap )
141  {
142  if (_is.read( (char*)this, 4 ).eof())
143  return 0;
144 
145  size_t bytes = 4;
146  bytes += binary<uint32_t>::restore( _is, n_vertices_, _swap );
147  bytes += binary<uint32_t>::restore( _is, n_faces_, _swap );
148  bytes += binary<uint32_t>::restore( _is, n_edges_, _swap );
149  return bytes;
150  }
151 
152  };
153 
154  struct Chunk
155  {
156  // Hardcoded this size to an uint32 to make the system 32/64 bit compatible.
157  // Needs further investigation!
158  typedef uint32 esize_t; // element size, used for custom properties
159 
160  enum Type {
161  Type_Pos = 0x00,
162  Type_Normal = 0x01,
163  Type_Texcoord = 0x02,
164  Type_Status = 0x03,
165  Type_Color = 0x04,
166  Type_Custom = 0x06,
167  Type_Topology = 0x07
168  };
169 
170  enum Entity {
171  Entity_Vertex = 0x00,
172  Entity_Mesh = 0x01,
173  Entity_Face = 0x02,
174  Entity_Edge = 0x04,
175  Entity_Halfedge = 0x06
176  };
177 
178  enum Dim {
179  Dim_1D = 0x00,
180  Dim_2D = 0x01,
181  Dim_3D = 0x02,
182  Dim_4D = 0x03,
183  Dim_5D = 0x04,
184  Dim_6D = 0x05,
185  Dim_7D = 0x06,
186  Dim_8D = 0x07
187  };
188 
189  enum Integer_Size {
190  Integer_8 = 0x00, // 1 byte for (unsigned) char
191  Integer_16 = 0x01, // 2 bytes for short
192  Integer_32 = 0x02, // 4 bytes for long
193  Integer_64 = 0x03 // 8 bytes for long long
194  };
195 
196  enum Float_Size {
197  Float_32 = 0x00, // 4 bytes for float
198  Float_64 = 0x01, // 8 bytes for double
199  Float_128 = 0x02 // 16 bytes for long double (an assumption!)
200  };
201 
202  static const int SIZE_RESERVED = 1; // 1
203  static const int SIZE_NAME = 1; // 2
204  static const int SIZE_ENTITY = 3; // 5
205  static const int SIZE_TYPE = 4; // 9
206 
207  static const int SIZE_SIGNED = 1; // 10
208  static const int SIZE_FLOAT = 1; // 11
209  static const int SIZE_DIM = 3; // 14
210  static const int SIZE_BITS = 2; // 16
211 
212  static const int OFF_RESERVED = 0; // 0
213  static const int OFF_NAME = SIZE_RESERVED + OFF_RESERVED; // 2
214  static const int OFF_ENTITY = SIZE_NAME + OFF_NAME; // 3
215  static const int OFF_TYPE = SIZE_ENTITY + OFF_ENTITY; // 5
216  static const int OFF_SIGNED = SIZE_TYPE + OFF_TYPE; // 9
217  static const int OFF_FLOAT = SIZE_SIGNED + OFF_SIGNED; // 10
218  static const int OFF_DIM = SIZE_FLOAT + OFF_FLOAT; // 11
219  static const int OFF_BITS = SIZE_DIM + OFF_DIM; // 14
220 
221  // !Attention! When changing the bit size, the operators
222  // << (uint16, Header) and << (Header, uint16) must be changed as well
223  //
224  // Entries signed_, float_, dim_, bits_ are not used when type_
225  // equals Type_Custom
226  //
227  struct Header // 16 bits long
228  {
229  unsigned reserved_: SIZE_RESERVED;
230  unsigned name_ : SIZE_NAME; // 1 named property, 0 anonymous
231  unsigned entity_ : SIZE_ENTITY; // 0 vertex, 1 mesh, 2 edge,
232  // 4 halfedge, 6 face
233  unsigned type_ : SIZE_TYPE; // 0 pos, 1 normal, 2 texcoord,
234  // 3 status, 4 color 6 custom 7 topology
235  unsigned signed_ : SIZE_SIGNED; // bool
236  unsigned float_ : SIZE_FLOAT; // bool
237  unsigned dim_ : SIZE_DIM; // 0 1D, 1 2D, 2 3D, .., 7 8D
238  unsigned bits_ : SIZE_BITS; // {8, 16, 32, 64} | {32, 64, 128}
239  // (integer) (float)
240  unsigned unused_ : 16; // fill up to 32 bits
241  }; // struct Header
242 
243 
244  class PropertyName : public std::string
245  {
246  public:
247 
248  static const size_t size_max = 256;
249 
250  PropertyName( ) { }
251 
252  PropertyName( const std::string& _name ) { *this = _name; }
253 
254  bool is_valid() const { return is_valid( size() ); }
255 
256  static bool is_valid( size_t _s ) { return _s <= size_max; }
257 
258  PropertyName& operator = ( const std::string& _rhs )
259  {
260  assert( is_valid( _rhs.size() ) );
261 
262  if ( is_valid( _rhs.size() ) )
263  std::string::operator = ( _rhs );
264  else
265  {
266  omerr() << "Warning! Property name too long. Will be shortened!\n";
267  this->std::string::operator = ( _rhs.substr(0, size_max) );
268  }
269 
270  return *this;
271  }
272 
273  };
274 
275  }; // Chunk
276 
277  // ------------------------------------------------------------ Helper
278 
279  // -------------------- get size information
280 
282  inline size_t header_size(void) { return sizeof(Header); }
283 
284 
286  inline size_t chunk_header_size( void ) { return sizeof(uint16); }
287 
288 
290  inline size_t scalar_size( const Chunk::Header& _hdr )
291  {
292  return _hdr.float_ ? (0x01 << _hdr.bits_) : (0x04 << _hdr.bits_);
293  }
294 
295 
297  inline size_t dimensions(const Chunk::Header& _chdr) { return _chdr.dim_+1; }
298 
299 
301  inline size_t vector_size( const Chunk::Header& _chdr )
302  {
303  return dimensions(_chdr)*scalar_size(_chdr);
304  }
305 
306 
308  inline size_t chunk_data_size( Header& _hdr, Chunk::Header& _chunk_hdr )
309  {
310  size_t C = 0;
311 
312  switch( _chunk_hdr.entity_ )
313  {
314  case Chunk::Entity_Vertex: C = _hdr.n_vertices_; break;
315  case Chunk::Entity_Face: C = _hdr.n_faces_; break;
316  case Chunk::Entity_Halfedge: C = _hdr.n_edges_; // no break!
317  case Chunk::Entity_Edge: C += _hdr.n_edges_; break;
318  case Chunk::Entity_Mesh: C = 1; break;
319  default:
320  std::cerr << "Invalid value in _chunk_hdr.entity_\n";
321  assert( false );
322  }
323 
324  return C * vector_size( _chunk_hdr );
325  }
326 
327  inline size_t chunk_size( Header& _hdr, Chunk::Header& _chunk_hdr )
328  {
329  return chunk_header_size() + chunk_data_size( _hdr, _chunk_hdr );
330  }
331 
332  // -------------------- convert from Chunk::Header to storage type
333 
334  uint16& operator << (uint16& val, const Chunk::Header& hdr);
335  Chunk::Header& operator << (Chunk::Header& hdr, const uint16 val);
336 
337 
338  // -------------------- type information
339 
340  template <typename T> bool is_float(const T&)
341  {
342 #if defined(OM_MISSING_HEADER_LIMITS)
343  return !Utils::NumLimitsT<T>::is_integer();
344 #else
345  return !std::numeric_limits<T>::is_integer;
346 #endif
347  }
348 
349  template <typename T> bool is_integer(const T)
350  {
351 #if defined(OM_MISSING_HEADER_LIMITS)
352  return Utils::NumLimitsT<T>::is_integer();
353 #else
354  return std::numeric_limits<T>::is_integer;
355 #endif
356  }
357 
358  template <typename T> bool is_signed(const T&)
359  {
360 #if defined(OM_MISSING_HEADER_LIMITS)
361  return Utils::NumLimitsT<T>::is_signed();
362 #else
363  return std::numeric_limits<T>::is_signed;
364 #endif
365  }
366 
367  // -------------------- conversions (format type <- type/value)
368 
369  template <typename VecType>
370  inline
371  Chunk::Dim dim( VecType )
372  {
373  assert( vector_traits< VecType >::size() < 9 );
374  return static_cast<Chunk::Dim>(vector_traits< VecType >::size() - 1);
375  }
376 
377  template <typename VecType>
378  inline
379  Chunk::Dim dim( const Chunk::Header& _hdr )
380  {
381  return static_cast<Chunk::Dim>( _hdr.dim_ );
382  }
383 
384  // calc minimum (power-of-2) number of bits needed
385  Chunk::Integer_Size needed_bits( size_t s );
386 
387  // Convert size of type to Integer_Size
388 #ifdef NDEBUG
389  template <typename T> Chunk::Integer_Size integer_size(const T&)
390 #else
391  template <typename T> Chunk::Integer_Size integer_size(const T& d)
392 #endif
393  {
394  assert( is_integer(d) );
395 
396  switch( sizeof(T) )
397  {
398  case 1: return OMFormat::Chunk::Integer_8;
399  case 2: return OMFormat::Chunk::Integer_16;
400  case 4: return OMFormat::Chunk::Integer_32;
401  case 8: return OMFormat::Chunk::Integer_64;
402  }
403  return Chunk::Integer_Size(0);
404  }
405 
406 
407  // Convert size of type to FLoat_Size
408 #ifdef NDEBUG
409  template <typename T> Chunk::Float_Size float_size(const T&)
410 #else
411  template <typename T> Chunk::Float_Size float_size(const T& d)
412 #endif
413  {
414  assert( is_float(d) );
415 
416  switch( sizeof(T) )
417  {
418  case 4: return OMFormat::Chunk::Float_32;
419  case 8: return OMFormat::Chunk::Float_64;
420  case 16: return OMFormat::Chunk::Float_128;
421  }
422  return Chunk::Float_Size(0);
423  }
424 
425  // Return the storage type (Chunk::Header::bits_)
426  template <typename T>
427  inline
428  unsigned int bits(const T& val)
429  {
430  return is_integer(val)
431  ? (static_cast<unsigned int>(integer_size(val)))
432  : (static_cast<unsigned int>(float_size(val)));
433  }
434 
435  // -------------------- create/read version
436 
437  inline uint8 mk_version(const uint16 major, const uint16 minor)
438  { return (major & 0x07) << 5 | (minor & 0x1f); }
439 
440 
441  inline uint16 major_version(const uint8 version)
442  { return (version >> 5) & 0x07; }
443 
444 
445  inline uint16 minor_version(const uint8 version)
446  { return (version & 0x001f); }
447 
448 
449  // ---------------------------------------- convenience functions
450 
451  const char *as_string(Chunk::Type t);
452  const char *as_string(Chunk::Entity e);
453  const char *as_string(Chunk::Dim d);
454  const char *as_string(Chunk::Integer_Size d);
455  const char *as_string(Chunk::Float_Size d);
456 
457  std::ostream& operator << ( std::ostream& _os, const Header& _h );
458  std::ostream& operator << ( std::ostream& _os, const Chunk::Header& _c );
459 
461 } // namespace OMFormat
462 
463  // -------------------- (re-)store header
464 
465  template <> inline
466  size_t store( std::ostream& _os, const OMFormat::Header& _hdr, bool _swap)
467  { return _hdr.store( _os, _swap ); }
468 
469  template <> inline
470  size_t restore( std::istream& _is, OMFormat::Header& _hdr, bool _swap )
471  { return _hdr.restore( _is, _swap ); }
472 
473 
474  // -------------------- (re-)store chunk header
475 
476  template <> inline
477  size_t
478  store( std::ostream& _os, const OMFormat::Chunk::Header& _hdr, bool _swap)
479  {
480  OMFormat::uint16 val; val << _hdr;
481  return binary<uint16_t>::store( _os, val, _swap );
482  }
483 
484  template <> inline
485  size_t
486  restore( std::istream& _is, OMFormat::Chunk::Header& _hdr, bool _swap )
487  {
488  OMFormat::uint16 val;
489  size_t bytes = binary<uint16_t>::restore( _is, val, _swap );
490 
491  _hdr << val;
492 
493  return bytes;
494  }
495 
496  // -------------------- (re-)store integer with wanted number of bits (bytes)
497 
498  typedef GenProg::True t_signed;
499  typedef GenProg::False t_unsigned;
500 
501  // helper to store a an integer
502  template< typename T >
503  size_t
504  store( std::ostream& _os,
505  const T& _val,
506  OMFormat::Chunk::Integer_Size _b,
507  bool _swap,
508  t_signed);
509 
510  // helper to store a an unsigned integer
511  template< typename T >
512  size_t
513  store( std::ostream& _os,
514  const T& _val,
515  OMFormat::Chunk::Integer_Size _b,
516  bool _swap,
517  t_unsigned);
518 
520  template< typename T >
521  inline
522  size_t
523  store( std::ostream& _os,
524  const T& _val,
525  OMFormat::Chunk::Integer_Size _b,
526  bool _swap)
527  {
528  assert( OMFormat::is_integer( _val ) );
529 
530  if ( OMFormat::is_signed( _val ) )
531  return store( _os, _val, _b, _swap, t_signed() );
532  return store( _os, _val, _b, _swap, t_unsigned() );
533  }
534 
535  // helper to store a an integer
536  template< typename T > inline
537  size_t restore( std::istream& _is,
538  T& _val,
539  OMFormat::Chunk::Integer_Size _b,
540  bool _swap,
541  t_signed);
542 
543  // helper to store a an unsigned integer
544  template< typename T > inline
545  size_t restore( std::istream& _is,
546  T& _val,
547  OMFormat::Chunk::Integer_Size _b,
548  bool _swap,
549  t_unsigned);
550 
552  template< typename T >
553  inline
554  size_t
555  restore( std::istream& _is,
556  T& _val,
557  OMFormat::Chunk::Integer_Size _b,
558  bool _swap)
559  {
560  assert( OMFormat::is_integer( _val ) );
561 
562  if ( OMFormat::is_signed( _val ) )
563  return restore( _is, _val, _b, _swap, t_signed() );
564  return restore( _is, _val, _b, _swap, t_unsigned() );
565  }
566 
567 
568  //
569  // ---------------------------------------- storing vectors
570  template <typename VecT> inline
571  size_t store( std::ostream& _os, const VecT& _vec, GenProg::Int2Type<2>,
572  bool _swap )
573  {
574  size_t bytes = store( _os, _vec[0], _swap );
575  bytes += store( _os, _vec[1], _swap );
576  return bytes;
577  }
578 
579  template <typename VecT> inline
580  size_t store( std::ostream& _os, const VecT& _vec, GenProg::Int2Type<3>,
581  bool _swap )
582  {
583  size_t bytes = store( _os, _vec[0], _swap );
584  bytes += store( _os, _vec[1], _swap );
585  bytes += store( _os, _vec[2], _swap );
586  return bytes;
587  }
588 
589  template <typename VecT> inline
590  size_t store( std::ostream& _os, const VecT& _vec, GenProg::Int2Type<4>,
591  bool _swap )
592  {
593  size_t bytes = store( _os, _vec[0], _swap );
594  bytes += store( _os, _vec[1], _swap );
595  bytes += store( _os, _vec[2], _swap );
596  bytes += store( _os, _vec[3], _swap );
597  return bytes;
598  }
599 
600  template <typename VecT> inline
601  size_t store( std::ostream& _os, const VecT& _vec, GenProg::Int2Type<1>,
602  bool _swap )
603  {
604  return store( _os, _vec[0], _swap );
605  }
606 
608  template <typename VecT> inline
609  size_t vector_store( std::ostream& _os, const VecT& _vec, bool _swap )
610  {
611  return store( _os, _vec,
612  GenProg::Int2Type< vector_traits<VecT>::size_ >(),
613  _swap );
614  }
615 
616  // ---------------------------------------- restoring vectors
617  template <typename VecT>
618  inline
619  size_t
620  restore( std::istream& _is, VecT& _vec, GenProg::Int2Type<2>,
621  bool _swap )
622  {
623  size_t bytes = restore( _is, _vec[0], _swap );
624  bytes += restore( _is, _vec[1], _swap );
625  return bytes;
626  }
627 
628  template <typename VecT>
629  inline
630  size_t
631  restore( std::istream& _is, VecT& _vec, GenProg::Int2Type<3>,
632  bool _swap )
633  {
634  typedef typename vector_traits<VecT>::value_type scalar_type;
635  size_t bytes;
636 
637  bytes = binary<scalar_type>::restore( _is, _vec[0], _swap );
638  bytes += binary<scalar_type>::restore( _is, _vec[1], _swap );
639  bytes += binary<scalar_type>::restore( _is, _vec[2], _swap );
640  return bytes;
641  }
642 
643  template <typename VecT>
644  inline
645  size_t
646  restore( std::istream& _is, VecT& _vec, GenProg::Int2Type<4>,
647  bool _swap )
648  {
649  typedef typename vector_traits<VecT>::value_type scalar_type;
650  size_t bytes;
651 
652  bytes = binary<scalar_type>::restore( _is, _vec[0], _swap );
653  bytes += binary<scalar_type>::restore( _is, _vec[1], _swap );
654  bytes += binary<scalar_type>::restore( _is, _vec[2], _swap );
655  bytes += binary<scalar_type>::restore( _is, _vec[3], _swap );
656  return bytes;
657  }
658 
659  template <typename VecT>
660  inline
661  size_t
662  restore( std::istream& _is, VecT& _vec, GenProg::Int2Type<1>,
663  bool _swap )
664  {
665  return restore( _is, _vec[0], _swap );
666  }
667 
669  template <typename VecT>
670  inline
671  size_t
672  vector_restore( std::istream& _is, VecT& _vec, bool _swap )
673  {
674  return restore( _is, _vec,
675  GenProg::Int2Type< vector_traits<VecT>::size_ >(),
676  _swap );
677  }
678 
679 
680  // ---------------------------------------- storing property names
681 
682  template <>
683  inline
684  size_t store( std::ostream& _os, const OMFormat::Chunk::PropertyName& _pn,
685  bool _swap )
686  {
687  store( _os, _pn.size(), OMFormat::Chunk::Integer_8, _swap ); // 1 byte
688  if ( _pn.size() )
689  _os.write( _pn.c_str(), _pn.size() ); // size bytes
690  return _pn.size() + 1;
691  }
692 
693  template <>
694  inline
695  size_t restore( std::istream& _is, OMFormat::Chunk::PropertyName& _pn,
696  bool _swap )
697  {
698  size_t size;
699 
700  restore( _is, size, OMFormat::Chunk::Integer_8, _swap); // 1 byte
701 
702  assert( OMFormat::Chunk::PropertyName::is_valid( size ) );
703 
704  if ( size > 0 )
705  {
706  char buf[256];
707  _is.read( buf, size ); // size bytes
708  buf[size] = '\0';
709  _pn.resize(size);
710  _pn = buf;
711  }
712  return size+1;
713  }
714 
715 //=============================================================================
716 } // namespace IO
717 } // namespace OpenMesh
718 #endif
719 //=============================================================================
720 #if defined(OM_MISSING_HEADER_LIMITS)
721 # undef OM_MISSING_HEADER_LIMITS
722 #endif
723 //=============================================================================
724 #if defined(OM_INCLUDE_TEMPLATES) && !defined(OPENMESH_IO_OMFORMAT_CC)
725 # define OPENMESH_IO_OMFORMAT_TEMPLATES
726 # include "OMFormatT.cc"
727 #endif
728 //=============================================================================
729 #endif
730 //=============================================================================

acg pic Project OpenMesh, ©  Computer Graphics Group, RWTH Aachen. Documentation generated using doxygen .