Blender V5.3
math_statistics.cc
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2015 Blender Authors
2 *
3 * SPDX-License-Identifier: GPL-2.0-or-later */
4
8
9#include "BLI_math_base_c.hh"
11#include "BLI_math_vector_c.hh"
12
13#include "BLI_task_c.hh"
14
15#include "BLI_strict_flags.hh" /* IWYU pragma: keep. Keep last. */
16
17namespace blender {
18
19/* -------------------------------------------------------------------- */
22
24 const float *cos_vn;
25 const float *center;
26 float *r_covmat;
27 float covfac;
28 int n;
30};
31
32static void covariance_m_vn_ex_task_cb(void *__restrict userdata,
33 const int a,
34 const TaskParallelTLS *__restrict /*tls*/)
35{
36 CovarianceData *data = static_cast<CovarianceData *>(userdata);
37 const float *cos_vn = data->cos_vn;
38 const float *center = data->center;
39 float *r_covmat = data->r_covmat;
40 const int n = data->n;
41 const int cos_vn_num = data->cos_vn_num;
42
43 int k;
44
45 /* Covariance matrices are always symmetrical, so we can compute only one half of it,
46 * and mirror it to the other half (at the end of the func).
47 *
48 * This allows using a flat loop of n*n with same results as imbricated one over half the matrix:
49 *
50 * for (i = 0; i < n; i++) {
51 * for (j = i; j < n; j++) {
52 * ...
53 * }
54 * }
55 */
56 const int i = a / n;
57 const int j = a % n;
58 if (j < i) {
59 return;
60 }
61
62 if (center) {
63 for (k = 0; k < cos_vn_num; k++) {
64 r_covmat[a] += (cos_vn[k * n + i] - center[i]) * (cos_vn[k * n + j] - center[j]);
65 }
66 }
67 else {
68 for (k = 0; k < cos_vn_num; k++) {
69 r_covmat[a] += cos_vn[k * n + i] * cos_vn[k * n + j];
70 }
71 }
72 r_covmat[a] *= data->covfac;
73 if (j != i) {
74 /* Mirror result to other half... */
75 r_covmat[j * n + i] = r_covmat[a];
76 }
77}
78
79void BLI_covariance_m_vn_ex(const int n,
80 const float *cos_vn,
81 const int cos_vn_num,
82 const float *center,
83 const bool use_sample_correction,
84 float *r_covmat)
85{
86 /* Note about that division: see https://en.wikipedia.org/wiki/Bessel%27s_correction.
87 * In a nutshell, it must be 1 / (n - 1) for 'sample data', and 1 / n for 'population data'...
88 */
89 const float covfac = 1.0f / float(use_sample_correction ? cos_vn_num - 1 : cos_vn_num);
90
91 memset(r_covmat, 0, sizeof(*r_covmat) * size_t(n * n));
92
94 data.cos_vn = cos_vn;
95 data.center = center;
96 data.r_covmat = r_covmat;
97 data.covfac = covfac;
98 data.n = n;
99 data.cos_vn_num = cos_vn_num;
100
101 TaskParallelSettings settings;
103 settings.use_threading = ((cos_vn_num * n * n) >= 10000);
105}
106
107void BLI_covariance_m3_v3n(const float (*cos_v3)[3],
108 const int cos_v3_num,
109 const bool use_sample_correction,
110 float r_covmat[3][3],
111 float r_center[3])
112{
113 float center[3];
114 const float mean_fac = 1.0f / float(cos_v3_num);
115 int i;
116
117 zero_v3(center);
118 for (i = 0; i < cos_v3_num; i++) {
119 /* Applying mean_fac here rather than once at the end reduce compute errors... */
120 madd_v3_v3fl(center, cos_v3[i], mean_fac);
121 }
122
123 if (r_center) {
124 copy_v3_v3(r_center, center);
125 }
126
128 reinterpret_cast<const float *>(cos_v3),
129 cos_v3_num,
130 center,
131 use_sample_correction,
132 reinterpret_cast<float *>(r_covmat));
133}
134
136
137} // namespace blender
BMesh Iterators.
BLI_INLINE void BLI_parallel_range_settings_defaults(TaskParallelSettings *settings)
MINLINE void copy_v3_v3(float r[3], const float a[3])
void BLI_covariance_m3_v3n(const float(*cos_v3)[3], int cos_v3_num, bool use_sample_correction, float r_covmat[3][3], float r_center[3])
Compute the covariance matrix of given set of 3D coordinates.
void BLI_task_parallel_range(int start, int stop, void *userdata, TaskParallelRangeFunc func, const TaskParallelSettings *settings)
MINLINE void madd_v3_v3fl(float r[3], const float a[3], float f)
static void covariance_m_vn_ex_task_cb(void *__restrict userdata, const int a, const TaskParallelTLS *__restrict)
BMesh const char void * data
void BLI_covariance_m_vn_ex(int n, const float *cos_vn, int cos_vn_num, const float *center, bool use_sample_correction, float *r_covmat)
Compute the covariance matrix of given set of nD coordinates.
MINLINE void zero_v3(float r[3])
KERNEL_FEATURE_LIGHT_PASSES KERNEL_FEATURE_PATH_GUIDING float