00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017 #ifndef __itkSubsample_h
00018 #define __itkSubsample_h
00019
00020 #include "itkSample.h"
00021 #include "itkMacro.h"
00022 #include "itkObjectFactory.h"
00023
00024 namespace itk{
00025 namespace Statistics{
00026
00037 template< class TSample >
00038 class ITK_EXPORT Subsample :
00039 public Sample< typename TSample::MeasurementVectorType >
00040 {
00041 public:
00043 typedef Subsample Self;
00044 typedef Sample< typename TSample::MeasurementVectorType > Superclass ;
00045 typedef SmartPointer< Self > Pointer ;
00046 typedef SmartPointer<const Self> ConstPointer;
00047
00049 itkTypeMacro(Subsample, Sample);
00050
00052 itkNewMacro(Self) ;
00053
00055 typedef typename TSample::Pointer SamplePointer ;
00056
00059 typedef typename TSample::MeasurementVectorType MeasurementVectorType;
00060 typedef typename TSample::MeasurementType MeasurementType;
00061 typedef typename TSample::InstanceIdentifier InstanceIdentifier;
00062 typedef typename TSample::FrequencyType FrequencyType ;
00063 typedef typename TSample::TotalFrequencyType TotalFrequencyType ;
00064 typedef MeasurementVectorType ValueType ;
00065
00070 typedef std::vector< InstanceIdentifier > InstanceIdentifierHolder ;
00071
00073 void SetSample(const TSample* sample)
00074 {
00075 m_Sample = sample ;
00076 this->SetMeasurementVectorSize( m_Sample->GetMeasurementVectorSize() );
00077 }
00078
00079 const TSample* GetSample() const
00080 { return m_Sample ; }
00081
00082
00083 void InitializeWithAllInstances()
00084 {
00085 m_IdHolder.resize(m_Sample->Size()) ;
00086 typename InstanceIdentifierHolder::iterator idIter = m_IdHolder.begin() ;
00087 typename TSample::ConstIterator iter = m_Sample->Begin() ;
00088 typename TSample::ConstIterator last = m_Sample->End() ;
00089 m_TotalFrequency = NumericTraits< FrequencyType >::Zero ;
00090 while (iter != last)
00091 {
00092 *idIter++ = iter.GetInstanceIdentifier() ;
00093 m_TotalFrequency += iter.GetFrequency() ;
00094 ++iter ;
00095 }
00096 }
00097
00098 void AddInstance(InstanceIdentifier id)
00099 {
00100 m_IdHolder.push_back(id) ;
00101 m_TotalFrequency += m_Sample->GetFrequency(id) ;
00102 }
00103
00106 unsigned int Size() const
00107 {
00108 return static_cast<unsigned int>( m_IdHolder.size() );
00109 }
00110
00111 void Clear()
00112 {
00113 m_IdHolder.clear() ;
00114 m_TotalFrequency = NumericTraits< FrequencyType >::Zero ;
00115 }
00116
00119 const MeasurementVectorType & GetMeasurementVector(const InstanceIdentifier &id) const
00120 { return m_Sample->GetMeasurementVector(id) ; }
00121
00123 FrequencyType GetFrequency(const InstanceIdentifier &id) const
00124 { return m_Sample->GetFrequency(id) ; }
00125
00127 TotalFrequencyType GetTotalFrequency() const
00128 { return m_TotalFrequency ; }
00129
00130 void Swap(int index1, int index2) ;
00131
00132 MeasurementVectorType GetMeasurementVectorByIndex(int index) const ;
00133
00134 FrequencyType GetFrequencyByIndex(int index) const;
00135
00136 InstanceIdentifier GetInstanceIdentifier(int index) const;
00137
00138
00139
00140 class Iterator
00141 {
00142 public:
00143 Iterator(typename InstanceIdentifierHolder::iterator iter,
00144 Self* classSample)
00145 :m_Iter(iter), m_Subsample(classSample),
00146 m_Sample(classSample->GetSample())
00147 {}
00148
00149 FrequencyType GetFrequency() const
00150 { return m_Sample->GetFrequency(*m_Iter) ; }
00151
00152 const MeasurementVectorType & GetMeasurementVector() const
00153 { return m_Sample->GetMeasurementVector(*m_Iter) ; }
00154
00155 InstanceIdentifier GetInstanceIdentifier() const
00156 { return *m_Iter ; }
00157
00158 Iterator& operator++()
00159 {
00160 ++m_Iter ;
00161 return *this ;
00162 }
00163
00164
00165
00166
00167 Iterator& operator+(int n)
00168 { m_Iter += n; return *this ;}
00169
00170 Iterator& operator-(int n)
00171 { m_Iter -= n; return *this ;}
00172
00173 bool operator!=(const Iterator& it)
00174 { return (m_Iter != it.m_Iter) ; }
00175
00176 bool operator==(const Iterator& it)
00177 { return (m_Iter == it.m_Iter) ; }
00178
00179 Iterator& operator=(const Iterator& iter)
00180 {
00181 m_Iter = iter.m_Iter;
00182 m_Subsample = iter.m_Subsample ;
00183 m_Sample = iter.m_Sample ;
00184 return *this ;
00185 }
00186
00187 Iterator(const Iterator& iter)
00188 {
00189 m_Iter = iter.m_Iter;
00190 m_Subsample = iter.m_Subsample ;
00191 m_Sample = iter.m_Sample ;
00192 }
00193
00194 private:
00195
00196 typename InstanceIdentifierHolder::iterator m_Iter ;
00197
00198 Self* m_Subsample ;
00199 const TSample* m_Sample ;
00200 } ;
00201
00202
00203
00204 class ConstIterator
00205 {
00206 public:
00207 ConstIterator(typename InstanceIdentifierHolder::const_iterator iter,
00208 const Self* classSample)
00209 :m_Iter(iter), m_Subsample(classSample),
00210 m_Sample(classSample->GetSample())
00211 {}
00212
00213 FrequencyType GetFrequency() const
00214 { return m_Sample->GetFrequency(*m_Iter) ; }
00215
00216 const MeasurementVectorType & GetMeasurementVector() const
00217 { return m_Sample->GetMeasurementVector(*m_Iter) ; }
00218
00219 InstanceIdentifier GetInstanceIdentifier() const
00220 { return *m_Iter ; }
00221
00222 ConstIterator& operator++()
00223 {
00224 ++m_Iter ;
00225 return *this ;
00226 }
00227
00228
00229
00230
00231 ConstIterator& operator+(int n)
00232 { m_Iter += n; return *this ;}
00233
00234 ConstIterator& operator-(int n)
00235 { m_Iter -= n; return *this ;}
00236
00237 bool operator!=(const ConstIterator& it)
00238 { return (m_Iter != it.m_Iter) ; }
00239
00240 bool operator==(const ConstIterator& it)
00241 { return (m_Iter == it.m_Iter) ; }
00242
00243 ConstIterator& operator=(const ConstIterator& iter)
00244 {
00245 m_Iter = iter.m_Iter;
00246 m_Subsample = iter.m_Subsample ;
00247 m_Sample = iter.m_Sample ;
00248 return *this ;
00249 }
00250
00251 ConstIterator(const ConstIterator& iter)
00252 {
00253 m_Iter = iter.m_Iter;
00254 m_Subsample = iter.m_Subsample ;
00255 m_Sample = iter.m_Sample ;
00256 }
00257
00258 private:
00259
00260 typename InstanceIdentifierHolder::const_iterator m_Iter ;
00261
00262 const Self* m_Subsample ;
00263 const TSample* m_Sample ;
00264 } ;
00265
00266
00267
00268 Iterator Begin()
00269 {
00270 Iterator iter(m_IdHolder.begin(), this) ;
00271 return iter;
00272 }
00273
00274 Iterator End()
00275 {
00276 Iterator iter(m_IdHolder.end(), this) ;
00277 return iter;
00278 }
00279
00280 ConstIterator Begin() const
00281 {
00282 ConstIterator iter(m_IdHolder.begin(), this) ;
00283 return iter;
00284 }
00285
00286 ConstIterator End() const
00287 {
00288 ConstIterator iter(m_IdHolder.end(), this) ;
00289 return iter;
00290 }
00291
00292 protected:
00293 Subsample() ;
00294 virtual ~Subsample() {}
00295 void PrintSelf(std::ostream& os, Indent indent) const;
00296
00297 private:
00298 Subsample(const Self&) ;
00299 void operator=(const Self&) ;
00300
00301 const TSample* m_Sample ;
00302 InstanceIdentifierHolder m_IdHolder ;
00303 unsigned int m_ActiveDimension ;
00304 FrequencyType m_TotalFrequency ;
00305 } ;
00306
00307
00308 }
00309 }
00310
00311
00312 #ifndef ITK_MANUAL_INSTANTIATION
00313 #include "itkSubsample.txx"
00314 #endif
00315
00316 #endif
00317
00318
00319
00320
00321
00322
00323