Blender V5.0
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#pragma once
6
7#include "util/math_base.h"
8#include "util/vector.h"
9
11
12/* Evaluate CDF of a given functor with given range and resolution. */
13template<typename Functor>
15 const int resolution, const float from, const float to, Functor functor, vector<float> &cdf)
16{
17 const int cdf_count = resolution + 1;
18 const float range = to - from;
19 cdf.resize(cdf_count);
20 cdf[0] = 0.0f;
21 /* Actual CDF evaluation. */
22 for (int i = 0; i < resolution; ++i) {
23 float x = from + range * (float)i / (resolution - 1);
24 float y = functor(x);
25 cdf[i + 1] = cdf[i] + fabsf(y);
26 }
27 /* Normalize the CDF. */
28 const float fac = (cdf[resolution] == 0.0f) ? 0.0f : 1.0f / cdf[resolution];
29 for (int i = 0; i <= resolution; i++) {
30 cdf[i] *= fac;
31 }
32 cdf[resolution] = 1.0f;
33}
34
35/* Invert pre-calculated CDF function. */
36void util_cdf_invert(const int resolution,
37 const float from,
38 const float to,
39 const vector<float> &cdf,
40 const bool make_symmetric,
41 vector<float> &inv_cdf);
42
43/* Evaluate inverted CDF of a given functor with given range and resolution. */
44template<typename Functor>
45void util_cdf_inverted(const int resolution,
46 const float from,
47 const float to,
48 Functor functor,
49 const bool make_symmetric,
50 vector<float> &inv_cdf)
51{
52 vector<float> cdf;
53 /* There is no much smartness going around lower resolution for the CDF table,
54 * this just to match the old code from pixel filter so it all stays exactly
55 * the same and no regression tests are failed.
56 */
57 util_cdf_evaluate(resolution - 1, from, to, functor, cdf);
58 util_cdf_invert(resolution, from, to, cdf, make_symmetric, inv_cdf);
59}
60
nullptr float
#define CCL_NAMESPACE_END
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:14
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:14
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:45
#define fabsf
i
Definition text_draw.cc:230