64 lines
1.3 KiB
C++
64 lines
1.3 KiB
C++
/*
|
||
* © Copyright 2014 – Urheberrechtshinweis
|
||
* Alle Rechte vorbehalten / All Rights Reserved
|
||
*
|
||
* Programmcode ist urheberrechtlich geschuetzt.
|
||
* Das Urheberrecht liegt, soweit nicht ausdruecklich anders gekennzeichnet, bei Frank Ebner.
|
||
* Keine Verwendung ohne explizite Genehmigung.
|
||
* (vgl. § 106 ff UrhG / § 97 UrhG)
|
||
*/
|
||
|
||
#ifndef HISTOGRAM2_H
|
||
#define HISTOGRAM2_H
|
||
|
||
#include <vector>
|
||
#include "../../geo/BBox2.h"
|
||
|
||
namespace Stats {
|
||
|
||
/** 2D histogram */
|
||
template <typename Scalar> class Histogram2 {
|
||
|
||
std::vector<Scalar> vec;
|
||
BBox2 bbox;
|
||
int binsX;
|
||
int binsY;
|
||
|
||
public:
|
||
|
||
/** ctor */
|
||
Histogram2(BBox2 bbox, int binsX, int binsY) : bbox(bbox), binsX(binsX), binsY(binsY) {
|
||
vec.resize(binsX*binsY);
|
||
}
|
||
|
||
Scalar get(Scalar x, Scalar y) const {
|
||
const int idx = binIdx(x,y);
|
||
return vec[idx];
|
||
}
|
||
|
||
void add(Scalar x, Scalar y, Scalar val) {
|
||
const int idx = binIdx(x,y);
|
||
vec[idx] += val;
|
||
}
|
||
|
||
int binIdx(const Scalar x, const Scalar y) {
|
||
const int ix = binIdxX(x);
|
||
const int iy = binIdxY(y);
|
||
return ix + iy*binsX;
|
||
}
|
||
|
||
int binIdxX(const Scalar val) const {
|
||
return (val - bbox.getMin().x) / (bbox.getSize().x) * binsX;
|
||
}
|
||
|
||
int binIdxY(const Scalar val) const {
|
||
return (val - bbox.getMin().y) / (bbox.getSize().y) * binsY;
|
||
}
|
||
|
||
|
||
};
|
||
|
||
}
|
||
|
||
#endif // HISTOGRAM2_H
|