123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579 |
- #ifndef IGL_MATLAB_MATLAB_WORKSPACE_H
- #define IGL_MATLAB_MATLAB_WORKSPACE_H
- #include <Eigen/Dense>
- #include <Eigen/Sparse>
- #include <mat.h>
- #include <string>
- #include <vector>
- namespace igl
- {
- namespace matlab
- {
-
-
-
-
-
-
-
-
-
-
-
-
-
- class MatlabWorkspace
- {
- private:
-
-
-
- std::vector<std::string> names;
-
- std::vector<mxArray*> data;
- public:
- MatlabWorkspace();
- ~MatlabWorkspace();
-
- inline void clear();
-
-
-
-
-
- inline bool write(const std::string & path) const;
-
-
-
-
-
- inline bool read(const std::string & path);
-
-
-
-
-
-
-
-
-
-
- template <typename DerivedM>
- inline MatlabWorkspace& save(
- const Eigen::PlainObjectBase<DerivedM>& M,
- const std::string & name);
-
-
- template <typename MT>
- inline MatlabWorkspace& save(
- const Eigen::SparseMatrix<MT>& M,
- const std::string & name);
-
-
- template <typename ScalarM>
- inline MatlabWorkspace& save(
- const std::vector<std::vector<ScalarM> > & vM,
- const std::string & name);
-
-
- template <typename ScalarV>
- inline MatlabWorkspace& save(
- const std::vector<ScalarV> & vV,
- const std::string & name);
-
-
-
-
-
-
- template <typename Q>
- inline MatlabWorkspace& save(
- const Eigen::Quaternion<Q> & q,
- const std::string & name);
- inline MatlabWorkspace& save(
- const double d,
- const std::string & name);
-
-
- template <typename DerivedM>
- inline MatlabWorkspace& save_index(
- const Eigen::DenseBase<DerivedM>& M,
- const std::string & name);
- template <typename ScalarM>
- inline MatlabWorkspace& save_index(
- const std::vector<std::vector<ScalarM> > & vM,
- const std::string & name);
- template <typename ScalarV>
- inline MatlabWorkspace& save_index(
- const std::vector<ScalarV> & vV,
- const std::string & name);
-
-
-
-
-
-
-
-
-
-
-
- template <typename DerivedM>
- inline bool find(
- const std::string & name,
- Eigen::PlainObjectBase<DerivedM>& M);
- template <typename MT>
- inline bool find(
- const std::string & name,
- Eigen::SparseMatrix<MT>& M);
- inline bool find(
- const std::string & name,
- double & d);
- inline bool find(
- const std::string & name,
- int & v);
-
- template <typename DerivedM>
- inline bool find_index(
- const std::string & name,
- Eigen::PlainObjectBase<DerivedM>& M);
- };
- }
- }
- #include "igl/list_to_matrix.h"
- #include "mat.h"
- #include <iostream>
- #include <algorithm>
- #include <vector>
- inline igl::matlab::MatlabWorkspace::MatlabWorkspace():
- names(),
- data()
- {
- }
- inline igl::matlab::MatlabWorkspace::~MatlabWorkspace()
- {
-
- clear();
- }
- inline void igl::matlab::MatlabWorkspace::clear()
- {
- for_each(data.begin(),data.end(),&mxDestroyArray);
- data.clear();
- names.clear();
- }
- inline bool igl::matlab::MatlabWorkspace::write(const std::string & path) const
- {
- using namespace std;
- MATFile * mat_file = matOpen(path.c_str(), "w");
- if(mat_file == NULL)
- {
- fprintf(stderr,"Error opening file %s\n",path.c_str());
- return false;
- }
- assert(names.size() == data.size());
-
- for(int i = 0;i < (int)names.size(); i++)
- {
-
- int status = matPutVariable(mat_file,names[i].c_str(), data[i]);
- if(status != 0)
- {
- cerr<<"^MatlabWorkspace::save Error: matPutVariable ("<<names[i]<<
- ") failed"<<endl;
- return false;
- }
- }
- if(matClose(mat_file) != 0)
- {
- fprintf(stderr,"Error closing file %s\n",path.c_str());
- return false;
- }
- return true;
- }
- inline bool igl::matlab::MatlabWorkspace::read(const std::string & path)
- {
- using namespace std;
- MATFile * mat_file;
- mat_file = matOpen(path.c_str(), "r");
- if (mat_file == NULL)
- {
- cerr<<"Error: failed to open "<<path<<endl;
- return false;
- }
- int ndir;
- const char ** dir = (const char **)matGetDir(mat_file, &ndir);
- if (dir == NULL) {
- cerr<<"Error reading directory of file "<< path<<endl;
- return false;
- }
- mxFree(dir);
-
- if(matClose(mat_file) != 0)
- {
- cerr<<"Error: failed to close file "<<path<<endl;
- return false;
- }
- mat_file = matOpen(path.c_str(), "r");
- if (mat_file == NULL)
- {
- cerr<<"Error: failed to open "<<path<<endl;
- return false;
- }
-
-
- for (int i=0; i<ndir; i++)
- {
- const char * name;
- mxArray * mx_data = matGetNextVariable(mat_file, &name);
- if (mx_data == NULL)
- {
- cerr<<"Error: matGetNextVariable failed in "<<path<<endl;
- return false;
- }
- const int dims = mxGetNumberOfDimensions(mx_data);
- assert(dims == 2);
- if(dims != 2)
- {
- fprintf(stderr,"Variable '%s' has %d ≠ 2 dimensions. Skipping\n",
- name,dims);
- mxDestroyArray(mx_data);
- continue;
- }
-
- names.push_back(name);
- data.push_back(mx_data);
- }
- if(matClose(mat_file) != 0)
- {
- cerr<<"Error: failed to close file "<<path<<endl;
- return false;
- }
- return true;
- }
- template <typename DerivedM>
- inline igl::matlab::MatlabWorkspace& igl::matlab::MatlabWorkspace::save(
- const Eigen::PlainObjectBase<DerivedM>& 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);
-
-
- Eigen::Map< Eigen::Matrix<double,Eigen::Dynamic,Eigen::Dynamic> >
- map(mxGetPr(mx_data),m,n);
- map = M.template cast<double>();
- return *this;
- }
- template <typename MT>
- inline igl::matlab::MatlabWorkspace& igl::matlab::MatlabWorkspace::save(
- const Eigen::SparseMatrix<MT>& M,
- const std::string & name)
- {
- using namespace std;
- const int m = M.rows();
- const int n = M.cols();
-
- 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);
-
- double * pr = mxGetPr(mx_data);
- mwIndex * ir = mxGetIr(mx_data);
- mwIndex * jc = mxGetJc(mx_data);
-
- int k = 0;
- for(int j=0; j<M.outerSize();j++)
- {
- jc[j] = k;
-
- for(typename Eigen::SparseMatrix<MT>::InnerIterator it (M,j); it; ++it)
- {
- pr[k] = it.value();
- ir[k] = it.row();
- k++;
- }
- }
- jc[M.outerSize()] = k;
- return *this;
- }
- template <typename ScalarM>
- inline igl::matlab::MatlabWorkspace& igl::matlab::MatlabWorkspace::save(
- const std::vector<std::vector<ScalarM> > & vM,
- const std::string & name)
- {
- Eigen::MatrixXd M;
- list_to_matrix(vM,M);
- return this->save(M,name);
- }
- template <typename ScalarV>
- inline igl::matlab::MatlabWorkspace& igl::matlab::MatlabWorkspace::save(
- const std::vector<ScalarV> & vV,
- const std::string & name)
- {
- Eigen::MatrixXd V;
- list_to_matrix(vV,V);
- return this->save(V,name);
- }
- template <typename Q>
- inline igl::matlab::MatlabWorkspace& igl::matlab::MatlabWorkspace::save(
- const Eigen::Quaternion<Q> & q,
- const std::string & name)
- {
- Eigen::Matrix<Q,1,4> 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::matlab::MatlabWorkspace& igl::matlab::MatlabWorkspace::save(
- const double d,
- const std::string & name)
- {
- Eigen::VectorXd v(1);
- v(0) = d;
- return save(v,name);
- }
- template <typename DerivedM>
- inline igl::matlab::MatlabWorkspace&
- igl::matlab::MatlabWorkspace::save_index(
- const Eigen::DenseBase<DerivedM>& M,
- const std::string & name)
- {
- DerivedM Mp1 = M;
- Mp1.array() += 1;
- return this->save(Mp1,name);
- }
- template <typename ScalarM>
- inline igl::matlab::MatlabWorkspace& igl::matlab::MatlabWorkspace::save_index(
- const std::vector<std::vector<ScalarM> > & vM,
- const std::string & name)
- {
- Eigen::MatrixXd M;
- list_to_matrix(vM,M);
- return this->save_index(M,name);
- }
- template <typename ScalarV>
- inline igl::matlab::MatlabWorkspace& igl::matlab::MatlabWorkspace::save_index(
- const std::vector<ScalarV> & vV,
- const std::string & name)
- {
- Eigen::MatrixXd V;
- list_to_matrix(vV,V);
- return this->save_index(V,name);
- }
- template <typename DerivedM>
- inline bool igl::matlab::MatlabWorkspace::find(
- const std::string & name,
- Eigen::PlainObjectBase<DerivedM>& 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);
-
- const int m = mxGetM(mx_data);
- const int n = mxGetN(mx_data);
-
-
- if(DerivedM::IsVectorAtCompileTime)
- {
- assert(m==1 || n==1 || (m==0 && n==0));
- M.resize(m*n,1);
- }else
- {
- M.resize(m,n);
- }
- assert(mxGetNumberOfElements(mx_data) == M.size());
-
- M = Eigen::Map< Eigen::Matrix<double,Eigen::Dynamic,Eigen::Dynamic> >
- (mxGetPr(mx_data),M.rows(),M.cols()).cast<typename DerivedM::Scalar>();
- return true;
- }
- template <typename MT>
- inline bool igl::matlab::MatlabWorkspace::find(
- const std::string & name,
- Eigen::SparseMatrix<MT>& 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];
-
- if(mxGetNumberOfElements(mx_data) == 0)
- {
- M.resize(0,0);
- return true;
- }
- assert(mxIsSparse(mx_data));
- assert(mxGetNumberOfDimensions(mx_data) == 2);
-
- const int m = mxGetM(mx_data);
- const int n = mxGetN(mx_data);
-
-
-
- double * pr = mxGetPr(mx_data);
- mwIndex * ir = mxGetIr(mx_data);
- mwIndex * jc = mxGetJc(mx_data);
- vector<Triplet<MT> > MIJV;
- const int nnz = mxGetNzmax(mx_data);
- MIJV.reserve(nnz);
-
- int k = 0;
- for(int j=0; j<n;j++)
- {
-
- while(k<(int)jc[j+1])
- {
-
- assert((int)ir[k]<m);
- assert((int)j<n);
- MIJV.push_back(Triplet<MT >(ir[k],j,pr[k]));
- k++;
- }
- }
- M.resize(m,n);
- M.setFromTriplets(MIJV.begin(),MIJV.end());
- return true;
- }
- inline bool igl::matlab::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);
-
- assert(mxGetNumberOfElements(mx_data) == 1);
- copy(
- mxGetPr(mx_data),
- mxGetPr(mx_data)+mxGetNumberOfElements(mx_data),
- &v);
- return true;
- }
- inline bool igl::matlab::MatlabWorkspace::find(
- const std::string & name,
- double & d)
- {
- 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);
-
- assert(mxGetNumberOfElements(mx_data) == 1);
- copy(
- mxGetPr(mx_data),
- mxGetPr(mx_data)+mxGetNumberOfElements(mx_data),
- &d);
- return true;
- }
- template <typename DerivedM>
- inline bool igl::matlab::MatlabWorkspace::find_index(
- const std::string & name,
- Eigen::PlainObjectBase<DerivedM>& M)
- {
- if(!find(name,M))
- {
- return false;
- }
- M.array() -= 1;
- return true;
- }
- #endif
|