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
00022 namespace itk{
00023 namespace Statistics{
00024
00025 template< class TSample >
00026 class ITK_EXPORT Subsample :
00027 public Sample< typename TSample::MeasurementVectorType >
00028 {
00029 public:
00031 typedef Subsample Self;
00032 typedef Sample< typename TSample::MeasurementVectorType > Superclass ;
00033 typedef SmartPointer< Self > Pointer ;
00034
00036 itkTypeMacro(Subsample, Sample);
00037
00039 itkNewMacro(Self) ;
00040
00042 typedef typename TSample::Pointer SamplePointer ;
00043
00046 typedef typename TSample::MeasurementVectorType MeasurementVectorType;
00047 typedef typename TSample::MeasurementType MeasurementType;
00048 typedef typename TSample::InstanceIdentifier InstanceIdentifier;
00049 typedef typename TSample::FrequencyType FrequencyType ;
00050 typedef MeasurementVectorType ValueType ;
00051
00052
00053
00055 itkStaticConstMacro(MeasurementVectorSize, unsigned int,
00056 TSample::MeasurementVectorSize);
00057
00062 typedef std::vector< InstanceIdentifier > InstanceIdentifierHolder ;
00063
00065 void SetSample(TSample* sample)
00066 { m_Sample = sample ; }
00067
00068 TSample* GetSample()
00069 { return m_Sample ; }
00070
00071 void InitializeWithAllInstances()
00072 {
00073 m_IdHolder.resize(m_Sample->Size()) ;
00074 typename InstanceIdentifierHolder::iterator idIter = m_IdHolder.begin() ;
00075 typename TSample::Iterator iter = m_Sample->Begin() ;
00076 typename TSample::Iterator last = m_Sample->End() ;
00077 m_TotalFrequency = NumericTraits< FrequencyType >::Zero ;
00078 while (iter != last)
00079 {
00080 *idIter++ = iter.GetInstanceIdentifier() ;
00081 m_TotalFrequency += iter.GetFrequency() ;
00082 ++iter ;
00083 }
00084 }
00085
00086 void AddInstance(InstanceIdentifier id)
00087 {
00088 m_IdHolder.push_back(id) ;
00089 m_TotalFrequency += m_Sample->GetFrequency(id) ;
00090 }
00091
00094 unsigned int Size() const
00095 {
00096 return m_IdHolder.size() ;
00097 }
00098
00099 unsigned int Size(const unsigned int &) const
00100 {
00101 return m_IdHolder.size() ;
00102 }
00103
00104 unsigned int GetNumberOfInstances() const ;
00105
00106 void Clear()
00107 {
00108 m_IdHolder.clear() ;
00109 m_TotalFrequency = NumericTraits< FrequencyType >::Zero ;
00110 }
00111
00114 MeasurementVectorType& GetMeasurementVector(const InstanceIdentifier &id)
00115 { return m_Sample->GetMeasurementVector(id) ; }
00116
00118 FrequencyType GetFrequency(const InstanceIdentifier &id) const
00119 { return m_Sample->GetFrequency(id) ; }
00120
00122 FrequencyType GetTotalFrequency(const unsigned int &) const
00123 { return m_TotalFrequency ; }
00124
00125 void Swap(int index1, int index2) ;
00126
00127 MeasurementVectorType& GetMeasurementVectorByIndex(int index) ;
00128
00129 InstanceIdentifier GetInstanceIdentifier(int index) ;
00130
00131 class Iterator;
00132 friend class Iterator;
00133
00134 Iterator Begin()
00135 {
00136 Iterator iter(m_IdHolder.begin(), this) ;
00137 return iter;
00138 }
00139
00140 Iterator End()
00141 {
00142 Iterator iter(m_IdHolder.end(), this) ;
00143 return iter;
00144 }
00145
00146 class Iterator
00147 {
00148 public:
00149 Iterator(typename InstanceIdentifierHolder::iterator iter,
00150 Self* classSample)
00151 :m_Iter(iter), m_Subsample(classSample),
00152 m_Sample(classSample->GetSample())
00153 {}
00154
00155 FrequencyType GetFrequency() const
00156 { return m_Sample->GetFrequency(*m_Iter) ; }
00157
00158 MeasurementVectorType& GetMeasurementVector()
00159 { return m_Sample->GetMeasurementVector(*m_Iter) ; }
00160
00161 InstanceIdentifier GetInstanceIdentifier() const
00162 { return *m_Iter ; }
00163
00164 Iterator& operator++()
00165 {
00166 ++m_Iter ;
00167 return *this ;
00168 }
00169
00170 Iterator& operator+()
00171 { m_Iter += n; return *this ;}
00172
00173 Iterator& operator+(int n)
00174 { m_Iter += n; return *this ;}
00175
00176 Iterator& operator-(int n)
00177 { m_Iter -= n; return *this ;}
00178
00179 bool operator!=(const Iterator& it)
00180 { return (m_Iter != it.m_Iter) ; }
00181
00182 bool operator==(const Iterator& it)
00183 { return (m_Iter == it.m_Iter) ; }
00184
00185 Iterator& operator=(const Iterator& iter)
00186 {
00187 m_Iter = iter.m_Iter;
00188 m_Subsample = iter.m_Subsample ;
00189 m_Sample = iter.m_Sample ;
00190 return *this ;
00191 }
00192
00193 Iterator(const Iterator& iter)
00194 {
00195 m_Iter = iter.m_Iter;
00196 m_Subsample = iter.m_Subsample ;
00197 m_Sample = iter.m_Sample ;
00198 }
00199
00200 private:
00201
00202 typename InstanceIdentifierHolder::iterator m_Iter ;
00203
00204 Self* m_Subsample ;
00205 TSample* m_Sample ;
00206 } ;
00207
00208 protected:
00209 Subsample() ;
00210 virtual ~Subsample() {}
00211 void PrintSelf(std::ostream& os, Indent indent) const;
00212
00213 private:
00214 Subsample(const Self&) ;
00215 void operator=(const Self&) ;
00216
00217 TSample* m_Sample ;
00218 InstanceIdentifierHolder m_IdHolder ;
00219 unsigned int m_ActiveDimension ;
00220 FrequencyType m_TotalFrequency ;
00221 } ;
00222
00223
00224 }
00225 }
00226
00227
00228 #ifndef ITK_MANUAL_INSTANTIATION
00229 #include "itkSubsample.txx"
00230 #endif
00231
00232 #endif
00233
00234
00235
00236
00237
00238
00239