KWStyle - itkImageReverseConstIterator.h
 
Matrix View
Description

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

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