DMRITool  v0.1.1-139-g860d86b4
Diffusion MRI Tool
utlDMRIStoredTables.h
Go to the documentation of this file.
1 
11 #ifndef __utlDMRIStoredTables_h
12 #define __utlDMRIStoredTables_h
13 
15 
16 #include "DMRITOOLConfigure.h"
17 #include "utlDMRI.h"
18 #include "utlCoreMacro.h"
19 #include "utlITK.h"
20 #include "utlCore.h"
21 
22 // #include "itkSphericalHarmonicsGenerator.h"
23 #include "utlNDArray.h"
24 
25 
26 
27 namespace itk
28 {
29 
30 
34 static LUTExpPointer lutExp = LUTExpType::New();
35 
36 inline void
38 {
39  if (!lutExp->IsTableBuilt())
40  {
41  lutExp = LUTExpType::New();
42  lutExp->SetVariableMax(0);
43  lutExp->SetVariableMin(-30);
44  lutExp->SetNumberOfBins(30*5e3);
45  lutExp->BuildTable();
46  }
47 }
48 
49 inline double
50 lutExpValue(const double x)
51 {
52  if (x<=0)
53  return lutExp->GetFunctionValue(x);
54  else
55  return std::exp(x);
56 }
57 
58 }
59 
60 namespace utl
61 {
62 
64 static utl_shared_ptr<NDArray<double,3> > SH3IntegralTable(new NDArray<double,3>);
65 
66 
73 template < class T=double >
75  {
76 public:
77 
79  typedef utl_shared_ptr<GradientTable<T> > Pointer;
80 
83  typedef utl_shared_ptr<MatrixType> MatrixPointer;
84 
87  {
88  }
89 
91  virtual ~GradientTable ()
92  {}
93 
94  static Pointer GetInstance()
95  {
96  if (!m_Instance)
97  {
98  m_Instance = utl_shared_ptr<Self>(new Self());
99  if (m_Instance->m_GradTable.size()!=7)
100  {
101  m_Instance->m_GradTable.resize(7);
102  for ( int i = 0; i < 7; ++i )
103  m_Instance->m_GradTable[i] = MatrixPointer(new MatrixType());
104  }
105  }
106  return m_Instance;
107  }
108 
110  static void
111  Initialize(const int tessorder)
112  {
113  utlSAGlobalException(tessorder<1 || tessorder>7)(tessorder).msg("wrong tess order. tessorder should be in [1,7]");
114  Pointer instance = GetInstance();
115  MatrixPointer mat = instance->m_GradTable[tessorder-1];
116  switch ( tessorder )
117  {
118  case 1 : { if (mat->Rows()==0) mat = utl::ReadGrad<double>(CreateExpandedPath(DirectionsT1), DIRECTION_NODUPLICATE, CARTESIAN_TO_CARTESIAN); break; }
119  case 2 : { if (mat->Rows()==0) mat = utl::ReadGrad<double>(CreateExpandedPath(DirectionsT2), DIRECTION_NODUPLICATE, CARTESIAN_TO_CARTESIAN); break; }
120  case 3 : { if (mat->Rows()==0) mat = utl::ReadGrad<double>(CreateExpandedPath(DirectionsT3), DIRECTION_NODUPLICATE, CARTESIAN_TO_CARTESIAN); break; }
121  case 4 : { if (mat->Rows()==0) mat = utl::ReadGrad<double>(CreateExpandedPath(DirectionsT4), DIRECTION_NODUPLICATE, CARTESIAN_TO_CARTESIAN); break; }
122  case 5 : { if (mat->Rows()==0) mat = utl::ReadGrad<double>(CreateExpandedPath(DirectionsT5), DIRECTION_NODUPLICATE, CARTESIAN_TO_CARTESIAN); break; }
123  case 6 : { if (mat->Rows()==0) mat = utl::ReadGrad<double>(CreateExpandedPath(DirectionsT6), DIRECTION_NODUPLICATE, CARTESIAN_TO_CARTESIAN); break; }
124  case 7 : { if (mat->Rows()==0) mat = utl::ReadGrad<double>(CreateExpandedPath(DirectionsT7), DIRECTION_NODUPLICATE, CARTESIAN_TO_CARTESIAN); break; }
125  default : utlSAGlobalException(true)(tessorder).msg("wrong tess order. tessorder should be in [1,7]"); break;
126  }
127  instance->m_GradTable[tessorder-1] = mat;
128  }
129 
131  static MatrixPointer
132  GetGrad(const int tessorder, const int NoSymmetricDuple=DIRECTION_NODUPLICATE, const int mode= CARTESIAN_TO_SPHERICAL,
133  const int flipx=DIRECTION_NOFLIP, const int flipy=DIRECTION_NOFLIP, const int flipz=DIRECTION_NOFLIP, const bool need_normalize=true)
134  {
135  utlSAGlobalException(tessorder<1 || tessorder>7)(tessorder).msg("wrong tess order. tessorder should be in [1,7]");
136  utlSAException(mode==SPHERICAL_TO_CARTESIAN || mode==SPHERICAL_TO_SPHERICAL)(mode).msg("wrong mode. The stored gradient tables are in cartesian mode");
137 
138  Pointer instance = GetInstance();
139  MatrixPointer grad = instance->m_GradTable[tessorder-1];
140  utlException(grad->Size()==0, "call Initialize first to read grad");
141  MatrixPointer mat(new MatrixType());
142  if (NoSymmetricDuple==DIRECTION_DUPLICATE)
143  mat->ReSize(2*grad->Rows(),3);
144  else
145  mat->ReSize(grad->Rows(),3);
146 
147  for ( int i=0, j=0; i < grad->Rows(); ++i, ++j )
148  {
149  (*mat)(j,0) = flipx==DIRECTION_FLIP ? -(*grad)(i,0) : (*grad)(i,0);
150  (*mat)(j,1) = flipy==DIRECTION_FLIP ? -(*grad)(i,1) : (*grad)(i,1);
151  (*mat)(j,2) = flipz==DIRECTION_FLIP ? -(*grad)(i,2) : (*grad)(i,2);
152 
153  if (NoSymmetricDuple==DIRECTION_DUPLICATE)
154  {
155  j++;
156  (*mat)(j,0) = -(*mat)(j-1,0);
157  (*mat)(j,1) = -(*mat)(j-1,1);
158  (*mat)(j,2) = -(*mat)(j-1,2);
159  }
160  }
161 
162  if (mode==CARTESIAN_TO_SPHERICAL)
163  *mat = utl::CartesianToSpherical(*mat);
164 
165  return mat;
166  }
167 
168 
169 protected:
170 
171  std::vector<MatrixPointer> m_GradTable;
172 
173  static Pointer m_Instance;
174 
175 private:
177  GradientTable ( const GradientTable &other );
179  GradientTable & operator = ( const GradientTable &other );
180 
181  };
182 
183 template<>
185 
186 
187 template <class T>
188 inline utl_shared_ptr<NDArray<T,2> >
189 ReadGrad(const int tess, const int NoSymmetricDuple=DIRECTION_NODUPLICATE, const int mode= CARTESIAN_TO_SPHERICAL,
190  const int flipx=DIRECTION_NOFLIP, const int flipy=DIRECTION_NOFLIP, const int flipz=DIRECTION_NOFLIP, const bool need_normalize=true)
191 {
193  utl_shared_ptr< utl::Matrix<double> > grad = utl::GradientTable<double>::GetGrad(tess, NoSymmetricDuple, mode, flipx, flipy, flipz);
194  return grad;
195 }
196 
197 template <>
198 inline utl_shared_ptr<NDArray<float,2> >
199 ReadGrad<float>(const int tess, const int NoSymmetricDuple, const int mode,
200  const int flipx, const int flipy, const int flipz, const bool need_normalize)
201 {
202  utl_shared_ptr< utl::Matrix<double> > grad = utl::ReadGrad<double>(tess, NoSymmetricDuple, mode, flipx, flipy, flipz);
203  utl_shared_ptr<utl::Matrix<float> > matOut(new utl::Matrix<float>());
204  *matOut = *grad;
205  return matOut;
206 }
207 
208 template <class T>
209 inline utl_shared_ptr<NDArray<T,2> >
210 ReadGradElectricRepulsion(const int num, const int NoSymmetricDuple=DIRECTION_NODUPLICATE, const int mode= CARTESIAN_TO_SPHERICAL,
211  const int flipx=DIRECTION_NOFLIP, const int flipy=DIRECTION_NOFLIP, const int flipz=DIRECTION_NOFLIP, const bool need_normalize=true)
212 {
213  char buf[255];
214  if (num<10)
215  sprintf(buf, "00%d", num);
216  else if (num<100)
217  sprintf(buf, "0%d", num);
218  else
219  sprintf(buf, "%d", num);
220  std::string index (buf);
221  std::string file= CreateExpandedPath(GradientsElec) + std::string("/Elec") + index + std::string(".txt");
222  return ReadGrad<T>(file, NoSymmetricDuple, mode, flipx, flipy, flipz, need_normalize);
223 }
224 
225 }
226 
227 
228 #endif
NDArray<T,1> is a vector class which uses blas mkl.
Definition: utlVector.h:36
static std::shared_ptr< NDArray< double, 3 > > SH3IntegralTable(new NDArray< double, 3 >)
static const std::string DirectionsT5
static MatrixPointer GetGrad(const int tessorder, const int NoSymmetricDuple=DIRECTION_NODUPLICATE, const int mode=CARTESIAN_TO_SPHERICAL, const int flipx=DIRECTION_NOFLIP, const int flipy=DIRECTION_NOFLIP, const int flipz=DIRECTION_NOFLIP, const bool need_normalize=true)
use UnaryFunctorLookUpTable to accelerate evaluation of functions.
std::shared_ptr< NDArray< T, 2 > > ReadGrad(const int tess, const int NoSymmetricDuple=DIRECTION_NODUPLICATE, const int mode=CARTESIAN_TO_SPHERICAL, const int flipx=DIRECTION_NOFLIP, const int flipy=DIRECTION_NOFLIP, const int flipz=DIRECTION_NOFLIP, const bool need_normalize=true)
static LUTExpPointer lutExp
gradient table related functions and stored tables.
#define utlException(cond, expout)
Definition: utlCoreMacro.h:548
#define utlSAGlobalException(expr)
Definition: utlCoreMacro.h:362
NDArray<T,2> is a row-major matrix.
Definition: utlMatrix.h:37
static const std::string DirectionsT7
void InitializeLUTExp()
LUTExpType::Pointer LUTExpPointer
static Pointer GetInstance()
utl::NDArray< T, 1 > VectorType
NDArray< T, 2 > CartesianToSpherical(const NDArray< T, 2 > &in)
std::string CreateExpandedPath(const std::string &path)
Definition: utlCore.h:509
static const std::string DirectionsT3
static Pointer m_Instance
Definition: utl.h:90
std::shared_ptr< NDArray< T, 2 > > ReadGradElectricRepulsion(const int num, const int NoSymmetricDuple=DIRECTION_NODUPLICATE, const int mode=CARTESIAN_TO_SPHERICAL, const int flipx=DIRECTION_NOFLIP, const int flipy=DIRECTION_NOFLIP, const int flipz=DIRECTION_NOFLIP, const bool need_normalize=true)
#define utlSAException(expr)
Definition: utlCoreMacro.h:543
UnaryFunctorLookUpTable< utl::Functor::Exp< double > > LUTExpType
std::shared_ptr< GradientTable< T > > Pointer
static void Initialize(const int tessorder)
std::vector< MatrixPointer > m_GradTable
GradientTable< T > Self
std::shared_ptr< NDArray< float, 2 > > ReadGrad< float >(const int tess, const int NoSymmetricDuple, const int mode, const int flipx, const int flipy, const int flipz, const bool need_normalize)
std::shared_ptr< MatrixType > MatrixPointer
static const std::string GradientsElec
double lutExpValue(const double x)
utl::NDArray< T, 2 > MatrixType
macros for utlCore
static const std::string DirectionsT6
static const std::string DirectionsT2
static const std::string DirectionsT1
static const std::string DirectionsT4