Prusa Slicer 2.6.0
Loading...
Searching...
No Matches
igl::matlab::MatlabWorkspace Class Reference

#include <src/libigl/igl/matlab/MatlabWorkspace.h>

+ Collaboration diagram for igl::matlab::MatlabWorkspace:

Public Member Functions

 MatlabWorkspace ()
 
 ~MatlabWorkspace ()
 
void clear ()
 
bool write (const std::string &path) const
 
bool read (const std::string &path)
 
template<typename DerivedM >
MatlabWorkspacesave (const Eigen::PlainObjectBase< DerivedM > &M, const std::string &name)
 
template<typename MT >
MatlabWorkspacesave (const Eigen::SparseMatrix< MT > &M, const std::string &name)
 
template<typename ScalarM >
MatlabWorkspacesave (const std::vector< std::vector< ScalarM > > &vM, const std::string &name)
 
template<typename ScalarV >
MatlabWorkspacesave (const std::vector< ScalarV > &vV, const std::string &name)
 
template<typename Q >
MatlabWorkspacesave (const Eigen::Quaternion< Q > &q, const std::string &name)
 
MatlabWorkspacesave (const double d, const std::string &name)
 
template<typename DerivedM >
MatlabWorkspacesave_index (const Eigen::DenseBase< DerivedM > &M, const std::string &name)
 
template<typename ScalarM >
MatlabWorkspacesave_index (const std::vector< std::vector< ScalarM > > &vM, const std::string &name)
 
template<typename ScalarV >
MatlabWorkspacesave_index (const std::vector< ScalarV > &vV, const std::string &name)
 
template<typename DerivedM >
bool find (const std::string &name, Eigen::PlainObjectBase< DerivedM > &M)
 
template<typename MT >
bool find (const std::string &name, Eigen::SparseMatrix< MT > &M)
 
bool find (const std::string &name, double &d)
 
bool find (const std::string &name, int &v)
 
template<typename DerivedM >
bool find_index (const std::string &name, Eigen::PlainObjectBase< DerivedM > &M)
 
template<typename DerivedM >
igl::matlab::MatlabWorkspacesave (const Eigen::PlainObjectBase< DerivedM > &M, const std::string &name)
 
template<typename MT >
igl::matlab::MatlabWorkspacesave (const Eigen::SparseMatrix< MT > &M, const std::string &name)
 
template<typename ScalarM >
igl::matlab::MatlabWorkspacesave (const std::vector< std::vector< ScalarM > > &vM, const std::string &name)
 
template<typename ScalarV >
igl::matlab::MatlabWorkspacesave (const std::vector< ScalarV > &vV, const std::string &name)
 
template<typename Q >
igl::matlab::MatlabWorkspacesave (const Eigen::Quaternion< Q > &q, const std::string &name)
 
template<typename DerivedM >
igl::matlab::MatlabWorkspacesave_index (const Eigen::DenseBase< DerivedM > &M, const std::string &name)
 
template<typename ScalarM >
igl::matlab::MatlabWorkspacesave_index (const std::vector< std::vector< ScalarM > > &vM, const std::string &name)
 
template<typename ScalarV >
igl::matlab::MatlabWorkspacesave_index (const std::vector< ScalarV > &vV, const std::string &name)
 

Private Attributes

std::vector< std::string > names
 
std::vector< mxArray * > data
 

Detailed Description

Constructor & Destructor Documentation

◆ MatlabWorkspace()

igl::matlab::MatlabWorkspace::MatlabWorkspace ( )
inline
171 :
172 names(),
173 data()
174{
175}
std::vector< std::string > names
Definition MatlabWorkspace.h:42
std::vector< mxArray * > data
Definition MatlabWorkspace.h:44

◆ ~MatlabWorkspace()

igl::matlab::MatlabWorkspace::~MatlabWorkspace ( )
inline
178{
179 // clean up data
180 clear();
181}
void clear()
Definition MatlabWorkspace.h:183

Member Function Documentation

◆ clear()

void igl::matlab::MatlabWorkspace::clear ( )
inline
184{
185 for_each(data.begin(),data.end(),&mxDestroyArray);
186 data.clear();
187 names.clear();
188}
void for_each(const Eigen::SparseMatrix< AType > &A, const Func &func)
Definition for_each.h:31

References igl::for_each().

+ Here is the call graph for this function:

◆ find() [1/4]

