MatlabWorkspace.cpp 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. // Don't let libigl.a compile anything here
  2. #ifdef IGL_HEADER_ONLY
  3. #include "MatlabWorkspace.h"
  4. // IGL
  5. #include "list_to_matrix.h"
  6. // MATLAB
  7. #include "mat.h"
  8. // STL
  9. #include <iostream>
  10. #include <algorithm>
  11. IGL_INLINE igl::MatlabWorkspace::MatlabWorkspace()
  12. {
  13. }
  14. IGL_INLINE igl::MatlabWorkspace::~MatlabWorkspace()
  15. {
  16. // clean up data
  17. clear();
  18. }
  19. IGL_INLINE void igl::MatlabWorkspace::clear()
  20. {
  21. for_each(data.begin(),data.end(),&mxDestroyArray);
  22. }
  23. IGL_INLINE bool igl::MatlabWorkspace::write(const std::string & path) const
  24. {
  25. using namespace std;
  26. MATFile * mat_file = matOpen(path.c_str(), "w");
  27. assert(names.size() == data.size());
  28. // loop over names and data
  29. for(int i = 0;i < names.size(); i++)
  30. {
  31. // Put variable as LOCAL variable
  32. int status = matPutVariable(mat_file,names[i].c_str(), data[i]);
  33. if(status != 0)
  34. {
  35. cerr<<"^MatlabWorkspace::save Error: matPutVariable ("<<names[i]<<
  36. ") failed"<<endl;
  37. return false;
  38. }
  39. }
  40. if(matClose(mat_file) != 0)
  41. {
  42. fprintf(stderr,"Error closing file %s\n",path.c_str());
  43. return false;
  44. }
  45. return true;
  46. }
  47. // Treat everything as a double
  48. template <typename DerivedM>
  49. IGL_INLINE igl::MatlabWorkspace::MatlabWorkspace& igl::MatlabWorkspace::save(
  50. const Eigen::PlainObjectBase<DerivedM>& M,
  51. const std::string & name)
  52. {
  53. using namespace std;
  54. const int m = M.rows();
  55. const int n = M.cols();
  56. mxArray * mx_data = mxCreateDoubleMatrix(m,n,mxREAL);
  57. data.push_back(mx_data);
  58. names.push_back(name);
  59. // Copy data immediately
  60. copy(M.data(),M.data()+M.size(),mxGetPr(mx_data));
  61. return *this;
  62. }
  63. template <typename ScalarM>
  64. IGL_INLINE igl::MatlabWorkspace::MatlabWorkspace& igl::MatlabWorkspace::save(
  65. const std::vector<std::vector<ScalarM> > & vM,
  66. const std::string & name)
  67. {
  68. Eigen::MatrixXd M;
  69. list_to_matrix(vM,M);
  70. return this->save(M,name);
  71. }
  72. template <typename ScalarV>
  73. IGL_INLINE igl::MatlabWorkspace::MatlabWorkspace& igl::MatlabWorkspace::save(
  74. const std::vector<ScalarV> & vV,
  75. const std::string & name)
  76. {
  77. Eigen::MatrixXd V;
  78. list_to_matrix(vV,V);
  79. return this->save(V,name);
  80. }
  81. template <typename DerivedM>
  82. IGL_INLINE igl::MatlabWorkspace::MatlabWorkspace&
  83. igl::MatlabWorkspace::save_index(
  84. const Eigen::PlainObjectBase<DerivedM>& M,
  85. const std::string & name)
  86. {
  87. DerivedM Mp1 = M;
  88. Mp1.array() += 1;
  89. return this->save(Mp1,name);
  90. }
  91. template <typename ScalarM>
  92. IGL_INLINE igl::MatlabWorkspace::MatlabWorkspace& igl::MatlabWorkspace::save_index(
  93. const std::vector<std::vector<ScalarM> > & vM,
  94. const std::string & name)
  95. {
  96. Eigen::MatrixXd M;
  97. list_to_matrix(vM,M);
  98. return this->save_index(M,name);
  99. }
  100. template <typename ScalarV>
  101. IGL_INLINE igl::MatlabWorkspace::MatlabWorkspace& igl::MatlabWorkspace::save_index(
  102. const std::vector<ScalarV> & vV,
  103. const std::string & name)
  104. {
  105. Eigen::MatrixXd V;
  106. list_to_matrix(vV,V);
  107. return this->save_index(V,name);
  108. }
  109. //template <typename Data>
  110. //bool igl::MatlabWorkspace::save(const Data & M, const std::string & name)
  111. //{
  112. // using namespace std;
  113. // // If I don't know the type then I can't save it
  114. // cerr<<"^MatlabWorkspace::save Error: Unknown data type. "<<
  115. // name<<" not saved."<<endl;
  116. // return false;
  117. //}
  118. #endif