OpenWalnut  1.4.0
WHistogram2D.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 WHISTOGRAM2D_H
00026 #define WHISTOGRAM2D_H
00027 
00028 #include <utility>
00029 
00030 #include <boost/array.hpp>
00031 
00032 #include <Eigen/Core>
00033 
00034 #include <core/graphicsEngine/WGETexture.h>
00035 
00036 #include "WHistogramND.h"
00037 
00038 /**
00039  * Uniform two dimensional histogram for double values. The terms bin and bucket are interchangeable. For the first dimensional part often the
00040  * analouge X-dimension is used and for the second, Y-dimension.
00041  */
00042 class WHistogram2D : public WHistogramND< 2 > // NOLINT
00043 {
00044 public:
00045     /**
00046      * Convenience type for a shared_ptr on this type.
00047      */
00048     typedef boost::shared_ptr< WHistogram2D > SPtr;
00049 
00050     /**
00051      * Creates a two dimensional histogram field, bounded by the given limits, containing the demanded number of buckets in each dimension.
00052      *
00053      * \param minX Minimal bound for X-values.
00054      * \param maxX Maximal bound for X-values.
00055      * \param minY Minimal bound for Y-values.
00056      * \param maxY Maximal bound for Y-values.
00057      * \param bucketsX Number of buckets in X direction.
00058      * \param bucketsY Number of buckets in Y direction.
00059      */
00060     WHistogram2D( double minX, double maxX, double minY, double maxY, size_t bucketsX, size_t bucketsY );
00061 
00062     /**
00063      * Cleans up!
00064      */
00065     ~WHistogram2D();
00066 
00067     /**
00068      * Copy constructor, performing a deep copy.
00069      *
00070      * \param other The other instance to copy from.
00071      */
00072     WHistogram2D( const WHistogram2D& other );
00073 
00074     /**
00075      * Get the count of the specified bucket.
00076      *
00077      * \param index in each dimension
00078      *
00079      * \return elements in the bucket.
00080      */
00081     virtual size_t operator()( SizeArray index ) const;
00082 
00083     /**
00084      * Convenience function to easier access the buckets for 2D.
00085      *
00086      * \param i X-index
00087      * \param j Y-index
00088      *
00089      * \return elements in the bucket.
00090      */
00091     virtual size_t operator()( size_t i, size_t j ) const;
00092 
00093     /**
00094      * Return the measure of one specific bucket. For one dimensional Histograms this is the width of the bucket, for two
00095      * dimensions this is the area, for three dims this is the volume, etc.
00096      *
00097      * \param index the measure for this bucket is queried.
00098      *
00099      * \return the size of a bucket.
00100      */
00101     virtual double getBucketSize( SizeArray index ) const;
00102 
00103     /**
00104      * Returns the actual (right-open) interval in each dimension associated with the given index.
00105      *
00106      * \param index for this bucket the intervals will be returned
00107      *
00108      * \return the right-open interval in each dimension.
00109      */
00110     virtual boost::array< std::pair< double, double >, 2 > getIntervalForIndex( SizeArray index ) const;
00111 
00112     /**
00113      * Given a value the corresponding bucket is determined and incremented by one.
00114      *
00115      * \param values The value to count into specific bucket.
00116      */
00117     void insert( TArray values );
00118 
00119     /**
00120      * Shorthand to overloaded insert function where each dimension can be overhanded separately.
00121      * \see insert()
00122      * \param x value for the first dimension.
00123      * \param y value for the second dimension.
00124      */
00125     void insert( double x, double y );
00126 
00127     /**
00128      * Copy-convert this into a texture.
00129      *
00130      * \return \c osg::ref_ptr to the two-dimensional texture.
00131      */
00132     WGETexture2D::RPtr getTexture();
00133 
00134     /**
00135      * Copy-convert this into a spherical texture. \e Spherical means hereby, that buckets representing areas near the poles have scaled counters.
00136      *
00137      * \return \c osg::ref_ptr to the two-dimensional spherical texture.
00138      */
00139     WGETexture2D::RPtr getSphereTexture();
00140 
00141 protected:
00142 private:
00143     /**
00144      * Shorthand for data structure storing bucket information. In 2D this is a matrix.
00145      */
00146     typedef Eigen::Matrix< size_t, Eigen::Dynamic, Eigen::Dynamic > BinType;
00147 
00148     /**
00149      * Storing the bucket counts, how often a value occurs.
00150      */
00151     BinType m_bins;
00152 
00153     /**
00154      * For each dimension this stores the uniform interval width.
00155      */
00156     TArray m_intervalWidth;
00157 };
00158 
00159 #endif  // WHISTOGRAM2D_H