Blender V5.0
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.h"
10#include "BLI_math_statistics.h"
11#include "BLI_math_vector.h"
12
13#include "BLI_task.h"
14
15#include "BLI_strict_flags.h" /* IWYU pragma: keep. Keep last. */
16
17/********************************** Covariance Matrices *********************************/
18
20 const float *cos_vn;
21 const float *center;
22 float *r_covmat;
23 float covfac;
24 int n;
26};
27
28static void covariance_m_vn_ex_task_cb(void *__restrict userdata,
29 const int a,
30 const TaskParallelTLS *__restrict /*tls*/)
31{
32 CovarianceData *data = static_cast<CovarianceData *>(userdata);
33 const float *cos_vn = data->cos_vn;
34 const float *center = data->center;
35 float *r_covmat = data->r_covmat;
36 const int n = data->n;
37 const int cos_vn_num = data->cos_vn_num;
38
39 int k;
40
41 /* Covariance matrices are always symmetrical, so we can compute only one half of it,
42 * and mirror it to the other half (at the end of the func).
43 *
44 * This allows using a flat loop of n*n with same results as imbricated one over half the matrix:
45 *
46 * for (i = 0; i < n; i++) {
47 * for (j = i; j < n; j++) {
48 * ...
49 * }
50 * }
51 */
52 const int i = a / n;
53 const int j = a % n;
54 if (j < i) {
55 return;
56 }
57
58 if (center) {
59 for (k = 0; k < cos_vn_num; k++) {
60 r_covmat[a] += (cos_vn[k * n + i] - center[i]) * (cos_vn[k * n + j] - center[j]);
61 }
62 }
63 else {
64 for (k = 0; k < cos_vn_num; k++) {
65 r_covmat[a] += cos_vn[k * n + i] * cos_vn[k * n + j];
66 }
67 }
68 r_covmat[a] *= data->covfac;
69 if (j != i) {
70 /* Mirror result to other half... */
71 r_covmat[j * n + i] = r_covmat[a];
72 }
73}
74
75void BLI_covariance_m_vn_ex(const int n,
76 const float *cos_vn,
77 const int cos_vn_num,
78 const float *center,
79 const bool use_sample_correction,
80 float *r_covmat)
81{
82 /* Note about that division: see https://en.wikipedia.org/wiki/Bessel%27s_correction.
83 * In a nutshell, it must be 1 / (n - 1) for 'sample data', and 1 / n for 'population data'...
84 */
85 const float covfac = 1.0f / float(use_sample_correction ? cos_vn_num - 1 : cos_vn_num);
86
87 memset(r_covmat, 0, sizeof(*r_covmat) * size_t(n * n));
88
90 data.cos_vn = cos_vn;
91 data.center = center;
92 data.r_covmat = r_covmat;
93 data.covfac = covfac;
94 data.n = n;
95 data.cos_vn_num = cos_vn_num;
96
97 TaskParallelSettings settings;
99 settings.use_threading = ((cos_vn_num * n * n) >= 10000);
101}
102
103void BLI_covariance_m3_v3n(const float (*cos_v3)[3],
104 const int cos_v3_num,
105 const bool use_sample_correction,
106 float r_covmat[3][3],
107 float r_center[3])
108{
109 float center[3];
110 const float mean_fac = 1.0f / float(cos_v3_num);
111 int i;
112
113 zero_v3(center);
114 for (i = 0; i < cos_v3_num; i++) {
115 /* Applying mean_fac here rather than once at the end reduce compute errors... */
116 madd_v3_v3fl(center, cos_v3[i], mean_fac);
117 }
118
119 if (r_center) {
120 copy_v3_v3(r_center, center);
121 }
122
124 3, (const float *)cos_v3, cos_v3_num, center, use_sample_correction, (float *)r_covmat);
125}
MINLINE void madd_v3_v3fl(float r[3], const float a[3], float f)
MINLINE void copy_v3_v3(float r[3], const float a[3])
MINLINE void zero_v3(float r[3])
void BLI_task_parallel_range(int start, int stop, void *userdata, TaskParallelRangeFunc func, const TaskParallelSettings *settings)
Definition task_range.cc:99
BLI_INLINE void BLI_parallel_range_settings_defaults(TaskParallelSettings *settings)
Definition BLI_task.h:221
BMesh const char void * data
nullptr float
static void covariance_m_vn_ex_task_cb(void *__restrict userdata, const int a, const TaskParallelTLS *__restrict)
void BLI_covariance_m3_v3n(const float(*cos_v3)[3], const int cos_v3_num, const 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_covariance_m_vn_ex(const int n, const float *cos_vn, const int cos_vn_num, const float *center, const bool use_sample_correction, float *r_covmat)
Compute the covariance matrix of given set of nD coordinates.
const float * cos_vn
const float * center
i
Definition text_draw.cc:230