DMRITool  v0.1.1-139-g860d86b4
Diffusion MRI Tool
itkDWIGeneratorBase.hxx
Go to the documentation of this file.
1 
19 #ifndef __itkDWIGeneratorBase_hxx
20 #define __itkDWIGeneratorBase_hxx
21 
22 #include "itkDWIGeneratorBase.h"
23 #include "utl.h"
24 
25 namespace itk
26 {
27 
28 template <class TOutputImage, class TScalarImage>
31 {
32  this->SetNumberOfRequiredOutputs(1); // at least 1 output
33  this->SetNthOutput( 0, ( TOutputImage::New() ).GetPointer() ); // dwi
34  this->SetNthOutput( 1, ( TScalarImage::New() ).GetPointer() ); // b0
35  this->SetNthOutput( 2, ( TOutputImage::New() ).GetPointer() ); // ODF
36  this->SetNthOutput( 3, ( TOutputImage::New() ).GetPointer() ); // EAP
37  this->SetNthOutput( 4, ( TOutputImage::New() ).GetPointer() ); // peak
38  this->SetNthOutput( 5, ( TScalarImage::New() ).GetPointer() ); // RTO
39  this->SetNthOutput( 6, ( TScalarImage::New() ).GetPointer() ); // MSD
40 
41  m_SamplingSchemeQSpace = SamplingSchemeQSpaceType::New();
42  m_SamplingSchemeRSpace = SamplingSchemeQSpaceType::New();
43 
44  m_CylinderModel = CylinderModelType::New();
45 
46  m_NoiseSigma = -1; // Unset
47  m_SNR = -1; // Unset
48  m_ODFOrder = 2;
49  m_ModelType = SYMMETRICAL_TENSOR_IN_CARTESIAN_COORDS;
50  m_B0Scale = 1.0; // if it is not set, 1 is used
51  m_MaxNumberOfPeaks = -1;
52  m_PeakType = NXYZ;
53 
54  m_OutputOrigin.Fill(0);
55  m_OutputDirection.SetIdentity();
56  for ( int i = 0; i < OutputImageType::ImageDimension; i += 1 )
57  {
58  m_OutputSize[i]=1.0;
59  m_OutputSpacing[i]=1.0;
60  }
61 
62  m_IsOutputDWI=false;
63  m_IsOutputEAP=false;
64  m_IsOutputODF=false;
65  m_IsOutputRTO=false;
66  m_IsOutputMSD=false;
67 }
68 
69 template <class TOutputImage, class TScalarImage>
72 {
73 }
74 
75 template <class TOutputImage, class TScalarImage>
76 typename LightObject::Pointer
79 {
80  typename LightObject::Pointer loPtr = Superclass::InternalClone();
81 
82  typename Self::Pointer rval = dynamic_cast<Self *>(loPtr.GetPointer());
83  if(rval.IsNull())
84  {
85  itkExceptionMacro(<< "downcast to type " << this->GetNameOfClass()<< " failed.");
86  }
87 
88  rval->m_NoiseSigma = m_NoiseSigma;
89  rval->m_SNR = m_SNR;
90 
91  rval->m_ODFOrder = m_ODFOrder;
92 
93  rval->m_ModelType = m_ModelType;
94  rval->m_B0Scale = m_B0Scale;
95 
96  rval->m_MaxNumberOfPeaks = m_MaxNumberOfPeaks;
97  rval->m_PeakType = m_PeakType;
98 
99  rval->m_CylinderModel = m_CylinderModel;
100 
101  rval->m_OutputSize = m_OutputSize;
102  rval->m_OutputSpacing = m_OutputSpacing;
103  rval->m_OutputOrigin = m_OutputOrigin;
104  rval->m_OutputDirection = m_OutputDirection;
105 
106  rval->m_SamplingSchemeQSpace = m_SamplingSchemeQSpace;
107  rval->m_SamplingSchemeRSpace = m_SamplingSchemeRSpace;
108 
109  rval->m_IsOutputDWI=m_IsOutputDWI;
110  rval->m_IsOutputEAP=m_IsOutputEAP;
111  rval->m_IsOutputODF=m_IsOutputODF;
112  rval->m_IsOutputRTO=m_IsOutputRTO;
113  rval->m_IsOutputMSD=m_IsOutputMSD;
114 
115  return loPtr;
116 }
117 
118 template <class TOutputImage, class TScalarImage>
119 void
122 {
123  utlShowPosition(this->GetDebug());
124  utlGlobalException(!m_IsOutputDWI && !m_IsOutputODF && !m_IsOutputEAP && !m_IsOutputRTO && !m_IsOutputMSD && m_MaxNumberOfPeaks<=0, "no output!");
125  utlGlobalException(m_IsOutputDWI && this->GetNumberOfQSpaceSamples()==0, "need to set sampling schemes in q-space for DWI");
126  utlGlobalException((m_IsOutputODF || m_IsOutputEAP) && this->GetNumberOfRSpaceSamples()==0, "need to set sampling schemes in r-space for EAP or ODF");
127 
128  MatrixPointer qSpaceOrientationMatrix = m_SamplingSchemeQSpace->GetOrientationsCartesian();
129  STDVectorPointer bVector = m_SamplingSchemeQSpace->GetBVector();
130  MatrixPointer rSpaceOrientationMatrix = m_SamplingSchemeRSpace->GetOrientationsCartesian();
131  STDVectorPointer rVector = m_SamplingSchemeRSpace->GetRadiusVector();
132  utlGlobalException(m_IsOutputDWI && qSpaceOrientationMatrix->Rows()!=bVector->size(), "Need to set OrientationsCartesian and BVector. qSpaceOrientationMatrix->Rows()=" << qSpaceOrientationMatrix->Rows() << ", bVector->size()=" << bVector->size());
133  utlGlobalException( (m_IsOutputODF || m_IsOutputEAP) && rSpaceOrientationMatrix->Rows()!=rVector->size(), "Need to set rSpaceOrientationMatrix and rVector");
134  utlGlobalException(m_SNR>0 && m_NoiseSigma>0, "Only one of m_SNR and m_NoiseSigma is allowed.");
135 
136  double tau = this->m_SamplingSchemeQSpace->GetTau();
137  utlGlobalException(tau!=this->m_SamplingSchemeRSpace->GetTau(), "need to use the same tau in m_SamplingSchemeQSpace and m_SamplingSchemeRSpace");
138 }
139 
140 
141 
142 template <class TOutputImage, class TScalarImage>
143 void
146 {
147  utlShowPosition(this->GetDebug());
148 
149  bool isOutputPeak = this->m_MaxNumberOfPeaks>0;
150 
151  OutputImagePointer outputDWI=this->GetDWIImage(), outputODF=this->GetODFImage(), outputEAP=this->GetEAPImage(), outputPeak=this->GetPeakImage();
152  ScalarImagePointer b0Image = this->GetB0Image();
153  ScalarImagePointer rtoImage = this->GetRTOImage();
154  ScalarImagePointer msdImage = this->GetMSDImage();
155  OutputImageIndexType outputStartIndex;
156  OutputImageRegionType outputRegion;
157  outputStartIndex.Fill(0);
158  outputRegion.SetIndex( outputStartIndex );
159  outputRegion.SetSize( m_OutputSize );
160 
161  int numberOfComponentsPerPixel4DWI = this->GetNumberOfQSpaceSamples();
162  int numberOfComponentsPerPixel4EAP = this->GetNumberOfRSpaceSamples();
163  int numberOfComponentsPerPixel4ODF = this->GetNumberOfRSpaceSamples();
164  int numberOfComponentsPerPixel4Peak = isOutputPeak?PeakContainerHelper::GetDimension(m_PeakType,m_MaxNumberOfPeaks):-1;
165  OutputImagePixelType zeroPixel;
166 
167  if (m_IsOutputDWI)
168  {
169  // outputDWI = OutputImageType::New();
170  outputDWI->SetRegions( outputRegion );
171  outputDWI->SetSpacing( m_OutputSpacing );
172  outputDWI->SetOrigin( m_OutputOrigin );
173  outputDWI->SetDirection( m_OutputDirection );
174  outputDWI->SetNumberOfComponentsPerPixel( numberOfComponentsPerPixel4DWI );
175  zeroPixel.SetSize( numberOfComponentsPerPixel4DWI );
176  zeroPixel.Fill(0);
177  outputDWI->Allocate();
178  outputDWI->FillBuffer( zeroPixel );
179 
180  // b0Image = ScalarImageType::New();
181  // b0Image = this->GetB0Image();
182  b0Image->SetRegions( outputRegion );
183  b0Image->SetSpacing( m_OutputSpacing );
184  b0Image->SetOrigin( m_OutputOrigin );
185  b0Image->SetDirection( m_OutputDirection );
186  b0Image->Allocate();
187  b0Image->FillBuffer( 0 );
188  }
189  if (m_IsOutputEAP)
190  {
191  // outputEAP = OutputImageType::New();
192  outputEAP->SetRegions( outputRegion );
193  outputEAP->SetSpacing( m_OutputSpacing );
194  outputEAP->SetOrigin( m_OutputOrigin );
195  outputEAP->SetDirection( m_OutputDirection );
196  outputEAP->SetNumberOfComponentsPerPixel( numberOfComponentsPerPixel4EAP );
197  zeroPixel.SetSize( numberOfComponentsPerPixel4EAP );
198  zeroPixel.Fill(0);
199  outputEAP->Allocate();
200  outputEAP->FillBuffer( zeroPixel );
201  }
202  if (m_IsOutputODF)
203  {
204  // outputODF = OutputImageType::New();
205  outputODF->SetRegions( outputRegion );
206  outputODF->SetSpacing( m_OutputSpacing );
207  outputODF->SetOrigin( m_OutputOrigin );
208  outputODF->SetDirection( m_OutputDirection );
209  outputODF->SetNumberOfComponentsPerPixel( numberOfComponentsPerPixel4ODF );
210  zeroPixel.SetSize( numberOfComponentsPerPixel4ODF );
211  zeroPixel.Fill(0);
212  outputODF->Allocate();
213  outputODF->FillBuffer( zeroPixel );
214  }
215  if (isOutputPeak)
216  {
217  // outputODF = OutputImageType::New();
218  outputPeak->SetRegions( outputRegion );
219  outputPeak->SetSpacing( m_OutputSpacing );
220  outputPeak->SetOrigin( m_OutputOrigin );
221  outputPeak->SetDirection( m_OutputDirection );
222  outputPeak->SetNumberOfComponentsPerPixel( numberOfComponentsPerPixel4Peak );
223  zeroPixel.SetSize( numberOfComponentsPerPixel4Peak );
224  zeroPixel.Fill(0);
225  outputPeak->Allocate();
226  outputPeak->FillBuffer( zeroPixel );
227  }
228  if (m_IsOutputRTO)
229  {
230  rtoImage->SetRegions( outputRegion );
231  rtoImage->SetSpacing( m_OutputSpacing );
232  rtoImage->SetOrigin( m_OutputOrigin );
233  rtoImage->SetDirection( m_OutputDirection );
234  rtoImage->Allocate();
235  rtoImage->FillBuffer( 0 );
236  }
237  if (m_IsOutputMSD)
238  {
239  msdImage->SetRegions( outputRegion );
240  msdImage->SetSpacing( m_OutputSpacing );
241  msdImage->SetOrigin( m_OutputOrigin );
242  msdImage->SetDirection( m_OutputDirection );
243  msdImage->Allocate();
244  msdImage->FillBuffer( 0 );
245  }
246 }
247 
248 template <class TOutputImage, class TScalarImage>
249 void
251 ::PrintSelf(std::ostream& os, Indent indent) const
252 {
253  Superclass::PrintSelf(os, indent);
254 
255  os << indent << "Gneratortype: " << m_ModelType << std::endl;
256  os << indent << "ODFOrder: " << m_ODFOrder << std::endl;
257  os << indent << "NoiseSigma: " << m_NoiseSigma << std::endl;
258  os << indent << "SNR: " << m_SNR << std::endl;
259  if (m_MaxNumberOfPeaks>0)
260  PrintVar2(true, m_MaxNumberOfPeaks, PeakContainerHelper::GetString(m_PeakType), os<<indent);
261  PrintVar1(true, m_OutputSize, os<<indent);
262  os << indent << "m_SamplingSchemeQSpace: " << m_SamplingSchemeQSpace << std::endl;
263  os << indent << "m_SamplingSchemeRSpace: " << m_SamplingSchemeRSpace << std::endl;
264 }
265 
266 }
267 
268 
269 #endif
270 
271 
OutputImageType::RegionType OutputImageRegionType
#define PrintVar1(cond, var, os)
Definition: utlCoreMacro.h:447
helper functions specifically used in dmritool
static int GetDimension(const PeakType peakType, const int numberOfPeaks)
void PrintSelf(std::ostream &os, Indent indent) const ITK_OVERRIDE
virtual void AllocateOutputs() ITK_OVERRIDE
utl_shared_ptr< STDVectorType > STDVectorPointer
static std::string GetString(const PeakType peakType)
#define utlGlobalException(cond, expout)
Definition: utlCoreMacro.h:372
utl_shared_ptr< MatrixType > MatrixPointer
OutputImageType::PixelType OutputImagePixelType
ScalarImageType::Pointer ScalarImagePointer
OutputImageType::IndexType OutputImageIndexType
LightObject::Pointer InternalClone() const ITK_OVERRIDE
Generate DWI data based on provided parameter file.
SmartPointer< Self > Pointer
OutputImageType::Pointer OutputImagePointer
#define utlShowPosition(cond)
Definition: utlCoreMacro.h:554
#define PrintVar2(cond, var1, var2, os)
Definition: utlCoreMacro.h:454