DMRITool  v0.1.1-139-g860d86b4
Diffusion MRI Tool
itkMultiVariableFunctorVectorImageFilter.hxx
Go to the documentation of this file.
1 
12 #ifndef itkMultiVariableFunctorVectorImageFilter_hxx
13 #define itkMultiVariableFunctorVectorImageFilter_hxx
14 
16 #include "utlCoreMacro.h"
18 #include "utlITK.h"
19 
20 namespace itk
21 {
22 
23 template< typename TInputImage, typename TOutputImage, typename TFunction, class TMaskImage >
26 {
27  this->SetNumberOfRequiredInputs(1);
28  this->SetNumberOfRequiredOutputs(1);
29 }
30 
31 template< typename TInputImage, typename TOutputImage, typename TFunction, class TMaskImage >
32 void
35 {
37  typename TInputImage::ConstPointer inputImage = this->GetInput();
38  typename TInputImage::Pointer inputTmp = TInputImage::New();
39  OutputImagePointer outImage = this->GetOutput();
40 
41  // copy information
42  itk::CopyImageInformation(inputImage, outImage);
43  utlException(this->m_VectorAxis<0, "need to set non-negative axis");
44 
45  int numberOfInputs = this->GetNumberOfInputs();
46 
47  std::vector<std::vector<int> > size4d;
48  std::vector<int> dimVec;
49  for ( int i = 0; i < numberOfInputs; ++i )
50  {
51  inputTmp->Graft(this->GetInput(i));
52  std::vector<int> tmpVec = itk::GetVectorImageFullSize(inputTmp);
53  size4d.push_back(tmpVec);
54  dimVec.push_back(tmpVec[this->m_VectorAxis]);
55 
56  if (i!=0)
57  {
58  for ( int kk = 0; kk < 4; ++kk )
59  {
60  utlSAGlobalException(kk!=this->m_VectorAxis && size4d[0][kk]!=size4d[i][kk])
61  (this->m_VectorAxis)(i)(kk).msg("wrong 4d size (x,y,z,t)");
62  }
63  }
64  }
65 
66  std::vector<int> outSize4d = size4d[0];
67  outSize4d[this->m_VectorAxis] = this->m_Functor.GetOutputDimension(dimVec);
68 
69  itk::SetVectorImageFullSize(outImage, outSize4d);
70 }
71 
72 template< typename TInputImage, typename TOutputImage, typename TFunction, class TMaskImage >
73 void
76 {
78 
79  this->VerifyInputParameters();
80 
81  if (utl::IsLogDebug(this->m_LogLevel) && this->GetNumberOfThreads()>1)
82  this->CreateLoggerVector();
83 
84  typename TInputImage::ConstPointer inputImage = this->GetInput();
85  std::vector<int> size = itk::GetVectorImageFullSize(inputImage);
86  this->m_Functor.VerifyInputParameters(size[this->m_VectorAxis]);
87  this->m_Functor.Initialize();
88 }
89 
93 template< typename TInputImage, typename TOutputImage, typename TFunction, class TMaskImage >
94 void
96 ::ThreadedGenerateData(const OutputImageRegionType & outputRegionForThread,
97  ThreadIdType threadId)
98 {
100 
101  InputImagePointer inputImage = const_cast<InputImageType *>(this->GetInput());
102  OutputImagePointer outImage = this->GetOutput();
103  int numberOfInputs = this->GetNumberOfInputs();
104 
105  // outImage->Print(std::cout << "outImage =\n");
106 
107  Pointer selfClone = this->Clone();
108  selfClone->m_ThreadID = threadId;
109  std::string threadIDStr = selfClone->ThreadIDToString();
110 
111  InputImageRegionType regionInput;
112  itk::CopyImageRegion(outputRegionForThread, regionInput);
113 
114  std::vector<VectorImageRegionIteratorWithIndex<InputImageType> > inputItVec;
115  for ( int i = 0; i < numberOfInputs; ++i )
116  {
117  InputImagePointer inputTmp = const_cast<InputImageType *>(this->GetInput(i));
118 
119  VectorImageRegionIteratorWithIndex<InputImageType> it(inputTmp, regionInput, this->m_VectorAxis);
120  inputItVec.push_back(it);
121  }
122 
124  int maskDim = 1;
125  if (this->IsMaskUsed())
126  {
127  typename MaskImageType::RegionType regionTmp;
128  itk::CopyImageRegion(outputRegionForThread, regionTmp, 1);
129  maskIt = VectorImageRegionIteratorWithIndex<MaskImageType>(this->m_MaskImage, regionTmp, this->m_VectorAxis);
130  maskDim = itk::GetVectorImageVectorSize(this->m_MaskImage);
131  }
132 
133  if (utl::IsLogDebug(this->m_LogLevel))
134  {
135  std::ostringstream msg;
136  msg << threadIDStr << "outputRegionForThread = " << outputRegionForThread << std::endl << std::flush;
137  msg << threadIDStr << "regionInput = " << regionInput << std::endl << std::flush;
138  this->WriteLogger(msg.str());
139  }
140 
141  int vecInputSize = itk::GetVectorImageVectorSize(inputImage);
142  int outDim = itk::GetVectorImageVectorSize(outImage, this->m_VectorAxis);
143  utlSAGlobalException(maskDim>1 && maskDim!=vecInputSize)(maskDim)(vecInputSize).msg("4D mask have a wrong 4-th dimension.");
144 
145 
146  itk::VectorImageRegionIteratorWithIndex<TOutputImage> outIt(outImage, outputRegionForThread, this->m_VectorAxis);
147 
148  OutputImageIndexType index;
149  VariableLengthVector<double> inPixel, outPixel, maskPixel;
150  std::vector<utl::Vector<double> > pixelVec(numberOfInputs);
151  utl::Vector<double> inVec, outVec(outDim);
152  outPixel.SetSize(outDim);
153  outPixel.Fill(0.0);
154 
155  for ( int n = 0; n < numberOfInputs; n += 1 )
156  inputItVec[n].GoToBegin();
157  outIt.GoToBegin();
158  maskIt.GoToBegin();
159 
160  while ( !outIt.IsAtEnd() )
161  {
162 
163  if (this->IsMaskUsed() && maskDim==1)
164  {
165  maskIt.GetVector(maskPixel,0);
166  if (maskPixel.GetSquaredNorm()==0)
167  {
168  outPixel.Fill(0.0);
169  for ( int i = 0; i < (this->m_VectorAxis==3?1:vecInputSize); ++i )
170  {
171  outIt.SetVector(outPixel, i);
172  }
173  for ( int n = 0; n < numberOfInputs; n += 1 )
174  ++inputItVec[n];
175  ++outIt;
176  ++maskIt;
177  continue;
178  }
179  }
180 
181  index = outIt.GetIndex();
182 
183  for ( int i = 0; i < (this->m_VectorAxis==3?1:vecInputSize); ++i )
184  {
185 
186  if (this->IsMaskUsed() && maskDim>1)
187  {
188  maskIt.GetVector(maskPixel, i);
189  if (maskPixel.GetSquaredNorm()==0)
190  {
191  outPixel.Fill(0.0);
192  outIt.SetVector(outPixel, i);
193  for ( int n = 0; n < numberOfInputs; n += 1 )
194  ++inputItVec[n];
195  ++outIt;
196  ++maskIt;
197  continue;
198  }
199  }
200 
201  for ( int n = 0; n < numberOfInputs; ++n )
202  {
203  inputItVec[n].GetVector(inPixel, i);
204  pixelVec[n] = utl::VariableLengthVectorToUtlVector(inPixel);
205  }
206 
207  if (utl::IsLogDebug(this->m_LogLevel))
208  {
209  std::ostringstream msg;
210  msg << "\n" << threadIDStr << "index = " << index << ", i=" << i << std::endl << std::flush;
211  for ( int n = 0; n < numberOfInputs; ++n )
212  utl::PrintUtlVector(pixelVec[n], "pixelVec[n]", " ", msg << threadIDStr);
213  this->WriteLogger(msg.str());
214  }
215 
216  outVec = selfClone->m_Functor(pixelVec);
217  outPixel = utl::UtlVectorToVariableLengthVector(outVec);
218 
219  if (utl::IsLogDebug(this->m_LogLevel))
220  {
221  std::ostringstream msg;
222  itk::PrintVariableLengthVector(outPixel, "outPixel", " ", msg << threadIDStr);
223  this->WriteLogger(msg.str());
224  }
225  outIt.SetVector(outPixel,i);
226  }
227 
228  for ( int n = 0; n < numberOfInputs; n += 1 )
229  ++inputItVec[n];
230  ++outIt;
231  ++maskIt;
232  }
233 
234 }
235 
236 } // end namespace itk
237 
238 #endif
239 
240 
241 
NDArray<T,1> is a vector class which uses blas mkl.
Definition: utlVector.h:36
void CopyImageRegion(const ImageRegion< dimIn > &regionIn, ImageRegion< dimOut > &regionOut, const int numberOfComponens=-1)
Definition: utlITK.h:513
#define utlException(cond, expout)
Definition: utlCoreMacro.h:548
#define utlSAGlobalException(expr)
Definition: utlCoreMacro.h:362
void SetVectorImageFullSize(SmartPointer< ImageType > &image, const std::vector< int > &size)
Definition: utlITK.h:400
itk::VariableLengthVector< T > UtlVectorToVariableLengthVector(const NDArray< T, 1 > &vec)
Definition: utl.h:114
void PrintVariableLengthVector(const VariableLengthVector< T >vec, const std::string &str="", const char *separate=" ", std::ostream &os=std::cout)
Definition: utlITK.h:45
bool IsLogDebug(const int level=utl::LogLevel)
Definition: utlCoreMacro.h:213
std::vector< int > GetVectorImageFullSize(const SmartPointer< ImageType > &image)
Definition: utlITK.h:373
void CopyImageInformation(const SmartPointer< ImageWithInfoType > &imageFrom, SmartPointer< ImageType > &imageTo)
Definition: utlITK.h:552
NDArray< T, 1 > VariableLengthVectorToUtlVector(const itk::VariableLengthVector< T > &vec)
Definition: utl.h:124
void GetVector(PixelVectorType &vec, const int offIndex=0) const
int GetVectorImageVectorSize(const SmartPointer< ImageType > &image, const int axis=-1)
Definition: utlITK.h:278
void ThreadedGenerateData(const OutputImageRegionType &outputRegionForThread, ThreadIdType threadId) ITK_OVERRIDE
#define itkShowPositionThreadedLogger(cond)
Definition: utlITKMacro.h:192
void PrintUtlVector(const NDArray< T, 1 > &vec, const std::string &str="", const char *separate=" ", std::ostream &os=std::cout, bool showStats=true)
macros for utlCore
A multi-dimensional iterator templated over image type. It provides the same interfaces for both itk:...
void SetVector(const PixelVectorType &value, const int offIndex=0) const