ITK  5.0.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 >
103 class ITK_TEMPLATE_EXPORT ImageRegionReverseConstIterator:public ImageReverseConstIterator< TImage >
104 {
105 public:
109 
113 
116  using SizeType = typename Superclass::SizeType;
117 
121 
124 
128 
133  using PixelContainerPointer = typename PixelContainer::Pointer;
134 
137 
140 
144 
147 
150  {
151  m_SpanBeginOffset = 0;
152  m_SpanEndOffset = 0;
153  }
154 
158  Superclass(ptr, region)
159  {
160  m_SpanBeginOffset = this->m_BeginOffset;
161  m_SpanEndOffset = this->m_BeginOffset - static_cast< OffsetValueType >( this->m_Region.GetSize()[0] );
162  }
164 
173  {
174  IndexType ind = this->GetIndex();
175 
176  m_SpanBeginOffset = this->m_Offset + static_cast< OffsetValueType >( this->m_Region.GetSize()[0] )
177  - ( ind[0] - this->m_Region.GetIndex()[0] );
178  m_SpanEndOffset = m_SpanBeginOffset - static_cast< OffsetValueType >( this->m_Region.GetSize()[0] );
179  }
180 
184  {
185  IndexType ind = this->GetIndex();
186 
187  m_SpanBeginOffset = this->m_Offset + static_cast< OffsetValueType >( this->m_Region.GetSize()[0] )
188  - ( ind[0] - this->m_Region.GetIndex()[0] );
189  m_SpanEndOffset = m_SpanBeginOffset - static_cast< OffsetValueType >( this->m_Region.GetSize()[0] );
190  }
191 
195  {
196  IndexType ind = this->GetIndex();
197 
198  m_SpanBeginOffset = this->m_Offset + static_cast< OffsetValueType >( this->m_Region.GetSize()[0] )
199  - ( ind[0] - this->m_Region.GetIndex()[0] );
200  m_SpanEndOffset = m_SpanBeginOffset - static_cast< OffsetValueType >( this->m_Region.GetSize()[0] );
201  }
202 
205  void GoToBegin()
206  {
207  Superclass::GoToBegin();
208 
209  // reset the span offsets
210  m_SpanBeginOffset = this->m_BeginOffset;
211  m_SpanEndOffset = this->m_BeginOffset - static_cast< OffsetValueType >( this->m_Region.GetSize()[0] );
212  }
213 
216  void GoToEnd()
217  {
218  Superclass::GoToEnd();
219 
220  // reset the span offsets
221  m_SpanEndOffset = this->m_EndOffset;
222  m_SpanBeginOffset = m_SpanEndOffset + static_cast< OffsetValueType >( this->m_Region.GetSize()[0] );
223  }
224 
228  void SetIndex(const IndexType & ind) override
229  {
230  Superclass::SetIndex(ind);
231  m_SpanBeginOffset = this->m_Offset + static_cast< OffsetValueType >( this->m_Region.GetSize()[0] )
232  - ( ind[0] - this->m_Region.GetIndex()[0] );
233  m_SpanEndOffset = m_SpanBeginOffset - static_cast< OffsetValueType >( this->m_Region.GetSize()[0] );
234  }
236 
245  Self &
247  {
248  if ( --this->m_Offset <= m_SpanEndOffset )
249  {
250  // We have past the beginning of the span (row), need to wrap around.
251 
252  // First move forward one pixel, because we are going to use a different
253  // algorithm to compute the next pixel
254  this->m_Offset++;
255 
256  // Get the index of the first pixel on the span (row)
258  ind = this->m_Image->ComputeIndex( static_cast< OffsetValueType >( this->m_Offset ) );
259 
261  startIndex = this->m_Region.GetIndex();
263  size = this->m_Region.GetSize();
264 
265  // Deccrement along a row, then wrap at the beginning of the region row.
266  bool done;
267  unsigned int dim;
268 
269  // Check to see if we are past the first pixel in the region
270  // Note that --ind[0] moves to the previous pixel along the row.
271  done = ( --ind[0] == startIndex[0] - 1 );
272  for ( unsigned int i = 1; done && i < this->ImageIteratorDimension; i++ )
273  {
274  done = ( ind[i] == startIndex[i] );
275  }
276 
277  // if the iterator is outside the region (but not past region begin) then
278  // we need to wrap around the region
279  dim = 0;
280  if ( !done )
281  {
282  while ( ( dim < this->ImageIteratorDimension - 1 )
283  && ( ind[dim] < startIndex[dim] ) )
284  {
285  ind[dim] = startIndex[dim] + static_cast< OffsetValueType >( size[dim] ) - 1;
286  ind[++dim]--;
287  }
288  }
289  this->m_Offset = this->m_Image->ComputeOffset(ind);
290  m_SpanBeginOffset = this->m_Offset;
291  m_SpanEndOffset = m_SpanBeginOffset - static_cast< OffsetValueType >( size[0] );
292  }
293  return *this;
294  }
295 
305  {
306  if ( ++this->m_Offset >= m_SpanBeginOffset )
307  {
308  // We have reached the end of the span (row), need to wrap around.
309 
310  // First back up one pixel, because we are going to use a different
311  // algorithm to compute the next pixel
312  --this->m_Offset;
313 
314  // Get the index of the last pixel on the span (row)
316  ind = this->m_Image->ComputeIndex( static_cast< OffsetValueType >( this->m_Offset ) );
317 
318  const typename ImageIterator< TImage >::IndexType &
319  startIndex = this->m_Region.GetIndex();
320  const typename ImageIterator< TImage >::SizeType &
321  size = this->m_Region.GetSize();
322 
323  // Increment along a row, then wrap at the end of the region row.
324  bool done;
325  unsigned int dim;
326 
327  // Check to see if we are past the last pixel in the region
328  // Note that ++ind[0] moves to the next pixel along the row.
329  done = ( ++ind[0] == startIndex[0] + static_cast< OffsetValueType >( size[0] ) );
330  for ( unsigned int i = 1; done && i < this->ImageIteratorDimension; i++ )
331  {
332  done = ( ind[i] == startIndex[i] + static_cast< OffsetValueType >( size[i] ) - 1 );
333  }
334 
335  // if the iterator is outside the region (but not past region end) then
336  // we need to wrap around the region
337  dim = 0;
338  if ( !done )
339  {
340  while ( ( dim < this->ImageIteratorDimension - 1 )
341  && ( ind[dim] > startIndex[dim] + static_cast< OffsetValueType >( size[dim] ) - 1 ) )
342  {
343  ind[dim] = startIndex[dim];
344  ind[++dim]++;
345  }
346  }
347  this->m_Offset = this->m_Image->ComputeOffset(ind);
348  m_SpanBeginOffset = this->m_Offset;
349  m_SpanEndOffset = this->m_Offset - static_cast< OffsetValueType >( size[0] );
350  }
351  return *this;
352  }
353 
354 protected:
355  SizeValueType m_SpanBeginOffset; // offset to last pixel in the row
356  SizeValueType m_SpanEndOffset; // offset to one pixel before the row
357 };
358 } // end namespace itk
359 
360 #endif
typename PixelContainer::Pointer PixelContainerPointer
typename Superclass::PixelContainer PixelContainer
unsigned long SizeValueType
Definition: itkIntTypes.h:83
ImageRegionReverseConstIterator(const ImageReverseConstIterator< TImage > &it)
A multi-dimensional image iterator designed to walk a specified image region in reverse.
typename TImage::PixelContainer PixelContainer
typename TImage::AccessorType AccessorType
ImageRegionReverseConstIterator(const ImageConstIterator< TImage > &it)
A multi-dimensional image iterator templated over image type.
typename TImage::InternalPixelType InternalPixelType
typename Superclass::SizeType SizeType
ImageRegionReverseConstIterator(const ImageRegionIterator< TImage > &it)
typename Superclass::IndexType IndexType
typename TImage::IndexType IndexType
typename Superclass::InternalPixelType InternalPixelType
Multi-dimensional image iterator.
typename TImage::SizeType SizeType
ImageRegionReverseConstIterator(const ImageType *ptr, const RegionType &region)
signed long OffsetValueType
Definition: itkIntTypes.h:94
A multi-dimensional iterator templated over image type that walks a region of pixels.