Main Page   Groups   Namespace List   Class Hierarchy   Alphabetical List   Compound List   File List   Namespace Members   Compound Members   File Members   Concepts

Numerics/Statistics/itkSubsample.h

Go to the documentation of this file.
00001 /*=========================================================================
00002 
00003   Program:   Insight Segmentation & Registration Toolkit
00004   Module:    $RCSfile: itkSubsample.h,v $
00005   Language:  C++
00006   Date:      $Date: 2009-03-04 19:29:54 $
00007   Version:   $Revision: 1.32 $
00008 
00009   Copyright (c) Insight Software Consortium. All rights reserved.
00010   See ITKCopyright.txt or http://www.itk.org/HTML/Copyright.htm for details.
00011 
00012      This software is distributed WITHOUT ANY WARRANTY; without even 
00013      the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR 
00014      PURPOSE.  See the above copyright notices for more information.
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 
00060   typedef typename TSample::MeasurementVectorType MeasurementVectorType;
00061   typedef typename TSample::MeasurementType       MeasurementType;
00062   typedef typename TSample::InstanceIdentifier    InstanceIdentifier;
00063   typedef typename TSample::FrequencyType         FrequencyType;
00064   typedef typename TSample::TotalFrequencyType    TotalFrequencyType;
00065   typedef MeasurementVectorType                   ValueType;
00066 
00071   typedef std::vector< InstanceIdentifier > InstanceIdentifierHolder;
00072 
00074   void SetSample(const TSample* sample)
00075     { 
00076     m_Sample = sample; 
00077     this->SetMeasurementVectorSize( m_Sample->GetMeasurementVectorSize() );
00078     }
00080 
00081   const TSample* GetSample() const
00082     { return m_Sample; } 
00083 
00084 
00085   void InitializeWithAllInstances()
00086     {
00087     m_IdHolder.resize(m_Sample->Size());
00088     typename InstanceIdentifierHolder::iterator idIter = m_IdHolder.begin();
00089     typename TSample::ConstIterator iter = m_Sample->Begin();
00090     typename TSample::ConstIterator last = m_Sample->End();
00091     m_TotalFrequency = NumericTraits< FrequencyType >::Zero;
00092     while (iter != last)
00093       {
00094       *idIter++ = iter.GetInstanceIdentifier();
00095       m_TotalFrequency += iter.GetFrequency();
00096       ++iter;
00097       }
00098     }
00099 
00100   void AddInstance(InstanceIdentifier id)
00101     { 
00102     m_IdHolder.push_back(id); 
00103     m_TotalFrequency += m_Sample->GetFrequency(id);
00104     }
00105 
00108   unsigned int Size() const
00109     { 
00110     return static_cast<unsigned int>( m_IdHolder.size() );
00111     }
00112 
00113   void Clear()
00114     {
00115     m_IdHolder.clear();
00116     m_TotalFrequency = NumericTraits< FrequencyType >::Zero;
00117     }
00118 
00121   const MeasurementVectorType & GetMeasurementVector(const InstanceIdentifier &id) const
00122     { return m_Sample->GetMeasurementVector(id); }
00123 
00125   FrequencyType GetFrequency(const InstanceIdentifier &id) const
00126     { return m_Sample->GetFrequency(id); }
00127 
00129   TotalFrequencyType GetTotalFrequency() const
00130     { return m_TotalFrequency; }
00131 
00132   void Swap(int index1, int index2);
00133   
00134   MeasurementVectorType GetMeasurementVectorByIndex(int index) const;
00135 
00136   FrequencyType GetFrequencyByIndex(int index) const;
00137 
00138   InstanceIdentifier GetInstanceIdentifier(int index) const;
00139 
00140   class ConstIterator;
00141  
00142   class Iterator
00143     {
00144     friend class ConstIterator;
00145 
00146     public:
00147     Iterator(typename InstanceIdentifierHolder::iterator iter, 
00148              Self* classSample)
00149       :m_Iter(iter), m_Subsample(classSample),
00150        m_Sample(classSample->GetSample()) {}
00151     
00152     FrequencyType GetFrequency() const
00153       { return  m_Sample->GetFrequency(*m_Iter); }
00154     
00155     const MeasurementVectorType & GetMeasurementVector() const
00156       { return m_Sample->GetMeasurementVector(*m_Iter); } 
00157     
00158     InstanceIdentifier GetInstanceIdentifier() const   
00159       { return *m_Iter; }
00160     
00161     Iterator& operator++() 
00162       { 
00163       ++m_Iter;
00164       return *this;
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       // Iterator pointing to ImageToListAdaptor
00196       typename InstanceIdentifierHolder::iterator m_Iter;  
00197       // Pointer to Subsample object
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     FrequencyType GetFrequency() const
00213       { return  m_Sample->GetFrequency(*m_Iter); }
00214     
00215     const MeasurementVectorType & GetMeasurementVector() const
00216       { return m_Sample->GetMeasurementVector(*m_Iter); } 
00217     
00218     InstanceIdentifier GetInstanceIdentifier() const   
00219       { return *m_Iter; }
00220     
00221     ConstIterator& operator++() 
00222       { 
00223       ++m_Iter;
00224       return *this;
00225       }
00226     
00227     ConstIterator& operator+(int n)
00228       { m_Iter += n; return *this;}
00229     
00230     ConstIterator& operator-(int n)
00231       { m_Iter -= n; return *this;}
00232 
00233     bool operator!=(const ConstIterator& it) 
00234       { return (m_Iter != it.m_Iter); }
00235     
00236     bool operator==(const ConstIterator& it) 
00237       { return (m_Iter == it.m_Iter); }
00238     
00239     ConstIterator& operator=(const ConstIterator& iter)
00240       {
00241       m_Iter = iter.m_Iter;
00242       m_Subsample = iter.m_Subsample;
00243       m_Sample = iter.m_Sample;
00244       return *this;
00245       }
00246 
00247     ConstIterator& operator=(const Iterator& iter)
00248       {
00249       m_Iter = iter.m_Iter;
00250       m_Subsample = iter.m_Subsample;
00251       m_Sample = iter.m_Sample;
00252       return *this;
00253       }
00254 
00255     ConstIterator(const ConstIterator& iter)
00256       {
00257       m_Iter = iter.m_Iter;
00258       m_Subsample = iter.m_Subsample;
00259       m_Sample = iter.m_Sample;
00260       }
00261 
00262     ConstIterator(const Iterator& iter)
00263       {
00264       m_Iter = iter.m_Iter;
00265       m_Subsample = iter.m_Subsample;
00266       m_Sample = iter.m_Sample;
00267       }
00268     
00269     private:
00270     // ConstIterator pointing to ImageToListAdaptor
00271     typename InstanceIdentifierHolder::const_iterator m_Iter;  
00272     // Pointer to Subsample object
00273     const Self* m_Subsample;
00274     const TSample* m_Sample;
00275   };
00276 
00277   Iterator Begin()
00278     { 
00279     Iterator iter(m_IdHolder.begin(), this);
00280     return iter; 
00281     }
00282    
00283   Iterator  End()
00284     {
00285     Iterator iter(m_IdHolder.end(), this); 
00286     return iter; 
00287     }
00288 
00289   ConstIterator Begin() const
00290     { 
00291     ConstIterator iter(m_IdHolder.begin(), this);
00292     return iter; 
00293     }
00294   
00295   ConstIterator  End()  const
00296     {
00297     ConstIterator iter(m_IdHolder.end(), this); 
00298     return iter; 
00299     }
00300  
00301 protected:
00302   Subsample();
00303   virtual ~Subsample() {}
00304   void PrintSelf(std::ostream& os, Indent indent) const;
00305   
00306 private:
00307   Subsample(const Self&); //purposely not implemented
00308   void operator=(const Self&); //purposely not implemented
00309 
00310   const TSample*              m_Sample;
00311   InstanceIdentifierHolder    m_IdHolder;
00312   unsigned int                m_ActiveDimension;
00313   FrequencyType               m_TotalFrequency;
00314 }; // end of class
00315 
00316 
00317 } // end of namespace Statistics 
00318 } // end of namespace itk
00319 
00320 
00321 #ifndef ITK_MANUAL_INSTANTIATION
00322 #include "itkSubsample.txx"
00323 #endif
00324 
00325 #endif
00326 

Generated at Tue Sep 15 05:05:36 2009 for ITK by doxygen 1.5.8 written by Dimitri van Heesch, © 1997-2000