Orfeo Toolbox  3.16
itkPermuteAxesImageFilter.txx
Go to the documentation of this file.
1 /*=========================================================================
2 
3  Program: Insight Segmentation & Registration Toolkit
4  Module: $RCSfile: itkPermuteAxesImageFilter.txx,v $
5  Language: C++
6  Date: $Date: 2008-10-17 16:30:50 $
7  Version: $Revision: 1.16 $
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 __itkPermuteAxesImageFilter_txx
18 #define __itkPermuteAxesImageFilter_txx
19 
22 #include "itkExceptionObject.h"
23 #include "itkProgressReporter.h"
24 
25 namespace itk
26 {
27 
31 template <class TImage>
34 {
35  for ( unsigned int j = 0; j < ImageDimension; j++ )
36  {
37  m_Order[j] = j;
38  m_InverseOrder[m_Order[j]] = j;
39  }
40 
41 }
42 
43 
47 template <class TImage>
48 void
50 ::PrintSelf(std::ostream& os, Indent indent) const
51 {
52  Superclass::PrintSelf(os,indent);
53 
54  unsigned int j;
55 
56  os << indent << "Order: [";
57  for( j = 0; j < ImageDimension - 1; j++ )
58  {
59  os << m_Order[j] << ", ";
60  }
61  os << m_Order[j] << "]" << std::endl;
62 
63  os << indent << "InverseOrder: [";
64  for( j = 0; j < ImageDimension - 1; j++ )
65  {
66  os << m_InverseOrder[j] << ", ";
67  }
68  os << m_InverseOrder[j] << "]" << std::endl;
69 
70 
71 }
72 
73 
77 template <class TImage>
78 void
81 {
82 
83  unsigned int j;
84 
85  // check if it the same as current
86  if ( m_Order == order ) return;
87 
88  // check that input is a rearrangement of the
89  // numbers from 0 to ImageDimension - 1
91  used.Fill( false );
92 
93  for ( j = 0; j < ImageDimension; j++ )
94  {
95  if ( order[j] > ImageDimension - 1 )
96  {
97  ExceptionObject err(__FILE__, __LINE__);
98  err.SetLocation( ITK_LOCATION );
99  err.SetDescription( "Order indices is out of range" );
100  throw err;
101  }
102  else if ( used[order[j]] )
103  {
104  ExceptionObject err(__FILE__, __LINE__);
105  err.SetLocation( ITK_LOCATION );
106  err.SetDescription( "Order indices must not repeat" );
107  throw err;
108  }
109  used[order[j]] = true;
110  }
111 
112  // copy to member variable
113  this->Modified();
114  m_Order = order;
115  for ( j = 0; j < ImageDimension; j++ )
116  {
117  m_InverseOrder[m_Order[j]] = j;
118  }
119 
120 }
121 
122 
127 template <class TImage>
128 void
131 {
132  // call the superclass's implementation of this method
133  Superclass::GenerateOutputInformation();
134 
135  // get pointers to the input and output
136  typename Superclass::InputImageConstPointer inputPtr = this->GetInput();
137  typename Superclass::OutputImagePointer outputPtr = this->GetOutput();
138 
139  if( !inputPtr || !outputPtr )
140  {
141  return;
142  }
143 
144  const typename TImage::SpacingType& inputSpacing = inputPtr->GetSpacing();
145  const typename TImage::PointType& inputOrigin = inputPtr->GetOrigin();
146  const typename TImage::DirectionType& inputDirection = inputPtr->GetDirection();
147  const typename TImage::SizeType& inputSize =
148  inputPtr->GetLargestPossibleRegion().GetSize();
149  const typename TImage::IndexType& inputStartIndex =
150  inputPtr->GetLargestPossibleRegion().GetIndex();
151 
152 
153  typename TImage::SpacingType outputSpacing;
154  typename TImage::PointType outputOrigin;
155  typename TImage::DirectionType outputDirection;
156  typename TImage::SizeType outputSize;
157  typename TImage::IndexType outputStartIndex;
158 
159  unsigned int i, j;
160  for ( j = 0; j < ImageDimension; j++ )
161  {
162  // origin does not change by a Permute. But spacing, directions,
163  // size and start index do.
164  outputOrigin[j] = inputOrigin[j];
165 
166  outputSpacing[j] = inputSpacing[m_Order[j]];
167  outputSize[j] = inputSize[m_Order[j]];
168  outputStartIndex[j] = inputStartIndex[m_Order[j]];
169  for ( i = 0; i < ImageDimension; i++ )
170  {
171  outputDirection[i][j] = inputDirection[i][m_Order[j]];
172  }
173  }
174 
175  outputPtr->SetSpacing( outputSpacing );
176  outputPtr->SetOrigin( outputOrigin );
177  outputPtr->SetDirection( outputDirection );
178 
179  typename TImage::RegionType outputRegion;
180  outputRegion.SetSize( outputSize );
181  outputRegion.SetIndex( outputStartIndex );
182 
183  outputPtr->SetLargestPossibleRegion( outputRegion );
184 
185 }
186 
187 
192 template <class TImage>
193 void
196 {
197  // call the superclass's implementation of this method
198  Superclass::GenerateInputRequestedRegion();
199 
200  // get pointers to the input and output
201  InputImagePointer inputPtr =
202  const_cast< TImage * >( this->GetInput() );
203  OutputImagePointer outputPtr = this->GetOutput();
204 
205  if( !inputPtr || !outputPtr )
206  {
207  return;
208  }
209 
210  const typename TImage::SizeType& outputSize =
211  outputPtr->GetRequestedRegion().GetSize();
212  const typename TImage::IndexType& outputIndex =
213  outputPtr->GetRequestedRegion().GetIndex();
214 
215  typename TImage::SizeType inputSize;
216  typename TImage::IndexType inputIndex;
217 
218  unsigned int j;
219  for ( j = 0; j < ImageDimension; j++ )
220  {
221  inputSize[j] = outputSize[m_InverseOrder[j]];
222  inputIndex[j] = outputIndex[m_InverseOrder[j]];
223  }
224 
225  typename TImage::RegionType inputRegion;
226  inputRegion.SetSize( inputSize );
227  inputRegion.SetIndex( inputIndex );
228 
229  inputPtr->SetRequestedRegion( inputRegion );
230 
231 }
232 
233 
237 template <class TImage>
238 void
240 ::ThreadedGenerateData(const OutputImageRegionType& outputRegionForThread,
241  int threadId)
242 {
243  unsigned long i;
244  unsigned int j;
245 
246  // Get the input and output pointers
247  typename Superclass::InputImageConstPointer inputPtr = this->GetInput();
248  typename Superclass::OutputImagePointer outputPtr = this->GetOutput();
249 
250  // Setup output region iterator
251  typedef ImageRegionIteratorWithIndex<TImage> OutputIterator;
252  OutputIterator outIt(outputPtr, outputRegionForThread);
253 
254  typename TImage::IndexType outputIndex;
255  typename TImage::IndexType inputIndex;
256 
257  // support progress methods/callbacks
258  ProgressReporter progress(this, threadId, outputRegionForThread.GetNumberOfPixels());
259 
260  // walk the output region, and sample the input image
261  for ( i = 0; !outIt.IsAtEnd(); ++outIt, i++ )
262  {
263  // determine the index of the output pixel
264  outputIndex = outIt.GetIndex();
265 
266  // determine the input pixel location associated with this output pixel
267  for ( j = 0; j < ImageDimension; j++ )
268  {
269  inputIndex[j] = outputIndex[ m_InverseOrder[j] ];
270  }
271 
272  // copy the input pixel to the output
273  outIt.Set( inputPtr->GetPixel(inputIndex) );
274  progress.CompletedPixel();
275  }
276 
277 }
278 
279 
280 } // namespace itk
281 
282 #endif

Generated at Sun Jun 16 2013 00:00:21 for Orfeo Toolbox with doxygen 1.8.3.1