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

Review/Statistics/itkHistogram.h

Go to the documentation of this file.
00001 /*=========================================================================
00002 
00003   Program:   Insight Segmentation & Registration Toolkit
00004   Module:    $RCSfile: itkHistogram.h,v $
00005   Language:  C++
00006   Date:      $Date: 2009-05-22 12:54:58 $
00007   Version:   $Revision: 1.3 $
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 __itkHistogram_h
00018 #define __itkHistogram_h
00019 
00020 #include <vector>
00021 
00022 #include "itkArray.h"
00023 #include "itkSample.h"
00024 #include "itkDenseFrequencyContainer2.h"
00025 #include "itkSparseFrequencyContainer2.h"
00026 
00027 namespace itk {
00028 namespace Statistics {
00029 
00036 template <typename THistogram>
00037 struct GetHistogramDimension
00038 {
00039   itkStaticConstMacro(HistogramDimension, unsigned int, THistogram::MeasurementVectorSize);
00040 };
00041 
00079 template < class TMeasurement = float,
00080            class TFrequencyContainer = DenseFrequencyContainer2 >
00081 class ITK_EXPORT Histogram
00082   : public Sample < Array< TMeasurement > >
00083 {
00084 public:
00085 
00086   // This type serves as the indirect definition of MeasurementVectorType
00087   typedef Array< TMeasurement >        ArrayType;
00088 
00090   typedef Histogram                    Self;
00091   typedef Sample< ArrayType  >         Superclass;
00092   typedef SmartPointer<Self>           Pointer;
00093   typedef SmartPointer<const Self>     ConstPointer;
00094 
00096   itkTypeMacro(Histogram, Sample);
00097 
00099   itkNewMacro(Self);
00100 
00102   typedef TMeasurement MeasurementType;
00103 
00105   itkSuperclassTraitMacro(MeasurementVectorType)
00106   itkSuperclassTraitMacro(InstanceIdentifier)
00107   itkSuperclassTraitMacro(MeasurementVectorSizeType)
00108     typedef MeasurementVectorType ValueType;
00109 
00111   typedef TFrequencyContainer                        FrequencyContainerType;
00112   typedef typename FrequencyContainerType::Pointer   FrequencyContainerPointer;
00113 
00115   typedef typename FrequencyContainerType::AbsoluteFrequencyType      AbsoluteFrequencyType;
00116   typedef typename FrequencyContainerType::TotalAbsoluteFrequencyType TotalAbsoluteFrequencyType;
00117   typedef typename FrequencyContainerType::RelativeFrequencyType      RelativeFrequencyType;
00118   typedef typename FrequencyContainerType::TotalRelativeFrequencyType TotalRelativeFrequencyType;
00119 
00121   typedef Array< long >                     IndexType;
00122   typedef typename IndexType::ValueType     IndexValueType;
00123 
00125   typedef Array< unsigned long >            SizeType;
00126   typedef typename SizeType::ValueType      SizeValueType;
00127 
00129   typedef std::vector< MeasurementType >    BinMinVectorType;
00130   typedef std::vector< MeasurementType >    BinMaxVectorType;
00131   typedef std::vector< BinMinVectorType >   BinMinContainerType;
00132   typedef std::vector< BinMaxVectorType >   BinMaxContainerType;
00133 
00134 
00138   void Initialize(const SizeType &size);
00139 
00140 
00144   void Initialize(const SizeType &size, MeasurementVectorType& lowerBound,
00145                   MeasurementVectorType& upperBound);
00146 
00148   void SetToZero();
00149 
00153   const IndexType & GetIndex(const MeasurementVectorType& measurement) const;
00154 
00158   bool GetIndex(const MeasurementVectorType & measurement,
00159                 IndexType & index ) const;
00160 
00164   const IndexType & GetIndex( InstanceIdentifier id) const;
00165 
00168   itkGetConstMacro(ClipBinsAtEnds, bool);
00169 
00172   itkSetMacro(ClipBinsAtEnds, bool);
00173 
00176   bool IsIndexOutOfBounds(const IndexType &index) const;
00177 
00181   InstanceIdentifier GetInstanceIdentifier(const IndexType &index) const;
00182 
00184   InstanceIdentifier Size() const;
00185 
00187   const SizeType & GetSize() const;
00188 
00190   SizeValueType GetSize(unsigned int dimension) const;
00191 
00193   const MeasurementType& GetBinMin(unsigned int dimension,
00194                              InstanceIdentifier nbin) const;
00195 
00197   const MeasurementType& GetBinMax(unsigned int dimension,
00198                              InstanceIdentifier nbin) const;
00199 
00201   void SetBinMin(unsigned int dimension, InstanceIdentifier nbin,
00202                  MeasurementType min);
00203 
00205   void SetBinMax(unsigned int dimension,
00206                  InstanceIdentifier nbin, MeasurementType max);
00207 
00210   const MeasurementType& GetBinMinFromValue(unsigned int dimension,
00211                                       float value ) const;
00212 
00215   const MeasurementType& GetBinMaxFromValue(unsigned int dimension,
00216                                       float value ) const;
00217 
00219   const BinMinVectorType& GetDimensionMins(unsigned int dimension) const;
00220 
00222   const BinMaxVectorType& GetDimensionMaxs(unsigned int dimension) const;
00223 
00225   const BinMinContainerType& GetMins() const;
00226 
00228   const BinMaxContainerType& GetMaxs() const;
00229 
00231   const MeasurementVectorType & GetHistogramMinFromIndex(const IndexType &index) const;
00232 
00234   const MeasurementVectorType& GetHistogramMaxFromIndex(const IndexType &index) const;
00235 
00237   AbsoluteFrequencyType GetFrequency( InstanceIdentifier id ) const;
00238 
00240   AbsoluteFrequencyType GetFrequency(const IndexType &index) const;
00241 
00243   void SetFrequency( AbsoluteFrequencyType value );
00244 
00247   bool SetFrequency( InstanceIdentifier id, AbsoluteFrequencyType value);
00248 
00251   bool SetFrequency(const IndexType &index,
00252                     AbsoluteFrequencyType value);
00253 
00256   bool SetFrequency(const MeasurementVectorType &measurement,
00257                     AbsoluteFrequencyType value);
00258 
00259 
00263   bool IncreaseFrequency(InstanceIdentifier id, AbsoluteFrequencyType value);
00264 
00268   bool IncreaseFrequency(const IndexType &index, AbsoluteFrequencyType value);
00269 
00273   bool IncreaseFrequency(const MeasurementVectorType &measurement,
00274                          AbsoluteFrequencyType value);
00275 
00279   const MeasurementVectorType & GetMeasurementVector( InstanceIdentifier id) const;
00280 
00282   const MeasurementVectorType & GetMeasurementVector(const IndexType &index) const;
00283 
00286   MeasurementType GetMeasurement(InstanceIdentifier n,
00287                                  unsigned int dimension) const;
00288 
00290   TotalAbsoluteFrequencyType GetTotalFrequency() const;
00291 
00293   AbsoluteFrequencyType GetFrequency(InstanceIdentifier n,
00294                              unsigned int dimension) const;
00295 
00311   double Quantile(unsigned int dimension, double p) const;
00313 
00315   virtual void Graft( const DataObject * );
00316 
00317 protected:
00318   void PrintSelf(std::ostream& os, Indent indent) const;
00319 
00320 public:
00321 
00324   class ConstIterator
00325     {
00326     public:
00327 
00328     friend class Histogram;
00329 
00330     ConstIterator(const Self * histogram)
00331       {
00332       m_Id = 0;
00333       m_Histogram = histogram;
00334       }
00335 
00336     ConstIterator(const ConstIterator & it)
00337       {
00338       m_Id        = it.m_Id;
00339       m_Histogram = it.m_Histogram;
00340       }
00341 
00342     ConstIterator& operator=(const ConstIterator& it)
00343       {
00344       m_Id  = it.m_Id;
00345       m_Histogram = it.m_Histogram;
00346       return *this;
00347       }
00348 
00349     AbsoluteFrequencyType GetFrequency() const
00350       {
00351       return  m_Histogram->GetFrequency(m_Id);
00352       }
00353 
00354     InstanceIdentifier GetInstanceIdentifier() const
00355       {
00356       return m_Id;
00357       }
00358 
00359     const MeasurementVectorType & GetMeasurementVector() const
00360       {
00361       return m_Histogram->GetMeasurementVector(m_Id);
00362       }
00363 
00364     ConstIterator& operator++()
00365       {
00366       ++m_Id;
00367       return *this;
00368       }
00369 
00370     bool operator!=(const ConstIterator& it)
00371       {
00372       return (m_Id != it.m_Id);
00373       }
00374 
00375     bool operator==(const ConstIterator& it)
00376       {
00377       return (m_Id == it.m_Id);
00378       }
00379 
00380 #if !(defined(_MSC_VER) && (_MSC_VER <= 1200))
00381   protected:
00382 #endif
00383     // This method is purposely not implemented
00384     ConstIterator();
00385 
00386     ConstIterator(InstanceIdentifier id, const Self * histogram)
00387       : m_Id(id), m_Histogram(histogram)
00388       {}
00389 
00390     // ConstIterator pointing DenseFrequencyContainer
00391     InstanceIdentifier m_Id;
00392 
00393     // Pointer of DenseFrequencyContainer
00394     const Self* m_Histogram;
00395     }; // end of iterator class
00396 
00398   class Iterator : public ConstIterator
00399   {
00400   public:
00401 
00402     Iterator(Self * histogram):ConstIterator( histogram )
00403       {
00404       }
00405 
00406     Iterator(InstanceIdentifier id, Self * histogram)
00407         : ConstIterator( id, histogram )
00408       {}
00409 
00410     Iterator(const Iterator & it):ConstIterator(it)
00411       {
00412       }
00413 
00414     Iterator & operator=(const Iterator& it)
00415       {
00416       this->ConstIterator::operator=( it );
00417       return *this;
00418       }
00419 
00420     bool SetFrequency(const AbsoluteFrequencyType value)
00421       {
00422       Self * histogram = const_cast< Self * >( this->m_Histogram );
00423       return histogram->SetFrequency( this->m_Id, value );
00424       }
00425 
00426 #if !(defined(_MSC_VER) && (_MSC_VER <= 1200))
00427   protected:
00428 #endif
00429     // To ensure const-correctness these method must not be in the public API.
00430     // The are purposly not implemented, since they should never be called.
00431     Iterator();
00432     Iterator(const Self * histogram);
00433     Iterator(InstanceIdentifier id, const Self * histogram);
00434     Iterator(const ConstIterator & it);
00435     ConstIterator& operator=(const ConstIterator& it);
00436 
00437   private:
00438     }; // end of iterator class
00439 
00440 
00441   Iterator  Begin()
00442     {
00443     Iterator iter(0, this);
00444     return iter;
00445     }
00446 
00447   Iterator  End()
00448     {
00449     return Iterator(m_OffsetTable[this->GetMeasurementVectorSize()], this);
00450     }
00451 
00452   ConstIterator  Begin() const
00453     {
00454     ConstIterator iter(0, this);
00455     return iter;
00456     }
00457 
00458   ConstIterator End() const
00459     {
00460     return ConstIterator(m_OffsetTable[this->GetMeasurementVectorSize()], this);
00461     }
00462 
00463 protected:
00464   Histogram();
00465   virtual ~Histogram() {}
00466 
00467   // The number of bins for each dimension
00468   SizeType m_Size;
00469 
00470 private:
00471   Histogram(const Self&); //purposely not implemented
00472   void operator=(const Self&); //purposely not implemented
00473 
00474   typedef std::vector< InstanceIdentifier >   OffsetTableType;
00475   OffsetTableType                             m_OffsetTable;
00476   FrequencyContainerPointer                   m_FrequencyContainer;
00477   unsigned int                                m_NumberOfInstances;
00478 
00479   // This method is provided here just to avoid a "hidden" warning 
00480   // related to the virtual method available in DataObject.
00481   virtual void Initialize() {};
00482 
00483   // lower bound of each bin
00484   std::vector< std::vector<MeasurementType> > m_Min;
00485 
00486   // upper bound of each bin
00487   std::vector< std::vector<MeasurementType> > m_Max;
00488 
00489   mutable MeasurementVectorType   m_TempMeasurementVector;
00490   mutable IndexType               m_TempIndex;
00491 
00492   bool                            m_ClipBinsAtEnds;
00493 };
00494 
00495 } // end of namespace Statistics
00496 } // end of namespace itk
00497 
00498 #ifndef ITK_MANUAL_INSTANTIATION
00499 #include "itkHistogram.txx"
00500 #endif
00501 
00502 #endif
00503 

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