ITK  4.1.0
Insight Segmentation and Registration Toolkit
itkImageRandomNonRepeatingConstIteratorWithIndex.h
Go to the documentation of this file.
00001 /*=========================================================================
00002  *
00003  *  Copyright Insight Software Consortium
00004  *
00005  *  Licensed under the Apache License, Version 2.0 (the "License");
00006  *  you may not use this file except in compliance with the License.
00007  *  You may obtain a copy of the License at
00008  *
00009  *         http://www.apache.org/licenses/LICENSE-2.0.txt
00010  *
00011  *  Unless required by applicable law or agreed to in writing, software
00012  *  distributed under the License is distributed on an "AS IS" BASIS,
00013  *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
00014  *  See the License for the specific language governing permissions and
00015  *  limitations under the License.
00016  *
00017  *=========================================================================*/
00018 #ifndef __itkImageRandomNonRepeatingConstIteratorWithIndex_h
00019 #define __itkImageRandomNonRepeatingConstIteratorWithIndex_h
00020 
00021 #include "itkImageConstIteratorWithIndex.h"
00022 #include <algorithm>
00023 #include <iostream>
00024 #include "itkMersenneTwisterRandomVariateGenerator.h"
00025 
00026 namespace itk
00027 {
00040 class NodeOfPermutation
00041 {
00042 public:
00043   SizeValueType m_Priority;
00044   SizeValueType m_Index;
00045   double        m_Value;
00046 
00047   NodeOfPermutation ()
00048   {
00049     m_Priority = 0;
00050     m_Index = 0;
00051     m_Value = 0.0;
00052   }
00053 
00054   bool operator<(const NodeOfPermutation & b) const
00055   {
00056     if ( m_Priority == b.m_Priority )
00057       {
00058       return m_Value < b.m_Value;
00059       }
00060     else
00061       {
00062       return m_Priority < b.m_Priority;
00063       }
00064   }
00065 };
00066 
00071 class RandomPermutation
00072 {
00073 public:
00074   typedef Statistics::MersenneTwisterRandomVariateGenerator::Pointer GeneratorPointer;
00075   NodeOfPermutation *m_Permutation;
00076   GeneratorPointer   m_Generator;
00077   SizeValueType      m_Size;
00078 
00079   RandomPermutation(SizeValueType sz)
00080   {
00081     m_Size = sz;
00082     m_Permutation = new NodeOfPermutation[m_Size];
00083     m_Generator = Statistics::MersenneTwisterRandomVariateGenerator::GetInstance();
00084     this->Shuffle();
00085   }
00086 
00087   void Dump()
00088   {
00089     for ( SizeValueType i = 0; i < m_Size; i++ )
00090       {
00091       std::cout << m_Permutation[i].m_Value << " " << m_Permutation[i].m_Priority
00092                 << " " << m_Permutation[i].m_Index << ";";
00093       std::cout << std::endl;
00094       }
00095   }
00096 
00097   void SetPriority(SizeValueType i, SizeValueType priority)
00098   {
00099     if ( i > m_Size )
00100       {
00101       std::cerr << "Error - i dont have " << i << " elements" << std::endl;
00102       }
00103     else
00104       {
00105       m_Permutation[i].m_Priority = priority;
00106       }
00107   }
00108 
00109   void Shuffle()
00110   {
00111     for ( SizeValueType i = 0; i < m_Size; i++ )
00112       {
00113       m_Permutation[i].m_Value = m_Generator->GetVariateWithClosedRange (1.0);
00114       m_Permutation[i].m_Index = i;
00115       }
00116     std::sort(m_Permutation, m_Permutation + m_Size);
00117   }
00118 
00119   SizeValueType operator[](SizeValueType i)
00120   {
00121     return m_Permutation[i].m_Index;
00122   }
00123 
00124   ~RandomPermutation()
00125   {
00126     delete[] m_Permutation;
00127   }
00128 
00130   void ReinitializeSeed()
00131   {
00132     m_Generator->Initialize();
00133   }
00134 
00135   void ReinitializeSeed(int seed)
00136   {
00137     m_Generator->SetSeed (seed);
00138   }
00139 };
00140 
00213 template< typename TImage >
00214 class ITK_EXPORT ImageRandomNonRepeatingConstIteratorWithIndex:public ImageConstIteratorWithIndex< TImage >
00215 {
00216 public:
00218   typedef ImageRandomNonRepeatingConstIteratorWithIndex Self;
00219   typedef ImageConstIteratorWithIndex< TImage >         Superclass;
00220 
00222   typedef typename Superclass::IndexType             IndexType;
00223   typedef typename Superclass::SizeType              SizeType;
00224   typedef typename Superclass::OffsetType            OffsetType;
00225   typedef typename Superclass::RegionType            RegionType;
00226   typedef typename Superclass::ImageType             ImageType;
00227   typedef typename Superclass::PixelContainer        PixelContainer;
00228   typedef typename Superclass::PixelContainerPointer PixelContainerPointer;
00229   typedef typename Superclass::InternalPixelType     InternalPixelType;
00230   typedef typename Superclass::PixelType             PixelType;
00231   typedef typename Superclass::AccessorType          AccessorType;
00232   typedef typename Superclass::IndexValueType        IndexValueType;
00233   typedef typename Superclass::OffsetValueType       OffsetValueType;
00234   typedef typename Superclass::SizeValueType         SizeValueType;
00235 
00237   ImageRandomNonRepeatingConstIteratorWithIndex();
00238   ~ImageRandomNonRepeatingConstIteratorWithIndex()
00239   {
00240     if ( m_Permutation )
00241       {
00242       delete m_Permutation;
00243       }
00244   }
00246 
00249   ImageRandomNonRepeatingConstIteratorWithIndex(const ImageType *ptr, const RegionType & region);
00250 
00257   ImageRandomNonRepeatingConstIteratorWithIndex(const ImageConstIteratorWithIndex< TImage > & it)
00258   {
00259     this->ImageConstIteratorWithIndex< TImage >::operator=(it);
00260 
00261     m_Permutation = NULL;
00262   }
00263 
00265   Self & operator=(const Self & it);
00266 
00268   void GoToBegin(void)
00269   {
00270     m_NumberOfSamplesDone = 0L;
00271     this->UpdatePosition();
00272   }
00274 
00276   void GoToEnd(void)
00277   {
00278     m_NumberOfSamplesDone = m_NumberOfSamplesRequested;
00279     this->UpdatePosition();
00280   }
00282 
00284   bool IsAtBegin(void) const
00285   {
00286     return ( m_NumberOfSamplesDone == 0L );
00287   }
00288 
00290   bool IsAtEnd(void) const
00291   {
00292     return ( m_NumberOfSamplesDone >= m_NumberOfSamplesRequested );
00293   }
00294 
00296   itkStaticConstMacro(ImageDimension, unsigned int, ::itk::GetImageDimension< TImage >::ImageDimension);
00297 
00299   typedef itk::Image< SizeValueType, itkGetStaticConstMacro(ImageDimension) > PriorityImageType;
00300 
00306   void SetPriorityImage(const PriorityImageType *priorityImage);
00307 
00310   Self & operator++()
00311   {
00312     m_NumberOfSamplesDone++;
00313     this->UpdatePosition();
00314     return *this;
00315   }
00317 
00320   Self & operator--()
00321   {
00322     m_NumberOfSamplesDone--;
00323     this->UpdatePosition();
00324     return *this;
00325   }
00327 
00329   void SetNumberOfSamples(SizeValueType number);
00330 
00331   SizeValueType GetNumberOfSamples(void) const;
00332 
00334   void ReinitializeSeed();
00335 
00338   void ReinitializeSeed(int);
00339 
00340 private:
00341   void UpdatePosition();
00342 
00343   SizeValueType      m_NumberOfSamplesRequested;
00344   SizeValueType      m_NumberOfSamplesDone;
00345   SizeValueType      m_NumberOfPixelsInRegion;
00346   RandomPermutation *m_Permutation;
00347 };
00348 } // end namespace itk
00349 
00350 #ifndef ITK_MANUAL_INSTANTIATION
00351 #include "itkImageRandomNonRepeatingConstIteratorWithIndex.hxx"
00352 #endif
00353 
00354 #endif
00355