DMRITool  v0.1.1-139-g860d86b4
Diffusion MRI Tool
itkFeaturesFromSPFImageFilter.hxx
Go to the documentation of this file.
1 
18 #ifndef __itkFeaturesFromSPFImageFilter_hxx
19 #define __itkFeaturesFromSPFImageFilter_hxx
20 
23 #include "utl.h"
24 
25 namespace itk
26 {
27 
28 template< class TInputImage, class TOutputImage >
31  m_SPFToFeatureTransform(new MatrixType()),
32  m_Orientations(new MatrixType())
33 {
34  m_Tau = ONE_OVER_4_PI_2;
35  m_MD0 = 0.7e-3;
36  m_SHRank=-1;
37  m_RadialRank=-1;
38  m_IsFourier=true;
39  // m_IsOriginalBasis=true;
40  m_ScaleImage=ScalarImageType::New();
41  m_BasisType=SPF;
42 
43  m_IsInQSpace=true;
44 
45  m_BasisScale=-1;
46  m_SPFIEstimator=SPFIFilterBaseType::New();
47 }
48 
49 template< class TInputImage, class TOutputImage >
50 double
52 ::ComputeScale(const bool setScale)
53 {
54  double scale=-1;
55  if (m_BasisType==SPF)
56  scale = 1.0 / (8*M_PI*M_PI*this->m_Tau*this->m_MD0); // 714.29 (700) scale for SPF basis, dual scale is 1/(4*pi^2*scale)
57  else if (m_BasisType==DSPF)
58  scale = 2*this->m_Tau*this->m_MD0; // 3.5462e-5 scale for SPF basis, dual scale is 1/(4*pi^2*scale)
59  if (setScale)
60  m_BasisScale = scale;
61  return scale;
62 }
63 
64 template< class TInputImage, class TOutputImage >
65 void
67 ::SetBasisScale(const double scale)
68 {
69  double scale_old = m_BasisScale;
70  if (scale>0)
71  m_BasisScale = scale;
72  else
73  this->ComputeScale(true);
74  itkDebugMacro("setting m_BasisScale to " << m_BasisScale);
75 
76  if (scale>0 && std::fabs((scale_old-m_BasisScale)/m_BasisScale)>1e-8)
77  {
78  this->Modified();
79  m_SPFToFeatureTransform=MatrixPointer(new MatrixType());
80  }
81 }
82 
83 
84 template< class TInputImage, class TOutputImage >
85 void
87 ::SetScaleImage(const ScalarImagePointer& scaleImage)
88 {
89  if( scaleImage != m_ScaleImage )
90  {
91  std::cout << "Use adaptive scale" << std::endl << std::flush;
92  m_ScaleImage = scaleImage;
93  m_SPFToFeatureTransform=MatrixPointer(new MatrixType());
94  this->Modified();
95  }
96 }
97 
98 template< class TInputImage, class TOutputImage >
99 void
102 {
103  utlGlobalException(m_SHRank<0 || m_RadialRank<0, "negative rank");
104  typename TInputImage::ConstPointer inputPtr = this->GetInput();
105  utlSAGlobalException(this->m_SPFIEstimator->RankToDim(false, this->m_RadialRank, this->m_SHRank)!=inputPtr->GetNumberOfComponentsPerPixel())
106  (this->m_RadialRank)(this->m_SHRank)(this->m_SPFIEstimator->RankToDim(false, this->m_RadialRank, this->m_SHRank))(inputPtr->GetNumberOfComponentsPerPixel()).msg("the size of input image is not consistent with the given shRank and radialRank");
107 }
108 
109 template< class TInputImage, class TOutputImage >
110 typename LightObject::Pointer
113 {
114  typename LightObject::Pointer loPtr = Superclass::InternalClone();
115 
116  typename Self::Pointer rval = dynamic_cast<Self *>(loPtr.GetPointer());
117  if(rval.IsNull())
118  {
119  itkExceptionMacro(<< "downcast to type " << this->GetNameOfClass()<< " failed.");
120  }
121  rval->m_BasisScale = m_BasisScale;
122  rval->m_MD0 = m_MD0;
123  rval->m_Tau = m_Tau;
124  rval->m_SHRank = m_SHRank;
125  rval->m_RadialRank = m_RadialRank;
126  rval->m_ScaleImage = m_ScaleImage;
127 
128  rval->m_IsFourier = m_IsFourier;
129  rval->m_IsInQSpace = m_IsInQSpace;
130  rval->m_Orientations = m_Orientations;
131 
132  // NOTE: shared_ptr is thread safe if different threads read the same data block.
133  // However if the data is modified in different threads, then it needs to copy the data block.
134  rval->m_SPFToFeatureTransform = m_SPFToFeatureTransform;
135  rval->m_BasisType = m_BasisType;
136 
137  rval->m_SPFIEstimator = m_SPFIEstimator->Clone();
138  rval->SetDebug(this->GetDebug());
139  return loPtr;
140 }
141 
142 template< class TInputImage, class TOutputImage >
143 void
146 {
147  std::cout << "Use " << this->GetNumberOfThreads() << " threads!" << std::endl << std::flush;
148  VerifyInputParameters();
149 }
150 
151 template< class TInputImage, class TOutputImage >
152 void
155 {
156  if (this->m_BasisType==SPF || this->m_BasisType==DSPF)
157  {
159  this->m_SPFIEstimator = SPFIFilterType::New();
160 
161  if (this->m_BasisType==SPF && !this->m_IsFourier)
162  this->m_SPFIEstimator->SetIsOriginalBasis(true);
163  else
164  this->m_SPFIEstimator->SetIsOriginalBasis(false);
165  }
166 }
167 
168 template< class TInputImage, class TOutputImage >
169 void
171 ::PrintSelf(std::ostream &os, Indent indent) const
172 {
173  Superclass::PrintSelf(os, indent);
174  os << indent
175  << "m_Tau = " << m_Tau
176  << ", m_BasisScalar = " << m_BasisScale
177  << ", m_MD0 = " << m_MD0
178  << ", m_SHRank = " << m_SHRank
179  << ", m_RadialRank = " << m_RadialRank
180  << ", m_IsInQSpace = " << m_IsInQSpace
181  << ", m_IsFourier = " << this->m_IsFourier
182  // << ", m_IsOriginalBasis = " << this->GetIsOriginalBasis()
183  // << ", m_ScaleImage = " << m_ScaleImage
184  << std::endl;
185  utl::PrintUtlMatrix(*m_Orientations, "m_Orientations", " ",os << indent);
186  utl::PrintUtlMatrix(*m_SPFToFeatureTransform, "m_SPFToFeatureTransform", " ",os << indent);
187  if (!itk::IsImageEmpty(m_ScaleImage))
188  std::cout << "m_ScaleImage = " << m_ScaleImage << std::endl << std::flush;
189  if (m_BasisType==SPF)
190  os << indent << "Use SPF basis" << std::endl << std::flush;
191  else if (m_BasisType==DSPF)
192  os << indent << "Use DSPF basis" << std::endl << std::flush;
193  os << indent << "m_SPFIEstimator = " << m_SPFIEstimator << std::endl << std::flush;
194 }
195 
196 }
197 
198 #endif
199 
void SetScaleImage(const ScalarImagePointer &scaleImage)
helper functions specifically used in dmritool
bool IsImageEmpty(const SmartPointer< ImageType > &image)
Definition: utlITK.h:435
void PrintUtlMatrix(const NDArray< T, 2 > &mat, const std::string &str="", const char *separate=" ", std::ostream &os=std::cout)
double ComputeScale(const bool setScale=true)
void PrintSelf(std::ostream &os, Indent indent) const ITK_OVERRIDE
virtual void SetBasisScale(const double scale)
#define utlSAGlobalException(expr)
Definition: utlCoreMacro.h:362
LightObject::Pointer InternalClone() const ITK_OVERRIDE
utl_shared_ptr< MatrixType > MatrixPointer
#define M_PI
Definition: utlCoreMacro.h:57
#define utlGlobalException(cond, expout)
Definition: utlCoreMacro.h:372
#define ONE_OVER_4_PI_2
Definition: utlCoreMacro.h:63
Compute some features (DWI/EAP profile, ODFs, scalar indices) from SPF coefficients.
virtual void VerifyInputParameters() const ITK_OVERRIDE