OMFormat.hh 21.4 KB
Newer Older
Jan Möbius's avatar
Jan Möbius committed
1 2 3
/*===========================================================================*\
 *                                                                           *
 *                               OpenMesh                                    *
4
 *      Copyright (C) 2001-2009 by Computer Graphics Group, RWTH Aachen      *
Jan Möbius's avatar
Jan Möbius committed
5 6
 *                           www.openmesh.org                                *
 *                                                                           *
7
 *---------------------------------------------------------------------------*
8
 *  This file is part of OpenMesh.                                           *
Jan Möbius's avatar
Jan Möbius committed
9
 *                                                                           *
10
 *  OpenMesh is free software: you can redistribute it and/or modify         *
11 12 13 14
 *  it under the terms of the GNU Lesser General Public License as           *
 *  published by the Free Software Foundation, either version 3 of           *
 *  the License, or (at your option) any later version with the              *
 *  following exceptions:                                                    *
Jan Möbius's avatar
Jan Möbius committed
15
 *                                                                           *
16 17 18 19 20 21 22
 *  If other files instantiate templates or use macros                       *
 *  or inline functions from this file, or you compile this file and         *
 *  link it with other files to produce an executable, this file does        *
 *  not by itself cause the resulting executable to be covered by the        *
 *  GNU Lesser General Public License. This exception does not however       *
 *  invalidate any other reasons why the executable file might be            *
 *  covered by the GNU Lesser General Public License.                        *
Jan Möbius's avatar
Jan Möbius committed
23
 *                                                                           *
24 25 26 27
 *  OpenMesh is distributed in the hope that it will be useful,              *
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of           *
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the            *
 *  GNU Lesser General Public License for more details.                      *
Jan Möbius's avatar
Jan Möbius committed
28
 *                                                                           *
29 30 31 32
 *  You should have received a copy of the GNU LesserGeneral Public          *
 *  License along with OpenMesh.  If not,                                    *
 *  see <http://www.gnu.org/licenses/>.                                      *
 *                                                                           *
33
\*===========================================================================*/
34 35

/*===========================================================================*\
36
 *                                                                           *
37 38
 *   $Revision$                                                         *
 *   $Date$                   *
Jan Möbius's avatar
Jan Möbius committed
39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124
 *                                                                           *
\*===========================================================================*/


#ifndef OPENMESH_IO_OMFORMAT_HH
#define OPENMESH_IO_OMFORMAT_HH


//=== INCLUDES ================================================================

#include <OpenMesh/Core/System/config.h>
#include <OpenMesh/Core/System/omstream.hh>
#include <OpenMesh/Core/IO/SR_store.hh>
#include <OpenMesh/Core/Utils/GenProg.hh>
#include <OpenMesh/Core/Utils/Endian.hh>
#include <OpenMesh/Core/Utils/vector_traits.hh>
// --------------------
#include <iostream>
#if defined(OM_CC_GCC) && (OM_GCC_VERSION < 30000)
#  include <OpenMesh/Tools/Utils/NumLimitsT.hh>
#  define OM_MISSING_HEADER_LIMITS 1
#else
#  include <limits>
#endif


//== NAMESPACES ==============================================================

#ifndef DOXY_IGNORE_THIS
namespace OpenMesh {
namespace IO   {
namespace OMFormat {


//=== IMPLEMENTATION ==========================================================


/** \name Mesh Reading / Writing
*/
//@{

//-----------------------------------------------------------------------------

  // <:Header>
  // <:Comment>
  // Chunk 0
  //   <:ChunkHeader>
  //   <:Comment>
  //   data
  // Chunk 1
  //   <:ChunkHeader>
  //   <:Comment>
  //   data
  // .
  // .
  // .
  // Chunk N

  //
  // NOTICE!
  //
  // The usage of data types who differ in size
  // on different pc architectures (32/64 bit) and/or
  // operating systems, e.g. (unsigned) long, size_t,
  // is not recommended because of inconsistencies
  // in case of cross writing and reading.
  //
  // Basic types that are supported are:


