OpenWalnut  1.4.0
WHistogramND.h
00001 //---------------------------------------------------------------------------
00002 //
00003 // Project: OpenWalnut ( http://www.openwalnut.org )
00004 //
00005 // Copyright 2009 OpenWalnut Community, BSV@Uni-Leipzig and CNCF@MPI-CBS
00006 // For more information see http://www.openwalnut.org/copying
00007 //
00008 // This file is part of OpenWalnut.
00009 //
00010 // OpenWalnut is free software: you can redistribute it and/or modify
00011 // it under the terms of the GNU Lesser General Public License as published by
00012 // the Free Software Foundation, either version 3 of the License, or
00013 // (at your option) any later version.
00014 //
00015 // OpenWalnut is distributed in the hope that it will be useful,
00016 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00017 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00018 // GNU Lesser General Public License for more details.
00019 //
00020 // You should have received a copy of the GNU Lesser General Public License
00021 // along with OpenWalnut. If not, see <http://www.gnu.org/licenses/>.
00022 //
00023 //---------------------------------------------------------------------------
00024 
00025 #ifndef WHISTOGRAMND_H
00026 #define WHISTOGRAMND_H
00027 
00028 #include <utility>
00029 
00030 #include <boost/array.hpp>
00031 
00032 /**
00033  * This template should handly arbitrary N-dimensional histograms.
00034  *
00035  * \tparam N specifies the dimensionality
00036  * \tparam T specifies the type of data. Normally this should be double or float.
00037  */
00038 template< std::size_t N, typename T = double >
00039 /**
00040  * Interface for an N-dimensional histogram.
00041  */
00042 class WHistogramND // NOLINT
00043 {
00044 public:
00045     /**
00046      * Shorthand for N-dimensional indices, counter, etc.
00047      */
00048     typedef boost::array< size_t, N > SizeArray;
00049 
00050     /**
00051      * Shorthand for N-dimensional values of type T.
00052      */
00053     typedef boost::array< T, N > TArray; // e.g. DoubleArray for T == double
00054 
00055     /**
00056      * Default constructor. Creates an empty N-dimensional histogram covering the specified min and max values with the specified number of buckets.
00057      *
00058      * \param min the smallest value(s) in each dimension
00059      * \param max the largest value(s) in each dimension
00060      * \param buckets the number of buckets in each direction (may be non uniform).
00061      */
00062     WHistogramND( TArray min, TArray max, SizeArray buckets );
00063 
00064     /**
00065      * Copy constructor. Creates a deep copy of the specified ND histogram.
00066      *
00067      * \param hist the HistogramND to copy.
00068      */
00069     WHistogramND( const WHistogramND& hist );
00070 
00071     /**
00072      * Default destructor.
00073      */
00074     virtual ~WHistogramND();
00075 
00076     /**
00077      * Get the count of the specified bucket.
00078      *
00079      * \param index in each dimension
00080      *
00081      * \return elements in the bucket.
00082      */
00083     virtual size_t operator()( SizeArray index ) const = 0;
00084 
00085     /**
00086      * Returns the number of buckets in the HistogramND with the actual mapping.
00087      *
00088      * \return number of buckets
00089      */
00090     virtual size_t size() const;
00091 
00092     /**
00093      * Returns the minimum value(s).
00094      *
00095      * \return minimum
00096      */
00097     virtual TArray getMinima() const;
00098 
00099     /**
00100      * Returns the maximum value(s).
00101      *
00102      * \return maximum
00103      */
00104     virtual TArray getMaxima() const;
00105 
00106     /**
00107      * Return the measure of one specific bucket. For one dimensional Histograms this is the width of the bucket, for two
00108      * dimensions this is the area, for three dims this is the volume, etc.
00109      *
00110      * \param index the measure for this bucket is queried.
00111      *
00112      * \return the size of a bucket.
00113      */
00114     virtual T getBucketSize( SizeArray index ) const = 0;
00115 
00116     /**
00117      * Returns the actual (right-open) interval in each dimension associated with the given index.
00118      *
00119      * \param index for this bucket the intervals will be returned
00120      *
00121      * \return the right-open interval in each dimension.
00122      */
00123     virtual boost::array< std::pair< T, T >, N > getIntervalForIndex( SizeArray index ) const = 0;
00124 
00125 protected:
00126     /**
00127      * Default constructor is protected to allow subclassing constructors not to use initialization lists which would
00128      * imply C++11 or GNU++11 style initializers for boost::array members. To workaround, reset() member function is
00129      * provided.
00130      */
00131     WHistogramND();
00132 
00133     /**
00134      * Initializes all members. This exists because to circumvent boost::array initializers in c'tor init lists and
00135      * reduces code duplication, as it is used in this abstract class as well as in its base classes.
00136      *
00137      * \param min Minimal values in each dimension.
00138      * \param max Maximal values in each dimension.
00139      * \param buckets \#buckets in each dimension.
00140      */
00141     void reset( TArray min, TArray max, SizeArray buckets );
00142 
00143     /**
00144      * The smallest value in each dimension.
00145      */
00146     TArray m_min;
00147 
00148     /**
00149      * The biggest value in each dimension.
00150      */
00151     TArray m_max;
00152 
00153     /**
00154      * The number of buckets.
00155      */
00156     SizeArray m_buckets;
00157 
00158     /**
00159      * Total number of buckets.
00160      */
00161     size_t m_nbBuckets;
00162 
00163 private:
00164 };
00165 
00166 template< std::size_t N, typename T >
00167 WHistogramND< N, T >::WHistogramND()
00168 {
00169 }
00170 
00171 template< std::size_t N, typename T >
00172 WHistogramND< N, T >::WHistogramND( TArray min, TArray max, SizeArray buckets )
00173 {
00174     reset( min, max, buckets );
00175 }
00176 
00177 template< std::size_t N, typename T >
00178 void WHistogramND< N, T >::reset( TArray min, TArray max, SizeArray buckets )
00179 {
00180     m_min = min;
00181     m_max = max;
00182 
00183     WAssert( min.size() == max.size(), "Error, WHistogram initialized with wrong dimensionality" );
00184     for( size_t i = 0; i < min.size(); ++i )
00185     {
00186         WAssert( min[i] <= max[i], "Error, WHistogram has at least one dimension where max is smaller than min" );
00187     }
00188 
00189     m_buckets = buckets;
00190     m_nbBuckets = 1;
00191     for( typename SizeArray::const_iterator cit = buckets.begin(); cit != buckets.end(); ++cit )
00192     {
00193         m_nbBuckets *= *cit;
00194     }
00195 }
00196 
00197 template< std::size_t N, typename T >
00198 WHistogramND< N, T >::~WHistogramND()
00199 {
00200 }
00201 
00202 template< std::size_t N, typename T >
00203 WHistogramND< N, T >::WHistogramND( const WHistogramND& other )
00204 {
00205     m_min = other.m_min;
00206     m_max = other.m_max;
00207     m_buckets = other.m_buckets;
00208     m_nbBuckets = other.m_nbBuckets;
00209 }
00210 
00211 template< std::size_t N, typename T >
00212 size_t WHistogramND< N, T >::size() const
00213 {
00214     return m_nbBuckets;
00215 }
00216 
00217 template< std::size_t N, typename T >
00218 typename WHistogramND< N, T >::TArray WHistogramND< N, T >::getMinima() const
00219 {
00220     return m_min;
00221 }
00222 
00223 template< std::size_t N, typename T >
00224 typename WHistogramND< N, T >::TArray WHistogramND< N, T >::getMaxima() const
00225 {
00226     return m_max;
00227 }
00228 
00229 #endif  // WHISTOGRAMND_H