Blender V4.3
math_cdf.cpp
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2011-2022 Blender Foundation
2 *
3 * SPDX-License-Identifier: Apache-2.0 */
4
5#include "util/math_cdf.h"
6
7#include "util/algorithm.h"
8#include "util/math.h"
9
11
12/* Invert pre-calculated CDF function. */
13void util_cdf_invert(const int resolution,
14 const float from,
15 const float to,
16 const vector<float> &cdf,
17 const bool make_symmetric,
18 vector<float> &inv_cdf)
19{
20 const int cdf_size = cdf.size();
21 assert(cdf[0] == 0.0f && cdf[cdf_size - 1] == 1.0f);
22
23 const float inv_resolution = 1.0f / (float)resolution;
24 const float range = to - from;
25 inv_cdf.resize(resolution);
26 if (make_symmetric) {
27 const int half_size = (resolution - 1) / 2;
28 for (int i = 0; i <= half_size; i++) {
29 float x = i / (float)half_size;
30 int index = upper_bound(cdf.begin(), cdf.end(), x) - cdf.begin();
31 float t;
32 if (index < cdf_size - 1) {
33 t = (x - cdf[index]) / (cdf[index + 1] - cdf[index]);
34 }
35 else {
36 t = 0.0f;
37 index = cdf_size - 1;
38 }
39 float y = ((index + t) / (resolution - 1)) * (2.0f * range);
40 inv_cdf[half_size + i] = 0.5f * (1.0f + y);
41 inv_cdf[half_size - i] = 0.5f * (1.0f - y);
42 }
43 }
44 else {
45 for (int i = 0; i < resolution; i++) {
46 float x = (i + 0.5f) * inv_resolution;
47 int index = upper_bound(cdf.begin(), cdf.end(), x) - cdf.begin() - 1;
48 float t;
49 if (index < cdf_size - 1) {
50 t = (x - cdf[index]) / (cdf[index + 1] - cdf[index]);
51 }
52 else {
53 t = 0.0f;
54 index = resolution;
55 }
56 inv_cdf[i] = from + range * (index + t) * inv_resolution;
57 }
58 }
59}
60
StackEntry * from
#define CCL_NAMESPACE_END
draw_view in_light_buf[] float
CCL_NAMESPACE_BEGIN 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