ITK  6.0.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 
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  itkOverrideGetNameOfClassMacro(Subsample);
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 
80  GetIdHolder() const
81  {
82  return this->m_IdHolder;
83  }
84 
86  void
87  SetSample(const TSample * sample);
88 
89  const TSample *
90  GetSample() const;
91 
93  void
94  InitializeWithAllInstances();
95 
97  void
98  AddInstance(InstanceIdentifier id);
99 
102  InstanceIdentifier
103  Size() const override;
104 
106  void
107  Clear();
108 
111  const MeasurementVectorType &
112  GetMeasurementVector(InstanceIdentifier id) const override;
113 
115  AbsoluteFrequencyType
116  GetFrequency(InstanceIdentifier id) const override;
117 
119  TotalAbsoluteFrequencyType
120  GetTotalFrequency() const override;
121 
122  void
123  Swap(unsigned int index1, unsigned int index2);
124 
125  InstanceIdentifier
126  GetInstanceIdentifier(unsigned int index);
127 
128  const MeasurementVectorType &
129  GetMeasurementVectorByIndex(unsigned int index) const;
130 
131  AbsoluteFrequencyType
132  GetFrequencyByIndex(unsigned int index) const;
133 
135  void
136  Graft(const DataObject * thatObject) override;
137 
139  {
140  friend class Subsample;
141 
142  public:
143  ConstIterator(const Self * sample) { *this = sample->Begin(); }
144 
146  {
147  m_Iter = iter.m_Iter;
148  m_Subsample = iter.m_Subsample;
149  m_Sample = iter.m_Sample;
150  }
151 
152  ConstIterator &
153  operator=(const ConstIterator & iter)
154  {
155  m_Iter = iter.m_Iter;
156  m_Subsample = iter.m_Subsample;
157  m_Sample = iter.m_Sample;
158  return *this;
159  }
160 
161  bool
162  operator==(const ConstIterator & it) const
163  {
164  return (m_Iter == it.m_Iter);
165  }
166 
167  ITK_UNEQUAL_OPERATOR_MEMBER_FUNCTION(ConstIterator);
168 
169  ConstIterator &
171  {
172  ++m_Iter;
173  return *this;
174  }
175 
177  GetFrequency() const
178  {
179  return m_Sample->GetFrequency(*m_Iter);
180  }
181 
182  const MeasurementVectorType &
184  {
185  return m_Sample->GetMeasurementVector(*m_Iter);
186  }
187 
190  {
191  return (m_Iter - m_Subsample->GetIdHolder().begin());
192  }
193 
194  protected:
195  // Purposely not implemented
196  ConstIterator();
197 
198  // Only to be called from the Subsample
199  ConstIterator(typename InstanceIdentifierHolder::const_iterator iter, const Self * classSample)
200  : m_Iter(iter)
201  , m_Subsample(classSample)
202  , 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  Iterator(Self * sample)
221  : ConstIterator(sample)
222  {}
223 
224  Iterator(const Iterator & iter)
225  : ConstIterator(iter)
226  {}
227 
228  Iterator &
229  operator=(const Iterator & iter)
230  {
231  this->ConstIterator::operator=(iter);
232  return *this;
233  }
234 
235  protected:
236  // To ensure const-correctness these method must not be in the public API.
237  // The are purposely not implemented, since they should never be called.
238  Iterator();
239  Iterator(const Self * sample);
240  Iterator(typename InstanceIdentifierHolder::const_iterator iter, const Self * classSample);
241  Iterator(const ConstIterator & it);
242  ConstIterator &
243  operator=(const ConstIterator & it);
244 
245  // Only to be called from the Subsample
246  Iterator(typename InstanceIdentifierHolder::iterator iter, Self * classSample)
247  : ConstIterator(iter, classSample)
248  {}
249 
250  private:
251  };
252 
255  Iterator
257  {
258  Iterator iter(m_IdHolder.begin(), this);
259 
260  return iter;
261  }
262 
265  Iterator
266  End()
267  {
268  Iterator iter(m_IdHolder.end(), this);
269 
270  return iter;
271  }
272 
273  ConstIterator
274  Begin() const
275  {
276  ConstIterator iter(m_IdHolder.begin(), this);
277 
278  return iter;
279  }
280 
281  ConstIterator
282  End() const
283  {
284  ConstIterator iter(m_IdHolder.end(), this);
285 
286  return iter;
287  }
288 
289 protected:
290  Subsample();
291  ~Subsample() override = default;
292  void
293  PrintSelf(std::ostream & os, Indent indent) const override;
294 
295 private:
296  const TSample * m_Sample{};
298  unsigned int m_ActiveDimension{};
299  TotalAbsoluteFrequencyType m_TotalFrequency{};
300 }; // end of class
301 } // end of namespace Statistics
302 } // end of namespace itk
303 
304 #ifndef ITK_MANUAL_INSTANTIATION
305 # include "itkSubsample.hxx"
306 #endif
307 
308 #endif
Pointer
SmartPointer< Self > Pointer
Definition: itkAddImageFilter.h:93
itk::Statistics::Subsample::Begin
ConstIterator Begin() const
Definition: itkSubsample.h:274
itkObjectFactory.h
itk::Statistics::Subsample::Iterator::Iterator
Iterator(const Iterator &iter)
Definition: itkSubsample.h:224
itk::Statistics::Subsample::GetIdHolder
const InstanceIdentifierHolder & GetIdHolder() const
Definition: itkSubsample.h:80
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:210
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:282
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::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:215
itk::Statistics::Subsample::ConstIterator::GetFrequency
AbsoluteFrequencyType GetFrequency() const
Definition: itkSubsample.h:177
itk::Statistics::Subsample::Iterator::operator=
Iterator & operator=(const Iterator &iter)
Definition: itkSubsample.h:229
itk::Statistics::Subsample::Iterator::Iterator
Iterator(typename InstanceIdentifierHolder::iterator iter, Self *classSample)
Definition: itkSubsample.h:246
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:183
itk::Statistics::Subsample::ConstIterator::GetInstanceIdentifier
InstanceIdentifier GetInstanceIdentifier() const
Definition: itkSubsample.h:189
itk::Statistics::Subsample::ConstIterator::m_Subsample
const Self * m_Subsample
Definition: itkSubsample.h:209
itk::Statistics::Subsample::ConstIterator::ConstIterator
ConstIterator(const ConstIterator &iter)
Definition: itkSubsample.h:145
itk
The "itk" namespace contains all Insight Segmentation and Registration Toolkit (ITK) classes....
Definition: itkAnatomicalOrientation.h:29
itk::Statistics::Subsample::ConstIterator::ConstIterator
ConstIterator(const Self *sample)
Definition: itkSubsample.h:143
itkSample.h
itk::Statistics::Subsample::ConstIterator::operator++
ConstIterator & operator++()
Definition: itkSubsample.h:170
itk::Statistics::Subsample::ConstIterator::operator==
bool operator==(const ConstIterator &it) const
Definition: itkSubsample.h:162
itk::Statistics::Subsample::Begin
Iterator Begin()
Definition: itkSubsample.h:256
itk::Statistics::Subsample::Iterator::Iterator
Iterator(Self *sample)
Definition: itkSubsample.h:220
itk::Statistics::Subsample::ConstIterator::m_Iter
InstanceIdentifierHolder::const_iterator m_Iter
Definition: itkSubsample.h:206
itk::Statistics::Subsample::MeasurementType
typename TSample::MeasurementType MeasurementType
Definition: itkSubsample.h:65
itk::Statistics::Subsample::ConstIterator
Definition: itkSubsample.h:138
itk::Statistics::Subsample::End
Iterator End()
Definition: itkSubsample.h:266
itk::Statistics::Subsample::ValueType
MeasurementVectorType ValueType
Definition: itkSubsample.h:67
itk::Statistics::Subsample::ConstIterator::operator=
ConstIterator & operator=(const ConstIterator &iter)
Definition: itkSubsample.h:153
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:199