OMFormat.hh 22.2 KB
Newer Older
Jan Möbius's avatar
Jan Möbius committed
1
/* ========================================================================= *
Jan Möbius's avatar
Jan Möbius committed
2 3
 *                                                                           *
 *                               OpenMesh                                    *
Jan Möbius's avatar
Jan Möbius committed
4
 *           Copyright (c) 2001-2015, RWTH-Aachen University                 *
Jan Möbius's avatar
Typo  
Jan Möbius committed
5
 *           Department of Computer Graphics and Multimedia                  *
Jan Möbius's avatar
Jan Möbius committed
6 7
 *                          All rights reserved.                             *
 *                            www.openmesh.org                               *
Jan Möbius's avatar
Jan Möbius committed
8
 *                                                                           *
9
 *---------------------------------------------------------------------------*
Jan Möbius's avatar
Jan Möbius committed
10 11
 * This file is part of OpenMesh.                                            *
 *---------------------------------------------------------------------------*
Jan Möbius's avatar
Jan Möbius committed
12
 *                                                                           *
Jan Möbius's avatar
Jan Möbius committed
13 14 15
 * Redistribution and use in source and binary forms, with or without        *
 * modification, are permitted provided that the following conditions        *
 * are met:                                                                  *
Jan Möbius's avatar
Jan Möbius committed
16
 *                                                                           *
Jan Möbius's avatar
Jan Möbius committed
17 18
 * 1. Redistributions of source code must retain the above copyright notice, *
 *    this list of conditions and the following disclaimer.                  *
Jan Möbius's avatar
Jan Möbius committed
19
 *                                                                           *
Jan Möbius's avatar
Jan Möbius committed
20 21 22
 * 2. Redistributions in binary form must reproduce the above copyright      *
 *    notice, this list of conditions and the following disclaimer in the    *
 *    documentation and/or other materials provided with the distribution.   *
Jan Möbius's avatar
Jan Möbius committed
23
 *                                                                           *
Jan Möbius's avatar
Jan Möbius committed
24 25 26
 * 3. Neither the name of the copyright holder nor the names of its          *
 *    contributors may be used to endorse or promote products derived from   *
 *    this software without specific prior written permission.               *
27
 *                                                                           *
Jan Möbius's avatar
Jan Möbius committed
28 29 30 31 32 33 34 35 36 37 38
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS       *
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED *
 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A           *
 * PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER *
 * OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,  *
 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,       *
 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR        *
 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF    *
 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING      *
 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS        *
 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.              *
Jan Möbius's avatar
Jan Möbius committed
39 40
 *                                                                           *
 * ========================================================================= */
41 42

/*===========================================================================*\
43
 *                                                                           *
44 45
 *   $Revision$                                                         *
 *   $Date$                   *
Jan Möbius's avatar
Jan Möbius committed
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 125 126 127 128 129 130 131
 *                                                                           *
\*===========================================================================*/


#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
132
    uint8  version_;
Jan Möbius's avatar
Jan Möbius committed
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 175 176 177 178 179 180 181
    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
182
      Entity_Halfedge  = 0x06
Jan Möbius's avatar
Jan Möbius committed
183 184 185 186 187 188 189 190 191 192 193 194 195
    };

    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
    };

196
    enum Integer_Size {
Jan Möbius's avatar
Jan Möbius committed
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 231 232 233 234 235 236 237
      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
238
      unsigned entity_  : SIZE_ENTITY; // 0 vertex, 1 mesh, 2 edge,
Jan Möbius's avatar
Jan Möbius committed
239
                                       // 4 halfedge, 6 face
240
      unsigned type_    : SIZE_TYPE;   // 0 pos, 1 normal, 2 texcoord,
Jan Möbius's avatar
Jan Möbius committed
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 298 299 300 301 302 303 304
                                       // 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; }
305

Jan Möbius's avatar
Jan Möbius committed
306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326

  /// 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:
327 328 329
        std::cerr << "Invalid value in _chunk_hdr.entity_\n";
        assert( false );
        break;
Jan Möbius's avatar
Jan Möbius committed
330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347
    }

    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

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

357 358
  template <typename T> bool is_integer(const T)
  {
Jan Möbius's avatar
Jan Möbius committed
359 360 361
#if defined(OM_MISSING_HEADER_LIMITS)
    return Utils::NumLimitsT<T>::is_integer();
#else
362
    return std::numeric_limits<T>::is_integer;
Jan Möbius's avatar
Jan Möbius committed
363 364 365
#endif
  }

