59 lines
1.1 KiB
C++
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
|