ITK  6.0.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 
126  {
127  return m_Permutation[i].m_Index;
128  }
129 
131 
133  void
135  {
136  m_Generator->Initialize();
137  }
138 
139  void
140  ReinitializeSeed(unsigned int seed) const
141  {
142  m_Generator->SetSeed(seed);
143  }
144 };
145 
217 template <typename TImage>
219 {
220 public:
224 
226  using typename Superclass::IndexType;
227  using typename Superclass::SizeType;
228  using typename Superclass::OffsetType;
229  using typename Superclass::RegionType;
230  using typename Superclass::ImageType;
231  using typename Superclass::PixelContainer;
232  using typename Superclass::PixelContainerPointer;
233  using typename Superclass::InternalPixelType;
234  using typename Superclass::PixelType;
235  using typename Superclass::AccessorType;
236  using typename Superclass::IndexValueType;
237  using typename Superclass::OffsetValueType;
238  using typename Superclass::SizeValueType;
239 
242  ~ImageRandomNonRepeatingConstIteratorWithIndex() override { delete m_Permutation; }
247  ImageRandomNonRepeatingConstIteratorWithIndex(const ImageType * ptr, const RegionType & region);
248 
256  {
258 
259  m_Permutation = nullptr;
260  }
261 
263  Self &
264  operator=(const Self & it);
265 
267  void
269  {
270  m_NumberOfSamplesDone = 0L;
271  this->UpdatePosition();
272  }
276  void
278  {
279  m_NumberOfSamplesDone = m_NumberOfSamplesRequested;
280  this->UpdatePosition();
281  }
285  bool
286  IsAtBegin() const
287  {
288  return (m_NumberOfSamplesDone == 0L);
289  }
290 
292  bool
293  IsAtEnd() const
294  {
295  return (m_NumberOfSamplesDone >= m_NumberOfSamplesRequested);
296  }
297 
299  static constexpr unsigned int ImageDimension = TImage::ImageDimension;
300 
303 
309  void
310  SetPriorityImage(const PriorityImageType * priorityImage);
311 
314  Self &
316  {
317  ++m_NumberOfSamplesDone;
318  this->UpdatePosition();
319  return *this;
320  }
325  Self &
327  {
328  --m_NumberOfSamplesDone;
329  this->UpdatePosition();
330  return *this;
331  }
335  void
337  {
338  m_NumberOfSamplesRequested = number;
339  if (number > m_NumberOfPixelsInRegion)
340  {
341  m_NumberOfSamplesRequested = m_NumberOfPixelsInRegion;
342  }
343  }
348  {
349  return m_NumberOfSamplesRequested;
350  }
351 
353  void
354  ReinitializeSeed();
355 
358  void
359  ReinitializeSeed(int);
360 
361 private:
363  void
364  UpdatePosition();
365 
366  SizeValueType m_NumberOfSamplesRequested{};
367  SizeValueType m_NumberOfSamplesDone{};
368  SizeValueType m_NumberOfPixelsInRegion{};
369  RandomPermutation * m_Permutation{};
370 };
371 } // end namespace itk
372 
373 #ifndef ITK_MANUAL_INSTANTIATION
374 # include "itkImageRandomNonRepeatingConstIteratorWithIndex.hxx"
375 #endif
376 
377 #endif
itk::RandomPermutation::operator[]
SizeValueType operator[](SizeValueType i) const
Definition: itkImageRandomNonRepeatingConstIteratorWithIndex.h:125
itk::ImageRandomNonRepeatingConstIteratorWithIndex::~ImageRandomNonRepeatingConstIteratorWithIndex
~ImageRandomNonRepeatingConstIteratorWithIndex() override
Definition: itkImageRandomNonRepeatingConstIteratorWithIndex.h:242
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:315
itk::NodeOfPermutation::m_Priority
SizeValueType m_Priority
Definition: itkImageRandomNonRepeatingConstIteratorWithIndex.h:43
itk::ImageRandomNonRepeatingConstIteratorWithIndex::SetNumberOfSamples
void SetNumberOfSamples(SizeValueType number)
Definition: itkImageRandomNonRepeatingConstIteratorWithIndex.h:336
itk::ImageConstIteratorWithIndex::PixelContainerPointer
typename PixelContainer::Pointer PixelContainerPointer
Definition: itkImageConstIteratorWithIndex.h:122
itk::ImageRandomNonRepeatingConstIteratorWithIndex::IsAtBegin
bool IsAtBegin() const
Definition: itkImageRandomNonRepeatingConstIteratorWithIndex.h:286
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:93
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:134
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:326
itk::NodeOfPermutation::operator<
bool operator<(const NodeOfPermutation &b) const
Definition: itkImageRandomNonRepeatingConstIteratorWithIndex.h:55
itk::RandomPermutation::~RandomPermutation
~RandomPermutation()
Definition: itkImageRandomNonRepeatingConstIteratorWithIndex.h:130
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:97
itk::ImageRandomNonRepeatingConstIteratorWithIndex::GetNumberOfSamples
SizeValueType GetNumberOfSamples() const
Definition: itkImageRandomNonRepeatingConstIteratorWithIndex.h:347
itk::RandomPermutation::m_Permutation
NodeOfPermutation * m_Permutation
Definition: itkImageRandomNonRepeatingConstIteratorWithIndex.h:76
itkMersenneTwisterRandomVariateGenerator.h
itk::ImageRandomNonRepeatingConstIteratorWithIndex::GoToEnd
void GoToEnd()
Definition: itkImageRandomNonRepeatingConstIteratorWithIndex.h:277
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: itkAnatomicalOrientation.h:29
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:218
itk::ImageRandomNonRepeatingConstIteratorWithIndex::IsAtEnd
bool IsAtEnd() const
Definition: itkImageRandomNonRepeatingConstIteratorWithIndex.h:293
itk::Image
Templated n-dimensional image class.
Definition: itkImage.h:88
itk::RandomPermutation::ReinitializeSeed
void ReinitializeSeed(unsigned int seed) const
Definition: itkImageRandomNonRepeatingConstIteratorWithIndex.h:140
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:268
itk::SizeValueType
unsigned long SizeValueType
Definition: itkIntTypes.h:86
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:255