This repository has been archived on 2020-04-08. You can view files and clone it, but cannot push or open issues or pull requests.
Files
Indoor/math/stats/Histogram2.h
2018-10-25 11:50:12 +02:00

64 lines
1.3 KiB
C++
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

/*
* © 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