DMRITool  v0.1.1-139-g860d86b4
Diffusion MRI Tool
mexReadGrad.cxx
Go to the documentation of this file.
1 
18 #include "utl.h"
19 
20 #include "mex.h"
21 #include "mexutils.h"
22 #include "utlMEX.h"
23 
25 
26 template <typename T>
27  inline void callFunction(mxArray* plhs[], const mxArray* prhs[],
28  const int nlhs,const int nrhs)
29 {
30  utlGlobalException(mxGetClassID(prhs[0])!=mxCHAR_CLASS, "the first input has to be a string");
31 
32  std::string homeStr = getenv ("HOME");
33  std::string gradPath = homeStr + "/.dmritool/Data/";
34  std::string gradFileStr;
35  // std::cout << "gradPath = " << gradPath << std::endl << std::flush;
36 
37  std::string gradStr;
38  utl::GetString(prhs[0], gradStr);
39 
40  typedef utl::NDArray<T,2> MatrixType;
41 
42  utl_shared_ptr<MatrixType> grad(new MatrixType());
43  std::string ext, fileNoExt;
44  utl::GetFileExtension(gradStr, ext, fileNoExt);
45 
46  if (ext=="txt")
47  {
48  gradFileStr = gradStr;
49  }
50  else if (ext=="")
51  {
52  utlGlobalException(nrhs!=2, "should have the second parameter");
53  int num = mxGetScalar(prhs[1]);
54  if (utl::StringCompareCaseIgnored(gradStr, "elec"))
55  {
56  if (num<10)
57  gradFileStr = gradPath + "ElectricRepulsion/Elec00" + utl::ConvertNumberToString(num) + ".txt";
58  else if (num<100)
59  gradFileStr = gradPath + "ElectricRepulsion/Elec0" + utl::ConvertNumberToString(num) + ".txt";
60  else
61  gradFileStr = gradPath + "ElectricRepulsion/Elec" + utl::ConvertNumberToString(num) + ".txt";
62  }
63  else if (utl::StringCompareCaseIgnored(gradStr, "tess"))
64  {
65  gradFileStr = gradPath + "Tessellation/directions_t" + utl::ConvertNumberToString(num) + ".txt";
66  }
67  else
68  utlGlobalException(true, "wrong mode");
69  }
70  else
71  utlGlobalException(true, "wrong mode or gradient file name");
72 
73  grad = utl::ReadGrad<T>(gradFileStr,DIRECTION_NODUPLICATE, CARTESIAN_TO_CARTESIAN);
74 
75  utl::GetMXArrayFromUtlMatrix(grad.get(), plhs[0]);
76 }
77 
78 void mexFunction(int nlhs, mxArray *plhs[],
79  int nrhs, const mxArray *prhs[])
80 {
81  utlGlobalException(nrhs!=1 && nrhs!=2, "Bad number of inputs arguments");
82  utlGlobalException(nlhs!=1, "Bad number of outputs arguments");
83 
84  // if (mxGetClassID(prhs[0]) == mxSINGLE_CLASS)
85  // callFunction<float>(plhs,prhs,nlhs,nrhs);
86  // else
87  callFunction<double>(plhs,prhs,nlhs,nrhs);
88 }
89 
helper functions specifically used in dmritool
std::string ConvertNumberToString(const T value, const int precision=6)
Definition: utlCore.h:740
NDArray<T,2> is a row-major matrix.
Definition: utlMatrix.h:37
void mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[])
Definition: mexReadGrad.cxx:78
void GetMXArrayFromUtlMatrix(const NDArray< T, 2 > *mat, mxArray *&pr)
Definition: utlMEX.h:50
void callFunction(mxArray *plhs[], const mxArray *prhs[], const int nlhs, const int nrhs)
Definition: mexReadGrad.cxx:27
#define utlGlobalException(cond, expout)
Definition: utlCoreMacro.h:372
Contains miscellaneous functions for mex files. utl functions for mex code. Some codes are from spams...
bool StringCompareCaseIgnored(const std::string &str1, const std::string &str2)
Definition: utlCore.h:311
void GetString(const mxArray *pr, std::string &str)
Definition: mexutils.h:275
void GetFileExtension(const std::string &fileNameAbsolute, std::string &ext, std::string &fileNoExt)
Definition: utlCore.h:559