Main Page   Groups   Namespace List   Class Hierarchy   Alphabetical List   Compound List   File List   Namespace Members   Compound Members   File Members   Concepts

itkImageRandomNonRepeatingConstIteratorWithIndex.h

Go to the documentation of this file.
00001 /*=========================================================================
00002 
00003   Program:   Insight Segmentation & Registration Toolkit
00004   Module:    $RCSfile: itkImageRandomNonRepeatingConstIteratorWithIndex.h,v $
00005   Language:  C++
00006   Date:      $Date: 2005/08/30 17:31:03 $
00007   Version:   $Revision: 1.5 $
00008 
00009   Copyright (c) Insight Software Consortium. All rights reserved.
00010   See ITKCopyright.txt or http://www.itk.org/HTML/Copyright.htm for details.
00011 
00012        This software is distributed WITHOUT ANY WARRANTY; without even 
00013        the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR 
00014        PURPOSE.  See the above copyright notices for more information.
00015 
00016   =========================================================================*/
00017 #ifndef __itkImageRandomNonRepeatingConstIteratorWithIndex_h
00018 #define __itkImageRandomNonRepeatingConstIteratorWithIndex_h
00019 
00020 #include "itkImageConstIteratorWithIndex.h"
00021 #include "itkImage.h"
00022 #include <algorithm>
00023 #include <iostream>
00024 #include "vnl/vnl_sample.h"
00025 
00026 
00027   namespace itk
00028   {
00029 
00030 
00031   /* 
00032      The itk::ImageRandomNonRepeatingIterator works by creating a random
00033      permutation of the image pixels and then using that to control the
00034      order in which it accesses them.  The classes nodeOfPermutation and
00035      randomPermutation are used to support that.  randomPermutation is
00036      basically container which holds nodeOfPermutation objects.  The
00037      node class overloads the < operator, which allows the sort algorithm
00038      from the STL to be used on it.
00039   */
00040   class nodeOfPermutation {
00041     public:
00042     unsigned long priority;
00043     unsigned long index;
00044     double value;
00045     nodeOfPermutation () {
00046       priority=0;
00047       index=0;
00048       value=0.0;
00049     }
00050     bool operator<( const nodeOfPermutation& b) const{
00051        if(priority==b.priority) {
00052         return value < b.value;
00053         } else {
00054            return priority<b.priority;
00055         }
00056     }
00057   };
00058   class randomPermutation {
00059    public:
00060     nodeOfPermutation * permutation;
00061     unsigned long size;
00062     randomPermutation(unsigned long sz) {
00063       size=sz;
00064       permutation=new nodeOfPermutation[size];
00065       this->Shuffle();
00066     }
00067     void Dump() {
00068       for(unsigned int i=0;i<size;i++) {
00069         std::cout<<permutation[i].value<<" "<<permutation[i].priority
00070            <<" "<<permutation[i].index<<";";
00071         std::cout<<std::endl;
00072       }
00073     }
00074     void SetPriority(unsigned long i,unsigned long priority){
00075       if(i>size) {
00076         std::cerr<<"Error - i dont have "<<i<<" elements"<<std::endl;
00077       } else {
00078       permutation[i].priority=priority;
00079       }
00080     }
00081     void Shuffle() {
00082       for(unsigned int i=0;i<size;i++) {
00083         permutation[i].value=vnl_sample_uniform(0.0,1.0);
00084         permutation[i].index=i;
00085       }
00086       std::sort(permutation,permutation+size);
00087     }
00088     unsigned long operator[](unsigned long i) {
00089       return permutation[i].index;
00090     }
00091     ~randomPermutation() {
00092       delete [] permutation;
00093     }
00094     
00096     void ReinitializeSeed()
00097       {
00098         vnl_sample_reseed();
00099       }
00100     
00101     void ReinitializeSeed(int seed)
00102       {
00103         vnl_sample_reseed(seed);
00104       }
00105   };
00106 
00107 
00175   template<typename TImage>
00176   class ITK_EXPORT ImageRandomNonRepeatingConstIteratorWithIndex : public ImageConstIteratorWithIndex<TImage>
00177   {
00178   public:
00180     typedef ImageRandomNonRepeatingConstIteratorWithIndex Self;
00181     typedef ImageConstIteratorWithIndex<TImage>  Superclass;
00182     
00187     typedef typename TImage::IndexType   IndexType;
00188 
00193     typedef typename TImage::RegionType RegionType;
00194     
00199     typedef TImage ImageType;
00200 
00204     typedef typename TImage::PixelContainer PixelContainer;
00205     typedef typename PixelContainer::Pointer PixelContainerPointer;
00206 
00208     ImageRandomNonRepeatingConstIteratorWithIndex();
00209     ~ImageRandomNonRepeatingConstIteratorWithIndex() { if( m_Permutation ) delete m_Permutation; };
00210     
00213     ImageRandomNonRepeatingConstIteratorWithIndex(const ImageType *ptr, const RegionType& region);
00214 
00221     ImageRandomNonRepeatingConstIteratorWithIndex( const ImageConstIteratorWithIndex<TImage> &it)
00222       { 
00223       this->ImageConstIteratorWithIndex<TImage>::operator=(it); 
00224       m_Permutation = NULL;
00225       }
00227     void GoToBegin(void)
00228     {
00229       m_NumberOfSamplesDone = 0L;
00230       this->UpdatePosition();
00231     }
00232 
00234     void GoToEnd(void)
00235     {
00236       m_NumberOfSamplesDone = m_NumberOfSamplesRequested;
00237       this->UpdatePosition();
00238     }
00239 
00241     bool IsAtBegin(void) const
00242       { return (m_NumberOfSamplesDone == 0L) ; }
00243 
00245     bool IsAtEnd(void) const
00246       { 
00247       return (m_NumberOfSamplesDone >= m_NumberOfSamplesRequested);  
00248       }
00249 
00250 
00252     itkStaticConstMacro( ImageDimension, unsigned int, 
00253                   ::itk::GetImageDimension< TImage >::ImageDimension );
00254    
00255 
00257     typedef itk::Image< unsigned long, 
00258                         itkGetStaticConstMacro(ImageDimension )  
00259                                                 >  PriorityImageType;
00260     
00266     void SetPriorityImage(const PriorityImageType * priorityImage);
00267 
00270   Self & operator++()
00271   {
00272     m_NumberOfSamplesDone++;
00273     this->UpdatePosition();
00274     return *this;
00275   }
00276 
00279   Self & operator--()
00280   {
00281     m_NumberOfSamplesDone--;
00282     this->UpdatePosition();
00283     return *this;
00284   }
00285   
00287   void SetNumberOfSamples( unsigned long number );
00288   unsigned long GetNumberOfSamples( void ) const;
00289 
00291   void ReinitializeSeed();
00295   void ReinitializeSeed(int);
00296 
00297 private:
00298   void UpdatePosition();
00299   unsigned long  m_NumberOfSamplesRequested;
00300   unsigned long  m_NumberOfSamplesDone;
00301   unsigned long  m_NumberOfPixelsInRegion;
00302   randomPermutation * m_Permutation;
00303 };
00304 
00305 } // end namespace itk
00306 
00307 #ifndef ITK_MANUAL_INSTANTIATION
00308 #include "itkImageRandomNonRepeatingConstIteratorWithIndex.txx"
00309 #endif
00310 
00311 #endif 
00312 
00313 
00314 

Generated at Tue Aug 30 16:44:30 2005 for ITK by doxygen 1.4.1 written by Dimitri van Heesch, © 1997-2000