ITK  4.3.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 
87  void Dump()
88  {
89  for ( SizeValueType i = 0; i < m_Size; i++ )
90  {
91  std::cout << m_Permutation[i].m_Value << " " << m_Permutation[i].m_Priority
92  << " " << m_Permutation[i].m_Index << ";";
93  std::cout << std::endl;
94  }
95  }
96 
98  {
99  if ( i > m_Size )
100  {
101  std::cerr << "Error - i dont have " << i << " elements" << std::endl;
102  }
103  else
104  {
105  m_Permutation[i].m_Priority = priority;
106  }
107  }
108 
109  void Shuffle()
110  {
111  for ( SizeValueType i = 0; i < m_Size; i++ )
112  {
113  m_Permutation[i].m_Value = m_Generator->GetVariateWithClosedRange (1.0);
114  m_Permutation[i].m_Index = i;
115  }
116  std::sort(m_Permutation, m_Permutation + m_Size);
117  }
118 
120  {
121  return m_Permutation[i].m_Index;
122  }
123 
125  {
126  delete[] m_Permutation;
127  }
128 
131  {
132  m_Generator->Initialize();
133  }
134 
135  void ReinitializeSeed(int seed)
136  {
137  m_Generator->SetSeed (seed);
138  }
139 };
140 
213 template< typename TImage >
215 {
216 public:
220 
223  typedef typename Superclass::SizeType SizeType;
235 
239  {
240  if ( m_Permutation )
241  {
242  delete m_Permutation;
243  }
244  }
246 
249  ImageRandomNonRepeatingConstIteratorWithIndex(const ImageType *ptr, const RegionType & region);
250 
258  {
260 
261  m_Permutation = NULL;
262  }
263 
265  Self & operator=(const Self & it);
266 
268  void GoToBegin(void)
269  {
270  m_NumberOfSamplesDone = 0L;
271  this->UpdatePosition();
272  }
274 
276  void GoToEnd(void)
277  {
278  m_NumberOfSamplesDone = m_NumberOfSamplesRequested;
279  this->UpdatePosition();
280  }
282 
284  bool IsAtBegin(void) const
285  {
286  return ( m_NumberOfSamplesDone == 0L );
287  }
288 
290  bool IsAtEnd(void) const
291  {
292  return ( m_NumberOfSamplesDone >= m_NumberOfSamplesRequested );
293  }
294 
296  itkStaticConstMacro(ImageDimension, unsigned int, TImage::ImageDimension);
297 
300 
306  void SetPriorityImage(const PriorityImageType *priorityImage);
307 
310  Self & operator++()
311  {
312  m_NumberOfSamplesDone++;
313  this->UpdatePosition();
314  return *this;
315  }
317 
320  Self & operator--()
321  {
322  m_NumberOfSamplesDone--;
323  this->UpdatePosition();
324  return *this;
325  }
327 
329  void SetNumberOfSamples(SizeValueType number);
330 
331  SizeValueType GetNumberOfSamples(void) const;
332 
334  void ReinitializeSeed();
335 
338  void ReinitializeSeed(int);
339 
340 private:
341  void UpdatePosition();
342 
347 };
348 } // end namespace itk
349 
350 #ifndef ITK_MANUAL_INSTANTIATION
351 #include "itkImageRandomNonRepeatingConstIteratorWithIndex.hxx"
352 #endif
353 
354 #endif
355