00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011 #ifndef __mitkVolumeRandomIterator_h
00012 #define __mitkVolumeRandomIterator_h
00013
00014 #include "mitkVolumeIterator.h"
00015 #include "mitk_nl/mitk_nl_rand.h"
00016
00017
00019
00021
00022 template<class T>
00023 class mitkVolumeRandomIterator : public mitkVolumeIterator<T>
00024 {
00025 public:
00026 mitkVolumeRandomIterator();
00027
00028 mitkVolumeRandomIterator(mitkVolume* vol);
00029
00030 mitkVolumeRandomIterator(mitkVolume* vol, VectorIndexType& region);
00031
00032 ~mitkVolumeRandomIterator();
00033
00034 virtual void GoToBegin(void)
00035 {
00036 this->RandomJump();
00037 m_NumberOfSamplesDone = 0L;
00038 }
00039
00040 virtual void GoToEnd(void)
00041 {
00042 this->RandomJump();
00043 m_NumberOfSamplesDone = m_NumberOfSamplesRequested;
00044 }
00045
00046 virtual bool IsAtBegin(void) const
00047 {
00048 return (m_NumberOfSamplesDone == 0L);
00049 }
00050
00051 virtual bool IsAtEnd(void) const
00052 {
00053 return (m_NumberOfSamplesDone >= m_NumberOfSamplesRequested);
00054 }
00055
00056 virtual void operator++()
00057 {
00058 this->RandomJump();
00059 m_NumberOfSamplesDone++;
00060 }
00061
00062 virtual void operator--()
00063 {
00064 this->RandomJump();
00065 m_NumberOfSamplesDone--;
00066 }
00067
00068 virtual void SetNumberOfSamples( unsigned long number );
00069 virtual unsigned long GetNumberOfSamples( void ) const;
00070
00071 virtual void ReinitializeSeed();
00072 virtual void ReinitializeSeed(int);
00073
00074 private:
00075 void RandomJump();
00076 mitk_nl::rand_mersenne_twister* m_Generator;
00077 unsigned long m_NumberOfSamplesRequested;
00078 unsigned long m_NumberOfSamplesDone;
00079 unsigned long m_NumberOfPixelsInRegion;
00080 };
00081
00082 template<class T>
00083 mitkVolumeRandomIterator<T>::mitkVolumeRandomIterator()
00084 : mitkVolumeIterator<T>()
00085 {
00086 m_NumberOfPixelsInRegion = 0L;
00087 m_NumberOfSamplesRequested = 0L;
00088 m_NumberOfSamplesDone = 0L;
00089 m_Generator = new mitk_nl::rand_mersenne_twister;
00090 }
00091
00092 template<class T>
00093 mitkVolumeRandomIterator<T>::mitkVolumeRandomIterator(mitkVolume* vol)
00094 : mitkVolumeIterator<T>(vol)
00095 {
00096 m_NumberOfPixelsInRegion = this->GetNumberOfPixelsInRegion();
00097 m_NumberOfSamplesRequested = 0L;
00098 m_NumberOfSamplesDone = 0L;
00099 m_Generator = new mitk_nl::rand_mersenne_twister;
00100 }
00101
00102 template<class T>
00103 mitkVolumeRandomIterator<T>::mitkVolumeRandomIterator(mitkVolume* vol, VectorIndexType& region)
00104 : mitkVolumeIterator<T>(vol, region)
00105 {
00106 m_NumberOfPixelsInRegion = this->GetNumberOfPixelsInRegion();
00107 m_NumberOfSamplesRequested = 0L;
00108 m_NumberOfSamplesDone = 0L;
00109 m_Generator = new mitk_nl::rand_mersenne_twister;
00110 }
00111
00112 template<class T>
00113 mitkVolumeRandomIterator<T>::~mitkVolumeRandomIterator()
00114 {
00115
00116 }
00117
00118 template<class T>
00119 void mitkVolumeRandomIterator<T>::SetNumberOfSamples( unsigned long number )
00120 {
00121 m_NumberOfSamplesRequested = number;
00122 }
00123
00124 template<class T>
00125 unsigned long mitkVolumeRandomIterator<T>::GetNumberOfSamples( void ) const
00126 {
00127 return m_NumberOfSamplesRequested;
00128 }
00129
00130 template<class T>
00131 void mitkVolumeRandomIterator<T>::ReinitializeSeed()
00132 {
00133 m_Generator->SetSeed();
00134 }
00135
00136 template<class T>
00137 void mitkVolumeRandomIterator<T>::ReinitializeSeed(int seed)
00138 {
00139 m_Generator->SetSeed(seed);
00140 }
00141
00142 template<class T>
00143 void mitkVolumeRandomIterator<T>::RandomJump()
00144 {
00145 const unsigned long randomPosition =
00146 static_cast<unsigned long > ( m_Generator->GetVariateWithOpenRange ( static_cast<double>(m_NumberOfPixelsInRegion)-0.5 ) );
00147
00148 unsigned long position = randomPosition;
00149 unsigned long residual;
00150 for( unsigned int dim = 0; dim < m_ImageDimension; dim++ )
00151 {
00152 const unsigned long sizeInThisDimension = m_Region[2*dim+1] - m_Region[2*dim];
00153 residual = position % sizeInThisDimension;
00154 m_PositionIndex[dim] = residual + m_BeginIndex[dim];
00155 position -= residual;
00156 position /= sizeInThisDimension;
00157 }
00158
00159 m_Position = m_DataPointer + this->ComputeOffset( m_PositionIndex );
00160
00161 }
00162
00163
00164 namespace MITK_VOL_ITERATOR
00165 {
00166
00167 static bool GenerateRandomVolumeIterator(mitkVolume* vol, mitkVolumeIteratorBase* &it, bool autorelease = false)
00168 {
00169 if( it != NULL && autorelease)
00170 {
00171 delete it;
00172 }
00173
00174 bool run_flag = true;
00175
00176 switch(vol->GetDataType())
00177 {
00178 case MITK_DOUBLE:
00179 it = new mitkVolumeRandomIterator<double>(vol);
00180 break;
00181 case MITK_FLOAT:
00182 it = new mitkVolumeRandomIterator<float>(vol);
00183 break;
00184 case MITK_LONG:
00185 it = new mitkVolumeRandomIterator<long>(vol);
00186 break;
00187 case MITK_UNSIGNED_LONG:
00188 it = new mitkVolumeRandomIterator<unsigned long>(vol);
00189 break;
00190 case MITK_INT:
00191 it = new mitkVolumeRandomIterator<int>(vol);
00192 break;
00193 case MITK_UNSIGNED_INT:
00194 it = new mitkVolumeRandomIterator<unsigned int>(vol);
00195 break;
00196 case MITK_SHORT:
00197 it = new mitkVolumeRandomIterator<short>(vol);
00198 break;
00199 case MITK_UNSIGNED_SHORT:
00200 it = new mitkVolumeRandomIterator<unsigned short>(vol);
00201 break;
00202 case MITK_CHAR:
00203 it = new mitkVolumeRandomIterator<char>(vol);
00204 break;
00205 case MITK_UNSIGNED_CHAR:
00206 it = new mitkVolumeRandomIterator<unsigned char>(vol);
00207 break;
00208 default:
00209 it = NULL;
00210 run_flag = false;
00211 }
00212
00213 return run_flag;
00214 }
00215
00216 }
00217
00218 #ifdef MITK_DLL_EXPORT
00219 #define DEFINED_mitkVolumeRandomIterator
00220 #include "mitkTemplateExport.h"
00221 #endif // MITK_DLL_EXPORT
00222
00223 #endif
00224
00225