KWStyle - itkImageConstIterator.h
 
Matrix View
Description

1 /*=========================================================================
2
3   Program:   Insight Segmentation & Registration Toolkit
4   Module:    $RCSfile: itkImageConstIterator.h.html,v $
5   Language:  C++
6   Date:      $Date: 2006/01/17 19:15:36 $
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 __itkImageConstIterator_h
18 #define __itkImageConstIterator_h
19
20 #include "itkImage.h"
21 #include "itkIndex.h"
22 #include "itkSize.h"
23 #include "itkOffset.h"
24
25 namespace itk
26 {
27
28 /** \class ImageConstIterator
29  * \brief A multi-dimensional image iterator templated over image type.
30  * 
31  * ImageConstIterator is a templated class to represent a multi-dimensional
32  * iterator. ImageConstIterator is templated over the type of
33  * the image to be iterated over.
34  *
35  * ImageConstIterator is a base class for all the image iterators. It provides
36  * the basic construction and comparison operations.  However, it does not
37  * provide mechanisms for moving the iterator.  A subclass of ImageConstIterator
38  * must be used to move the iterator.
39  * 
40 LEN  * ImageConstIterator is a multi-dimensional iterator, requiring more information
41  * be specified before the iterator can be used than conventional iterators.
42  * Whereas the std::vector::iterator from the STL only needs to be passed
43  * a pointer to establish the iterator, the multi-dimensional image iterator
44  * needs a pointer, the size of the buffer, the size of the region, the
45  * start index of the buffer, and the start index of the region. To gain
46  * access to this information, ImageConstIterator holds a reference to the image
47  * over which it is traversing.
48  *
49 LEN  * ImageConstIterator assumes a particular layout of the image data. In particular,
50  * the data is arranged in a 1D array as if it were [][][][slice][row][col]
51  * with Index[0] = col, Index[1] = row, Index[2] = slice, etc.
52  *
53  * \ingroup ImageConstIterators
54  * \par MORE INFORMATION
55  * For a complete description of the ITK Image Iterators and their API, please
56  * see the Iterators chapter in the ITK Software Guide.  The ITK Software Guide
57  * is available in print and as a free .pdf download from http://www.itk.org.
58  *
59  * \ingroup ImageIterators
60  *
61  * \sa ImageConstIterator \sa ConditionalConstIterator
62  * \sa ConstNeighborhoodIterator \sa ConstShapedNeighborhoodIterator
63  * \sa ConstSliceIterator  \sa CorrespondenceDataStructureIterator 
64  * \sa FloodFilledFunctionConditionalConstIterator 
65  * \sa FloodFilledImageFunctionConditionalConstIterator 
66  * \sa FloodFilledImageFunctionConditionalIterator 
67  * \sa FloodFilledSpatialFunctionConditionalConstIterator 
68  * \sa FloodFilledSpatialFunctionConditionalIterator 
69  * \sa ImageConstIterator \sa ImageConstIteratorWithIndex 
70  * \sa ImageIterator \sa ImageIteratorWithIndex
71  * \sa ImageLinearConstIteratorWithIndex  \sa ImageLinearIteratorWithIndex 
72  * \sa ImageRandomConstIteratorWithIndex  \sa ImageRandomIteratorWithIndex 
73  * \sa ImageRegionConstIterator \sa ImageRegionConstIteratorWithIndex 
74  * \sa ImageRegionExclusionConstIteratorWithIndex 
75  * \sa ImageRegionExclusionIteratorWithIndex 
76  * \sa ImageRegionIterator  \sa ImageRegionIteratorWithIndex 
77  * \sa ImageRegionReverseConstIterator  \sa ImageRegionReverseIterator 
78  * \sa ImageReverseConstIterator  \sa ImageReverseIterator 
79  * \sa ImageSliceConstIteratorWithIndex  \sa ImageSliceIteratorWithIndex 
80  * \sa NeighborhoodIterator \sa PathConstIterator  \sa PathIterator 
81  * \sa ShapedNeighborhoodIterator  \sa SliceIterator 
82  */
83 template<typename TImage>
84 class ITK_EXPORT ImageConstIterator
85 {
86 public:
87   /** Standard class typedefs. */
88   typedef ImageConstIterator Self;
89   
90   /** Dimension of the image the iterator walks.  This constant is needed so 
91    * functions that are templated over image iterator type (as opposed to
92    * being templated over pixel type and dimension) can have compile time
93    * access to the dimension of the image that the iterator walks. */
94   itkStaticConstMacro(ImageIteratorDimension, unsigned int,
95                       TImage::ImageDimension);
96
97   /** Index typedef support. */
98   typedef typename TImage::IndexType  IndexType;
99 TDA   typedef typename TImage::IndexValueType  IndexValueType;
100   
101   /** Size typedef support. */
102   typedef typename TImage::SizeType    SizeType;
103 TDA   typedef typename TImage::SizeValueType  SizeValueType;
104     
105   /** Offset typedef support. */
106   typedef typename TImage::OffsetType    OffsetType;
107 TDA   typedef typename TImage::OffsetValueType  OffsetValueType;
108     
109   /** Region typedef support. */
110   typedef typename TImage::RegionType   RegionType;
111
112   /** Image typedef support. */
113   typedef TImage   ImageType;
114
115   /** PixelContainer typedef support. Used to refer to the container for
116    * the pixel data. While this was already typdef'ed in the superclass
117 LEN    * it needs to be redone here for this subclass to compile properly with gcc. */
118   typedef typename TImage::PixelContainer PixelContainer;
119 TDA   typedef typename PixelContainer::Pointer PixelContainerPointer;
120   
121   /** Internal Pixel Type */
122   typedef typename TImage::InternalPixelType   InternalPixelType;
123
124   /** External Pixel Type */
125   typedef typename TImage::PixelType   PixelType;
126
127   /**  Accessor type that convert data between internal and external
128    *  representations. */
129   typedef typename TImage::AccessorType            AccessorType;
130   typedef typename TImage::AccessorFunctorType     AccessorFunctorType;
131
132   /** Default Constructor. Need to provide a default constructor since we
133    * provide a copy constructor. */
134   ImageConstIterator()
135 IND ****: m_Region(),
136 IND ******m_PixelAccessor(),
137 IND ******m_PixelAccessorFunctor()
138 IND **{
139     m_Image = 0;
140     m_Buffer = 0;
141     m_Offset = 0;
142     m_BeginOffset = 0;
143     m_EndOffset = 0;
144     m_PixelAccessorFunctor.SetBegin( m_Buffer );
145 IND **}
146
147   /** Default Destructor. */
148   virtual ~ImageConstIterator() {};
149
150   /** Copy Constructor. The copy constructor is provided to make sure the
151    * handle to the image is properly reference counted. */
152   ImageConstIterator(const Self& it)
153 IND **{
154     m_Image = it.m_Image;     // copy the smart pointer
155
156     m_Region = it.m_Region;
157     
158     m_Buffer = it.m_Buffer;
159     m_Offset = it.m_Offset;
160     m_BeginOffset = it.m_BeginOffset;
161     m_EndOffset = it.m_EndOffset;
162     m_PixelAccessor = it.m_PixelAccessor;
163     m_PixelAccessorFunctor = it.m_PixelAccessorFunctor;
164     m_PixelAccessorFunctor.SetBegin( m_Buffer );
165 IND **}
166
167   /** Constructor establishes an iterator to walk a particular image and a
168    * particular region of that image. */
169   ImageConstIterator( const ImageType *ptr,
170                       const RegionType ®ion )
171 IND **{
172     m_Image = ptr;
173     m_Buffer = m_Image->GetBufferPointer();
174     m_Region = region;
175
176     // Compute the start offset
177     m_Offset = m_Image->ComputeOffset( m_Region.GetIndex() );
178     m_BeginOffset = m_Offset;
179     
180     // Compute the end offset. If any component of m_Region.GetSize()
181     // is zero, the region is not valid and we set the EndOffset
182     // to be same as BeginOffset so that iterator end condition is met
183     // immediately.
184     if (m_Region.GetNumberOfPixels() == 0)
185       {
186       // region is empty, probably has a size of 0 along one dimension
187       m_EndOffset = m_BeginOffset;
188       }
189     else
190       {
191       IndexType ind(m_Region.GetIndex());
192       SizeType size(m_Region.GetSize());
193       for (unsigned int i=0; i < ImageIteratorDimension; ++i)
194         {
195         ind[i] += (static_cast<IndexValueType>(size[i]) - 1);
196         }
197       m_EndOffset = m_Image->ComputeOffset( ind );
198       m_EndOffset++;
199       }
200
201     m_PixelAccessor = ptr->GetPixelAccessor();
202     m_PixelAccessorFunctor.SetPixelAccessor( m_PixelAccessor );
203     m_PixelAccessorFunctor.SetBegin( m_Buffer );
204 IND **}
205   
206   /** operator= is provided to make sure the handle to the image is properly
207    * reference counted. */
208   Self &operator=(const Self& it)
209 IND **{
210     m_Image = it.m_Image;     // copy the smart pointer
211     m_Region = it.m_Region;
212     
213     m_Buffer = it.m_Buffer;
214     m_Offset = it.m_Offset;
215     m_BeginOffset = it.m_BeginOffset;
216     m_EndOffset = it.m_EndOffset;
217     m_PixelAccessor = it.m_PixelAccessor;
218     m_PixelAccessorFunctor = it.m_PixelAccessorFunctor;
219     m_PixelAccessorFunctor.SetBegin( m_Buffer );
220
221     return *this;
222 IND **}
223   
224   /** Get the dimension (size) of the index. */
225   static unsigned int GetImageIteratorDimension() 
226     {return ImageIteratorDimension;}
227
228   /** Comparison operator. Two iterators are the same if they "point to" the
229    * same memory location */
230   bool
231   operator!=(const Self &it) const
232     {
233     // two iterators are the same if they "point to" the same memory location
234     return (m_Buffer + m_Offset) != (it.m_Buffer + it.m_Offset);
235     };
236
237   /** Comparison operator. Two iterators are the same if they "point to" the
238    * same memory location */
239   bool
240   operator==(const Self &it) const
241     {
242     // two iterators are the same if they "point to" the same memory location
243     return (m_Buffer + m_Offset) == (it.m_Buffer + it.m_Offset);
244     };
245   
246   /** Comparison operator. An iterator is "less than" another if it "points to"
247    * a lower memory location. */
248   bool
249   operator<=(const Self &it) const
250     {
251     // an iterator is "less than" another if it "points to" a lower
252     // memory location
253     return (m_Buffer + m_Offset) <= (it.m_Buffer + it.m_Offset);
254     };
255
256   /** Comparison operator. An iterator is "less than" another if it "points to"
257    * a lower memory location. */
258   bool
259   operator<(const Self &it) const
260     {
261     // an iterator is "less than" another if it "points to" a lower
262     // memory location
263     return (m_Buffer + m_Offset) < (it.m_Buffer + it.m_Offset);
264     };
265
266   /** Comparison operator. An iterator is "greater than" another if it
267    * "points to" a higher location. */
268   bool
269   operator>=(const Self &it) const
270     {
271     // an iterator is "greater than" another if it "points to" a higher
272     // memory location
273     return (m_Buffer + m_Offset) >= (it.m_Buffer + it.m_Offset);
274     };
275
276   /** Comparison operator. An iterator is "greater than" another if it
277    * "points to" a higher location. */
278   bool
279   operator>(const Self &it) const
280     {
281     // an iterator is "greater than" another if it "points to" a higher
282     // memory location
283     return (m_Buffer + m_Offset) > (it.m_Buffer + it.m_Offset);
284     };
285
286   /** Get the index. This provides a read only reference to the index.
287    * This causes the index to be calculated from pointer arithmetic and is
288    * therefore an expensive operation.
289    * \sa SetIndex */
290   const IndexType GetIndex() const
291     { return m_Image->ComputeIndex( static_cast<OffsetValueType>(m_Offset) );  }
292
293   /** Set the index. No bounds checking is performed.
294    * \sa GetIndex */
295   virtual void SetIndex(const IndexType &ind)
296     { m_Offset = m_Image->ComputeOffset( ind ); }
297
298   /** Get the region that this iterator walks. ImageConstIterators know the
299    * beginning and the end of the region of the image to iterate over. */
300   const RegionType& GetRegion() const
301     { return m_Region; };
302
303   /** Get the image that this iterator walks. */
304   const ImageType * GetImage() const
305     { return m_Image.GetPointer(); };
306
307   /** Get the pixel value */
308   PixelType Get(void) const  
309     { return m_PixelAccessorFunctor.Get(*(m_Buffer+m_Offset)); }
310   
311   /** Return a const reference to the pixel 
312    * This method will provide the fastest access to pixel
313    * data, but it will NOT support ImageAdaptors. */
314   const PixelType & Value(void) const  
315     { return *(m_Buffer + m_Offset); }
316  
317   /** Return an iterator for the beginning of the region. "Begin"
318    * is defined as the first pixel in the region.
319    * \deprecated Use GoToBegin() instead.
320    */
321   Self Begin(void) const;
322
323 IND */** Move an iterator to the beginning of the region. "Begin" is
324 IND *** defined as the first pixel in the region. */
325   void GoToBegin()
326     {
327     m_Offset = m_BeginOffset;
328     };
329
330   /** Return an iterator for the end of the region. "End" is defined
331    * as one pixel past the last pixel of the region.
332    * \deprecated Use GoToEnd() instead.
333    */
334   Self End(void) const;
335
336 IND */** Move an iterator to the end of the region. "End" is defined as
337 IND *** one pixel past the last pixel of the region. */
338   void GoToEnd()
339     {
340     m_Offset = m_EndOffset;
341     };
342
343   /** Is the iterator at the beginning of the region? "Begin" is defined
344    * as the first pixel in the region. */
345   bool IsAtBegin(void) const
346     {
347     return (m_Offset == m_BeginOffset);
348     }
349
350   /** Is the iterator at the end of the region? "End" is defined as one
351    * pixel past the last pixel of the region. */
352   bool IsAtEnd(void) const
353     {
354     return (m_Offset == m_EndOffset);
355     }
356   
357
358 protected: //made protected so other iterators can access 
359   typename TImage::ConstWeakPointer  m_Image;
360   RegionType                     m_Region;      // region to iterate over
361   
362   unsigned long  m_Offset;
363   unsigned long  m_BeginOffset; // offset to first pixel in region
364   unsigned long  m_EndOffset;  // offset to one pixel past last pixel in region
365
366   const InternalPixelType      * m_Buffer;
367
368   AccessorType                   m_PixelAccessor;
369   AccessorFunctorType            m_PixelAccessorFunctor;
370 };
371
372 // end namespace itk
373
374 #ifndef ITK_MANUAL_INSTANTIATION
375 #include "itkImageConstIterator.txx"
376 #endif
377
378 #endif 
379

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