ITK  5.0.0
Insight Segmentation and Registration 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:
42  public TSample
43 {
44 public:
45  ITK_DISALLOW_COPY_AND_ASSIGN(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  {
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 SetSample(const TSample *sample);
101 
102  const TSample * GetSample() const;
103 
105  void InitializeWithAllInstances();
106 
108  void AddInstance(InstanceIdentifier id);
109 
112  InstanceIdentifier Size() const override;
113 
115  void Clear();
116 
119  const MeasurementVectorType & GetMeasurementVector(InstanceIdentifier id) const override;
120 
122  AbsoluteFrequencyType GetFrequency(InstanceIdentifier id) const override;
123 
125  TotalAbsoluteFrequencyType GetTotalFrequency() const override;
126 
127  void Swap(unsigned int index1, unsigned int index2);
128 
129  InstanceIdentifier GetInstanceIdentifier(unsigned int index);
130 
131  const MeasurementVectorType & GetMeasurementVectorByIndex(unsigned int index) const;
132 
133  AbsoluteFrequencyType GetFrequencyByIndex(unsigned int index) const;
134 
136  void Graft(const DataObject *thatObject) override;
137 
139  {
140  friend class Subsample;
141 
142 public:
143 
144  ConstIterator(const Self *sample)
145  {
146  *this = sample->Begin();
147  }
148 
150  {
151  m_Iter = iter.m_Iter;
152  m_Subsample = iter.m_Subsample;
153  m_Sample = iter.m_Sample;
154  }
155 
157  {
158  m_Iter = iter.m_Iter;
159  m_Subsample = iter.m_Subsample;
160  m_Sample = iter.m_Sample;
161  return *this;
162  }
163 
164  bool operator!=(const ConstIterator & it)
165  {
166  return ( m_Iter != it.m_Iter );
167  }
168 
169  bool operator==(const ConstIterator & it)
170  {
171  return ( m_Iter == it.m_Iter );
172  }
173 
175  {
176  ++m_Iter;
177  return *this;
178  }
179 
181  {
182  return m_Sample->GetFrequency(*m_Iter);
183  }
184 
186  {
187  return m_Sample->GetMeasurementVector(*m_Iter);
188  }
189 
191  {
192  return ( m_Iter - m_Subsample->GetIdHolder().begin() );
193  }
194 
195 protected:
196  // Purposely not implemented
197  ConstIterator();
198 
199  // Only to be called from the Subsample
200  ConstIterator(typename InstanceIdentifierHolder::const_iterator iter,
201  const Self *classSample):
202  m_Iter(iter), m_Subsample(classSample), m_Sample( classSample->GetSample() )
203  {}
204 
205  // ConstIterator pointing to ImageToListSampleAdaptor
206  typename InstanceIdentifierHolder::const_iterator m_Iter;
207 
208  // Pointer to Subsample object
209  const Self * m_Subsample;
210  const TSample *m_Sample;
211 
212 private:
213  };
214 
215  class Iterator:public ConstIterator
216  {
217  friend class Subsample;
218 
219 public:
220 
221  Iterator(Self *sample):ConstIterator(sample)
222  {}
223 
224  Iterator(const Iterator & iter):ConstIterator(iter)
225  {}
226 
227  Iterator & operator=(const Iterator & iter)
228  {
229  this->ConstIterator::operator=(iter);
230  return *this;
231  }
232 
233 protected:
234  // To ensure const-correctness these method must not be in the public API.
235  // The are purposly not implemented, since they should never be called.
236  Iterator();
237  Iterator(const Self *sample);
238  Iterator(typename InstanceIdentifierHolder::const_iterator iter,
239  const Self *classSample);
240  Iterator(const ConstIterator & it);
241  ConstIterator & operator=(const ConstIterator & it);
242 
243  // Only to be called from the Subsample
244  Iterator(typename InstanceIdentifierHolder::iterator iter,
245  Self *classSample):
246  ConstIterator(iter, classSample)
247  {}
248 
249 private:
250  };
251 
255  {
256  Iterator iter(m_IdHolder.begin(), this);
257 
258  return iter;
259  }
260 
264  {
265  Iterator iter(m_IdHolder.end(), this);
266 
267  return iter;
268  }
269 
271  {
272  ConstIterator iter(m_IdHolder.begin(), this);
273 
274  return iter;
275  }
276 
278  {
279  ConstIterator iter(m_IdHolder.end(), this);
280 
281  return iter;
282  }
283 
284 protected:
285  Subsample();
286  ~Subsample() override = default;
287  void PrintSelf(std::ostream & os, Indent indent) const override;
288 
289 private:
290  const TSample * m_Sample;
292  unsigned int m_ActiveDimension;
294 }; // end of class
295 } // end of namespace Statistics
296 } // end of namespace itk
297 
298 #ifndef ITK_MANUAL_INSTANTIATION
299 #include "itkSubsample.hxx"
300 #endif
301 
302 #endif
ConstIterator(typename InstanceIdentifierHolder::const_iterator iter, const Self *classSample)
Definition: itkSubsample.h:200
const MeasurementVectorType & GetMeasurementVector() const
Definition: itkSubsample.h:185
std::vector< InstanceIdentifier > InstanceIdentifierHolder
Definition: itkSubsample.h:76
bool operator==(const ConstIterator &it)
Definition: itkSubsample.h:169
bool operator!=(const ConstIterator &it)
Definition: itkSubsample.h:164
typename TSample::InstanceIdentifier InstanceIdentifier
Definition: itkSubsample.h:66
This class stores a subset of instance identifiers from another sample object. You can create a subsa...
Definition: itkSubsample.h:41
const InstanceIdentifierHolder & GetIdHolder() const
Definition: itkSubsample.h:88
typename TSample::Pointer SamplePointer
Definition: itkSubsample.h:60
unsigned int m_ActiveDimension
Definition: itkSubsample.h:292
Iterator & operator=(const Iterator &iter)
Definition: itkSubsample.h:227
InstanceIdentifierHolder m_IdHolder
Definition: itkSubsample.h:291
const TSample * m_Sample
Definition: itkSubsample.h:290
InstanceIdentifier GetInstanceIdentifier() const
Definition: itkSubsample.h:190
InstanceIdentifierHolder::const_iterator m_Iter
Definition: itkSubsample.h:206
Represent a n-dimensional size (bounds) of a n-dimensional image.
Definition: itkSize.h:68
typename TSample::TotalAbsoluteFrequencyType TotalAbsoluteFrequencyType
Definition: itkSubsample.h:70
typename TSample::MeasurementType MeasurementType
Definition: itkSubsample.h:65
ConstIterator(const ConstIterator &iter)
Definition: itkSubsample.h:149
TotalAbsoluteFrequencyType m_TotalFrequency
Definition: itkSubsample.h:293
AbsoluteFrequencyType GetFrequency() const
Definition: itkSubsample.h:180
MeasurementVectorType ValueType
Definition: itkSubsample.h:67
typename TSample::MeasurementVectorType MeasurementVectorType
Definition: itkSubsample.h:64
ConstIterator & operator=(const ConstIterator &iter)
Definition: itkSubsample.h:156
ConstIterator End() const
Definition: itkSubsample.h:277
Control indentation during Print() invocation.
Definition: itkIndent.h:49
Iterator(typename InstanceIdentifierHolder::iterator iter, Self *classSample)
Definition: itkSubsample.h:244
typename TSample::AbsoluteFrequencyType AbsoluteFrequencyType
Definition: itkSubsample.h:69
ConstIterator Begin() const
Definition: itkSubsample.h:270
Base class for all data objects in ITK.