ITK  4.9.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 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 
77  virtual const InstanceIdentifierHolder & GetIdHolder() const
78  {
79  return this->m_IdHolder;
80  }
81 
83  void SetSample(const TSample *sample);
84 
85  const TSample * GetSample() const;
86 
89 
92 
95  InstanceIdentifier Size() const ITK_OVERRIDE;
96 
98  void Clear();
99 
102  const MeasurementVectorType & GetMeasurementVector(InstanceIdentifier id) const ITK_OVERRIDE;
103 
106 
108  TotalAbsoluteFrequencyType GetTotalFrequency() const ITK_OVERRIDE;
109 
110  void Swap(unsigned int index1, unsigned int index2);
111 
112  InstanceIdentifier GetInstanceIdentifier(unsigned int index);
113 
114  const MeasurementVectorType & GetMeasurementVectorByIndex(unsigned int index) const;
115 
116  AbsoluteFrequencyType GetFrequencyByIndex(unsigned int index) const;
117 
119  virtual void Graft(const DataObject *thatObject) ITK_OVERRIDE;
120 
122  {
123  friend class Subsample;
124 
125 public:
126 
127  ConstIterator(const Self *sample)
128  {
129  *this = sample->Begin();
130  }
131 
132  ConstIterator(const ConstIterator & iter)
133  {
134  m_Iter = iter.m_Iter;
135  m_Subsample = iter.m_Subsample;
136  m_Sample = iter.m_Sample;
137  }
138 
139  ConstIterator & operator=(const ConstIterator & iter)
140  {
141  m_Iter = iter.m_Iter;
142  m_Subsample = iter.m_Subsample;
143  m_Sample = iter.m_Sample;
144  return *this;
145  }
146 
147  bool operator!=(const ConstIterator & it)
148  {
149  return ( m_Iter != it.m_Iter );
150  }
151 
152  bool operator==(const ConstIterator & it)
153  {
154  return ( m_Iter == it.m_Iter );
155  }
156 
157  ConstIterator & operator++()
158  {
159  ++m_Iter;
160  return *this;
161  }
162 
164  {
165  return m_Sample->GetFrequency(*m_Iter);
166  }
167 
169  {
170  return m_Sample->GetMeasurementVector(*m_Iter);
171  }
172 
174  {
175  return ( m_Iter - m_Subsample->GetIdHolder().begin() );
176  }
177 
178 protected:
179  // Purposely not implemented
180  ConstIterator();
181 
182  // Only to be called from the Subsample
183  ConstIterator(typename InstanceIdentifierHolder::const_iterator iter,
184  const Self *classSample):
185  m_Iter(iter), m_Subsample(classSample), m_Sample( classSample->GetSample() )
186  {}
187 
188  // ConstIterator pointing to ImageToListSampleAdaptor
189  typename InstanceIdentifierHolder::const_iterator m_Iter;
190 
191  // Pointer to Subsample object
192  const Self * m_Subsample;
193  const TSample *m_Sample;
194 
195 private:
196  };
197 
198  class Iterator:public ConstIterator
199  {
200  friend class Subsample;
201 
202 public:
203 
204  Iterator(Self *sample):ConstIterator(sample)
205  {}
206 
207  Iterator(const Iterator & iter):ConstIterator(iter)
208  {}
209 
210  Iterator & operator=(const Iterator & iter)
211  {
212  this->ConstIterator::operator=(iter);
213  return *this;
214  }
215 
216 protected:
217  // To ensure const-correctness these method must not be in the public API.
218  // The are purposly not implemented, since they should never be called.
219  Iterator();
220  Iterator(const Self *sample);
221  Iterator(typename InstanceIdentifierHolder::const_iterator iter,
222  const Self *classSample);
223  Iterator(const ConstIterator & it);
224  ConstIterator & operator=(const ConstIterator & it);
225 
226  // Only to be called from the Subsample
227  Iterator(typename InstanceIdentifierHolder::iterator iter,
228  Self *classSample):
229  ConstIterator(iter, classSample)
230  {}
231 
232 private:
233  };
234 
238  {
239  Iterator iter(m_IdHolder.begin(), this);
240 
241  return iter;
242  }
243 
247  {
248  Iterator iter(m_IdHolder.end(), this);
249 
250  return iter;
251  }
252 
254  {
255  ConstIterator iter(m_IdHolder.begin(), this);
256 
257  return iter;
258  }
259 
261  {
262  ConstIterator iter(m_IdHolder.end(), this);
263 
264  return iter;
265  }
266 
267 protected:
268  Subsample();
269  virtual ~Subsample() {}
270  void PrintSelf(std::ostream & os, Indent indent) const ITK_OVERRIDE;
271 
272 private:
273  Subsample(const Self &) ITK_DELETE_FUNCTION;
274  void operator=(const Self &) ITK_DELETE_FUNCTION;
275 
276  const TSample * m_Sample;
278  unsigned int m_ActiveDimension;
280 }; // end of class
281 } // end of namespace Statistics
282 } // end of namespace itk
283 
284 #ifndef ITK_MANUAL_INSTANTIATION
285 #include "itkSubsample.hxx"
286 #endif
287 
288 #endif
InstanceIdentifier GetInstanceIdentifier(unsigned int index)
ConstIterator(typename InstanceIdentifierHolder::const_iterator iter, const Self *classSample)
Definition: itkSubsample.h:183
void Swap(unsigned int index1, unsigned int index2)
AbsoluteFrequencyType GetFrequency(InstanceIdentifier id) const override
const MeasurementVectorType & GetMeasurementVector() const
Definition: itkSubsample.h:168
TotalAbsoluteFrequencyType GetTotalFrequency() const override
AbsoluteFrequencyType GetFrequencyByIndex(unsigned int index) const
const MeasurementVectorType & GetMeasurementVectorByIndex(unsigned int index) const
virtual void Graft(const DataObject *thatObject) override
TSample::MeasurementType MeasurementType
Definition: itkSubsample.h:63
bool operator==(const ConstIterator &it)
Definition: itkSubsample.h:152
bool operator!=(const ConstIterator &it)
Definition: itkSubsample.h:147
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:278
Iterator & operator=(const Iterator &iter)
Definition: itkSubsample.h:210
InstanceIdentifierHolder m_IdHolder
Definition: itkSubsample.h:277
const TSample * m_Sample
Definition: itkSubsample.h:276
InstanceIdentifier GetInstanceIdentifier() const
Definition: itkSubsample.h:173
TSample::Pointer SamplePointer
Definition: itkSubsample.h:55
InstanceIdentifier Size() const override
InstanceIdentifierHolder::const_iterator m_Iter
Definition: itkSubsample.h:189
TSample::AbsoluteFrequencyType AbsoluteFrequencyType
Definition: itkSubsample.h:67
const TSample * GetSample() const
void AddInstance(InstanceIdentifier id)
SmartPointer< Self > Pointer
Definition: itkSubsample.h:48
MeasurementVectorType ValueType
Definition: itkSubsample.h:65
ConstIterator(const ConstIterator &iter)
Definition: itkSubsample.h:132
TotalAbsoluteFrequencyType m_TotalFrequency
Definition: itkSubsample.h:279
void PrintSelf(std::ostream &os, Indent indent) const override
TSample::MeasurementVectorType MeasurementVectorType
Definition: itkSubsample.h:62
AbsoluteFrequencyType GetFrequency() const
Definition: itkSubsample.h:163
ConstIterator & operator=(const ConstIterator &iter)
Definition: itkSubsample.h:139
ConstIterator End() const
Definition: itkSubsample.h:260
void SetSample(const TSample *sample)
virtual const InstanceIdentifierHolder & GetIdHolder() const
Definition: itkSubsample.h:77
Control indentation during Print() invocation.
Definition: itkIndent.h:49
const MeasurementVectorType & GetMeasurementVector(InstanceIdentifier id) const override
Iterator(typename InstanceIdentifierHolder::iterator iter, Self *classSample)
Definition: itkSubsample.h:227
ConstIterator Begin() const
Definition: itkSubsample.h:253
Base class for all data objects in ITK.
std::vector< InstanceIdentifier > InstanceIdentifierHolder
Definition: itkSubsample.h:74