ITK  4.13.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:
46  typedef Subsample Self;
47  typedef TSample Superclass;
50 
52  itkTypeMacro(Subsample, TSample);
53 
55  itkNewMacro(Self);
56 
58  typedef typename TSample::Pointer SamplePointer;
59 
62  typedef typename TSample::MeasurementVectorType MeasurementVectorType;
63  typedef typename TSample::MeasurementType MeasurementType;
64  typedef typename TSample::InstanceIdentifier InstanceIdentifier;
66 
67  typedef typename TSample::AbsoluteFrequencyType AbsoluteFrequencyType;
68  typedef typename TSample::TotalAbsoluteFrequencyType TotalAbsoluteFrequencyType;
69 
74  typedef std::vector< InstanceIdentifier > InstanceIdentifierHolder;
75 
76 // Disable clang warning false positive.
77 // <https://llvm.org/bugs/show_bug.cgi?id=22582>
78 #if defined(__clang__) && defined(__has_warning)
79 # if __has_warning("-Winconsistent-missing-override")
80 # pragma clang diagnostic push
81 # pragma clang diagnostic ignored "-Winconsistent-missing-override"
82 # endif
83 #endif
84 
86  virtual const InstanceIdentifierHolder & GetIdHolder() const
87  {
88  return this->m_IdHolder;
89  }
90 
91 #if defined(__clang__) && defined(__has_warning)
92 # if __has_warning("-Winconsistent-missing-override")
93 # pragma clang diagnostic pop
94 # endif
95 #endif
96 
98  void SetSample(const TSample *sample);
99 
100  const TSample * GetSample() const;
101 
103  void InitializeWithAllInstances();
104 
106  void AddInstance(InstanceIdentifier id);
107 
110  InstanceIdentifier Size() const ITK_OVERRIDE;
111 
113  void Clear();
114 
117  const MeasurementVectorType & GetMeasurementVector(InstanceIdentifier id) const ITK_OVERRIDE;
118 
120  AbsoluteFrequencyType GetFrequency(InstanceIdentifier id) const ITK_OVERRIDE;
121 
123  TotalAbsoluteFrequencyType GetTotalFrequency() const ITK_OVERRIDE;
124 
125  void Swap(unsigned int index1, unsigned int index2);
126 
127  InstanceIdentifier GetInstanceIdentifier(unsigned int index);
128 
129  const MeasurementVectorType & GetMeasurementVectorByIndex(unsigned int index) const;
130 
131  AbsoluteFrequencyType GetFrequencyByIndex(unsigned int index) const;
132 
134  virtual void Graft(const DataObject *thatObject) ITK_OVERRIDE;
135 
137  {
138  friend class Subsample;
139 
140 public:
141 
142  ConstIterator(const Self *sample)
143  {
144  *this = sample->Begin();
145  }
146 
147  ConstIterator(const ConstIterator & iter)
148  {
149  m_Iter = iter.m_Iter;
150  m_Subsample = iter.m_Subsample;
151  m_Sample = iter.m_Sample;
152  }
153 
154  ConstIterator & operator=(const ConstIterator & iter)
155  {
156  m_Iter = iter.m_Iter;
157  m_Subsample = iter.m_Subsample;
158  m_Sample = iter.m_Sample;
159  return *this;
160  }
161 
162  bool operator!=(const ConstIterator & it)
163  {
164  return ( m_Iter != it.m_Iter );
165  }
166 
167  bool operator==(const ConstIterator & it)
168  {
169  return ( m_Iter == it.m_Iter );
170  }
171 
172  ConstIterator & operator++()
173  {
174  ++m_Iter;
175  return *this;
176  }
177 
179  {
180  return m_Sample->GetFrequency(*m_Iter);
181  }
182 
184  {
185  return m_Sample->GetMeasurementVector(*m_Iter);
186  }
187 
189  {
190  return ( m_Iter - m_Subsample->GetIdHolder().begin() );
191  }
192 
193 protected:
194  // Purposely not implemented
195  ConstIterator();
196 
197  // Only to be called from the Subsample
198  ConstIterator(typename InstanceIdentifierHolder::const_iterator iter,
199  const Self *classSample):
200  m_Iter(iter), m_Subsample(classSample), m_Sample( classSample->GetSample() )
201  {}
202 
203  // ConstIterator pointing to ImageToListSampleAdaptor
204  typename InstanceIdentifierHolder::const_iterator m_Iter;
205 
206  // Pointer to Subsample object
207  const Self * m_Subsample;
208  const TSample *m_Sample;
209 
210 private:
211  };
212 
213  class Iterator:public ConstIterator
214  {
215  friend class Subsample;
216 
217 public:
218 
219  Iterator(Self *sample):ConstIterator(sample)
220  {}
221 
222  Iterator(const Iterator & iter):ConstIterator(iter)
223  {}
224 
225  Iterator & operator=(const Iterator & iter)
226  {
227  this->ConstIterator::operator=(iter);
228  return *this;
229  }
230 
231 protected:
232  // To ensure const-correctness these method must not be in the public API.
233  // The are purposly not implemented, since they should never be called.
234  Iterator();
235  Iterator(const Self *sample);
236  Iterator(typename InstanceIdentifierHolder::const_iterator iter,
237  const Self *classSample);
238  Iterator(const ConstIterator & it);
239  ConstIterator & operator=(const ConstIterator & it);
240 
241  // Only to be called from the Subsample
242  Iterator(typename InstanceIdentifierHolder::iterator iter,
243  Self *classSample):
244  ConstIterator(iter, classSample)
245  {}
246 
247 private:
248  };
249 
253  {
254  Iterator iter(m_IdHolder.begin(), this);
255 
256  return iter;
257  }
258 
262  {
263  Iterator iter(m_IdHolder.end(), this);
264 
265  return iter;
266  }
267 
269  {
270  ConstIterator iter(m_IdHolder.begin(), this);
271 
272  return iter;
273  }
274 
276  {
277  ConstIterator iter(m_IdHolder.end(), this);
278 
279  return iter;
280  }
281 
282 protected:
283  Subsample();
284  virtual ~Subsample() ITK_OVERRIDE {}
285  void PrintSelf(std::ostream & os, Indent indent) const ITK_OVERRIDE;
286 
287 private:
288  ITK_DISALLOW_COPY_AND_ASSIGN(Subsample);
289 
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
virtual ~Subsample() override
Definition: itkSubsample.h:284
ConstIterator(typename InstanceIdentifierHolder::const_iterator iter, const Self *classSample)
Definition: itkSubsample.h:198
const MeasurementVectorType & GetMeasurementVector() const
Definition: itkSubsample.h:183
Represent the size (bounds) of a n-dimensional image.
Definition: itkSize.h:52
TSample::MeasurementType MeasurementType
Definition: itkSubsample.h:63
bool operator==(const ConstIterator &it)
Definition: itkSubsample.h:167
bool operator!=(const ConstIterator &it)
Definition: itkSubsample.h:162
SmartPointer< const Self > ConstPointer
Definition: itkSubsample.h:49
This class stores a subset of instance identifiers from another sample object. You can create a subsa...
Definition: itkSubsample.h:41
TSample::InstanceIdentifier InstanceIdentifier
Definition: itkSubsample.h:64
TSample::TotalAbsoluteFrequencyType TotalAbsoluteFrequencyType
Definition: itkSubsample.h:68
unsigned int m_ActiveDimension
Definition: itkSubsample.h:292
Iterator & operator=(const Iterator &iter)
Definition: itkSubsample.h:225
InstanceIdentifierHolder m_IdHolder
Definition: itkSubsample.h:291
const TSample * m_Sample
Definition: itkSubsample.h:288
InstanceIdentifier GetInstanceIdentifier() const
Definition: itkSubsample.h:188
TSample::Pointer SamplePointer
Definition: itkSubsample.h:55
InstanceIdentifierHolder::const_iterator m_Iter
Definition: itkSubsample.h:204
TSample::AbsoluteFrequencyType AbsoluteFrequencyType
Definition: itkSubsample.h:67
SmartPointer< Self > Pointer
Definition: itkSubsample.h:48
MeasurementVectorType ValueType
Definition: itkSubsample.h:65
ConstIterator(const ConstIterator &iter)
Definition: itkSubsample.h:147
TotalAbsoluteFrequencyType m_TotalFrequency
Definition: itkSubsample.h:293
TSample::MeasurementVectorType MeasurementVectorType
Definition: itkSubsample.h:62
AbsoluteFrequencyType GetFrequency() const
Definition: itkSubsample.h:178
ConstIterator & operator=(const ConstIterator &iter)
Definition: itkSubsample.h:154
ConstIterator End() const
Definition: itkSubsample.h:275
virtual const InstanceIdentifierHolder & GetIdHolder() const
Definition: itkSubsample.h:86
Control indentation during Print() invocation.
Definition: itkIndent.h:49
Iterator(typename InstanceIdentifierHolder::iterator iter, Self *classSample)
Definition: itkSubsample.h:242
ConstIterator Begin() const
Definition: itkSubsample.h:268
Base class for all data objects in ITK.
std::vector< InstanceIdentifier > InstanceIdentifierHolder
Definition: itkSubsample.h:74