MatlabWorkspace.h 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  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. // KNOWN BUG: Why not use a map? Any reason to allow duplicate names?
  25. //
  26. // List of names
  27. std::vector<std::string> names;
  28. // List of data pointers
  29. std::vector<mxArray*> data;
  30. public:
  31. MatlabWorkspace();
  32. ~MatlabWorkspace();
  33. // Clear names and data of variables in workspace
  34. IGL_INLINE void clear();
  35. // Save current list of variables
  36. //
  37. // Inputs:
  38. // path path to .mat file
  39. // Returns true on success, false on failure
  40. IGL_INLINE bool write(const std::string & path) const;
  41. // Load list of variables from .mat file
  42. //
  43. // Inputs:
  44. // path path to .mat file
  45. // Returns true on success, false on failure
  46. IGL_INLINE bool read(const std::string & path);
  47. // Assign data to a variable name in the workspace
  48. //
  49. // Template:
  50. // DerivedM eigen matrix (e.g. MatrixXd)
  51. // Inputs:
  52. // M data (usually a matrix)
  53. // name variable name to save into work space
  54. // Returns true on success, false on failure
  55. //
  56. // Known Bugs: Assumes Eigen is using column major ordering
  57. template <typename DerivedM>
  58. IGL_INLINE MatlabWorkspace& save(
  59. const Eigen::PlainObjectBase<DerivedM>& M,
  60. const std::string & name);
  61. // Template:
  62. // MT sparse matrix type (e.g. double)
  63. template <typename MT>
  64. IGL_INLINE MatlabWorkspace& save(
  65. const Eigen::SparseMatrix<MT>& M,
  66. const std::string & name);
  67. // Templates:
  68. // ScalarM scalar type, e.g. double
  69. template <typename ScalarM>
  70. IGL_INLINE MatlabWorkspace& save(
  71. const std::vector<std::vector<ScalarM> > & vM,
  72. const std::string & name);
  73. // Templates:
  74. // ScalarV scalar type, e.g. double
  75. template <typename ScalarV>
  76. IGL_INLINE MatlabWorkspace& save(
  77. const std::vector<ScalarV> & vV,
  78. const std::string & name);
  79. // Same as save() but adds 1 to each element, useful for saving "index"
  80. // matrices like lists of faces or elements
  81. template <typename DerivedM>
  82. IGL_INLINE MatlabWorkspace& save_index(
  83. const Eigen::PlainObjectBase<DerivedM>& M,
  84. const std::string & name);
  85. template <typename ScalarM>
  86. IGL_INLINE MatlabWorkspace& save_index(
  87. const std::vector<std::vector<ScalarM> > & vM,
  88. const std::string & name);
  89. template <typename ScalarV>
  90. IGL_INLINE MatlabWorkspace& save_index(
  91. const std::vector<ScalarV> & vV,
  92. const std::string & name);
  93. // Find a certain matrix by name.
  94. //
  95. // KNOWN BUG: Outputs the first found (not necessarily unique lists).
  96. //
  97. // Template:
  98. // DerivedM eigen matrix (e.g. MatrixXd)
  99. // Inputs:
  100. // name exact name of matrix as string
  101. // Outputs:
  102. // M matrix
  103. // Returns true only if found.
  104. template <typename DerivedM>
  105. IGL_INLINE bool find(
  106. const std::string & name,
  107. Eigen::PlainObjectBase<DerivedM>& M);
  108. template <typename MT>
  109. IGL_INLINE bool find(
  110. const std::string & name,
  111. Eigen::SparseMatrix<MT>& M);
  112. // Subtracts 1 from all entries
  113. template <typename DerivedM>
  114. IGL_INLINE bool find_index(
  115. const std::string & name,
  116. Eigen::PlainObjectBase<DerivedM>& M);
  117. };
  118. }
  119. // Be sure that this is not compiled into libigl.a
  120. #ifdef IGL_HEADER_ONLY
  121. # include "MatlabWorkspace.cpp"
  122. #endif
  123. #endif