ITK  4.2.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< class TSample >
41 class ITK_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 
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 
88  void InitializeWithAllInstances();
89 
91  void AddInstance(InstanceIdentifier id);
92 
95  InstanceIdentifier Size() const;
96 
98  void Clear();
99 
102  const MeasurementVectorType & GetMeasurementVector(InstanceIdentifier id) const;
103 
105  AbsoluteFrequencyType GetFrequency(InstanceIdentifier id) const;
106 
108  TotalAbsoluteFrequencyType GetTotalFrequency() const;
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);
120 
122  {
123  friend class Subsample;
124 public:
125 
126  ConstIterator(const Self *sample)
127  {
128  *this = sample->Begin();
129  }
130 
132  {
133  m_Iter = iter.m_Iter;
134  m_Subsample = iter.m_Subsample;
135  m_Sample = iter.m_Sample;
136  }
137 
138  ConstIterator & operator=(const ConstIterator & iter)
139  {
140  m_Iter = iter.m_Iter;
141  m_Subsample = iter.m_Subsample;
142  m_Sample = iter.m_Sample;
143  return *this;
144  }
145 
146  bool operator!=(const ConstIterator & it)
147  {
148  return ( m_Iter != it.m_Iter );
149  }
150 
151  bool operator==(const ConstIterator & it)
152  {
153  return ( m_Iter == it.m_Iter );
154  }
155 
156  ConstIterator & operator++()
157  {
158  ++m_Iter;
159  return *this;
160  }
161 
162  AbsoluteFrequencyType GetFrequency() const
163  {
164  return m_Sample->GetFrequency(*m_Iter);
165  }
166 
167  const MeasurementVectorType & GetMeasurementVector() const
168  {
169  return m_Sample->GetMeasurementVector(*m_Iter);
170  }
171 
172  InstanceIdentifier GetInstanceIdentifier() const
173  {
174  return ( m_Iter - m_Subsample->GetIdHolder().begin() );
175  }
176 
177 protected:
178  // Purposely not implemented
179  ConstIterator();
180 
181  // Only to be called from the Subsample
182  ConstIterator(typename InstanceIdentifierHolder::const_iterator iter,
183  const Self *classSample):
184  m_Iter(iter), m_Subsample(classSample), m_Sample( classSample->GetSample() )
185  {}
186 
187  // ConstIterator pointing to ImageToListSampleAdaptor
188  typename InstanceIdentifierHolder::const_iterator m_Iter;
189 
190  // Pointer to Subsample object
191  const Self * m_Subsample;
192  const TSample *m_Sample;
193 private:
194  };
195 
196  class Iterator:public ConstIterator
197  {
198  friend class Subsample;
199 public:
200 
201  Iterator(Self *sample):ConstIterator(sample)
202  {}
203 
204  Iterator(const Iterator & iter):ConstIterator(iter)
205  {}
206 
207  Iterator & operator=(const Iterator & iter)
208  {
209  this->ConstIterator::operator=(iter);
210  return *this;
211  }
212 
213 protected:
214  // To ensure const-correctness these method must not be in the public API.
215  // The are purposly not implemented, since they should never be called.
216  Iterator();
217  Iterator(const Self *sample);
218  Iterator(typename InstanceIdentifierHolder::const_iterator iter,
219  const Self *classSample);
220  Iterator(const ConstIterator & it);
221  ConstIterator & operator=(const ConstIterator & it);
222 
223  // Only to be called from the Subsample
224  Iterator(typename InstanceIdentifierHolder::iterator iter,
225  Self *classSample):
226  ConstIterator(iter, classSample)
227  {}
228 private:
229  };
230 
233  Iterator Begin()
234  {
235  Iterator iter(m_IdHolder.begin(), this);
236 
237  return iter;
238  }
239 
242  Iterator End()
243  {
244  Iterator iter(m_IdHolder.end(), this);
245 
246  return iter;
247  }
248 
249  ConstIterator Begin() const
250  {
251  ConstIterator iter(m_IdHolder.begin(), this);
252 
253  return iter;
254  }
255 
256  ConstIterator End() const
257  {
258  ConstIterator iter(m_IdHolder.end(), this);
259 
260  return iter;
261  }
262 
263 protected:
264  Subsample();
265  virtual ~Subsample() {}
266  void PrintSelf(std::ostream & os, Indent indent) const;
267 
268 private:
269  Subsample(const Self &); //purposely not implemented
270  void operator=(const Self &); //purposely not implemented
271 
272  const TSample * m_Sample;
274  unsigned int m_ActiveDimension;
276 }; // end of class
277 } // end of namespace Statistics
278 } // end of namespace itk
279 
280 #ifndef ITK_MANUAL_INSTANTIATION
281 #include "itkSubsample.hxx"
282 #endif
283 
284 #endif
285