MatlabWorkspace.h 3.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. #ifndef IGL_WRITE_MATLAB_WORKSPACE
  2. #define IGL_WRITE_MATLAB_WORKSPACE
  3. #include "igl/igl_inline.h"
  4. #include <string>
  5. #include <vector>
  6. #include <Eigen/Dense>
  7. #include <Eigen/Sparse>
  8. #include "mat.h"
  9. namespace igl
  10. {
  11. // Class which contains data of a matlab workspace which can be written to a
  12. // .mat file and loaded from matlab
  13. //
  14. // This depends on matlab at compile time (though it shouldn't necessarily
  15. // have to) but it does not depend on running the matlab engine at run-time.
  16. //
  17. // Known bugs: Treats all matrices as doubles (this may actually be desired
  18. // for some "index" matrices since matlab's sparse command takes doubles
  19. // rather than int class matrices). It is of course not desired when dealing
  20. // with logicals or uint's for images.
  21. class MatlabWorkspace
  22. {
  23. private:
  24. // List of names
  25. std::vector<std::string> names;
  26. // List of data pointers
  27. std::vector<mxArray*> data;
  28. public:
  29. MatlabWorkspace();
  30. ~MatlabWorkspace();
  31. // Clear names and data of variables in workspace
  32. IGL_INLINE void clear();
  33. // Save current list of variables
  34. //
  35. // Inputs:
  36. // path path to .mat file
  37. // Returns true on success, false on failure
  38. IGL_INLINE bool write(const std::string & path) const;
  39. // Assign data to a variable name in the workspace
  40. //
  41. // Template:
  42. // DerivedM eigen matrix (e.g. MatrixXd)
  43. // Inputs:
  44. // M data (usually a matrix)
  45. // name variable name to save into work space
  46. // Returns true on success, false on failure
  47. //
  48. // Known Bugs: Assumes Eigen is using column major ordering
  49. template <typename DerivedM>
  50. IGL_INLINE MatlabWorkspace& save(
  51. const Eigen::PlainObjectBase<DerivedM>& M,
  52. const std::string & name);
  53. // Template:
  54. // MT sparse matrix type (e.g. double)
  55. template <typename MT>
  56. IGL_INLINE MatlabWorkspace& save(
  57. const Eigen::SparseMatrix<MT>& M,
  58. const std::string & name);
  59. // Templates:
  60. // ScalarM scalar type, e.g. double
  61. template <typename ScalarM>
  62. IGL_INLINE MatlabWorkspace& save(
  63. const std::vector<std::vector<ScalarM> > & vM,
  64. const std::string & name);
  65. // Templates:
  66. // ScalarV scalar type, e.g. double
  67. template <typename ScalarV>
  68. IGL_INLINE MatlabWorkspace& save(
  69. const std::vector<ScalarV> & vV,
  70. const std::string & name);
  71. // Same as save() but adds 1 to each element, useful for saving "index"
  72. // matrices like lists of faces or elements
  73. template <typename DerivedM>
  74. IGL_INLINE MatlabWorkspace& save_index(
  75. const Eigen::PlainObjectBase<DerivedM>& M,
  76. const std::string & name);
  77. template <typename ScalarM>
  78. IGL_INLINE MatlabWorkspace& save_index(
  79. const std::vector<std::vector<ScalarM> > & vM,
  80. const std::string & name);
  81. template <typename ScalarV>
  82. IGL_INLINE MatlabWorkspace& save_index(
  83. const std::vector<ScalarV> & vV,
  84. const std::string & name);
  85. };
  86. }
  87. // Be sure that this is not compiled into libigl.a
  88. #ifdef IGL_HEADER_ONLY
  89. # include "MatlabWorkspace.cpp"
  90. #endif
  91. #endif