KWStyle - itkImageRandomConstIteratorWithIndex.h
 
Matrix View
Description

1 /*=========================================================================
2
3   Program:   Insight Segmentation & Registration Toolkit
4   Module:    $RCSfile: itkImageRandomConstIteratorWithIndex.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 __itkImageRandomConstIteratorWithIndex_h
18 #define __itkImageRandomConstIteratorWithIndex_h
19
20 #include "itkImageConstIteratorWithIndex.h"
21 #include "itkMersenneTwisterRandomVariateGenerator.h"
22
23 namespace itk
24 {
25
26 /** \class ImageRandomConstIteratorWithIndex
27  * \brief A multi-dimensional image iterator that visits a random set of pixels
28  * within an image region.
29  * 
30  * ImageRandomConstIteratorWithIndex is a multi-dimensional iterator class that
31  * is templated over image type.  ImageRandomConstIteratorWithIndex is
32  * constrained to walk  only within the specified region. It samples random
33  * pixel positions at each increment or decrement.
34  *
35 LEN  * ImageRandomConstIteratorWithIndex assumes a particular layout of the image data. The
36  * is arranged in a 1D array as if it were [][][][slice][row][col] with
37  * Index[0] = col, Index[1] = row, Index[2] = slice, etc.
38  *
39  * The operator++ method provides a simple syntax for walking around a region
40  * of a multidimensional image. operator++ performs a jump to a random position
41  * within the specified image region.  This is designed to facilitate the
42  * extraction of random samples from the image.
43  *
44  * This is the typical use of this iterator in a loop:
45  *
46  * \code
47  *  
48 LEN  * ImageRandomConstIteratorWithIndex<ImageType> it( image, image->GetRequestedRegion() );
49  * 
50  * it.SetNumberOfSamples(200);
51  * it.GoToBegin();
52  * while( !it.IsAtEnd() )
53  * {
54 IND **   it.Get();
55 IND **   ++it;  // here it jumps to another random position inside the region
56 IND **  } 
57  *
58  *  \endcode
59  *
60  * or
61  *
62  * \code
63  *  
64 LEN  * ImageRandomConstIteratorWithIndex<ImageType> it( image, image->GetRequestedRegion() );
65  * 
66  * it.SetNumberOfSamples(200);
67  * it.GoToEnd();
68  * while( !it.IsAtBegin() )
69  * {
70 IND **   it.Get();
71 IND **   --it;  // here it jumps to another random position inside the region
72 IND **  } 
73  *
74  *  \endcode
75  *
76  * \warning Incrementing the iterator (++it) followed by a decrement (--it)
77  * or vice versa does not in general return the iterator to the same position.
78  *
79  * \example  Examples/itkImageRandomConstIteratorWithIndex.cxx
80  *
81  * \par MORE INFORMATION
82  * For a complete description of the ITK Image Iterators and their API, please
83  * see the Iterators chapter in the ITK Software Guide.  The ITK Software Guide
84  * is available in print and as a free .pdf download from http://www.itk.org.
85  *
86  * \ingroup ImageIterators
87  *
88  * \sa ImageConstIterator \sa ConditionalConstIterator
89  * \sa ConstNeighborhoodIterator \sa ConstShapedNeighborhoodIterator
90  * \sa ConstSliceIterator  \sa CorrespondenceDataStructureIterator 
91  * \sa FloodFilledFunctionConditionalConstIterator 
92  * \sa FloodFilledImageFunctionConditionalConstIterator 
93  * \sa FloodFilledImageFunctionConditionalIterator 
94  * \sa FloodFilledSpatialFunctionConditionalConstIterator 
95  * \sa FloodFilledSpatialFunctionConditionalIterator 
96  * \sa ImageConstIterator \sa ImageConstIteratorWithIndex 
97  * \sa ImageIterator \sa ImageIteratorWithIndex
98  * \sa ImageLinearConstIteratorWithIndex  \sa ImageLinearIteratorWithIndex 
99  * \sa ImageRandomConstIteratorWithIndex  \sa ImageRandomIteratorWithIndex 
100  * \sa ImageRegionConstIterator \sa ImageRegionConstIteratorWithIndex 
101  * \sa ImageRegionExclusionConstIteratorWithIndex 
102  * \sa ImageRegionExclusionIteratorWithIndex 
103  * \sa ImageRegionIterator  \sa ImageRegionIteratorWithIndex 
104  * \sa ImageRegionReverseConstIterator  \sa ImageRegionReverseIterator 
105  * \sa ImageReverseConstIterator  \sa ImageReverseIterator 
106  * \sa ImageSliceConstIteratorWithIndex  \sa ImageSliceIteratorWithIndex 
107  * \sa NeighborhoodIterator \sa PathConstIterator  \sa PathIterator 
108  * \sa ShapedNeighborhoodIterator  \sa SliceIterator 
109  * \sa ImageConstIteratorWithIndex
110  *
111  */
112 template<typename TImage>
113 LEN class ITK_EXPORT ImageRandomConstIteratorWithIndex : public ImageConstIteratorWithIndex<TImage>
114 {
115 public:
116   /** Standard class typedefs. */
117   typedef ImageRandomConstIteratorWithIndex Self;
118 TDA   typedef ImageConstIteratorWithIndex<TImage>  Superclass;
119   
120   /** Index typedef support. While this was already typdef'ed in the superclass
121    * it needs to be redone here for this subclass to compile properly with gcc.
122    * Note that we have to rescope Index back to itk::Index to that is it not
123    * confused with ImageIterator::Index. */
124   typedef typename TImage::IndexType   IndexType;
125
126   /** Region typedef support. While this was already typdef'ed in the superclass
127    * it needs to be redone here for this subclass to compile properly with gcc.
128    * Note that we have to rescope Region back to itk::ImageRegion so that is
129    * it not confused with ImageIterator::Index. */
130   typedef typename TImage::RegionType RegionType;
131   
132   /** Image typedef support. While this was already typdef'ed in the superclass
133    * it needs to be redone here for this subclass to compile properly with gcc.
134    * Note that we have to rescope Index back to itk::Index to that is it not
135    * confused with ImageIterator::Index. */
136   typedef TImage ImageType;
137
138   /** PixelContainer typedef support. Used to refer to the container for
139    * the pixel data. While this was already typdef'ed in the superclass
140 LEN    * it needs to be redone here for this subclass to compile properly with gcc. */
141   typedef typename TImage::PixelContainer PixelContainer;
142 TDA   typedef typename PixelContainer::Pointer PixelContainerPointer;
143   
144   /** Default constructor. Needed since we provide a cast constructor. */
145   ImageRandomConstIteratorWithIndex();
146   ~ImageRandomConstIteratorWithIndex() {};
147   
148   /** Constructor establishes an iterator to walk a particular image and a
149    * particular region of that image. */
150 LEN   ImageRandomConstIteratorWithIndex(const ImageType *ptr, const RegionType& region);
151
152   /** Constructor that can be used to cast from an ImageIterator to an
153 LEN    * ImageRandomConstIteratorWithIndex. Many routines return an ImageIterator but for a
154 LEN    * particular task, you may want an ImageRandomConstIteratorWithIndex.  Rather than
155    * provide overloaded APIs that return different types of Iterators, itk
156    * returns ImageIterators and uses constructors to cast from an
157    * ImageIterator to a ImageRandomConstIteratorWithIndex. */
158 LEN   ImageRandomConstIteratorWithIndex( const ImageConstIteratorWithIndex<TImage> &it)
159     { this->ImageConstIteratorWithIndex<TImage>::operator=(it); }
160
161   /** Move an iterator to the beginning of the region. */
162   void GoToBegin(void)
163 IND **{
164     this->RandomJump();
165     m_NumberOfSamplesDone = 0L;
166 IND **}
167
168   /** Move an iterator to one position past the End of the region. */
169   void GoToEnd(void)
170 IND **{
171     this->RandomJump();
172     m_NumberOfSamplesDone = m_NumberOfSamplesRequested;
173 IND **}
174
175   /** Is the iterator at the beginning of the region? */
176   bool IsAtBegin(void) const
177 SEM     { return (m_NumberOfSamplesDone == 0L) ; }
178
179   /** Is the iterator at the end of the region? */
180   bool IsAtEnd(void) const
181     { return (m_NumberOfSamplesDone >= m_NumberOfSamplesRequested);  }
182  
183   /** Increment (prefix) the selected dimension.
184    * No bounds checking is performed. \sa GetIndex \sa operator-- */
185   Self & operator++()
186 IND **{
187     this->RandomJump();
188     m_NumberOfSamplesDone++;
189     return *this;
190 IND **}
191
192   /** Decrement (prefix) the selected dimension.
193    * No bounds checking is performed. \sa GetIndex \sa operator++ */
194   Self & operator--()
195 IND **{
196     this->RandomJump();
197     m_NumberOfSamplesDone--;
198     return *this;
199 IND **}
200   
201   /** Set/Get number of random samples to get from the image region */
202   void SetNumberOfSamples( unsigned long number );
203   unsigned long GetNumberOfSamples( void ) const;
204
205   /** Reinitialize the seed of the random number generator  */
206   void ReinitializeSeed();
207   void ReinitializeSeed(int);
208
209 private:
210   void RandomJump();
211   Statistics::MersenneTwisterRandomVariateGenerator::Pointer m_Generator;
212   unsigned long  m_NumberOfSamplesRequested;
213   unsigned long  m_NumberOfSamplesDone;
214   unsigned long  m_NumberOfPixelsInRegion;
215 };
216
217 // end namespace itk
218
219 #ifndef ITK_MANUAL_INSTANTIATION
220 #include "itkImageRandomConstIteratorWithIndex.txx"
221 #endif
222
223 #endif 
224
225 EOF
226 EOF,EML
227 EOF,EML

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