DMRITool  v0.1.1-139-g860d86b4
Diffusion MRI Tool
utl.h
Go to the documentation of this file.
1 
18 #ifndef __utl_h
19 #define __utl_h
20 
21 
22 #include "DMRITOOLConfigure.h"
23 
24 #ifndef UTL_MAX_THREADS
25 #define UTL_MAX_THREADS 8
26 #endif
27 
28 #ifdef UTL_USE_OPENMP
29 #include <omp.h>
30 #include "utlOpenMP.h"
31 #endif
32 
33 #include "utlSTDHeaders.h"
34 
35 #ifdef UTL_USE_MKL
36 #include "utlMKL.h"
37 #endif
38 
39 
40 #include "utlCore.h"
41 #include "utlNDArray.h"
42 
43 #include "utlMath.h"
44 #include "utlVNL.h"
45 #include "utlVNLIO.h"
46 #include "utlITK.h"
47 #include "utlVNLBlas.h"
48 #include "utlVNLLapack.h"
49 
50 #include "utlDMRI.h"
51 
53 #include "utlDMRIStoredTables.h"
54 
56 
59 namespace itk
60 {
61 
62 
64 template <class PixelType>
65 inline void
66 ReadVectorImage ( const std::string& filename, SmartPointer<VectorImage<PixelType,3> > &image, const std::string& printInfo="Reading Image:" )
67 {
68  utlGlobalException(!utl::IsFileExist(filename), filename + " does not exist");
69  typedef VectorImage<PixelType,3> VectorImageType;
70  if (itk::IsVectorImage(filename))
71  itk::ReadImage<VectorImageType>(filename, image, printInfo);
72  else
73  {
74  typedef itk::Image<PixelType, 4> MultiVolumeImageType;
75  typename MultiVolumeImageType::Pointer MultiVolumeImage;
76  itk::ReadImage<MultiVolumeImageType>(filename, MultiVolumeImage, printInfo);
77 
79  typename MultiVolumeToVectorFilter::Pointer filter = MultiVolumeToVectorFilter::New();
80  filter->SetInput(MultiVolumeImage);
81  filter->Update();
82  image = filter->GetOutput();
83  }
84 }
85 
86 }
87 
88 
89 
90 namespace utl
91 {
92 
93 // [>* The table of integration of triple SH basis (real, thesis), genegrated by print_sh_integration <]
94 // const static std::string SH3Itegralhdr = std::string(SH3Integral_HDR);
95 
96 // [>* gradients file for tess=1 with 6 directions <]
97 // const static std::string DirectionsT1 = std::string(Path_Directions_T1);
98 // [>* gradients file for tess=2 with 21 directions <]
99 // const static std::string DirectionsT2 = std::string(Path_Directions_T2);
100 // [>* gradients file for tess=3 with 81 directions <]
101 // const static std::string DirectionsT3 = std::string(Path_Directions_T3);
102 // [>* gradients file for tess=4 with 321 directions <]
103 // const static std::string DirectionsT4 = std::string(Path_Directions_T4);
104 // [>* gradients file for tess=5 with 1281 directions <]
105 // const static std::string DirectionsT5 = std::string(Path_Directions_T5);
106 // [>* gradients file for tess=6 with 5121 directions <]
107 // const static std::string DirectionsT6 = std::string(Path_Directions_T6);
108 // [>* gradients file for tess=7 with 20481 directions <]
109 // const static std::string DirectionsT7 = std::string(Path_Directions_T7);
110 
111 
112 template <class T>
113 itk::VariableLengthVector<T>
115 {
116  itk::VariableLengthVector<T> v(vec.Size());
117  for ( int i = 0; i < vec.Size(); i += 1 )
118  v[i] = vec[i];
119  return v;
120 }
121 
122 template <class T>
124 VariableLengthVectorToUtlVector ( const itk::VariableLengthVector<T>& vec )
125 {
126  NDArray<T,1> v(vec.GetSize());
127  for ( int i = 0; i < vec.GetSize(); i += 1 )
128  v[i] = vec[i];
129  return v;
130 }
131 
132 // [>* generate SH basis with given rank and the gradients <]
133 // template <class T>
134 // inline utl_shared_ptr<vnl_matrix<T> >
135 // ComputeSHMatrix ( const unsigned int rank, const vnl_matrix<T>& grad, const int mode)
136 // {
137 // utlException(grad.rows()==0 || grad.columns()!=3, "wrong size of gradients!");
138 // int numberOfBasisFunctions = (rank + 1)*(rank + 2)/2;
139 // int numberOfDirections = grad.rows();
140 // vnl_matrix<T> gradSpherical;
141 // if (mode==CARTESIAN_TO_SPHERICAL)
142 // gradSpherical = CartesianToSpherical(grad);
143 // else if (mode==SPHERICAL_TO_SPHERICAL)
144 // ;
145 // else
146 // utlGlobalException(true, "wrong mode");
147 
148 // utl_shared_ptr<vnl_matrix<T> > BMatrix (new vnl_matrix<T>(numberOfDirections, numberOfBasisFunctions));
149 
150 // // itk::SphericalHarmonicsGenerator<double>::Pointer sh = itk::SphericalHarmonicsGenerator<double>::New();
151 // int jj=0;
152 // for ( unsigned int k = 0; k < numberOfDirections; k++ )
153 // {
154 // jj = 0;
155 // for ( int l = 0; l <= rank; l += 2 )
156 // for ( int m = -l; m <= l; m++ )
157 // {
158 // if (mode==SPHERICAL_TO_SPHERICAL)
159 // (*BMatrix)(k,jj) = itk::SphericalHarmonicsGenerator<double>::RealSH(l,m,grad(k,1),grad(k,2));
160 // else
161 // (*BMatrix)(k,jj) = itk::SphericalHarmonicsGenerator<double>::RealSH(l,m,gradSpherical(k,1),gradSpherical(k,2));
162 // jj++;
163 // }
164 // }
165 // return BMatrix;
166 // }
167 
169 template <class T>
170 inline utl_shared_ptr<NDArray<T,2> >
171 ComputeSHMatrix ( const unsigned int rank, const NDArray<T,2>& grad, const int mode)
172 {
173  utlException(grad.Rows()==0 || grad.Columns()!=3, "wrong size of gradients!");
174  int numberOfBasisFunctions = (rank + 1)*(rank + 2)/2;
175  int numberOfDirections = grad.Rows();
176  NDArray<T,2> gradSpherical;
177  if (mode==CARTESIAN_TO_SPHERICAL)
178  gradSpherical = CartesianToSpherical(grad);
179  else if (mode==SPHERICAL_TO_SPHERICAL)
180  ;
181  else
182  utlGlobalException(true, "wrong mode");
183 
184  utl_shared_ptr<NDArray<T,2> > BMatrix (new NDArray<T,2>(numberOfDirections, numberOfBasisFunctions));
185 
186  // itk::SphericalHarmonicsGenerator<double>::Pointer sh = itk::SphericalHarmonicsGenerator<double>::New();
187  int jj=0;
188  for ( unsigned int k = 0; k < numberOfDirections; k++ )
189  {
190  jj = 0;
191  for ( int l = 0; l <= rank; l += 2 )
192  for ( int m = -l; m <= l; m++ )
193  {
194  if (mode==SPHERICAL_TO_SPHERICAL)
195  (*BMatrix)(k,jj) = itk::SphericalHarmonicsGenerator<double>::RealSH(l,m,grad(k,1),grad(k,2));
196  else
197  (*BMatrix)(k,jj) = itk::SphericalHarmonicsGenerator<double>::RealSH(l,m,gradSpherical(k,1),gradSpherical(k,2));
198  jj++;
199  }
200  }
201  return BMatrix;
202 }
203 
204 // template < class T >
205 // void
206 // MatchBVectorAndGradientMatrix (const T& br, std::vector<T>& vec, const vnl_matrix<T>& grad )
207 // {
208 // utlException(grad.rows()==0, "grad.size()==0");
209 // vec = std::vector<T>(grad.rows(), br);
210 // }
211 
212 // template < class T >
213 // void
214 // MatchBVectorAndGradientMatrix ( std::vector<T>& vec, vnl_matrix<T>& grad )
215 // {
216 // utlException(vec.size()==0 || grad.rows()==0, "wrong size! vec.size()="<<vec.size()<<", grad.rows()="<<grad.rows());
217 
218 // if (grad.rows()==vec.size())
219 // return;
220 
221 // std::vector<T> vec_result;
222 // if (vec.size()==1)
223 // {
224 // MatchBVectorAndGradientMatrix(vec[0], vec_result, grad);
225 // vec = vec_result;
226 // return;
227 // }
228 
229 // vec_result.resize(vec.size()* grad.rows());
230 // vnl_matrix<T> grad_result(vec.size()* grad.rows(), grad.columns());
231 // for (int i = 0; i<vec.size(); i++)
232 // {
233 // for ( int j = 0; j < grad.rows(); j += 1 )
234 // {
235 // vec_result[i*grad.rows()+j] = vec[i];
236 // for ( int kk = 0; kk < 3; kk += 1 )
237 // grad_result(i*grad.rows()+j,kk) = grad(j,kk);
238 // }
239 // }
240 // vec = vec_result;
241 // grad = grad_result;
242 // return;
243 // }
244 
245 template <class PointsContainer, class VnlValueType>
246 void
247 PointsContainerToUtlMatrix ( const PointsContainer& points, utl::NDArray<VnlValueType,2>& matrix )
248 {
249  matrix.Clear();
250  typedef typename PointsContainer::ConstIterator PointsIterator;
251  typedef typename PointsContainer::Element PointType;
252 
253  const unsigned int pointDimension = PointType::PointDimension;
254  unsigned int numberOfPoints = points.Size();
255  if (numberOfPoints==0)
256  return;
257 
258  matrix.ReSize(numberOfPoints, pointDimension);
259  PointsIterator iterator = points.Begin();
260  PointsIterator end = points.End();
261 
262  unsigned int count=0;
263  while( iterator != end )
264  {
265  PointType orientation = iterator.Value();
266  for (unsigned int k=0; k<pointDimension; k++)
267  matrix(count,k) = orientation[k];
268  iterator++;
269  count++;
270  }
271 }
272 
273 template <class VnlValueType, class PointsContainer>
274 void
275 UtlMatrixToPointsContainer ( const NDArray<VnlValueType,2>& matrix, PointsContainer& points )
276 {
277  points.Initialize();
278  typedef typename PointsContainer::Element PointType;
279 
280  const unsigned int pointDimension = PointType::PointDimension;
281  utlGlobalException(pointDimension!=matrix.Columns(), "wrong size of matrix or point dimension. pointDimension="<< pointDimension << ", matrix.Columns()="<<matrix.Columns());
282 
283  for ( int i = 0; i < matrix.Rows(); i += 1 )
284  {
285  PointType point;
286  for ( int j = 0; j < matrix.Columns(); j += 1 )
287  {
288  point[j] = matrix(i,j);
289  }
290  points.InsertElement(i, point);
291  }
292 }
293 
294 
295 
296 static inline int
297 InitializeMKL(const int numThreads)
298 {
299  int NUM_THREADS;
300 #ifdef UTL_USE_MKL
301  NUM_THREADS = (numThreads <= 0 ) ? std::min(UTL_MAX_THREADS,mkl_get_max_threads()) : numThreads;
302  mkl_domain_set_num_threads ( NUM_THREADS, MKL_DOMAIN_ALL );
303  mkl_set_num_threads(NUM_THREADS);
304  mkl_set_dynamic(0);
305 #else
306  NUM_THREADS = 1;
307 #endif
308  return NUM_THREADS;
309 }
310 
311 static inline int
312 InitializeOpenMP(const int numThreads)
313 {
314  int NUM_THREADS;
315 #ifdef UTL_USE_OPENMP
316  NUM_THREADS = (numThreads <= 0) ? std::min(UTL_MAX_THREADS,omp_get_num_procs()) : numThreads;
317  omp_set_nested(0);
318  omp_set_dynamic(0);
319  omp_set_num_threads(NUM_THREADS);
320 #else
321  NUM_THREADS = 1;
322 #endif
323  return NUM_THREADS;
324 }
325 
326 static inline void
327 InitializeThreadedLibraries(const int numThreads)
328 {
329  utl::InitializeMKL((numThreads>1 || numThreads<=0)?1:-1);
330  utl::InitializeOpenMP((numThreads>1 || numThreads<=0)?1:-1);
331 }
332 
333 }
336 #endif
NDArray is a N-Dimensional array class (row-major, c version)
Definition: utlFunctors.h:131
Created "11-12-2015.
NDArray<T,1> is a vector class which uses blas mkl.
Definition: utlVector.h:36
void PointsContainerToUtlMatrix(const PointsContainer &points, utl::NDArray< VnlValueType, 2 > &matrix)
Definition: utl.h:247
#define UTL_MAX_THREADS
Definition: utl.h:25
void ReadVectorImage(const std::string &filename, SmartPointer< VectorImage< PixelType, 3 > > &image, const std::string &printInfo="Reading Image:")
Definition: utl.h:66
SizeType Columns() const
Definition: utlMatrix.h:137
#define utlException(cond, expout)
Definition: utlCoreMacro.h:548
bool IsFileExist(const std::string &file)
Definition: utlCore.h:529
void UtlMatrixToPointsContainer(const NDArray< VnlValueType, 2 > &matrix, PointsContainer &points)
Definition: utl.h:275
SizeType Rows() const
Definition: utlMatrix.h:136
NDArray<T,2> is a row-major matrix.
Definition: utlMatrix.h:37
bool ReSize(const ShapeType &shape)
Definition: utlNDArray.h:697
convert Image<TInputPixelType, VImageDimension+1> to VectorImage<TOutputPixelType, VImageDimension>
itk::VariableLengthVector< T > UtlVectorToVariableLengthVector(const NDArray< T, 1 > &vec)
Definition: utl.h:114
NDArray< T, 2 > CartesianToSpherical(const NDArray< T, 2 > &in)
std::shared_ptr< NDArray< T, 2 > > ComputeSHMatrix(const unsigned int rank, const NDArray< T, 2 > &grad, const int mode)
Definition: utl.h:171
const T & min(const T &a, const T &b)
Return the minimum between a and b.
Definition: utlCore.h:257
#define utlGlobalException(cond, expout)
Definition: utlCoreMacro.h:372
NDArray< T, 1 > VariableLengthVectorToUtlVector(const itk::VariableLengthVector< T > &vec)
Definition: utl.h:124
static int InitializeOpenMP(const int numThreads)
Definition: utl.h:312
Definition: utl.h:90
Generate complex and real Spherical Harmonic Basis.
static void InitializeThreadedLibraries(const int numThreads)
Definition: utl.h:327
bool IsVectorImage(const std::string &filename)
Definition: utlITK.h:253
SizeType Size() const
Definition: utlNDArray.h:321
itk::VectorImage< ScalarType, 3 > VectorImageType
Definition: 4DImageMath.cxx:30
static int InitializeMKL(const int numThreads)
Definition: utl.h:297
help functions for VNL