Blender V4.3
math_solvers.c
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2015 Blender Authors
2 *
3 * SPDX-License-Identifier: GPL-2.0-or-later */
4
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 <string.h>
20
21#include "BLI_strict_flags.h" /* 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, (float *)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 size_t bytes = sizeof(double) * (uint)count;
55 double *c1 = (double *)MEM_mallocN(bytes * 2, "tridiagonal_c1d1");
56 if (!c1) {
57 return false;
58 }
59 double *d1 = c1 + count;
60
61 int i;
62 double c_prev, d_prev, x_prev;
63
64 /* forward pass */
65
66 c1[0] = c_prev = ((double)c[0]) / b[0];
67 d1[0] = d_prev = ((double)d[0]) / b[0];
68
69 for (i = 1; i < count; i++) {
70 double denum = b[i] - a[i] * c_prev;
71
72 c1[i] = c_prev = c[i] / denum;
73 d1[i] = d_prev = (d[i] - a[i] * d_prev) / denum;
74 }
75
76 /* back pass */
77
78 x_prev = d_prev;
79 r_x[--i] = ((float)x_prev);
80
81 while (--i >= 0) {
82 x_prev = d1[i] - c1[i] * x_prev;
83 r_x[i] = ((float)x_prev);
84 }
85
86 MEM_freeN(c1);
87
88 return isfinite(x_prev);
89}
90
92 const float *a, const float *b, const float *c, const float *d, float *r_x, const int count)
93{
94 if (count < 1) {
95 return false;
96 }
97
98 /* Degenerate case not handled correctly by the generic formula. */
99 if (count == 1) {
100 r_x[0] = d[0] / (a[0] + b[0] + c[0]);
101
102 return isfinite(r_x[0]);
103 }
104
105 /* Degenerate case that works but can be simplified. */
106 if (count == 2) {
107 const float a2[2] = {0, a[1] + c[1]};
108 const float c2[2] = {a[0] + c[0], 0};
109
110 return BLI_tridiagonal_solve(a2, b, c2, d, r_x, count);
111 }
112
113 /* If not really cyclic, fall back to the simple solver. */
114 float a0 = a[0], cN = c[count - 1];
115
116 if (a0 == 0.0f && cN == 0.0f) {
117 return BLI_tridiagonal_solve(a, b, c, d, r_x, count);
118 }
119
120 size_t bytes = sizeof(float) * (uint)count;
121 float *tmp = (float *)MEM_mallocN(bytes * 2, "tridiagonal_ex");
122 if (!tmp) {
123 return false;
124 }
125 float *b2 = tmp + count;
126
127 /* Prepare the non-cyclic system; relies on tridiagonal_solve ignoring values. */
128 memcpy(b2, b, bytes);
129 b2[0] -= a0;
130 b2[count - 1] -= cN;
131
132 memset(tmp, 0, bytes);
133 tmp[0] = a0;
134 tmp[count - 1] = cN;
135
136 /* solve for partial solution and adjustment vector */
137 bool success = BLI_tridiagonal_solve(a, b2, c, tmp, tmp, count) &&
138 BLI_tridiagonal_solve(a, b2, c, d, r_x, count);
139
140 /* apply adjustment */
141 if (success) {
142 float coeff = (r_x[0] + r_x[count - 1]) / (1.0f + tmp[0] + tmp[count - 1]);
143
144 for (int i = 0; i < count; i++) {
145 r_x[i] -= coeff * tmp[i];
146 }
147 }
148
149 MEM_freeN(tmp);
150
151 return success;
152}
153
155 Newton3D_JacobianFunc func_jacobian,
156 Newton3D_CorrectionFunc func_correction,
157 void *userdata,
158 float epsilon,
159 int max_iterations,
160 bool trace,
161 const float x_init[3],
162 float result[3])
163{
164 float fdelta[3], fdeltav, next_fdeltav;
165 float jacobian[3][3], step[3], x[3], x_next[3];
166
167 epsilon *= epsilon;
168
169 copy_v3_v3(x, x_init);
170
171 func_delta(userdata, x, fdelta);
172 fdeltav = len_squared_v3(fdelta);
173
174 if (trace) {
175 printf("START (%g, %g, %g) %g %g\n", x[0], x[1], x[2], fdeltav, epsilon);
176 }
177
178 for (int i = 0; i == 0 || (i < max_iterations && fdeltav > epsilon); i++) {
179 /* Newton's method step. */
180 func_jacobian(userdata, x, jacobian);
181
182 if (!invert_m3(jacobian)) {
183 return false;
184 }
185
186 mul_v3_m3v3(step, jacobian, fdelta);
187 sub_v3_v3v3(x_next, x, step);
188
189 /* Custom out-of-bounds value correction. */
190 if (func_correction) {
191 if (trace) {
192 printf("%3d * (%g, %g, %g)\n", i, x_next[0], x_next[1], x_next[2]);
193 }
194
195 if (!func_correction(userdata, x, step, x_next)) {
196 return false;
197 }
198 }
199
200 func_delta(userdata, x_next, fdelta);
201 next_fdeltav = len_squared_v3(fdelta);
202
203 if (trace) {
204 printf("%3d ? (%g, %g, %g) %g\n", i, x_next[0], x_next[1], x_next[2], next_fdeltav);
205 }
206
207 /* Line search correction. */
208 while (next_fdeltav > fdeltav && next_fdeltav > epsilon) {
209 float g0 = sqrtf(fdeltav), g1 = sqrtf(next_fdeltav);
210 float g01 = -g0 / len_v3(step);
211 float det = 2.0f * (g1 - g0 - g01);
212 float l = (det == 0.0f) ? 0.1f : -g01 / det;
213 CLAMP_MIN(l, 0.1f);
214
215 mul_v3_fl(step, l);
216 sub_v3_v3v3(x_next, x, step);
217
218 func_delta(userdata, x_next, fdelta);
219 next_fdeltav = len_squared_v3(fdelta);
220
221 if (trace) {
222 printf("%3d . (%g, %g, %g) %g\n", i, x_next[0], x_next[1], x_next[2], next_fdeltav);
223 }
224 }
225
226 copy_v3_v3(x, x_next);
227 fdeltav = next_fdeltav;
228 }
229
230 bool success = (fdeltav <= epsilon);
231
232 if (trace) {
233 printf("%s (%g, %g, %g) %g\n", success ? "OK " : "FAIL", x[0], x[1], x[2], fdeltav);
234 }
235
236 copy_v3_v3(result, x);
237 return success;
238}
#define BLI_assert(a)
Definition BLI_assert.h:50
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)
typedef double(DMatrix)[4][4]
Read Guarded memory(de)allocation.
ATTR_WARN_UNUSED_RESULT const BMLoop * l
local_group_size(16, 16) .push_constant(Type b
#define printf
#define sqrtf(x)
draw_view in_light_buf[] float
bool EIG_self_adjoint_eigen_solve(const int size, const float *matrix, float *r_eigen_values, float *r_eigen_vectors)
int count
void *(* MEM_mallocN)(size_t len, const char *str)
Definition mallocn.cc:44
void MEM_freeN(void *vmemh)
Definition mallocn.cc:105
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.
void EIG_svd_square_matrix(const int size, const float *matrix, float *r_U, float *r_S, float *r_V)
Definition svd.cc:40