Blender V5.0
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 <cassert>
8
9#include "util/algorithm.h"
10
12
13/* Invert pre-calculated CDF function. */
14void util_cdf_invert(const int resolution,
15 const float from,
16 const float to,
17 const vector<float> &cdf,
18 const bool make_symmetric,
19 vector<float> &inv_cdf)
20{
21 const int cdf_size = cdf.size();
22 assert(cdf[0] == 0.0f && cdf[cdf_size - 1] == 1.0f);
23
24 const float inv_resolution = 1.0f / (float)resolution;
25 const float range = to - from;
26 inv_cdf.resize(resolution);
27 if (make_symmetric) {
28 const int half_size = (resolution - 1) / 2;
29 for (int i = 0; i <= half_size; i++) {
30 const float x = i / (float)half_size;
31 int index = upper_bound(cdf.begin(), cdf.end(), x) - cdf.begin();
32 float t;
33 if (index < cdf_size - 1) {
34 t = (x - cdf[index]) / (cdf[index + 1] - cdf[index]);
35 }
36 else {
37 t = 0.0f;
38 index = cdf_size - 1;
39 }
40 const float y = ((index + t) / (resolution - 1)) * (2.0f * range);
41 inv_cdf[half_size + i] = 0.5f * (1.0f + y);
42 inv_cdf[half_size - i] = 0.5f * (1.0f - y);
43 }
44 }
45 else {
46 for (int i = 0; i < resolution; i++) {
47 const float x = (i + 0.5f) * inv_resolution;
48 int index = upper_bound(cdf.begin(), cdf.end(), x) - cdf.begin() - 1;
49 float t;
50 if (index < cdf_size - 1) {
51 t = (x - cdf[index]) / (cdf[index + 1] - cdf[index]);
52 }
53 else {
54 t = 0.0f;
55 index = resolution;
56 }
57 inv_cdf[i] = from + range * (index + t) * inv_resolution;
58 }
59 }
60}
61
nullptr float
#define CCL_NAMESPACE_END
#define assert(assertion)
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:14
i
Definition text_draw.cc:230