bool igl::matlab::MatlabWorkspace::find ( const std::string &  name,
double &  d 
)
inline
534{
535 using namespace std;
536 const int i = std::find(names.begin(), names.end(), name)-names.begin();
537 if(i>=(int)names.size())
538 {
539 return false;
540 }
541 assert(i<=(int)data.size());
542 mxArray * mx_data = data[i];
543 assert(!mxIsSparse(mx_data));
544 assert(mxGetNumberOfDimensions(mx_data) == 2);
545 //cout<<name<<": "<<mxGetM(mx_data)<<" "<<mxGetN(mx_data)<<endl;
546 assert(mxGetNumberOfElements(mx_data) == 1);
547 copy(
548 mxGetPr(mx_data),
549 mxGetPr(mx_data)+mxGetNumberOfElements(mx_data),
550 &d);
551 return true;
552}
STL namespace.

◆ find() [2/4]

template<typename DerivedM >
bool igl::matlab::MatlabWorkspace::find ( const std::string &  name,
Eigen::PlainObjectBase< DerivedM > &  M 
)
inline
423{
424 using namespace std;
425 const int i = std::find(names.begin(), names.end(), name)-names.begin();
426 if(i>=(int)names.size())
427 {
428 return false;
429 }
430 assert(i<=(int)data.size());
431 mxArray * mx_data = data[i];
432 assert(!mxIsSparse(mx_data));
433 assert(mxGetNumberOfDimensions(mx_data) == 2);
434 //cout<<name<<": "<<mxGetM(mx_data)<<" "<<mxGetN(mx_data)<<endl;
435 const int m = mxGetM(mx_data);
436 const int n = mxGetN(mx_data);
437 // Handle vectors: in the sense that anything found becomes a column vector,
438 // whether it was column vector, row vector or matrix
439 if(DerivedM::IsVectorAtCompileTime)
440 {
441 assert(m==1 || n==1 || (m==0 && n==0));
442 M.resize(m*n,1);
443 }else
444 {
445 M.resize(m,n);
446 }
447 assert(mxGetNumberOfElements(mx_data) == M.size());
448 // Use Eigen's map and cast to copy
450 (mxGetPr(mx_data),M.rows(),M.cols()).cast<typename DerivedM::Scalar>();
451 return true;
452}
A matrix or vector expression mapping an existing array of data.
Definition Map.h:96
EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE Index cols() const
Definition PlainObjectBase.h:153
EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE void resize(Index rows, Index cols)
Definition PlainObjectBase.h:279
EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE Index rows() const
Definition PlainObjectBase.h:151

References Eigen::PlainObjectBase< Derived >::cols(), Eigen::PlainObjectBase< Derived >::resize(), and Eigen::PlainObjectBase< Derived >::rows().

+ Here is the call graph for this function:

◆ find() [3/4]

template<typename MT >
bool igl::matlab::MatlabWorkspace::find ( const std::string &  name,
Eigen::SparseMatrix< MT > &  M 
)
inline
458{
459 using namespace std;
460 using namespace Eigen;
461 const int i = std::find(names.begin(), names.end(), name)-names.begin();
462 if(i>=(int)names.size())
463 {
464 return false;
465 }
466 assert(i<=(int)data.size());
467 mxArray * mx_data = data[i];
468 // Handle boring case where matrix is actually an empty dense matrix
469 if(mxGetNumberOfElements(mx_data) == 0)
470 {
471 M.resize(0,0);
472 return true;
473 }
474 assert(mxIsSparse(mx_data));
475 assert(mxGetNumberOfDimensions(mx_data) == 2);
476 //cout<<name<<": "<<mxGetM(mx_data)<<" "<<mxGetN(mx_data)<<endl;
477 const int m = mxGetM(mx_data);
478 const int n = mxGetN(mx_data);
479 // TODO: It should be possible to directly load the data into the sparse
480 // matrix without going through the triplets
481 // Copy data immediately
482 double * pr = mxGetPr(mx_data);
483 mwIndex * ir = mxGetIr(mx_data);
484 mwIndex * jc = mxGetJc(mx_data);
485 vector<Triplet<MT> > MIJV;
486 const int nnz = mxGetNzmax(mx_data);
487 MIJV.reserve(nnz);
488 // Iterate over outside
489 int k = 0;
490 for(int j=0; j<n;j++)
491 {
492 // Iterate over inside
493 while(k<(int)jc[j+1])
494 {
495 //cout<<ir[k]<<" "<<j<<" "<<pr[k]<<endl;
496 assert((int)ir[k]<m);
497 assert((int)j<n);
498 MIJV.push_back(Triplet<MT >(ir[k],j,pr[k]));
499 k++;
500 }
501 }
502 M.resize(m,n);
503 M.setFromTriplets(MIJV.begin(),MIJV.end());
504
505 return true;
506}
void setFromTriplets(const InputIterators &begin, const InputIterators &end)
Definition SparseMatrix.h:993
void resize(Index rows, Index cols)
Definition SparseMatrix.h:621
A small structure to hold a non zero as a triplet (i,j,value).
Definition SparseUtil.h:155
Definition LDLT.h:16

