// This file is part of libigl, a simple c++ geometry processing library. // // Copyright (C) 2013 Alec Jacobson // // This Source Code Form is subject to the terms of the Mozilla Public License // v. 2.0. If a copy of the MPL was not distributed with this file, You can // obtain one at http://mozilla.org/MPL/2.0/. #include // Implementation // Init the MATLAB engine // (no need to call it directly since it is automatically invoked by any other command) IGL_INLINE void igl::matlab::mlinit(Engine** mlengine) { *mlengine = engOpen("\0"); } // Closes the MATLAB engine IGL_INLINE void igl::matlab::mlclose(Engine** mlengine) { engClose(*mlengine); *mlengine = 0; } // Send a matrix to MATLAB IGL_INLINE void igl::matlab::mlsetmatrix(Engine** mlengine, std::string name, const Eigen::MatrixXd& M) { if (*mlengine == 0) mlinit(mlengine); mxArray *A = mxCreateDoubleMatrix(M.rows(), M.cols(), mxREAL); double *pM = mxGetPr(A); int c = 0; for(int j=0; j& M) { if (*mlengine == 0) mlinit(mlengine); 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::MatrixXf(0,0); } else { m = mxGetM(ary); n = mxGetN(ary); M = Eigen::MatrixXf(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& M) { if (*mlengine == 0) mlinit(mlengine); unsigned long m = 0; unsigned long n = 0; std::vector t; mxArray *ary = engGetVariable(*mlengine, name.c_str()); if (ary == NULL) { m = 0; n = 0; M = Eigen::Matrix(0,0); } else { m = mxGetM(ary); n = mxGetN(ary); M = Eigen::Matrix(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); } // Send a sparse matrix IGL_INLINE void igl::matlab::mlsetmatrix(Engine** mlengine, std::string name, const Eigen::SparseMatrix& M) { int count = 0; // // Count non-zero // for (unsigned k=0; k::InnerIterator it(M,k); it; ++it) // if (it.value() != 0) // ++count; Eigen::MatrixXd T(M.nonZeros(),3); for (unsigned k=0; k<(unsigned)M.outerSize(); ++k) { for (Eigen::SparseMatrix::InnerIterator it(M,k); it; ++it) { T(count,0) = it.row(); T(count,1) = it.col(); T(count,2) = it.value(); ++count; } } T.col(0) = T.col(0).array()+1; T.col(1) = T.col(1).array()+1; mlsetmatrix(mlengine,"temp93765",T); std::string temp = name + " = sparse(temp93765(:,1),temp93765(:,2),temp93765(:,3)," + std::to_string(M.rows()) + "," + std::to_string(M.cols()) + ");"; mleval(mlengine,temp); mleval(mlengine,"clear temp93765"); }