DMRITool  v0.1.1-139-g860d86b4
Diffusion MRI Tool
itkUnaryFunctorVectorImageFilter.hxx
Go to the documentation of this file.
1 
12 #ifndef itkUnaryFunctorVectorImageFilter_hxx
13 #define itkUnaryFunctorVectorImageFilter_hxx
14 
16 #include "utlCoreMacro.h"
18 
19 namespace itk
20 {
21 
22 template< typename TInputImage, typename TOutputImage, typename TFunction, class TMaskImage >
25 {
26  this->SetNumberOfRequiredInputs(1);
27  this->SetNumberOfRequiredOutputs(1);
28 }
29 
30 template< typename TInputImage, typename TOutputImage, typename TFunction, class TMaskImage >
31 void
34 {
36  typename TInputImage::ConstPointer inputImage = this->GetInput();
37  OutputImagePointer outImage = this->GetOutput();
38 
39  // copy information
40  itk::CopyImageInformation(inputImage, outImage);
41  utlException(this->m_VectorAxis<0, "need to set non-negative axis");
42 
43  int vecInputSize = itk::GetVectorImageVectorSize(inputImage);
44  int outVectorSize = this->m_Functor.GetOutputDimension(vecInputSize);
45  int outDim= (this->m_VectorAxis==3) ? outVectorSize : vecInputSize;
46  if (this->m_VectorAxis!=3)
47  {
48  OutputImageRegionType regionTmp = outImage->GetLargestPossibleRegion();
49  OutputImageSizeType sizeTmp = regionTmp.GetSize();
50  sizeTmp[this->m_VectorAxis] = this->m_Functor.GetOutputDimension(sizeTmp[this->m_VectorAxis]);
51  regionTmp.SetSize(sizeTmp);
52  outImage->SetRegions(regionTmp);
53  }
54  utlSAException(outDim<=0)(outDim).msg("wrong outDim");
55  itk::SetVectorImageVectorSize(outImage, outDim);
56 }
57 
58 template< typename TInputImage, typename TOutputImage, typename TFunction, class TMaskImage >
59 void
62 {
64 
65  this->VerifyInputParameters();
66 
67  if (utl::IsLogDebug(this->m_LogLevel) && this->GetNumberOfThreads()>1)
68  this->CreateLoggerVector();
69 
70  typename TInputImage::ConstPointer inputImage = this->GetInput();
71  std::vector<int> size = itk::GetVectorImageFullSize(inputImage);
72  this->m_Functor.VerifyInputParameters(size[this->m_VectorAxis]);
73  this->m_Functor.Initialize();
74 }
75 
79 template< typename TInputImage, typename TOutputImage, typename TFunction, class TMaskImage >
80 void
82 ::ThreadedGenerateData(const OutputImageRegionType & outputRegionForThread,
83  ThreadIdType threadId)
84 {
86  InputImagePointer inputImage = const_cast<InputImageType *>(this->GetInput());
87  OutputImagePointer outImage = this->GetOutput();
88 
89  Pointer selfClone = this->Clone();
90  selfClone->m_ThreadID = threadId;
91  std::string threadIDStr = selfClone->ThreadIDToString();
92 
93  InputImageRegionType regionInput;
94  itk::CopyImageRegion(outputRegionForThread, regionInput);
95  itk::VectorImageRegionIteratorWithIndex<InputImageType> it(inputImage, regionInput, this->m_VectorAxis);
96  // itk::VectorImageRegionIteratorWithIndex<InputImageType> it(inputImage, outputRegionForThread, m_VectorAxis);
97 
98  if (utl::IsLogDebug(this->m_LogLevel))
99  {
100  std::ostringstream msg;
101  selfClone->Print(msg<< threadIDStr << "selfClone = \n");
102  this->WriteLogger(msg.str());
103  }
104 
106  int maskDim = 1;
107  if (this->IsMaskUsed())
108  {
109  typename MaskImageType::RegionType regionTmp;
110  itk::CopyImageRegion(outputRegionForThread, regionTmp, 1);
111  maskIt = VectorImageRegionIteratorWithIndex<MaskImageType>(this->m_MaskImage, regionTmp, this->m_VectorAxis);
112  maskDim = itk::GetVectorImageVectorSize(this->m_MaskImage);
113  }
114 
115  int vecInputSize = itk::GetVectorImageVectorSize(inputImage);
116  int outDim = itk::GetVectorImageVectorSize(outImage, this->m_VectorAxis);
117  utlSAGlobalException(maskDim>1 && maskDim!=vecInputSize)(maskDim)(vecInputSize).msg("4D mask have a wrong 4-th dimension.");
118 
119  itk::VectorImageRegionIteratorWithIndex<TOutputImage> outIt(outImage, outputRegionForThread, this->m_VectorAxis);
120 
121  InputImageIndexType index;
122  VariableLengthVector<double> inPixel, outPixel, maskPixel;
123  utl::Vector<double> inVec, outVec(outDim);
124  outPixel.SetSize(outDim);
125  outPixel.Fill(0.0);
126  for (it.GoToBegin(), maskIt.GoToBegin(), outIt.GoToBegin();
127  !it.IsAtEnd();
128  ++it, ++maskIt, ++outIt)
129  {
130 
131  if (this->IsMaskUsed() && maskDim==1)
132  {
133  maskIt.GetVector(maskPixel,0);
134  if (maskPixel.GetSquaredNorm()==0)
135  {
136  outPixel.Fill(0.0);
137  for ( int i = 0; i < (this->m_VectorAxis==3?1:vecInputSize); ++i )
138  {
139  outIt.SetVector(outPixel, i);
140  }
141  continue;
142  }
143  }
144 
145  index = it.GetIndex();
146  // if (IsOutsideBox(index, realBox))
147  // { outPixel.Fill(0.0); outIt.SetVector(outPixel); continue; }
148 
149 
150  for ( int i = 0; i < (this->m_VectorAxis==3?1:vecInputSize); ++i )
151  {
152 
153  if (this->IsMaskUsed() && maskDim>1)
154  {
155  maskIt.GetVector(maskPixel, i);
156  if (maskPixel.GetSquaredNorm()==0)
157  {
158  outPixel.Fill(0.0);
159  outIt.SetVector(outPixel, i);
160  continue;
161  }
162  }
163 
164  it.GetVector(inPixel, i);
165 
166  inVec = utl::VariableLengthVectorToUtlVector(inPixel);
167  if (utl::IsLogDebug(this->m_LogLevel))
168  {
169  std::ostringstream msg;
170  msg << "\n" << threadIDStr << "index = " << index << std::endl << std::flush;
171  itk::PrintVariableLengthVector(inPixel, "inPixel", " ", msg << threadIDStr);
172  this->WriteLogger(msg.str());
173  }
174 
175  outVec = selfClone->m_Functor(inVec);
176  outPixel = utl::UtlVectorToVariableLengthVector(outVec);
177 
178  if (utl::IsLogDebug(this->m_LogLevel))
179  {
180  std::ostringstream msg;
181  itk::PrintVariableLengthVector(outPixel, "outPixel", " ", msg << threadIDStr);
182  this->WriteLogger(msg.str());
183  }
184  outIt.SetVector(outPixel,i);
185  }
186  }
187 }
188 
189 } // end namespace itk
190 
191 #endif
192 
193 
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
void SetVectorImageVectorSize(const SmartPointer< ImageType > &image, const int vecsize)
Definition: utlITK.h:310
used for debug information. this->GetDebug()
Definition: utlCoreMacro.h:193
#define utlException(cond, expout)
Definition: utlCoreMacro.h:548
#define utlSAGlobalException(expr)
Definition: utlCoreMacro.h:362
void ThreadedGenerateData(const OutputImageRegionType &outputRegionForThread, ThreadIdType threadId) ITK_OVERRIDE
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
#define utlSAException(expr)
Definition: utlCoreMacro.h:543
void GetVector(PixelVectorType &vec, const int offIndex=0) const
int GetVectorImageVectorSize(const SmartPointer< ImageType > &image, const int axis=-1)
Definition: utlITK.h:278
#define utlVLogPosition(level)
Definition: utlCoreMacro.h:298
#define itkShowPositionThreadedLogger(cond)
Definition: utlITKMacro.h:192
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