OMToolkit  1.0
The polygonal mesh processing tool.
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
OMSerializableMatrix.h
Go to the documentation of this file.
1 //==============================================================================
14 #ifndef _OM_SERIALIZABLE_MATRIX_H_
15 #define _OM_SERIALIZABLE_MATRIX_H_
16 
17 #include <MDSTk\Math\mdsMatrix.h>
18 #include <OpenMesh\Core\Utils\BaseProperty.hh>
20 
21 namespace OMToolkit {
22 namespace Types {
23 
27  template <typename T>
28  class OMSerializableMatrix : public Eigen::Matrix<T, Eigen::Dynamic, Eigen::Dynamic, Eigen::RowMajor | Eigen::AutoAlign>
29  {
30  public:
31 
37  OMSerializableMatrix(int nRows, int nCols) : Eigen::Matrix<T, Eigen::Dynamic, Eigen::Dynamic, Eigen::RowMajor | Eigen::AutoAlign>()
38  {
39  resize(nRows, nCols);
40  }
41 
45  OMSerializableMatrix() : Eigen::Matrix<T, Eigen::Dynamic, Eigen::Dynamic, Eigen::RowMajor | Eigen::AutoAlign>()
46  {
47  }
48  }; // class OMSerializableMatrix
49 } // namespace Types
50 } // namespace OMToolkit
51 
52 namespace OpenMesh{
53 namespace IO{
54 
58  template <class T> struct binary<OMToolkit::Types::OMSerializableMatrix<T>>
59  {
64 
68  static const bool is_streamable = true;
69 
74  static size_t size_of(void)
75  {
76  return UnknownSize;
77  }
78 
82  static size_t size_of(const value_type& _v)
83  {
84  return _v.size() * sizeof(T) + 2 * sizeof(int);
85  }
86 
91  static size_t store(std::ostream& _os, const value_type& _v, bool _swap=false)
92  {
93  size_t bytes;
94  int size = _v.size();
95  bytes = IO::store( _os, _v.rows(), _swap);
96  bytes += IO::store( _os, _v.cols(), _swap);
97  for (int i = 0; i < size; ++i)
98  bytes += IO::store( _os, _v(i), _swap );
99  return _os.good() ? bytes : 0;
100  }
101 
106  static size_t restore( std::istream& _is, value_type& _v, bool _swap=false)
107  {
108  size_t bytes;
109  int rows, cols;
110  bytes = IO::restore( _is, rows, _swap );
111  bytes += IO::restore( _is, cols, _swap );
112  _v.resize(rows, cols);
113  unsigned int size = rows * cols;
114 
115  for (unsigned int i = 0; i < size && _is.good(); ++i)
116  bytes += IO::restore( _is, _v(i), _swap );
117  return _is.good() ? bytes : 0;
118  }
119  };
120 } // namespace IO
121 } // namespace OpenMesh
122 
123 #endif //_OM_SERIALIZABLE_MATRIX_H_