References Eigen::SparseMatrix< _Scalar, _Options, _StorageIndex >::resize(), and Eigen::SparseMatrix< _Scalar, _Options, _StorageIndex >::setFromTriplets().

+ Here is the call graph for this function:

◆ find() [4/4]

bool igl::matlab::MatlabWorkspace::find ( const std::string &  name,
int &  v 
)
inline
511{
512 using namespace std;
513 const int i = std::find(names.begin(), names.end(), name)-names.begin();
514 if(i>=(int)names.size())
515 {
516 return false;
517 }
518 assert(i<=(int)data.size());
519 mxArray * mx_data = data[i];
520 assert(!mxIsSparse(mx_data));
521 assert(mxGetNumberOfDimensions(mx_data) == 2);
522 //cout<<name<<": "<<mxGetM(mx_data)<<" "<<mxGetN(mx_data)<<endl;
523 assert(mxGetNumberOfElements(mx_data) == 1);
524 copy(
525 mxGetPr(mx_data),
526 mxGetPr(mx_data)+mxGetNumberOfElements(mx_data),
527 &v);
528 return true;
529}

◆ find_index()

template<typename DerivedM >
bool igl::matlab::MatlabWorkspace::find_index ( const std::string &  name,
Eigen::PlainObjectBase< DerivedM > &  M 
)
inline
558{
559 if(!find(name,M))
560 {
561 return false;
562 }
563 M.array() -= 1;
564 return true;
565}
bool find(const std::string &name, Eigen::PlainObjectBase< DerivedM > &M)
Definition MatlabWorkspace.h:420

References igl::find().

+ Here is the call graph for this function:

◆ read()

bool igl::matlab::MatlabWorkspace::read ( const std::string &  path)
inline
221{
222 using namespace std;
223
224 MATFile * mat_file;
225
226 mat_file = matOpen(path.c_str(), "r");
227 if (mat_file == NULL)
228 {
229 cerr<<"Error: failed to open "<<path<<endl;
230 return false;
231 }
232
233 int ndir;
234 const char ** dir = (const char **)matGetDir(mat_file, &ndir);
235 if (dir == NULL) {
236 cerr<<"Error reading directory of file "<< path<<endl;
237 return false;
238 }
239 mxFree(dir);
240
241 // Must close and reopen
242 if(matClose(mat_file) != 0)
243 {
244 cerr<<"Error: failed to close file "<<path<<endl;
245 return false;
246 }
247 mat_file = matOpen(path.c_str(), "r");
248 if (mat_file == NULL)
249 {
250 cerr<<"Error: failed to open "<<path<<endl;
251 return false;
252 }
253
254
255 /* Read in each array. */
256 for (int i=0; i<ndir; i++)
257 {
258 const char * name;
259 mxArray * mx_data = matGetNextVariable(mat_file, &name);
260 if (mx_data == NULL)
261 {
262 cerr<<"Error: matGetNextVariable failed in "<<path<<endl;
263 return false;
264 }
265 const int dims = mxGetNumberOfDimensions(mx_data);
266 assert(dims == 2);
267 if(dims != 2)
268 {
269 fprintf(stderr,"Variable '%s' has %d ≠ 2 dimensions. Skipping\n",
270 name,dims);
271 mxDestroyArray(mx_data);
272 continue;
273 }
274 // don't destroy
275 names.push_back(name);
276 data.push_back(mx_data);
277 }
278
279 if(matClose(mat_file) != 0)
280 {
281 cerr<<"Error: failed to close file "<<path<<endl;
282 return false;
283 }
284
285 return true;
286}

◆ save() [1/11]

igl::matlab::MatlabWorkspace & igl::matlab::MatlabWorkspace::save ( const double  d,
const std::string &  name 
)
inline
382{
383 Eigen::VectorXd v(1);
384 v(0) = d;
385 return save(v,name);
386}
MatlabWorkspace & save(const Eigen::PlainObjectBase< DerivedM > &M, const std::string &name)

References save().

+ Here is the call graph for this function:

◆ save() [2/11]

template<typename DerivedM >
MatlabWorkspace & igl::matlab::MatlabWorkspace::save ( const Eigen::PlainObjectBase< DerivedM > &  M,
const std::string &  name 
)
inline

◆ save() [3/11]

