ITK  5.4.0
Insight Toolkit
itkSubsample.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 itkSubsample_h
19#define itkSubsample_h
20
21#include "itkSample.h"
22#include "itkMacro.h"
23#include "itkObjectFactory.h"
24
25namespace itk
26{
27namespace Statistics
28{
41template <typename TSample>
42class ITK_TEMPLATE_EXPORT Subsample : public TSample
43{
44public:
45 ITK_DISALLOW_COPY_AND_MOVE(Subsample);
46
48 using Self = Subsample;
49 using Superclass = TSample;
52
54 itkOverrideGetNameOfClassMacro(Subsample);
55
57 itkNewMacro(Self);
58
61
64 using MeasurementVectorType = typename TSample::MeasurementVectorType;
65 using MeasurementType = typename TSample::MeasurementType;
66 using InstanceIdentifier = typename TSample::InstanceIdentifier;
68
69 using AbsoluteFrequencyType = typename TSample::AbsoluteFrequencyType;
70 using TotalAbsoluteFrequencyType = typename TSample::TotalAbsoluteFrequencyType;
71
76 using InstanceIdentifierHolder = std::vector<InstanceIdentifier>;
77
78// Disable clang warning false positive.
79// <https://llvm.org/bugs/show_bug.cgi?id=22582>
80#if defined(__clang__) && defined(__has_warning)
81# if __has_warning("-Winconsistent-missing-override")
82# pragma clang diagnostic push
83# pragma clang diagnostic ignored "-Winconsistent-missing-override"
84# endif
85#endif
86
90 {
91 return this->m_IdHolder;
92 }
93
94#if defined(__clang__) && defined(__has_warning)
95# if __has_warning("-Winconsistent-missing-override")
96# pragma clang diagnostic pop
97# endif
98#endif
99
101 void
102 SetSample(const TSample * sample);
103
104 const TSample *
105 GetSample() const;
106
108 void
110
112 void
114
118 Size() const override;
119
121 void
123
128
132
135 GetTotalFrequency() const override;
136
137 void
138 Swap(unsigned int index1, unsigned int index2);
139
141 GetInstanceIdentifier(unsigned int index);
142
144 GetMeasurementVectorByIndex(unsigned int index) const;
145
147 GetFrequencyByIndex(unsigned int index) const;
148
150 void
151 Graft(const DataObject * thatObject) override;
152
154 {
155 friend class Subsample;
156
157 public:
158 ConstIterator(const Self * sample) { *this = sample->Begin(); }
159
161 {
162 m_Iter = iter.m_Iter;
163 m_Subsample = iter.m_Subsample;
164 m_Sample = iter.m_Sample;
165 }
166
169 {
170 m_Iter = iter.m_Iter;
171 m_Subsample = iter.m_Subsample;
172 m_Sample = iter.m_Sample;
173 return *this;
174 }
175
176 bool
177 operator==(const ConstIterator & it) const
178 {
179 return (m_Iter == it.m_Iter);
180 }
181
183
186 {
187 ++m_Iter;
188 return *this;
189 }
190
193 {
194 return m_Sample->GetFrequency(*m_Iter);
195 }
196
199 {
200 return m_Sample->GetMeasurementVector(*m_Iter);
201 }
202
205 {
206 return (m_Iter - m_Subsample->GetIdHolder().begin());
207 }
208
209 protected:
210 // Purposely not implemented
212
213 // Only to be called from the Subsample
214 ConstIterator(typename InstanceIdentifierHolder::const_iterator iter, const Self * classSample)
215 : m_Iter(iter)
216 , m_Subsample(classSample)
217 , m_Sample(classSample->GetSample())
218 {}
219
220 // ConstIterator pointing to ImageToListSampleAdaptor
221 typename InstanceIdentifierHolder::const_iterator m_Iter;
222
223 // Pointer to Subsample object
225 const TSample * m_Sample;
226
227 private:
228 };
229
230 class Iterator : public ConstIterator
231 {
232 friend class Subsample;
233
234 public:
235 Iterator(Self * sample)
236 : ConstIterator(sample)
237 {}
238
239 Iterator(const Iterator & iter)
240 : ConstIterator(iter)
241 {}
242
243 Iterator &
244 operator=(const Iterator & iter)
245 {
246 this->ConstIterator::operator=(iter);
247 return *this;
248 }
249
250 protected:
251 // To ensure const-correctness these method must not be in the public API.
252 // The are purposely not implemented, since they should never be called.
254 Iterator(const Self * sample);
255 Iterator(typename InstanceIdentifierHolder::const_iterator iter, const Self * classSample);
259
260 // Only to be called from the Subsample
261 Iterator(typename InstanceIdentifierHolder::iterator iter, Self * classSample)
262 : ConstIterator(iter, classSample)
263 {}
264
265 private:
266 };
267
270 Iterator
272 {
273 Iterator iter(m_IdHolder.begin(), this);
274
275 return iter;
276 }
277
280 Iterator
282 {
283 Iterator iter(m_IdHolder.end(), this);
284
285 return iter;
286 }
287
288 ConstIterator
289 Begin() const
290 {
291 ConstIterator iter(m_IdHolder.begin(), this);
292
293 return iter;
294 }
295
296 ConstIterator
297 End() const
298 {
299 ConstIterator iter(m_IdHolder.end(), this);
300
301 return iter;
302 }
303
304protected:
306 ~Subsample() override = default;
307 void
308 PrintSelf(std::ostream & os, Indent indent) const override;
309
310private:
311 const TSample * m_Sample{};
313 unsigned int m_ActiveDimension{};
314 TotalAbsoluteFrequencyType m_TotalFrequency{};
315}; // end of class
316} // end of namespace Statistics
317} // end of namespace itk
318
319#ifndef ITK_MANUAL_INSTANTIATION
320# include "itkSubsample.hxx"
321#endif
322
323#endif
Base class for all data objects in ITK.
Control indentation during Print() invocation.
Definition: itkIndent.h:50
InstanceIdentifier GetInstanceIdentifier() const
Definition: itkSubsample.h:204
bool operator==(const ConstIterator &it) const
Definition: itkSubsample.h:177
AbsoluteFrequencyType GetFrequency() const
Definition: itkSubsample.h:192
InstanceIdentifierHolder::const_iterator m_Iter
Definition: itkSubsample.h:221
const MeasurementVectorType & GetMeasurementVector() const
Definition: itkSubsample.h:198
ConstIterator(const ConstIterator &iter)
Definition: itkSubsample.h:160
ConstIterator & operator=(const ConstIterator &iter)
Definition: itkSubsample.h:168
ConstIterator(typename InstanceIdentifierHolder::const_iterator iter, const Self *classSample)
Definition: itkSubsample.h:214
ConstIterator & operator=(const ConstIterator &it)
Iterator(typename InstanceIdentifierHolder::iterator iter, Self *classSample)
Definition: itkSubsample.h:261
Iterator(typename InstanceIdentifierHolder::const_iterator iter, const Self *classSample)
Iterator(const ConstIterator &it)
Iterator & operator=(const Iterator &iter)
Definition: itkSubsample.h:244
This class stores a subset of instance identifiers from another sample object. You can create a subsa...
Definition: itkSubsample.h:43
void Graft(const DataObject *thatObject) override
TotalAbsoluteFrequencyType GetTotalFrequency() const override
AbsoluteFrequencyType GetFrequencyByIndex(unsigned int index) const
InstanceIdentifier GetInstanceIdentifier(unsigned int index)
ConstIterator Begin() const
Definition: itkSubsample.h:289
typename TSample::AbsoluteFrequencyType AbsoluteFrequencyType
Definition: itkSubsample.h:69
typename TSample::InstanceIdentifier InstanceIdentifier
Definition: itkSubsample.h:66
MeasurementVectorType ValueType
Definition: itkSubsample.h:67
void Swap(unsigned int index1, unsigned int index2)
InstanceIdentifier Size() const override
typename TSample::MeasurementVectorType MeasurementVectorType
Definition: itkSubsample.h:64
~Subsample() override=default
const TSample * GetSample() const
typename TSample::TotalAbsoluteFrequencyType TotalAbsoluteFrequencyType
Definition: itkSubsample.h:70
void PrintSelf(std::ostream &os, Indent indent) const override
const MeasurementVectorType & GetMeasurementVectorByIndex(unsigned int index) const
AbsoluteFrequencyType GetFrequency(InstanceIdentifier id) const override
void AddInstance(InstanceIdentifier id)
std::vector< InstanceIdentifier > InstanceIdentifierHolder
Definition: itkSubsample.h:76
const MeasurementVectorType & GetMeasurementVector(InstanceIdentifier id) const override
ConstIterator End() const
Definition: itkSubsample.h:297
typename TSample::MeasurementType MeasurementType
Definition: itkSubsample.h:65
const InstanceIdentifierHolder & GetIdHolder() const
Definition: itkSubsample.h:89
void SetSample(const TSample *sample)
typename TSample::Pointer SamplePointer
Definition: itkSubsample.h:60
SmartPointer< Self > Pointer
The "itk" namespace contains all Insight Segmentation and Registration Toolkit (ITK) classes....