Blender  V2.93
math_solvers.c
Go to the documentation of this file.
1 /*
2  * This program is free software; you can redistribute it and/or
3  * modify it under the terms of the GNU General Public License
4  * as published by the Free Software Foundation; either version 2
5  * of the License, or (at your option) any later version.
6  *
7  * This program is distributed in the hope that it will be useful,
8  * but WITHOUT ANY WARRANTY; without even the implied warranty of
9  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10  * GNU General Public License for more details.
11  *
12  * You should have received a copy of the GNU General Public License
13  * along with this program; if not, write to the Free Software Foundation,
14  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
15  *
16  * The Original Code is Copyright (C) 2015 by Blender Foundation.
17  * All rights reserved.
18  */
19 
24 #include "MEM_guardedalloc.h"
25 
26 #include "BLI_math.h"
27 #include "BLI_utildefines.h"
28 
29 #include "BLI_strict_flags.h"
30 
31 #include "eigen_capi.h"
32 
33 /********************************** Eigen Solvers *********************************/
34 
42 bool BLI_eigen_solve_selfadjoint_m3(const float m3[3][3],
43  float r_eigen_values[3],
44  float r_eigen_vectors[3][3])
45 {
46 #ifndef NDEBUG
47  /* We must assert given matrix is self-adjoint (i.e. symmetric) */
48  if ((m3[0][1] != m3[1][0]) || (m3[0][2] != m3[2][0]) || (m3[1][2] != m3[2][1])) {
49  BLI_assert(0);
50  }
51 #endif
52 
54  3, (const float *)m3, r_eigen_values, (float *)r_eigen_vectors);
55 }
56 
65 void BLI_svd_m3(const float m3[3][3], float r_U[3][3], float r_S[3], float r_V[3][3])
66 {
67  EIG_svd_square_matrix(3, (const float *)m3, (float *)r_U, (float *)r_S, (float *)r_V);
68 }
69 
70 /***************************** Simple Solvers ************************************/
71 
83  const float *a, const float *b, const float *c, const float *d, float *r_x, const int count)
84 {
85  if (count < 1) {
86  return false;
87  }
88 
89  size_t bytes = sizeof(double) * (unsigned)count;
90  double *c1 = (double *)MEM_mallocN(bytes * 2, "tridiagonal_c1d1");
91  double *d1 = c1 + count;
92 
93  if (!c1) {
94  return false;
95  }
96 
97  int i;
98  double c_prev, d_prev, x_prev;
99 
100  /* forward pass */
101 
102  c1[0] = c_prev = ((double)c[0]) / b[0];
103  d1[0] = d_prev = ((double)d[0]) / b[0];
104 
105  for (i = 1; i < count; i++) {
106  double denum = b[i] - a[i] * c_prev;
107 
108  c1[i] = c_prev = c[i] / denum;
109  d1[i] = d_prev = (d[i] - a[i] * d_prev) / denum;
110  }
111 
112  /* back pass */
113 
114  x_prev = d_prev;
115  r_x[--i] = ((float)x_prev);
116 
117  while (--i >= 0) {
118  x_prev = d1[i] - c1[i] * x_prev;
119  r_x[i] = ((float)x_prev);
120  }
121 
122  MEM_freeN(c1);
123 
124  return isfinite(x_prev);
125 }
126 
134  const float *a, const float *b, const float *c, const float *d, float *r_x, const int count)
135 {
136  if (count < 1) {
137  return false;
138  }
139 
140  /* Degenerate case not handled correctly by the generic formula. */
141  if (count == 1) {
142  r_x[0] = d[0] / (a[0] + b[0] + c[0]);
143 
144  return isfinite(r_x[0]);
145  }
146 
147  /* Degenerate case that works but can be simplified. */
148  if (count == 2) {
149  float a2[2] = {0, a[1] + c[1]};
150  float c2[2] = {a[0] + c[0], 0};
151 
152  return BLI_tridiagonal_solve(a2, b, c2, d, r_x, count);
153  }
154 
155  /* If not really cyclic, fall back to the simple solver. */
156  float a0 = a[0], cN = c[count - 1];
157 
158  if (a0 == 0.0f && cN == 0.0f) {
159  return BLI_tridiagonal_solve(a, b, c, d, r_x, count);
160  }
161 
162  size_t bytes = sizeof(float) * (unsigned)count;
163  float *tmp = (float *)MEM_mallocN(bytes * 2, "tridiagonal_ex");
164  float *b2 = tmp + count;
165 
166  if (!tmp) {
167  return false;
168  }
169 
170  /* Prepare the non-cyclic system; relies on tridiagonal_solve ignoring values. */
171  memcpy(b2, b, bytes);
172  b2[0] -= a0;
173  b2[count - 1] -= cN;
174 
175  memset(tmp, 0, bytes);
176  tmp[0] = a0;
177  tmp[count - 1] = cN;
178 
179  /* solve for partial solution and adjustment vector */
180  bool success = BLI_tridiagonal_solve(a, b2, c, tmp, tmp, count) &&
181  BLI_tridiagonal_solve(a, b2, c, d, r_x, count);
182 
183  /* apply adjustment */
184  if (success) {
185  float coeff = (r_x[0] + r_x[count - 1]) / (1.0f + tmp[0] + tmp[count - 1]);
186 
187  for (int i = 0; i < count; i++) {
188  r_x[i] -= coeff * tmp[i];
189  }
190  }
191 
192  MEM_freeN(tmp);
193 
194  return success;
195 }
196 
213  Newton3D_JacobianFunc func_jacobian,
214  Newton3D_CorrectionFunc func_correction,
215  void *userdata,
216  float epsilon,
217  int max_iterations,
218  bool trace,
219  const float x_init[3],
220  float result[3])
221 {
222  float fdelta[3], fdeltav, next_fdeltav;
223  float jacobian[3][3], step[3], x[3], x_next[3];
224 
225  epsilon *= epsilon;
226 
227  copy_v3_v3(x, x_init);
228 
229  func_delta(userdata, x, fdelta);
230  fdeltav = len_squared_v3(fdelta);
231 
232  if (trace) {
233  printf("START (%g, %g, %g) %g %g\n", x[0], x[1], x[2], fdeltav, epsilon);
234  }
235 
236  for (int i = 0; i == 0 || (i < max_iterations && fdeltav > epsilon); i++) {
237  /* Newton's method step. */
238  func_jacobian(userdata, x, jacobian);
239 
240  if (!invert_m3(jacobian)) {
241  return false;
242  }
243 
244  mul_v3_m3v3(step, jacobian, fdelta);
245  sub_v3_v3v3(x_next, x, step);
246 
247  /* Custom out-of-bounds value correction. */
248  if (func_correction) {
249  if (trace) {
250  printf("%3d * (%g, %g, %g)\n", i, x_next[0], x_next[1], x_next[2]);
251  }
252 
253  if (!func_correction(userdata, x, step, x_next)) {
254  return false;
255  }
256  }
257 
258  func_delta(userdata, x_next, fdelta);
259  next_fdeltav = len_squared_v3(fdelta);
260 
261  if (trace) {
262  printf("%3d ? (%g, %g, %g) %g\n", i, x_next[0], x_next[1], x_next[2], next_fdeltav);
263  }
264 
265  /* Line search correction. */
266  while (next_fdeltav > fdeltav && next_fdeltav > epsilon) {
267  float g0 = sqrtf(fdeltav), g1 = sqrtf(next_fdeltav);
268  float g01 = -g0 / len_v3(step);
269  float det = 2.0f * (g1 - g0 - g01);
270  float l = (det == 0.0f) ? 0.1f : -g01 / det;
271  CLAMP_MIN(l, 0.1f);
272 
273  mul_v3_fl(step, l);
274  sub_v3_v3v3(x_next, x, step);
275 
276  func_delta(userdata, x_next, fdelta);
277  next_fdeltav = len_squared_v3(fdelta);
278 
279  if (trace) {
280  printf("%3d . (%g, %g, %g) %g\n", i, x_next[0], x_next[1], x_next[2], next_fdeltav);
281  }
282  }
283 
284  copy_v3_v3(x, x_next);
285  fdeltav = next_fdeltav;
286  }
287 
288  bool success = (fdeltav <= epsilon);
289 
290  if (trace) {
291  printf("%s (%g, %g, %g) %g\n", success ? "OK " : "FAIL", x[0], x[1], x[2], fdeltav);
292  }
293 
294  copy_v3_v3(result, x);
295  return success;
296 }
typedef float(TangentPoint)[2]
#define BLI_assert(a)
Definition: BLI_assert.h:58
bool invert_m3(float R[3][3])
Definition: math_matrix.c:1152
void mul_v3_m3v3(float r[3], const float M[3][3], const float a[3])
Definition: math_matrix.c:901
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
Strict compiler flags for areas of code we want to ensure don't do conversions without us knowing abo...
#define CLAMP_MIN(a, b)
typedef double(DMatrix)[4][4]
Read Guarded memory(de)allocation.
ATTR_WARN_UNUSED_RESULT const BMLoop * l
bool EIG_self_adjoint_eigen_solve(const int size, const float *matrix, float *r_eigen_values, float *r_eigen_vectors)
Definition: eigenvalues.cc:41
int count
#define sqrtf(x)
void(* MEM_freeN)(void *vmemh)
Definition: mallocn.c:41
void *(* MEM_mallocN)(size_t len, const char *str)
Definition: mallocn.c:47
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.
Definition: math_solvers.c:212
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*).
Definition: math_solvers.c:65
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:
Definition: math_solvers.c:82
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.
Definition: math_solvers.c:133
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.
Definition: math_solvers.c:42
bool isfinite(uchar)
Definition: image.cpp:44
static unsigned c
Definition: RandGen.cpp:97
static unsigned a[3]
Definition: RandGen.cpp:92
static double epsilon
void EIG_svd_square_matrix(const int size, const float *matrix, float *r_U, float *r_S, float *r_V)
Definition: svd.cc:50