| 1 |
|
/*========================================================================= |
| 2 |
|
|
| 3 |
|
Program: Insight Segmentation & Registration Toolkit |
| 4 |
|
Module: $RCSfile: itkImageRandomConstIteratorWithIndex.txx.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 |
DEF |
#ifndef _itkImageRandomConstIteratorWithIndex_txx |
| 18 |
DEF |
#define _itkImageRandomConstIteratorWithIndex_txx |
| 19 |
|
|
| 20 |
|
#include "itkImageRandomConstIteratorWithIndex.h" |
| 21 |
|
|
| 22 |
|
namespace itk |
| 23 |
|
{ |
| 24 |
|
|
| 25 |
|
|
| 26 |
|
/** Default constructor. Needed since we provide a cast constructor. */ |
| 27 |
|
template<class TImage> |
| 28 |
|
ImageRandomConstIteratorWithIndex<TImage> |
| 29 |
|
::ImageRandomConstIteratorWithIndex() : ImageConstIteratorWithIndex<TImage>() |
| 30 |
|
{ |
| 31 |
|
m_NumberOfPixelsInRegion = 0L; |
| 32 |
|
m_NumberOfSamplesRequested = 0L; |
| 33 |
|
m_NumberOfSamplesDone = 0L; |
| 34 |
|
m_Generator = Statistics::MersenneTwisterRandomVariateGenerator::New(); |
| 35 |
|
} |
| 36 |
|
|
| 37 |
|
/** Constructor establishes an iterator to walk a particular image and a |
| 38 |
|
* particular region of that image. */ |
| 39 |
|
template<class TImage> |
| 40 |
|
ImageRandomConstIteratorWithIndex<TImage> |
| 41 |
LEN |
::ImageRandomConstIteratorWithIndex(const ImageType *ptr, const RegionType& region) |
| 42 |
IND |
**: ImageConstIteratorWithIndex<TImage>( ptr, region ) |
| 43 |
|
{ |
| 44 |
|
m_NumberOfPixelsInRegion = region.GetNumberOfPixels(); |
| 45 |
|
m_NumberOfSamplesRequested = 0L; |
| 46 |
|
m_NumberOfSamplesDone = 0L; |
| 47 |
|
m_Generator = Statistics::MersenneTwisterRandomVariateGenerator::New(); |
| 48 |
|
} |
| 49 |
|
|
| 50 |
|
/** Set the number of samples to extract from the region */ |
| 51 |
|
template<class TImage> |
| 52 |
|
void |
| 53 |
|
ImageRandomConstIteratorWithIndex<TImage> |
| 54 |
|
::SetNumberOfSamples( unsigned long number ) |
| 55 |
|
{ |
| 56 |
|
m_NumberOfSamplesRequested = number; |
| 57 |
|
} |
| 58 |
|
|
| 59 |
|
/** Set the number of samples to extract from the region */ |
| 60 |
|
template<class TImage> |
| 61 |
|
unsigned long |
| 62 |
|
ImageRandomConstIteratorWithIndex<TImage> |
| 63 |
|
::GetNumberOfSamples( void ) const |
| 64 |
|
{ |
| 65 |
|
return m_NumberOfSamplesRequested; |
| 66 |
|
} |
| 67 |
|
|
| 68 |
|
/** Reinitialize the seed of the random number generator */ |
| 69 |
|
template<class TImage> |
| 70 |
|
void |
| 71 |
|
ImageRandomConstIteratorWithIndex<TImage> |
| 72 |
|
::ReinitializeSeed() |
| 73 |
|
{ |
| 74 |
|
m_Generator->SetSeed(); |
| 75 |
|
} |
| 76 |
|
|
| 77 |
|
template<class TImage> |
| 78 |
|
void |
| 79 |
|
ImageRandomConstIteratorWithIndex<TImage> |
| 80 |
|
::ReinitializeSeed(int seed) |
| 81 |
|
{ |
| 82 |
|
m_Generator->SetSeed ( seed ); |
| 83 |
|
// vnl_sample_reseed(seed); |
| 84 |
|
} |
| 85 |
|
|
| 86 |
|
/** Execute an acrobatic random jump */ |
| 87 |
|
template<class TImage> |
| 88 |
|
void |
| 89 |
|
ImageRandomConstIteratorWithIndex<TImage> |
| 90 |
|
::RandomJump() |
| 91 |
|
{ |
| 92 |
|
const unsigned long randomPosition = |
| 93 |
LEN,IND |
****static_cast<unsigned long > ( m_Generator->GetVariateWithOpenRange ( static_cast<double>(m_NumberOfPixelsInRegion)-0.5 ) ); |
| 94 |
|
/* |
| 95 |
IND |
******vnl_sample_uniform(0.0f, |
| 96 |
|
static_cast<double>(m_NumberOfPixelsInRegion)-0.5) ); |
| 97 |
|
*/ |
| 98 |
|
|
| 99 |
|
unsigned long position = randomPosition; |
| 100 |
|
unsigned long residual; |
| 101 |
|
for( unsigned int dim = 0; dim < TImage::ImageDimension; dim++ ) |
| 102 |
|
{ |
| 103 |
|
const unsigned long sizeInThisDimension = this->m_Region.GetSize()[dim]; |
| 104 |
|
residual = position % sizeInThisDimension; |
| 105 |
|
this->m_PositionIndex[dim] = residual + this->m_BeginIndex[dim]; |
| 106 |
|
position -= residual; |
| 107 |
|
position /= sizeInThisDimension; |
| 108 |
|
} |
| 109 |
|
|
| 110 |
LEN |
this->m_Position = this->m_Image->GetBufferPointer() + this->m_Image->ComputeOffset( this->m_PositionIndex ); |
| 111 |
|
} |
| 112 |
|
|
| 113 |
|
|
| 114 |
|
} // end namespace itk |
| 115 |
|
|
| 116 |
|
#endif |
| 117 |
|
|