Orfeo Toolbox  3.16
itkFlipImageFilter.txx
Go to the documentation of this file.
1 /*=========================================================================
2 
3  Program: Insight Segmentation & Registration Toolkit
4  Module: $RCSfile: itkFlipImageFilter.txx,v $
5  Language: C++
6  Date: $Date: 2008-10-07 17:31:02 $
7  Version: $Revision: 1.19 $
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 __itkFlipImageFilter_txx
18 #define __itkFlipImageFilter_txx
19 
20 #include "itkFlipImageFilter.h"
22 #include "itkExceptionObject.h"
23 #include "itkProgressReporter.h"
24 
25 namespace itk
26 {
27 
31 template <class TImage>
34 {
35 
36  m_FlipAxes.Fill( false );
37  m_FlipAboutOrigin = true;
38 
39 }
40 
41 
45 template <class TImage>
46 void
48 ::PrintSelf(std::ostream& os, Indent indent) const
49 {
50  Superclass::PrintSelf(os,indent);
51  os << indent << "FlipAxes: " << m_FlipAxes << std::endl;
52  os << indent << "FlipAboutOrigin: " << m_FlipAboutOrigin << std::endl;
53 
54 }
55 
56 
61 template <class TImage>
62 void
65 {
66  // call the superclass's implementation of this method
67  Superclass::GenerateOutputInformation();
68 
69  // get pointers to the input and output
70  InputImagePointer inputPtr =
71  const_cast< TImage * >( this->GetInput() );
72  OutputImagePointer outputPtr = this->GetOutput();
73 
74  if( !inputPtr || !outputPtr )
75  {
76  return;
77  }
78 
79  const typename TImage::DirectionType& inputDirection = inputPtr->GetDirection();
80  const typename TImage::SizeType& inputSize =
81  inputPtr->GetLargestPossibleRegion().GetSize();
82  const typename TImage::IndexType& inputStartIndex =
83  inputPtr->GetLargestPossibleRegion().GetIndex();
84 
85  typename TImage::PointType outputOrigin;
86  typename TImage::IndexType newIndex = inputStartIndex;
87 
88  unsigned int j;
89 
90  typename TImage::DirectionType flipMatrix;
91  flipMatrix.SetIdentity();
92 
93  // Need the coordinate of the pixel that will become the first pixel
94  // and need a matrix to model the flip
95  for ( j = 0; j < ImageDimension; j++ )
96  {
97  if ( m_FlipAxes[j] )
98  {
99  // If flipping the axis, then we need to know the last pixel in
100  // that dimension
101  newIndex[j] += (inputSize[j] - 1);
102 
103  // What we really want is the index padded out past this point
104  // by the amount the start index is from [0,0,0] (because the
105  // output regions have the same index layout as the input
106  // regions)
107  newIndex[j] += inputStartIndex[j];
108 
109  // Only flip the directions if we are NOT flipping about the
110  // origin (when flipping about the origin, the pixels are
111  // ordered in the same direction as the input directions. when
112  // NOT flipping about the origin, the pixels traverse space in
113  // the opposite direction. when flipping about the origin,
114  // increasing indices traverse space in the same direction as
115  // the original data.).
116  if (!m_FlipAboutOrigin)
117  {
118  flipMatrix[j][j] = -1.0;
119  }
120  }
121  }
122 
123  inputPtr->TransformIndexToPhysicalPoint( newIndex, outputOrigin );
124 
125  // Finally, flip about the origin if needed
126  if (m_FlipAboutOrigin)
127  {
128  for ( j = 0; j < ImageDimension; j++ )
129  {
130  if ( m_FlipAxes[j] )
131  {
132  outputOrigin[j] *= -1;
133  }
134  }
135  }
136 
137  outputPtr->SetDirection( inputDirection * flipMatrix );
138  outputPtr->SetOrigin( outputOrigin );
139 }
140 
141 
146 template <class TImage>
147 void
150 {
151  // call the superclass's implementation of this method
152  Superclass::GenerateInputRequestedRegion();
153 
154  // get pointers to the input and output
155  InputImagePointer inputPtr =
156  const_cast< TImage * >( this->GetInput());
157  OutputImagePointer outputPtr = this->GetOutput();
158 
159  if( !inputPtr || !outputPtr )
160  {
161  return;
162  }
163 
164  const typename TImage::SizeType& outputRequestedSize =
165  outputPtr->GetRequestedRegion().GetSize();
166  const typename TImage::IndexType& outputRequestedIndex =
167  outputPtr->GetRequestedRegion().GetIndex();
168 
169  const typename TImage::SizeType & outputLargestPossibleSize =
170  outputPtr->GetLargestPossibleRegion().GetSize();
171  const typename TImage::IndexType& outputLargestPossibleIndex =
172  outputPtr->GetLargestPossibleRegion().GetIndex();
173 
174  IndexType inputRequestedIndex;
175 
176  unsigned int j;
177  for ( j = 0; j < ImageDimension; j++ )
178  {
179  if ( m_FlipAxes[j] )
180  {
181  inputRequestedIndex[j] =
182  2 * outputLargestPossibleIndex[j]
183  + static_cast<IndexValueType>( outputLargestPossibleSize[j] )
184  - static_cast<IndexValueType>( outputRequestedSize[j] )
185  - outputRequestedIndex[j];
186  }
187  else
188  {
189  inputRequestedIndex[j] = outputRequestedIndex[j];
190  }
191  }
192 
193  typename TImage::RegionType inputRequestedRegion;
194  inputRequestedRegion.SetSize( outputRequestedSize );
195  inputRequestedRegion.SetIndex( inputRequestedIndex );
196 
197  inputPtr->SetRequestedRegion( inputRequestedRegion );
198 
199 
200 }
201 
202 
206 template <class TImage>
207 void
209 ::ThreadedGenerateData(const OutputImageRegionType& outputRegionForThread,
210  int threadId)
211 {
212 
213  unsigned long i;
214  unsigned int j;
215 
216  // Get the input and output pointers
217  InputImageConstPointer inputPtr = this->GetInput();
218  OutputImagePointer outputPtr = this->GetOutput();
219 
220  // Setup output region iterator
221  typedef ImageRegionIteratorWithIndex<TImage> OutputIterator;
222  OutputIterator outIt(outputPtr, outputRegionForThread);
223 
224  typename TImage::IndexType outputIndex;
225  typename TImage::IndexType inputIndex;
226 
227  // support progress methods/callbacks
228  ProgressReporter progress(this, threadId, outputRegionForThread.GetNumberOfPixels());
229 
230  const typename TImage::SizeType & outputLargestPossibleSize =
231  outputPtr->GetLargestPossibleRegion().GetSize();
232  const typename TImage::IndexType& outputLargestPossibleIndex =
233  outputPtr->GetLargestPossibleRegion().GetIndex();
234 
235  IndexValueType offset[ImageDimension];
236  for ( j = 0; j < ImageDimension; j++ )
237  {
238  if ( m_FlipAxes[j] )
239  {
240  offset[j] = 2 * outputLargestPossibleIndex[j]
241  + static_cast<IndexValueType>( outputLargestPossibleSize[j] ) - 1;
242  }
243  }
244 
245  // walk the output region, and sample the input image
246  for ( i = 0; !outIt.IsAtEnd(); ++outIt, i++ )
247  {
248  // determine the index of the output pixel
249  outputIndex = outIt.GetIndex();
250 
251  // determine the input pixel location associated with this output pixel
252  for ( j = 0; j < ImageDimension; j++ )
253  {
254  if ( m_FlipAxes[j] )
255  {
256  inputIndex[j] = - 1 * outputIndex[j] + offset[j];
257  }
258  else
259  {
260  inputIndex[j] = outputIndex[ j ];
261  }
262  }
263 
264  // copy the input pixel to the output
265  outIt.Set( inputPtr->GetPixel(inputIndex) );
266  progress.CompletedPixel();
267  }
268 
269 }
270 
271 
272 } // namespace itk
273 
274 #endif

Generated at Sat Feb 2 2013 23:38:23 for Orfeo Toolbox with doxygen 1.8.1.1