Blender V5.0
math_solvers.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_matrix.h"
11#include "BLI_math_solvers.h"
12#include "BLI_math_vector.h"
13#include "MEM_guardedalloc.h"
14
15#include "BLI_utildefines.h"
16
17#include "eigen_capi.h"
18
19#include <cstring>
20
21#include "BLI_strict_flags.h" /* IWYU pragma: keep. Keep last. */
22
23/********************************** Eigen Solvers *********************************/
24
25bool BLI_eigen_solve_selfadjoint_m3(const float m3[3][3],
26 float r_eigen_values[3],
27 float r_eigen_vectors[3][3])
28{
29#ifndef NDEBUG
30 /* We must assert given matrix is self-adjoint (i.e. symmetric) */
31 if ((m3[0][1] != m3[1][0]) || (m3[0][2] != m3[2][0]) || (m3[1][2] != m3[2][1])) {
32 BLI_assert(0);
33 }
34#endif
35
37 3, (const float *)m3, r_eigen_values, (float *)r_eigen_vectors);
38}
39
40void BLI_svd_m3(const float m3[3][3], float r_U[3][3], float r_S[3], float r_V[3][3])
41{
42 EIG_svd_square_matrix(3, (const float *)m3, (float *)r_U, r_S, (float *)r_V);
43}
44
45/***************************** Simple Solvers ************************************/
46
48 const float *a, const float *b, const float *c, const float *d, float *r_x, const int count)
49{
50 if (count < 1) {
51 return false;
52 }
53
54 double *c1 = MEM_malloc_arrayN<double>(size_t(count) * 2, "tridiagonal_c1d1");
55 if (!c1) {
56 return false;
57 }
58 double *d1 = c1 + count;
59
60 int i;
61 double c_prev, d_prev, x_prev;
62
63 /* forward pass */
64
65 c1[0] = c_prev = double(c[0]) / b[0];
66 d1[0] = d_prev = double(d[0]) / b[0];
67
68 for (i = 1; i < count; i++) {
69 double denum = b[i] - a[i] * c_prev;
70
71 c1[i] = c_prev = c[i] / denum;
72 d1[i] = d_prev = (d[i] - a[i] * d_prev) / denum;
73 }
74
75 /* back pass */
76
77 x_prev = d_prev;
78 r_x[--i] = float(x_prev);
79
80 while (--i >= 0) {
81 x_prev = d1[i] - c1[i] * x_prev;
82 r_x[i] = float(x_prev);
83 }
84
85 MEM_freeN(c1);
86
87 return isfinite(x_prev);
88}
89
91 const float *a, const float *b, const float *c, const float *d, float *r_x, const int count)
92{
93 if (count < 1) {
94 return false;
95 }
96
97 /* Degenerate case not handled correctly by the generic formula. */
98 if (count == 1) {
99 r_x[0] = d[0] / (a[0] + b[0] + c[0]);
100
101 return isfinite(r_x[0]);
102 }
103
104 /* Degenerate case that works but can be simplified. */
105 if (count == 2) {
106 const float a2[2] = {0, a[1] + c[1]};
107 const float c2[2] = {a[0] + c[0], 0};
108
109 return BLI_tridiagonal_solve(a2, b, c2, d, r_x, count);
110 }
111
112 /* If not really cyclic, fall back to the simple solver. */
113 float a0 = a[0], cN = c[count - 1];
114
115 if (a0 == 0.0f && cN == 0.0f) {
116 return BLI_tridiagonal_solve(a, b, c, d, r_x, count);
117 }
118
119 size_t bytes = sizeof(float) * uint(count);
120 float *tmp = MEM_malloc_arrayN<float>(size_t(count) * 2, "tridiagonal_ex");
121 if (!tmp) {
122 return false;
123 }
124 float *b2 = tmp + count;
125
126 /* Prepare the non-cyclic system; relies on tridiagonal_solve ignoring values. */
127 memcpy(b2, b, bytes);
128 b2[0] -= a0;
129 b2[count - 1] -= cN;
130
131 memset(tmp, 0, bytes);
132 tmp[0] = a0;
133 tmp[count - 1] = cN;
134
135 /* solve for partial solution and adjustment vector */
136 bool success = BLI_tridiagonal_solve(a, b2, c, tmp, tmp, count) &&
137 BLI_tridiagonal_solve(a, b2, c, d, r_x, count);
138
139 /* apply adjustment */
140 if (success) {
141 float coeff = (r_x[0] + r_x[count - 1]) / (1.0f + tmp[0] + tmp[count - 1]);
142
143 for (int i = 0; i < count; i++) {
144 r_x[i] -= coeff * tmp[i];
145 }
146 }
147
148 MEM_freeN(tmp);
149
150 return success;
151}
152
154 Newton3D_JacobianFunc func_jacobian,
155 Newton3D_CorrectionFunc func_correction,
156 void *userdata,
157 float epsilon,
158 int max_iterations,
159 bool trace,
160 const float x_init[3],
161 float result[3])
162{
163 float fdelta[3], fdeltav, next_fdeltav;
164 float jacobian[3][3], step[3], x[3], x_next[3];
165
166 epsilon *= epsilon;
167
168 copy_v3_v3(x, x_init);
169
170 func_delta(userdata, x, fdelta);
171 fdeltav = len_squared_v3(fdelta);
172
173 if (trace) {
174 printf("START (%g, %g, %g) %g %g\n", x[0], x[1], x[2], fdeltav, epsilon);
175 }
176
177 for (int i = 0; i == 0 || (i < max_iterations && fdeltav > epsilon); i++) {
178 /* Newton's method step. */
179 func_jacobian(userdata, x, jacobian);
180
181 if (!invert_m3(jacobian)) {
182 return false;
183 }
184
185 mul_v3_m3v3(step, jacobian, fdelta);
186 sub_v3_v3v3(x_next, x, step);
187
188 /* Custom out-of-bounds value correction. */
189 if (func_correction) {
190 if (trace) {
191 printf("%3d * (%g, %g, %g)\n", i, x_next[0], x_next[1], x_next[2]);
192 }
193
194 if (!func_correction(userdata, x, step, x_next)) {
195 return false;
196 }
197 }
198
199 func_delta(userdata, x_next, fdelta);
200 next_fdeltav = len_squared_v3(fdelta);
201
202 if (trace) {
203 printf("%3d ? (%g, %g, %g) %g\n", i, x_next[0], x_next[1], x_next[2], next_fdeltav);
204 }
205
206 /* Line search correction. */
207 while (next_fdeltav > fdeltav && next_fdeltav > epsilon) {
208 float g0 = sqrtf(fdeltav), g1 = sqrtf(next_fdeltav);
209 float g01 = -g0 / len_v3(step);
210 float det = 2.0f * (g1 - g0 - g01);
211 float l = (det == 0.0f) ? 0.1f : -g01 / det;
212 CLAMP_MIN(l, 0.1f);
213
214 mul_v3_fl(step, l);
215 sub_v3_v3v3(x_next, x, step);
216
217 func_delta(userdata, x_next, fdelta);
218 next_fdeltav = len_squared_v3(fdelta);
219
220 if (trace) {
221 printf("%3d . (%g, %g, %g) %g\n", i, x_next[0], x_next[1], x_next[2], next_fdeltav);
222 }
223 }
224
225 copy_v3_v3(x, x_next);
226 fdeltav = next_fdeltav;
227 }
228
229 bool success = (fdeltav <= epsilon);
230
231 if (trace) {
232 printf("%s (%g, %g, %g) %g\n", success ? "OK " : "FAIL", x[0], x[1], x[2], fdeltav);
233 }
234
236 return success;
237}
#define BLI_assert(a)
Definition BLI_assert.h:46
void mul_v3_m3v3(float r[3], const float M[3][3], const float a[3])
bool invert_m3(float mat[3][3])
bool(* Newton3D_CorrectionFunc)(void *userdata, const float x[3], float step[3], float x_next[3])
void(* Newton3D_JacobianFunc)(void *userdata, const float x[3], float r_jacobian[3][3])
void(* Newton3D_DeltaFunc)(void *userdata, const float x[3], float r_delta[3])
MINLINE float len_squared_v3(const float v[3]) ATTR_WARN_UNUSED_RESULT
MINLINE void sub_v3_v3v3(float r[3], const float a[3], const float b[3])
MINLINE void mul_v3_fl(float r[3], float f)
MINLINE void copy_v3_v3(float r[3], const float a[3])
MINLINE float len_v3(const float a[3]) ATTR_WARN_UNUSED_RESULT
unsigned int uint
#define CLAMP_MIN(a, b)
Read Guarded memory(de)allocation.
ATTR_WARN_UNUSED_RESULT const BMLoop * l
nullptr float
bool EIG_self_adjoint_eigen_solve(const int size, const float *matrix, float *r_eigen_values, float *r_eigen_vectors)
#define printf(...)
VecBase< float, D > step(VecOp< float, D >, VecOp< float, D >) RET
int count
void * MEM_malloc_arrayN(size_t len, size_t size, const char *str)
Definition mallocn.cc:133
void MEM_freeN(void *vmemh)
Definition mallocn.cc:113
bool BLI_newton3d_solve(Newton3D_DeltaFunc func_delta, Newton3D_JacobianFunc func_jacobian, Newton3D_CorrectionFunc func_correction, void *userdata, float epsilon, int max_iterations, bool trace, const float x_init[3], float result[3])
Solve a generic f(x) = 0 equation using Newton's method.
void BLI_svd_m3(const float m3[3][3], float r_U[3][3], float r_S[3], float r_V[3][3])
Compute the SVD (Singular Values Decomposition) of given 3D matrix (m3 = USV*).
bool BLI_tridiagonal_solve(const float *a, const float *b, const float *c, const float *d, float *r_x, const int count)
Solve a tridiagonal system of equations:
bool BLI_tridiagonal_solve_cyclic(const float *a, const float *b, const float *c, const float *d, float *r_x, const int count)
Solve a possibly cyclic tridiagonal system using the Sherman-Morrison formula.
bool BLI_eigen_solve_selfadjoint_m3(const float m3[3][3], float r_eigen_values[3], float r_eigen_vectors[3][3])
Compute the eigen values and/or vectors of given 3D symmetric (aka adjoint) matrix.
#define sqrtf
void EIG_svd_square_matrix(const int size, const float *matrix, float *r_U, float *r_S, float *r_V)
Definition svd.cc:40
i
Definition text_draw.cc:230