- worked on about everything - grid walker using plugable modules - wifi models - new distributions - worked on geometric data-structures - added typesafe timestamps - worked on grid-building - added sensor-classes - added sensor analysis (step-detection, turn-detection) - offline data reader - many test-cases
83 lines
1.7 KiB
C++
83 lines
1.7 KiB
C++
#ifndef LUT_H
|
|
#define LUT_H
|
|
|
|
#include <vector>
|
|
#include "../Math.h"
|
|
#include "../DrawList.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];
|
|
}
|
|
|
|
/** get a DrawList for this LUT to draw random values out of it */
|
|
DrawList<Scalar> getDrawList() {
|
|
DrawList<Scalar> dl;
|
|
for (int idx = 0; idx < (int) samples.size(); ++idx) {
|
|
const Scalar val = idxToVal(idx);
|
|
const Scalar prob = samples[idx];
|
|
dl.add(val, prob);
|
|
}
|
|
return dl;
|
|
}
|
|
|
|
protected:
|
|
|
|
/** convert value [min:max] to index [0:numSamples] */
|
|
inline int valToIdx(const Scalar val) const {
|
|
return ((val - min) * numSamples / diff);
|
|
}
|
|
|
|
/** convert index [0:numSamples] to value [min:max] */
|
|
inline Scalar idxToVal(const int idx) const {
|
|
return min + (idx * diff / numSamples);
|
|
}
|
|
|
|
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
|