DMRITool  v0.1.1-139-g860d86b4
Diffusion MRI Tool
itkFunctorFromStringImageFilter.hxx
Go to the documentation of this file.
1 
11 #ifndef itkFunctorFromStringImageFilter_hxx
12 #define itkFunctorFromStringImageFilter_hxx
13 
15 #include "utlCoreMacro.h"
17 #include "utlITK.h"
18 
19 namespace itk
20 {
21 
22 template< typename TInputImage, typename TOutputImage, class TMaskImage >
25 {
26  this->SetNumberOfRequiredInputs(1);
27  this->SetNumberOfRequiredOutputs(1);
28 }
29 
30 template< typename TInputImage, typename TOutputImage, class TMaskImage >
31 void
34 {
36  typename TInputImage::ConstPointer inputImage = this->GetInput();
37  typename TInputImage::Pointer inputTmp = TInputImage::New();
38  OutputImagePointer outImage = this->GetOutput();
39 
40  // copy information
41  itk::CopyImageInformation(inputImage, outImage);
42 
43  int numberOfInputs = this->GetNumberOfInputs();
44 
45  std::vector<std::vector<int> > size4d;
46  for ( int i = 0; i < numberOfInputs; ++i )
47  {
48  inputTmp->Graft(this->GetInput(i));
49  std::vector<int> tmpVec = itk::GetVectorImageFullSize(inputTmp);
50  size4d.push_back(tmpVec);
51 
52  if (i!=0)
53  {
54  for ( int kk = 0; kk < 4; ++kk )
55  {
56  utlSAGlobalException(size4d[0][kk]!=size4d[i][kk])
57  (this->m_VectorAxis)(i)(kk).msg("wrong 4d size (x,y,z,t)");
58  }
59  }
60  }
61 }
62 
63 template< typename TInputImage, typename TOutputImage, class TMaskImage >
64 void
67 {
69 
70  this->VerifyInputParameters();
71 
72  if (utl::IsLogDebug(this->m_LogLevel) && this->GetNumberOfThreads()>1)
73  this->CreateLoggerVector();
74 
75  utlException(this->m_VectorAxis!=3, "m_VectorAxis should be 3, for element-wise function");
76 
77  // typename TInputImage::ConstPointer inputImage = this->GetInput();
78  // std::vector<int> size = itk::GetVectorImageFullSize(inputImage);
79  // this->m_Functor.VerifyInputParameters(size[this->m_VectorAxis]);
80  // this->m_Functor.Initialize();
81 }
82 
86 template< typename TInputImage, typename TOutputImage, class TMaskImage >
87 void
89 ::ThreadedGenerateData(const OutputImageRegionType & outputRegionForThread,
90  ThreadIdType threadId)
91 {
93 
94  InputImagePointer inputImage = const_cast<InputImageType *>(this->GetInput());
95  OutputImagePointer outImage = this->GetOutput();
96  int numberOfInputs = this->GetNumberOfInputs();
97 
98  // outImage->Print(std::cout << "outImage =\n");
99 
100  Pointer selfClone = this->Clone();
101  selfClone->m_ThreadID = threadId;
102  std::string threadIDStr = selfClone->ThreadIDToString();
103 
104  std::vector<double> x(numberOfInputs);
105  exprtk::symbol_table<double> symbol_table;
106  symbol_table.add_vector("x",x);
107  symbol_table.add_constants();
108 
109  exprtk::expression<double> expression;
110  expression.register_symbol_table(symbol_table);
111 
112  exprtk::parser<double> parser;
113  // parser.compile(m_Expression, expression);
114 
115  InputImageRegionType regionInput;
116  itk::CopyImageRegion(outputRegionForThread, regionInput);
117 
118  std::vector<VectorImageRegionIteratorWithIndex<InputImageType> > inputItVec;
119  for ( int i = 0; i < numberOfInputs; ++i )
120  {
121  InputImagePointer inputTmp = const_cast<InputImageType *>(this->GetInput(i));
122 
123  VectorImageRegionIteratorWithIndex<InputImageType> it(inputTmp, regionInput, this->m_VectorAxis);
124  inputItVec.push_back(it);
125  }
126 
128  int maskDim = 1;
129  if (this->IsMaskUsed())
130  {
131  typename MaskImageType::RegionType regionTmp;
132  itk::CopyImageRegion(outputRegionForThread, regionTmp, 1);
133  maskIt = VectorImageRegionIteratorWithIndex<MaskImageType>(this->m_MaskImage, regionTmp, this->m_VectorAxis);
134  maskDim = itk::GetVectorImageVectorSize(this->m_MaskImage);
135  }
136 
137  if (utl::IsLogDebug(this->m_LogLevel))
138  {
139  std::ostringstream msg;
140  msg << threadIDStr << "outputRegionForThread = " << outputRegionForThread << std::endl << std::flush;
141  msg << threadIDStr << "regionInput = " << regionInput << std::endl << std::flush;
142  this->WriteLogger(msg.str());
143  }
144 
145  int vecInputSize = itk::GetVectorImageVectorSize(inputImage);
146  int outDim = itk::GetVectorImageVectorSize(outImage, this->m_VectorAxis);
147  utlSAGlobalException(maskDim>1 && maskDim!=vecInputSize)(maskDim)(vecInputSize).msg("4D mask have a wrong 4-th dimension.");
148 
149 
150  itk::VectorImageRegionIteratorWithIndex<TOutputImage> outIt(outImage, outputRegionForThread, this->m_VectorAxis);
151 
152  OutputImageIndexType index;
153  VariableLengthVector<double> inPixel, outPixel, maskPixel;
154  std::vector<utl::Vector<double> > pixelVec(numberOfInputs);
155  std::vector<double> inStdVec(numberOfInputs);
156  utl::Vector<double> outVec(outDim);
157  outPixel.SetSize(outDim);
158  outPixel.Fill(0.0);
159 
160  for ( int n = 0; n < numberOfInputs; n += 1 )
161  inputItVec[n].GoToBegin();
162  outIt.GoToBegin();
163  maskIt.GoToBegin();
164 
165  while ( !outIt.IsAtEnd() )
166  {
167 
168  if (this->IsMaskUsed() && maskDim==1)
169  {
170  maskIt.GetVector(maskPixel,0);
171  if (maskPixel.GetSquaredNorm()==0)
172  {
173  outPixel.Fill(0.0);
174  for ( int i = 0; i < (this->m_VectorAxis==3?1:vecInputSize); ++i )
175  {
176  outIt.SetVector(outPixel, i);
177  }
178  for ( int n = 0; n < numberOfInputs; n += 1 )
179  ++inputItVec[n];
180  ++outIt;
181  ++maskIt;
182  continue;
183  }
184  }
185 
186  index = outIt.GetIndex();
187 
188  for ( int i = 0; i < (this->m_VectorAxis==3?1:vecInputSize); ++i )
189  {
190 
191  if (this->IsMaskUsed() && maskDim>1)
192  {
193  maskIt.GetVector(maskPixel, i);
194  if (maskPixel.GetSquaredNorm()==0)
195  {
196  outPixel.Fill(0.0);
197  outIt.SetVector(outPixel, i);
198  for ( int n = 0; n < numberOfInputs; n += 1 )
199  ++inputItVec[n];
200  ++outIt;
201  ++maskIt;
202  continue;
203  }
204  }
205 
206  for ( int n = 0; n < numberOfInputs; ++n )
207  {
208  inputItVec[n].GetVector(inPixel, i);
209  pixelVec[n] = utl::VariableLengthVectorToUtlVector(inPixel);
210  }
211 
212  if (utl::IsLogDebug(this->m_LogLevel))
213  {
214  std::ostringstream msg;
215  msg << "\n" << threadIDStr << "index = " << index << ", i=" << i << std::endl << std::flush;
216  for ( int n = 0; n < numberOfInputs; ++n )
217  utl::PrintUtlVector(pixelVec[n], "pixelVec[n]", " ", msg << threadIDStr);
218  this->WriteLogger(msg.str());
219  }
220 
221  for ( int v = 0; v < inPixel.Size(); ++v )
222  {
223  for ( int n = 0; n < numberOfInputs; n += 1 )
224  {
225  inStdVec[n] = pixelVec[n][v];
226  }
227  symbol_table.get_vector("x")->operator=(inStdVec);
228  parser.compile(m_Expression,expression);
229  outVec[v] = expression.value();
230  }
231 
232  // outVec = selfClone->m_Functor(pixelVec);
233  outPixel = utl::UtlVectorToVariableLengthVector(outVec);
234 
235  if (utl::IsLogDebug(this->m_LogLevel))
236  {
237  std::ostringstream msg;
238  itk::PrintVariableLengthVector(outPixel, "outPixel", " ", msg << threadIDStr);
239  this->WriteLogger(msg.str());
240  }
241  outIt.SetVector(outPixel,i);
242  }
243 
244  for ( int n = 0; n < numberOfInputs; n += 1 )
245  ++inputItVec[n];
246  ++outIt;
247  ++maskIt;
248  }
249 
250 }
251 
252 } // end namespace itk
253 
254 #endif
255 
256 
257 
258 
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
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
#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
void ThreadedGenerateData(const OutputImageRegionType &outputRegionForThread, ThreadIdType threadId) ITK_OVERRIDE