DMRITool  v0.1.1-139-g860d86b4
Diffusion MRI Tool
itkDiffusionTensor.h
Go to the documentation of this file.
1 
19 #ifndef __itkDiffusionTensor_h
20 #define __itkDiffusionTensor_h
21 
22 #include <itkDiffusionTensor3D.h>
23 #include "utlNDArray.h"
24 
25 namespace itk
26 {
27 
45 template< class TPrecision >
46 class DiffusionTensor : public DiffusionTensor3D<TPrecision>
47 {
48 
49 public:
52  typedef DiffusionTensor3D<TPrecision> Superclass;
53 
54  const static unsigned int NDimension = 3;
55  itkStaticConstMacro (NDegreesOfFreedom, unsigned int, NDimension*(NDimension+1)/2);
56 
57  typedef typename Superclass::ValueType ValueType;
58  typedef typename Superclass::ComponentType ComponentType;
59 
60  typedef typename Superclass::EigenValuesArrayType EigenValuesArrayType;
61  typedef typename Superclass::EigenVectorsMatrixType EigenVectorsMatrixType;
62 
63  typedef typename Superclass::AccumulateValueType AccumulateValueType;
64  typedef typename Superclass::RealValueType RealValueType;
65 
66  typedef typename Superclass::SymmetricEigenAnalysisType SymmetricEigenAnalysisType;
67 
68  typedef Vector<TPrecision, NDimension> VectorType;
69 
71  inline DiffusionTensor(): Superclass(){};
72 
74  inline DiffusionTensor (const Self& r): Superclass (r){}
75 
76  template< class TTensorValueType >
77  inline DiffusionTensor(const DiffusionTensor< TTensorValueType>& v): Superclass(v) {}
78 
79  inline bool operator==(const Self & mat) const
80  {
81  for ( int i = 0; i < NDegreesOfFreedom; ++i )
82  {
83  if ((*this)[i]!=mat[i])
84  return false;
85  }
86  return true;
87  }
88  inline bool operator!=(const Self& mat) const
89  {
90  return !operator==(mat);
91  }
92 
93  inline bool IsSame(const Self & mat, const double eps=1e-8) const
94  {
95  for ( int i = 0; i < NDegreesOfFreedom; ++i )
96  {
97  if ( std::fabs((*this)[i]-mat[i]) > eps )
98  return false;
99  }
100  return true;
101  }
102 
103  template< class TMatrixType >
104  inline Self& operator=(const TMatrixType & mat)
105  {
106  for ( unsigned int i = 0; i < NDimension; i++ )
107  for(unsigned int j=i; j<NDimension; j++)
108  (*this)[NDimension*i-(i+1)*i/2+j] = mat(i,j);
109  return *this;
110  }
111 
112  inline Self operator-(const Self & mat) const
113  {
114  Self result;
115  for ( unsigned int i = 0; i < NDimension; i++ )
116  result[i] = ( *this )[i] - mat[i];
117  return result;
118  }
119  inline Self operator+(const Self & mat) const
120  {
121  Self result;
122  for ( unsigned int i = 0; i < NDimension; i++ )
123  result[i] = ( *this )[i] + mat[i];
124  return result;
125  }
126 
127  inline Self operator*(const Self& A) const
128  {
129  Self res;
130  res.SetVnlMatrix ( this->GetVnlMatrix() * A.GetVnlMatrix() );
131  return res;
132  }
133  inline vnl_matrix<TPrecision> operator*(const vnl_matrix<TPrecision>& A) const
134  {
135  return this->GetVnlMatrix() * A;
136  }
137 
139  inline VectorType operator*(const VectorType& v) const
140  {
141  VectorType result;
142  for( unsigned int i=0; i<NDimension; i++ )
143  {
144  result[i]=0;
145  for( unsigned int j=0; j<NDimension; j++)
146  result[i] += (*this)(i,j)*v[j];
147  }
148  return result;
149  }
150 
153  ValueType & operator()(unsigned int row, unsigned int col);
154 
155  const ValueType & operator()(unsigned int row, unsigned int col) const;
156 
157  void Flip(const int flipx, const int flipy, const int flipz)
158  {
159  double sign_xy = flipx^flipy ? -1 : 1;
160  double sign_xz = flipx^flipz ? -1 : 1;
161  double sign_yz = flipy^flipz ? -1 : 1;
162  (*this)[1] = sign_xy*(*this)[1];
163  (*this)[2] = sign_xz*(*this)[2];
164  (*this)[4] = sign_yz*(*this)[4];
165  }
166 
173  void GetEigenValuesVectors(vnl_diag_matrix<TPrecision> & eigenValues, vnl_matrix<TPrecision> & eigenVectors) const;
174 
185  template<class TArrayType, class TMatrixType >
186  void GetEigenValuesVectorsAnalytic(TArrayType& eigenValues, TMatrixType& eigenVectors) const;
187 
188  template <class TArrayType >
189  void SetEigenValues(const TArrayType& array);
190  template <class TMatrixType >
191  void SetEigenVectors(const TMatrixType& vectors);
192 
194  inline void SetVnlMatrix( const vnl_matrix<TPrecision> & mat);
195 
197  inline vnl_matrix<TPrecision> GetVnlMatrix () const;
198 
199  inline double GetDeterminant() const;
200  inline double GetNorm() const;
201 
203  inline Self GetRotate (const typename Self::MatrixType& mat) const;
204  inline Self& Rotate (const typename Self::MatrixType& mat);
205 
207  inline vnl_matrix<TPrecision> GetRotate (const vnl_matrix<TPrecision> & vec) const;
208 
209  double GetQuadraticForm( const double x, const double y, const double z) const;
210 
214  template <class TVectorType >
215  void GetSphericalSamples( TVectorType& vec, const utl::NDArray<TPrecision,2>& gradients) const;
216 
220  template <class TVectorType >
221  void GetDWISamples( TVectorType& vec, const utl::NDArray<TPrecision,2>& gradients, const std::vector<TPrecision>& bValues) const;
222 
226  template <class TVectorType >
227  void GetEAPSamples( TVectorType& vec, const utl::NDArray<TPrecision,2>& gradients, const std::vector<TPrecision>& rValues, const TPrecision& tau) const;
228 
233  template <class TVectorType >
234  void GetODFSamples( TVectorType& vec, const utl::NDArray<TPrecision,2>& gradients, const int& odfOrder=2, const bool& isNormalize=false) const;
235 
237  RealValueType GetReturnToOrigin (const TPrecision tau) const;
239  RealValueType GetMeanSquaredDisplacement (const TPrecision tau) const;
241  RealValueType GetGA() const;
243  RealValueType GetFA() const;
245  RealValueType GetMODE() const;
247  RealValueType GetRA() const;
249  RealValueType GetADC() const;
250  RealValueType GetMD() const
251  { return GetADC(); }
252 
253  bool IsFinite() const;
254  bool HasNans() const;
255  bool IsZero(const double eps=1e-10) const;
256 
257  bool IsPositive() const;
258  bool IsDiagonal(const double eps=1e-10) const;
259 
260  void Positivize() const;
261 
263  inline Self Log() const;
264 
266  inline Self Exp () const;
267 
269  inline Self Pow (const double& n) const;
270  inline Self Inv () const;
271  inline Self Sqrt () const;
272  inline Self InvSqrt () const;
273 
274  inline TPrecision EuclideanDistance(const DiffusionTensor<TPrecision>& X) const;
275  inline TPrecision KLDistance(const DiffusionTensor<TPrecision>& X) const;
276  inline TPrecision GeodesicDistance(const DiffusionTensor<TPrecision>& X) const;
277  inline TPrecision LogEucDistance(const DiffusionTensor<TPrecision>& X) const;
278 
279  void Print(std::ostream& os, Indent indent=0) const
280  {
281  os << indent << "[";
282  for ( int i = 0; i < NDimension; i += 1 )
283  {
284  for ( int j = 0; j < NDimension -1; j += 1 )
285  os<< (*this)(i,j) << " ";
286  os << (*this)(i,NDimension-1) << "; ";
287  }
288  os <<"];" << std::endl;
289  }
290 
291 };
292 
293 
294 }
295 
296 #define ITK_TEMPLATE_DiffusionTensor(_, EXPORT, TypeX, TypeY) \
297  namespace itk \
298  { \
299  _( 2 ( class EXPORT DiffusionTensor< ITK_TEMPLATE_2 TypeX > ) ) \
300  namespace Templates \
301  { \
302  typedef DiffusionTensor< ITK_TEMPLATE_2 TypeX > \
303  DiffusionTensor##TypeY; \
304  } \
305  }
306 
307 #if ITK_TEMPLATE_EXPLICIT
308 #include "Templates/itkDiffusionTensor+-.h"
309 #endif
310 
311 #if !defined(ITK_MANUAL_INSTANTIATION) && !defined(__itkDiffusionTensor_hxx)
312 #include "itkDiffusionTensor.hxx"
313 #endif
314 
315 
316 #endif
317 
318 
Superclass::RealValueType RealValueType
NDArray is a N-Dimensional array class (row-major, c version)
Definition: utlFunctors.h:131
void GetEAPSamples(TVectorType &vec, const utl::NDArray< TPrecision, 2 > &gradients, const std::vector< TPrecision > &rValues, const TPrecision &tau) const
RealValueType GetMODE() const
TPrecision GeodesicDistance(const DiffusionTensor< TPrecision > &X) const
TPrecision LogEucDistance(const DiffusionTensor< TPrecision > &X) const
TPrecision EuclideanDistance(const DiffusionTensor< TPrecision > &X) const
static const unsigned int NDegreesOfFreedom
RealValueType GetRA() const
void GetDWISamples(TVectorType &vec, const utl::NDArray< TPrecision, 2 > &gradients, const std::vector< TPrecision > &bValues) const
RealValueType GetReturnToOrigin(const TPrecision tau) const
Self Pow(const double &n) const
void GetEigenValuesVectors(vnl_diag_matrix< TPrecision > &eigenValues, vnl_matrix< TPrecision > &eigenVectors) const
Return an array containing EigenValues in ascending order, and a matrix containing the corresponding ...
void GetEigenValuesVectorsAnalytic(TArrayType &eigenValues, TMatrixType &eigenVectors) const
analytic way to calculate eigenValues (in ascending order) and eigenVectors. In mathematica, Eigenvectors[( { {a, b, c}, {b, d, e}, {c, e, f} } )]
VectorType operator*(const VectorType &v) const
bool IsSame(const Self &mat, const double eps=1e-8) const
double GetDeterminant() const
RealValueType GetFA() const
Self & Rotate(const typename Self::MatrixType &mat)
ValueType & operator()(unsigned int row, unsigned int col)
bool operator==(const Self &mat) const
Self operator*(const Self &A) const
static const unsigned int NDimension
RealValueType GetADC() const
void Flip(const int flipx, const int flipy, const int flipz)
Self & operator=(const TMatrixType &mat)
Superclass::AccumulateValueType AccumulateValueType
Superclass::EigenValuesArrayType EigenValuesArrayType
DiffusionTensor(const Self &r)
void SetEigenValues(const TArrayType &array)
DiffusionTensor3D< TPrecision > Superclass
RealValueType GetMD() const
vnl_matrix< TPrecision > GetVnlMatrix() const
Superclass::EigenVectorsMatrixType EigenVectorsMatrixType
bool operator!=(const Self &mat) const
void SetVnlMatrix(const vnl_matrix< TPrecision > &mat)
bool IsZero(const double eps=1e-10) const
bool IsDiagonal(const double eps=1e-10) const
vnl_matrix< TPrecision > operator*(const vnl_matrix< TPrecision > &A) const
void Print(std::ostream &os, Indent indent=0) const
RealValueType GetGA() const
DiffusionTensor(const DiffusionTensor< TTensorValueType > &v)
Self operator+(const Self &mat) const
void GetSphericalSamples(TVectorType &vec, const utl::NDArray< TPrecision, 2 > &gradients) const
RealValueType GetMeanSquaredDisplacement(const TPrecision tau) const
void GetODFSamples(TVectorType &vec, const utl::NDArray< TPrecision, 2 > &gradients, const int &odfOrder=2, const bool &isNormalize=false) const
Self GetRotate(const typename Self::MatrixType &mat) const
TPrecision KLDistance(const DiffusionTensor< TPrecision > &X) const
Superclass::ComponentType ComponentType
double GetQuadraticForm(const double x, const double y, const double z) const
tensor with some useful functions
Superclass::ValueType ValueType
Superclass::SymmetricEigenAnalysisType SymmetricEigenAnalysisType
Vector< TPrecision, NDimension > VectorType
Self operator-(const Self &mat) const
void SetEigenVectors(const TMatrixType &vectors)