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  ITK_UNEQUAL_OPERATOR_MEMBER_FUNCTION(ConstIterator);
183 
184  ConstIterator &
186  {
187  ++m_Iter;
188  return *this;
189  }
190 
192  GetFrequency() const
193  {
194  return m_Sample->GetFrequency(*m_Iter);
195  }
196 
197  const MeasurementVectorType &
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
211  ConstIterator();
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
224  const Self * m_Subsample;
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 purposly not implemented, since they should never be called.
253  Iterator();
254  Iterator(const Self * sample);
255  Iterator(typename InstanceIdentifierHolder::const_iterator iter, const Self * classSample);
256  Iterator(const ConstIterator & it);
257  ConstIterator &
258  operator=(const ConstIterator & it);
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
281  End()
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 
304 protected:
305  Subsample();
306  ~Subsample() override = default;
307  void
308  PrintSelf(std::ostream & os, Indent indent) const override;
309 
310 private:
311  const TSample * m_Sample;
313  unsigned int m_ActiveDimension;
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
itk::Statistics::Subsample::Begin
ConstIterator Begin() const
Definition: itkSubsample.h:289
itkObjectFactory.h
itk::Statistics::Subsample::Iterator::Iterator
Iterator(const Iterator &iter)
Definition: itkSubsample.h:239
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:225
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:297
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:312
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:230
itk::Statistics::Subsample::ConstIterator::GetFrequency
AbsoluteFrequencyType GetFrequency() const
Definition: itkSubsample.h:192
itk::Statistics::Subsample::Iterator::operator=
Iterator & operator=(const Iterator &iter)
Definition: itkSubsample.h:244
itk::Statistics::Subsample::Iterator::Iterator
Iterator(typename InstanceIdentifierHolder::iterator iter, Self *classSample)
Definition: itkSubsample.h:261
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:198
itk::Statistics::Subsample::ConstIterator::GetInstanceIdentifier
InstanceIdentifier GetInstanceIdentifier() const
Definition: itkSubsample.h:204
itk::Statistics::Subsample::ConstIterator::m_Subsample
const Self * m_Subsample
Definition: itkSubsample.h:224
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:185
itk::Statistics::Subsample::ConstIterator::operator==
bool operator==(const ConstIterator &it) const
Definition: itkSubsample.h:177
itk::Statistics::Subsample::m_TotalFrequency
TotalAbsoluteFrequencyType m_TotalFrequency
Definition: itkSubsample.h:314
itk::Statistics::Subsample::Begin
Iterator Begin()
Definition: itkSubsample.h:271
itk::Statistics::Subsample::m_ActiveDimension
unsigned int m_ActiveDimension
Definition: itkSubsample.h:313
itk::Statistics::Subsample::Iterator::Iterator
Iterator(Self *sample)
Definition: itkSubsample.h:235
itk::Statistics::Subsample::ConstIterator::m_Iter
InstanceIdentifierHolder::const_iterator m_Iter
Definition: itkSubsample.h:221
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:281
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:311
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:214