Orfeo Toolbox  3.16
itkUnaryFunctorImageFilter.txx
Go to the documentation of this file.
1 /*=========================================================================
2 
3  Program: Insight Segmentation & Registration Toolkit
4  Module: $RCSfile: itkUnaryFunctorImageFilter.txx,v $
5  Language: C++
6  Date: $Date: 2009-10-28 03:37:14 $
7  Version: $Revision: 1.34 $
8 
9  Copyright (c) Insight Software Consortium. All rights reserved.
10  See ITKCopyright.txt or http://www.itk.org/HTML/Copyright.htm for details.
11 
12  This software is distributed WITHOUT ANY WARRANTY; without even
13  the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
14  PURPOSE. See the above copyright notices for more information.
15 
16 =========================================================================*/
17 #ifndef __itkUnaryFunctorImageFilter_txx
18 #define __itkUnaryFunctorImageFilter_txx
19 
21 #include "itkImageRegionIterator.h"
23 #include "itkProgressReporter.h"
24 
25 namespace itk
26 {
27 
31 template <class TInputImage, class TOutputImage, class TFunction >
34 {
35  this->SetNumberOfRequiredInputs( 1 );
36  this->InPlaceOff();
37 }
38 
48 template <class TInputImage, class TOutputImage, class TFunction>
49 void
52 {
53  // do not call the superclass' implementation of this method since
54  // this filter allows the input the output to be of different dimensions
55 
56  // get pointers to the input and output
57  typename Superclass::OutputImagePointer outputPtr = this->GetOutput();
58  typename Superclass::InputImageConstPointer inputPtr = this->GetInput();
59 
60  if ( !outputPtr || !inputPtr)
61  {
62  return;
63  }
64 
65  // Copy information from input to output. If input and output are of
66  // different dimensions, Region information will not be copied.
67  outputPtr->CopyInformation(inputPtr);
68 
69  // Set the output image largest possible region. Use a RegionCopier
70  // so that the input and output images can be different dimensions.
71  OutputImageRegionType outputLargestPossibleRegion;
72  this->CallCopyInputRegionToOutputRegion(outputLargestPossibleRegion,
73  inputPtr->GetLargestPossibleRegion());
74  outputPtr->SetLargestPossibleRegion( outputLargestPossibleRegion );
75 
76  // Set the output spacing and origin
78 
79  phyData
80  = dynamic_cast<const ImageBase<Superclass::InputImageDimension>*>(this->GetInput());
81 
82  if (phyData)
83  {
84  // Copy what we can from the image from spacing and origin of the input
85  // This logic needs to be augmented with logic that select which
86  // dimensions to copy
87  unsigned int i, j;
88  const typename InputImageType::SpacingType&
89  inputSpacing = inputPtr->GetSpacing();
90  const typename InputImageType::PointType&
91  inputOrigin = inputPtr->GetOrigin();
92  const typename InputImageType::DirectionType&
93  inputDirection = inputPtr->GetDirection();
94 
95  typename OutputImageType::SpacingType outputSpacing;
96  typename OutputImageType::PointType outputOrigin;
97  typename OutputImageType::DirectionType outputDirection;
98 
99  // copy the input to the output and fill the rest of the
100  // output with zeros.
101  for (i=0; i < Superclass::InputImageDimension; ++i)
102  {
103  outputSpacing[i] = inputSpacing[i];
104  outputOrigin[i] = inputOrigin[i];
105  for (j=0; j < Superclass::OutputImageDimension; j++)
106  {
107  if (j < Superclass::InputImageDimension)
108  {
109  outputDirection[j][i] = inputDirection[j][i];
110  }
111  else
112  {
113  outputDirection[j][i] = 0.0;
114  }
115  }
116  }
117  for (; i < Superclass::OutputImageDimension; ++i)
118  {
119  outputSpacing[i] = 1.0;
120  outputOrigin[i] = 0.0;
121  for (j=0; j < Superclass::OutputImageDimension; j++)
122  {
123  if (j == i)
124  {
125  outputDirection[j][i] = 1.0;
126  }
127  else
128  {
129  outputDirection[j][i] = 0.0;
130  }
131  }
132  }
133 
134  // set the spacing and origin
135  outputPtr->SetSpacing( outputSpacing );
136  outputPtr->SetOrigin( outputOrigin );
137  outputPtr->SetDirection( outputDirection );
138  outputPtr->SetNumberOfComponentsPerPixel( // propagate vector length info
139  inputPtr->GetNumberOfComponentsPerPixel());
140  }
141  else
142  {
143  // pointer could not be cast back down
144  itkExceptionMacro(<< "itk::UnaryFunctorImageFilter::GenerateOutputInformation "
145  << "cannot cast input to "
146  << typeid(ImageBase<Superclass::InputImageDimension>*).name() );
147  }
148 }
149 
150 
154 template <class TInputImage, class TOutputImage, class TFunction >
155 void
157 ::ThreadedGenerateData( const OutputImageRegionType &outputRegionForThread,
158  int threadId)
159 {
160  InputImagePointer inputPtr = this->GetInput();
161  OutputImagePointer outputPtr = this->GetOutput(0);
162 
163  // Define the portion of the input to walk for this thread, using
164  // the CallCopyOutputRegionToInputRegion method allows for the input
165  // and output images to be different dimensions
166  InputImageRegionType inputRegionForThread;
167  this->CallCopyOutputRegionToInputRegion(inputRegionForThread, outputRegionForThread);
168 
169  // Define the iterators
170  ImageRegionConstIterator<TInputImage> inputIt(inputPtr, inputRegionForThread);
171  ImageRegionIterator<TOutputImage> outputIt(outputPtr, outputRegionForThread);
172 
173  ProgressReporter progress(this, threadId, outputRegionForThread.GetNumberOfPixels());
174 
175  inputIt.GoToBegin();
176  outputIt.GoToBegin();
177 
178  while( !inputIt.IsAtEnd() )
179  {
180  outputIt.Set( m_Functor( inputIt.Get() ) );
181  ++inputIt;
182  ++outputIt;
183  progress.CompletedPixel(); // potential exception thrown here
184  }
185 }
186 
187 } // end namespace itk
188 
189 #endif

Generated at Sun May 19 2013 00:11:18 for Orfeo Toolbox with doxygen 1.8.3.1