matlabinterface.cpp 6.4 KB

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