DMRITool  v0.1.1-139-g860d86b4
Diffusion MRI Tool
mexGetSamplesFromSPFCoefficients.cxx
Go to the documentation of this file.
1 
18 #include "mex.h"
19 #include "utl.h"
20 #include "utlMEX.h"
21 
24 
25 
26 template <typename T>
27  inline void callFunction(mxArray* plhs[], const mxArray* prhs[],
28  const int nlhs,const int nrhs)
29 {
30  utlGlobalException(!utl::mexCheckType<T>(prhs[0]),"type of argument 1 is not consistent");
31  utlGlobalException(!utl::mexCheckType<T>(prhs[1]),"type of argument 2 is not consistent");
32 
33  typedef itk::VectorImage<T, 3> VectorImageType;
34  typedef itk::Image<double, 3> ImageType;
35  typedef utl::NDArray<T,2> MatrixType;
36 
38  typename ProfileFromSPFFilterType::Pointer profEstimator = ProfileFromSPFFilterType::New();
39 
40  utlGlobalException(mxGetNumberOfDimensions(prhs[0])!=4, "the input should have 4 dimension");
41 
42  const mwSize* dimsDWIs = mxGetDimensions(prhs[0]);
43  int Nx = static_cast<int>(dimsDWIs[0]);
44  int Ny = static_cast<int>(dimsDWIs[1]);
45  int Nz = static_cast<int>(dimsDWIs[2]);
46  int numberOfDWIs = static_cast<int>(dimsDWIs[3]);
47 
48  typename VectorImageType::Pointer dwiImage = VectorImageType::New();
49  itk::GetITKVectorImageFromMXArray(prhs[0], dwiImage);
50  profEstimator->SetInput(dwiImage);
51 
52  // utlGlobalException(nrhs==3 && mxGetNumberOfDimensions(prhs[1])!=1, "radius should be a scalar value. mxGetNumberOfDimensions(prhs[1])="<< mxGetNumberOfDimensions(prhs[1]));
53  if (nrhs==3)
54  {
55  double radius = mxGetScalar(prhs[1]);
56  profEstimator->SetRadius(radius);
57  }
58 
59  const mxArray* params = nrhs==3?prhs[2]:prhs[3];
60 
61  int verbose = utl::GetScalarStructDef<int>(params,"verbose",1);
62  utl::LogLevel = verbose;
63 
64  double MD0 = utl::GetScalarStructDef<double>(params,"MD0",-1.0);
65  double tau = utl::GetScalarStructDef<double>(params,"tau",ONE_OVER_4_PI_2);
66  double scale = utl::GetScalarStructDef<double>(params,"scale",-1.0);
67  int sh = utl::GetScalarStructDef<int>(params,"sh",-1);
68  utlGlobalException(sh<=0, "need to set sh");
69  int ra = utl::GetScalarStructDef<int>(params,"ra",-1);
70  utlGlobalException(ra<=0, "need to set ra");
71  std::string basisType = utl::GetScalarStructDef<std::string>(params,"basisType","SPF");
72  double radius = utl::GetScalarStructDef<double>(params,"radius",0.015);
73  mxArray* mdImageArray = utl::GetArrayStruct(params, "mdImage" );
74  mxArray* maskArray = utl::GetArrayStruct(params, "mask" );
75  bool fourier = utl::GetScalarStructDef<bool>(params,"fourier",false);
76  bool inqspace = utl::GetScalarStructDef<bool>(params,"inqspace",true);
77 
78  int thread = utl::GetScalarStructDef<int>(params,"thread",-1);
79 
80  if (nrhs==4)
81  {
82  typename ProfileFromSPFFilterType::STDVectorPointer radiusVec(new typename ProfileFromSPFFilterType::STDVectorType());
83  utl::GetSTDVectorFromMXArray(prhs[1], radiusVec.get() );
84  profEstimator->SetRadiusVector(radiusVec);
85 
86  utl_shared_ptr<MatrixType> grad(new MatrixType());
87  utl::GetUtlMatrixFromMXArray(prhs[2], grad.get() );
88  *grad = utl::CartesianToSpherical(*grad); // convert to spherical format
89  profEstimator->SetOrientations(grad);
90  }
91  profEstimator->SetSHRank(sh);
92  profEstimator->SetRadialRank(ra);
93  if (MD0>0)
94  profEstimator->SetMD0(MD0);
95  if (tau>0)
96  profEstimator->SetTau(tau);
97  profEstimator->SetBasisScale(scale);
98  profEstimator->SetIsFourier(fourier);
99  profEstimator->SetIsInQSpace(inqspace);
100 
101  if (basisType=="SPF")
102  {
103  profEstimator->SetBasisType(ProfileFromSPFFilterType::SPF);
104  }
105  else if (basisType=="DSPF")
106  profEstimator->SetBasisType(ProfileFromSPFFilterType::DSPF);
107 
108  if (mdImageArray)
109  {
110  typename ProfileFromSPFFilterType::ScalarImagePointer mdImage = ProfileFromSPFFilterType::ScalarImageType::New();
111  typename ProfileFromSPFFilterType::ScalarImagePointer scaleImage = ProfileFromSPFFilterType::ScalarImageType::New();
112  itk::GetITKImageFromMXArray(mdImageArray, mdImage);
114  ScaleFromMDfilterType::Pointer scaleFromMDfilter = ScaleFromMDfilterType::New();
115  scaleFromMDfilter->SetMD0(MD0);
116  scaleFromMDfilter->SetTau(tau);
117  if (basisType=="SPF" || basisType=="SHORE" || basisType=="DPI" || basisType=="QBI")
118  scaleFromMDfilter->SetIsOriginalBasis(true);
119  else if (basisType=="DSPF")
120  scaleFromMDfilter->SetIsOriginalBasis(false);
121  scaleFromMDfilter->SetInput(mdImage);
122  scaleFromMDfilter->Update();
123  scaleImage = scaleFromMDfilter->GetOutput();
124  profEstimator->SetScaleImage(scaleImage);
125  }
126 
127  if (maskArray)
128  {
129  typename ProfileFromSPFFilterType::ScalarImagePointer maskImage = ProfileFromSPFFilterType::ScalarImageType::New();
130  itk::GetITKImageFromMXArray(maskArray, maskImage);
131  profEstimator->SetMaskImage(maskImage);
132  }
133 
134  profEstimator->SetDebug(utl::IsLogDebug());
135  profEstimator->SetLogLevel(utl::LogLevel);
137  if (thread>0)
138  profEstimator->SetNumberOfThreads(thread);
139 
140  // profEstimator->Print(std::cout<<"profEstimator=");
141  profEstimator->Update();
142 
143  typename VectorImageType::Pointer samples = profEstimator->GetOutput();
144  itk::GetMXArrayFromITKVectorImage(samples, plhs[0]);
145 }
146 
147 void mexFunction(int nlhs, mxArray *plhs[],
148  int nrhs, const mxArray *prhs[])
149 {
150  utlGlobalException(nrhs!=3 && nrhs!=4, "Bad number of inputs arguments");
151  utlGlobalException(nlhs!=1, "Bad number of outputs arguments");
152 
153  // if (mxGetClassID(prhs[0]) == mxSINGLE_CLASS)
154  // callFunction<float>(plhs,prhs,nlhs,nrhs);
155  // else
156  callFunction<double>(plhs,prhs,nlhs,nrhs);
157 }
mxArray * GetArrayStruct(const mxArray *pr_struct, const char *name)
Definition: mexutils.h:333
helper functions specifically used in dmritool
Calculate DWI/EAP profile from SPF coefficients.
void mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[])
void GetMXArrayFromITKVectorImage(const SmartPointer< VectorImage< T, VImageDimension > > &image, mxArray *&pr)
Definition: mexITK.h:96
void callFunction(mxArray *plhs[], const mxArray *prhs[], const int nlhs, const int nrhs)
NDArray<T,2> is a row-major matrix.
Definition: utlMatrix.h:37
void GetITKVectorImageFromMXArray(const mxArray *pr, SmartPointer< VectorImage< T, 3 > > &image)
Definition: mexITK.h:128
int mwSize
Definition: utils.h:38
NDArray< T, 2 > CartesianToSpherical(const NDArray< T, 2 > &in)
bool IsLogDebug(const int level=utl::LogLevel)
Definition: utlCoreMacro.h:213
#define utlGlobalException(cond, expout)
Definition: utlCoreMacro.h:372
#define ONE_OVER_4_PI_2
Definition: utlCoreMacro.h:63
static int LogLevel
Definition: utlCoreMacro.h:203
static void InitializeThreadedLibraries(const int numThreads)
Definition: utl.h:327
void GetUtlMatrixFromMXArray(const mxArray *pr, NDArray< T, 2 > *mat)
Definition: utlMEX.h:33
itk::VectorImage< ScalarType, 3 > VectorImageType
Definition: 4DImageMath.cxx:30
void GetSTDVectorFromMXArray(const mxArray *pr, std::vector< T > *vec)
Definition: mexSTD.h:30
void GetITKImageFromMXArray(const mxArray *pr, SmartPointer< Image< T, VImageDimension > > &image)
Definition: mexITK.h:55