  typedef unsigned char      uchar;
  typedef uint8_t            uint8;
  typedef uint16_t           uint16;
  typedef uint32_t           uint32;
  typedef uint64_t           uint64;
  typedef int8_t             int8;
  typedef int16_t            int16;
  typedef int32_t            int32;
  typedef int64_t            int64;
  typedef float32_t          float32;
  typedef float64_t          float64;

  struct Header
  {
    uchar  magic_[2]; // OM
    uchar  mesh_;    // [T]riangles, [Q]uads, [P]olygonals
125
    uint8  version_;
Jan Möbius's avatar
Jan Möbius committed
126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174
    uint32 n_vertices_;
    uint32 n_faces_;
    uint32 n_edges_;

    size_t store( std::ostream& _os, bool _swap ) const
    {
      _os.write( (char*)this, 4); // magic_, mesh_, version_
      size_t bytes = 4;
      bytes += binary<uint32_t>::store( _os, n_vertices_, _swap );
      bytes += binary<uint32_t>::store( _os, n_faces_, _swap );
      bytes += binary<uint32_t>::store( _os, n_edges_, _swap );
      return bytes;
    }

    size_t restore( std::istream& _is, bool _swap )
    {
      if (_is.read( (char*)this, 4 ).eof())
	return 0;

      size_t bytes = 4;
      bytes += binary<uint32_t>::restore( _is, n_vertices_, _swap );
      bytes += binary<uint32_t>::restore( _is, n_faces_, _swap );
      bytes += binary<uint32_t>::restore( _is, n_edges_, _swap );
      return bytes;
    }

  };

  struct Chunk
  {
    // Hardcoded this size to an uint32 to make the system 32/64 bit compatible.
    // Needs further investigation!
    typedef uint32 esize_t; // element size, used for custom properties

    enum Type {
      Type_Pos       = 0x00,
      Type_Normal    = 0x01,
      Type_Texcoord  = 0x02,
      Type_Status    = 0x03,
      Type_Color     = 0x04,
      Type_Custom    = 0x06,
      Type_Topology  = 0x07
    };

    enum Entity {
      Entity_Vertex    = 0x00,
      Entity_Mesh      = 0x01,
      Entity_Face      = 0x02,
      Entity_Edge      = 0x04,
Jan Möbius's avatar
Jan Möbius committed
175
      Entity_Halfedge  = 0x06
Jan Möbius's avatar
Jan Möbius committed
176 177 178 179 180 181 182 183 184 185 186 187 188
    };

    enum Dim {
      Dim_1D = 0x00,
      Dim_2D = 0x01,
      Dim_3D = 0x02,
      Dim_4D = 0x03,
      Dim_5D = 0x04,
      Dim_6D = 0x05,
      Dim_7D = 0x06,
      Dim_8D = 0x07
    };

189
    enum Integer_Size {
Jan Möbius's avatar
Jan Möbius committed
190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230
      Integer_8   = 0x00, // 1 byte for (unsigned) char
      Integer_16  = 0x01, // 2 bytes for short
      Integer_32  = 0x02, // 4 bytes for long
      Integer_64  = 0x03  // 8 bytes for long long
    };

    enum Float_Size {
      Float_32  = 0x00, //  4 bytes for float
      Float_64  = 0x01, //  8 bytes for double
      Float_128 = 0x02  // 16 bytes for long double (an assumption!)
    };

    static const int SIZE_RESERVED = 1; //  1
    static const int SIZE_NAME     = 1; //  2
    static const int SIZE_ENTITY   = 3; //  5
    static const int SIZE_TYPE     = 4; //  9

    static const int SIZE_SIGNED   = 1; // 10
    static const int SIZE_FLOAT    = 1; // 11
    static const int SIZE_DIM      = 3; // 14
    static const int SIZE_BITS     = 2; // 16