template<typename DerivedM >
igl::matlab::MatlabWorkspace & igl::matlab::MatlabWorkspace::save ( const Eigen::PlainObjectBase< DerivedM > &  M,
const std::string &  name 
)
inline
293{
294 using namespace std;
295 const int m = M.rows();
296 const int n = M.cols();
297 mxArray * mx_data = mxCreateDoubleMatrix(m,n,mxREAL);
298 data.push_back(mx_data);
299 names.push_back(name);
300 // Copy data immediately
301 // Use Eigen's map and cast to copy
303 map(mxGetPr(mx_data),m,n);
304 map = M.template cast<double>();
305 return *this;
306}

References Eigen::PlainObjectBase< Derived >::cols(), and Eigen::PlainObjectBase< Derived >::rows().

+ Here is the call graph for this function:

◆ save() [4/11]

template<typename Q >
MatlabWorkspace & igl::matlab::MatlabWorkspace::save ( const Eigen::Quaternion< Q > &  q,
const std::string &  name 
)
inline

◆ save() [5/11]

template<typename Q >
igl::matlab::MatlabWorkspace & igl::matlab::MatlabWorkspace::save ( const Eigen::Quaternion< Q > &  q,
const std::string &  name 
)
inline
370{
372 qm(0,0) = q.w();
373 qm(0,1) = q.x();
374 qm(0,2) = q.y();
375 qm(0,3) = q.z();
376 return save(qm,name);
377}
The matrix class, also used for vectors and row-vectors.
Definition Matrix.h:180
EIGEN_DEVICE_FUNC CoeffReturnType w() const
Definition Quaternion.h:72
EIGEN_DEVICE_FUNC CoeffReturnType y() const
Definition Quaternion.h:68
EIGEN_DEVICE_FUNC CoeffReturnType x() const
Definition Quaternion.h:66
EIGEN_DEVICE_FUNC CoeffReturnType z() const
Definition Quaternion.h:70

References save(), Eigen::QuaternionBase< Derived >::w(), Eigen::QuaternionBase< Derived >::x(), Eigen::QuaternionBase< Derived >::y(), and Eigen::QuaternionBase< Derived >::z().

+ Here is the call graph for this function:

◆ save() [6/11]

template<typename MT >
MatlabWorkspace & igl::matlab::MatlabWorkspace::save ( const Eigen::SparseMatrix< MT > &  M,
const std::string &  name 
)
inline

◆ save() [7/11]

template<typename MT >
igl::matlab::MatlabWorkspace & igl::matlab::MatlabWorkspace::save ( const Eigen::SparseMatrix< MT > &  M,
const std::string &  name 
)
inline
313{
314 using namespace std;
315 const int m = M.rows();
316 const int n = M.cols();
317 // THIS WILL NOT WORK FOR ROW-MAJOR
318 assert(n==M.outerSize());
319 const int nzmax = M.nonZeros();
320 mxArray * mx_data = mxCreateSparse(m, n, nzmax, mxREAL);
321 data.push_back(mx_data);
322 names.push_back(name);
323 // Copy data immediately
324 double * pr = mxGetPr(mx_data);
325 mwIndex * ir = mxGetIr(mx_data);
326 mwIndex * jc = mxGetJc(mx_data);
327
328 // Iterate over outside
329 int k = 0;
330 for(int j=0; j<M.outerSize();j++)
331 {
332 jc[j] = k;
333 // Iterate over inside
334 for(typename Eigen::SparseMatrix<MT>::InnerIterator it (M,j); it; ++it)
335 {
336 pr[k] = it.value();
337 ir[k] = it.row();
338 k++;
339 }
340 }
341 jc[M.outerSize()] = k;
342
343 return *this;
344}
Definition SparseCompressedBase.h:137
Index nonZeros() const
Definition SparseCompressedBase.h:56
Index outerSize() const
Definition SparseMatrix.h:143
Index rows() const
Definition SparseMatrix.h:136
Index cols() const
Definition SparseMatrix.h:138

References Eigen::SparseMatrix< _Scalar, _Options, _StorageIndex >::cols(), Eigen::SparseMatrix< _Scalar, _Options, _StorageIndex >::nonZeros(), Eigen::SparseMatrix< _Scalar, _Options, _StorageIndex >::outerSize(), and Eigen::SparseMatrix< _Scalar, _Options, _StorageIndex >::rows().

+ Here is the call graph for this function:

◆ save() [8/11]

template<typename ScalarV >
MatlabWorkspace & igl::matlab::MatlabWorkspace::save ( const std::vector< ScalarV > &  vV,
const std::string &  name 
)
inline

◆ save() [9/11]

