matlabinterface.h 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305
  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 = /Applications/MATLAB_R2011a.app/bin/maci64
  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. // Receive a matrix from MATLAB
  39. inline void mlgetmatrix(Engine** engine, std::string name, Eigen::MatrixXd& M);
  40. // Receive a matrix from MATLAB
  41. inline void mlgetmatrix(Engine** engine, std::string name, Eigen::MatrixXf& M);
  42. // Receive a matrix from MATLAB
  43. inline void mlgetmatrix(Engine** engine, std::string name, Eigen::MatrixXi& M);
  44. // Send a single scalar to MATLAB
  45. inline void mlsetscalar(Engine** engine, std::string name, double s);
  46. // Receive a single scalar from MATLAB
  47. inline double mlgetscalar(Engine** engine, std::string name);
  48. // Execute arbitrary MATLAB code and return the MATLAB output
  49. inline std::string mleval(Engine** engine, std::string code);
  50. }
  51. // Implementation
  52. // Init the MATLAB engine
  53. // (no need to call it directly since it is automatically invoked by any other command)
  54. inline void igl::mlinit(Engine** mlengine)
  55. {
  56. *mlengine = engOpen("\0");
  57. }
  58. // Closes the MATLAB engine
  59. inline void igl::mlclose(Engine** mlengine)
  60. {
  61. engClose(*mlengine);
  62. *mlengine = 0;
  63. }
  64. // Send a matrix to MATLAB
  65. inline void igl::mlsetmatrix(Engine** mlengine, std::string name, const Eigen::MatrixXd& 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));
  75. engPutVariable(*mlengine, name.c_str(), A);
  76. mxDestroyArray(A);
  77. }
  78. // Send a matrix to MATLAB
  79. inline void igl::mlsetmatrix(Engine** mlengine, std::string name, const Eigen::MatrixXf& M)
  80. {
  81. if (*mlengine == 0)
  82. mlinit(mlengine);
  83. mxArray *A = mxCreateDoubleMatrix(M.rows(), M.cols(), mxREAL);
  84. double *pM = mxGetPr(A);
  85. int c = 0;
  86. for(int j=0; j<M.cols();++j)
  87. for(int i=0; i<M.rows();++i)
  88. pM[c++] = double(M(i,j));
  89. engPutVariable(*mlengine, name.c_str(), A);
  90. mxDestroyArray(A);
  91. }
  92. // Send a matrix to MATLAB
  93. inline void igl::mlsetmatrix(Engine** mlengine, std::string name, const Eigen::MatrixXi& M)
  94. {
  95. if (*mlengine == 0)
  96. mlinit(mlengine);
  97. mxArray *A = mxCreateDoubleMatrix(M.rows(), M.cols(), mxREAL);
  98. double *pM = mxGetPr(A);
  99. int c = 0;
  100. for(int j=0; j<M.cols();++j)
  101. for(int i=0; i<M.rows();++i)
  102. pM[c++] = double(M(i,j))+1;
  103. engPutVariable(*mlengine, name.c_str(), A);
  104. mxDestroyArray(A);
  105. }
  106. // Receive a matrix from MATLAB
  107. inline void igl::mlgetmatrix(Engine** mlengine, std::string name, Eigen::MatrixXd& M)
  108. {
  109. if (*mlengine == 0)
  110. mlinit(mlengine);
  111. unsigned long m = 0;
  112. unsigned long n = 0;
  113. std::vector<double> t;
  114. mxArray *ary = engGetVariable(*mlengine, name.c_str());
  115. if (ary == NULL)
  116. {
  117. m = 0;
  118. n = 0;
  119. M = Eigen::MatrixXd(0,0);
  120. }
  121. else
  122. {
  123. m = mxGetM(ary);
  124. n = mxGetN(ary);
  125. M = Eigen::MatrixXd(m,n);
  126. double *pM = mxGetPr(ary);
  127. int c = 0;
  128. for(int j=0; j<M.cols();++j)
  129. for(int i=0; i<M.rows();++i)
  130. M(i,j) = pM[c++];
  131. }
  132. mxDestroyArray(ary);
  133. }
  134. inline void igl::mlgetmatrix(Engine** mlengine, std::string name, Eigen::MatrixXf& 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::MatrixXf(0,0);
  147. }
  148. else
  149. {
  150. m = mxGetM(ary);
  151. n = mxGetN(ary);
  152. M = Eigen::MatrixXf(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) = pM[c++];
  158. }
  159. mxDestroyArray(ary);
  160. }
  161. // Receive a matrix from MATLAB
  162. inline void igl::mlgetmatrix(Engine** mlengine, std::string name, Eigen::MatrixXi& 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::MatrixXi(0,0);
  175. }
  176. else
  177. {
  178. m = mxGetM(ary);
  179. n = mxGetN(ary);
  180. M = Eigen::MatrixXi(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) = int(pM[c++])-1;
  186. }
  187. mxDestroyArray(ary);
  188. }
  189. // Send a single scalar to MATLAB
  190. 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. 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. 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. #endif