    static const int OFF_RESERVED = 0;                          //  0
    static const int OFF_NAME     = SIZE_RESERVED + OFF_RESERVED; //  2
    static const int OFF_ENTITY   = SIZE_NAME     + OFF_NAME;   //  3
    static const int OFF_TYPE     = SIZE_ENTITY   + OFF_ENTITY; //  5
    static const int OFF_SIGNED   = SIZE_TYPE     + OFF_TYPE;   //  9
    static const int OFF_FLOAT    = SIZE_SIGNED   + OFF_SIGNED; // 10
    static const int OFF_DIM      = SIZE_FLOAT    + OFF_FLOAT;  // 11
    static const int OFF_BITS     = SIZE_DIM      + OFF_DIM;    // 14

    // !Attention! When changing the bit size, the operators
    // << (uint16, Header) and << (Header, uint16) must be changed as well
    //
    // Entries signed_, float_, dim_, bits_ are not used when type_
    // equals Type_Custom
    //
    struct Header // 16 bits long
    {
      unsigned reserved_: SIZE_RESERVED;
      unsigned name_    : SIZE_NAME;   // 1 named property, 0 anonymous
231
      unsigned entity_  : SIZE_ENTITY; // 0 vertex, 1 mesh, 2 edge,
Jan Möbius's avatar
Jan Möbius committed
232
                                       // 4 halfedge, 6 face
233
      unsigned type_    : SIZE_TYPE;   // 0 pos, 1 normal, 2 texcoord,
Jan Möbius's avatar
Jan Möbius committed
234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297
                                       // 3 status, 4 color 6 custom 7 topology
      unsigned signed_  : SIZE_SIGNED; // bool
      unsigned float_   : SIZE_FLOAT;  // bool
      unsigned dim_     : SIZE_DIM;    // 0 1D, 1 2D, 2 3D, .., 7 8D
      unsigned bits_    : SIZE_BITS;   // {8, 16, 32, 64} | {32, 64, 128}
                                       // (integer)         (float)
      unsigned unused_  : 16; // fill up to 32 bits
    }; // struct Header


    class PropertyName : public std::string
    {
    public:

      static const size_t size_max = 256;

      PropertyName( ) { }

      PropertyName( const std::string& _name ) { *this = _name; }

      bool is_valid() const { return is_valid( size() ); }

      static bool is_valid( size_t _s ) { return _s <= size_max; }

      PropertyName& operator = ( const std::string& _rhs )
      {
	assert( is_valid( _rhs.size() ) );

	if ( is_valid( _rhs.size() ) )
	  std::string::operator = ( _rhs );
	else
	{
	  omerr() << "Warning! Property name too long. Will be shortened!\n";
	  this->std::string::operator = ( _rhs.substr(0, size_max) );
	}

	return *this;
      }

    };

  }; // Chunk

  // ------------------------------------------------------------ Helper

  // -------------------- get size information

  /// Return size of header in bytes.
  inline size_t header_size(void) { return sizeof(Header); }


  /// Return size of chunk header in bytes.
  inline size_t chunk_header_size( void ) { return sizeof(uint16); }


  /// Return the size of a scale in bytes.
  inline size_t scalar_size( const Chunk::Header& _hdr )
  {
    return _hdr.float_ ? (0x01 << _hdr.bits_) : (0x04 << _hdr.bits_);
  }


  /// Return the dimension of the vector in a chunk
  inline size_t dimensions(const Chunk::Header& _chdr) { return _chdr.dim_+1; }
298

Jan Möbius's avatar
Jan Möbius committed
299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339

  /// Return the size of a vector in bytes.
  inline size_t vector_size( const Chunk::Header& _chdr )
  {
    return dimensions(_chdr)*scalar_size(_chdr);
  }


  /// Return the size of chunk data in bytes
  inline size_t chunk_data_size( Header& _hdr,  Chunk::Header& _chunk_hdr )
  {
    size_t C     = 0;

    switch( _chunk_hdr.entity_ )
    {
      case Chunk::Entity_Vertex:   C  = _hdr.n_vertices_; break;
      case Chunk::Entity_Face:     C  = _hdr.n_faces_;    break;
      case Chunk::Entity_Halfedge: C  = _hdr.n_edges_;    // no break!
      case Chunk::Entity_Edge:     C += _hdr.n_edges_;    break;
      case Chunk::Entity_Mesh:     C  = 1;                break;
      default:
	std::cerr << "Invalid value in _chunk_hdr.entity_\n";
	assert( false );
    }

    return C * vector_size( _chunk_hdr );
  }

