ITK  5.1.0
Insight Toolkit
itkSubsample.h
Go to the documentation of this file.
1 /*=========================================================================
2  *
3  * Copyright Insight Software Consortium
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 {
40 template <typename TSample>
41 class ITK_TEMPLATE_EXPORT Subsample : public TSample
42 {
43 public:
44  ITK_DISALLOW_COPY_AND_ASSIGN(Subsample);
45 
47  using Self = Subsample;
48  using Superclass = TSample;
51 
53  itkTypeMacro(Subsample, TSample);
54 
56  itkNewMacro(Self);
57 
59  using SamplePointer = typename TSample::Pointer;
60 
63  using MeasurementVectorType = typename TSample::MeasurementVectorType;
64  using MeasurementType = typename TSample::MeasurementType;
65  using InstanceIdentifier = typename TSample::InstanceIdentifier;
67 
68  using AbsoluteFrequencyType = typename TSample::AbsoluteFrequencyType;
69  using TotalAbsoluteFrequencyType = typename TSample::TotalAbsoluteFrequencyType;
70 
75  using InstanceIdentifierHolder = std::vector<InstanceIdentifier>;
76 
77 // Disable clang warning false positive.
78 // <https://llvm.org/bugs/show_bug.cgi?id=22582>
79 #if defined(__clang__) && defined(__has_warning)
80 # if __has_warning("-Winconsistent-missing-override")
81 # pragma clang diagnostic push
82 # pragma clang diagnostic ignored "-Winconsistent-missing-override"
83 # endif
84 #endif
85 
88  GetIdHolder() const
89  {
90  return this->m_IdHolder;
91  }
92 
93 #if defined(__clang__) && defined(__has_warning)
94 # if __has_warning("-Winconsistent-missing-override")
95 # pragma clang diagnostic pop
96 # endif
97 #endif
98 
100  void
101  SetSample(const TSample * sample);
102 
103  const TSample *
104  GetSample() const;
105 
107  void
108  InitializeWithAllInstances();
109 
111  void
112  AddInstance(InstanceIdentifier id);
113 
116  InstanceIdentifier
117  Size() const override;
118 
120  void
121  Clear();
122 
125  const MeasurementVectorType &
126  GetMeasurementVector(InstanceIdentifier id) const override;
127 
129  AbsoluteFrequencyType
130  GetFrequency(InstanceIdentifier id) const override;
131 
133  TotalAbsoluteFrequencyType
134  GetTotalFrequency() const override;
135 
136  void
137  Swap(unsigned int index1, unsigned int index2);
138 
139  InstanceIdentifier
140  GetInstanceIdentifier(unsigned int index);
141 
142  const MeasurementVectorType &
143  GetMeasurementVectorByIndex(unsigned int index) const;
144 
145  AbsoluteFrequencyType
146  GetFrequencyByIndex(unsigned int index) const;
147 
149  void
150  Graft(const DataObject * thatObject) override;
151 
153  {
154  friend class Subsample;
155 
156  public:
157  ConstIterator(const Self * sample) { *this = sample->Begin(); }
158 
160  {
161  m_Iter = iter.m_Iter;
162  m_Subsample = iter.m_Subsample;
163  m_Sample = iter.m_Sample;
164  }
165 
166  ConstIterator &
167  operator=(const ConstIterator & iter)
168  {
169  m_Iter = iter.m_Iter;
170  m_Subsample = iter.m_Subsample;
171  m_Sample = iter.m_Sample;
172  return *this;
173  }
174 
175  bool
177  {
178  return (m_Iter != it.m_Iter);
179  }
180 
181  bool
183  {
184  return (m_Iter == it.m_Iter);
185  }
186 
187  ConstIterator &
189  {
190  ++m_Iter;
191  return *this;
192  }
193 
195  GetFrequency() const
196  {
197  return m_Sample->GetFrequency(*m_Iter);
198  }
199 
200  const MeasurementVectorType &
202  {
203  return m_Sample->GetMeasurementVector(*m_Iter);
204  }
205 
208  {
209  return (m_Iter - m_Subsample->GetIdHolder().begin());
210  }
211 
212  protected:
213  // Purposely not implemented
214  ConstIterator();
215 
216  // Only to be called from the Subsample
217  ConstIterator(typename InstanceIdentifierHolder::const_iterator iter, const Self * classSample)
218  : m_Iter(iter)
219  , m_Subsample(classSample)
220  , m_Sample(classSample->GetSample())
221  {}
222 
223  // ConstIterator pointing to ImageToListSampleAdaptor
224  typename InstanceIdentifierHolder::const_iterator m_Iter;
225 
226  // Pointer to Subsample object
227  const Self * m_Subsample;
228  const TSample * m_Sample;
229 
230  private:
231  };
232 
233  class Iterator : public ConstIterator
234  {
235  friend class Subsample;
236 
237  public:
238  Iterator(Self * sample)
239  : ConstIterator(sample)
240  {}
241 
242  Iterator(const Iterator & iter)
243  : ConstIterator(iter)
244  {}
245 
246  Iterator &
247  operator=(const Iterator & iter)
248  {
249  this->ConstIterator::operator=(iter);
250  return *this;
251  }
252 
253  protected:
254  // To ensure const-correctness these method must not be in the public API.
255  // The are purposly not implemented, since they should never be called.
256  Iterator();
257  Iterator(const Self * sample);
258  Iterator(typename InstanceIdentifierHolder::const_iterator iter, const Self * classSample);
259  Iterator(const ConstIterator & it);
260  ConstIterator &
261  operator=(const ConstIterator & it);
262 
263  // Only to be called from the Subsample
264  Iterator(typename InstanceIdentifierHolder::iterator iter, Self * classSample)
265  : ConstIterator(iter, classSample)
266  {}
267 
268  private:
269  };
270 
273  Iterator
275  {
276  Iterator iter(m_IdHolder.begin(), this);
277 
278  return iter;
279  }
280 
283  Iterator
284  End()
285  {
286  Iterator iter(m_IdHolder.end(), this);
287 
288  return iter;
289  }
290 
291  ConstIterator
292  Begin() const
293  {
294  ConstIterator iter(m_IdHolder.begin(), this);
295 
296  return iter;
297  }
298 
299  ConstIterator
300  End() const
301  {
302  ConstIterator iter(m_IdHolder.end(), this);
303 
304  return iter;
305  }
306 
307 protected:
308  Subsample();
309  ~Subsample() override = default;
310  void
311  PrintSelf(std::ostream & os, Indent indent) const override;
312 
313 private:
314  const TSample * m_Sample;
316  unsigned int m_ActiveDimension;
318 }; // end of class
319 } // end of namespace Statistics
320 } // end of namespace itk
321 
322 #ifndef ITK_MANUAL_INSTANTIATION
323 # include "itkSubsample.hxx"
324 #endif
325 
326 #endif
itk::Statistics::Subsample::Begin
ConstIterator Begin() const
Definition: itkSubsample.h:292
itkObjectFactory.h
itk::Statistics::Subsample::Iterator::Iterator
Iterator(const Iterator &iter)
Definition: itkSubsample.h:242
itk::Statistics::Subsample::GetIdHolder
const InstanceIdentifierHolder & GetIdHolder() const
Definition: itkSubsample.h:88
itk::Statistics::Subsample::InstanceIdentifier
typename TSample::InstanceIdentifier InstanceIdentifier
Definition: itkSubsample.h:65
itk::Statistics::Subsample::ConstIterator::m_Sample
const TSample * m_Sample
Definition: itkSubsample.h:228
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:48
itk::Statistics::Subsample::MeasurementVectorType
typename TSample::MeasurementVectorType MeasurementVectorType
Definition: itkSubsample.h:63
itk::Statistics::Subsample::End
ConstIterator End() const
Definition: itkSubsample.h:300
itk::Statistics::Subsample::AbsoluteFrequencyType
typename TSample::AbsoluteFrequencyType AbsoluteFrequencyType
Definition: itkSubsample.h:68
itk::Statistics::Subsample::SamplePointer
typename TSample::Pointer SamplePointer
Definition: itkSubsample.h:59
itk::Statistics::Subsample::m_IdHolder
InstanceIdentifierHolder m_IdHolder
Definition: itkSubsample.h:315
itk::SmartPointer< Self >
itk::Indent
Control indentation during Print() invocation.
Definition: itkIndent.h:49
itk::Statistics::Subsample::ConstIterator::operator!=
bool operator!=(const ConstIterator &it)
Definition: itkSubsample.h:176
itk::Statistics::Subsample::InstanceIdentifierHolder
std::vector< InstanceIdentifier > InstanceIdentifierHolder
Definition: itkSubsample.h:75
itk::Statistics::Subsample::Iterator
Definition: itkSubsample.h:233
itk::Statistics::Subsample::ConstIterator::GetFrequency
AbsoluteFrequencyType GetFrequency() const
Definition: itkSubsample.h:195
itk::Statistics::Subsample::Iterator::operator=
Iterator & operator=(const Iterator &iter)
Definition: itkSubsample.h:247
itk::Statistics::Subsample::Iterator::Iterator
Iterator(typename InstanceIdentifierHolder::iterator iter, Self *classSample)
Definition: itkSubsample.h:264
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:41
itk::Statistics::Subsample::ConstIterator::GetMeasurementVector
const MeasurementVectorType & GetMeasurementVector() const
Definition: itkSubsample.h:201
itk::Statistics::Subsample::ConstIterator::GetInstanceIdentifier
InstanceIdentifier GetInstanceIdentifier() const
Definition: itkSubsample.h:207
itk::Statistics::Subsample::ConstIterator::m_Subsample
const Self * m_Subsample
Definition: itkSubsample.h:227
itk::Statistics::Subsample::ConstIterator::ConstIterator
ConstIterator(const ConstIterator &iter)
Definition: itkSubsample.h:159
itk
The "itk" namespace contains all Insight Segmentation and Registration Toolkit (ITK) classes....
Definition: itkArray.h:26
itk::Statistics::Subsample::ConstIterator::ConstIterator
ConstIterator(const Self *sample)
Definition: itkSubsample.h:157
itkSample.h
itk::Statistics::Subsample::ConstIterator::operator++
ConstIterator & operator++()
Definition: itkSubsample.h:188
itk::Statistics::Subsample::m_TotalFrequency
TotalAbsoluteFrequencyType m_TotalFrequency
Definition: itkSubsample.h:317
itk::Statistics::Subsample::Begin
Iterator Begin()
Definition: itkSubsample.h:274
itk::Statistics::Subsample::m_ActiveDimension
unsigned int m_ActiveDimension
Definition: itkSubsample.h:316
itk::Statistics::Subsample::Iterator::Iterator
Iterator(Self *sample)
Definition: itkSubsample.h:238
itk::Statistics::Subsample::ConstIterator::operator==
bool operator==(const ConstIterator &it)
Definition: itkSubsample.h:182
itk::Statistics::Subsample::ConstIterator::m_Iter
InstanceIdentifierHolder::const_iterator m_Iter
Definition: itkSubsample.h:224
itk::Statistics::Subsample::MeasurementType
typename TSample::MeasurementType MeasurementType
Definition: itkSubsample.h:64
itk::Statistics::Subsample::ConstIterator
Definition: itkSubsample.h:152
itk::Statistics::Subsample::End
Iterator End()
Definition: itkSubsample.h:284
itk::Statistics::Subsample::ValueType
MeasurementVectorType ValueType
Definition: itkSubsample.h:66
itk::Statistics::Subsample::ConstIterator::operator=
ConstIterator & operator=(const ConstIterator &iter)
Definition: itkSubsample.h:167
itk::Statistics::Subsample::m_Sample
const TSample * m_Sample
Definition: itkSubsample.h:314
itk::Statistics::Subsample::TotalAbsoluteFrequencyType
typename TSample::TotalAbsoluteFrequencyType TotalAbsoluteFrequencyType
Definition: itkSubsample.h:69
itk::DataObject
Base class for all data objects in ITK.
Definition: itkDataObject.h:288
itk::Statistics::Subsample::ConstIterator::ConstIterator
ConstIterator(typename InstanceIdentifierHolder::const_iterator iter, const Self *classSample)
Definition: itkSubsample.h:217