ITK  4.9.0
Insight Segmentation and Registration Toolkit
itkImageRandomNonRepeatingConstIteratorWithIndex.h
Go to the documentation of this file.
1 /*=========================================================================
2  *
3  * Copyright Insight Software Consortium
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0.txt
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  *
17  *=========================================================================*/
18 #ifndef itkImageRandomNonRepeatingConstIteratorWithIndex_h
19 #define itkImageRandomNonRepeatingConstIteratorWithIndex_h
20 
22 #include <algorithm>
23 #include <iostream>
25 
26 namespace itk
27 {
41 {
42 public:
45  double m_Value;
46 
48  {
49  m_Priority = 0;
50  m_Index = 0;
51  m_Value = 0.0;
52  }
53 
54  bool operator<(const NodeOfPermutation & b) const
55  {
56  if ( m_Priority == b.m_Priority )
57  {
58  return m_Value < b.m_Value;
59  }
60  else
61  {
62  return m_Priority < b.m_Priority;
63  }
64  }
65 };
66 
72 {
73 public:
78 
80  {
81  m_Size = sz;
84  this->Shuffle();
85  }
86 
88  {
89  delete[] m_Permutation;
90  m_Size = it.m_Size;
93  return *this;
94  }
95 
96 #if !defined(ITK_LEGACY_REMOVE)
97  void Dump()
98  {
99  for ( SizeValueType i = 0; i < m_Size; i++ )
100  {
101  std::cout << m_Permutation[i].m_Value << " " << m_Permutation[i].m_Priority
102  << " " << m_Permutation[i].m_Index << ";";
103  std::cout << std::endl;
104  }
105  }
106 #endif
107 
109  {
110  if ( i > m_Size )
111  {
112  std::ostringstream ostrm;
113  ostrm << "Error: RandomPermuation does not have " << i << " elements" << std::endl;
114  throw std::runtime_error(ostrm.str());
115  }
116  else
117  {
118  m_Permutation[i].m_Priority = priority;
119  }
120  }
121 
122  void Shuffle()
123  {
124  for ( SizeValueType i = 0; i < m_Size; i++ )
125  {
126  m_Permutation[i].m_Value = m_Generator->GetVariateWithClosedRange (1.0);
127  m_Permutation[i].m_Index = i;
128  }
129  std::sort(m_Permutation, m_Permutation + m_Size);
130  }
131 
133  {
134  return m_Permutation[i].m_Index;
135  }
136 
138  {
139  delete[] m_Permutation;
140  }
141 
144  {
145  m_Generator->Initialize();
146  }
147 
148  void ReinitializeSeed(unsigned int seed)
149  {
150  m_Generator->SetSeed (seed);
151  }
152 };
153 
226 template< typename TImage >
228 {
229 public:
233 
236  typedef typename Superclass::SizeType SizeType;
248 
252  {
253  delete m_Permutation;
254  }
256 
260 
268  {
270 
271  m_Permutation = ITK_NULLPTR;
272  }
273 
275  Self & operator=(const Self & it);
276 
278  void GoToBegin(void)
279  {
281  this->UpdatePosition();
282  }
284 
286  void GoToEnd(void)
287  {
289  this->UpdatePosition();
290  }
292 
294  bool IsAtBegin(void) const
295  {
296  return ( m_NumberOfSamplesDone == 0L );
297  }
298 
300  bool IsAtEnd(void) const
301  {
303  }
304 
306  itkStaticConstMacro(ImageDimension, unsigned int, TImage::ImageDimension);
307 
310 
316  void SetPriorityImage(const PriorityImageType *priorityImage);
317 
321  {
323  this->UpdatePosition();
324  return *this;
325  }
327 
331  {
333  this->UpdatePosition();
334  return *this;
335  }
337 
339  void SetNumberOfSamples(SizeValueType number);
340 
342 
344  void ReinitializeSeed();
345 
348  void ReinitializeSeed(int);
349 
350 private:
351  void UpdatePosition();
352 
357 };
358 } // end namespace itk
359 
360 #ifndef ITK_MANUAL_INSTANTIATION
361 #include "itkImageRandomNonRepeatingConstIteratorWithIndex.hxx"
362 #endif
363 
364 #endif
void SetPriority(SizeValueType i, SizeValueType priority)
itk::Image< SizeValueType, itkGetStaticConstMacro(ImageDimension) > PriorityImageType
A multi-dimensional image iterator that visits a random set of pixels within an image region...
Statistics::MersenneTwisterRandomVariateGenerator::Pointer GeneratorPointer
ImageRandomNonRepeatingConstIteratorWithIndex(const ImageConstIteratorWithIndex< TImage > &it)
A node to be used when computing permutations.
void SetPriorityImage(const PriorityImageType *priorityImage)
unsigned long SizeValueType
Definition: itkIntTypes.h:143
bool operator<(const NodeOfPermutation &b) const
A base class for multi-dimensional iterators templated over image type that are designed to efficient...
Produce a random permutation of a collection.
RandomPermutation & operator=(const RandomPermutation &it)
Self & operator=(const Self &it)
Templated n-dimensional image class.
Definition: itkImage.h:75