KWStyle - itkImageRegionConstIterator.h
 
Matrix View
Description

1 /*=========================================================================
2
3   Program:   Insight Segmentation & Registration Toolkit
4   Module:    $RCSfile: itkImageRegionConstIterator.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 __itkImageRegionConstIterator_h
18 #define __itkImageRegionConstIterator_h
19
20 #include "itkImageConstIterator.h"
21 #include "itkImageIterator.h"
22
23 #if defined(_MSC_VER) && !defined(ITK_LEAN_AND_MEAN)
24 #define ITK_LEAN_AND_MEAN
25 #endif
26
27 namespace itk
28 {
29
30 /** \class ImageRegionConstIterator
31  * \brief A multi-dimensional iterator templated over image type that walks a
32  * region of pixels.
33  *
34  * The itk::ImageRegionConstIterator is optimized for iteration speed and is the
35  * first choice for iterative, pixel-wise operations on an image.
36  * ImageRegionIterator is the least specialized of the ITK image iterator
37  * classes.  ImageRegionConstIterator is templated over the image type, and is
38  * constrained to walk only within the specified region and along a line
39  * parallel to one of the coordinate axes, "wrapping" to the next line as it
40  * reaches the boundary of the image.  To walk the entire image, specify the
41  * region to be \c image->GetRequestedRegion().
42  *
43  * ImageRegionConstIterator provides read-only access to image data.  It is the
44  * base class for the read/write access ImageRegionIterator.
45  *
46  * ImageRegionConstIterator is a multi-dimensional iterator, requiring more
47  * information be specified before the iterator can be used than conventional
48  * iterators. Whereas the std::vector::iterator from the STL only needs to be
49  * passed a pointer to establish the iterator, the multi-dimensional image
50  * iterator needs a pointer, the size of the buffer, the size of the region,
51  * the start index of the buffer, and the start index of the region. To gain
52  * access to this information, ImageRegionConstIterator holds a reference to the
53  * image over which it is traversing.
54  *
55  * ImageRegionConstIterator assumes a particular layout of the image data. The
56  * is arranged in a 1D array as if it were [][][][slice][row][col] with
57  * Index[0] = col, Index[1] = row, Index[2] = slice, etc.
58  *
59  * operator++ provides a simple syntax for walking around a region of
60  * a multidimensional image. operator++ iterates across a row, constraining
61  * the movement to within a region of image. When the iterator reaches
62  * the boundary of the region along a row, the iterator automatically
63  * wraps to the next row, starting at the first pixel in the row that is
64  * part of the region. This allows for simple processing loops of the form:
65  *
66  * \code
67  *
68  *      it = it.Begin();
69  *      for (; !it.IsAtEnd(); ++it)
70  *         {
71 IND **         *it += 100.0;
72 IND **         }
73  *
74  * \endcode
75  *
76  *
77  * \par MORE INFORMATION
78  * For a complete description of the ITK Image Iterators and their API, please
79  * see the Iterators chapter in the ITK Software Guide.  The ITK Software Guide
80  * is available in print and as a free .pdf download from http://www.itk.org.
81  *
82  * \ingroup ImageIterators
83  * \example Iterators/ImageRegionIterator.cxx
84  *
85  * \sa ImageConstIterator \sa ConditionalConstIterator
86  * \sa ConstNeighborhoodIterator \sa ConstShapedNeighborhoodIterator
87  * \sa ConstSliceIterator  \sa CorrespondenceDataStructureIterator 
88  * \sa FloodFilledFunctionConditionalConstIterator 
89  * \sa FloodFilledImageFunctionConditionalConstIterator 
90  * \sa FloodFilledImageFunctionConditionalIterator 
91  * \sa FloodFilledSpatialFunctionConditionalConstIterator 
92  * \sa FloodFilledSpatialFunctionConditionalIterator 
93  * \sa ImageConstIterator \sa ImageConstIteratorWithIndex 
94  * \sa ImageIterator \sa ImageIteratorWithIndex
95  * \sa ImageLinearConstIteratorWithIndex  \sa ImageLinearIteratorWithIndex 
96  * \sa ImageRandomConstIteratorWithIndex  \sa ImageRandomIteratorWithIndex 
97  * \sa ImageRegionConstIterator \sa ImageRegionConstIteratorWithIndex 
98  * \sa ImageRegionExclusionConstIteratorWithIndex 
99  * \sa ImageRegionExclusionIteratorWithIndex 
100  * \sa ImageRegionIterator  \sa ImageRegionIteratorWithIndex 
101  * \sa ImageRegionReverseConstIterator  \sa ImageRegionReverseIterator 
102  * \sa ImageReverseConstIterator  \sa ImageReverseIterator 
103  * \sa ImageSliceConstIteratorWithIndex  \sa ImageSliceIteratorWithIndex 
104  * \sa NeighborhoodIterator \sa PathConstIterator  \sa PathIterator 
105  * \sa ShapedNeighborhoodIterator  \sa SliceIterator 
106  * \sa ImageConstIteratorWithIndex */
107 template<typename TImage>
108 class ITK_EXPORT ImageRegionConstIterator : public ImageConstIterator<TImage>
109 {
110 public:
111   /** Standard class typedef. */
112   typedef ImageRegionConstIterator Self;
113 TDA   typedef ImageConstIterator<TImage>  Superclass;
114   
115   /** Dimension of the image the iterator walks.  This constant is needed so
116    * functions that are templated over image iterator type (as opposed to
117    * being templated over pixel type and dimension) can have compile time
118    * access to the dimension of the image that the iterator walks. */
119   itkStaticConstMacro(ImageIteratorDimension, unsigned int,
120                       Superclass::ImageIteratorDimension);
121
122   /** Index typedef support. While this was already typdef'ed in the superclass
123 LEN    * it needs to be redone here for this subclass to compile properly with gcc. */
124   typedef typename Superclass::IndexType IndexType;
125
126   /** Size typedef support. While this was already typdef'ed in the superclass
127 LEN    * it needs to be redone here for this subclass to compile properly with gcc. */
128   typedef typename Superclass::SizeType SizeType;
129
130   /** Region typedef support. */
131   typedef typename Superclass::RegionType   RegionType;
132
133   /** Image typedef support. While this was already typdef'ed in the superclass
134 LEN    * it needs to be redone here for this subclass to compile properly with gcc. */
135   typedef typename Superclass::ImageType ImageType;
136
137   /** PixelContainer typedef support. Used to refer to the container for
138    * the pixel data. While this was already typdef'ed in the superclass
139 LEN    * it needs to be redone here for this subclass to compile properly with gcc. */
140   typedef typename Superclass::PixelContainer         PixelContainer;
141   typedef typename Superclass::PixelContainerPointer  PixelContainerPointer;
142   
143   /** Internal Pixel Type */
144   typedef typename Superclass::InternalPixelType   InternalPixelType;
145
146   /** External Pixel Type */
147   typedef typename Superclass::PixelType   PixelType;
148
149   /**  Accessor type that convert data between internal and external
150    *  representations. */
151   typedef typename Superclass::AccessorType     AccessorType;
152
153   /** Run-time type information (and related methods). */
154   itkTypeMacro(ImageRegionConstIterator, ImageIterator);
155
156   /** Default constructor. Needed since we provide a cast constructor. */
157   ImageRegionConstIterator() : ImageConstIterator<TImage>()
158 IND **{
159     m_SpanBeginOffset = 0;
160     m_SpanEndOffset = 0;
161 IND **}
162   
163   /** Constructor establishes an iterator to walk a particular image and a
164    * particular region of that image. */
165   ImageRegionConstIterator(const ImageType *ptr,
166                       const RegionType ®ion)
167 IND ****: ImageConstIterator<TImage>(ptr, region)
168 IND **{
169     m_SpanBeginOffset = this->m_BeginOffset;
170 LEN     m_SpanEndOffset   = this->m_BeginOffset + static_cast<long>(this->m_Region.GetSize()[0]);
171 IND **}
172
173   /** Constructor that can be used to cast from an ImageIterator to an
174    * ImageRegionConstIterator. Many routines return an ImageIterator but for a
175    * particular task, you may want an ImageRegionConstIterator.  Rather than
176    * provide overloaded APIs that return different types of Iterators, itk
177    * returns ImageIterators and uses constructors to cast from an
178    * ImageIterator to a ImageRegionConstIterator. */
179   ImageRegionConstIterator( const ImageIterator<TImage> &it)
180 IND **{
181     this->ImageIterator<TImage>::operator=(it);
182     IndexType ind = this->GetIndex();
183 LEN     m_SpanEndOffset = this->m_Offset + static_cast<long>(this->m_Region.GetSize()[0]) 
184 IND ******- (ind[0] - this->m_Region.GetIndex()[0]);
185     m_SpanBeginOffset = m_SpanEndOffset
186 IND ******- static_cast<long>(this->m_Region.GetSize()[0]);
187 IND **}
188
189   /** Constructor that can be used to cast from an ImageConstIterator to an
190    * ImageRegionConstIterator. Many routines return an ImageIterator but for a
191    * particular task, you may want an ImageRegionConstIterator.  Rather than
192    * provide overloaded APIs that return different types of Iterators, itk
193    * returns ImageIterators and uses constructors to cast from an
194    * ImageIterator to a ImageRegionConstIterator. */
195   ImageRegionConstIterator( const ImageConstIterator<TImage> &it)
196 IND **{
197     this->ImageConstIterator<TImage>::operator=(it);
198     IndexType ind = this->GetIndex();
199 LEN     m_SpanEndOffset = this->m_Offset + static_cast<long>(this->m_Region.GetSize()[0]) 
200 IND ******- (ind[0] - this->m_Region.GetIndex()[0]);
201     m_SpanBeginOffset = m_SpanEndOffset
202 IND ******- static_cast<long>(this->m_Region.GetSize()[0]);
203 IND **}
204
205 IND */** Move an iterator to the beginning of the region. "Begin" is
206 IND *** defined as the first pixel in the region. */
207   void GoToBegin()
208 IND **{
209     Superclass::GoToBegin();
210
211     // reset the span offsets
212     m_SpanBeginOffset = this->m_BeginOffset;
213     m_SpanEndOffset   = this->m_BeginOffset
214 IND ******+ static_cast<long>(this->m_Region.GetSize()[0]);
215 IND **};
216   
217 IND */** Move an iterator to the end of the region. "End" is defined as
218 IND *** one pixel past the last pixel of the region. */
219   void GoToEnd()
220 IND **{
221     Superclass::GoToEnd();
222     
223     // reset the span offsets
224     m_SpanEndOffset = this->m_EndOffset;
225     m_SpanBeginOffset = m_SpanEndOffset
226 IND ******- static_cast<long>(this->m_Region.GetSize()[0]);
227 IND **};
228
229   /** Return an iterator for the beginning of the region. "Begin"
230    * is defined as the first pixel in the region.
231    * \deprecated Use GoToBegin() instead */
232   Self Begin(void) const;
233
234 IND ***/** Return an iterator for the end of the region. "End" is defined
235    * as one pixel past the last pixel of the region. 
236    * \deprecated Use GoToEnd() instead */
237   Self End(void) const;
238
239
240   /** Set the index. No bounds checking is performed. This is overridden
241    * from the parent because we have an extra ivar.
242    * \sa GetIndex */
243   void SetIndex(const IndexType &ind)
244 IND **{ Superclass::SetIndex(ind);
245 LEN     m_SpanEndOffset = this->m_Offset + static_cast<long>(this->m_Region.GetSize()[0]) 
246 IND ******- (ind[0] - this->m_Region.GetIndex()[0]);
247     m_SpanBeginOffset = m_SpanEndOffset
248 IND ******- static_cast<long>(this->m_Region.GetSize()[0]);
249 IND **}
250   
251   /** Increment (prefix) the fastest moving dimension of the iterator's index.
252    * This operator will constrain the iterator within the region (i.e. the
253    * iterator will automatically wrap from the end of the row of the region
254    * to the beginning of the next row of the region) up until the iterator
255    * tries to moves past the last pixel of the region.  Here, the iterator
256    * will be set to be one pixel past the end of the region.
257    * \sa operator++(int) */
258   Self &
259   operator++()
260 IND **{
261     if (++this->m_Offset >= m_SpanEndOffset)
262       {
263       this->Increment();
264       }
265     return *this;
266 IND **}
267
268   /** Decrement (prefix) the fastest moving dimension of the iterator's index.
269    * This operator will constrain the iterator within the region (i.e. the
270 LEN    * iterator will automatically wrap from the beginning of the row of the region
271    * to the end of the next row of the region) up until the iterator
272    * tries to moves past the first pixel of the region.  Here, the iterator
273    * will be set to be one pixel past the beginning of the region.
274    * \sa operator--(int) */
275   Self & operator--()
276 IND **{
277     if (--this->m_Offset < m_SpanBeginOffset)
278       {
279       this->Decrement();
280       }
281     return *this;
282 IND **}
283
284 private:
285   void Increment(); // advance in a direction other than the fastest moving
286   void Decrement(); // go back in a direction other than the fastest moving
287   
288 protected:
289 LEN   unsigned long m_SpanBeginOffset;  // one pixel before the beginning of the span (row)
290   unsigned long m_SpanEndOffset;  // one pixel past the end of the span (row)
291        
292 };
293
294 // end namespace itk
295
296 #ifndef ITK_MANUAL_INSTANTIATION
297 #include "itkImageRegionConstIterator.txx"
298 #endif
299
300 #if defined(_MSC_VER)
301 #undef ITK_LEAN_AND_MEAN
302 #endif
303
304 #endif 
305

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