MatlabWorkspace.h 2.9 KB

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