#ifndef IGL_WRITE_MATLAB_WORKSPACE #define IGL_WRITE_MATLAB_WORKSPACE #include #include #include #include #include namespace igl { // Class which contains data of a matlab workspace which can be written to a // .mat file and loaded from matlab // // This depends on matlab at compile time (though it shouldn't necessarily // have to) but it does not depend on running the matlab engine at run-time. // // Known bugs: Treats all matrices as doubles (this may actually be desired // for some "index" matrices since matlab's sparse command takes doubles // rather than int class matrices). It is of course not desired when dealing // with logicals or uint's for images. class MatlabWorkspace { private: // KNOWN BUG: Why not use a map? Any reason to allow duplicate names? // // List of names std::vector names; // List of data pointers std::vector data; public: MatlabWorkspace(); ~MatlabWorkspace(); // Clear names and data of variables in workspace inline void clear(); // Save current list of variables // // Inputs: // path path to .mat file // Returns true on success, false on failure inline bool write(const std::string & path) const; // Load list of variables from .mat file // // Inputs: // path path to .mat file // Returns true on success, false on failure inline bool read(const std::string & path); // Assign data to a variable name in the workspace // // Template: // DerivedM eigen matrix (e.g. MatrixXd) // Inputs: // M data (usually a matrix) // name variable name to save into work space // Returns true on success, false on failure // // Known Bugs: Assumes Eigen is using column major ordering template inline MatlabWorkspace& save( const Eigen::PlainObjectBase& M, const std::string & name); // Template: // MT sparse matrix type (e.g. double) template inline MatlabWorkspace& save( const Eigen::SparseMatrix& M, const std::string & name); // Templates: // ScalarM scalar type, e.g. double template inline MatlabWorkspace& save( const std::vector > & vM, const std::string & name); // Templates: // ScalarV scalar type, e.g. double template inline MatlabWorkspace& save( const std::vector & vV, const std::string & name); // NOTE: Eigen stores quaternions coefficients as (i,j,k,1), but most of // our matlab code stores them as (1,i,j,k) This takes a quaternion and // saves it as a (1,i,j,k) row vector // // Templates: // Q quaternion type template inline MatlabWorkspace& save( const Eigen::Quaternion & q, const std::string & name); inline MatlabWorkspace& save( const double d, const std::string & name); // Same as save() but adds 1 to each element, useful for saving "index" // matrices like lists of faces or elements template inline MatlabWorkspace& save_index( const Eigen::PlainObjectBase& M, const std::string & name); template inline MatlabWorkspace& save_index( const std::vector > & vM, const std::string & name); template inline MatlabWorkspace& save_index( const std::vector & vV, const std::string & name); // Find a certain matrix by name. // // KNOWN BUG: Outputs the first found (not necessarily unique lists). // // Template: // DerivedM eigen matrix (e.g. MatrixXd) // Inputs: // name exact name of matrix as string // Outputs: // M matrix // Returns true only if found. template inline bool find( const std::string & name, Eigen::PlainObjectBase& M); template inline bool find( const std::string & name, Eigen::SparseMatrix& M); inline bool find( const std::string & name, double & d); inline bool find( const std::string & name, int & v); // Subtracts 1 from all entries template inline bool find_index( const std::string & name, Eigen::PlainObjectBase& M); }; } // Implementation // Be sure that this is not compiled into libigl.a // http://stackoverflow.com/a/3318993/148668 // IGL #include "igl/list_to_matrix.h" // MATLAB #include "mat.h" // STL #include #include #include inline igl::MatlabWorkspace::MatlabWorkspace(): names(), data() { } inline igl::MatlabWorkspace::~MatlabWorkspace() { // clean up data clear(); } inline void igl::MatlabWorkspace::clear() { for_each(data.begin(),data.end(),&mxDestroyArray); data.clear(); names.clear(); } inline bool igl::MatlabWorkspace::write(const std::string & path) const { using namespace std; MATFile * mat_file = matOpen(path.c_str(), "w"); assert(names.size() == data.size()); // loop over names and data for(int i = 0;i < (int)names.size(); i++) { // Put variable as LOCAL variable int status = matPutVariable(mat_file,names[i].c_str(), data[i]); if(status != 0) { cerr<<"^MatlabWorkspace::save Error: matPutVariable ("< inline igl::MatlabWorkspace& igl::MatlabWorkspace::save( const Eigen::PlainObjectBase& M, const std::string & name) { using namespace std; const int m = M.rows(); const int n = M.cols(); mxArray * mx_data = mxCreateDoubleMatrix(m,n,mxREAL); data.push_back(mx_data); names.push_back(name); // Copy data immediately // Q: Won't this be incorrect for integers? copy(M.data(),M.data()+M.size(),mxGetPr(mx_data)); return *this; } // Treat everything as a double template inline igl::MatlabWorkspace& igl::MatlabWorkspace::save( const Eigen::SparseMatrix& M, const std::string & name) { using namespace std; const int m = M.rows(); const int n = M.cols(); // THIS WILL NOT WORK FOR ROW-MAJOR assert(n==M.outerSize()); const int nzmax = M.nonZeros(); mxArray * mx_data = mxCreateSparse(m, n, nzmax, mxREAL); data.push_back(mx_data); names.push_back(name); // Copy data immediately double * pr = mxGetPr(mx_data); mwIndex * ir = mxGetIr(mx_data); mwIndex * jc = mxGetJc(mx_data); // Iterate over outside int k = 0; for(int j=0; j::InnerIterator it (M,j); it; ++it) { pr[k] = it.value(); ir[k] = it.row(); k++; } } jc[M.outerSize()] = k; return *this; } template inline igl::MatlabWorkspace& igl::MatlabWorkspace::save( const std::vector > & vM, const std::string & name) { Eigen::MatrixXd M; list_to_matrix(vM,M); return this->save(M,name); } template inline igl::MatlabWorkspace& igl::MatlabWorkspace::save( const std::vector & vV, const std::string & name) { Eigen::MatrixXd V; list_to_matrix(vV,V); return this->save(V,name); } template inline igl::MatlabWorkspace& igl::MatlabWorkspace::save( const Eigen::Quaternion & q, const std::string & name) { Eigen::Matrix qm; qm(0,0) = q.w(); qm(0,1) = q.x(); qm(0,2) = q.y(); qm(0,3) = q.z(); return save(qm,name); } inline igl::MatlabWorkspace& igl::MatlabWorkspace::save( const double d, const std::string & name) { Eigen::VectorXd v(1); v(0) = d; return save(v,name); } template inline igl::MatlabWorkspace& igl::MatlabWorkspace::save_index( const Eigen::PlainObjectBase& M, const std::string & name) { DerivedM Mp1 = M; Mp1.array() += 1; return this->save(Mp1,name); } template inline igl::MatlabWorkspace& igl::MatlabWorkspace::save_index( const std::vector > & vM, const std::string & name) { Eigen::MatrixXd M; list_to_matrix(vM,M); return this->save_index(M,name); } template inline igl::MatlabWorkspace& igl::MatlabWorkspace::save_index( const std::vector & vV, const std::string & name) { Eigen::MatrixXd V; list_to_matrix(vV,V); return this->save_index(V,name); } template inline bool igl::MatlabWorkspace::find( const std::string & name, Eigen::PlainObjectBase& M) { using namespace std; const int i = std::find(names.begin(), names.end(), name)-names.begin(); if(i>=(int)names.size()) { return false; } assert(i<=(int)data.size()); mxArray * mx_data = data[i]; assert(!mxIsSparse(mx_data)); assert(mxGetNumberOfDimensions(mx_data) == 2); //cout< inline bool igl::MatlabWorkspace::find( const std::string & name, Eigen::SparseMatrix& M) { using namespace std; using namespace Eigen; const int i = std::find(names.begin(), names.end(), name)-names.begin(); if(i>=(int)names.size()) { return false; } assert(i<=(int)data.size()); mxArray * mx_data = data[i]; // Handle boring case where matrix is actually an empty dense matrix if(mxGetNumberOfElements(mx_data) == 0) { M.resize(0,0); return true; } assert(mxIsSparse(mx_data)); assert(mxGetNumberOfDimensions(mx_data) == 2); //cout< > MIJV; MIJV.reserve(mxGetNumberOfElements(mx_data)); // Iterate over outside int k = 0; for(int j=0; j(ir[k],j,pr[k])); k++; } } M.resize(m,n); M.setFromTriplets(MIJV.begin(),MIJV.end()); return true; } inline bool igl::MatlabWorkspace::find( const std::string & name, int & v) { using namespace std; const int i = std::find(names.begin(), names.end(), name)-names.begin(); if(i>=(int)names.size()) { return false; } assert(i<=(int)data.size()); mxArray * mx_data = data[i]; assert(!mxIsSparse(mx_data)); assert(mxGetNumberOfDimensions(mx_data) == 2); //cout<=(int)names.size()) { return false; } assert(i<=(int)data.size()); mxArray * mx_data = data[i]; assert(!mxIsSparse(mx_data)); assert(mxGetNumberOfDimensions(mx_data) == 2); //cout< inline bool igl::MatlabWorkspace::find_index( const std::string & name, Eigen::PlainObjectBase& M) { if(!find(name,M)) { return false; } M.array() -= 1; return true; } //template //bool igl::MatlabWorkspace::save(const Data & M, const std::string & name) //{ // using namespace std; // // If I don't know the type then I can't save it // cerr<<"^MatlabWorkspace::save Error: Unknown data type. "<< // name<<" not saved."<