ITK  4.4.0
Insight Segmentation and Registration Toolkit
itkImageRegionReverseConstIterator.h
Go to the documentation of this file.
1 /*=========================================================================
2  *
3  * Copyright Insight Software Consortium
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0.txt
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  *
17  *=========================================================================*/
18 #ifndef __itkImageRegionReverseConstIterator_h
19 #define __itkImageRegionReverseConstIterator_h
20 
22 #include "itkImageRegionIterator.h"
23 
24 namespace itk
25 {
102 template< typename TImage >
104 {
105 public:
106 
110 
115  enum { ImageIteratorDimension = Superclass::ImageIteratorDimension };
116 
120 
123  typedef typename Superclass::SizeType SizeType;
124 
128 
131 
135 
140  typedef typename PixelContainer::Pointer PixelContainerPointer;
141 
144 
147 
151 
154 
157  {
158  m_SpanBeginOffset = 0;
159  m_SpanEndOffset = 0;
160  }
161 
165  Superclass(ptr, region)
166  {
167  m_SpanBeginOffset = this->m_BeginOffset;
168  m_SpanEndOffset = this->m_BeginOffset - static_cast< OffsetValueType >( this->m_Region.GetSize()[0] );
169  }
171 
180  {
181  IndexType ind = this->GetIndex();
182 
183  m_SpanBeginOffset = this->m_Offset + static_cast< OffsetValueType >( this->m_Region.GetSize()[0] )
184  - ( ind[0] - this->m_Region.GetIndex()[0] );
185  m_SpanEndOffset = m_SpanBeginOffset - static_cast< OffsetValueType >( this->m_Region.GetSize()[0] );
186  }
187 
191  {
192  IndexType ind = this->GetIndex();
193 
194  m_SpanBeginOffset = this->m_Offset + static_cast< OffsetValueType >( this->m_Region.GetSize()[0] )
195  - ( ind[0] - this->m_Region.GetIndex()[0] );
196  m_SpanEndOffset = m_SpanBeginOffset - static_cast< OffsetValueType >( this->m_Region.GetSize()[0] );
197  }
198 
202  {
203  IndexType ind = this->GetIndex();
204 
205  m_SpanBeginOffset = this->m_Offset + static_cast< OffsetValueType >( this->m_Region.GetSize()[0] )
206  - ( ind[0] - this->m_Region.GetIndex()[0] );
207  m_SpanEndOffset = m_SpanBeginOffset - static_cast< OffsetValueType >( this->m_Region.GetSize()[0] );
208  }
209 
212  void GoToBegin()
213  {
214  Superclass::GoToBegin();
215 
216  // reset the span offsets
217  m_SpanBeginOffset = this->m_BeginOffset;
218  m_SpanEndOffset = this->m_BeginOffset - static_cast< OffsetValueType >( this->m_Region.GetSize()[0] );
219  }
220 
223  void GoToEnd()
224  {
225  Superclass::GoToEnd();
226 
227  // reset the span offsets
228  m_SpanEndOffset = this->m_EndOffset;
229  m_SpanBeginOffset = m_SpanEndOffset + static_cast< OffsetValueType >( this->m_Region.GetSize()[0] );
230  }
231 
235  itkLegacyMacro(Self Begin(void) const);
236 
241  itkLegacyMacro(Self End(void) const);
242 
246  void SetIndex(const IndexType & ind)
247  {
248  Superclass::SetIndex(ind);
249  m_SpanBeginOffset = this->m_Offset + static_cast< OffsetValueType >( this->m_Region.GetSize()[0] )
250  - ( ind[0] - this->m_Region.GetIndex()[0] );
251  m_SpanEndOffset = m_SpanBeginOffset - static_cast< OffsetValueType >( this->m_Region.GetSize()[0] );
252  }
254 
263  Self &
264  operator++()
265  {
266  if ( --this->m_Offset <= m_SpanEndOffset )
267  {
268  // We have past the beginning of the span (row), need to wrap around.
269 
270  // First move forward one pixel, because we are going to use a different
271  // algorithm to compute the next pixel
272  this->m_Offset++;
273 
274  // Get the index of the first pixel on the span (row)
276  ind = this->m_Image->ComputeIndex( static_cast< OffsetValueType >( this->m_Offset ) );
277 
279  startIndex = this->m_Region.GetIndex();
281  size = this->m_Region.GetSize();
282 
283  // Deccrement along a row, then wrap at the beginning of the region row.
284  bool done;
285  unsigned int dim;
286 
287  // Check to see if we are past the first pixel in the region
288  // Note that --ind[0] moves to the previous pixel along the row.
289  done = ( --ind[0] == startIndex[0] - 1 );
290  for ( unsigned int i = 1; done && i < ImageIteratorDimension; i++ )
291  {
292  done = ( ind[i] == startIndex[i] );
293  }
294 
295  // if the iterator is outside the region (but not past region begin) then
296  // we need to wrap around the region
297  dim = 0;
298  if ( !done )
299  {
300  while ( ( dim < ImageIteratorDimension - 1 )
301  && ( ind[dim] < startIndex[dim] ) )
302  {
303  ind[dim] = startIndex[dim] + static_cast< OffsetValueType >( size[dim] ) - 1;
304  ind[++dim]--;
305  }
306  }
307  this->m_Offset = this->m_Image->ComputeOffset(ind);
308  m_SpanBeginOffset = this->m_Offset;
309  m_SpanEndOffset = m_SpanBeginOffset - static_cast< OffsetValueType >( size[0] );
310  }
311  return *this;
312  }
313 
322  Self & operator--()
323  {
324  if ( ++this->m_Offset >= m_SpanBeginOffset )
325  {
326  // We have reached the end of the span (row), need to wrap around.
327 
328  // First back up one pixel, because we are going to use a different
329  // algorithm to compute the next pixel
330  --this->m_Offset;
331 
332  // Get the index of the last pixel on the span (row)
334  ind = this->m_Image->ComputeIndex( static_cast< OffsetValueType >( this->m_Offset ) );
335 
336  const typename ImageIterator< TImage >::IndexType &
337  startIndex = this->m_Region.GetIndex();
338  const typename ImageIterator< TImage >::SizeType &
339  size = this->m_Region.GetSize();
340 
341  // Increment along a row, then wrap at the end of the region row.
342  bool done;
343  unsigned int dim;
344 
345  // Check to see if we are past the last pixel in the region
346  // Note that ++ind[0] moves to the next pixel along the row.
347  done = ( ++ind[0] == startIndex[0] + static_cast< OffsetValueType >( size[0] ) );
348  for ( unsigned int i = 1; done && i < ImageIteratorDimension; i++ )
349  {
350  done = ( ind[i] == startIndex[i] + static_cast< OffsetValueType >( size[i] ) - 1 );
351  }
352 
353  // if the iterator is outside the region (but not past region end) then
354  // we need to wrap around the region
355  dim = 0;
356  if ( !done )
357  {
358  while ( ( dim < ImageIteratorDimension - 1 )
359  && ( ind[dim] > startIndex[dim] + static_cast< OffsetValueType >( size[dim] ) - 1 ) )
360  {
361  ind[dim] = startIndex[dim];
362  ind[++dim]++;
363  }
364  }
365  this->m_Offset = this->m_Image->ComputeOffset(ind);
366  m_SpanBeginOffset = this->m_Offset;
367  m_SpanEndOffset = this->m_Offset - static_cast< OffsetValueType >( size[0] );
368  }
369  return *this;
370  }
371 
372 protected:
373  SizeValueType m_SpanBeginOffset; // offset to last pixel in the row
374  SizeValueType m_SpanEndOffset; // offset to one pixel before the row
375 };
376 } // end namespace itk
377 
378 #ifndef ITK_MANUAL_INSTANTIATION
379 #include "itkImageRegionReverseConstIterator.hxx"
380 #endif
381 
382 #endif
383