DMRITool  v0.1.1-139-g860d86b4
Diffusion MRI Tool
mexVNL.h
Go to the documentation of this file.
1 
18 #ifndef __mexVNL_h
19 #define __mexVNL_h
20 
21 #include <mex.h>
22 #include <vnl/vnl_matrix.h>
23 #include "mexutils.h"
24 
25 #include "utlCore.h"
26 
27 namespace utl
28 {
29 
30 template <class T>
31 inline void
32 GetVNLMatrixFromMXArray ( const mxArray* pr, vnl_matrix<T>* mat )
33 {
34  const mwSize* dims = mxGetDimensions(pr);
35  int row = static_cast<int>(dims[0]);
36  int column = static_cast<int>(dims[1]);
37 
38  double * data = mxGetPr(pr);
39  if (mat->rows()!=row || mat->columns()!=column )
40  mat->set_size(row, column);
41 
42  for ( int i = 0; i < row; i += 1 )
43  for ( int j = 0; j < column; j += 1 )
44  (*mat)[i][j] = data[j*row+i];
45 }
46 
47 template <class T>
48 inline void
49 GetMXArrayFromVNLMatrix ( const vnl_matrix<T>* mat, mxArray*& pr )
50 {
51  int row = mat->rows();
52  int column = mat->columns();
53  pr = CreateMatrix<double>(row, column);
54 
55  double * data = mxGetPr(pr);
56  for ( int i = 0; i < row; i += 1 )
57  for ( int j = 0; j < column; j += 1 )
58  data[j*row+i] = (*mat)[i][j];
59 }
60 
61 template <class T>
62 inline void
63 GetVNLVectorFromMXArray ( const mxArray* pr, vnl_vector<T>* mat )
64 {
65  const mwSize* dims = mxGetDimensions(pr);
66  int row = static_cast<int>(dims[0]);
67  int column = static_cast<int>(dims[1]);
68 
69  utlException(row>1 && column>1, "mxArray is not a vector");
70  int size = utl::max(row, column);
71  double * data = mxGetPr(pr);
72  if (mat->size()!=size )
73  mat->set_size(size);
74 
75  for ( int i = 0; i < size; i += 1 )
76  (*mat)[i] = data[i];
77 }
78 
79 template <class T>
80 inline void
81 GetMXArrayFromVNLVector ( const vnl_vector<T>* mat, mxArray*& pr )
82 {
83  int row = mat->size();
84  pr = CreateMatrix<double>(row, 1);
85 
86  double * data = mxGetPr(pr);
87  for ( int i = 0; i < row; i += 1 )
88  data[i] = (*mat)[i];
89 }
90 
91 template <class T>
92 inline void
93 SaveVNLMatrixToMatFile ( const vnl_matrix<T>* mat, const std::string fileName, const std::string varibleName )
94 {
95  SaveMatrixToMatFile<vnl_matrix<T> >(*mat, mat->rows(), mat->columns(), fileName, varibleName);
96 }
97 
98 template <class T>
99 inline void
100 ReadMatFileToVNLMatrix ( const std::string fileName, const std::string varibleName, vnl_matrix<T>* mat )
101 {
102  MATFile *pmat;
103  pmat = matOpen(fileName.c_str(), "r");
104  utlGlobalException(!pmat, "Error creating file " << fileName);
105  int status;
106 
107  mxArray *pr= matGetVariable(pmat, varibleName.c_str());
108  utlGlobalException(!pr, "Out of memory. Unable to create mxArray.");
109  const mwSize* dims = mxGetDimensions(pr);
110  int row = static_cast<int>(dims[0]);
111  int column = static_cast<int>(dims[1]);
112 
113  if (mat->rows()!=row || mat->columns()!=column )
114  mat->set_size(row, column);
115 
116  double * data = (double*)mxGetData(pr);
117  for ( int i = 0; i < row; i += 1 )
118  for ( int j = 0; j < column; j += 1 )
119  (*mat)(i,j) = data[j*row+i];
120 }
121 
122 }
123 
124 #endif
125 
void ReadMatFileToVNLMatrix(const std::string fileName, const std::string varibleName, vnl_matrix< T > *mat)
Definition: mexVNL.h:100
#define utlException(cond, expout)
Definition: utlCoreMacro.h:548
void GetVNLMatrixFromMXArray(const mxArray *pr, vnl_matrix< T > *mat)
Definition: mexVNL.h:32
mxArray * CreateMatrix< double >(int m, int n)
Create a m x n double matrix.
Definition: mexutils.h:139
int mwSize
Definition: utils.h:38
const T & max(const T &a, const T &b)
Return the maximum between a and b.
Definition: utlCore.h:263
void GetMXArrayFromVNLVector(const vnl_vector< T > *mat, mxArray *&pr)
Definition: mexVNL.h:81
#define utlGlobalException(cond, expout)
Definition: utlCoreMacro.h:372
Definition: utl.h:90
Contains miscellaneous functions for mex files. utl functions for mex code. Some codes are from spams...
void GetVNLVectorFromMXArray(const mxArray *pr, vnl_vector< T > *mat)
Definition: mexVNL.h:63
void GetMXArrayFromVNLMatrix(const vnl_matrix< T > *mat, mxArray *&pr)
Definition: mexVNL.h:49
void SaveVNLMatrixToMatFile(const vnl_matrix< T > *mat, const std::string fileName, const std::string varibleName)
Definition: mexVNL.h:93