OMFormat.hh 21.9 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

Jan Möbius's avatar
Jan Möbius committed
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 125 126


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

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

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

Jan Möbius's avatar
Jan Möbius committed
302 303 304 305 306 307 308 309 310 311 312

  /// 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 )
  {
313
    size_t C;
Jan Möbius's avatar
Jan Möbius committed
314 315 316 317
    switch( _chunk_hdr.entity_ )
    {
      case Chunk::Entity_Vertex:   C  = _hdr.n_vertices_; break;
      case Chunk::Entity_Face:     C  = _hdr.n_faces_;    break;
318 319
      case Chunk::Entity_Halfedge: C  = _hdr.n_edges_*2;  break;
      case Chunk::Entity_Edge:     C  = _hdr.n_edges_;    break;
Jan Möbius's avatar
Jan Möbius committed
320 321
      case Chunk::Entity_Mesh:     C  = 1;                break;
      default:
322
        C = 0;
323 324 325
        std::cerr << "Invalid value in _chunk_hdr.entity_\n";
        assert( false );
        break;
Jan Möbius's avatar
Jan Möbius committed
326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343
    }

    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

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

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

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

381
  template <typename VecType>
Jan Möbius's avatar
Jan Möbius committed
382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397
  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
398
#ifndef NDEBUG
Jan Möbius's avatar
Jan Möbius committed
399
    assert( is_integer(d) );
David Bommes's avatar
David Bommes committed
400
#endif
Jan Möbius's avatar
Jan Möbius committed
401 402 403 404 405 406 407

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


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

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

441 442 443 444 445 446 447 448 449 450
  // 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
451 452 453 454 455 456
  // -------------------- create/read version

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


457
  inline uint16 major_version(const uint8 version)
Jan Möbius's avatar
Jan Möbius committed
458 459 460
  { return (version >> 5) & 0x07; }


461
  inline uint16 minor_version(const uint8 version)
Jan Möbius's avatar
Jan Möbius committed
462 463 464 465 466
  { return (version & 0x001f); }


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

467 468
  std::string as_string(uint8 version);

Jan Möbius's avatar
Jan Möbius committed
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
  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
495
  size_t
Jan Möbius's avatar
Jan Möbius committed
496 497 498 499 500 501 502
  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
503
  size_t
Jan Möbius's avatar
Jan Möbius committed
504 505 506 507 508 509
  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;
510

Jan Möbius's avatar
Jan Möbius committed
511 512 513 514 515
    return bytes;
  }

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

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

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

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

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

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

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

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


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

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

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

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

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

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


  // ---------------------------------------- storing property names
699

Jan Möbius's avatar
Jan Möbius committed
700 701
  template <>
  inline
702
  size_t store( std::ostream& _os, const OMFormat::Chunk::PropertyName& _pn,
Jan Möbius's avatar
Jan Möbius committed
703 704 705 706 707 708 709 710 711 712
		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
713
  size_t restore( std::istream& _is, OMFormat::Chunk::PropertyName& _pn,
Jan Möbius's avatar
Jan Möbius committed
714
		  bool _swap )
715
  {
Jan Möbius's avatar
Jan Möbius committed
716 717 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
    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
744
#  include "OMFormatT_impl.hh"
Jan Möbius's avatar
Jan Möbius committed
745 746 747 748
#endif
//=============================================================================
#endif
//=============================================================================