ITK  5.4.0
Insight Toolkit
itkImageRandomNonRepeatingConstIteratorWithIndex.h
Go to the documentation of this file.
1 /*=========================================================================
2  *
3  * Copyright NumFOCUS
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  * https://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
55  operator<(const NodeOfPermutation & b) const
56  {
57  if (m_Priority == b.m_Priority)
58  {
59  return m_Value < b.m_Value;
60  }
61  else
62  {
63  return m_Priority < b.m_Priority;
64  }
65  }
66 };
67 
73 {
74 public:
79 
81  {
82  m_Size = sz;
85  this->Shuffle();
86  }
87 
90  {
91  delete[] m_Permutation;
92  m_Size = it.m_Size;
95  return *this;
96  }
97 
98  void
100  {
101  if (i > m_Size)
102  {
103  std::ostringstream ostrm;
104  ostrm << "Error: RandomPermuation does not have " << i << " elements" << std::endl;
105  throw std::runtime_error(ostrm.str());
106  }
107  else
108  {
109  m_Permutation[i].m_Priority = priority;
110  }
111  }
112 
113  void
114  Shuffle() const
115  {
116  for (SizeValueType i = 0; i < m_Size; ++i)
117  {
118  m_Permutation[i].m_Value = m_Generator->GetVariateWithClosedRange(1.0);
119  m_Permutation[i].m_Index = i;
120  }
121  std::sort(m_Permutation, m_Permutation + m_Size);
122  }
123 
125 
127 
129  void
131  {
132  m_Generator->Initialize();
133  }
134 
135  void
136  ReinitializeSeed(unsigned int seed) const
137  {
138  m_Generator->SetSeed(seed);
139  }
140 };
141 
213 template <typename TImage>
215 {
216 public:
220 
222  using typename Superclass::IndexType;
223  using typename Superclass::SizeType;
224  using typename Superclass::OffsetType;
225  using typename Superclass::RegionType;
226  using typename Superclass::ImageType;
227  using typename Superclass::PixelContainer;
228  using typename Superclass::PixelContainerPointer;
229  using typename Superclass::InternalPixelType;
230  using typename Superclass::PixelType;
231  using typename Superclass::AccessorType;
232  using typename Superclass::IndexValueType;
233  using typename Superclass::OffsetValueType;
234  using typename Superclass::SizeValueType;
235 
238  ~ImageRandomNonRepeatingConstIteratorWithIndex() override { delete m_Permutation; }
243  ImageRandomNonRepeatingConstIteratorWithIndex(const ImageType * ptr, const RegionType & region);
244 
252  {
254 
255  m_Permutation = nullptr;
256  }
257 
259  Self &
260  operator=(const Self & it);
261 
263  void
265  {
266  m_NumberOfSamplesDone = 0L;
267  this->UpdatePosition();
268  }
272  void
274  {
275  m_NumberOfSamplesDone = m_NumberOfSamplesRequested;
276  this->UpdatePosition();
277  }
281  bool
282  IsAtBegin() const
283  {
284  return (m_NumberOfSamplesDone == 0L);
285  }
286 
288  bool
289  IsAtEnd() const
290  {
291  return (m_NumberOfSamplesDone >= m_NumberOfSamplesRequested);
292  }
293 
295  static constexpr unsigned int ImageDimension = TImage::ImageDimension;
296 
299 
305  void
306  SetPriorityImage(const PriorityImageType * priorityImage);
307 
310  Self &
312  {
313  ++m_NumberOfSamplesDone;
314  this->UpdatePosition();
315  return *this;
316  }
321  Self &
323  {
324  --m_NumberOfSamplesDone;
325  this->UpdatePosition();
326  return *this;
327  }
331  void
333  {
334  m_NumberOfSamplesRequested = number;
335  if (number > m_NumberOfPixelsInRegion)
336  {
337  m_NumberOfSamplesRequested = m_NumberOfPixelsInRegion;
338  }
339  }
344  {
345  return m_NumberOfSamplesRequested;
346  }
347 
349  void
350  ReinitializeSeed();
351 
354  void
355  ReinitializeSeed(int);
356 
357 private:
359  void
360  UpdatePosition();
361 
362  SizeValueType m_NumberOfSamplesRequested{};
363  SizeValueType m_NumberOfSamplesDone{};
364  SizeValueType m_NumberOfPixelsInRegion{};
365  RandomPermutation * m_Permutation{};
366 };
367 } // end namespace itk
368 
369 #ifndef ITK_MANUAL_INSTANTIATION
370 # include "itkImageRandomNonRepeatingConstIteratorWithIndex.hxx"
371 #endif
372 
373 #endif
itk::RandomPermutation::operator[]
SizeValueType operator[](SizeValueType i) const
Definition: itkImageRandomNonRepeatingConstIteratorWithIndex.h:124
itk::ImageRandomNonRepeatingConstIteratorWithIndex::~ImageRandomNonRepeatingConstIteratorWithIndex
~ImageRandomNonRepeatingConstIteratorWithIndex() override
Definition: itkImageRandomNonRepeatingConstIteratorWithIndex.h:238
itk::RandomPermutation::m_Size
SizeValueType m_Size
Definition: itkImageRandomNonRepeatingConstIteratorWithIndex.h:78
itk::RandomPermutation
Produce a random permutation of a collection.
Definition: itkImageRandomNonRepeatingConstIteratorWithIndex.h:72
itk::RandomPermutation::GeneratorPointer
typename Statistics::MersenneTwisterRandomVariateGenerator::Pointer GeneratorPointer
Definition: itkImageRandomNonRepeatingConstIteratorWithIndex.h:75
itk::ImageRandomNonRepeatingConstIteratorWithIndex::operator++
Self & operator++()
Definition: itkImageRandomNonRepeatingConstIteratorWithIndex.h:311
itk::NodeOfPermutation::m_Priority
SizeValueType m_Priority
Definition: itkImageRandomNonRepeatingConstIteratorWithIndex.h:43
itk::ImageRandomNonRepeatingConstIteratorWithIndex::SetNumberOfSamples
void SetNumberOfSamples(SizeValueType number)
Definition: itkImageRandomNonRepeatingConstIteratorWithIndex.h:332
itk::ImageConstIteratorWithIndex::PixelContainerPointer
typename PixelContainer::Pointer PixelContainerPointer
Definition: itkImageConstIteratorWithIndex.h:122
itk::ImageRandomNonRepeatingConstIteratorWithIndex::IsAtBegin
bool IsAtBegin() const
Definition: itkImageRandomNonRepeatingConstIteratorWithIndex.h:282
itkImageConstIteratorWithIndex.h
itk::GTest::TypedefsAndConstructors::Dimension2::SizeType
ImageBaseType::SizeType SizeType
Definition: itkGTestTypedefsAndConstructors.h:49
itk::RandomPermutation::SetPriority
void SetPriority(SizeValueType i, SizeValueType priority) const
Definition: itkImageRandomNonRepeatingConstIteratorWithIndex.h:99
itk::ImageConstIteratorWithIndex::PixelType
typename TImage::PixelType PixelType
Definition: itkImageConstIteratorWithIndex.h:128
itk::IndexValueType
long IndexValueType
Definition: itkIntTypes.h:90
itk::ImageConstIteratorWithIndex::InternalPixelType
typename TImage::InternalPixelType InternalPixelType
Definition: itkImageConstIteratorWithIndex.h:125
itk::NodeOfPermutation::NodeOfPermutation
NodeOfPermutation()
Definition: itkImageRandomNonRepeatingConstIteratorWithIndex.h:47
itk::GTest::TypedefsAndConstructors::Dimension2::IndexType
ImageBaseType::IndexType IndexType
Definition: itkGTestTypedefsAndConstructors.h:50
itk::RandomPermutation::ReinitializeSeed
void ReinitializeSeed() const
Definition: itkImageRandomNonRepeatingConstIteratorWithIndex.h:130
itk::GTest::TypedefsAndConstructors::Dimension2::RegionType
ImageBaseType::RegionType RegionType
Definition: itkGTestTypedefsAndConstructors.h:54
itk::NodeOfPermutation
A node to be used when computing permutations.
Definition: itkImageRandomNonRepeatingConstIteratorWithIndex.h:40
itk::NodeOfPermutation::m_Index
SizeValueType m_Index
Definition: itkImageRandomNonRepeatingConstIteratorWithIndex.h:44
itk::ImageConstIteratorWithIndex::AccessorType
typename TImage::AccessorType AccessorType
Definition: itkImageConstIteratorWithIndex.h:132
itk::Statistics::MersenneTwisterRandomVariateGenerator::New
static Pointer New()
Method for creation through the object factory.
itk::ImageRandomNonRepeatingConstIteratorWithIndex::operator--
Self & operator--()
Definition: itkImageRandomNonRepeatingConstIteratorWithIndex.h:322
itk::NodeOfPermutation::operator<
bool operator<(const NodeOfPermutation &b) const
Definition: itkImageRandomNonRepeatingConstIteratorWithIndex.h:55
itk::RandomPermutation::~RandomPermutation
~RandomPermutation()
Definition: itkImageRandomNonRepeatingConstIteratorWithIndex.h:126
itk::ImageConstIteratorWithIndex
A base class for multi-dimensional iterators templated over image type that are designed to efficient...
Definition: itkImageConstIteratorWithIndex.h:92
itk::RandomPermutation::operator=
RandomPermutation & operator=(const RandomPermutation &it)
Definition: itkImageRandomNonRepeatingConstIteratorWithIndex.h:89
itk::OffsetValueType
long OffsetValueType
Definition: itkIntTypes.h:94
itk::ImageRandomNonRepeatingConstIteratorWithIndex::GetNumberOfSamples
SizeValueType GetNumberOfSamples() const
Definition: itkImageRandomNonRepeatingConstIteratorWithIndex.h:343
itk::RandomPermutation::m_Permutation
NodeOfPermutation * m_Permutation
Definition: itkImageRandomNonRepeatingConstIteratorWithIndex.h:76
itkMersenneTwisterRandomVariateGenerator.h
itk::ImageRandomNonRepeatingConstIteratorWithIndex::GoToEnd
void GoToEnd()
Definition: itkImageRandomNonRepeatingConstIteratorWithIndex.h:273
itk::Statistics::MersenneTwisterRandomVariateGenerator::Pointer
SmartPointer< Self > Pointer
Definition: itkMersenneTwisterRandomVariateGenerator.h:133
itk
The "itk" namespace contains all Insight Segmentation and Registration Toolkit (ITK) classes....
Definition: itkAnnulusOperator.h:24
itk::RandomPermutation::m_Generator
GeneratorPointer m_Generator
Definition: itkImageRandomNonRepeatingConstIteratorWithIndex.h:77
itk::ImageRandomNonRepeatingConstIteratorWithIndex
A multi-dimensional image iterator that visits a random set of pixels within an image region....
Definition: itkImageRandomNonRepeatingConstIteratorWithIndex.h:214
itk::ImageRandomNonRepeatingConstIteratorWithIndex::IsAtEnd
bool IsAtEnd() const
Definition: itkImageRandomNonRepeatingConstIteratorWithIndex.h:289
itk::Image
Templated n-dimensional image class.
Definition: itkImage.h:88
itk::RandomPermutation::ReinitializeSeed
void ReinitializeSeed(unsigned int seed) const
Definition: itkImageRandomNonRepeatingConstIteratorWithIndex.h:136
AddImageFilter
Definition: itkAddImageFilter.h:81
itk::ImageConstIteratorWithIndex::ImageType
TImage ImageType
Definition: itkImageConstIteratorWithIndex.h:116
itk::ImageConstIteratorWithIndex::PixelContainer
typename TImage::PixelContainer PixelContainer
Definition: itkImageConstIteratorWithIndex.h:121
itk::ImageConstIteratorWithIndex::SizeValueType
typename SizeType::SizeValueType SizeValueType
Definition: itkImageConstIteratorWithIndex.h:110
itk::ImageConstIteratorWithIndex::operator=
Self & operator=(const Self &it)
itk::ImageRandomNonRepeatingConstIteratorWithIndex::GoToBegin
void GoToBegin()
Definition: itkImageRandomNonRepeatingConstIteratorWithIndex.h:264
itk::SizeValueType
unsigned long SizeValueType
Definition: itkIntTypes.h:83
itk::RandomPermutation::Shuffle
void Shuffle() const
Definition: itkImageRandomNonRepeatingConstIteratorWithIndex.h:114
itk::RandomPermutation::RandomPermutation
RandomPermutation(SizeValueType sz)
Definition: itkImageRandomNonRepeatingConstIteratorWithIndex.h:80
itk::NodeOfPermutation::m_Value
double m_Value
Definition: itkImageRandomNonRepeatingConstIteratorWithIndex.h:45
itk::ImageConstIteratorWithIndex::OffsetType
typename TImage::OffsetType OffsetType
Definition: itkImageConstIteratorWithIndex.h:136
itk::ImageRandomNonRepeatingConstIteratorWithIndex::ImageRandomNonRepeatingConstIteratorWithIndex
ImageRandomNonRepeatingConstIteratorWithIndex(const ImageConstIteratorWithIndex< TImage > &it)
Definition: itkImageRandomNonRepeatingConstIteratorWithIndex.h:251