diff --git a/example2.cpp b/example2.cpp new file mode 100644 index 000000000..26767476f --- /dev/null +++ b/example2.cpp @@ -0,0 +1,55 @@ +// +// IGL Lib - Simple C++ mesh library +// +// Copyright 2011, Daniele Panozzo. All rights reserved. +// +// +// Example that shows the integration with matlab +// + +// IMPORTANT DO NOT REMOVE OR MOVE +#define EIGEN_YES_I_KNOW_SPARSE_MODULE_IS_NOT_STABLE_YET + +#include +#include +#include + +#include + +using namespace std; + +int main (int argc, const char * argv[]) +{ + // read the header of matlabinterface.h for compilation instructions + + Eigen::MatrixXd V,V2; + Eigen::MatrixXi F,F2; + + // Read mesh from file + igl::read(string(argv[1]),V,F); + + // Send mesh to matlab + igl::mlsetmatrix("V",V); + igl::mlsetmatrix("F",F); + + // Plot the mesh from matlab + igl::mleval("trimesh(F,V(:,1),V(:,2),V(:,3))"); + + // Receive mesh from matlab + igl::mlgetmatrix("V",V2); + igl::mlgetmatrix("F",F2); + + // Plot the received mesh + cerr << "V " << endl << V2 << endl; + cerr << "F " << endl << F2 << endl; + + // It is also possible to send scalars + igl::mlsetscalar("s", 3); + cerr << "s = " << igl::mlgetscalar("s") << endl; + + // If the program closes the matlab session is killed too.. + getchar(); + + return 0; +} + diff --git a/main.cpp b/main.cpp index 4fd85af02..26767476f 100644 --- a/main.cpp +++ b/main.cpp @@ -2,6 +2,10 @@ // IGL Lib - Simple C++ mesh library // // Copyright 2011, Daniele Panozzo. All rights reserved. +// +// +// Example that shows the integration with matlab +// // IMPORTANT DO NOT REMOVE OR MOVE #define EIGEN_YES_I_KNOW_SPARSE_MODULE_IS_NOT_STABLE_YET @@ -9,52 +13,42 @@ #include #include #include -#include -#include -#include -#include + +#include using namespace std; int main (int argc, const char * argv[]) { - Eigen::MatrixXd V; - Eigen::MatrixXi F; + // read the header of matlabinterface.h for compilation instructions + + Eigen::MatrixXd V,V2; + Eigen::MatrixXi F,F2; + + // Read mesh from file igl::read(string(argv[1]),V,F); + + // Send mesh to matlab + igl::mlsetmatrix("V",V); + igl::mlsetmatrix("F",F); - std::cout << "Mesh loaded!\n"; - cout << "Vertex Array:" << endl; - cout << V << endl; - cout << "-------------" << endl; - cout << "Face Array:" << endl; - cout << F << endl; - cout << "-------------" << endl; + // Plot the mesh from matlab + igl::mleval("trimesh(F,V(:,1),V(:,2),V(:,3))"); - cout << "CotMatrix:" << endl; - Eigen::SparseMatrix L; - igl::cotmatrix(V,F,L); - cout << L << endl; - cout << "-------------" << endl; - - igl::write(string(argv[2]),V,F); - - // Face Topology - cout << "TT Topology:" << endl; - Eigen::MatrixXi TT; - igl::tt(V,F,TT); - cout << TT << endl; - cout << "-------------" << endl; + // Receive mesh from matlab + igl::mlgetmatrix("V",V2); + igl::mlgetmatrix("F",F2); - // Edge Topology - cout << "Edge Topology:" << endl; - Eigen::MatrixXi EV; - Eigen::MatrixXi FE; - Eigen::MatrixXi EF; - - igl::edgetopology(V,F,EV,FE, EF); - cout << EV << endl << FE << endl << EF << endl; - cout << "-------------" << endl; + // Plot the received mesh + cerr << "V " << endl << V2 << endl; + cerr << "F " << endl << F2 << endl; + // It is also possible to send scalars + igl::mlsetscalar("s", 3); + cerr << "s = " << igl::mlgetscalar("s") << endl; + + // If the program closes the matlab session is killed too.. + getchar(); return 0; } diff --git a/matlabinterface.h b/matlabinterface.h new file mode 100644 index 000000000..9ef6f6dbe --- /dev/null +++ b/matlabinterface.h @@ -0,0 +1,220 @@ +// +// IGL Lib - Simple C++ mesh library +// +// Copyright 2011, Daniele Panozzo. All rights reserved. + +// WARNING: These functions require matlab installed +// Additional header folder required: +// /Applications/MATLAB_R2010b.app/extern/include +// Additional binary lib to be linked with: +// /Applications/MATLAB_R2010b.app/bin/maci64/libeng.dylib +// /Applications/MATLAB_R2010b.app/bin/maci64/libmx.dylib + +// MAC ONLY: +// Add to the environment variables: +// DYLD_LIBRARY_PATH = /Applications/MATLAB_R2010b.app/bin/maci64 +// PATH = /opt/local/bin:/opt/local/sbin:/Applications/MATLAB_R2010b.app/bin:/usr/bin:/bin:/usr/sbin:/sbin:/usr/local/bin:/usr/texbin:/usr/X11/bin + +#ifndef MATLAB_INTERFACE_H +#define MATLAB_INTERFACE_H + +#include +#include + +#include +#include +#include +#include +#include + +#include "engine.h" // Matlab engine header + +namespace igl +{ + // Global pointer to the matlab engine + Engine* mlengine = 0; + + // Init the MATLAB engine + // (no need to call it directly since it is automatically invoked by any other command) + void mlinit() + { + mlengine = engOpen("\0"); + } + + // Closes the MATLAB engine + void mlclose(Engine* engine) + { + engClose(mlengine); + mlengine = 0; + } + + // Send a matrix to MATLAB + void mlsetmatrix(std::string name, Eigen::MatrixXd& M) + { + if (mlengine == 0) + mlinit(); + + mxArray *A = mxCreateDoubleMatrix(M.rows(), M.cols(), mxREAL); + double *pM = mxGetPr(A); + + int c = 0; + for(int j=0; j t; + + mxArray *ary = engGetVariable(mlengine, name.c_str()); + if (ary == NULL) + { + m = 0; + n = 0; + M = Eigen::MatrixXd(0,0); + } + else + { + m = mxGetM(ary); + n = mxGetN(ary); + M = Eigen::MatrixXd(m,n); + + double *pM = mxGetPr(ary); + + int c = 0; + for(int j=0; j t; + + mxArray *ary = engGetVariable(mlengine, name.c_str()); + if (ary == NULL) + { + m = 0; + n = 0; + M = Eigen::MatrixXi(0,0); + } + else + { + m = mxGetM(ary); + n = mxGetN(ary); + M = Eigen::MatrixXi(m,n); + + double *pM = mxGetPr(ary); + + int c = 0; + for(int j=0; j' && buf[1] == '>' && buf[2] == ' ') + buf += 3; + if (buf[0] == '\n') ++buf; + + return std::string(buf); + } + +} + +#endif \ No newline at end of file