ITK  4.3.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 
125 public:
126 
127  ConstIterator(const Self *sample)
128  {
129  *this = sample->Begin();
130  }
131 
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 
163  AbsoluteFrequencyType GetFrequency() const
164  {
165  return m_Sample->GetFrequency(*m_Iter);
166  }
167 
168  const MeasurementVectorType & GetMeasurementVector() const
169  {
170  return m_Sample->GetMeasurementVector(*m_Iter);
171  }
172 
173  InstanceIdentifier GetInstanceIdentifier() const
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 
237  Iterator Begin()
238  {
239  Iterator iter(m_IdHolder.begin(), this);
240 
241  return iter;
242  }
243 
246  Iterator End()
247  {
248  Iterator iter(m_IdHolder.end(), this);
249 
250  return iter;
251  }
252 
253  ConstIterator Begin() const
254  {
255  ConstIterator iter(m_IdHolder.begin(), this);
256 
257  return iter;
258  }
259 
260  ConstIterator End() const
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;
271 
272 private:
273  Subsample(const Self &); //purposely not implemented
274  void operator=(const Self &); //purposely not implemented
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
289