matlabinterface.h 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364
  1. //
  2. // IGL Lib - Simple C++ mesh library
  3. //
  4. // Copyright 2011, Daniele Panozzo. All rights reserved.
  5. // WARNING: These functions require matlab installed
  6. // Additional header folder required:
  7. // /Applications/MATLAB_R2011a.app/extern/include
  8. // Additional binary lib to be linked with:
  9. // /Applications/MATLAB_R2011a.app/bin/maci64/libeng.dylib
  10. // /Applications/MATLAB_R2011a.app/bin/maci64/libmx.dylib
  11. // MAC ONLY:
  12. // Add to the environment variables:
  13. // DYLD_LIBRARY_PATH =
  14. // PATH = /opt/local/bin:/opt/local/sbin:/Applications/MATLAB_R2011a.app/bin:/usr/bin:/bin:/usr/sbin:/sbin:/usr/local/bin:/usr/texbin:/usr/X11/bin
  15. #ifndef IGL_MATLAB_INTERFACE_H
  16. #define IGL_MATLAB_INTERFACE_H
  17. #include <Eigen/Core>
  18. #include <string>
  19. #include <complex>
  20. #include <cassert>
  21. #include <map>
  22. #include <string>
  23. #include <vector>
  24. #include "engine.h" // Matlab engine header
  25. namespace igl
  26. {
  27. // Init the MATLAB engine
  28. // (no need to call it directly since it is automatically invoked by any other command)
  29. inline void mlinit(Engine** engine);
  30. // Closes the MATLAB engine
  31. inline void mlclose(Engine** engine);
  32. // Send a matrix to MATLAB
  33. inline void mlsetmatrix(Engine** engine, std::string name, const Eigen::MatrixXd& M);
  34. // Send a matrix to MATLAB
  35. inline void mlsetmatrix(Engine** engine, std::string name, const Eigen::MatrixXf& M);
  36. // Send a matrix to MATLAB
  37. inline void mlsetmatrix(Engine** engine, std::string name, const Eigen::MatrixXi& M);
  38. // Send a matrix to MATLAB
  39. inline void mlsetmatrix(Engine** mlengine, std::string name, const Eigen::Matrix<unsigned int, Eigen::Dynamic, Eigen::Dynamic >& M);
  40. // Receive a matrix from MATLAB
  41. inline void mlgetmatrix(Engine** engine, std::string name, Eigen::MatrixXd& M);
  42. // Receive a matrix from MATLAB
  43. inline void mlgetmatrix(Engine** engine, std::string name, Eigen::MatrixXf& M);
  44. // Receive a matrix from MATLAB
  45. inline void mlgetmatrix(Engine** engine, std::string name, Eigen::MatrixXi& M);
  46. // Receive a matrix from MATLAB
  47. inline void mlgetmatrix(Engine** mlengine, std::string name, Eigen::Matrix<unsigned int, Eigen::Dynamic, Eigen::Dynamic >& M);
  48. // Send a single scalar to MATLAB
  49. inline void mlsetscalar(Engine** engine, std::string name, double s);
  50. // Receive a single scalar from MATLAB
  51. inline double mlgetscalar(Engine** engine, std::string name);
  52. // Execute arbitrary MATLAB code and return the MATLAB output
  53. inline std::string mleval(Engine** engine, std::string code);
  54. }
  55. // Implementation
  56. // Init the MATLAB engine
  57. // (no need to call it directly since it is automatically invoked by any other command)
  58. inline void igl::mlinit(Engine** mlengine)
  59. {
  60. *mlengine = engOpen("\0");
  61. }
  62. // Closes the MATLAB engine
  63. inline void igl::mlclose(Engine** mlengine)
  64. {
  65. engClose(*mlengine);
  66. *mlengine = 0;
  67. }
  68. // Send a matrix to MATLAB
  69. inline void igl::mlsetmatrix(Engine** mlengine, std::string name, const Eigen::MatrixXd& M)
  70. {
  71. if (*mlengine == 0)
  72. mlinit(mlengine);
  73. mxArray *A = mxCreateDoubleMatrix(M.rows(), M.cols(), mxREAL);
  74. double *pM = mxGetPr(A);
  75. int c = 0;
  76. for(int j=0; j<M.cols();++j)
  77. for(int i=0; i<M.rows();++i)
  78. pM[c++] = double(M(i,j));
  79. engPutVariable(*mlengine, name.c_str(), A);
  80. mxDestroyArray(A);
  81. }
  82. // Send a matrix to MATLAB
  83. inline void igl::mlsetmatrix(Engine** mlengine, std::string name, const Eigen::MatrixXf& M)
  84. {
  85. if (*mlengine == 0)
  86. mlinit(mlengine);
  87. mxArray *A = mxCreateDoubleMatrix(M.rows(), M.cols(), mxREAL);
  88. double *pM = mxGetPr(A);
  89. int c = 0;
  90. for(int j=0; j<M.cols();++j)
  91. for(int i=0; i<M.rows();++i)
  92. pM[c++] = double(M(i,j));
  93. engPutVariable(*mlengine, name.c_str(), A);
  94. mxDestroyArray(A);
  95. }
  96. // Send a matrix to MATLAB
  97. inline void igl::mlsetmatrix(Engine** mlengine, std::string name, const Eigen::MatrixXi& M)
  98. {
  99. if (*mlengine == 0)
  100. mlinit(mlengine);
  101. mxArray *A = mxCreateDoubleMatrix(M.rows(), M.cols(), mxREAL);
  102. double *pM = mxGetPr(A);
  103. int c = 0;
  104. for(int j=0; j<M.cols();++j)
  105. for(int i=0; i<M.rows();++i)
  106. pM[c++] = double(M(i,j))+1;
  107. engPutVariable(*mlengine, name.c_str(), A);
  108. mxDestroyArray(A);
  109. }
  110. // Send a matrix to MATLAB
  111. inline void igl::mlsetmatrix(Engine** mlengine, std::string name, const Eigen::Matrix<unsigned int, Eigen::Dynamic, Eigen::Dynamic >& M)
  112. {
  113. if (*mlengine == 0)
  114. mlinit(mlengine);
  115. mxArray *A = mxCreateDoubleMatrix(M.rows(), M.cols(), mxREAL);
  116. double *pM = mxGetPr(A);
  117. int c = 0;
  118. for(int j=0; j<M.cols();++j)
  119. for(int i=0; i<M.rows();++i)
  120. pM[c++] = double(M(i,j))+1;
  121. engPutVariable(*mlengine, name.c_str(), A);
  122. mxDestroyArray(A);
  123. }
  124. // Receive a matrix from MATLAB
  125. inline void igl::mlgetmatrix(Engine** mlengine, std::string name, Eigen::MatrixXd& M)
  126. {
  127. if (*mlengine == 0)
  128. mlinit(mlengine);
  129. unsigned long m = 0;
  130. unsigned long n = 0;
  131. std::vector<double> t;
  132. mxArray *ary = engGetVariable(*mlengine, name.c_str());
  133. if (ary == NULL)
  134. {
  135. m = 0;
  136. n = 0;
  137. M = Eigen::MatrixXd(0,0);
  138. }
  139. else
  140. {
  141. m = mxGetM(ary);
  142. n = mxGetN(ary);
  143. M = Eigen::MatrixXd(m,n);
  144. double *pM = mxGetPr(ary);
  145. int c = 0;
  146. for(int j=0; j<M.cols();++j)
  147. for(int i=0; i<M.rows();++i)
  148. M(i,j) = pM[c++];
  149. }
  150. mxDestroyArray(ary);
  151. }
  152. inline void igl::mlgetmatrix(Engine** mlengine, std::string name, Eigen::MatrixXf& M)
  153. {
  154. if (*mlengine == 0)
  155. mlinit(mlengine);
  156. unsigned long m = 0;
  157. unsigned long n = 0;
  158. std::vector<double> t;
  159. mxArray *ary = engGetVariable(*mlengine, name.c_str());
  160. if (ary == NULL)
  161. {
  162. m = 0;
  163. n = 0;
  164. M = Eigen::MatrixXf(0,0);
  165. }
  166. else
  167. {
  168. m = mxGetM(ary);
  169. n = mxGetN(ary);
  170. M = Eigen::MatrixXf(m,n);
  171. double *pM = mxGetPr(ary);
  172. int c = 0;
  173. for(int j=0; j<M.cols();++j)
  174. for(int i=0; i<M.rows();++i)
  175. M(i,j) = pM[c++];
  176. }
  177. mxDestroyArray(ary);
  178. }
  179. // Receive a matrix from MATLAB
  180. inline void igl::mlgetmatrix(Engine** mlengine, std::string name, Eigen::MatrixXi& M)
  181. {
  182. if (*mlengine == 0)
  183. mlinit(mlengine);
  184. unsigned long m = 0;
  185. unsigned long n = 0;
  186. std::vector<double> t;
  187. mxArray *ary = engGetVariable(*mlengine, name.c_str());
  188. if (ary == NULL)
  189. {
  190. m = 0;
  191. n = 0;
  192. M = Eigen::MatrixXi(0,0);
  193. }
  194. else
  195. {
  196. m = mxGetM(ary);
  197. n = mxGetN(ary);
  198. M = Eigen::MatrixXi(m,n);
  199. double *pM = mxGetPr(ary);
  200. int c = 0;
  201. for(int j=0; j<M.cols();++j)
  202. for(int i=0; i<M.rows();++i)
  203. M(i,j) = int(pM[c++])-1;
  204. }
  205. mxDestroyArray(ary);
  206. }
  207. // Receive a matrix from MATLAB
  208. inline void igl::mlgetmatrix(Engine** mlengine, std::string name, Eigen::Matrix<unsigned int, Eigen::Dynamic, Eigen::Dynamic >& M)
  209. {
  210. if (*mlengine == 0)
  211. mlinit(mlengine);
  212. unsigned long m = 0;
  213. unsigned long n = 0;
  214. std::vector<double> t;
  215. mxArray *ary = engGetVariable(*mlengine, name.c_str());
  216. if (ary == NULL)
  217. {
  218. m = 0;
  219. n = 0;
  220. M = Eigen::Matrix<unsigned int, Eigen::Dynamic, Eigen::Dynamic >(0,0);
  221. }
  222. else
  223. {
  224. m = mxGetM(ary);
  225. n = mxGetN(ary);
  226. M = Eigen::Matrix<unsigned int, Eigen::Dynamic, Eigen::Dynamic >(m,n);
  227. double *pM = mxGetPr(ary);
  228. int c = 0;
  229. for(int j=0; j<M.cols();++j)
  230. for(int i=0; i<M.rows();++i)
  231. M(i,j) = (unsigned int)(pM[c++])-1;
  232. }
  233. mxDestroyArray(ary);
  234. }
  235. // Send a single scalar to MATLAB
  236. inline void igl::mlsetscalar(Engine** mlengine, std::string name, double s)
  237. {
  238. if (*mlengine == 0)
  239. mlinit(mlengine);
  240. Eigen::MatrixXd M(1,1);
  241. M(0,0) = s;
  242. mlsetmatrix(mlengine, name, M);
  243. }
  244. // Receive a single scalar from MATLAB
  245. inline double igl::mlgetscalar(Engine** mlengine, std::string name)
  246. {
  247. if (*mlengine == 0)
  248. mlinit(mlengine);
  249. Eigen::MatrixXd M;
  250. mlgetmatrix(mlengine, name,M);
  251. return M(0,0);
  252. }
  253. // Execute arbitrary MATLAB code and return the MATLAB output
  254. inline std::string igl::mleval(Engine** mlengine, std::string code)
  255. {
  256. if (*mlengine == 0)
  257. mlinit(mlengine);
  258. const char *matlab_code = code.c_str();
  259. const int BUF_SIZE = 4096*4096;
  260. // allocate on the heap to avoid running out of stack
  261. std::string bufauto(BUF_SIZE+1, '\0');
  262. char *buf = &bufauto[0];
  263. assert(matlab_code != NULL);
  264. // Use RAII ensure that on leaving this scope, the output buffer is
  265. // always nullified (to prevent Matlab from accessing memory that might
  266. // have already been deallocated).
  267. struct cleanup {
  268. Engine *m_ep;
  269. cleanup(Engine *ep) : m_ep(ep) { }
  270. ~cleanup() { engOutputBuffer(m_ep, NULL, 0); }
  271. } cleanup_obj(*mlengine);
  272. if (buf != NULL)
  273. engOutputBuffer(*mlengine, buf, BUF_SIZE);
  274. int res = engEvalString(*mlengine, matlab_code);
  275. if (res != 0) {
  276. std::ostringstream oss;
  277. oss << "ERROR: Matlab command failed with error code " << res << ".\n";
  278. return oss.str();
  279. }
  280. if (buf[0] == '>' && buf[1] == '>' && buf[2] == ' ')
  281. buf += 3;
  282. if (buf[0] == '\n') ++buf;
  283. return std::string(buf);
  284. }
  285. #endif