cmtkHistogramBase.h

Go to the documentation of this file.
00001 /*
00002 //
00003 //  Copyright 1997-2009 Torsten Rohlfing
00004 //
00005 //  Copyright 2004-2010 SRI International
00006 //
00007 //  This file is part of the Computational Morphometry Toolkit.
00008 //
00009 //  http://www.nitrc.org/projects/cmtk/
00010 //
00011 //  The Computational Morphometry Toolkit is free software: you can
00012 //  redistribute it and/or modify it under the terms of the GNU General Public
00013 //  License as published by the Free Software Foundation, either version 3 of
00014 //  the License, or (at your option) any later version.
00015 //
00016 //  The Computational Morphometry Toolkit is distributed in the hope that it
00017 //  will be useful, but WITHOUT ANY WARRANTY; without even the implied
00018 //  warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00019 //  GNU General Public License for more details.
00020 //
00021 //  You should have received a copy of the GNU General Public License along
00022 //  with the Computational Morphometry Toolkit.  If not, see
00023 //  <http://www.gnu.org/licenses/>.
00024 //
00025 //  $Revision: 2752 $
00026 //
00027 //  $LastChangedDate: 2011-01-17 11:33:31 -0800 (Mon, 17 Jan 2011) $
00028 //
00029 //  $LastChangedBy: torstenrohlfing $
00030 //
00031 */
00032 
00033 #ifndef __cmtkHistogramBase_h_included_
00034 #define __cmtkHistogramBase_h_included_
00035 
00036 #include <cmtkconfig.h>
00037 
00038 #include <Base/cmtkTypes.h>
00039 #include <Base/cmtkMathUtil.h>
00040 
00041 #include <System/cmtkSmartPtr.h>
00042 
00043 #include <algorithm>
00044 
00045 #ifndef CMTK_HISTOGRAM_AUTOBINS
00046 
00047 #define CMTK_HISTOGRAM_AUTOBINS 0
00048 #endif
00049 
00051 #define CMTK_HISTOGRAM_FRACTIONAL true
00052 
00054 #define CMTK_HISTOGRAM_DISCRETE false
00055 
00057 #define CMTK_HISTOGRAM_RESET true
00058 
00060 #define CMTK_HISTOGRAM_NORESET false
00061 
00063 #define CMTK_HISTOGRAM_COPY true
00064 
00066 #define CMTK_HISTOGRAM_NOCOPY false
00067 
00068 namespace
00069 cmtk
00070 {
00071 
00074 
00077 class HistogramBase
00078 {
00079 protected:
00081   Types::DataItem m_BinWidth;
00082 
00084   Types::DataItem m_BinsLowerBound;
00085 
00087   Types::DataItem m_BinsUpperBound;
00088 
00089 public:
00091   typedef HistogramBase Self;
00092   
00094   HistogramBase()
00095   {
00096     this->m_BinWidth = 1.0;
00097     this->m_BinsLowerBound = this->m_BinsUpperBound = 0.0;
00098   }
00099 
00101   virtual ~HistogramBase() {}
00102 
00104   virtual size_t GetNumBins() const = 0;
00105 
00108   void SetRange ( const Types::DataItemRange& range ) 
00109   {
00110     this->m_BinsLowerBound = range.m_LowerBound;
00111     this->m_BinsUpperBound = range.m_UpperBound;
00112     this->m_BinWidth = range.Width() / (this->GetNumBins() - 1);
00113   }
00114   
00117   void SetRangeCentered( const Types::DataItemRange& range ) 
00118   {
00119     this->m_BinWidth = range.Width() / (this->GetNumBins() - 1);
00120     this->m_BinsLowerBound = static_cast<Types::DataItem>( range.m_LowerBound - 0.5 * this->m_BinWidth );
00121     this->m_BinsUpperBound = static_cast<Types::DataItem>( range.m_UpperBound + 0.5 * this->m_BinWidth );
00122   }
00123 
00126   const Types::DataItemRange GetRange() const 
00127   {
00128     return Types::DataItemRange( this->m_BinsLowerBound, this->m_BinsUpperBound );
00129   }
00130 
00133   virtual const Types::DataItemRange GetRangeBin( const size_t bin ) const 
00134   {
00135     const Types::DataItem from = this->m_BinsLowerBound + this->m_BinWidth * bin;
00136     return Types::DataItemRange( from, from + this->m_BinWidth );
00137   }
00138 
00140   Types::DataItem GetBinWidth() const
00141   {
00142     return this->m_BinWidth;
00143   }
00144   
00149   virtual size_t ValueToBin ( const Types::DataItem value ) const 
00150   {
00151     const size_t binIndex = static_cast<size_t>( (value - this->m_BinsLowerBound) / this->m_BinWidth );
00152     return std::max<size_t>( 0, std::min( this->GetNumBins()-1, binIndex ) );
00153   }
00154   
00161   virtual Types::DataItem ValueToBinFractional ( const Types::DataItem value ) const 
00162   {
00163     const Types::DataItem binIndex = (value - this->m_BinsLowerBound) / this->m_BinWidth;
00164     return std::max<Types::DataItem>( 0, std::min<Types::DataItem>( static_cast<Types::DataItem>( this->GetNumBins()-1 ), binIndex));
00165   }
00166   
00171   virtual Types::DataItem BinToValue ( const size_t bin ) const 
00172   {
00173     return static_cast<Types::DataItem>( this->m_BinsLowerBound + (bin+0.5) * this->m_BinWidth );
00174   }
00175 };
00176 
00178 
00179 } // namespace cmtk
00180 
00181 #endif // #ifndef __cmtkHistogramBase_h_included_
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines