matlabinterface.cpp 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330
  1. // This file is part of libigl, a simple c++ geometry processing library.
  2. //
  3. // Copyright (C) 2013 Alec Jacobson <alecjacobson@gmail.com>
  4. //
  5. // This Source Code Form is subject to the terms of the Mozilla Public License
  6. // v. 2.0. If a copy of the MPL was not distributed with this file, You can
  7. // obtain one at http://mozilla.org/MPL/2.0/.
  8. #include <igl/matlab/matlabinterface.h>
  9. // Implementation
  10. // Init the MATLAB engine
  11. // (no need to call it directly since it is automatically invoked by any other command)
  12. IGL_INLINE void igl::mlinit(Engine** mlengine)
  13. {
  14. *mlengine = engOpen("\0");
  15. }
  16. // Closes the MATLAB engine
  17. IGL_INLINE void igl::mlclose(Engine** mlengine)
  18. {
  19. engClose(*mlengine);
  20. *mlengine = 0;
  21. }
  22. // Send a matrix to MATLAB
  23. IGL_INLINE void igl::mlsetmatrix(Engine** mlengine, std::string name, const Eigen::MatrixXd& M)
  24. {
  25. if (*mlengine == 0)
  26. mlinit(mlengine);
  27. mxArray *A = mxCreateDoubleMatrix(M.rows(), M.cols(), mxREAL);
  28. double *pM = mxGetPr(A);
  29. int c = 0;
  30. for(int j=0; j<M.cols();++j)
  31. for(int i=0; i<M.rows();++i)
  32. pM[c++] = double(M(i,j));
  33. engPutVariable(*mlengine, name.c_str(), A);
  34. mxDestroyArray(A);
  35. }
  36. // Send a matrix to MATLAB
  37. IGL_INLINE void igl::mlsetmatrix(Engine** mlengine, std::string name, const Eigen::MatrixXf& M)
  38. {
  39. if (*mlengine == 0)
  40. mlinit(mlengine);
  41. mxArray *A = mxCreateDoubleMatrix(M.rows(), M.cols(), mxREAL);
  42. double *pM = mxGetPr(A);
  43. int c = 0;
  44. for(int j=0; j<M.cols();++j)
  45. for(int i=0; i<M.rows();++i)
  46. pM[c++] = double(M(i,j));
  47. engPutVariable(*mlengine, name.c_str(), A);
  48. mxDestroyArray(A);
  49. }
  50. // Send a matrix to MATLAB
  51. IGL_INLINE void igl::mlsetmatrix(Engine** mlengine, std::string name, const Eigen::MatrixXi& M)
  52. {
  53. if (*mlengine == 0)
  54. mlinit(mlengine);
  55. mxArray *A = mxCreateDoubleMatrix(M.rows(), M.cols(), mxREAL);
  56. double *pM = mxGetPr(A);
  57. int c = 0;
  58. for(int j=0; j<M.cols();++j)
  59. for(int i=0; i<M.rows();++i)
  60. pM[c++] = double(M(i,j))+1;
  61. engPutVariable(*mlengine, name.c_str(), A);
  62. mxDestroyArray(A);
  63. }
  64. // Send a matrix to MATLAB
  65. IGL_INLINE void igl::mlsetmatrix(Engine** mlengine, std::string name, const Eigen::Matrix<unsigned int, Eigen::Dynamic, Eigen::Dynamic >& M)
  66. {
  67. if (*mlengine == 0)
  68. mlinit(mlengine);
  69. mxArray *A = mxCreateDoubleMatrix(M.rows(), M.cols(), mxREAL);
  70. double *pM = mxGetPr(A);
  71. int c = 0;
  72. for(int j=0; j<M.cols();++j)
  73. for(int i=0; i<M.rows();++i)
  74. pM[c++] = double(M(i,j))+1;
  75. engPutVariable(*mlengine, name.c_str(), A);
  76. mxDestroyArray(A);
  77. }
  78. // Receive a matrix from MATLAB
  79. IGL_INLINE void igl::mlgetmatrix(Engine** mlengine, std::string name, Eigen::MatrixXd& M)
  80. {
  81. if (*mlengine == 0)
  82. mlinit(mlengine);
  83. unsigned long m = 0;
  84. unsigned long n = 0;
  85. std::vector<double> t;
  86. mxArray *ary = engGetVariable(*mlengine, name.c_str());
  87. if (ary == NULL)
  88. {
  89. m = 0;
  90. n = 0;
  91. M = Eigen::MatrixXd(0,0);
  92. }
  93. else
  94. {
  95. m = mxGetM(ary);
  96. n = mxGetN(ary);
  97. M = Eigen::MatrixXd(m,n);
  98. double *pM = mxGetPr(ary);
  99. int c = 0;
  100. for(int j=0; j<M.cols();++j)
  101. for(int i=0; i<M.rows();++i)
  102. M(i,j) = pM[c++];
  103. }
  104. mxDestroyArray(ary);
  105. }
  106. IGL_INLINE void igl::mlgetmatrix(Engine** mlengine, std::string name, Eigen::MatrixXf& M)
  107. {
  108. if (*mlengine == 0)
  109. mlinit(mlengine);
  110. unsigned long m = 0;
  111. unsigned long n = 0;
  112. std::vector<double> t;
  113. mxArray *ary = engGetVariable(*mlengine, name.c_str());
  114. if (ary == NULL)
  115. {
  116. m = 0;
  117. n = 0;
  118. M = Eigen::MatrixXf(0,0);
  119. }
  120. else
  121. {
  122. m = mxGetM(ary);
  123. n = mxGetN(ary);
  124. M = Eigen::MatrixXf(m,n);
  125. double *pM = mxGetPr(ary);
  126. int c = 0;
  127. for(int j=0; j<M.cols();++j)
  128. for(int i=0; i<M.rows();++i)
  129. M(i,j) = pM[c++];
  130. }
  131. mxDestroyArray(ary);
  132. }
  133. // Receive a matrix from MATLAB
  134. IGL_INLINE void igl::mlgetmatrix(Engine** mlengine, std::string name, Eigen::MatrixXi& M)
  135. {
  136. if (*mlengine == 0)
  137. mlinit(mlengine);
  138. unsigned long m = 0;
  139. unsigned long n = 0;
  140. std::vector<double> t;
  141. mxArray *ary = engGetVariable(*mlengine, name.c_str());
  142. if (ary == NULL)
  143. {
  144. m = 0;
  145. n = 0;
  146. M = Eigen::MatrixXi(0,0);
  147. }
  148. else
  149. {
  150. m = mxGetM(ary);
  151. n = mxGetN(ary);
  152. M = Eigen::MatrixXi(m,n);
  153. double *pM = mxGetPr(ary);
  154. int c = 0;
  155. for(int j=0; j<M.cols();++j)
  156. for(int i=0; i<M.rows();++i)
  157. M(i,j) = int(pM[c++])-1;
  158. }
  159. mxDestroyArray(ary);
  160. }
  161. // Receive a matrix from MATLAB
  162. IGL_INLINE void igl::mlgetmatrix(Engine** mlengine, std::string name, Eigen::Matrix<unsigned int, Eigen::Dynamic, Eigen::Dynamic >& M)
  163. {
  164. if (*mlengine == 0)
  165. mlinit(mlengine);
  166. unsigned long m = 0;
  167. unsigned long n = 0;
  168. std::vector<double> t;
  169. mxArray *ary = engGetVariable(*mlengine, name.c_str());
  170. if (ary == NULL)
  171. {
  172. m = 0;
  173. n = 0;
  174. M = Eigen::Matrix<unsigned int, Eigen::Dynamic, Eigen::Dynamic >(0,0);
  175. }
  176. else
  177. {
  178. m = mxGetM(ary);
  179. n = mxGetN(ary);
  180. M = Eigen::Matrix<unsigned int, Eigen::Dynamic, Eigen::Dynamic >(m,n);
  181. double *pM = mxGetPr(ary);
  182. int c = 0;
  183. for(int j=0; j<M.cols();++j)
  184. for(int i=0; i<M.rows();++i)
  185. M(i,j) = (unsigned int)(pM[c++])-1;
  186. }
  187. mxDestroyArray(ary);
  188. }
  189. // Send a single scalar to MATLAB
  190. IGL_INLINE void igl::mlsetscalar(Engine** mlengine, std::string name, double s)
  191. {
  192. if (*mlengine == 0)
  193. mlinit(mlengine);
  194. Eigen::MatrixXd M(1,1);
  195. M(0,0) = s;
  196. mlsetmatrix(mlengine, name, M);
  197. }
  198. // Receive a single scalar from MATLAB
  199. IGL_INLINE double igl::mlgetscalar(Engine** mlengine, std::string name)
  200. {
  201. if (*mlengine == 0)
  202. mlinit(mlengine);
  203. Eigen::MatrixXd M;
  204. mlgetmatrix(mlengine, name,M);
  205. return M(0,0);
  206. }
  207. // Execute arbitrary MATLAB code and return the MATLAB output
  208. IGL_INLINE std::string igl::mleval(Engine** mlengine, std::string code)
  209. {
  210. if (*mlengine == 0)
  211. mlinit(mlengine);
  212. const char *matlab_code = code.c_str();
  213. const int BUF_SIZE = 4096*4096;
  214. // allocate on the heap to avoid running out of stack
  215. std::string bufauto(BUF_SIZE+1, '\0');
  216. char *buf = &bufauto[0];
  217. assert(matlab_code != NULL);
  218. // Use RAII ensure that on leaving this scope, the output buffer is
  219. // always nullified (to prevent Matlab from accessing memory that might
  220. // have already been deallocated).
  221. struct cleanup {
  222. Engine *m_ep;
  223. cleanup(Engine *ep) : m_ep(ep) { }
  224. ~cleanup() { engOutputBuffer(m_ep, NULL, 0); }
  225. } cleanup_obj(*mlengine);
  226. if (buf != NULL)
  227. engOutputBuffer(*mlengine, buf, BUF_SIZE);
  228. int res = engEvalString(*mlengine, matlab_code);
  229. if (res != 0) {
  230. std::ostringstream oss;
  231. oss << "ERROR: Matlab command failed with error code " << res << ".\n";
  232. return oss.str();
  233. }
  234. if (buf[0] == '>' && buf[1] == '>' && buf[2] == ' ')
  235. buf += 3;
  236. if (buf[0] == '\n') ++buf;
  237. return std::string(buf);
  238. }
  239. // Send a sparse matrix
  240. IGL_INLINE void igl::mlsetmatrix(Engine** mlengine, std::string name, const Eigen::SparseMatrix<double>& M)
  241. {
  242. int count = 0;
  243. // // Count non-zero
  244. // for (unsigned k=0; k<M.outerSize(); ++k)
  245. // for (Eigen::SparseMatrix<double>::InnerIterator it(M,k); it; ++it)
  246. // if (it.value() != 0)
  247. // ++count;
  248. Eigen::MatrixXd T(M.nonZeros(),3);
  249. for (unsigned k=0; k<M.outerSize(); ++k)
  250. {
  251. for (Eigen::SparseMatrix<double>::InnerIterator it(M,k); it; ++it)
  252. {
  253. T(count,0) = it.row();
  254. T(count,1) = it.col();
  255. T(count,2) = it.value();
  256. ++count;
  257. }
  258. }
  259. T.col(0) = T.col(0).array()+1;
  260. T.col(1) = T.col(1).array()+1;
  261. mlsetmatrix(mlengine,"temp93765",T);
  262. std::string temp = name + " = sparse(temp93765(:,1),temp93765(:,2),temp93765(:,3),"
  263. + std::to_string(M.rows()) + ","
  264. + std::to_string(M.cols()) + ");";
  265. mleval(mlengine,temp);
  266. mleval(mlengine,"clear temp93765");
  267. }