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
26namespace itk
27{
41{
42public:
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{
74public:
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
213template <typename TImage>
214class ITK_TEMPLATE_EXPORT ImageRandomNonRepeatingConstIteratorWithIndex : public ImageConstIteratorWithIndex<TImage>
215{
216public:
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;
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
260 operator=(const Self & it);
261
263 void
264 GoToBegin()
265 {
266 m_NumberOfSamplesDone = 0L;
267 this->UpdatePosition();
268 }
272 void
273 GoToEnd()
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
298 using PriorityImageType = itk::Image<SizeValueType, Self::ImageDimension>;
299
305 void
306 SetPriorityImage(const PriorityImageType * priorityImage);
307
311 operator++()
312 {
313 ++m_NumberOfSamplesDone;
314 this->UpdatePosition();
315 return *this;
316 }
322 operator--()
323 {
324 --m_NumberOfSamplesDone;
325 this->UpdatePosition();
326 return *this;
327 }
331 void
332 SetNumberOfSamples(SizeValueType number)
333 {
334 m_NumberOfSamplesRequested = number;
335 if (number > m_NumberOfPixelsInRegion)
336 {
337 m_NumberOfSamplesRequested = m_NumberOfPixelsInRegion;
338 }
339 }
343 GetNumberOfSamples() const
344 {
345 return m_NumberOfSamplesRequested;
346 }
347
349 void
350 ReinitializeSeed();
351
354 void
355 ReinitializeSeed(int);
356
357private:
359 void
360 UpdatePosition();
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
A base class for multi-dimensional iterators templated over image type that are designed to efficient...
Self & operator=(const Self &it)
typename SizeType::SizeValueType SizeValueType
typename PixelContainer::Pointer PixelContainerPointer
typename TImage::InternalPixelType InternalPixelType
typename TImage::PixelContainer PixelContainer
A multi-dimensional image iterator that visits a random set of pixels within an image region....
Templated n-dimensional image class.
Definition: itkImage.h:89
A node to be used when computing permutations.
bool operator<(const NodeOfPermutation &b) const
Produce a random permutation of a collection.
RandomPermutation & operator=(const RandomPermutation &it)
void SetPriority(SizeValueType i, SizeValueType priority) const
SizeValueType operator[](SizeValueType i) const
typename Statistics::MersenneTwisterRandomVariateGenerator::Pointer GeneratorPointer
static Pointer New()
Method for creation through the object factory.
The "itk" namespace contains all Insight Segmentation and Registration Toolkit (ITK) classes....
long IndexValueType
Definition: itkIntTypes.h:93
unsigned long SizeValueType
Definition: itkIntTypes.h:86
long OffsetValueType
Definition: itkIntTypes.h:97