Blender V4.3
math_cdf.h
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2011-2022 Blender Foundation
2 *
3 * SPDX-License-Identifier: Apache-2.0 */
4
5#ifndef __UTIL_MATH_CDF_H__
6#define __UTIL_MATH_CDF_H__
7
8#include "util/algorithm.h"
9#include "util/math.h"
10#include "util/vector.h"
11
13
14/* Evaluate CDF of a given functor with given range and resolution. */
15template<typename Functor>
17 const int resolution, const float from, const float to, Functor functor, vector<float> &cdf)
18{
19 const int cdf_count = resolution + 1;
20 const float range = to - from;
21 cdf.resize(cdf_count);
22 cdf[0] = 0.0f;
23 /* Actual CDF evaluation. */
24 for (int i = 0; i < resolution; ++i) {
25 float x = from + range * (float)i / (resolution - 1);
26 float y = functor(x);
27 cdf[i + 1] = cdf[i] + fabsf(y);
28 }
29 /* Normalize the CDF. */
30 float fac = (cdf[resolution] == 0.0f) ? 0.0f : 1.0f / cdf[resolution];
31 for (int i = 0; i <= resolution; i++) {
32 cdf[i] *= fac;
33 }
34 cdf[resolution] = 1.0f;
35}
36
37/* Invert pre-calculated CDF function. */
38void util_cdf_invert(const int resolution,
39 const float from,
40 const float to,
41 const vector<float> &cdf,
42 const bool make_symmetric,
43 vector<float> &inv_cdf);
44
45/* Evaluate inverted CDF of a given functor with given range and resolution. */
46template<typename Functor>
47void util_cdf_inverted(const int resolution,
48 const float from,
49 const float to,
50 Functor functor,
51 const bool make_symmetric,
52 vector<float> &inv_cdf)
53{
54 vector<float> cdf;
55 /* There is no much smartness going around lower resolution for the CDF table,
56 * this just to match the old code from pixel filter so it all stays exactly
57 * the same and no regression tests are failed.
58 */
59 util_cdf_evaluate(resolution - 1, from, to, functor, cdf);
60 util_cdf_invert(resolution, from, to, cdf, make_symmetric, inv_cdf);
61}
62
64
65#endif /* __UTIL_MATH_H_CDF__ */
StackEntry * from
#define CCL_NAMESPACE_END
#define fabsf(x)
draw_view in_light_buf[] float
CCL_NAMESPACE_BEGIN void util_cdf_evaluate(const int resolution, const float from, const float to, Functor functor, vector< float > &cdf)
Definition math_cdf.h:16
void util_cdf_invert(const int resolution, const float from, const float to, const vector< float > &cdf, const bool make_symmetric, vector< float > &inv_cdf)
Definition math_cdf.cpp:13
void util_cdf_inverted(const int resolution, const float from, const float to, Functor functor, const bool make_symmetric, vector< float > &inv_cdf)
Definition math_cdf.h:47