2023-06-14 16:52:36 +10:00
|
|
|
/* SPDX-FileCopyrightText: 2011-2022 Blender Foundation
|
|
|
|
|
*
|
|
|
|
|
* SPDX-License-Identifier: Apache-2.0 */
|
2015-10-26 19:20:40 +05:00
|
|
|
|
2021-10-24 14:19:19 +02:00
|
|
|
#include "util/math_cdf.h"
|
2015-10-26 19:20:40 +05:00
|
|
|
|
2024-12-26 17:53:56 +01:00
|
|
|
#include <cassert>
|
|
|
|
|
|
2021-10-24 14:19:19 +02:00
|
|
|
#include "util/algorithm.h"
|
2015-10-26 19:20:40 +05:00
|
|
|
|
|
|
|
|
CCL_NAMESPACE_BEGIN
|
|
|
|
|
|
|
|
|
|
/* Invert pre-calculated CDF function. */
|
|
|
|
|
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)
|
|
|
|
|
{
|
2023-01-10 02:07:00 +01:00
|
|
|
const int cdf_size = cdf.size();
|
|
|
|
|
assert(cdf[0] == 0.0f && cdf[cdf_size - 1] == 1.0f);
|
|
|
|
|
|
2015-10-26 19:20:40 +05:00
|
|
|
const float inv_resolution = 1.0f / (float)resolution;
|
|
|
|
|
const float range = to - from;
|
|
|
|
|
inv_cdf.resize(resolution);
|
|
|
|
|
if (make_symmetric) {
|
|
|
|
|
const int half_size = (resolution - 1) / 2;
|
|
|
|
|
for (int i = 0; i <= half_size; i++) {
|
2024-12-29 17:32:00 +01:00
|
|
|
const float x = i / (float)half_size;
|
2015-10-26 19:20:40 +05:00
|
|
|
int index = upper_bound(cdf.begin(), cdf.end(), x) - cdf.begin();
|
|
|
|
|
float t;
|
2023-01-10 02:07:00 +01:00
|
|
|
if (index < cdf_size - 1) {
|
2015-10-26 19:20:40 +05:00
|
|
|
t = (x - cdf[index]) / (cdf[index + 1] - cdf[index]);
|
|
|
|
|
}
|
|
|
|
|
else {
|
|
|
|
|
t = 0.0f;
|
2023-01-10 02:07:00 +01:00
|
|
|
index = cdf_size - 1;
|
2015-10-26 19:20:40 +05:00
|
|
|
}
|
2024-12-29 17:32:00 +01:00
|
|
|
const float y = ((index + t) / (resolution - 1)) * (2.0f * range);
|
2015-10-26 19:20:40 +05:00
|
|
|
inv_cdf[half_size + i] = 0.5f * (1.0f + y);
|
|
|
|
|
inv_cdf[half_size - i] = 0.5f * (1.0f - y);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
else {
|
2015-10-27 13:16:04 +05:00
|
|
|
for (int i = 0; i < resolution; i++) {
|
2024-12-29 17:32:00 +01:00
|
|
|
const float x = (i + 0.5f) * inv_resolution;
|
2023-01-09 03:05:57 +01:00
|
|
|
int index = upper_bound(cdf.begin(), cdf.end(), x) - cdf.begin() - 1;
|
2015-10-26 19:20:40 +05:00
|
|
|
float t;
|
2023-01-10 02:07:00 +01:00
|
|
|
if (index < cdf_size - 1) {
|
2015-10-26 19:20:40 +05:00
|
|
|
t = (x - cdf[index]) / (cdf[index + 1] - cdf[index]);
|
|
|
|
|
}
|
|
|
|
|
else {
|
|
|
|
|
t = 0.0f;
|
|
|
|
|
index = resolution;
|
|
|
|
|
}
|
2023-01-09 03:05:57 +01:00
|
|
|
inv_cdf[i] = from + range * (index + t) * inv_resolution;
|
2015-10-26 19:20:40 +05:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
CCL_NAMESPACE_END
|