MatlabWorkspace.cpp 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. #include "igl/matlab/MatlabWorkspace.h"
  2. // IGL
  3. #include "igl/list_to_matrix.h"
  4. // MATLAB
  5. #include "mat.h"
  6. // STL
  7. #include <iostream>
  8. #include <algorithm>
  9. IGL_INLINE igl::MatlabWorkspace::MatlabWorkspace()
  10. {
  11. }
  12. IGL_INLINE igl::MatlabWorkspace::~MatlabWorkspace()
  13. {
  14. // clean up data
  15. clear();
  16. }
  17. IGL_INLINE void igl::MatlabWorkspace::clear()
  18. {
  19. for_each(data.begin(),data.end(),&mxDestroyArray);
  20. }
  21. IGL_INLINE bool igl::MatlabWorkspace::write(const std::string & path) const
  22. {
  23. using namespace std;
  24. MATFile * mat_file = matOpen(path.c_str(), "w");
  25. assert(names.size() == data.size());
  26. // loop over names and data
  27. for(int i = 0;i < (int)names.size(); i++)
  28. {
  29. // Put variable as LOCAL variable
  30. int status = matPutVariable(mat_file,names[i].c_str(), data[i]);
  31. if(status != 0)
  32. {
  33. cerr<<"^MatlabWorkspace::save Error: matPutVariable ("<<names[i]<<
  34. ") failed"<<endl;
  35. return false;
  36. }
  37. }
  38. if(matClose(mat_file) != 0)
  39. {
  40. fprintf(stderr,"Error closing file %s\n",path.c_str());
  41. return false;
  42. }
  43. return true;
  44. }
  45. // Treat everything as a double
  46. template <typename DerivedM>
  47. IGL_INLINE igl::MatlabWorkspace& igl::MatlabWorkspace::save(
  48. const Eigen::PlainObjectBase<DerivedM>& M,
  49. const std::string & name)
  50. {
  51. using namespace std;
  52. const int m = M.rows();
  53. const int n = M.cols();
  54. mxArray * mx_data = mxCreateDoubleMatrix(m,n,mxREAL);
  55. data.push_back(mx_data);
  56. names.push_back(name);
  57. // Copy data immediately
  58. // Q: Won't this be incorrect for integers?
  59. copy(M.data(),M.data()+M.size(),mxGetPr(mx_data));
  60. return *this;
  61. }
  62. // Treat everything as a double
  63. template <typename MT>
  64. IGL_INLINE igl::MatlabWorkspace& igl::MatlabWorkspace::save(
  65. const Eigen::SparseMatrix<MT>& M,
  66. const std::string & name)
  67. {
  68. using namespace std;
  69. const int m = M.rows();
  70. const int n = M.cols();
  71. // THIS WILL NOT WORK FOR ROW-MAJOR
  72. assert(n==M.outerSize());
  73. const int nzmax = M.nonZeros();
  74. mxArray * mx_data = mxCreateSparse(m, n, nzmax, mxREAL);
  75. data.push_back(mx_data);
  76. names.push_back(name);
  77. // Copy data immediately
  78. double * pr = mxGetPr(mx_data);
  79. mwIndex * ir = mxGetIr(mx_data);
  80. mwIndex * jc = mxGetJc(mx_data);
  81. // Iterate over outside
  82. int k = 0;
  83. for(int j=0; j<M.outerSize();j++)
  84. {
  85. jc[j] = k;
  86. // Iterate over inside
  87. for(typename Eigen::SparseMatrix<MT>::InnerIterator it (M,j); it; ++it)
  88. {
  89. pr[k] = it.value();
  90. ir[k] = it.row();
  91. k++;
  92. }
  93. }
  94. jc[M.outerSize()] = k;
  95. return *this;
  96. }
  97. template <typename ScalarM>
  98. IGL_INLINE igl::MatlabWorkspace& igl::MatlabWorkspace::save(
  99. const std::vector<std::vector<ScalarM> > & vM,
  100. const std::string & name)
  101. {
  102. Eigen::MatrixXd M;
  103. list_to_matrix(vM,M);
  104. return this->save(M,name);
  105. }
  106. template <typename ScalarV>
  107. IGL_INLINE igl::MatlabWorkspace& igl::MatlabWorkspace::save(
  108. const std::vector<ScalarV> & vV,
  109. const std::string & name)
  110. {
  111. Eigen::MatrixXd V;
  112. list_to_matrix(vV,V);
  113. return this->save(V,name);
  114. }
  115. template <typename DerivedM>
  116. IGL_INLINE igl::MatlabWorkspace&
  117. igl::MatlabWorkspace::save_index(
  118. const Eigen::PlainObjectBase<DerivedM>& M,
  119. const std::string & name)
  120. {
  121. DerivedM Mp1 = M;
  122. Mp1.array() += 1;
  123. return this->save(Mp1,name);
  124. }
  125. template <typename ScalarM>
  126. IGL_INLINE igl::MatlabWorkspace& igl::MatlabWorkspace::save_index(
  127. const std::vector<std::vector<ScalarM> > & vM,
  128. const std::string & name)
  129. {
  130. Eigen::MatrixXd M;
  131. list_to_matrix(vM,M);
  132. return this->save_index(M,name);
  133. }
  134. template <typename ScalarV>
  135. IGL_INLINE igl::MatlabWorkspace& igl::MatlabWorkspace::save_index(
  136. const std::vector<ScalarV> & vV,
  137. const std::string & name)
  138. {
  139. Eigen::MatrixXd V;
  140. list_to_matrix(vV,V);
  141. return this->save_index(V,name);
  142. }
  143. //template <typename Data>
  144. //bool igl::MatlabWorkspace::save(const Data & M, const std::string & name)
  145. //{
  146. // using namespace std;
  147. // // If I don't know the type then I can't save it
  148. // cerr<<"^MatlabWorkspace::save Error: Unknown data type. "<<
  149. // name<<" not saved."<<endl;
  150. // return false;
  151. //}