DMRITool  v0.1.1-139-g860d86b4
Diffusion MRI Tool
utlMEX.h
Go to the documentation of this file.
1 
18 #ifndef __utlMEX_h
19 #define __utlMEX_h
20 
21 #include "mexutils.h"
22 #include "mexSTD.h"
23 #include "mexVNL.h"
24 #include "mexITK.h"
25 
26 #include "utlNDArray.h"
27 
28 namespace utl
29 {
30 
31 template <class T>
32 inline void
33 GetUtlMatrixFromMXArray ( const mxArray* pr, NDArray<T,2>* mat )
34 {
35  const mwSize* dims = mxGetDimensions(pr);
36  int row = static_cast<int>(dims[0]);
37  int column = static_cast<int>(dims[1]);
38 
39  double * data = mxGetPr(pr);
40  if (mat->Rows()!=row || mat->Columns()!=column )
41  mat->ReSize(row, column);
42 
43  for ( int i = 0; i < row; i += 1 )
44  for ( int j = 0; j < column; j += 1 )
45  (*mat)(i,j) = data[j*row+i];
46 }
47 
48 template <class T>
49 inline void
50 GetMXArrayFromUtlMatrix ( const NDArray<T,2>* mat, mxArray*& pr )
51 {
52  int row = mat->Rows();
53  int column = mat->Columns();
54  pr = CreateMatrix<double>(row, column);
55 
56  double * data = mxGetPr(pr);
57  for ( int i = 0; i < row; i += 1 )
58  for ( int j = 0; j < column; j += 1 )
59  data[j*row+i] = (*mat)(i,j);
60 }
61 
62 template <class T>
63 inline void
64 GetUtlVectorFromMXArray ( const mxArray* pr, NDArray<T,1>* mat )
65 {
66  const mwSize* dims = mxGetDimensions(pr);
67  int row = static_cast<int>(dims[0]);
68  int column = static_cast<int>(dims[1]);
69 
70  utlException(row>1 && column>1, "mxArray is not a vector");
71  int size = utl::max(row, column);
72  double * data = mxGetPr(pr);
73  if (mat->Size()!=size )
74  mat->ReSize(size);
75 
76  for ( int i = 0; i < size; i += 1 )
77  (*mat)[i] = data[i];
78 }
79 
80 template <class T>
81 inline void
82 GetMXArrayFromUtlVector ( const NDArray<T,1>* mat, mxArray*& pr )
83 {
84  int row = mat->Size();
85  pr = CreateMatrix<double>(row, 1);
86 
87  double * data = mxGetPr(pr);
88  for ( int i = 0; i < row; i += 1 )
89  data[i] = (*mat)[i];
90 }
91 
92 
93 }
94 
95 
96 #endif
NDArray<T,1> is a vector class which uses blas mkl.
Definition: utlVector.h:36
SizeType Columns() const
Definition: utlMatrix.h:137
#define utlException(cond, expout)
Definition: utlCoreMacro.h:548
SizeType Rows() const
Definition: utlMatrix.h:136
NDArray<T,2> is a row-major matrix.
Definition: utlMatrix.h:37
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 GetMXArrayFromUtlMatrix(const NDArray< T, 2 > *mat, mxArray *&pr)
Definition: utlMEX.h:50
void GetMXArrayFromUtlVector(const NDArray< T, 1 > *mat, mxArray *&pr)
Definition: utlMEX.h:82
Definition: utl.h:90
Contains miscellaneous functions for mex files. utl functions for mex code. Some codes are from spams...
SizeType Size() const
Definition: utlNDArray.h:321
void GetUtlMatrixFromMXArray(const mxArray *pr, NDArray< T, 2 > *mat)
Definition: utlMEX.h:33
bool ReSize(const SizeType rows, const SizeType cols)
Definition: utlMatrix.h:140
bool ReSize(const SizeType size)
Definition: utlVector.h:167
void GetUtlVectorFromMXArray(const mxArray *pr, NDArray< T, 1 > *mat)
Definition: utlMEX.h:64