366 367
  template <typename T> bool is_signed(const T&)
  {
Jan Möbius's avatar
Jan Möbius committed
368 369 370
#if defined(OM_MISSING_HEADER_LIMITS)
    return Utils::NumLimitsT<T>::is_signed();
#else
371
    return std::numeric_limits<T>::is_signed;
Jan Möbius's avatar
Jan Möbius committed
372 373 374 375 376 377 378 379 380 381 382 383 384
#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);
  }

385
  template <typename VecType>
Jan Möbius's avatar
Jan Möbius committed
386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401
  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 );

  // 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
  {
David Bommes's avatar
David Bommes committed
402
#ifndef NDEBUG
Jan Möbius's avatar
Jan Möbius committed
403
    assert( is_integer(d) );
David Bommes's avatar
David Bommes committed
404
#endif
Jan Möbius's avatar
Jan Möbius committed
405 406 407 408 409 410 411

    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;
412 413 414 415
      default:
        std::cerr << "Invalid value in integer_size\n";
        assert( false );
        break;
Jan Möbius's avatar
Jan Möbius committed
416 417 418 419 420
    }
    return Chunk::Integer_Size(0);
  }


421
  // Convert size of type to FLoat_Size
Jan Möbius's avatar
Jan Möbius committed
422 423 424 425 426 427
#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
  {
David Bommes's avatar
David Bommes committed
428
#ifndef NDEBUG
Jan Möbius's avatar
Jan Möbius committed
429
    assert( is_float(d) );
David Bommes's avatar
David Bommes committed
430
#endif
Jan Möbius's avatar
Jan Möbius committed
431 432 433 434 435 436

    switch( sizeof(T) )
    {
      case  4: return OMFormat::Chunk::Float_32;
      case  8: return OMFormat::Chunk::Float_64;
      case 16: return OMFormat::Chunk::Float_128;
437 438 439 440
      default:
        std::cerr << "Invalid value in float_size\n";
        assert( false );
        break;
Jan Möbius's avatar
Jan Möbius committed
441 442 443 444
    }
    return Chunk::Float_Size(0);
  }

445 446 447 448 449 450 451 452 453 454
  // Return the storage type (Chunk::Header::bits_)
  template <typename T>
  inline
  unsigned int bits(const T& val)
  {
    return is_integer(val)
      ? (static_cast<unsigned int>(integer_size(val)))
      : (static_cast<unsigned int>(float_size(val)));
  }

Jan Möbius's avatar
Jan Möbius committed
455 456 457 458 459 460
  // -------------------- create/read version

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


461
  inline uint16 major_version(const uint8 version)
Jan Möbius's avatar
Jan Möbius committed
462 463 464
  { return (version >> 5) & 0x07; }


465
  inline uint16 minor_version(const uint8 version)
Jan Möbius's avatar
Jan Möbius committed
466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496
  { 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
497
  size_t
Jan Möbius's avatar
Jan Möbius committed
498 499 500 501 502 503 504
  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
505
  size_t
Jan Möbius's avatar
Jan Möbius committed
506 507 508 509 510 511
  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;
512

Jan Möbius's avatar
Jan Möbius committed
513 514 515 516 517
    return bytes;
  }

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

518 519
  typedef GenProg::TrueType  t_signed;
  typedef GenProg::FalseType t_unsigned;
Jan Möbius's avatar
Jan Möbius committed
520 521

  // helper to store a an integer
522 523 524 525 526
  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
527 528 529 530
	 bool _swap,
	 t_signed);

  // helper to store a an unsigned integer
531 532 533 534 535
  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
536 537 538 539
	 bool _swap,
	 t_unsigned);

  /// Store an integer with a wanted number of bits
540
  template< typename T >
Jan Möbius's avatar
Jan Möbius committed
541
  inline
542 543 544 545
  size_t
  store( std::ostream& _os,
	 const T& _val,
	 OMFormat::Chunk::Integer_Size _b,
Jan Möbius's avatar
Jan Möbius committed
546
	 bool _swap)
