ITK  5.3.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  * 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 itkSubsample_h
19 #define itkSubsample_h
20 
21 #include "itkSample.h"
22 #include "itkMacro.h"
23 #include "itkObjectFactory.h"
24 
25 namespace itk
26 {
27 namespace Statistics
28 {
41 template <typename TSample>
42 class ITK_TEMPLATE_EXPORT Subsample : public TSample
43 {
44 public:
45  ITK_DISALLOW_COPY_AND_MOVE(Subsample);
46 
48  using Self = Subsample;
49  using Superclass = TSample;
52 
54  itkTypeMacro(Subsample, TSample);
55 
57  itkNewMacro(Self);
58 
60  using SamplePointer = typename TSample::Pointer;
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 
89  GetIdHolder() const
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
109  InitializeWithAllInstances();
110 
112  void
113  AddInstance(InstanceIdentifier id);
114 
117  InstanceIdentifier
118  Size() const override;
119 
121  void
122  Clear();
123 
126  const MeasurementVectorType &
127  GetMeasurementVector(InstanceIdentifier id) const override;
128 
130  AbsoluteFrequencyType
131  GetFrequency(InstanceIdentifier id) const override;
132 
134  TotalAbsoluteFrequencyType
135  GetTotalFrequency() const override;
136 
137  void
138  Swap(unsigned int index1, unsigned int index2);
139 
140  InstanceIdentifier
141  GetInstanceIdentifier(unsigned int index);
142 
143  const MeasurementVectorType &
144  GetMeasurementVectorByIndex(unsigned int index) const;
145 
146  AbsoluteFrequencyType
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 
167  ConstIterator &
168  operator=(const ConstIterator & iter)
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 
182  bool
183  operator==(const ConstIterator & it) const
184  {
185  return (m_Iter == it.m_Iter);
186  }
187 
188  ConstIterator &
190  {
191  ++m_Iter;
192  return *this;
193  }
194 
196  GetFrequency() const
197  {
198  return m_Sample->GetFrequency(*m_Iter);
199  }
200 
201  const MeasurementVectorType &
203  {
204  return m_Sample->GetMeasurementVector(*m_Iter);
205  }
206 
209  {
210  return (m_Iter - m_Subsample->GetIdHolder().begin());
211  }
212 
213  protected:
214  // Purposely not implemented
215  ConstIterator();
216 
217  // Only to be called from the Subsample
218  ConstIterator(typename InstanceIdentifierHolder::const_iterator iter, const Self * classSample)
219  : m_Iter(iter)
220  , m_Subsample(classSample)
221  , m_Sample(classSample->GetSample())
222  {}
223 
224  // ConstIterator pointing to ImageToListSampleAdaptor
225  typename InstanceIdentifierHolder::const_iterator m_Iter;
226 
227  // Pointer to Subsample object
228  const Self * m_Subsample;
229  const TSample * m_Sample;
230 
231  private:
232  };
233 
234  class Iterator : public ConstIterator
235  {
236  friend class Subsample;
237 
238  public:
239  Iterator(Self * sample)
240  : ConstIterator(sample)
241  {}
242 
243  Iterator(const Iterator & iter)
244  : ConstIterator(iter)
245  {}
246 
247  Iterator &
248  operator=(const Iterator & iter)
249  {
250  this->ConstIterator::operator=(iter);
251  return *this;
252  }
253 
254  protected:
255  // To ensure const-correctness these method must not be in the public API.
256  // The are purposly not implemented, since they should never be called.
257  Iterator();
258  Iterator(const Self * sample);
259  Iterator(typename InstanceIdentifierHolder::const_iterator iter, const Self * classSample);
260  Iterator(const ConstIterator & it);
261  ConstIterator &
262  operator=(const ConstIterator & it);
263 
264  // Only to be called from the Subsample
265  Iterator(typename InstanceIdentifierHolder::iterator iter, Self * classSample)
266  : ConstIterator(iter, classSample)
267  {}
268 
269  private:
270  };
271 
274  Iterator
276  {
277  Iterator iter(m_IdHolder.begin(), this);
278 
279  return iter;
280  }
281 
284  Iterator
285  End()
286  {
287  Iterator iter(m_IdHolder.end(), this);
288 
289  return iter;
290  }
291 
292  ConstIterator
293  Begin() const
294  {
295  ConstIterator iter(m_IdHolder.begin(), this);
296 
297  return iter;
298  }
299 
300  ConstIterator
301  End() const
302  {
303  ConstIterator iter(m_IdHolder.end(), this);
304 
305  return iter;
306  }
307 
308 protected:
309  Subsample();
310  ~Subsample() override = default;
311  void
312  PrintSelf(std::ostream & os, Indent indent) const override;
313 
314 private:
315  const TSample * m_Sample;
317  unsigned int m_ActiveDimension;
319 }; // end of class
320 } // end of namespace Statistics
321 } // end of namespace itk
322 
323 #ifndef ITK_MANUAL_INSTANTIATION
324 # include "itkSubsample.hxx"
325 #endif
326 
327 #endif
itk::Statistics::Subsample::Begin
ConstIterator Begin() const
Definition: itkSubsample.h:293
itkObjectFactory.h
itk::Statistics::Subsample::Iterator::Iterator
Iterator(const Iterator &iter)
Definition: itkSubsample.h:243
itk::Statistics::Subsample::GetIdHolder
const InstanceIdentifierHolder & GetIdHolder() const
Definition: itkSubsample.h:89
itk::Statistics::Subsample::InstanceIdentifier
typename TSample::InstanceIdentifier InstanceIdentifier
Definition: itkSubsample.h:66
itk::Statistics::Subsample::ConstIterator::m_Sample
const TSample * m_Sample
Definition: itkSubsample.h:229
itk::Size
Represent a n-dimensional size (bounds) of a n-dimensional image.
Definition: itkSize.h:69
itk::Statistics::Subsample::Superclass
TSample Superclass
Definition: itkSubsample.h:49
itk::Statistics::Subsample::MeasurementVectorType
typename TSample::MeasurementVectorType MeasurementVectorType
Definition: itkSubsample.h:64
itk::Statistics::Subsample::End
ConstIterator End() const
Definition: itkSubsample.h:301
itk::Statistics::Subsample::AbsoluteFrequencyType
typename TSample::AbsoluteFrequencyType AbsoluteFrequencyType
Definition: itkSubsample.h:69
itk::Statistics::Subsample::SamplePointer
typename TSample::Pointer SamplePointer
Definition: itkSubsample.h:60
itk::Statistics::Subsample::m_IdHolder
InstanceIdentifierHolder m_IdHolder
Definition: itkSubsample.h:316
itk::SmartPointer< Self >
itk::Indent
Control indentation during Print() invocation.
Definition: itkIndent.h:49
itk::Statistics::Subsample::InstanceIdentifierHolder
std::vector< InstanceIdentifier > InstanceIdentifierHolder
Definition: itkSubsample.h:76
itk::Statistics::Subsample::Iterator
Definition: itkSubsample.h:234
itk::Statistics::Subsample::ConstIterator::GetFrequency
AbsoluteFrequencyType GetFrequency() const
Definition: itkSubsample.h:196
itk::Statistics::Subsample::Iterator::operator=
Iterator & operator=(const Iterator &iter)
Definition: itkSubsample.h:248
itk::Statistics::Subsample::Iterator::Iterator
Iterator(typename InstanceIdentifierHolder::iterator iter, Self *classSample)
Definition: itkSubsample.h:265
itkMacro.h
itk::Statistics::Subsample
This class stores a subset of instance identifiers from another sample object. You can create a subsa...
Definition: itkSubsample.h:42
itk::Statistics::Subsample::ConstIterator::GetMeasurementVector
const MeasurementVectorType & GetMeasurementVector() const
Definition: itkSubsample.h:202
itk::Statistics::Subsample::ConstIterator::GetInstanceIdentifier
InstanceIdentifier GetInstanceIdentifier() const
Definition: itkSubsample.h:208
itk::Statistics::Subsample::ConstIterator::operator!=
bool operator!=(const ConstIterator &it) const
Definition: itkSubsample.h:177
itk::Statistics::Subsample::ConstIterator::m_Subsample
const Self * m_Subsample
Definition: itkSubsample.h:228
itk::Statistics::Subsample::ConstIterator::ConstIterator
ConstIterator(const ConstIterator &iter)
Definition: itkSubsample.h:160
itk
The "itk" namespace contains all Insight Segmentation and Registration Toolkit (ITK) classes....
Definition: itkAnnulusOperator.h:24
itk::Statistics::Subsample::ConstIterator::ConstIterator
ConstIterator(const Self *sample)
Definition: itkSubsample.h:158
itkSample.h
itk::Statistics::Subsample::ConstIterator::operator++
ConstIterator & operator++()
Definition: itkSubsample.h:189
itk::Statistics::Subsample::ConstIterator::operator==
bool operator==(const ConstIterator &it) const
Definition: itkSubsample.h:183
itk::Statistics::Subsample::m_TotalFrequency
TotalAbsoluteFrequencyType m_TotalFrequency
Definition: itkSubsample.h:318
itk::Statistics::Subsample::Begin
Iterator Begin()
Definition: itkSubsample.h:275
itk::Statistics::Subsample::m_ActiveDimension
unsigned int m_ActiveDimension
Definition: itkSubsample.h:317
itk::Statistics::Subsample::Iterator::Iterator
Iterator(Self *sample)
Definition: itkSubsample.h:239
itk::Statistics::Subsample::ConstIterator::m_Iter
InstanceIdentifierHolder::const_iterator m_Iter
Definition: itkSubsample.h:225
itk::Statistics::Subsample::MeasurementType
typename TSample::MeasurementType MeasurementType
Definition: itkSubsample.h:65
itk::Statistics::Subsample::ConstIterator
Definition: itkSubsample.h:153
itk::Statistics::Subsample::End
Iterator End()
Definition: itkSubsample.h:285
itk::Statistics::Subsample::ValueType
MeasurementVectorType ValueType
Definition: itkSubsample.h:67
itk::Statistics::Subsample::ConstIterator::operator=
ConstIterator & operator=(const ConstIterator &iter)
Definition: itkSubsample.h:168
itk::Statistics::Subsample::m_Sample
const TSample * m_Sample
Definition: itkSubsample.h:315
itk::Statistics::Subsample::TotalAbsoluteFrequencyType
typename TSample::TotalAbsoluteFrequencyType TotalAbsoluteFrequencyType
Definition: itkSubsample.h:70
itk::DataObject
Base class for all data objects in ITK.
Definition: itkDataObject.h:293
itk::Statistics::Subsample::ConstIterator::ConstIterator
ConstIterator(typename InstanceIdentifierHolder::const_iterator iter, const Self *classSample)
Definition: itkSubsample.h:218