  inline size_t chunk_size( Header& _hdr, Chunk::Header& _chunk_hdr )
  {
    return chunk_header_size() + chunk_data_size( _hdr, _chunk_hdr );
  }

  // -------------------- convert from Chunk::Header to storage type

  uint16& operator << (uint16& val, const Chunk::Header& hdr);
  Chunk::Header& operator << (Chunk::Header& hdr, const uint16 val);


  // -------------------- type information

340 341
  template <typename T> bool is_float(const T&)
  {
Jan Möbius's avatar
Jan Möbius committed
342 343 344
#if defined(OM_MISSING_HEADER_LIMITS)
    return !Utils::NumLimitsT<T>::is_integer();
#else
345
    return !std::numeric_limits<T>::is_integer;
Jan Möbius's avatar
Jan Möbius committed
346 347 348
#endif
  }

349 350
  template <typename T> bool is_integer(const T)
  {
Jan Möbius's avatar
Jan Möbius committed
351 352 353
#if defined(OM_MISSING_HEADER_LIMITS)
    return Utils::NumLimitsT<T>::is_integer();
#else
354
    return std::numeric_limits<T>::is_integer;
Jan Möbius's avatar
Jan Möbius committed
355 356 357
#endif
  }

358 359
  template <typename T> bool is_signed(const T&)
  {
Jan Möbius's avatar
Jan Möbius committed
360 361 362
#if defined(OM_MISSING_HEADER_LIMITS)
    return Utils::NumLimitsT<T>::is_signed();
#else
363
    return std::numeric_limits<T>::is_signed;
Jan Möbius's avatar
Jan Möbius committed
364 365 366 367 368 369 370 371 372 373 374 375 376
#endif
  }

  // -------------------- conversions (format type <- type/value)

  template <typename VecType>
  inline
  Chunk::Dim dim( VecType )
  {
    assert( vector_traits< VecType >::size() < 9 );
    return static_cast<Chunk::Dim>(vector_traits< VecType >::size() - 1);
  }

377
  template <typename VecType>
Jan Möbius's avatar
Jan Möbius committed
378 379 380 381 382 383 384 385 386 387 388
  inline
  Chunk::Dim dim( const Chunk::Header& _hdr )
  {
    return static_cast<Chunk::Dim>( _hdr.dim_ );
  }

  // calc minimum (power-of-2) number of bits needed
  Chunk::Integer_Size needed_bits( size_t s );


  // Return the storage type (Chunk::Header::bits_)
389
  template <typename T>
Jan Möbius's avatar
Jan Möbius committed
390 391 392
  inline
  unsigned int bits(const T& val)
  {
393
    return is_integer(val)
Jan Möbius's avatar
Jan Möbius committed
394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417
      ? (static_cast<unsigned int>(integer_size(val)))
      : (static_cast<unsigned int>(float_size(val)));
  }

  // Convert size of type to Integer_Size
#ifdef NDEBUG
  template <typename T> Chunk::Integer_Size integer_size(const T&)
#else
  template <typename T> Chunk::Integer_Size integer_size(const T& d)
#endif
  {
    assert( is_integer(d) );

    switch( sizeof(T) )
    {
      case  1: return OMFormat::Chunk::Integer_8;
      case  2: return OMFormat::Chunk::Integer_16;
      case  4: return OMFormat::Chunk::Integer_32;
      case  8: return OMFormat::Chunk::Integer_64;
    }
    return Chunk::Integer_Size(0);
  }


418
  // Convert size of type to FLoat_Size
Jan Möbius's avatar
Jan Möbius committed
419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441
#ifdef NDEBUG
  template <typename T> Chunk::Float_Size float_size(const T&)
#else
  template <typename T> Chunk::Float_Size float_size(const T& d)
#endif
  {
    assert( is_float(d) );

    switch( sizeof(T) )
    {
      case  4: return OMFormat::Chunk::Float_32;
      case  8: return OMFormat::Chunk::Float_64;
      case 16: return OMFormat::Chunk::Float_128;
    }
    return Chunk::Float_Size(0);
  }

