DMRITool  v0.1.1-139-g860d86b4
Diffusion MRI Tool
itkScalarMapFromSPFImageFilter.hxx
Go to the documentation of this file.
1 
11 #ifndef __itkScalarMapFromSPFImageFilter_hxx
12 #define __itkScalarMapFromSPFImageFilter_hxx
13 
14 #include <itkProgressReporter.h>
16 #include "utl.h"
19 
20 namespace itk
21 {
22 
23 template< class TInputImage, class TOutputImage >
24 void
27 {
28  Superclass::GenerateOutputInformation();
29  typename TInputImage::ConstPointer inputPtr = this->GetInput();
30  typename TOutputImage::Pointer outputPtr = this->GetOutput();
31  itk::CopyImageInformation(inputPtr, outputPtr);
32 }
33 
34 template< class TInputImage, class TOutputImage >
35 typename LightObject::Pointer
38 {
39  typename LightObject::Pointer loPtr = Superclass::InternalClone();
40 
41  typename Self::Pointer rval = dynamic_cast<Self *>(loPtr.GetPointer());
42  if(rval.IsNull())
43  {
44  itkExceptionMacro(<< "downcast to type " << this->GetNameOfClass()<< " failed.");
45  }
46  rval->m_SumWeight = m_SumWeight;
47  rval->m_MapType = m_MapType;
48  return loPtr;
49 }
50 
51 template< class TInputImage, class TOutputImage >
52 void
55 {
56  // utlShowPosition(true);
57  m_SumWeight.ReSize(this->m_RadialRank+1);
58  if (m_MapType==RTO)
59  {
60  for ( int i = 0; i <= this->m_RadialRank ; ++i )
61  {
62  double sign = utl::IsEven(i) ? 1.0 : -1.0;
63  m_SumWeight[i] = sign * std::sqrt( utl::GammaHalfInteger(i+1.5) / utl::Factorial(i) );
64  }
65  m_SumWeight %= 4*std::sqrt(M_PI);
66  }
67  else if (m_MapType==MSD)
68  {
69  for ( int n = 0; n <= this->m_RadialRank; ++n )
70  {
71  if (n==0)
72  m_SumWeight[n] = utl::Lagurre(n,0.5,0.0);
73  else
74  m_SumWeight[n] = 2*utl::Lagurre(n-1,1.5,0.0) + utl::Lagurre(n,0.5,0.0);
75  }
76  m_SumWeight.Scale(3.0/(8.0*M_PI*M_PI*std::sqrt(M_PI)));
77  }
78  else if (m_MapType==PFA)
79  {
80 
81  }
82 
83  Superclass::BeforeThreadedGenerateData();
84 }
85 
86 template <class TInputImage, class TOutputImage>
87 void
89 ::ThreadedGenerateData( const typename TOutputImage::RegionType &outputRegionForThread, ThreadIdType threadId)
90 {
91  // utlShowPosition(true);
92  typename TInputImage::ConstPointer inputPtr = this->GetInput();
93  typename TOutputImage::Pointer outputPtr = this->GetOutput();
94 
95  // Define the iterators
96  ImageRegionConstIteratorWithIndex<TInputImage> inputIt(inputPtr, outputRegionForThread);
97  ImageRegionIteratorWithIndex<TOutputImage> outputIt(outputPtr, outputRegionForThread);
98  ImageRegionIteratorWithIndex<MaskImageType> maskIt;
99  if (this->IsMaskUsed())
100  maskIt = ImageRegionIteratorWithIndex<MaskImageType>(this->m_MaskImage, outputRegionForThread);
101 
102  ProgressReporter progress(this, threadId, outputRegionForThread.GetNumberOfPixels());
103  typename TInputImage::PixelType inputPixel;
104  typename TInputImage::IndexType inputIndex;
105  typename TOutputImage::PixelType outputPixel;
106  ScalarImagePointer scaleImage = this->GetScaleImage();
107 
108  unsigned int inputDim = inputPtr->GetNumberOfComponentsPerPixel();
109  inputPixel.SetSize(inputDim);
110 
111  inputIt.GoToBegin();
112  outputIt.GoToBegin();
113 
114  double scale = this->m_BasisScale;
115  int dimSH = utl::RankToDimSH(this->m_SHRank);
116 
117  while( !inputIt.IsAtEnd() )
118  {
119  if (!this->IsMaskUsed() || (this->IsMaskUsed() && maskIt.Get()>0))
120  {
121  inputPixel = inputIt.Get();
122  if (inputPixel.GetSquaredNorm()>1e-8)
123  {
124  inputIndex = inputIt.GetIndex();
125  if (this->GetDebug())
126  std::cout << "index = " << inputIndex << std::endl << std::flush;
127 
128  if (!IsImageEmpty(scaleImage))
129  scale = scaleImage->GetPixel(inputIndex);
130 
131  if (m_MapType==RTO)
132  {
133  outputPixel=0;
134  for ( int i = 0; i <= this->m_RadialRank; ++i )
135  outputPixel += inputPixel[i*dimSH]*m_SumWeight[i];
136  outputPixel *= std::pow(scale, 0.75);
137  }
138  else if (m_MapType==MSD)
139  {
140  VectorType scaleWeight(this->m_RadialRank+1);
141  for ( int n = 0; n <= this->m_RadialRank; n += 1 )
142  {
143  scaleWeight[n] = 2.0 / std::pow(scale,(double)1.5) * utl::Factorial(n) / utl::Gamma(n+1.5);
144  scaleWeight[n] = std::sqrt(scaleWeight[n])/scale;
145  }
146  outputPixel=0;
147  for ( int i = 0; i <= this->m_RadialRank; ++i )
148  outputPixel += inputPixel[i*dimSH]*m_SumWeight[i]*scaleWeight[i];
149  }
150  else if (m_MapType==PFA)
151  {
152  double sum = 0;
153  for ( int i = 0; i <= this->m_RadialRank; ++i )
154  sum += inputPixel[i*dimSH]*inputPixel[i*dimSH];
155  outputPixel = std::sqrt(1- sum/inputPixel.GetSquaredNorm() );
156  }
157 
158  }
159  else
160  outputPixel=0;
161  }
162  else
163  outputPixel=0;
164 
165  outputIt.Set(outputPixel);
166  progress.CompletedPixel();
167  if (this->IsMaskUsed())
168  ++maskIt;
169  ++inputIt;
170  ++outputIt;
171  progress.CompletedPixel(); // potential exception thrown here
172  }
173 }
174 
175 template< class TInputImage, class TOutputImage >
176 void
178 ::PrintSelf(std::ostream &os, Indent indent) const
179 {
180  Superclass::PrintSelf(os, indent);
181  utl::PrintUtlVector(m_SumWeight, "m_SumWeight", " ", os<<indent);
182 }
183 
184 }
185 
186 #endif
187 
188 
189 
NDArray is a N-Dimensional array class (row-major, c version)
Definition: utlFunctors.h:131
helper functions specifically used in dmritool
bool IsImageEmpty(const SmartPointer< ImageType > &image)
Definition: utlITK.h:435
bool IsEven(const int value)
Definition: utlCore.h:819
unsigned long Factorial(const int n)
Definition: utlMath.h:190
void CopyImageInformation(const SmartPointer< ImageWithInfoType > &imageFrom, SmartPointer< ImageType > &imageTo)
Definition: utlITK.h:552
#define M_PI
Definition: utlCoreMacro.h:57
void PrintSelf(std::ostream &os, Indent indent) const ITK_OVERRIDE
T Lagurre(const int n, const double a, const T x)
LightObject::Pointer InternalClone() const ITK_OVERRIDE
int RankToDimSH(const int shRank)
Definition: utlDMRI.h:193
double GammaHalfInteger(const double x)
Definition: utlMath.h:887
void ThreadedGenerateData(const typename TOutputImage::RegionType &outputRegionForThread, ThreadIdType threadId) ITK_OVERRIDE
void PrintUtlVector(const NDArray< T, 1 > &vec, const std::string &str="", const char *separate=" ", std::ostream &os=std::cout, bool showStats=true)
int sign(const T &x)
Return the sign of x.
Definition: utlCore.h:285
Compute some features (DWI/EAP profile, ODFs, scalar indices) from SPF coefficients.
double Gamma(const double x)
gamma function.