KWStyle - itkImageRandomNonRepeatingConstIteratorWithIndex.txx
 
Matrix View
Description

1 /*=========================================================================
2
3   Program:   Insight Segmentation & Registration Toolkit
4   Module:    $RCSfile: itkImageRandomNonRepeatingConstIteratorWithIndex.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 _itkImageRandomNonRepeatingConstIteratorWithIndex_txx
18 DEF #define _itkImageRandomNonRepeatingConstIteratorWithIndex_txx
19
20 #include "itkImageRandomNonRepeatingConstIteratorWithIndex.h"
21
22 namespace itk
23 {
24
25
26 /** Default constructor. Needed since we provide a cast constructor. */
27 template<class TImage>
28 ImageRandomNonRepeatingConstIteratorWithIndex<TImage>
29 LEN ::ImageRandomNonRepeatingConstIteratorWithIndex() : ImageConstIteratorWithIndex<TImage>()
30 {
31   m_NumberOfPixelsInRegion    = 0L;
32   m_NumberOfSamplesRequested  = 0L;
33   m_NumberOfSamplesDone       = 0L;
34   m_Permutation=NULL;
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 ImageRandomNonRepeatingConstIteratorWithIndex<TImage>
41 LEN ::ImageRandomNonRepeatingConstIteratorWithIndex(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_Permutation=new randomPermutation(m_NumberOfPixelsInRegion);
48 }
49
50 /**  Set the number of samples to extract from the region */
51 template<class TImage>
52 void
53 ImageRandomNonRepeatingConstIteratorWithIndex<TImage>
54 ::SetNumberOfSamples( unsigned long number )
55 {
56   m_NumberOfSamplesRequested = number;
57 LEN   if(number>m_NumberOfPixelsInRegion) m_NumberOfSamplesRequested=m_NumberOfPixelsInRegion;
58 }
59
60 /**  Set the number of samples to extract from the region */
61 template<class TImage>
62 unsigned long
63 ImageRandomNonRepeatingConstIteratorWithIndex<TImage>
64 ::GetNumberOfSamples( void ) const
65 {
66   return m_NumberOfSamplesRequested;
67 }
68
69 /** Reinitialize the seed of the random number generator */
70 template<class TImage>
71 void
72 ImageRandomNonRepeatingConstIteratorWithIndex<TImage>
73 ::ReinitializeSeed()
74 {
75   this->m_Permutation->ReinitializeSeed();
76   this->m_Permutation->Shuffle();
77 }
78
79 template<class TImage>
80 void
81 ImageRandomNonRepeatingConstIteratorWithIndex<TImage>
82 ::ReinitializeSeed(int seed)
83 {
84   this->m_Permutation->ReinitializeSeed(seed);
85   this->m_Permutation->Shuffle();
86 }
87
88 /** update the position */
89 template<class TImage>
90 void
91 ImageRandomNonRepeatingConstIteratorWithIndex<TImage>
92 ::SetPriorityImage(const PriorityImageType* priorityImage) 
93 {
94   // should probably do error checking to be sure that the priority
95   // image is the right size
96   IndexType positionIndex;
97   unsigned long position,pixel;
98   unsigned long residual;
99   for(pixel=0;pixel<m_NumberOfPixelsInRegion;pixel++) 
100     {
101     position=pixel;
102     for( unsigned int dim = 0; dim < TImage::ImageDimension; dim++ )
103       {
104       const unsigned long sizeInThisDimension = this->m_Region.GetSize()[dim];
105       residual = position % sizeInThisDimension;
106       positionIndex[dim] =  residual + this->m_BeginIndex[dim];
107       position -= residual;
108       position /= sizeInThisDimension;
109       }
110 LEN     //std::cout<<pixel<<" "<<positionIndex<<" "<<priorityImage->GetPixel(positionIndex)std::endl;
111 LEN     this->m_Permutation->SetPriority(pixel,priorityImage->GetPixel(positionIndex));
112     }
113   this->m_Permutation->Shuffle();
114 }
115
116 /** update the position */
117 template<class TImage>
118 void
119 ImageRandomNonRepeatingConstIteratorWithIndex<TImage>
120 ::UpdatePosition()
121 {
122 LEN   unsigned long position = (*(this->m_Permutation))[m_NumberOfSamplesDone%m_NumberOfSamplesRequested];
123   unsigned long residual;
124   for( unsigned int dim = 0; dim < TImage::ImageDimension; dim++ )
125     {
126     const unsigned long sizeInThisDimension = this->m_Region.GetSize()[dim];
127     residual = position % sizeInThisDimension;
128     this->m_PositionIndex[dim] =  residual + this->m_BeginIndex[dim];
129     position -= residual;
130     position /= sizeInThisDimension;
131     }
132
133 LEN   this->m_Position = this->m_Image->GetBufferPointer() + this->m_Image->ComputeOffset( this->m_PositionIndex );
134 }
135
136
137 // end namespace itk
138
139 #endif
140

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