ITK  5.0.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 
97  {
98  if ( i > m_Size )
99  {
100  std::ostringstream ostrm;
101  ostrm << "Error: RandomPermuation does not have " << i << " elements" << std::endl;
102  throw std::runtime_error(ostrm.str());
103  }
104  else
105  {
106  m_Permutation[i].m_Priority = priority;
107  }
108  }
109 
110  void Shuffle()
111  {
112  for ( SizeValueType i = 0; i < m_Size; i++ )
113  {
114  m_Permutation[i].m_Value = m_Generator->GetVariateWithClosedRange (1.0);
115  m_Permutation[i].m_Index = i;
116  }
117  std::sort(m_Permutation, m_Permutation + m_Size);
118  }
119 
121  {
122  return m_Permutation[i].m_Index;
123  }
124 
126  {
127  delete[] m_Permutation;
128  }
129 
132  {
133  m_Generator->Initialize();
134  }
135 
136  void ReinitializeSeed(unsigned int seed)
137  {
138  m_Generator->SetSeed (seed);
139  }
140 };
141 
214 template< typename TImage >
216 {
217 public:
221 
224  using SizeType = typename Superclass::SizeType;
236 
240  {
241  delete m_Permutation;
242  }
244 
247  ImageRandomNonRepeatingConstIteratorWithIndex(const ImageType *ptr, const RegionType & region);
248 
256  {
258 
259  m_Permutation = nullptr;
260  }
261 
263  Self & operator=(const Self & it);
264 
266  void GoToBegin()
267  {
268  m_NumberOfSamplesDone = 0L;
269  this->UpdatePosition();
270  }
272 
274  void GoToEnd()
275  {
276  m_NumberOfSamplesDone = m_NumberOfSamplesRequested;
277  this->UpdatePosition();
278  }
280 
282  bool IsAtBegin() const
283  {
284  return ( m_NumberOfSamplesDone == 0L );
285  }
286 
288  bool IsAtEnd() const
289  {
290  return ( m_NumberOfSamplesDone >= m_NumberOfSamplesRequested );
291  }
292 
294  static constexpr unsigned int ImageDimension = TImage::ImageDimension;
295 
298 
304  void SetPriorityImage(const PriorityImageType *priorityImage);
305 
309  {
310  m_NumberOfSamplesDone++;
311  this->UpdatePosition();
312  return *this;
313  }
315 
319  {
320  m_NumberOfSamplesDone--;
321  this->UpdatePosition();
322  return *this;
323  }
325 
327  void SetNumberOfSamples(SizeValueType number);
328 
329  SizeValueType GetNumberOfSamples() const;
330 
332  void ReinitializeSeed();
333 
336  void ReinitializeSeed(int);
337 
338 private:
339  void UpdatePosition();
340 
345 };
346 } // end namespace itk
347 
348 #ifndef ITK_MANUAL_INSTANTIATION
349 #include "itkImageRandomNonRepeatingConstIteratorWithIndex.hxx"
350 #endif
351 
352 #endif
typename SizeType::SizeValueType SizeValueType
typename IndexType::IndexValueType IndexValueType
void SetPriority(SizeValueType i, SizeValueType priority)
A multi-dimensional image iterator that visits a random set of pixels within an image region...
ImageRandomNonRepeatingConstIteratorWithIndex(const ImageConstIteratorWithIndex< TImage > &it)
unsigned long SizeValueType
Definition: itkIntTypes.h:83
typename PixelContainer::Pointer PixelContainerPointer
A node to be used when computing permutations.
typename TImage::InternalPixelType InternalPixelType
typename OffsetType::OffsetValueType OffsetValueType
static Pointer New()
Method for creation through the object factory.
bool operator<(const NodeOfPermutation &b) const
A base class for multi-dimensional iterators templated over image type that are designed to efficient...
signed long IndexValueType
Definition: itkIntTypes.h:90
Produce a random permutation of a collection.
RandomPermutation & operator=(const RandomPermutation &it)
typename TImage::PixelContainer PixelContainer
Self & operator=(const Self &it)
signed long OffsetValueType
Definition: itkIntTypes.h:94
Templated n-dimensional image class.
Definition: itkImage.h:75
typename Statistics::MersenneTwisterRandomVariateGenerator::Pointer GeneratorPointer