00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036 #ifndef HISTOGRAM2D_H_
00037 #define HISTOGRAM2D_H_
00038
00039 #include <functional>
00040 #include <algorithm>
00041 #include <vector>
00042 #include <utility>
00043 #include <structureColoring/histograms/OutOfRangeException.h>
00044
00045 template<class Key, class Value, class BinType = Value>
00046 class Histogram2D {
00047 public:
00048 typedef OutOfRangeException OutOfRange;
00049 typedef std::pair<Key, Key> Keys;
00050
00056 Histogram2D(const Key& minX, const Key& maxX, const Key& minY, const Key& maxY, const Key& binSize) :
00057 mMinX(minX), mMaxX(maxX), mMinY(minY), mMaxY(maxY), mBinSize(binSize),
00058 mXSpan(floor( (mMaxX - mMinX) / mBinSize ) + 1), mYSpan(floor( (mMaxY - mMinY) / mBinSize ) + 1),
00059 mBins(mXSpan * mYSpan){
00060 }
00061
00062 Histogram2D(const Keys& min, const Keys& max, const Key& binSize) :
00063 mMinX(min.first), mMaxX(max.first), mMinY(min.second), mMaxY(max.second), mBinSize(binSize),
00064 mXSpan(floor( (mMaxX - mMinX) / mBinSize ) + 1), mYSpan(floor( (mMaxY - mMinY) / mBinSize ) + 1),
00065 mBins(mXSpan * mYSpan){
00066 }
00067
00068 BinType& bin(const Key& x, const Key& y) { return mBins[getIdx(x, y)]; }
00069 const BinType& bin(const Key& x, const Key& y) const { return mBins[getIdx(x, y)]; }
00070 BinType& bin(const Keys& k) { return mBins[getIdx(k.first, k.second)]; }
00071 const BinType& bin(const Keys& k) const { return mBins[getIdx(k.first, k.second)]; }
00072
00073 void addToBin(const Key& x, const Key& y, const Value& v) { mBins[getIdx(x, y)] += v; }
00074 void addToBin(const Keys& k, const Value& v) { mBins[getIdx(k.first, k.second)] += v; }
00075
00076 void getMaxBin(BinType& bin, Key& x, Key& y) const {
00077 Keys k;
00078 getMaxBin(bin, k, std::less<BinType>());
00079 x = k.first;
00080 y = k.second;
00081
00082 }
00083 void getMaxBin(BinType& bin, Keys& k) const { getMaxBin(bin, k, std::less<BinType>()); std::cout << "Histogram2D.getMaxBin(bin, k)" << std::endl;}
00084 template<class Comp>
00085 void getMaxBin(BinType& bin, Key& x, Key& y, Comp comp) const {
00086 Keys k = std::make_pair(x, y);
00087 getMaxBin(bin, k, comp);
00088 x = k.first;
00089 y = k.second;
00090
00091 }
00092 template<class Comp>
00093 void getMaxBin(BinType& bin, Keys& k, Comp comp) const;
00094 protected:
00095 typedef std::vector<BinType> Bins;
00096
00097 size_t getIdx(const Key& x, const Key& y) const {
00098 size_t xIdx, yIdx;
00099 if ((x < mMinX) || (x > mMaxX) || (y < mMinY) || (y < mMaxY))
00100 throw OutOfRange("key is out of range");
00101 xIdx = (x - mMinX) / mBinSize;
00102 yIdx = (y - mMinY) / mBinSize;
00103 return xIdx + mXSpan * yIdx;
00104
00105
00106
00107
00108
00109 }
00110
00111 size_t getIdx(const size_t& xIdx, const size_t& yIdx) const {
00112 if ((xIdx > mXSpan) || (yIdx > mYSpan)){
00113 throw OutOfRange("key is out of range");
00114 }
00115 return xIdx + mXSpan * yIdx;
00116 }
00117
00118 Keys getKeys(const size_t& idx) const {
00119
00120 size_t yIdx = floor( ((float)idx) / ((float)mXSpan) );
00121 size_t xIdx = idx - mXSpan * yIdx;
00122
00123 Key x( mMinX + ((float)xIdx) * mBinSize );
00124 Key y( mMinY + ((float)yIdx) * mBinSize );
00125
00126 return std::make_pair(x, y);
00127 }
00128
00129 Key mMinX, mMaxX, mMinY, mMaxY, mBinSize;
00130 unsigned int mXSpan, mYSpan;
00131 Bins mBins;
00132 };
00133
00134 template<class Key, class Value, class BinType>
00135 template<class Comp>
00136 void Histogram2D<Key, Value, BinType>::getMaxBin(BinType& bin, Keys& k, Comp comp) const{
00137 typename Bins::const_iterator it = std::max_element(mBins.begin(), mBins.end(), comp);
00138 size_t idx = it - mBins.begin();
00139
00140 k = getKeys(idx);
00141
00142 bin = *it;
00143
00144 }
00145
00146
00147 #endif