ITK  4.4.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  void Dump()
97  {
98  for ( SizeValueType i = 0; i < m_Size; i++ )
99  {
100  std::cout << m_Permutation[i].m_Value << " " << m_Permutation[i].m_Priority
101  << " " << m_Permutation[i].m_Index << ";";
102  std::cout << std::endl;
103  }
104  }
105 
107  {
108  if ( i > m_Size )
109  {
110  std::cerr << "Error - i dont have " << i << " elements" << std::endl;
111  }
112  else
113  {
114  m_Permutation[i].m_Priority = priority;
115  }
116  }
117 
118  void Shuffle()
119  {
120  for ( SizeValueType i = 0; i < m_Size; i++ )
121  {
122  m_Permutation[i].m_Value = m_Generator->GetVariateWithClosedRange (1.0);
123  m_Permutation[i].m_Index = i;
124  }
125  std::sort(m_Permutation, m_Permutation + m_Size);
126  }
127 
129  {
130  return m_Permutation[i].m_Index;
131  }
132 
134  {
135  delete[] m_Permutation;
136  }
137 
140  {
141  m_Generator->Initialize();
142  }
143 
144  void ReinitializeSeed(unsigned int seed)
145  {
146  m_Generator->SetSeed (seed);
147  }
148 };
149 
222 template< typename TImage >
224 {
225 public:
229 
232  typedef typename Superclass::SizeType SizeType;
244 
248  {
249  if ( m_Permutation )
250  {
251  delete m_Permutation;
252  }
253  }
255 
258  ImageRandomNonRepeatingConstIteratorWithIndex(const ImageType *ptr, const RegionType & region);
259 
267  {
269 
270  m_Permutation = NULL;
271  }
272 
274  Self & operator=(const Self & it);
275 
277  void GoToBegin(void)
278  {
279  m_NumberOfSamplesDone = 0L;
280  this->UpdatePosition();
281  }
283 
285  void GoToEnd(void)
286  {
287  m_NumberOfSamplesDone = m_NumberOfSamplesRequested;
288  this->UpdatePosition();
289  }
291 
293  bool IsAtBegin(void) const
294  {
295  return ( m_NumberOfSamplesDone == 0L );
296  }
297 
299  bool IsAtEnd(void) const
300  {
301  return ( m_NumberOfSamplesDone >= m_NumberOfSamplesRequested );
302  }
303 
305  itkStaticConstMacro(ImageDimension, unsigned int, TImage::ImageDimension);
306 
309 
315  void SetPriorityImage(const PriorityImageType *priorityImage);
316 
319  Self & operator++()
320  {
321  m_NumberOfSamplesDone++;
322  this->UpdatePosition();
323  return *this;
324  }
326 
329  Self & operator--()
330  {
331  m_NumberOfSamplesDone--;
332  this->UpdatePosition();
333  return *this;
334  }
336 
338  void SetNumberOfSamples(SizeValueType number);
339 
340  SizeValueType GetNumberOfSamples(void) const;
341 
343  void ReinitializeSeed();
344 
347  void ReinitializeSeed(int);
348 
349 private:
350  void UpdatePosition();
351 
356 };
357 } // end namespace itk
358 
359 #ifndef ITK_MANUAL_INSTANTIATION
360 #include "itkImageRandomNonRepeatingConstIteratorWithIndex.hxx"
361 #endif
362 
363 #endif
364