  // -------------------- create/read version

  inline uint8 mk_version(const uint16 major, const uint16 minor)
  { return (major & 0x07) << 5 | (minor & 0x1f); }


442
  inline uint16 major_version(const uint8 version)
Jan Möbius's avatar
Jan Möbius committed
443 444 445
  { return (version >> 5) & 0x07; }


446
  inline uint16 minor_version(const uint8 version)
Jan Möbius's avatar
Jan Möbius committed
447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477
  { return (version & 0x001f); }


  // ---------------------------------------- convenience functions

  const char *as_string(Chunk::Type t);
  const char *as_string(Chunk::Entity e);
  const char *as_string(Chunk::Dim d);
  const char *as_string(Chunk::Integer_Size d);
  const char *as_string(Chunk::Float_Size d);

  std::ostream& operator << ( std::ostream& _os, const Header& _h );
  std::ostream& operator << ( std::ostream& _os, const Chunk::Header& _c );

//@}
} // namespace OMFormat

  // -------------------- (re-)store header

  template <> inline
  size_t store( std::ostream& _os, const OMFormat::Header& _hdr, bool _swap)
  { return _hdr.store( _os, _swap ); }

  template <> inline
  size_t restore( std::istream& _is, OMFormat::Header& _hdr, bool _swap )
  { return _hdr.restore( _is, _swap ); }


  // -------------------- (re-)store chunk header

  template <> inline
478
  size_t
Jan Möbius's avatar
Jan Möbius committed
479 480 481 482 483 484 485
  store( std::ostream& _os, const OMFormat::Chunk::Header& _hdr, bool _swap)
  {
    OMFormat::uint16 val; val << _hdr;
    return binary<uint16_t>::store( _os, val, _swap );
  }

  template <> inline
486
  size_t
Jan Möbius's avatar
Jan Möbius committed
487 488 489 490 491 492
  restore( std::istream& _is, OMFormat::Chunk::Header& _hdr, bool _swap )
  {
    OMFormat::uint16 val;
    size_t bytes = binary<uint16_t>::restore( _is, val, _swap );

    _hdr << val;
493

Jan Möbius's avatar
Jan Möbius committed
494 495 496 497 498 499 500 501 502
    return bytes;
  }

  // -------------------- (re-)store integer with wanted number of bits (bytes)

  typedef GenProg::True  t_signed;
  typedef GenProg::False t_unsigned;

  // helper to store a an integer
503 504 505 506 507
  template< typename T >
  size_t
  store( std::ostream& _os,
	 const T& _val,
	 OMFormat::Chunk::Integer_Size _b,
Jan Möbius's avatar
Jan Möbius committed
508 509 510 511
	 bool _swap,
	 t_signed);

  // helper to store a an unsigned integer
512 513 514 515 516
  template< typename T >
  size_t
  store( std::ostream& _os,
	 const T& _val,
	 OMFormat::Chunk::Integer_Size _b,
Jan Möbius's avatar
Jan Möbius committed
517 518 519 520
	 bool _swap,
	 t_unsigned);

  /// Store an integer with a wanted number of bits
521
  template< typename T >
Jan Möbius's avatar
Jan Möbius committed
522
  inline
523 524 525 526
  size_t
  store( std::ostream& _os,
	 const T& _val,
	 OMFormat::Chunk::Integer_Size _b,
Jan Möbius's avatar
Jan Möbius committed
527
	 bool _swap)
528
  {
Jan Möbius's avatar
Jan Möbius committed
529 530 531 532 533 534 535 536 537
    assert( OMFormat::is_integer( _val ) );

    if ( OMFormat::is_signed( _val ) )
      return store( _os, _val, _b, _swap, t_signed()   );
    return   store( _os, _val, _b, _swap, t_unsigned() );
  }

