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/distribution/LUT.h
FrankE f77a28735b added von mises distributionb
quick&dirty: added activity to the grid-walkers
2016-04-21 08:59:05 +02:00

59 lines
1.1 KiB
C++

#ifndef LUT_H
#define LUT_H
#include <vector>
#include "../Math.h"
namespace Distribution {
template <typename Scalar> class LUT {
private:
Scalar min;
Scalar max;
Scalar diff;
/** number of samples between min and max */
int numSamples;
/** the look-up-table */
std::vector<Scalar> samples;
public:
/** ctor */
template <typename Distribution> LUT(const Distribution dist, const Scalar min, const Scalar max, const int numSamples) :
min(min), max(max), diff(max-min), numSamples(numSamples) {
buildLUT(dist);
}
/** get the probability of val from the LUT */
Scalar getProbability(const Scalar val) const {
int idx = ((val - min) * numSamples / diff);
idx = Math::clamp(0, numSamples-1, idx);
return samples[idx];
}
private:
/** build the look-up-table */
template <typename Distribution> void buildLUT(const Distribution dist) {
samples.resize(numSamples);
for (int idx = 0; idx < numSamples; ++idx) {
const Scalar val = min + (idx * diff / numSamples);
samples[idx] = dist.getProbability(val);
}
}
};
}
#endif // LUT_H