21 #ifndef LIB_LIGHTMETRICA_MATH_DISTRIBUTION_H
22 #define LIB_LIGHTMETRICA_MATH_DISTRIBUTION_H
24 #include "math.types.h"
29 LM_MATH_NAMESPACE_BEGIN
43 void Add(
const Math::Float& v)
45 cdf.push_back(cdf.back() + v);
50 auto sum = cdf.back();
51 if (sum > Math::Float(0))
53 auto invSum = Math::Float(1) / sum;
61 LM_LOG_WARN(
"Unable to normalize. Sum is zero.")
65 size_t Sample(
const Math::Float& u)
const
67 size_t v =
static_cast<size_t>(std::upper_bound(cdf.begin(), cdf.end(), u) - cdf.begin()) - 1;
68 return Math::Clamp<size_t>(v, 0, cdf.size() - 2);
71 Math::Float EvaluatePDF(
int i)
const
73 return (i < 0 || i + 1 >= static_cast<int>(cdf.size())) ? Math::Float(0) : cdf[i + 1] - cdf[i];
79 cdf.push_back(Math::Float(0));
84 return cdf.size() == 1;
89 std::vector<Math::Float> cdf;
96 #endif // LIB_LIGHTMETRICA_MATH_DISTRIBUTION_H
Definition: math.distribution.h:35