  // helper to store a an integer
  template< typename T > inline
538 539 540
  size_t restore( std::istream& _is,
		  T& _val,
		  OMFormat::Chunk::Integer_Size _b,
Jan Möbius's avatar
Jan Möbius committed
541 542 543 544 545
		  bool _swap,
		  t_signed);

  // helper to store a an unsigned integer
  template< typename T > inline
546 547 548
  size_t restore( std::istream& _is,
		  T& _val,
		  OMFormat::Chunk::Integer_Size _b,
Jan Möbius's avatar
Jan Möbius committed
549 550 551 552
		  bool _swap,
		  t_unsigned);

  /// Restore an integer with a wanted number of bits
553
  template< typename T >
Jan Möbius's avatar
Jan Möbius committed
554
  inline
555 556 557 558
  size_t
  restore( std::istream& _is,
	   T& _val,
	   OMFormat::Chunk::Integer_Size _b,
Jan Möbius's avatar
Jan Möbius committed
559
	   bool _swap)
560
  {
Jan Möbius's avatar
Jan Möbius committed
561
    assert( OMFormat::is_integer( _val ) );
562

Jan Möbius's avatar
Jan Möbius committed
563 564
    if ( OMFormat::is_signed( _val ) )
      return restore( _is, _val, _b, _swap, t_signed() );
565
    return restore( _is, _val, _b, _swap, t_unsigned() );
Jan Möbius's avatar
Jan Möbius committed
566 567 568
  }


569
  //
Jan Möbius's avatar
Jan Möbius committed
570 571
  // ---------------------------------------- storing vectors
  template <typename VecT> inline
572
  size_t store( std::ostream& _os, const VecT& _vec, GenProg::Int2Type<2>,
Jan Möbius's avatar
Jan Möbius committed
573 574 575 576 577 578 579 580
		bool _swap )
  {
    size_t bytes =  store( _os, _vec[0], _swap );
    bytes += store( _os, _vec[1], _swap );
    return bytes;
  }

  template <typename VecT> inline
581
  size_t store( std::ostream& _os, const VecT& _vec, GenProg::Int2Type<3>,
Jan Möbius's avatar
Jan Möbius committed
582 583 584 585 586 587 588 589 590
		bool _swap )
  {
    size_t bytes =  store( _os, _vec[0], _swap );
    bytes += store( _os, _vec[1], _swap );
    bytes += store( _os, _vec[2], _swap );
    return bytes;
  }

  template <typename VecT> inline
591
  size_t store( std::ostream& _os, const VecT& _vec, GenProg::Int2Type<4>,
Jan Möbius's avatar
Jan Möbius committed
592 593 594 595 596 597 598 599 600 601
		bool _swap )
  {
    size_t bytes =  store( _os, _vec[0], _swap );
    bytes += store( _os, _vec[1], _swap );
    bytes += store( _os, _vec[2], _swap );
    bytes += store( _os, _vec[3], _swap );
    return bytes;
  }

  template <typename VecT> inline
602
  size_t store( std::ostream& _os, const VecT& _vec, GenProg::Int2Type<1>,
Jan Möbius's avatar
Jan Möbius committed
603 604 605 606
		bool _swap )
  {
    return store( _os, _vec[0], _swap );
  }
607

Jan Möbius's avatar
Jan Möbius committed
608 609 610 611
  /// storing a vector type
  template <typename VecT> inline
  size_t vector_store( std::ostream& _os, const VecT& _vec, bool _swap )
  {
612
    return store( _os, _vec,
Jan Möbius's avatar
Jan Möbius committed
613 614 615 616 617 618 619 620
		  GenProg::Int2Type< vector_traits<VecT>::size_ >(),
		  _swap );
  }

  // ---------------------------------------- restoring vectors
  template <typename VecT>
  inline
  size_t
621
  restore( std::istream& _is, VecT& _vec, GenProg::Int2Type<2>,
Jan Möbius's avatar
Jan Möbius committed
622 623 624 625 626 627 628 629 630 631
	 bool _swap )
  {
    size_t bytes =  restore( _is, _vec[0], _swap );
    bytes += restore( _is, _vec[1], _swap );
    return bytes;
  }

