DMRITool  v0.1.1-139-g860d86b4
Diffusion MRI Tool
itkSHCoefficientsFit.h
Go to the documentation of this file.
1 
12 #ifndef __itkSHCoefficientsFit_h
13 #define __itkSHCoefficientsFit_h
14 
15 #include "utlITKMacro.h"
16 #include "utl.h"
17 
18 
19 namespace itk
20 {
21 
22 namespace Functor
23 {
24 
33 template < class T = double >
34 class ITK_EXPORT SHCoefficientsFit
35  : public utl::Functor::VectorFunctorBase<utl::Vector<T> >
36 {
37 public:
41  typedef SmartPointer<Self> Pointer;
42  typedef SmartPointer<const Self> ConstPointer;
43 
44 
45  typedef T PreciseType;
46 
49  typedef utl_shared_ptr<MatrixType> MatrixPointer;
50 
51  SHCoefficientsFit() : Superclass(),
52  m_Orientations(new MatrixType()),
53  m_BasisSHMatrix(new MatrixType()),
54  m_BasisSHMatrixPInv(new MatrixType())
55  {
56  }
57  virtual ~SHCoefficientsFit() {};
58 
59  bool operator==(const Self & other) const
60  { return false; }
61  bool operator!=(const Self & other) const
62  { return ! (*this==other); }
63 
64 
65  SHCoefficientsFit(const Self& other)
66  {
67  *this = other;
68  }
69 
70  Self& operator=(const Self& other)
71  {
72  Superclass::operator=(other);
73  if ( this != &other )
74  {
75  m_SHRank = other.m_SHRank;
76  m_Lambda = other.m_Lambda;
77  m_Power = other.m_Power;
78  m_Orientations = other.m_Orientations;
79  m_BasisSHMatrix = other.m_BasisSHMatrix;
80  m_BasisSHMatrixPInv = other.m_BasisSHMatrixPInv;
81  }
82  return *this;
83  }
84 
85  utlSetGetMacro(SHRank, int);
86  utlSetGetMacro(Lambda, double);
87  utlSetGetMacro(Power, double);
88 
89  utlSetGetMacro(Orientations, MatrixPointer);
90 
91  utlGetMacro(BasisSHMatrix, MatrixPointer);
92  utlGetMacro(BasisSHMatrixPInv, MatrixPointer);
93 
94  void VerifyInputParameters(const int inputVecSize=-1) const
95  {
97  Superclass::VerifyInputParameters();
98  utlGlobalException(m_SHRank<0, "need to set m_SHRank");
99  utlSAGlobalException(m_Orientations->Rows()==0).msg("need to set m_Orientations");
100 
101  if (inputVecSize>0)
102  {
103  utlSAGlobalException(m_Orientations->Rows()!=inputVecSize)
104  (m_Orientations->Rows())(inputVecSize).msg("need to set orientations correctly");
105  }
106  }
107 
108  void Initialize()
109  {
111  VerifyInputParameters();
112 
113  m_BasisSHMatrix = utl::ComputeSHMatrix(m_SHRank, *m_Orientations, CARTESIAN_TO_SPHERICAL);
114  int J = m_BasisSHMatrix->Cols();
115  int N = m_BasisSHMatrix->Rows();
116 
117  MatrixType shT = m_BasisSHMatrix->GetTranspose();
118 
119  if (m_Lambda>0)
120  {
121  MatrixType regMat(J, J, 0.0);
122  for ( int i = 0; i < J; ++i )
123  {
124  std::vector<int> lm = utl::GetIndexSHlm(i);
125  regMat(i,i) = m_Lambda*lm[0]*lm[0]*(lm[0]+1.)*(lm[0]+1.);
126  }
127  if (utl::IsLogDebug(this->m_LogLevel))
128  utl::PrintUtlMatrix(regMat, "regMat");
129  *m_BasisSHMatrixPInv = utl::InverseSymmericMatrix( utl::Eval<double,2>(shT * (*m_BasisSHMatrix) + regMat) )* shT;
130  }
131  else
132  *m_BasisSHMatrixPInv = utl::InverseSymmericMatrix( shT * (*m_BasisSHMatrix) )* shT;
133 
134  if (utl::IsLogDebug(this->m_LogLevel))
135  {
136  utl::PrintUtlMatrix(*m_BasisSHMatrix, "m_BasisSHMatrix");
137  utl::PrintUtlMatrix(*m_BasisSHMatrixPInv, "m_BasisSHMatrixPInv");
138  }
139  }
140 
141  VectorType operator()(const VectorType& sf) const
142  {
143  if (std::fabs(m_Power-1.0)>1e-8)
144  {
145  VectorType sf_power = utl::Pow(sf, m_Power);
146  return (*m_BasisSHMatrixPInv) * sf_power;
147  }
148  else
149  return (*m_BasisSHMatrixPInv) * sf;
150  }
151 
152  int GetOutputDimension(const int ) const
153  {
154  utlSAException(m_SHRank<0)(m_SHRank).msg("need to set m_SHRank");
155  return utl::RankToDimSH(m_SHRank);
156  }
157 
158  void Print(std::ostream & os=std::cout) const
159  {
160  Superclass::Print(os);
161  utlLogOSVar(os , m_Power, m_SHRank, m_Lambda);
162  utl::PrintUtlMatrix(*m_Orientations, "m_Orientations", " ", os);
163  utl::PrintUtlMatrix(*m_BasisSHMatrix, "m_BasisSHMatrix", " ", os);
164  utl::PrintUtlMatrix(*m_BasisSHMatrixPInv, "m_BasisSHMatrixPInv", " ", os);
165  }
166 
167 protected:
168 
169 
170 
172  int m_SHRank=-1;
173 
175  double m_Lambda=0;
176 
177  double m_Power=1.0;
178 
180  MatrixPointer m_Orientations;
181 
183  MatrixPointer m_BasisSHMatrix;
185  MatrixPointer m_BasisSHMatrixPInv;
186 
187 
188 private:
189 
190 };
191 
192 }
193 
194 }
195 
196 
197 #endif
NDArray is a N-Dimensional array class (row-major, c version)
Definition: utlFunctors.h:131
utl::NDArray< double, 2 > MatrixType
helper functions specifically used in dmritool
utl_shared_ptr< MatrixType > MatrixPointer
auto Pow(const TLeft &lhs, const TRight &rhs) -> decltype(utl::F< utl::Functor::Pow< Expr2ValueType< TLeft, TRight >> >(lhs, rhs))
Definition: utlFunctors.h:385
#define utlLogOSVar(os,...)
Definition: utlCoreMacro.h:285
void Print(Args...args)
Definition: utlCore11.h:87
void PrintUtlMatrix(const NDArray< T, 2 > &mat, const std::string &str="", const char *separate=" ", std::ostream &os=std::cout)
used for debug information. this->GetDebug()
Definition: utlCoreMacro.h:193
std::vector< int > GetIndexSHlm(const int j)
Definition: utlDMRI.h:211
#define utlSAGlobalException(expr)
Definition: utlCoreMacro.h:362
utl::NDArray< double, 1 > VectorType
void VerifyInputParameters(const int inputVecSize=-1) const
#define utlGetMacro(name, type)
Definition: utlCoreMacro.h:582
fit SH coefficients from spherical function samples.
std::shared_ptr< NDArray< T, 2 > > ComputeSHMatrix(const unsigned int rank, const NDArray< T, 2 > &grad, const int mode)
Definition: utl.h:171
SmartPointer< const Self > ConstPointer
bool IsLogDebug(const int level=utl::LogLevel)
Definition: utlCoreMacro.h:213
#define utlSetGetMacro(name, type)
Definition: utlCoreMacro.h:589
#define utlGlobalException(cond, expout)
Definition: utlCoreMacro.h:372
Self & operator=(const Self &other)
int GetOutputDimension(const int) const
bool operator!=(const Self &other) const
utl::Functor::VectorFunctorBase< utl::Vector< T > > Superclass
#define utlSAException(expr)
Definition: utlCoreMacro.h:543
int RankToDimSH(const int shRank)
Definition: utlDMRI.h:193
#define utlVLogPosition(level)
Definition: utlCoreMacro.h:298
NDArray< T, 2 > InverseSymmericMatrix(const NDArray< T, 2 > &mat, const double eps=1e-10)
bool operator==(const Self &other) const
void Print(std::ostream &os=std::cout) const
VectorType operator()(const VectorType &sf) const