OMToolkit  1.0
The polygonal mesh processing tool.
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
OMFormatExt.h
Go to the documentation of this file.
1 //==============================================================================
15 #ifndef OM_FORMAT_EXT_H
16 #define OM_FORMAT_EXT_H
17 
18 #include <OpenMesh\Core\IO\OMFormat.hh>
20 
21 namespace OpenMesh {
22 namespace IO {
23 namespace OMFormat{
24 
28 struct HeaderExt : Header
29 {
36  size_t store( mds::mod::CChannel& _os, bool _swap ) const
37  {
38  _os.write( (char*)this, 4); // magic_, mesh_, version_
39  size_t bytes = 4;
40  bytes += binaryExt<uint32_t>::store( _os, n_vertices_, _swap );
41  bytes += binaryExt<uint32_t>::store( _os, n_faces_, _swap );
42  bytes += binaryExt<uint32_t>::store( _os, n_edges_, _swap );
43  return bytes;
44  }
45 
52  size_t restore( mds::mod::CChannel& _is, bool _swap )
53  {
54  if (_is.read( (char*)this, 4 ) == 0)
55  return 0;
56 
57  size_t bytes = 4;
58  bytes += binaryExt<uint32_t>::restore( _is, n_vertices_, _swap );
59  bytes += binaryExt<uint32_t>::restore( _is, n_faces_, _swap );
60  bytes += binaryExt<uint32_t>::restore( _is, n_edges_, _swap );
61  return bytes;
62  }
63 };
64 } // namespace OMFormat
65 
66 
74 template <typename T> inline
75 size_t store( mds::mod::CChannel& _os, const T& _v, bool _swap=false)
76 {
77  return binaryExt< T >::store( _os, _v, _swap );
78 }
79 
87 template <typename T> inline
88 size_t restore( mds::mod::CChannel& _is, T& _v, bool _swap=false)
89 {
90  return binaryExt< T >::restore( _is, _v, _swap );
91 }
92 
100 template <> inline
101 size_t store( mds::mod::CChannel& _os, const OMFormat::HeaderExt& _hdr, bool _swap)
102 {
103  return _hdr.store( _os, _swap );
104 }
105 
113 template <> inline
114 size_t restore( mds::mod::CChannel& _is, OMFormat::HeaderExt& _hdr, bool _swap )
115 {
116  return _hdr.restore( _is, _swap );
117 }
118 
119 
127 template <> inline
128 size_t store( mds::mod::CChannel& _os, const OMFormat::Chunk::Header& _hdr, bool _swap)
129 {
130  OMFormat::uint16 val; val << _hdr;
131  return binaryExt<uint16_t>::store( _os, val, _swap );
132 }
133 
141 template <> inline
142 size_t restore( mds::mod::CChannel& _is, OMFormat::Chunk::Header& _hdr, bool _swap )
143 {
144  OMFormat::uint16 val;
145  size_t bytes = binaryExt<uint16_t>::restore( _is, val, _swap );
146  _hdr << val;
147  return bytes;
148 }
149 
150 
151 
152 
154 // Storing/restoring scalars
155 
165 template< typename T > size_t store( mds::mod::CChannel& _os, const T& _val, OMFormat::Chunk::Integer_Size _b, bool _swap, t_signed);
166 
176 template< typename T > size_t store( mds::mod::CChannel& _os, const T& _val, OMFormat::Chunk::Integer_Size _b, bool _swap, t_unsigned);
177 
186 template< typename T > inline size_t store( mds::mod::CChannel& _os, const T& _val, OMFormat::Chunk::Integer_Size _b, bool _swap)
187 {
188  assert( OMFormat::is_integer( _val ) );
189  if ( OMFormat::is_signed( _val ) )
190  return store( _os, _val, _b, _swap, t_signed() );
191  return store( _os, _val, _b, _swap, t_unsigned() );
192 }
193 
203 template< typename T > inline size_t restore( mds::mod::CChannel& _is, T& _val, OMFormat::Chunk::Integer_Size _b,bool _swap, t_signed);
204 
214 template< typename T > inline size_t restore( mds::mod::CChannel& _is, T& _val, OMFormat::Chunk::Integer_Size _b, bool _swap, t_unsigned);
215 
224 template< typename T > inline size_t restore( mds::mod::CChannel& _is, T& _val, OMFormat::Chunk::Integer_Size _b, bool _swap)
225 {
226  assert( OMFormat::is_integer( _val ) );
227 
228  if ( OMFormat::is_signed( _val ) )
229  return restore( _is, _val, _b, _swap, t_signed() );
230  return restore( _is, _val, _b, _swap, t_unsigned() );
231 }
232 
233 
234 
236 // Storing vectors
237 
245 template <typename VecT> inline size_t store( mds::mod::CChannel& _os, const VecT& _vec, GenProg::Int2Type<2>, bool _swap )
246 {
247  size_t bytes = store( _os, _vec[0], _swap );
248  bytes += store( _os, _vec[1], _swap );
249  return bytes;
250 }
251 
259 template <typename VecT> inline size_t store( mds::mod::CChannel& _os, const VecT& _vec, GenProg::Int2Type<3>, bool _swap )
260 {
261  size_t bytes = store( _os, _vec[0], _swap );
262  bytes += store( _os, _vec[1], _swap );
263  bytes += store( _os, _vec[2], _swap );
264  return bytes;
265 }
266 
274 template <typename VecT> inline size_t store( mds::mod::CChannel& _os, const VecT& _vec, GenProg::Int2Type<4>, bool _swap )
275 {
276  size_t bytes = store( _os, _vec[0], _swap );
277  bytes += store( _os, _vec[1], _swap );
278  bytes += store( _os, _vec[2], _swap );
279  bytes += store( _os, _vec[3], _swap );
280  return bytes;
281 }
282 
290 template <typename VecT> inline size_t store( mds::mod::CChannel& _os, const VecT& _vec, GenProg::Int2Type<1>, bool _swap )
291 {
292  return store( _os, _vec[0], _swap );
293 }
294 
302 template <typename VecT> inline size_t vector_store(mds::mod::CChannel& _os, const VecT& _vec, bool _swap )
303 {
304  return store( _os, _vec, GenProg::Int2Type< vector_traits<VecT>::size_ >(), _swap );
305 }
306 
308 // Restoring vectors
309 
317 template <typename VecT> inline size_t restore( mds::mod::CChannel& _is, VecT& _vec, GenProg::Int2Type<2>, bool _swap )
318 {
319  size_t bytes = restore( _is, _vec[0], _swap );
320  bytes += restore( _is, _vec[1], _swap );
321  return bytes;
322 }
323 
331 template <typename VecT> inline size_t restore( mds::mod::CChannel& _is, VecT& _vec, GenProg::Int2Type<3>, bool _swap )
332 {
333  typedef typename vector_traits<VecT>::value_type scalar_type;
334  size_t bytes;
335 
336  bytes = binaryExt<scalar_type>::restore( _is, _vec[0], _swap );
337  bytes += binaryExt<scalar_type>::restore( _is, _vec[1], _swap );
338  bytes += binaryExt<scalar_type>::restore( _is, _vec[2], _swap );
339  return bytes;
340 }
341 
349 template <typename VecT> inline size_t restore( mds::mod::CChannel& _is, VecT& _vec, GenProg::Int2Type<4>, bool _swap )
350 {
351  typedef typename vector_traits<VecT>::value_type scalar_type;
352  size_t bytes;
353 
354  bytes = binaryExt<scalar_type>::restore( _is, _vec[0], _swap );
355  bytes += binaryExt<scalar_type>::restore( _is, _vec[1], _swap );
356  bytes += binaryExt<scalar_type>::restore( _is, _vec[2], _swap );
357  bytes += binaryExt<scalar_type>::restore( _is, _vec[3], _swap );
358  return bytes;
359 }
360 
368 template <typename VecT> inline size_t restore( mds::mod::CChannel& _is, VecT& _vec, GenProg::Int2Type<1>, bool _swap )
369 {
370  return restore( _is, _vec[0], _swap );
371 }
372 
380 template <typename VecT> inline size_t vector_restore( mds::mod::CChannel& _is, VecT& _vec, bool _swap )
381 {
382  return restore( _is, _vec, GenProg::Int2Type< vector_traits<VecT>::size_ >(), _swap );
383 }
384 
386 // Storing property names
387 
395 template <> inline size_t store( mds::mod::CChannel& _os, const OMFormat::Chunk::PropertyName& _pn, bool _swap )
396 {
397  store( _os, _pn.size(), OMFormat::Chunk::Integer_8, _swap ); // 1 byte
398  if ( _pn.size() )
399  _os.write( _pn.c_str(), _pn.size() ); // size bytes
400  return _pn.size() + 1;
401 }
402 
410 template <> inline size_t restore( mds::mod::CChannel& _is, OMFormat::Chunk::PropertyName& _pn, bool _swap )
411 {
412  size_t size;
413 
414  restore( _is, size, OMFormat::Chunk::Integer_8, _swap); // 1 byte
415 
416  assert( OMFormat::Chunk::PropertyName::is_valid( size ) );
417 
418  if ( size > 0 )
419  {
420  char buf[256];
421  _is.read( buf, size ); // size bytes
422  buf[size] = '\0';
423  _pn.resize(size);
424  _pn = buf;
425  }
426  return size+1;
427 }
428 
429 } // namespace IO
430 } // namespace Openmesh
431 
433 #endif