  template <typename VecT>
  inline
  size_t
632
  restore( std::istream& _is, VecT& _vec, GenProg::Int2Type<3>,
Jan Möbius's avatar
Jan Möbius committed
633 634 635 636 637 638 639 640 641 642 643 644 645 646
	 bool _swap )
  {
    typedef typename vector_traits<VecT>::value_type scalar_type;
    size_t bytes;

    bytes  = binary<scalar_type>::restore( _is, _vec[0], _swap );
    bytes += binary<scalar_type>::restore( _is, _vec[1], _swap );
    bytes += binary<scalar_type>::restore( _is, _vec[2], _swap );
    return bytes;
  }

  template <typename VecT>
  inline
  size_t
647
  restore( std::istream& _is, VecT& _vec, GenProg::Int2Type<4>,
Jan Möbius's avatar
Jan Möbius committed
648 649 650 651 652 653 654 655 656 657 658
	   bool _swap )
  {
    typedef typename vector_traits<VecT>::value_type scalar_type;
    size_t bytes;

    bytes  = binary<scalar_type>::restore( _is, _vec[0], _swap );
    bytes += binary<scalar_type>::restore( _is, _vec[1], _swap );
    bytes += binary<scalar_type>::restore( _is, _vec[2], _swap );
    bytes += binary<scalar_type>::restore( _is, _vec[3], _swap );
    return bytes;
  }
659

Jan Möbius's avatar
Jan Möbius committed
660
  template <typename VecT>
661
  inline
Jan Möbius's avatar
Jan Möbius committed
662
  size_t
663
  restore( std::istream& _is, VecT& _vec, GenProg::Int2Type<1>,
Jan Möbius's avatar
Jan Möbius committed
664 665 666 667 668 669 670 671 672 673 674
	 bool _swap )
  {
    return restore( _is, _vec[0], _swap );
  }

  /// Restoring a vector type
  template <typename VecT>
  inline
  size_t
  vector_restore( std::istream& _is, VecT& _vec, bool _swap )
  {
675
    return restore( _is, _vec,
Jan Möbius's avatar
Jan Möbius committed
676 677 678 679 680 681
		    GenProg::Int2Type< vector_traits<VecT>::size_ >(),
		    _swap );
  }


  // ---------------------------------------- storing property names
682

Jan Möbius's avatar
Jan Möbius committed
683 684
  template <>
  inline
685
  size_t store( std::ostream& _os, const OMFormat::Chunk::PropertyName& _pn,
Jan Möbius's avatar
Jan Möbius committed
686 687 688 689 690 691 692 693 694 695
		bool _swap )
  {
    store( _os, _pn.size(), OMFormat::Chunk::Integer_8, _swap ); // 1 byte
    if ( _pn.size() )
      _os.write( _pn.c_str(), _pn.size() ); // size bytes
    return _pn.size() + 1;
  }

  template <>
  inline
696
  size_t restore( std::istream& _is, OMFormat::Chunk::PropertyName& _pn,
Jan Möbius's avatar
Jan Möbius committed
697
		  bool _swap )
698
  {
Jan Möbius's avatar
Jan Möbius committed
699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731
    size_t size;

    restore( _is, size, OMFormat::Chunk::Integer_8, _swap); // 1 byte

    assert( OMFormat::Chunk::PropertyName::is_valid( size ) );

    if ( size > 0 )
    {
      char buf[256];
      _is.read( buf, size ); // size bytes
      buf[size] = '\0';
      _pn.resize(size);
      _pn = buf;
    }
    return size+1;
  }

//=============================================================================
} // namespace IO
} // namespace OpenMesh
#endif
//=============================================================================
#if defined(OM_MISSING_HEADER_LIMITS)
#  undef OM_MISSING_HEADER_LIMITS
#endif
//=============================================================================
#if defined(OM_INCLUDE_TEMPLATES) && !defined(OPENMESH_IO_OMFORMAT_CC)
#  define OPENMESH_IO_OMFORMAT_TEMPLATES
#  include "OMFormatT.cc"
#endif
//=============================================================================
#endif
//=============================================================================