547
  {
Jan Möbius's avatar
Jan Möbius committed
548 549 550 551 552 553 554 555 556
    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
557 558 559
  size_t restore( std::istream& _is,
		  T& _val,
		  OMFormat::Chunk::Integer_Size _b,
Jan Möbius's avatar
Jan Möbius committed
560 561 562 563 564
		  bool _swap,
		  t_signed);

  // helper to store a an unsigned integer
  template< typename T > inline
565 566 567
  size_t restore( std::istream& _is,
		  T& _val,
		  OMFormat::Chunk::Integer_Size _b,
Jan Möbius's avatar
Jan Möbius committed
568 569 570 571
		  bool _swap,
		  t_unsigned);

  /// Restore an integer with a wanted number of bits
572
  template< typename T >
Jan Möbius's avatar
Jan Möbius committed
573
  inline
574 575 576 577
  size_t
  restore( std::istream& _is,
	   T& _val,
	   OMFormat::Chunk::Integer_Size _b,
Jan Möbius's avatar
Jan Möbius committed
578
	   bool _swap)
579
  {
Jan Möbius's avatar
Jan Möbius committed
580
    assert( OMFormat::is_integer( _val ) );
581

Jan Möbius's avatar
Jan Möbius committed
582 583
    if ( OMFormat::is_signed( _val ) )
      return restore( _is, _val, _b, _swap, t_signed() );
584
    return restore( _is, _val, _b, _swap, t_unsigned() );
Jan Möbius's avatar
Jan Möbius committed
585 586 587
  }


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

  template <typename VecT> inline
600
  size_t store( std::ostream& _os, const VecT& _vec, GenProg::Int2Type<3>,
Jan Möbius's avatar
Jan Möbius committed
601 602 603 604 605 606 607 608 609
		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
610
  size_t store( std::ostream& _os, const VecT& _vec, GenProg::Int2Type<4>,
Jan Möbius's avatar
Jan Möbius committed
611 612 613 614 615 616 617 618 619 620
		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
621
  size_t store( std::ostream& _os, const VecT& _vec, GenProg::Int2Type<1>,
Jan Möbius's avatar
Jan Möbius committed
622 623 624 625
		bool _swap )
  {
    return store( _os, _vec[0], _swap );
  }
626

Jan Möbius's avatar
Jan Möbius committed
627 628 629 630
  /// storing a vector type
  template <typename VecT> inline
  size_t vector_store( std::ostream& _os, const VecT& _vec, bool _swap )
  {
631
    return store( _os, _vec,
Jan Möbius's avatar
Jan Möbius committed
632 633 634 635 636 637 638 639
		  GenProg::Int2Type< vector_traits<VecT>::size_ >(),
		  _swap );
  }

  // ---------------------------------------- restoring vectors
  template <typename VecT>
  inline
  size_t
640
  restore( std::istream& _is, VecT& _vec, GenProg::Int2Type<2>,
Jan Möbius's avatar
Jan Möbius committed
641 642 643 644 645 646 647 648 649 650
	 bool _swap )
  {
    size_t bytes =  restore( _is, _vec[0], _swap );
    bytes += restore( _is, _vec[1], _swap );
    return bytes;
  }

  template <typename VecT>
  inline
  size_t
651
  restore( std::istream& _is, VecT& _vec, GenProg::Int2Type<3>,
Jan Möbius's avatar
Jan Möbius committed
652 653 654 655 656 657 658 659 660 661 662 663 664 665
	 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
666
  restore( std::istream& _is, VecT& _vec, GenProg::Int2Type<4>,
Jan Möbius's avatar
Jan Möbius committed
667 668 669 670 671 672 673 674 675 676 677
	   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;
  }
678

Jan Möbius's avatar
Jan Möbius committed
679
  template <typename VecT>
680
  inline
Jan Möbius's avatar
Jan Möbius committed
681
  size_t
682
  restore( std::istream& _is, VecT& _vec, GenProg::Int2Type<1>,
Jan Möbius's avatar
Jan Möbius committed
683 684 685 686 687 688 689 690 691 692 693
	 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 )
  {
694
    return restore( _is, _vec,
Jan Möbius's avatar
Jan Möbius committed
695 696 697 698 699 700
		    GenProg::Int2Type< vector_traits<VecT>::size_ >(),
		    _swap );
  }


  // ---------------------------------------- storing property names
701

Jan Möbius's avatar
Jan Möbius committed
702 703
  template <>
  inline
704
  size_t store( std::ostream& _os, const OMFormat::Chunk::PropertyName& _pn,
Jan Möbius's avatar
Jan Möbius committed
705 706 707 708 709 710 711 712 713 714
		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
715
  size_t restore( std::istream& _is, OMFormat::Chunk::PropertyName& _pn,
Jan Möbius's avatar
Jan Möbius committed
716
		  bool _swap )
717
  {
Jan Möbius's avatar
Jan Möbius committed
718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750
    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
//=============================================================================