KWStyle - itkImageRegionReverseConstIterator.h
 
Matrix View
Description

1 /*=========================================================================
2
3   Program:   Insight Segmentation & Registration Toolkit
4   Module:    $RCSfile: itkImageRegionReverseConstIterator.h.html,v $
5   Language:  C++
6   Date:      $Date: 2006/01/17 19:15:39 $
7   Version:   $Revision: 1.4 $
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 __itkImageRegionReverseConstIterator_h
18 #define __itkImageRegionReverseConstIterator_h
19
20 #include "itkImageReverseConstIterator.h"
21
22 namespace itk
23 {
24
25 /**
26  * \class ImageRegionReverseConstIterator
27  * \brief A multi-dimensional image iterator designed to walk a specified image
28  * region in reverse.
29  * 
30  * ImageRegionReverseConstIterator is a templated class to represent a
31  * multi-dimensional iterator. ImageRegionReverseConstIterator is templated
32  * over the dimension of the image and the data type of the
33  * image. ImageRegionReverseConstIterator is constrained to walk only
34  * within the specified region.
35  *
36  * ImageRegionReverseConstIterator is a multi-dimensional iterator,
37  * requiring more information be specified before the iterator can be
38  * used than conventional iterators. Whereas the std::vector::iterator
39  * from the STL only needs to be passed a pointer to establish the
40  * iterator, the multi-dimensional image iterator needs a pointer, the
41  * size of the buffer, the size of the region, the start index of the
42  * buffer, and the start index of the region. To gain access to this
43  * information, ImageRegionReverseConstIterator holds a reference to the
44  * image over which it is traversing.
45  *
46  * ImageRegionReverseConstIterator assumes a particular layout of the image
47  * data. The is arranged in a 1D array as if it were
48  * [][][][slice][row][col] with Index[0] = col, Index[1] = row,
49  * Index[2] = slice, etc.
50  *
51  * operator++ provides a simple syntax for walking around a region of
52  * a multidimensional image. For a reverse iterator, operator++ moves
53  * backwards through the region. operator++ iterates across a row,
54  * constraining the movement to within a region of image. When the
55  * iterator reaches the boundary of the region along a row, the
56  * iterator automatically wraps to the previous row, starting at the last
57  * pixel in the row that is part of the region. This allows for simple
58  * processing loops of the form:
59  *
60  * \code
61  *
62  *      it = it.Begin();
63  *      for (; !it.IsAtEnd(); ++it)
64  *         {
65 IND **         *it += 100.0;
66 IND **         }
67  *
68  *  \endcode
69  *
70  * \par MORE INFORMATION
71  * For a complete description of the ITK Image Iterators and their API, please
72  * see the Iterators chapter in the ITK Software Guide.  The ITK Software Guide
73  * is available in print and as a free .pdf download from http://www.itk.org.
74  *
75  * \ingroup ImageIterators
76  *
77  * \sa ImageConstIterator \sa ConditionalConstIterator
78  * \sa ConstNeighborhoodIterator \sa ConstShapedNeighborhoodIterator
79  * \sa ConstSliceIterator  \sa CorrespondenceDataStructureIterator 
80  * \sa FloodFilledFunctionConditionalConstIterator 
81  * \sa FloodFilledImageFunctionConditionalConstIterator 
82  * \sa FloodFilledImageFunctionConditionalIterator 
83  * \sa FloodFilledSpatialFunctionConditionalConstIterator 
84  * \sa FloodFilledSpatialFunctionConditionalIterator 
85  * \sa ImageConstIterator \sa ImageConstIteratorWithIndex 
86  * \sa ImageIterator \sa ImageIteratorWithIndex
87  * \sa ImageLinearConstIteratorWithIndex  \sa ImageLinearIteratorWithIndex 
88  * \sa ImageRandomConstIteratorWithIndex  \sa ImageRandomIteratorWithIndex 
89  * \sa ImageRegionConstIterator \sa ImageRegionConstIteratorWithIndex 
90  * \sa ImageRegionExclusionConstIteratorWithIndex 
91  * \sa ImageRegionExclusionIteratorWithIndex 
92  * \sa ImageRegionIterator  \sa ImageRegionIteratorWithIndex 
93  * \sa ImageRegionReverseConstIterator  \sa ImageRegionReverseIterator 
94  * \sa ImageReverseConstIterator  \sa ImageReverseIterator 
95  * \sa ImageSliceConstIteratorWithIndex  \sa ImageSliceIteratorWithIndex 
96  * \sa NeighborhoodIterator \sa PathConstIterator  \sa PathIterator 
97  * \sa ShapedNeighborhoodIterator  \sa SliceIterator 
98  * \sa ImageConstIteratorWithIndex */
99 template<typename TImage>
100 LEN class ITK_EXPORT ImageRegionReverseConstIterator : public ImageReverseConstIterator<TImage>
101 {
102 public:
103   /** Standard class typedefs. */
104   typedef ImageRegionReverseConstIterator Self;
105 TDA   typedef ImageReverseConstIterator<TImage>  Superclass;
106   
107   /** Dimension of the image the iterator walks.  This enum is needed so that
108    * functions that are templated over image iterator type (as opposed to
109    * being templated over pixel type and dimension) can have compile time
110    * access to the dimension of the image that the iterator walks. */
111   enum { ImageIteratorDimension = Superclass::ImageIteratorDimension };
112
113   /** Index typedef support. While this was already typdef'ed in the superclass
114 LEN    * it needs to be redone here for this subclass to compile properly with gcc. */
115   typedef typename Superclass::IndexType IndexType;
116 TDA   typedef typename Superclass::IndexValueType IndexValueType;
117   
118   /** Size typedef support. While this was already typdef'ed in the superclass
119 LEN    * it needs to be redone here for this subclass to compile properly with gcc. */
120   typedef typename Superclass::SizeType SizeType;
121 TDA   typedef typename Superclass::SizeValueType SizeValueType;
122   
123   /** Offset typedef support. While this was already typdef'ed in the superclass
124 LEN    * it needs to be redone here for this subclass to compile properly with gcc. */
125   typedef typename Superclass::OffsetType OffsetType;
126 TDA   typedef typename Superclass::OffsetValueType OffsetValueType;
127   
128   /** Region typedef support. */
129   typedef typename Superclass::RegionType   RegionType;
130
131   /** Image typedef support. While this was already typdef'ed in the superclass
132 LEN    * it needs to be redone here for this subclass to compile properly with gcc. */
133   typedef typename Superclass::ImageType ImageType;
134
135   /** PixelContainer typedef support. Used to refer to the container for
136    * the pixel data. While this was already typdef'ed in the superclass
137 LEN    * it needs to be redone here for this subclass to compile properly with gcc. */
138   typedef typename Superclass::PixelContainer PixelContainer;
139 TDA   typedef typename PixelContainer::Pointer PixelContainerPointer;
140   
141   /** Internal Pixel Type */
142   typedef typename Superclass::InternalPixelType   InternalPixelType;
143
144   /** External Pixel Type */
145   typedef typename Superclass::PixelType   PixelType;
146
147   /**  Accessor type that convert data between internal and external
148    *  representations. */
149   typedef typename Superclass::AccessorType     AccessorType;
150
151   /** Run-time type information (and related methods). */
152   itkTypeMacro(ImageRegionReverseConstIterator, ImageReverseConstIterator);
153
154   /** Default constructor. Needed since we provide a cast constructor. */
155   ImageRegionReverseConstIterator() : Superclass()
156 IND **{
157     m_SpanBeginOffset = 0;
158     m_SpanEndOffset = 0;
159 IND **}
160   
161   /** Constructor establishes an iterator to walk a particular image and a
162    * particular region of that image. */
163   ImageRegionReverseConstIterator(ImageType *ptr,
164                       const RegionType ®ion)
165 IND ****: Superclass(ptr, region)
166 IND **{
167     m_SpanBeginOffset = this->m_BeginOffset;
168 LEN     m_SpanEndOffset = this->m_BeginOffset - static_cast<long>(this->m_Region.GetSize()[0]);
169 IND **}
170
171   /** Constructor that can be used to cast from an ImageIterator to an
172    * ImageRegionReverseConstIterator. Many routines return an ImageIterator
173    * but for a particular task, you may want an
174    * ImageRegionReverseConstIterator.  Rather than provide overloaded APIs
175    * that return different types of Iterators, itk returns
176    * ImageIterators and uses constructors to cast from an
177    * ImageIterator to a ImageRegionReverseConstIterator. */
178 LEN   ImageRegionReverseConstIterator( const ImageConstIterator<TImage> &it):Superclass(it)
179 IND **{
180     IndexType ind = this->GetIndex();
181 LEN     m_SpanBeginOffset = this->m_Offset + static_cast<long>(this->m_Region.GetSize()[0]) 
182 IND ******- (ind[0] - this->m_Region.GetIndex()[0]);
183 LEN     m_SpanEndOffset = m_SpanBeginOffset - static_cast<long>(this->m_Region.GetSize()[0]);
184 IND **}
185
186   /** Constructor that takes in a reverse image iterator.  This can be used
187    * to cast between the various types of reverse image iterators. */
188 LEN   ImageRegionReverseConstIterator( const ImageReverseConstIterator<TImage> &it):Superclass(it)
189 IND **{
190     IndexType ind = this->GetIndex();
191 LEN     m_SpanBeginOffset = this->m_Offset + static_cast<long>(this->m_Region.GetSize()[0]) 
192 IND ******- (ind[0] - this->m_Region.GetIndex()[0]);
193 LEN     m_SpanEndOffset = m_SpanBeginOffset - static_cast<long>(this->m_Region.GetSize()[0]);
194 IND **}
195
196 IND ***/** Constructor that takes in an image region iterator.  This can be used
197    * to cast between the various types of reverse image iterators. */
198 LEN   ImageRegionReverseConstIterator( const ImageRegionIterator<TImage> &it):Superclass(it)
199 IND **{
200     IndexType ind = this->GetIndex();
201 LEN     m_SpanBeginOffset = this->m_Offset + static_cast<long>(this->m_Region.GetSize()[0]) 
202 IND ******- (ind[0] - this->m_Region.GetIndex()[0]);
203 LEN     m_SpanEndOffset = m_SpanBeginOffset - static_cast<long>(this->m_Region.GetSize()[0]);
204 IND **}
205
206 IND */** Move an iterator to the beginning of the region. "Begin" for a reverse
207 IND *** iterator is the last pixel in the region. */
208   void GoToBegin()
209 IND **{
210     Superclass::GoToBegin();
211
212     // reset the span offsets
213     m_SpanBeginOffset = this->m_BeginOffset;
214     m_SpanEndOffset   = this->m_BeginOffset
215 IND ******- static_cast<long>(this->m_Region.GetSize()[0]);
216 IND **};
217   
218 IND */** Move an iterator to the end of the region. "End" for a reverse iterator
219 IND *** is defined as one pixel before the first pixel in the region. */
220   void GoToEnd()
221 IND **{
222     Superclass::GoToEnd();
223     
224     // reset the span offsets
225     m_SpanEndOffset = this->m_EndOffset;
226     m_SpanBeginOffset = m_SpanEndOffset
227 IND ******+ static_cast<long>(this->m_Region.GetSize()[0]);
228 IND **};
229
230   /** Return an iterator for the beginning of the region. "Begin" for a reverse
231    * iterator is the last pixel in the region.
232    * \deprecated Use GoToBegin() instead */
233   Self Begin(void) const;
234
235   /** Return an iterator for the end of the region. "End" for a
236 IND *** reverse iterator is one pixel before the first pixel in the
237 IND *** region.
238 IND *** \deprecated Use GoToEnd() instead */
239   Self End(void) const;
240
241
242   /** Set the index. No bounds checking is performed. This is overridden
243    * from the parent because we have an extra ivar.
244    * \sa GetIndex */
245   void SetIndex(const IndexType &ind)
246 IND **{ Superclass::SetIndex(ind);
247 LEN     m_SpanBeginOffset = this->m_Offset + static_cast<long>(this->m_Region.GetSize()[0]) 
248 IND ******- (ind[0] - this->m_Region.GetIndex()[0]);
249 LEN     m_SpanEndOffset = m_SpanBeginOffset - static_cast<long>(this->m_Region.GetSize()[0]);
250 IND **}
251   
252   /** iterator's index.  For a reverse iterator, this moves backwards
253    * through the region.  This operator will constrain the iterator
254    * within the region (i.e. the iterator will automatically wrap from
255    * the start of the row of the region to the end of the previous row
256    * of the region) up until the iterator tries to moves before the
257    * first pixel of the region.  Here, the iterator will be set to be
258    * one pixel before the start of the region.
259    * \sa operator++(int). */
260   Self &
261   operator++()
262 IND **{
263     if (--this->m_Offset <= m_SpanEndOffset)
264       {
265       // We have past the beginning of the span (row), need to wrap around.
266
267       // First move forward one pixel, because we are going to use a different
268       // algorithm to compute the next pixel
269       this->m_Offset++;
270       
271       // Get the index of the first pixel on the span (row)
272       typename ImageConstIterator<TImage>::IndexType
273 LEN,IND ********ind = this->m_Image->ComputeIndex( static_cast<OffsetValueType>(this->m_Offset) );
274
275       const typename ImageConstIterator<TImage>::IndexType&
276 IND ********startIndex = this->m_Region.GetIndex();
277       const typename ImageConstIterator<TImage>::SizeType&
278 IND ********size = this->m_Region.GetSize();
279
280       // Deccrement along a row, then wrap at the beginning of the region row.
281       bool done;
282       unsigned int dim;
283
284       // Check to see if we are past the first pixel in the region
285       // Note that --ind[0] moves to the previous pixel along the row.
286       done = (--ind[0] == startIndex[0] - 1);
287       for (unsigned int i=1; done && i < ImageIteratorDimension; i++)
288         {
289         done = (ind[i] == startIndex[i]);
290         }
291       
292       // if the iterator is outside the region (but not past region begin) then
293       // we need to wrap around the region
294       dim = 0;
295       if (!done)
296         {
297         while ( (dim < ImageIteratorDimension - 1)
298                 && (ind[dim] < startIndex[dim]) )
299           {
300           ind[dim] = startIndex[dim] + static_cast<long>(size[dim]) - 1;
301           ind[++dim]--;
302           }
303         }
304       this->m_Offset = this->m_Image->ComputeOffset( ind );
305       m_SpanBeginOffset = this->m_Offset;
306       m_SpanEndOffset = m_SpanBeginOffset - static_cast<long>(size[0]);
307       }
308     return *this;
309 IND **}
310
311   /** Decrement (prefix) the fastest moving dimension of the iterator's index.
312    * For a reverse iterator, this moves forward through the region.
313    * This operator will constrain the iterator within the region (i.e. the
314    * iterator will automatically wrap from the end of the row of the region
315    * to the start of the next row of the region) up until the iterator
316    * tries to moves past the last pixel of the region.  Here, the iterator
317    * will be set to be one pixel past the end of the region.
318    * \sa operator--(int) */
319   Self & operator--()
320 IND **{
321     if (++this->m_Offset >= m_SpanBeginOffset)
322       {
323       // We have reached the end of the span (row), need to wrap around.
324
325       // First back up one pixel, because we are going to use a different
326       // algorithm to compute the next pixel
327       --this->m_Offset;
328       
329       // Get the index of the last pixel on the span (row)
330       typename ImageConstIterator<TImage>::IndexType
331 LEN,IND ********ind = this->m_Image->ComputeIndex( static_cast<OffsetValueType>(this->m_Offset) );
332
333       const typename ImageIterator<TImage>::IndexType&
334 IND ********startIndex = this->m_Region.GetIndex();
335       const typename ImageIterator<TImage>::SizeType&
336 IND ********size = this->m_Region.GetSize();
337
338       // Increment along a row, then wrap at the end of the region row.
339       bool done;
340       unsigned int dim;
341
342       // Check to see if we are past the last pixel in the region
343       // Note that ++ind[0] moves to the next pixel along the row.
344       done = (++ind[0] == startIndex[0] + static_cast<long>(size[0]));
345       for (unsigned int i=1; done && i < ImageIteratorDimension; i++)
346         {
347         done = (ind[i] == startIndex[i] + static_cast<long>(size[i]) - 1);
348         }
349       
350       // if the iterator is outside the region (but not past region end) then
351       // we need to wrap around the region
352       dim = 0;
353       if (!done)
354         {
355         while ( (dim < ImageIteratorDimension - 1)
356 LEN                 && (ind[dim] > startIndex[dim] + static_cast<long>(size[dim]) - 1) )
357           {
358           ind[dim] = startIndex[dim];
359           ind[++dim]++;
360           }
361         }
362       this->m_Offset = this->m_Image->ComputeOffset( ind );
363       m_SpanBeginOffset = this->m_Offset;
364       m_SpanEndOffset = this->m_Offset - static_cast<long>(size[0]);
365       }
366     return *this;
367 IND **}
368
369 protected:
370   unsigned long m_SpanBeginOffset;  // offset to last pixel in the row
371   unsigned long m_SpanEndOffset;  // offset to one pixel before the row
372        
373 };
374
375 // end namespace itk
376
377 #ifndef ITK_MANUAL_INSTANTIATION
378 #include "itkImageRegionReverseConstIterator.txx"
379 #endif
380
381 #endif 
382

Generated by KWStyle 1.0b on Tuesday January,17 at 02:14:14PM
© Kitware Inc.