/* * © 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 LUT_H #define LUT_H #include #include "../Math.h" #include "../DrawList.h" namespace Distribution { template class LUT { private: Scalar min; Scalar max; Scalar diff; /** number of samples between min and max */ int numSamples; /** the look-up-table */ std::vector samples; public: /** ctor */ template 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 getDrawList() { DrawList 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 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