template<typename ScalarV >
igl::matlab::MatlabWorkspace & igl::matlab::MatlabWorkspace::save ( const std::vector< ScalarV > &  vV,
const std::string &  name 
)
inline
360{
361 Eigen::MatrixXd V;
362 list_to_matrix(vV,V);
363 return this->save(V,name);
364}
IGL_INLINE bool list_to_matrix(const std::vector< std::vector< T > > &V, Eigen::PlainObjectBase< Derived > &M)
Definition list_to_matrix.cpp:19

References igl::list_to_matrix(), and save().

+ Here is the call graph for this function:

◆ save() [10/11]

template<typename ScalarM >
MatlabWorkspace & igl::matlab::MatlabWorkspace::save ( const std::vector< std::vector< ScalarM > > &  vM,
const std::string &  name 
)
inline

◆ save() [11/11]

template<typename ScalarM >
igl::matlab::MatlabWorkspace & igl::matlab::MatlabWorkspace::save ( const std::vector< std::vector< ScalarM > > &  vM,
const std::string &  name 
)
inline
350{
351 Eigen::MatrixXd M;
352 list_to_matrix(vM,M);
353 return this->save(M,name);
354}

References igl::list_to_matrix(), and save().

+ Here is the call graph for this function:

◆ save_index() [1/6]

template<typename DerivedM >
MatlabWorkspace & igl::matlab::MatlabWorkspace::save_index ( const Eigen::DenseBase< DerivedM > &  M,
const std::string &  name 
)
inline

◆ save_index() [2/6]

template<typename DerivedM >
igl::matlab::MatlabWorkspace & igl::matlab::MatlabWorkspace::save_index ( const Eigen::DenseBase< DerivedM > &  M,
const std::string &  name 
)
inline
393{
394 DerivedM Mp1 = M;
395 Mp1.array() += 1;
396 return this->save(Mp1,name);
397}

References save().

+ Here is the call graph for this function:

◆ save_index() [3/6]

template<typename ScalarV >
MatlabWorkspace & igl::matlab::MatlabWorkspace::save_index ( const std::vector< ScalarV > &  vV,
const std::string &  name 
)
inline

◆ save_index() [4/6]

template<typename ScalarV >
igl::matlab::MatlabWorkspace & igl::matlab::MatlabWorkspace::save_index ( const std::vector< ScalarV > &  vV,
const std::string &  name 
)
inline
413{
414 Eigen::MatrixXd V;
415 list_to_matrix(vV,V);
416 return this->save_index(V,name);
417}
MatlabWorkspace & save_index(const Eigen::DenseBase< DerivedM > &M, const std::string &name)

References igl::list_to_matrix().

+ Here is the call graph for this function:

◆ save_index() [5/6]

template<typename ScalarM >
MatlabWorkspace & igl::matlab::MatlabWorkspace::save_index ( const std::vector< std::vector< ScalarM > > &  vM,
const std::string &  name 
)
inline

◆ save_index() [6/6]

template<typename ScalarM >
igl::matlab::MatlabWorkspace & igl::matlab::MatlabWorkspace::save_index ( const std::vector< std::vector< ScalarM > > &  vM,
const std::string &  name 
)
inline
403{
404 Eigen::MatrixXd M;
405 list_to_matrix(vM,M);
406 return this->save_index(M,name);
407}

References igl::list_to_matrix().

+ Here is the call graph for this function:

◆ write()

bool igl::matlab::MatlabWorkspace::write ( const std::string &  path) const
inline
191{
192 using namespace std;
193 MATFile * mat_file = matOpen(path.c_str(), "w");
194 if(mat_file == NULL)
195 {
196 fprintf(stderr,"Error opening file %s\n",path.c_str());
197 return false;
198 }
199 assert(names.size() == data.size());
200 // loop over names and data
201 for(int i = 0;i < (int)names.size(); i++)
202 {
203 // Put variable as LOCAL variable
204 int status = matPutVariable(mat_file,names[i].c_str(), data[i]);
205 if(status != 0)
206 {
207 cerr<<"^MatlabWorkspace::save Error: matPutVariable ("<<names[i]<<
208 ") failed"<<endl;
209 return false;
210 }
211 }
212 if(matClose(mat_file) != 0)
213 {
214 fprintf(stderr,"Error closing file %s\n",path.c_str());
215 return false;
216 }
217 return true;
218}

Member Data Documentation

◆ data

std::vector<mxArray*> igl::matlab::MatlabWorkspace::data
private

◆ names

std::vector<std::string> igl::matlab::MatlabWorkspace::names
private

The documentation for this class was generated from the following file: