DMRITool  v0.1.1-139-g860d86b4
Diffusion MRI Tool
mexutils.h
Go to the documentation of this file.
1 
10 #ifndef MEXUTILS_H
11 #define MEXUTILS_H
12 
13 #include <mex.h>
14 #include <mat.h>
15 #include "utlCoreMacro.h"
16 
17 #include <typeinfo>
18 #include <stdlib.h>
19 #include <iostream>
20 #ifndef MATLAB_MEX_FILE
21 #define MATLAB_MEX_FILE
22 #endif
23 
24 #include <cstdio>
25 #include <cstdlib>
26 #include <iostream>
27 #include <vector>
28 #include <complex>
29 
30 // #include <utils.h>
31 // #include <misc.h>
32 
33 
34 //#ifndef EM64T
35 //#define mwSize int
36 //#endif
37 
38 namespace utl
39 {
40 
41 
43 template <typename T>
44 bool mexCheckType(const mxArray* array);
45 
47 template <> inline bool mexCheckType<double>(const mxArray* array) {
48  return mxGetClassID(array) == mxDOUBLE_CLASS && !mxIsComplex(array);
49 };
50 
52 template <> inline bool mexCheckType<float>(const mxArray* array) {
53  return mxGetClassID(array) == mxSINGLE_CLASS && !mxIsComplex(array);
54 };
55 
57 template <> inline bool mexCheckType<int>(const mxArray* array) {
58  return mxGetClassID(array) == mxINT32_CLASS && !mxIsComplex(array);
59 };
60 
62 template <> inline bool mexCheckType<bool>(const mxArray* array) {
63  return mxGetClassID(array) == mxLOGICAL_CLASS && !mxIsComplex(array);
64 };
65 
67 template <> inline bool mexCheckType<std::complex<double> >(const mxArray* array) {
68  return mxGetClassID(array) == mxDOUBLE_CLASS && mxIsComplex(array);
69 };
70 
72 template <> inline bool mexCheckType<std::complex<float> >(const mxArray* array) {
73  return mxGetClassID(array) == mxSINGLE_CLASS && mxIsComplex(array);
74 };
75 
76 
78 inline bool
79 CheckSize(const mxArray* array, const int m, const int n)
80 {
81  const mwSize* dims=mxGetDimensions(array);
82  int _m=static_cast<int>(dims[0]);
83  int _n=static_cast<int>(dims[1]);
84  return _n==n && _m==m;
85 };
86 
89 template <typename T>
90 void
91 CreateCopySparse(T*& alpha_v2, int*& alpha_r2, int*& alpha_pB2, int*& alpha_pE2,
92  double* alpha_v, mwSize* alpha_r, mwSize* alpha_pB, mwSize* alpha_pE, int M)
93 {
94  if (typeid(alpha_v) == typeid(alpha_v2)) {
95  alpha_v2=reinterpret_cast<T*>(alpha_v);
96  } else {
97  alpha_v2 = new T[alpha_pB[M]];
98  for (mwSize i = 0; i<alpha_pB[M]; ++i) alpha_v2[i] = static_cast<T>(alpha_v[i]);
99  }
100  if (typeid(alpha_r2) == typeid(alpha_r)) {
101  alpha_r2=reinterpret_cast<int*>(alpha_r);
102  alpha_pB2=reinterpret_cast<int*>(alpha_pB);
103  alpha_pE2=reinterpret_cast<int*>(alpha_pE);
104  } else {
105  alpha_r2= new int[alpha_pB[M]];
106  for (mwSize i = 0; i<alpha_pB[M]; ++i) alpha_r2[i]=static_cast<int>(alpha_r[i]);
107  alpha_pB2= new int[M+1];
108  for (int i = 0; i<=M; ++i) alpha_pB2[i]=static_cast<int>(alpha_pB[i]);
109  alpha_pE2=alpha_pB2+1;
110  }
111 };
112 
114 template <typename T>
115 inline void
116 DeleteCopySparse(T*& alpha_v2, int*& alpha_r2, int*& alpha_pB2, int*& alpha_pE2,
117  double* alpha_v, mwSize* alpha_r)
118 {
119  if (typeid(alpha_v) != typeid(alpha_v2)) {
120  delete[](alpha_v2);
121  }
122  if (typeid(alpha_r2) != typeid(alpha_r)) {
123  delete[](alpha_r2);
124  delete[](alpha_pB2);
125  }
126  alpha_v2=NULL;
127  alpha_r2=NULL;
128  alpha_pB2=NULL;
129  alpha_pE2=NULL;
130 };
131 
133 template <typename T>
134 inline mxArray* CreateMatrix(int m, int n);
135 
137 template <>
138 inline mxArray*
139 CreateMatrix<double>(int m, int n)
140 {
141  return mxCreateNumericMatrix(static_cast<mwSize>(m), static_cast<mwSize>(n),mxDOUBLE_CLASS,mxREAL);
142 }
143 
145 template <>
146 inline mxArray*
147 CreateMatrix<float>(int m, int n)
148 {
149  return mxCreateNumericMatrix(static_cast<mwSize>(m),static_cast<mwSize>(n),mxSINGLE_CLASS,mxREAL);
150 }
151 
153 template <typename T>
154 inline mxArray* CreateImage(int h, int w, int V);
155 
157 template <>
158 inline mxArray*
159 CreateImage<double>(int h, int w, int V)
160 {
161  if (V ==1) {
162  return CreateMatrix<double>(h,w);
163  } else {
164  mwSize dims[3];
165  dims[0]=h;
166  dims[1]=w;
167  dims[2]=V;
168  return mxCreateNumericArray(3,dims,mxDOUBLE_CLASS,mxREAL);
169  }
170 }
171 
173 template <>
174 inline mxArray*
175 CreateImage<float>(int h, int w, int V)
176 {
177  if (V ==1) {
178  return CreateMatrix<float>(h,w);
179  } else {
180  mwSize dims[3];
181  dims[0]=h;
182  dims[1]=w;
183  dims[2]=V;
184  return mxCreateNumericArray(3,dims,mxSINGLE_CLASS,mxREAL);
185  }
186 }
187 
189 template <typename T>
190 inline mxArray*
191 Create4DImage(int h, int w, int V, int dim);
192 
193 template <>
194 inline mxArray*
195 Create4DImage<double>(int h, int w, int V, int dim)
196 {
197  if (dim ==1) {
198  return CreateImage<double>(h,w,V);
199  } else {
200  mwSize dims[4];
201  dims[0]=h;
202  dims[1]=w;
203  dims[2]=V;
204  dims[3]=dim;
205  return mxCreateNumericArray(4,dims,mxDOUBLE_CLASS,mxREAL);
206  }
207 }
208 
209 template <>
210 inline mxArray*
211 Create4DImage<float>(int h, int w, int V, int dim)
212 {
213  if (dim ==1) {
214  return CreateImage<float>(h,w,V);
215  } else {
216  mwSize dims[4];
217  dims[0]=h;
218  dims[1]=w;
219  dims[2]=V;
220  dims[3]=dim;
221  return mxCreateNumericArray(4,dims,mxSINGLE_CLASS,mxREAL);
222  }
223 }
224 
226 template <typename T>
227 inline mxArray*
229 {
230  return CreateMatrix<T>(1,1);
231 }
232 
234 template <typename T>
235 inline void
236 ConvertSpMatrix(mxArray*& matlab_mat, int K,
237  int M, int n, int nzmax, const T* v, const int* r, const int* pB)
238 {
239  matlab_mat=mxCreateSparse(K,M,nzmax,mxREAL);
240  double* Pr=mxGetPr(matlab_mat);
241  for (int i = 0; i<nzmax; ++i) Pr[i]=static_cast<double>(v[i]);
242  mwSize* Ir=mxGetIr(matlab_mat);
243  for (int i = 0; i<nzmax; ++i) Ir[i]=static_cast<mwSize>(r[i]);
244  mwSize* Jc=mxGetJc(matlab_mat);
245  if (n == 0) return;
246  for (int i = 0; i<=n; ++i) Jc[i]=static_cast<mwSize>(pB[i]);
247 }
248 
250 template <typename T>
251 inline T
252 GetScalarStruct(const mxArray* pr_struct,const char* name)
253 {
254  mxArray *pr_field = mxGetField(pr_struct,0,name);
255  if (!pr_field) {
256  mexPrintf("Missing field: ");
257  mexErrMsgTxt(name);
258  }
259  return static_cast<T>(mxGetScalar(pr_field));
260 }
261 
263 inline void
264 GetStringStruct(const mxArray* pr_struct, const char* name, char* field, const mwSize length)
265 {
266  mxArray *pr_field = mxGetField(pr_struct,0,name);
267  if (!pr_field) {
268  mexPrintf("Missing field: ");
269  mexErrMsgTxt(name);
270  }
271  mxGetString(pr_field,field,length);
272 }
273 
274 inline void
275 GetString(const mxArray* pr, std::string& str)
276 {
277  mwSize buflen = mxGetNumberOfElements(pr) + 1;
278  char* buf = reinterpret_cast<char*>(mxCalloc(buflen, sizeof(char)));
279 
280  /* Copy the string data from string_array_ptr and place it into buf. */
281  if (mxGetString(pr, buf, buflen) != 0)
282  mexErrMsgTxt( "Could not convert string data.");
283  str = std::string(buf);
284 }
285 
286 inline std::string
287 GetString(const mxArray* pr)
288 {
289  mwSize buflen = mxGetNumberOfElements(pr) + 1;
290  char* buf = reinterpret_cast<char*>(mxCalloc(buflen, sizeof(char)));
291 
292  /* Copy the string data from string_array_ptr and place it into buf. */
293  if (mxGetString(pr, buf, buflen) != 0)
294  mexErrMsgTxt( "Could not convert string data.");
295  return std::string(buf);
296 }
297 
299 inline bool
300 CheckField(const mxArray* pr_struct,const char* name)
301 {
302  mxArray *pr_field = mxGetField(pr_struct,0,name);
303  if (!pr_field) {
304  mexPrintf("Missing field: ");
305  mexPrintf(name);
306  return false;
307  }
308  return true;
309 };
310 
312 template <typename T>
313 inline T
314 GetScalarStructDef(const mxArray* pr_struct, const char* name, const T def)
315 {
316  if (!pr_struct)
317  return def;
318  mxArray *pr_field = mxGetField(pr_struct,0,name);
319  return pr_field ? (T)(mxGetScalar(pr_field)) : def;
320 }
321 
322 template <>
323 inline std::string
324 GetScalarStructDef(const mxArray* pr_struct, const char* name, const std::string def)
325 {
326  if (!pr_struct)
327  return def;
328  mxArray *pr_field = mxGetField(pr_struct,0,name);
329  return pr_field ? GetString(pr_field) : def;
330 }
331 
332 inline mxArray*
333 GetArrayStruct(const mxArray* pr_struct,const char* name)
334 {
335  if (!pr_struct)
336  return NULL;
337  mxArray *pr_field = mxGetField(pr_struct,0,name);
338  return pr_field;
339 }
340 
341 inline void
342 super_flush(std::ostream& stream)
343 {
344  std::flush(stream);
345  mexEvalString("pause(0.0000000001);"); // to dump string.
346 }
347 
348 template <class TMatrixType>
349 void
350 SaveMatrixToMatFile ( const TMatrixType& matrix, const int NumberRows, const int NumberColumns, const std::string fileName, const std::string varibleName )
351 {
352  MATFile *pmat;
353  pmat = matOpen(fileName.c_str(), "w");
354  utlGlobalException(!pmat, "Error creating file " << fileName);
355  int status;
356 
357  mxArray *pa1= mxCreateDoubleMatrix(NumberRows,NumberColumns,mxREAL);
358  utlGlobalException(!pa1, "Out of memory. Unable to create mxArray.");
359 
360  double * data = (double*)mxGetData(pa1);
361  for ( int i = 0; i < NumberRows; i += 1 )
362  for ( int j = 0; j < NumberColumns; j += 1 )
363  data[j*NumberRows+i] = matrix(i,j);
364 
365  status = matPutVariableAsGlobal(pmat, varibleName.c_str(), pa1);
366  utlGlobalException(status != 0, "Error using matPutVariableAsGlobal");
367 
368  mxDestroyArray(pa1);
369  status = matClose(pmat);
370  utlGlobalException(status!=0, "Error closing file " << fileName);
371 }
372 
373 template <class TMatrixType>
374 void
375 ReadMatFileToMatrix ( const std::string fileName, const std::string varibleName, TMatrixType& matrix )
376 {
377  MATFile *pmat;
378  pmat = matOpen(fileName.c_str(), "r");
379  utlGlobalException(!pmat, "Error creating file " << fileName);
380  int status;
381 
382  mxArray *pa1= matGetVariable(pmat, varibleName.c_str());
383  utlGlobalException(!pa1, "Out of memory. Unable to create mxArray.");
384  const mwSize* dims = mxGetDimensions(pa1);
385  int row = static_cast<int>(dims[0]);
386  int column = static_cast<int>(dims[1]);
387 
388  double * data = (double*)mxGetData(pa1);
389  for ( int i = 0; i < row; i += 1 )
390  for ( int j = 0; j < column; j += 1 )
391  matrix(i,j) = data[j*row+i];
392 
393  mxDestroyArray(pa1);
394  status = matClose(pmat);
395  utlGlobalException(status!=0, "Error closing file " << fileName);
396 }
397 
398 }
399 
400 #endif
mxArray * GetArrayStruct(const mxArray *pr_struct, const char *name)
Definition: mexutils.h:333
void SaveMatrixToMatFile(const TMatrixType &matrix, const int NumberRows, const int NumberColumns, const std::string fileName, const std::string varibleName)
Definition: mexutils.h:350
mxArray * CreateMatrix< float >(int m, int n)
Create a m x n float matrix.
Definition: mexutils.h:147
mxArray * CreateImage< double >(int h, int w, int V)
Create a h x w x V double image.
Definition: mexutils.h:159
mxArray * CreateImage(int h, int w, int V)
Create a h x w x V image.
void CreateCopySparse(T *&alpha_v2, int *&alpha_r2, int *&alpha_pB2, int *&alpha_pE2, double *alpha_v, mwSize *alpha_r, mwSize *alpha_pB, mwSize *alpha_pE, int M)
Definition: mexutils.h:91
mxArray * Create4DImage(int h, int w, int V, int dim)
Create a h x w x V x dim image.
bool CheckSize(const mxArray *array, const int m, const int n)
Check the size of a 2D-array.
Definition: mexutils.h:79
bool mexCheckType< int >(const mxArray *array)
Check the type of an array (int)
Definition: mexutils.h:57
void ConvertSpMatrix(mxArray *&matlab_mat, int K, int M, int n, int nzmax, const T *v, const int *r, const int *pB)
convert sparse matrix to Matlab sparse matrix
Definition: mexutils.h:236
mxArray * CreateScalar()
Create a scalar.
Definition: mexutils.h:228
mxArray * CreateMatrix< double >(int m, int n)
Create a m x n double matrix.
Definition: mexutils.h:139
int mwSize
Definition: utils.h:38
mxArray * Create4DImage< double >(int h, int w, int V, int dim)
Definition: mexutils.h:195
void DeleteCopySparse(T *&alpha_v2, int *&alpha_r2, int *&alpha_pB2, int *&alpha_pE2, double *alpha_v, mwSize *alpha_r)
Delete a sparse matrix which has been created using createCopySparse.
Definition: mexutils.h:116
#define utlGlobalException(cond, expout)
Definition: utlCoreMacro.h:372
bool CheckField(const mxArray *pr_struct, const char *name)
get a scalar from a struct
Definition: mexutils.h:300
Definition: utl.h:90
void super_flush(std::ostream &stream)
Definition: mexutils.h:342
bool mexCheckType< bool >(const mxArray *array)
Check the type of an array (int)
Definition: mexutils.h:62
void ReadMatFileToMatrix(const std::string fileName, const std::string varibleName, TMatrixType &matrix)
Definition: mexutils.h:375
bool mexCheckType(const mxArray *array)
Check the type of an array.
mxArray * Create4DImage< float >(int h, int w, int V, int dim)
Definition: mexutils.h:211
void GetStringStruct(const mxArray *pr_struct, const char *name, char *field, const mwSize length)
get a scalar from a struct
Definition: mexutils.h:264
bool mexCheckType< double >(const mxArray *array)
Check the type of an array (double)
Definition: mexutils.h:47
mxArray * CreateMatrix(int m, int n)
Create a m x n matrix.
void GetString(const mxArray *pr, std::string &str)
Definition: mexutils.h:275
macros for utlCore
T GetScalarStruct(const mxArray *pr_struct, const char *name)
get a scalar from a struct
Definition: mexutils.h:252
bool mexCheckType< float >(const mxArray *array)
Check the type of an array (float)
Definition: mexutils.h:52
T GetScalarStructDef(const mxArray *pr_struct, const char *name, const T def)
get a scalar from a struct and provide a default value
Definition: mexutils.h:314
mxArray * CreateImage< float >(int h, int w, int V)
Create a h x w x V float image.
Definition: mexutils.h:175