Blender V5.0
implicit_blender.cc
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: Blender Authors
2 *
3 * SPDX-License-Identifier: GPL-2.0-or-later */
4
8
9#include "implicit.h"
10
11#ifdef IMPLICIT_SOLVER_BLENDER
12
13# include "MEM_guardedalloc.h"
14
15# include "BLI_math_geom.h"
16# include "BLI_math_matrix.h"
17# include "BLI_math_vector.h"
18# include "BLI_task.hh"
19
20# include "BKE_cloth.hh"
21
22# include "SIM_mass_spring.h"
23
24# ifdef __GNUC__
25# pragma GCC diagnostic ignored "-Wtype-limits"
26# endif
27
28# define CLOTH_PARALLEL_LIMIT 1024
29
30// #define DEBUG_TIME
31
32# ifdef DEBUG_TIME
33# include "BLI_time.h"
34# endif
35
36static float I[3][3] = {{1, 0, 0}, {0, 1, 0}, {0, 0, 1}};
37static float ZERO[3][3] = {{0, 0, 0}, {0, 0, 0}, {0, 0, 0}};
38
39# if 0
40# define C99
41# ifdef C99
42# defineDO_INLINE inline
43# else
44# defineDO_INLINE static
45# endif
46# endif /* if 0 */
47
49/* fast vector / matrix library, enhancements are welcome :) -dg */
51
52/* DEFINITIONS */
53using lfVector = float[3];
54struct fmatrix3x3 {
55 float m[3][3]; /* 3x3 matrix */
56 uint c, r; /* column and row number */
57 // int pinned; /* is this vertex allowed to move? */
58 float n1, n2, n3; /* three normal vectors for collision constrains */
59 uint vcount; /* vertex count */
60 uint scount; /* spring count */
61};
62
64/* float[3] vector */
66/* simple vector code */
67/* STATUS: verified */
68DO_INLINE void mul_fvector_S(float to[3], const float from[3], float scalar)
69{
70 to[0] = from[0] * scalar;
71 to[1] = from[1] * scalar;
72 to[2] = from[2] * scalar;
73}
74/* simple v^T * v product ("outer product") */
75/* STATUS: HAS TO BE verified (*should* work) */
76DO_INLINE void mul_fvectorT_fvector(float to[3][3], const float vectorA[3], const float vectorB[3])
77{
78 mul_fvector_S(to[0], vectorB, vectorA[0]);
79 mul_fvector_S(to[1], vectorB, vectorA[1]);
80 mul_fvector_S(to[2], vectorB, vectorA[2]);
81}
82/* simple v^T * v product with scalar ("outer product") */
83/* STATUS: HAS TO BE verified (*should* work) */
84DO_INLINE void mul_fvectorT_fvectorS(float to[3][3], float vectorA[3], float vectorB[3], float aS)
85{
86 mul_fvectorT_fvector(to, vectorA, vectorB);
87
88 mul_fvector_S(to[0], to[0], aS);
89 mul_fvector_S(to[1], to[1], aS);
90 mul_fvector_S(to[2], to[2], aS);
91}
92
93# if 0
94/* printf vector[3] on console: for debug output */
95static void print_fvector(float m3[3])
96{
97 printf("%f\n%f\n%f\n\n", m3[0], m3[1], m3[2]);
98}
99
101/* long float vector float (*)[3] */
103/* print long vector on console: for debug output */
104DO_INLINE void print_lfvector(float (*fLongVector)[3], uint verts)
105{
106 uint i = 0;
107 for (i = 0; i < verts; i++) {
108 print_fvector(fLongVector[i]);
109 }
110}
111# endif
112
113/* create long vector */
115{
116 /* TODO: check if memory allocation was successful */
117 return MEM_calloc_arrayN<lfVector>(verts, "cloth_implicit_alloc_vector");
118 // return (lfVector *)cloth_aligned_malloc(&MEMORY_BASE, verts * sizeof(lfVector));
119}
120/* delete long vector */
121DO_INLINE void del_lfvector(float (*fLongVector)[3])
122{
123 if (fLongVector != nullptr) {
124 MEM_freeN(fLongVector);
125 // cloth_aligned_free(&MEMORY_BASE, fLongVector);
126 }
127}
128/* copy long vector */
129DO_INLINE void cp_lfvector(float (*to)[3], float (*from)[3], uint verts)
130{
131 memcpy(to, from, verts * sizeof(lfVector));
132}
133/* init long vector with float[3] */
134DO_INLINE void init_lfvector(float (*fLongVector)[3], const float vector[3], uint verts)
135{
136 uint i = 0;
137 for (i = 0; i < verts; i++) {
138 copy_v3_v3(fLongVector[i], vector);
139 }
140}
141/* zero long vector with float[3] */
142DO_INLINE void zero_lfvector(float (*to)[3], uint verts)
143{
144 memset(to, 0.0f, verts * sizeof(lfVector));
145}
146/* Multiply long vector with scalar. */
147DO_INLINE void mul_lfvectorS(float (*to)[3], float (*fLongVector)[3], float scalar, uint verts)
148{
149 uint i = 0;
150
151 for (i = 0; i < verts; i++) {
152 mul_fvector_S(to[i], fLongVector[i], scalar);
153 }
154}
155/* Multiply long vector with scalar.
156 * `A -= B * float` */
157DO_INLINE void submul_lfvectorS(float (*to)[3], float (*fLongVector)[3], float scalar, uint verts)
158{
159 uint i = 0;
160 for (i = 0; i < verts; i++) {
161 VECSUBMUL(to[i], fLongVector[i], scalar);
162 }
163}
164/* dot product for big vector */
165DO_INLINE float dot_lfvector(float (*fLongVectorA)[3], float (*fLongVectorB)[3], uint verts)
166{
167# if 0
168 /* TODO: try enabling this and measuring performance. It was previously disabled
169 * due to non-deterministic behavior, but parallel_deterministic_reduce should
170 * give consistent results. */
174 0.0,
175 [=](const blender::IndexRange &range, float value) {
176 float temp = value;
177 for (const int i : range) {
178 temp += dot_v3v3(fLongVectorA[i], fLongVectorB[i]);
179 }
180 return temp;
181 },
182 std::plus<>());
183# else
184 float temp = 0.0;
185 for (uint i = 0; i < verts; i++) {
186 temp += dot_v3v3(fLongVectorA[i], fLongVectorB[i]);
187 }
188 return temp;
189# endif
190}
191/* `A = B + C` -> for big vector. */
193 float (*fLongVectorA)[3],
194 float (*fLongVectorB)[3],
195 uint verts)
196{
197 uint i = 0;
198
199 for (i = 0; i < verts; i++) {
200 add_v3_v3v3(to[i], fLongVectorA[i], fLongVectorB[i]);
201 }
202}
203/* `A = B + C * float` -> for big vector. */
205 float (*to)[3], float (*fLongVectorA)[3], float (*fLongVectorB)[3], float bS, uint verts)
206{
207 uint i = 0;
208
209 for (i = 0; i < verts; i++) {
210 VECADDS(to[i], fLongVectorA[i], fLongVectorB[i], bS);
211 }
212}
213/* `A = B * float + C * float` -> for big vector */
215 float (*fLongVectorA)[3],
216 float aS,
217 float (*fLongVectorB)[3],
218 float bS,
219 uint verts)
220{
221 uint i = 0;
222
223 for (i = 0; i < verts; i++) {
224 VECADDSS(to[i], fLongVectorA[i], aS, fLongVectorB[i], bS);
225 }
226}
227/* `A = B - C * float` -> for big vector. */
229 float (*to)[3], float (*fLongVectorA)[3], float (*fLongVectorB)[3], float bS, uint verts)
230{
231 uint i = 0;
232 for (i = 0; i < verts; i++) {
233 VECSUBS(to[i], fLongVectorA[i], fLongVectorB[i], bS);
234 }
235}
236/* `A = B - C` -> for big vector. */
238 float (*fLongVectorA)[3],
239 float (*fLongVectorB)[3],
240 uint verts)
241{
242 uint i = 0;
243
244 for (i = 0; i < verts; i++) {
245 sub_v3_v3v3(to[i], fLongVectorA[i], fLongVectorB[i]);
246 }
247}
248
249// 3x3 matrix
251# if 0
252/* printf 3x3 matrix on console: for debug output */
253static void print_fmatrix(float m3[3][3])
254{
255 printf("%f\t%f\t%f\n", m3[0][0], m3[0][1], m3[0][2]);
256 printf("%f\t%f\t%f\n", m3[1][0], m3[1][1], m3[1][2]);
257 printf("%f\t%f\t%f\n\n", m3[2][0], m3[2][1], m3[2][2]);
258}
259
260static void print_sparse_matrix(fmatrix3x3 *m)
261{
262 if (m) {
263 uint i;
264 for (i = 0; i < m[0].vcount + m[0].scount; i++) {
265 printf("%d:\n", i);
266 print_fmatrix(m[i].m);
267 }
268 }
269}
270# endif
271
272# if 0
273static void print_lvector(lfVector *v, int numverts)
274{
275 int i;
276 for (i = 0; i < numverts; i++) {
277 if (i > 0) {
278 printf("\n");
279 }
280
281 printf("%f,\n", v[i][0]);
282 printf("%f,\n", v[i][1]);
283 printf("%f,\n", v[i][2]);
284 }
285}
286# endif
287
288# if 0
289static void print_bfmatrix(fmatrix3x3 *m)
290{
291 int tot = m[0].vcount + m[0].scount;
292 int size = m[0].vcount * 3;
293 float *t = MEM_calloc_array<float>N(size * size, "bfmatrix");
294 int q, i, j;
295
296 for (q = 0; q < tot; q++) {
297 int k = 3 * m[q].r;
298 int l = 3 * m[q].c;
299
300 for (j = 0; j < 3; j++) {
301 for (i = 0; i < 3; i++) {
302# if 0
303 if (t[k + i + (l + j) * size] != 0.0f) {
304 printf("warning: overwriting value at %d, %d\n", m[q].r, m[q].c);
305 }
306# endif
307 if (k == l) {
308 t[k + i + (k + j) * size] += m[q].m[i][j];
309 }
310 else {
311 t[k + i + (l + j) * size] += m[q].m[i][j];
312 t[l + j + (k + i) * size] += m[q].m[j][i];
313 }
314 }
315 }
316 }
317
318 for (j = 0; j < size; j++) {
319 if (j > 0 && j % 3 == 0) {
320 printf("\n");
321 }
322
323 for (i = 0; i < size; i++) {
324 if (i > 0 && i % 3 == 0) {
325 printf(" ");
326 }
327
329 }
330 printf("\n");
331 }
332
333 MEM_freeN(t);
334}
335# endif
336
337/* copy 3x3 matrix */
338DO_INLINE void cp_fmatrix(float to[3][3], const float from[3][3])
339{
340 // memcpy(to, from, sizeof(float[3][3]));
341 copy_v3_v3(to[0], from[0]);
342 copy_v3_v3(to[1], from[1]);
343 copy_v3_v3(to[2], from[2]);
344}
345
346/* copy 3x3 matrix */
347DO_INLINE void initdiag_fmatrixS(float to[3][3], float aS)
348{
349 cp_fmatrix(to, ZERO);
350
351 to[0][0] = aS;
352 to[1][1] = aS;
353 to[2][2] = aS;
354}
355
356# if 0
357/* calculate determinant of 3x3 matrix */
358DO_INLINE float det_fmatrix(float m[3][3])
359{
360 return m[0][0] * m[1][1] * m[2][2] + m[1][0] * m[2][1] * m[0][2] + m[0][1] * m[1][2] * m[2][0] -
361 m[0][0] * m[1][2] * m[2][1] - m[0][1] * m[1][0] * m[2][2] - m[2][0] * m[1][1] * m[0][2];
362}
363
364DO_INLINE void inverse_fmatrix(float to[3][3], float from[3][3])
365{
366 uint i, j;
367 float d;
368
369 if ((d = det_fmatrix(from)) == 0) {
370 // printf("can't build inverse");
371 }
372 for (i = 0; i < 3; i++) {
373 for (j = 0; j < 3; j++) {
374 int i1 = (i + 1) % 3;
375 int i2 = (i + 2) % 3;
376 int j1 = (j + 1) % 3;
377 int j2 = (j + 2) % 3;
379 to[j][i] = (from[i1][j1] * from[i2][j2] - from[i1][j2] * from[i2][j1]) / d;
390 }
391 }
392}
393# endif
394
395/* 3x3 matrix multiplied by a scalar */
396/* STATUS: verified */
397DO_INLINE void mul_fmatrix_S(float matrix[3][3], float scalar)
398{
399 mul_fvector_S(matrix[0], matrix[0], scalar);
400 mul_fvector_S(matrix[1], matrix[1], scalar);
401 mul_fvector_S(matrix[2], matrix[2], scalar);
402}
403
404/* a vector multiplied by a 3x3 matrix */
405/* STATUS: verified */
406DO_INLINE void mul_fvector_fmatrix(float *to, const float *from, const float matrix[3][3])
407{
408 to[0] = matrix[0][0] * from[0] + matrix[1][0] * from[1] + matrix[2][0] * from[2];
409 to[1] = matrix[0][1] * from[0] + matrix[1][1] * from[1] + matrix[2][1] * from[2];
410 to[2] = matrix[0][2] * from[0] + matrix[1][2] * from[1] + matrix[2][2] * from[2];
411}
412
413/* 3x3 matrix multiplied by a vector */
414/* STATUS: verified */
415DO_INLINE void mul_fmatrix_fvector(float *to, const float matrix[3][3], const float from[3])
416{
417 to[0] = dot_v3v3(matrix[0], from);
418 to[1] = dot_v3v3(matrix[1], from);
419 to[2] = dot_v3v3(matrix[2], from);
420}
421/* 3x3 matrix addition with 3x3 matrix */
422DO_INLINE void add_fmatrix_fmatrix(float to[3][3],
423 const float matrixA[3][3],
424 const float matrixB[3][3])
425{
426 add_v3_v3v3(to[0], matrixA[0], matrixB[0]);
427 add_v3_v3v3(to[1], matrixA[1], matrixB[1]);
428 add_v3_v3v3(to[2], matrixA[2], matrixB[2]);
429}
430/* `A -= B*x + (C * y)` (3x3 matrix sub-addition with 3x3 matrix). */
432 float to[3][3], const float matrixA[3][3], float aS, const float matrixB[3][3], float bS)
433{
434 VECSUBADDSS(to[0], matrixA[0], aS, matrixB[0], bS);
435 VECSUBADDSS(to[1], matrixA[1], aS, matrixB[1], bS);
436 VECSUBADDSS(to[2], matrixA[2], aS, matrixB[2], bS);
437}
438/* `A = B - C` (3x3 matrix subtraction with 3x3 matrix). */
439DO_INLINE void sub_fmatrix_fmatrix(float to[3][3],
440 const float matrixA[3][3],
441 const float matrixB[3][3])
442{
443 sub_v3_v3v3(to[0], matrixA[0], matrixB[0]);
444 sub_v3_v3v3(to[1], matrixA[1], matrixB[1]);
445 sub_v3_v3v3(to[2], matrixA[2], matrixB[2]);
446}
447
448/* special functions */
450/* 3x3 matrix multiplied+added by a vector */
451/* STATUS: verified */
452DO_INLINE void muladd_fmatrix_fvector(float to[3], const float matrix[3][3], const float from[3])
453{
454 to[0] += dot_v3v3(matrix[0], from);
455 to[1] += dot_v3v3(matrix[1], from);
456 to[2] += dot_v3v3(matrix[2], from);
457}
458
459DO_INLINE void muladd_fmatrixT_fvector(float to[3], const float matrix[3][3], const float from[3])
460{
461 to[0] += matrix[0][0] * from[0] + matrix[1][0] * from[1] + matrix[2][0] * from[2];
462 to[1] += matrix[0][1] * from[0] + matrix[1][1] * from[1] + matrix[2][1] * from[2];
463 to[2] += matrix[0][2] * from[0] + matrix[1][2] * from[1] + matrix[2][2] * from[2];
464}
465
466BLI_INLINE void outerproduct(float r[3][3], const float a[3], const float b[3])
467{
468 mul_v3_v3fl(r[0], a, b[0]);
469 mul_v3_v3fl(r[1], a, b[1]);
470 mul_v3_v3fl(r[2], a, b[2]);
471}
472
473BLI_INLINE void cross_m3_v3m3(float r[3][3], const float v[3], const float m[3][3])
474{
475 cross_v3_v3v3(r[0], v, m[0]);
476 cross_v3_v3v3(r[1], v, m[1]);
477 cross_v3_v3v3(r[2], v, m[2]);
478}
479
480BLI_INLINE void cross_v3_identity(float r[3][3], const float v[3])
481{
482 r[0][0] = 0.0f;
483 r[1][0] = v[2];
484 r[2][0] = -v[1];
485 r[0][1] = -v[2];
486 r[1][1] = 0.0f;
487 r[2][1] = v[0];
488 r[0][2] = v[1];
489 r[1][2] = -v[0];
490 r[2][2] = 0.0f;
491}
492
493BLI_INLINE void madd_m3_m3fl(float r[3][3], const float m[3][3], float f)
494{
495 r[0][0] += m[0][0] * f;
496 r[0][1] += m[0][1] * f;
497 r[0][2] += m[0][2] * f;
498 r[1][0] += m[1][0] * f;
499 r[1][1] += m[1][1] * f;
500 r[1][2] += m[1][2] * f;
501 r[2][0] += m[2][0] * f;
502 r[2][1] += m[2][1] * f;
503 r[2][2] += m[2][2] * f;
504}
505
507
509/* SPARSE SYMMETRIC big matrix with 3x3 matrix entries */
511/* printf a big matrix on console: for debug output */
512# if 0
513static void print_bfmatrix(fmatrix3x3 *m3)
514{
515 uint i = 0;
516
517 for (i = 0; i < m3[0].vcount + m3[0].scount; i++) {
518 print_fmatrix(m3[i].m);
519 }
520}
521# endif
522
523BLI_INLINE void init_fmatrix(fmatrix3x3 *matrix, int r, int c)
524{
525 matrix->r = r;
526 matrix->c = c;
527}
528
529/* create big matrix */
531{
532 /* TODO: check if memory allocation was successful */
533 fmatrix3x3 *temp = MEM_calloc_arrayN<fmatrix3x3>(verts + springs, "cloth_implicit_alloc_matrix");
534 int i;
535
536 temp[0].vcount = verts;
537 temp[0].scount = springs;
538
539 /* vertex part of the matrix is diagonal blocks */
540 for (i = 0; i < verts; i++) {
541 init_fmatrix(temp + i, i, i);
542 }
543
544 return temp;
545}
546/* delete big matrix */
548{
549 if (matrix != nullptr) {
550 MEM_freeN(matrix);
551 }
552}
553
554/* copy big matrix */
556{
557 /* TODO: bounds checking. */
558 memcpy(to, from, sizeof(fmatrix3x3) * (from[0].vcount + from[0].scount));
559}
560
561/* init big matrix */
562/* slow in parallel */
563DO_INLINE void init_bfmatrix(fmatrix3x3 *matrix, float m3[3][3])
564{
565 uint i;
566
567 for (i = 0; i < matrix[0].vcount + matrix[0].scount; i++) {
568 cp_fmatrix(matrix[i].m, m3);
569 }
570}
571
572/* init the diagonal of big matrix */
573/* slow in parallel */
574DO_INLINE void initdiag_bfmatrix(fmatrix3x3 *matrix, float m3[3][3])
575{
576 uint i, j;
577 float tmatrix[3][3] = {{0, 0, 0}, {0, 0, 0}, {0, 0, 0}};
578
579 for (i = 0; i < matrix[0].vcount; i++) {
580 cp_fmatrix(matrix[i].m, m3);
581 }
582 for (j = matrix[0].vcount; j < matrix[0].vcount + matrix[0].scount; j++) {
583 cp_fmatrix(matrix[j].m, tmatrix);
584 }
585}
586
587/* SPARSE SYMMETRIC multiply big matrix with long vector. */
588/* STATUS: verified */
589DO_INLINE void mul_bfmatrix_lfvector(float (*to)[3], fmatrix3x3 *from, lfVector *fLongVector)
590{
591 uint vcount = from[0].vcount;
592 lfVector *temp = create_lfvector(vcount);
593
594 zero_lfvector(to, vcount);
595
597 vcount > CLOTH_PARALLEL_LIMIT,
598 [&]() {
599 for (uint i = from[0].vcount; i < from[0].vcount + from[0].scount; i++) {
600 /* This is the lower triangle of the sparse matrix,
601 * therefore multiplication occurs with transposed sub-matrices. */
602 muladd_fmatrixT_fvector(to[from[i].c], from[i].m, fLongVector[from[i].r]);
603 }
604 },
605 [&]() {
606 for (uint i = 0; i < from[0].vcount + from[0].scount; i++) {
607 muladd_fmatrix_fvector(temp[from[i].r], from[i].m, fLongVector[from[i].c]);
608 }
609 });
610
611 add_lfvector_lfvector(to, to, temp, from[0].vcount);
612
613 del_lfvector(temp);
614}
615
616/* SPARSE SYMMETRIC sub big matrix with big matrix. */
617/* A -= B * float + C * float --> for big matrix */
618/* VERIFIED */
620 fmatrix3x3 *to, fmatrix3x3 *from, float aS, fmatrix3x3 *matrix, float bS)
621{
622 uint i = 0;
623
624 /* process diagonal elements */
625 for (i = 0; i < matrix[0].vcount + matrix[0].scount; i++) {
626 subadd_fmatrixS_fmatrixS(to[i].m, from[i].m, aS, matrix[i].m, bS);
627 }
628}
629
631/* simulator start */
633
635 /* inputs */
636 fmatrix3x3 *bigI; /* identity (constant) */
637 fmatrix3x3 *tfm; /* local coordinate transform */
638 fmatrix3x3 *M; /* masses */
639 lfVector *F; /* forces */
640 fmatrix3x3 *dFdV, *dFdX; /* force jacobians */
641 int num_blocks; /* number of off-diagonal blocks (springs) */
642
643 /* motion state data */
644 lfVector *X, *Xnew; /* positions */
645 lfVector *V, *Vnew; /* velocities */
646
647 /* internal solver data */
648 lfVector *B; /* B for A*dV = B */
649 fmatrix3x3 *A; /* A for A*dV = B */
650
651 lfVector *dV; /* velocity change (solution of A*dV = B) */
652 lfVector *z; /* target velocity in constrained directions */
653 fmatrix3x3 *S; /* filtering matrix for constraints */
654 fmatrix3x3 *P, *Pinv; /* pre-conditioning matrix */
655};
656
657Implicit_Data *SIM_mass_spring_solver_create(int numverts, int numsprings)
658{
659 Implicit_Data *id = MEM_callocN<Implicit_Data>("implicit vecmat");
660
661 /* process diagonal elements */
662 id->tfm = create_bfmatrix(numverts, 0);
663 id->A = create_bfmatrix(numverts, numsprings);
664 id->dFdV = create_bfmatrix(numverts, numsprings);
665 id->dFdX = create_bfmatrix(numverts, numsprings);
666 id->S = create_bfmatrix(numverts, 0);
667 id->Pinv = create_bfmatrix(numverts, numsprings);
668 id->P = create_bfmatrix(numverts, numsprings);
669 id->bigI = create_bfmatrix(numverts, numsprings); /* TODO: 0 springs. */
670 id->M = create_bfmatrix(numverts, numsprings);
671 id->X = create_lfvector(numverts);
672 id->Xnew = create_lfvector(numverts);
673 id->V = create_lfvector(numverts);
674 id->Vnew = create_lfvector(numverts);
675 id->F = create_lfvector(numverts);
676 id->B = create_lfvector(numverts);
677 id->dV = create_lfvector(numverts);
678 id->z = create_lfvector(numverts);
679
681
682 return id;
683}
684
686{
687 del_bfmatrix(id->tfm);
688 del_bfmatrix(id->A);
689 del_bfmatrix(id->dFdV);
690 del_bfmatrix(id->dFdX);
691 del_bfmatrix(id->S);
692 del_bfmatrix(id->P);
693 del_bfmatrix(id->Pinv);
694 del_bfmatrix(id->bigI);
695 del_bfmatrix(id->M);
696
697 del_lfvector(id->X);
698 del_lfvector(id->Xnew);
699 del_lfvector(id->V);
700 del_lfvector(id->Vnew);
701 del_lfvector(id->F);
702 del_lfvector(id->B);
703 del_lfvector(id->dV);
704 del_lfvector(id->z);
705
706 MEM_freeN(id);
707}
708
709/* ==== Transformation from/to root reference frames ==== */
710
711BLI_INLINE void world_to_root_v3(Implicit_Data *data, int index, float r[3], const float v[3])
712{
713 copy_v3_v3(r, v);
714 mul_transposed_m3_v3(data->tfm[index].m, r);
715}
716
717BLI_INLINE void root_to_world_v3(Implicit_Data *data, int index, float r[3], const float v[3])
718{
719 mul_v3_m3v3(r, data->tfm[index].m, v);
720}
721
723 int index,
724 float r[3][3],
725 const float m[3][3])
726{
727 float trot[3][3];
728 copy_m3_m3(trot, data->tfm[index].m);
729 transpose_m3(trot);
730 mul_m3_m3m3(r, trot, m);
731}
732
734 int index,
735 float r[3][3],
736 const float m[3][3])
737{
738 mul_m3_m3m3(r, data->tfm[index].m, m);
739}
740
741/* ================================ */
742
744{
745 uint i = 0;
746
747 for (i = 0; i < S[0].vcount; i++) {
748 mul_m3_v3(S[i].m, V[S[i].r]);
749 }
750}
751
752/* this version of the CG algorithm does not work very well with partial constraints
753 * (where S has non-zero elements). */
754# if 0
755static int cg_filtered(lfVector *ldV, fmatrix3x3 *lA, lfVector *lB, lfVector *z, fmatrix3x3 *S)
756{
757 /* Solves for unknown X in equation AX=B */
758 uint conjgrad_loopcount = 0, conjgrad_looplimit = 100;
759 float conjgrad_epsilon = 0.0001f /* , conjgrad_lasterror=0 */ /* UNUSED */;
760 lfVector *q, *d, *tmp, *r;
761 float s, starget, a, s_prev;
762 uint numverts = lA[0].vcount;
763 q = create_lfvector(numverts);
764 d = create_lfvector(numverts);
765 tmp = create_lfvector(numverts);
766 r = create_lfvector(numverts);
767
768 // zero_lfvector(ldV, CLOTHPARTICLES);
769 filter(ldV, S);
770
771 add_lfvector_lfvector(ldV, ldV, z, numverts);
772
773 // r = B - Mul(tmp, A, X); /* just use B if X known to be zero. */
774 cp_lfvector(r, lB, numverts);
775 mul_bfmatrix_lfvector(tmp, lA, ldV);
776 sub_lfvector_lfvector(r, r, tmp, numverts);
777
778 filter(r, S);
779
780 cp_lfvector(d, r, numverts);
781
782 s = dot_lfvector(r, r, numverts);
783 starget = s * sqrtf(conjgrad_epsilon);
784
785 while (s > starget && conjgrad_loopcount < conjgrad_looplimit) {
786 // Mul(q, A, d); /* q = A*d; */
787 mul_bfmatrix_lfvector(q, lA, d);
788
789 filter(q, S);
790
791 a = s / dot_lfvector(d, q, numverts);
792
793 /* `X = X + d*a;` */
794 add_lfvector_lfvectorS(ldV, ldV, d, a, numverts);
795
796 /* `r = r - q*a;` */
797 sub_lfvector_lfvectorS(r, r, q, a, numverts);
798
799 s_prev = s;
800 s = dot_lfvector(r, r, numverts);
801
802 /* `d = r+d*(s/s_prev);` */
803 add_lfvector_lfvectorS(d, r, d, (s / s_prev), numverts);
804
805 filter(d, S);
806
807 conjgrad_loopcount++;
808 }
809 // conjgrad_lasterror = s; /* UNUSED */
810
811 del_lfvector(q);
812 del_lfvector(d);
813 del_lfvector(tmp);
814 del_lfvector(r);
815 // printf("W/O conjgrad_loopcount: %d\n", conjgrad_loopcount);
816
817 /* True means we reached desired accuracy in given time - ie stable. */
818 return conjgrad_loopcount < conjgrad_looplimit;
819}
820# endif
821
822static int cg_filtered(lfVector *ldV,
823 fmatrix3x3 *lA,
824 lfVector *lB,
825 lfVector *z,
826 fmatrix3x3 *S,
828{
829 /* Solves for unknown X in equation AX=B */
830 uint conjgrad_loopcount = 0, conjgrad_looplimit = 100;
831 float conjgrad_epsilon = 0.01f;
832
833 uint numverts = lA[0].vcount;
834 lfVector *fB = create_lfvector(numverts);
835 lfVector *AdV = create_lfvector(numverts);
836 lfVector *r = create_lfvector(numverts);
837 lfVector *c = create_lfvector(numverts);
838 lfVector *q = create_lfvector(numverts);
839 lfVector *s = create_lfvector(numverts);
840 float bnorm2, delta_new, delta_old, delta_target, alpha;
841
842 cp_lfvector(ldV, z, numverts);
843
844 /* d0 = filter(B)^T * P * filter(B) */
845 cp_lfvector(fB, lB, numverts);
846 filter(fB, S);
847 bnorm2 = dot_lfvector(fB, fB, numverts);
848 delta_target = conjgrad_epsilon * conjgrad_epsilon * bnorm2;
849
850 /* r = filter(B - A * dV) */
851 mul_bfmatrix_lfvector(AdV, lA, ldV);
852 sub_lfvector_lfvector(r, lB, AdV, numverts);
853 filter(r, S);
854
855 /* c = filter(P^-1 * r) */
856 cp_lfvector(c, r, numverts);
857 filter(c, S);
858
859 /* delta = r^T * c */
860 delta_new = dot_lfvector(r, c, numverts);
861
862# ifdef IMPLICIT_PRINT_SOLVER_INPUT_OUTPUT
863 printf("==== A ====\n");
864 print_bfmatrix(lA);
865 printf("==== z ====\n");
866 print_lvector(z, numverts);
867 printf("==== B ====\n");
868 print_lvector(lB, numverts);
869 printf("==== S ====\n");
870 print_bfmatrix(S);
871# endif
872
873 while (delta_new > delta_target && conjgrad_loopcount < conjgrad_looplimit) {
874 mul_bfmatrix_lfvector(q, lA, c);
875 filter(q, S);
876
877 alpha = delta_new / dot_lfvector(c, q, numverts);
878
879 add_lfvector_lfvectorS(ldV, ldV, c, alpha, numverts);
880
881 add_lfvector_lfvectorS(r, r, q, -alpha, numverts);
882
883 /* s = P^-1 * r */
884 cp_lfvector(s, r, numverts);
885 delta_old = delta_new;
886 delta_new = dot_lfvector(r, s, numverts);
887
888 add_lfvector_lfvectorS(c, s, c, delta_new / delta_old, numverts);
889 filter(c, S);
890
891 conjgrad_loopcount++;
892 }
893
894# ifdef IMPLICIT_PRINT_SOLVER_INPUT_OUTPUT
895 printf("==== dV ====\n");
896 print_lvector(ldV, numverts);
897 printf("========\n");
898# endif
899
900 del_lfvector(fB);
901 del_lfvector(AdV);
902 del_lfvector(r);
903 del_lfvector(c);
904 del_lfvector(q);
905 del_lfvector(s);
906 // printf("W/O conjgrad_loopcount: %d\n", conjgrad_loopcount);
907
908 result->status = conjgrad_loopcount < conjgrad_looplimit ? SIM_SOLVER_SUCCESS :
910 result->iterations = conjgrad_loopcount;
911 result->error = bnorm2 > 0.0f ? sqrtf(delta_new / bnorm2) : 0.0f;
912
913 /* True means we reached desired accuracy in given time - ie stable. */
914 return conjgrad_loopcount < conjgrad_looplimit;
915}
916
917# if 0
918/* block diagonalizer */
919DO_INLINE void BuildPPinv(fmatrix3x3 *lA, fmatrix3x3 *P, fmatrix3x3 *Pinv)
920{
921 /* Take only the diagonal blocks of A */
924 [&](const blender::IndexRange &range) {
925 for (int64_t i : range) {
926 /* block diagonalizer */
927 cp_fmatrix(P[i].m, lA[i].m);
928 inverse_fmatrix(Pinv[i].m, P[i].m);
929 }
930 });
931}
932
933# if 0
934/* version 1.3 */
935static int cg_filtered_pre(lfVector *dv,
936 fmatrix3x3 *lA,
937 lfVector *lB,
938 lfVector *z,
939 fmatrix3x3 *S,
940 fmatrix3x3 *P,
941 fmatrix3x3 *Pinv)
942{
943 uint numverts = lA[0].vcount, iterations = 0, conjgrad_looplimit = 100;
944 float delta0 = 0, deltaNew = 0, deltaOld = 0, alpha = 0;
945 float conjgrad_epsilon = 0.0001; /* 0.2 is dt for steps=5 */
946 lfVector *r = create_lfvector(numverts);
947 lfVector *p = create_lfvector(numverts);
948 lfVector *s = create_lfvector(numverts);
949 lfVector *h = create_lfvector(numverts);
950
951 BuildPPinv(lA, P, Pinv);
952
953 filter(dv, S);
954 add_lfvector_lfvector(dv, dv, z, numverts);
955
956 mul_bfmatrix_lfvector(r, lA, dv);
957 sub_lfvector_lfvector(r, lB, r, numverts);
958 filter(r, S);
959
960 mul_prevfmatrix_lfvector(p, Pinv, r);
961 filter(p, S);
962
963 deltaNew = dot_lfvector(r, p, numverts);
964
965 delta0 = deltaNew * sqrt(conjgrad_epsilon);
966
967# ifdef DEBUG_TIME
968 double start = BLI_time_now_seconds();
969# endif
970
971 while ((deltaNew > delta0) && (iterations < conjgrad_looplimit)) {
972 iterations++;
973
974 mul_bfmatrix_lfvector(s, lA, p);
975 filter(s, S);
976
977 alpha = deltaNew / dot_lfvector(p, s, numverts);
978
979 add_lfvector_lfvectorS(dv, dv, p, alpha, numverts);
980
981 add_lfvector_lfvectorS(r, r, s, -alpha, numverts);
982
983 mul_prevfmatrix_lfvector(h, Pinv, r);
984 filter(h, S);
985
986 deltaOld = deltaNew;
987
988 deltaNew = dot_lfvector(r, h, numverts);
989
990 add_lfvector_lfvectorS(p, h, p, deltaNew / deltaOld, numverts);
991
992 filter(p, S);
993 }
994
995# ifdef DEBUG_TIME
996 double end = BLI_time_now_seconds();
997 printf("cg_filtered_pre time: %f\n", float(end - start));
998# endif
999
1000 del_lfvector(h);
1001 del_lfvector(s);
1002 del_lfvector(p);
1003 del_lfvector(r);
1004
1005 printf("iterations: %d\n", iterations);
1006
1007 return iterations < conjgrad_looplimit;
1008}
1009# endif
1010
1011/* version 1.4 */
1012static int cg_filtered_pre(lfVector *dv,
1013 fmatrix3x3 *lA,
1014 lfVector *lB,
1015 lfVector *z,
1016 fmatrix3x3 *S,
1017 fmatrix3x3 *P,
1018 fmatrix3x3 *Pinv,
1019 fmatrix3x3 *bigI)
1020{
1021 uint numverts = lA[0].vcount, iterations = 0, conjgrad_looplimit = 100;
1022 float delta0 = 0, deltaNew = 0, deltaOld = 0, alpha = 0, tol = 0;
1023 lfVector *r = create_lfvector(numverts);
1024 lfVector *p = create_lfvector(numverts);
1025 lfVector *s = create_lfvector(numverts);
1026 lfVector *h = create_lfvector(numverts);
1027 lfVector *bhat = create_lfvector(numverts);
1028 lfVector *btemp = create_lfvector(numverts);
1029
1030 BuildPPinv(lA, P, Pinv);
1031
1032 initdiag_bfmatrix(bigI, I);
1033 sub_bfmatrix_Smatrix(bigI, bigI, S);
1034
1035 /* x = Sx_0+(I-S)z */
1036 filter(dv, S);
1037 add_lfvector_lfvector(dv, dv, z, numverts);
1038
1039 /* b_hat = S(b-A(I-S)z) */
1040 mul_bfmatrix_lfvector(r, lA, z);
1041 mul_bfmatrix_lfvector(bhat, bigI, r);
1042 sub_lfvector_lfvector(bhat, lB, bhat, numverts);
1043
1044 /* r = S(b-Ax) */
1045 mul_bfmatrix_lfvector(r, lA, dv);
1046 sub_lfvector_lfvector(r, lB, r, numverts);
1047 filter(r, S);
1048
1049 /* p = SP^-1r */
1050 mul_prevfmatrix_lfvector(p, Pinv, r);
1051 filter(p, S);
1052
1053 /* delta0 = bhat^TP^-1bhat */
1054 mul_prevfmatrix_lfvector(btemp, Pinv, bhat);
1055 delta0 = dot_lfvector(bhat, btemp, numverts);
1056
1057 /* deltaNew = r^TP */
1058 deltaNew = dot_lfvector(r, p, numverts);
1059
1060# if 0
1061 filter(dv, S);
1062 add_lfvector_lfvector(dv, dv, z, numverts);
1063
1064 mul_bfmatrix_lfvector(r, lA, dv);
1065 sub_lfvector_lfvector(r, lB, r, numverts);
1066 filter(r, S);
1067
1068 mul_prevfmatrix_lfvector(p, Pinv, r);
1069 filter(p, S);
1070
1071 deltaNew = dot_lfvector(r, p, numverts);
1072
1073 delta0 = deltaNew * sqrt(conjgrad_epsilon);
1074# endif
1075
1076# ifdef DEBUG_TIME
1077 double start = BLI_time_now_seconds();
1078# endif
1079
1080 tol = (0.01 * 0.2);
1081
1082 while ((deltaNew > delta0 * tol * tol) && (iterations < conjgrad_looplimit)) {
1083 iterations++;
1084
1085 mul_bfmatrix_lfvector(s, lA, p);
1086 filter(s, S);
1087
1088 alpha = deltaNew / dot_lfvector(p, s, numverts);
1089
1090 add_lfvector_lfvectorS(dv, dv, p, alpha, numverts);
1091
1092 add_lfvector_lfvectorS(r, r, s, -alpha, numverts);
1093
1094 mul_prevfmatrix_lfvector(h, Pinv, r);
1095 filter(h, S);
1096
1097 deltaOld = deltaNew;
1098
1099 deltaNew = dot_lfvector(r, h, numverts);
1100
1101 add_lfvector_lfvectorS(p, h, p, deltaNew / deltaOld, numverts);
1102
1103 filter(p, S);
1104 }
1105
1106# ifdef DEBUG_TIME
1107 double end = BLI_time_now_seconds();
1108 printf("cg_filtered_pre time: %f\n", float(end - start));
1109# endif
1110
1111 del_lfvector(btemp);
1112 del_lfvector(bhat);
1113 del_lfvector(h);
1114 del_lfvector(s);
1115 del_lfvector(p);
1116 del_lfvector(r);
1117
1118 // printf("iterations: %d\n", iterations);
1119
1120 return iterations < conjgrad_looplimit;
1121}
1122# endif
1123
1125{
1126 uint numverts = data->dFdV[0].vcount;
1127
1128 lfVector *dFdXmV = create_lfvector(numverts);
1129 zero_lfvector(data->dV, numverts);
1130
1131 cp_bfmatrix(data->A, data->M);
1132
1133 subadd_bfmatrixS_bfmatrixS(data->A, data->dFdV, dt, data->dFdX, (dt * dt));
1134
1135 mul_bfmatrix_lfvector(dFdXmV, data->dFdX, data->V);
1136
1137 add_lfvectorS_lfvectorS(data->B, data->F, dt, dFdXmV, (dt * dt), numverts);
1138
1139# ifdef DEBUG_TIME
1140 double start = BLI_time_now_seconds();
1141# endif
1142
1143 /* Conjugate gradient algorithm to solve Ax=b. */
1144 cg_filtered(data->dV, data->A, data->B, data->z, data->S, result);
1145
1146 // cg_filtered_pre(id->dV, id->A, id->B, id->z, id->S, id->P, id->Pinv, id->bigI);
1147
1148# ifdef DEBUG_TIME
1149 double end = BLI_time_now_seconds();
1150 printf("cg_filtered calc time: %f\n", float(end - start));
1151# endif
1152
1153 /* advance velocities */
1154 add_lfvector_lfvector(data->Vnew, data->V, data->dV, numverts);
1155
1156 del_lfvector(dFdXmV);
1157
1158 return result->status == SIM_SOLVER_SUCCESS;
1159}
1160
1162{
1163 int numverts = data->M[0].vcount;
1164
1165 /* advance positions */
1166 add_lfvector_lfvectorS(data->Xnew, data->X, data->Vnew, dt, numverts);
1167
1168 return true;
1169}
1170
1172{
1173 int numverts = data->M[0].vcount;
1174 cp_lfvector(data->X, data->Xnew, numverts);
1175 cp_lfvector(data->V, data->Vnew, numverts);
1176}
1177
1179{
1180 unit_m3(data->M[index].m);
1181 mul_m3_fl(data->M[index].m, mass);
1182}
1183
1185{
1186# ifdef CLOTH_ROOT_FRAME
1187 copy_m3_m3(data->tfm[index].m, tfm);
1188# else
1189 unit_m3(data->tfm[index].m);
1190 (void)tfm;
1191# endif
1192}
1193
1195 int index,
1196 const float x[3],
1197 const float v[3])
1198{
1199 world_to_root_v3(data, index, data->X[index], x);
1200 world_to_root_v3(data, index, data->V[index], v);
1201}
1202
1203void SIM_mass_spring_set_position(Implicit_Data *data, int index, const float x[3])
1204{
1205 world_to_root_v3(data, index, data->X[index], x);
1206}
1207
1208void SIM_mass_spring_set_velocity(Implicit_Data *data, int index, const float v[3])
1209{
1210 world_to_root_v3(data, index, data->V[index], v);
1211}
1212
1213void SIM_mass_spring_get_motion_state(Implicit_Data *data, int index, float x[3], float v[3])
1214{
1215 if (x) {
1216 root_to_world_v3(data, index, x, data->X[index]);
1217 }
1218 if (v) {
1219 root_to_world_v3(data, index, v, data->V[index]);
1220 }
1221}
1222
1224{
1225 root_to_world_v3(data, index, x, data->X[index]);
1226}
1227
1229{
1230 root_to_world_v3(data, index, v, data->V[index]);
1231}
1232
1234{
1235 root_to_world_v3(data, index, x, data->Xnew[index]);
1236}
1237
1238void SIM_mass_spring_set_new_position(Implicit_Data *data, int index, const float x[3])
1239{
1240 world_to_root_v3(data, index, data->Xnew[index], x);
1241}
1242
1244{
1245 root_to_world_v3(data, index, v, data->Vnew[index]);
1246}
1247
1248void SIM_mass_spring_set_new_velocity(Implicit_Data *data, int index, const float v[3])
1249{
1250 world_to_root_v3(data, index, data->Vnew[index], v);
1251}
1252
1253/* -------------------------------- */
1254
1256{
1257 int s = data->M[0].vcount + data->num_blocks; /* index from array start */
1258 BLI_assert(s < data->M[0].vcount + data->M[0].scount);
1259 ++data->num_blocks;
1260
1261 /* tfm and S don't have spring entries (diagonal blocks only) */
1262 init_fmatrix(data->bigI + s, v1, v2);
1263 init_fmatrix(data->M + s, v1, v2);
1264 init_fmatrix(data->dFdX + s, v1, v2);
1265 init_fmatrix(data->dFdV + s, v1, v2);
1266 init_fmatrix(data->A + s, v1, v2);
1267 init_fmatrix(data->P + s, v1, v2);
1268 init_fmatrix(data->Pinv + s, v1, v2);
1269
1270 return s;
1271}
1272
1274{
1275 int i, numverts = data->S[0].vcount;
1276 for (i = 0; i < numverts; i++) {
1277 unit_m3(data->S[i].m);
1278 zero_v3(data->z[i]);
1279 }
1280}
1281
1282void SIM_mass_spring_add_constraint_ndof0(Implicit_Data *data, int index, const float dV[3])
1283{
1284 zero_m3(data->S[index].m);
1285
1286 world_to_root_v3(data, index, data->z[index], dV);
1287}
1288
1290 Implicit_Data *data, int index, const float c1[3], const float c2[3], const float dV[3])
1291{
1292 float m[3][3], p[3], q[3], u[3], cmat[3][3];
1293
1294 world_to_root_v3(data, index, p, c1);
1295 mul_fvectorT_fvector(cmat, p, p);
1296 sub_m3_m3m3(m, I, cmat);
1297
1298 world_to_root_v3(data, index, q, c2);
1299 mul_fvectorT_fvector(cmat, q, q);
1300 sub_m3_m3m3(m, m, cmat);
1301
1302 /* XXX not sure but multiplication should work here */
1303 copy_m3_m3(data->S[index].m, m);
1304 // mul_m3_m3m3(data->S[index].m, data->S[index].m, m);
1305
1306 world_to_root_v3(data, index, u, dV);
1307 add_v3_v3(data->z[index], u);
1308}
1309
1311 int index,
1312 const float c1[3],
1313 const float dV[3])
1314{
1315 float m[3][3], p[3], u[3], cmat[3][3];
1316
1317 world_to_root_v3(data, index, p, c1);
1318 mul_fvectorT_fvector(cmat, p, p);
1319 sub_m3_m3m3(m, I, cmat);
1320
1321 copy_m3_m3(data->S[index].m, m);
1322 // mul_m3_m3m3(data->S[index].m, data->S[index].m, m);
1323
1324 world_to_root_v3(data, index, u, dV);
1325 add_v3_v3(data->z[index], u);
1326}
1327
1329{
1330 int numverts = data->M[0].vcount;
1331 zero_lfvector(data->F, numverts);
1332 init_bfmatrix(data->dFdX, ZERO);
1333 init_bfmatrix(data->dFdV, ZERO);
1334
1335 data->num_blocks = 0;
1336}
1337
1339 int index,
1340 const float acceleration[3],
1341 const float omega[3],
1342 const float domega_dt[3],
1343 float mass)
1344{
1345# ifdef CLOTH_ROOT_FRAME
1346 float acc[3], w[3], dwdt[3];
1347 float f[3], dfdx[3][3], dfdv[3][3];
1348 float euler[3], coriolis[3], centrifugal[3], rotvel[3];
1349 float deuler[3][3], dcoriolis[3][3], dcentrifugal[3][3], drotvel[3][3];
1350
1351 world_to_root_v3(data, index, acc, acceleration);
1352 world_to_root_v3(data, index, w, omega);
1353 world_to_root_v3(data, index, dwdt, domega_dt);
1354
1355 cross_v3_v3v3(euler, dwdt, data->X[index]);
1356 cross_v3_v3v3(coriolis, w, data->V[index]);
1357 mul_v3_fl(coriolis, 2.0f);
1358 cross_v3_v3v3(rotvel, w, data->X[index]);
1359 cross_v3_v3v3(centrifugal, w, rotvel);
1360
1361 sub_v3_v3v3(f, acc, euler);
1362 sub_v3_v3(f, coriolis);
1363 sub_v3_v3(f, centrifugal);
1364
1365 mul_v3_fl(f, mass); /* F = m * a */
1366
1367 cross_v3_identity(deuler, dwdt);
1368 cross_v3_identity(dcoriolis, w);
1369 mul_m3_fl(dcoriolis, 2.0f);
1370 cross_v3_identity(drotvel, w);
1371 cross_m3_v3m3(dcentrifugal, w, drotvel);
1372
1373 add_m3_m3m3(dfdx, deuler, dcentrifugal);
1374 negate_m3(dfdx);
1375 mul_m3_fl(dfdx, mass);
1376
1377 copy_m3_m3(dfdv, dcoriolis);
1378 negate_m3(dfdv);
1379 mul_m3_fl(dfdv, mass);
1380
1381 add_v3_v3(data->F[index], f);
1382 add_m3_m3m3(data->dFdX[index].m, data->dFdX[index].m, dfdx);
1383 add_m3_m3m3(data->dFdV[index].m, data->dFdV[index].m, dfdv);
1384# else
1385 (void)data;
1386 (void)index;
1387 (void)acceleration;
1388 (void)omega;
1389 (void)domega_dt;
1390# endif
1391}
1392
1393void SIM_mass_spring_force_gravity(Implicit_Data *data, int index, float mass, const float g[3])
1394{
1395 /* force = mass * acceleration (in this case: gravity) */
1396 float f[3];
1397 world_to_root_v3(data, index, f, g);
1398 mul_v3_fl(f, mass);
1399
1400 add_v3_v3(data->F[index], f);
1401}
1402
1404{
1405 int i, numverts = data->M[0].vcount;
1406 for (i = 0; i < numverts; i++) {
1407 float tmp[3][3];
1408
1409 /* NOTE: Uses root space velocity, no need to transform. */
1410 madd_v3_v3fl(data->F[i], data->V[i], -drag);
1411
1412 copy_m3_m3(tmp, I);
1413 mul_m3_fl(tmp, -drag);
1414 add_m3_m3m3(data->dFdV[i].m, data->dFdV[i].m, tmp);
1415 }
1416}
1417
1419 Implicit_Data *data, int i, const float f[3], float dfdx[3][3], float dfdv[3][3])
1420{
1421 float tf[3], tdfdx[3][3], tdfdv[3][3];
1422 world_to_root_v3(data, i, tf, f);
1423 world_to_root_m3(data, i, tdfdx, dfdx);
1424 world_to_root_m3(data, i, tdfdv, dfdv);
1425
1426 add_v3_v3(data->F[i], tf);
1427 add_m3_m3m3(data->dFdX[i].m, data->dFdX[i].m, tdfdx);
1428 add_m3_m3m3(data->dFdV[i].m, data->dFdV[i].m, tdfdv);
1429}
1430
1431static float calc_nor_area_tri(float nor[3],
1432 const float v1[3],
1433 const float v2[3],
1434 const float v3[3])
1435{
1436 float n1[3], n2[3];
1437
1438 sub_v3_v3v3(n1, v1, v2);
1439 sub_v3_v3v3(n2, v2, v3);
1440
1441 cross_v3_v3v3(nor, n1, n2);
1442 return normalize_v3(nor) / 2.0f;
1443}
1444
1446 Implicit_Data *data, int v1, int v2, int v3, const float (*winvec)[3])
1447{
1448 /* XXX does not support force jacobians yet,
1449 * since the effector system does not provide them either. */
1450
1451 const float effector_scale = 0.02f;
1452 const int vs[3] = {v1, v2, v3};
1453 float win[3], nor[3], area;
1454 float factor, base_force;
1455 float force[3];
1456
1457 /* calculate face normal and area */
1458 area = calc_nor_area_tri(nor, data->X[v1], data->X[v2], data->X[v3]);
1459 /* The force is calculated and split up evenly for each of the three face verts */
1460 factor = effector_scale * area / 3.0f;
1461
1462 /* Calculate wind pressure at each vertex by projecting the wind field on the normal. */
1463 for (int i = 0; i < 3; i++) {
1464 world_to_root_v3(data, vs[i], win, winvec[vs[i]]);
1465
1466 force[i] = dot_v3v3(win, nor);
1467 }
1468
1469 /* Compute per-vertex force values from local pressures.
1470 * From integrating the pressure over the triangle and deriving
1471 * equivalent vertex forces, it follows that:
1472 *
1473 * force[idx] = (sum(pressure) + pressure[idx]) * area / 12
1474 *
1475 * Effectively, 1/4 of the pressure acts just on its vertex,
1476 * while 3/4 is split evenly over all three.
1477 */
1478 mul_v3_fl(force, factor / 4.0f);
1479
1480 base_force = force[0] + force[1] + force[2];
1481
1482 /* add pressure to each of the face verts */
1483 madd_v3_v3fl(data->F[v1], nor, base_force + force[0]);
1484 madd_v3_v3fl(data->F[v2], nor, base_force + force[1]);
1485 madd_v3_v3fl(data->F[v3], nor, base_force + force[2]);
1486}
1487
1489 Implicit_Data *data, int v1, int v2, int v3, const float (*forcevec)[3])
1490{
1491 const float effector_scale = 0.02f;
1492 const int vs[3] = {v1, v2, v3};
1493 float nor[3], area;
1494 float factor, base_force[3];
1495 float force[3][3];
1496
1497 /* calculate face normal and area */
1498 area = calc_nor_area_tri(nor, data->X[v1], data->X[v2], data->X[v3]);
1499 /* The force is calculated and split up evenly for each of the three face verts */
1500 factor = effector_scale * area / 3.0f;
1501
1502 /* Compute common and per-vertex force vectors from the original inputs. */
1503 zero_v3(base_force);
1504
1505 for (int i = 0; i < 3; i++) {
1506 world_to_root_v3(data, vs[i], force[i], forcevec[vs[i]]);
1507
1508 mul_v3_fl(force[i], factor / 4.0f);
1509 add_v3_v3(base_force, force[i]);
1510 }
1511
1512 /* Apply the common and vertex components to all vertices. */
1513 for (int i = 0; i < 3; i++) {
1514 add_v3_v3(force[i], base_force);
1515 add_v3_v3(data->F[vs[i]], force[i]);
1516 }
1517}
1518
1520{
1521 /* The result will be 6x the volume */
1522 return volume_tri_tetrahedron_signed_v3_6x(data->X[v1], data->X[v2], data->X[v3]);
1523}
1524
1525float SIM_tri_area(Implicit_Data *data, int v1, int v2, int v3)
1526{
1527 float nor[3];
1528
1529 return calc_nor_area_tri(nor, data->X[v1], data->X[v2], data->X[v3]);
1530}
1531
1533 int v1,
1534 int v2,
1535 int v3,
1536 float common_pressure,
1537 const float *vertex_pressure,
1538 const float weights[3])
1539{
1540 float nor[3], area;
1541 float factor, base_force;
1542 float force[3];
1543
1544 /* calculate face normal and area */
1545 area = calc_nor_area_tri(nor, data->X[v1], data->X[v2], data->X[v3]);
1546 /* The force is calculated and split up evenly for each of the three face verts */
1547 factor = area / 3.0f;
1548 base_force = common_pressure * factor;
1549
1550 /* Compute per-vertex force values from local pressures.
1551 * From integrating the pressure over the triangle and deriving
1552 * equivalent vertex forces, it follows that:
1553 *
1554 * force[idx] = (sum(pressure) + pressure[idx]) * area / 12
1555 *
1556 * Effectively, 1/4 of the pressure acts just on its vertex,
1557 * while 3/4 is split evenly over all three.
1558 */
1559 if (vertex_pressure) {
1560 copy_v3_fl3(force, vertex_pressure[v1], vertex_pressure[v2], vertex_pressure[v3]);
1561 mul_v3_fl(force, factor / 4.0f);
1562
1563 base_force += force[0] + force[1] + force[2];
1564 }
1565 else {
1566 zero_v3(force);
1567 }
1568
1569 /* add pressure to each of the face verts */
1570 madd_v3_v3fl(data->F[v1], nor, (base_force + force[0]) * weights[0]);
1571 madd_v3_v3fl(data->F[v2], nor, (base_force + force[1]) * weights[1]);
1572 madd_v3_v3fl(data->F[v3], nor, (base_force + force[2]) * weights[2]);
1573}
1574
1575static void edge_wind_vertex(const float dir[3],
1576 float length,
1577 float radius,
1578 const float wind[3],
1579 float f[3],
1580 float /*dfdx*/[3][3],
1581 float /*dfdv*/[3][3])
1582{
1583 const float density = 0.01f; /* XXX arbitrary value, corresponds to effect of air density */
1584 float cos_alpha, sin_alpha, cross_section;
1585 float windlen = len_v3(wind);
1586
1587 if (windlen == 0.0f) {
1588 zero_v3(f);
1589 return;
1590 }
1591
1592 /* angle of wind direction to edge */
1593 cos_alpha = dot_v3v3(wind, dir) / windlen;
1594 sin_alpha = sqrtf(1.0f - cos_alpha * cos_alpha);
1595 cross_section = radius * (float(M_PI) * radius * sin_alpha + length * cos_alpha);
1596
1597 mul_v3_v3fl(f, wind, density * cross_section);
1598}
1599
1601 Implicit_Data *data, int v1, int v2, float radius1, float radius2, const float (*winvec)[3])
1602{
1603 float win[3], dir[3], length;
1604 float f[3], dfdx[3][3], dfdv[3][3];
1605
1606 sub_v3_v3v3(dir, data->X[v1], data->X[v2]);
1607 length = normalize_v3(dir);
1608
1609 world_to_root_v3(data, v1, win, winvec[v1]);
1610 edge_wind_vertex(dir, length, radius1, win, f, dfdx, dfdv);
1611 add_v3_v3(data->F[v1], f);
1612
1613 world_to_root_v3(data, v2, win, winvec[v2]);
1614 edge_wind_vertex(dir, length, radius2, win, f, dfdx, dfdv);
1615 add_v3_v3(data->F[v2], f);
1616}
1617
1619 int v,
1620 float /*radius*/,
1621 const float (*winvec)[3])
1622{
1623 const float density = 0.01f; /* XXX arbitrary value, corresponds to effect of air density */
1624
1625 float wind[3];
1626 float f[3];
1627
1628 world_to_root_v3(data, v, wind, winvec[v]);
1629 mul_v3_v3fl(f, wind, density);
1630 add_v3_v3(data->F[v], f);
1631}
1632
1633BLI_INLINE void dfdx_spring(float to[3][3], const float dir[3], float length, float L, float k)
1634{
1635 /* dir is unit length direction, rest is spring's restlength, k is spring constant. */
1636 // return ( (I-outerprod(dir, dir))*Min(1.0f, rest/length) - I) * -k;
1637 outerproduct(to, dir, dir);
1638 sub_m3_m3m3(to, I, to);
1639
1640 mul_m3_fl(to, (L / length));
1641 sub_m3_m3m3(to, to, I);
1642 mul_m3_fl(to, k);
1643}
1644
1645/* unused */
1646# if 0
1647BLI_INLINE void dfdx_damp(float to[3][3],
1648 const float dir[3],
1649 float length,
1650 const float vel[3],
1651 float rest,
1652 float damping)
1653{
1654 /* Inner spring damping `vel` is the relative velocity of the endpoints. */
1655 // return (I - outerprod(dir, dir)) * (-damping * -(dot(dir, vel) / Max(length, rest)));
1656 mul_fvectorT_fvector(to, dir, dir);
1657 sub_fmatrix_fmatrix(to, I, to);
1658 mul_fmatrix_S(to, (-damping * -(dot_v3v3(dir, vel) / std::max(length, rest))));
1659}
1660# endif
1661
1662BLI_INLINE void dfdv_damp(float to[3][3], const float dir[3], float damping)
1663{
1664 /* Derivative of force with regards to velocity. */
1665 outerproduct(to, dir, dir);
1666 mul_m3_fl(to, -damping);
1667}
1668
1669BLI_INLINE float fb(float length, float L)
1670{
1671 float x = length / L;
1672 float xx = x * x;
1673 float xxx = xx * x;
1674 float xxxx = xxx * x;
1675 return (-11.541f * xxxx + 34.193f * xxx - 39.083f * xx + 23.116f * x - 9.713f);
1676}
1677
1678BLI_INLINE float fbderiv(float length, float L)
1679{
1680 float x = length / L;
1681 float xx = x * x;
1682 float xxx = xx * x;
1683 return (-46.164f * xxx + 102.579f * xx - 78.166f * x + 23.116f);
1684}
1685
1686BLI_INLINE float fbstar(float length, float L, float kb, float cb)
1687{
1688 float tempfb_fl = kb * fb(length, L);
1689 float fbstar_fl = cb * (length - L);
1690
1691 if (tempfb_fl < fbstar_fl) {
1692 return fbstar_fl;
1693 }
1694
1695 return tempfb_fl;
1696}
1697
1698/* Function to calculate bending spring force (taken from Choi & Co). */
1699BLI_INLINE float fbstar_jacobi(float length, float L, float kb, float cb)
1700{
1701 float tempfb_fl = kb * fb(length, L);
1702 float fbstar_fl = cb * (length - L);
1703
1704 if (tempfb_fl < fbstar_fl) {
1705 return -cb;
1706 }
1707
1708 return -kb * fbderiv(length, L);
1709}
1710
1711/* calculate elongation */
1713 int i,
1714 int j,
1715 float r_extent[3],
1716 float r_dir[3],
1717 float *r_length,
1718 float r_vel[3])
1719{
1720 sub_v3_v3v3(r_extent, data->X[j], data->X[i]);
1721 sub_v3_v3v3(r_vel, data->V[j], data->V[i]);
1722 *r_length = len_v3(r_extent);
1723
1724 if (*r_length > ALMOST_ZERO) {
1725# if 0
1726 if (length > L) {
1727 if ((clmd->sim_parms->flags & CSIMSETT_FLAG_TEARING_ENABLED) &&
1728 (((length - L) * 100.0f / L) > clmd->sim_parms->maxspringlen))
1729 {
1730 /* cut spring! */
1731 s->flags |= CSPRING_FLAG_DEACTIVATE;
1732 return false;
1733 }
1734 }
1735# endif
1736 mul_v3_v3fl(r_dir, r_extent, 1.0f / (*r_length));
1737 }
1738 else {
1739 zero_v3(r_dir);
1740 }
1741
1742 return true;
1743}
1744
1746 int i,
1747 int j,
1748 const float f[3],
1749 const float dfdx[3][3],
1750 const float dfdv[3][3])
1751{
1752 int block_ij = SIM_mass_spring_add_block(data, i, j);
1753
1754 add_v3_v3(data->F[i], f);
1755 sub_v3_v3(data->F[j], f);
1756
1757 add_m3_m3m3(data->dFdX[i].m, data->dFdX[i].m, dfdx);
1758 add_m3_m3m3(data->dFdX[j].m, data->dFdX[j].m, dfdx);
1759 sub_m3_m3m3(data->dFdX[block_ij].m, data->dFdX[block_ij].m, dfdx);
1760
1761 add_m3_m3m3(data->dFdV[i].m, data->dFdV[i].m, dfdv);
1762 add_m3_m3m3(data->dFdV[j].m, data->dFdV[j].m, dfdv);
1763 sub_m3_m3m3(data->dFdV[block_ij].m, data->dFdV[block_ij].m, dfdv);
1764}
1765
1767 int i,
1768 int j,
1769 float restlen,
1770 float stiffness_tension,
1771 float damping_tension,
1772 float stiffness_compression,
1773 float damping_compression,
1774 bool resist_compress,
1775 bool new_compress,
1776 float clamp_force)
1777{
1778 float extent[3], length, dir[3], vel[3];
1779 float f[3], dfdx[3][3], dfdv[3][3];
1780 float damping = 0;
1781
1782 /* calculate elongation */
1783 spring_length(data, i, j, extent, dir, &length, vel);
1784
1785 /* This code computes not only the force, but also its derivative.
1786 * Zero derivative effectively disables the spring for the implicit solver.
1787 * Thus length > restlen makes cloth unconstrained at the start of simulation. */
1788 if ((length >= restlen && length > 0) || resist_compress) {
1789 float stretch_force;
1790
1791 damping = damping_tension;
1792
1793 stretch_force = stiffness_tension * (length - restlen);
1794 if (clamp_force > 0.0f && stretch_force > clamp_force) {
1795 stretch_force = clamp_force;
1796 }
1797 mul_v3_v3fl(f, dir, stretch_force);
1798
1799 dfdx_spring(dfdx, dir, length, restlen, stiffness_tension);
1800 }
1801 else if (new_compress) {
1802 /* This is based on the Choi and Ko bending model,
1803 * which works surprisingly well for compression. */
1804 float kb = stiffness_compression;
1805 float cb = kb; /* cb equal to kb seems to work, but a factor can be added if necessary */
1806
1807 damping = damping_compression;
1808
1809 mul_v3_v3fl(f, dir, fbstar(length, restlen, kb, cb));
1810
1811 outerproduct(dfdx, dir, dir);
1812 mul_m3_fl(dfdx, fbstar_jacobi(length, restlen, kb, cb));
1813 }
1814 else {
1815 return false;
1816 }
1817
1818 madd_v3_v3fl(f, dir, damping * dot_v3v3(vel, dir));
1819 dfdv_damp(dfdv, dir, damping);
1820
1821 apply_spring(data, i, j, f, dfdx, dfdv);
1822
1823 return true;
1824}
1825
1827 Implicit_Data *data, int i, int j, float restlen, float kb, float cb)
1828{
1829 /* See "Stable but Responsive Cloth" (Choi, Ko 2005). */
1830
1831 float extent[3], length, dir[3], vel[3];
1832
1833 /* calculate elongation */
1834 spring_length(data, i, j, extent, dir, &length, vel);
1835
1836 if (length < restlen) {
1837 float f[3], dfdx[3][3], dfdv[3][3];
1838
1839 mul_v3_v3fl(f, dir, fbstar(length, restlen, kb, cb));
1840
1841 outerproduct(dfdx, dir, dir);
1842 mul_m3_fl(dfdx, fbstar_jacobi(length, restlen, kb, cb));
1843
1844 /* XXX damping not supported */
1845 zero_m3(dfdv);
1846
1847 apply_spring(data, i, j, f, dfdx, dfdv);
1848
1849 return true;
1850 }
1851
1852 return false;
1853}
1854
1855BLI_INLINE void poly_avg(lfVector *data, const int *inds, int len, float r_avg[3])
1856{
1857 float fact = 1.0f / float(len);
1858
1859 zero_v3(r_avg);
1860
1861 for (int i = 0; i < len; i++) {
1862 madd_v3_v3fl(r_avg, data[inds[i]], fact);
1863 }
1864}
1865
1866BLI_INLINE void poly_norm(lfVector *data, int i, int j, int *inds, int len, float r_dir[3])
1867{
1868 float mid[3];
1869
1870 poly_avg(data, inds, len, mid);
1871
1872 normal_tri_v3(r_dir, data[i], data[j], mid);
1873}
1874
1875BLI_INLINE void edge_avg(lfVector *data, int i, int j, float r_avg[3])
1876{
1877 r_avg[0] = (data[i][0] + data[j][0]) * 0.5f;
1878 r_avg[1] = (data[i][1] + data[j][1]) * 0.5f;
1879 r_avg[2] = (data[i][2] + data[j][2]) * 0.5f;
1880}
1881
1882BLI_INLINE void edge_norm(lfVector *data, int i, int j, float r_dir[3])
1883{
1884 sub_v3_v3v3(r_dir, data[i], data[j]);
1885 normalize_v3(r_dir);
1886}
1887
1888BLI_INLINE float bend_angle(const float dir_a[3], const float dir_b[3], const float dir_e[3])
1889{
1890 float cos, sin;
1891 float tmp[3];
1892
1893 cos = dot_v3v3(dir_a, dir_b);
1894
1895 cross_v3_v3v3(tmp, dir_a, dir_b);
1896 sin = dot_v3v3(tmp, dir_e);
1897
1898 return atan2f(sin, cos);
1899}
1900
1902 int i,
1903 int j,
1904 int *i_a,
1905 int *i_b,
1906 int len_a,
1907 int len_b,
1908 float r_dir_a[3],
1909 float r_dir_b[3],
1910 float *r_angle,
1911 float r_vel_a[3],
1912 float r_vel_b[3])
1913{
1914 float dir_e[3], vel_e[3];
1915
1916 poly_norm(data->X, j, i, i_a, len_a, r_dir_a);
1917 poly_norm(data->X, i, j, i_b, len_b, r_dir_b);
1918
1919 edge_norm(data->X, i, j, dir_e);
1920
1921 *r_angle = bend_angle(r_dir_a, r_dir_b, dir_e);
1922
1923 poly_avg(data->V, i_a, len_a, r_vel_a);
1924 poly_avg(data->V, i_b, len_b, r_vel_b);
1925
1926 edge_avg(data->V, i, j, vel_e);
1927
1928 sub_v3_v3(r_vel_a, vel_e);
1929 sub_v3_v3(r_vel_b, vel_e);
1930}
1931
1933 int i,
1934 int j,
1935 int *i_a,
1936 int *i_b,
1937 int len_a,
1938 int len_b,
1939 float restang,
1940 float stiffness,
1941 float damping)
1942{
1943 float angle, dir_a[3], dir_b[3], vel_a[3], vel_b[3];
1944 float f_a[3], f_b[3], f_e[3];
1945 float force;
1946 int x;
1947
1948 spring_angle(data, i, j, i_a, i_b, len_a, len_b, dir_a, dir_b, &angle, vel_a, vel_b);
1949
1950 /* spring force */
1951 force = stiffness * (angle - restang);
1952
1953 /* damping force */
1954 force += -damping * (dot_v3v3(vel_a, dir_a) + dot_v3v3(vel_b, dir_b));
1955
1956 mul_v3_v3fl(f_a, dir_a, force / len_a);
1957 mul_v3_v3fl(f_b, dir_b, force / len_b);
1958
1959 for (x = 0; x < len_a; x++) {
1960 add_v3_v3(data->F[i_a[x]], f_a);
1961 }
1962
1963 for (x = 0; x < len_b; x++) {
1964 add_v3_v3(data->F[i_b[x]], f_b);
1965 }
1966
1967 mul_v3_v3fl(f_a, dir_a, force * 0.5f);
1968 mul_v3_v3fl(f_b, dir_b, force * 0.5f);
1969
1970 add_v3_v3v3(f_e, f_a, f_b);
1971
1972 sub_v3_v3(data->F[i], f_e);
1973 sub_v3_v3(data->F[j], f_e);
1974
1975 return true;
1976}
1977
1978/* Jacobian of a direction vector.
1979 * Basically the part of the differential orthogonal to the direction,
1980 * inversely proportional to the length of the edge.
1981 *
1982 * dD_ij/dx_i = -dD_ij/dx_j = (D_ij * D_ij^T - I) / len_ij
1983 */
1985 Implicit_Data *data, int i, int j, float edge[3], float dir[3], float grad_dir[3][3])
1986{
1987 float length;
1988
1989 sub_v3_v3v3(edge, data->X[j], data->X[i]);
1990 length = normalize_v3_v3(dir, edge);
1991
1992 if (length > ALMOST_ZERO) {
1993 outerproduct(grad_dir, dir, dir);
1994 sub_m3_m3m3(grad_dir, I, grad_dir);
1995 mul_m3_fl(grad_dir, 1.0f / length);
1996 }
1997 else {
1998 zero_m3(grad_dir);
1999 }
2000}
2001
2003 int i,
2004 int j,
2005 int k,
2006 const float goal[3],
2007 float stiffness,
2008 float damping,
2009 int q,
2010 const float dx[3],
2011 const float dv[3],
2012 float r_f[3])
2013{
2014 float edge_ij[3], dir_ij[3];
2015 float edge_jk[3], dir_jk[3];
2016 float vel_ij[3], vel_jk[3], vel_ortho[3];
2017 float f_bend[3], f_damp[3];
2018 float fk[3];
2019 float dist[3];
2020
2021 zero_v3(fk);
2022
2023 sub_v3_v3v3(edge_ij, data->X[j], data->X[i]);
2024 if (q == i) {
2025 sub_v3_v3(edge_ij, dx);
2026 }
2027 if (q == j) {
2028 add_v3_v3(edge_ij, dx);
2029 }
2030 normalize_v3_v3(dir_ij, edge_ij);
2031
2032 sub_v3_v3v3(edge_jk, data->X[k], data->X[j]);
2033 if (q == j) {
2034 sub_v3_v3(edge_jk, dx);
2035 }
2036 if (q == k) {
2037 add_v3_v3(edge_jk, dx);
2038 }
2039 normalize_v3_v3(dir_jk, edge_jk);
2040
2041 sub_v3_v3v3(vel_ij, data->V[j], data->V[i]);
2042 if (q == i) {
2043 sub_v3_v3(vel_ij, dv);
2044 }
2045 if (q == j) {
2046 add_v3_v3(vel_ij, dv);
2047 }
2048
2049 sub_v3_v3v3(vel_jk, data->V[k], data->V[j]);
2050 if (q == j) {
2051 sub_v3_v3(vel_jk, dv);
2052 }
2053 if (q == k) {
2054 add_v3_v3(vel_jk, dv);
2055 }
2056
2057 /* bending force */
2058 sub_v3_v3v3(dist, goal, edge_jk);
2059 mul_v3_v3fl(f_bend, dist, stiffness);
2060
2061 add_v3_v3(fk, f_bend);
2062
2063 /* damping force */
2064 madd_v3_v3v3fl(vel_ortho, vel_jk, dir_jk, -dot_v3v3(vel_jk, dir_jk));
2065 mul_v3_v3fl(f_damp, vel_ortho, damping);
2066
2067 sub_v3_v3(fk, f_damp);
2068
2069 copy_v3_v3(r_f, fk);
2070}
2071
2072/* Finite Differences method for estimating the jacobian of the force */
2074 int i,
2075 int j,
2076 int k,
2077 const float goal[3],
2078 float stiffness,
2079 float damping,
2080 int q,
2081 float dfdx[3][3])
2082{
2083 const float delta = 0.00001f; /* TODO: find a good heuristic for this. */
2084 float dvec_null[3][3], dvec_pos[3][3], dvec_neg[3][3];
2085 float f[3];
2086 int a, b;
2087
2088 zero_m3(dvec_null);
2089 unit_m3(dvec_pos);
2090 mul_m3_fl(dvec_pos, delta * 0.5f);
2091 copy_m3_m3(dvec_neg, dvec_pos);
2092 negate_m3(dvec_neg);
2093
2094 /* XXX TODO: offset targets to account for position dependency. */
2095
2096 for (a = 0; a < 3; a++) {
2098 data, i, j, k, goal, stiffness, damping, q, dvec_pos[a], dvec_null[a], f);
2099 copy_v3_v3(dfdx[a], f);
2100
2102 data, i, j, k, goal, stiffness, damping, q, dvec_neg[a], dvec_null[a], f);
2103 sub_v3_v3(dfdx[a], f);
2104
2105 for (b = 0; b < 3; b++) {
2106 dfdx[a][b] /= delta;
2107 }
2108 }
2109}
2110
2111/* Finite Differences method for estimating the jacobian of the force */
2113 int i,
2114 int j,
2115 int k,
2116 const float goal[3],
2117 float stiffness,
2118 float damping,
2119 int q,
2120 float dfdv[3][3])
2121{
2122 const float delta = 0.00001f; /* TODO: find a good heuristic for this. */
2123 float dvec_null[3][3], dvec_pos[3][3], dvec_neg[3][3];
2124 float f[3];
2125 int a, b;
2126
2127 zero_m3(dvec_null);
2128 unit_m3(dvec_pos);
2129 mul_m3_fl(dvec_pos, delta * 0.5f);
2130 copy_m3_m3(dvec_neg, dvec_pos);
2131 negate_m3(dvec_neg);
2132
2133 /* XXX TODO: offset targets to account for position dependency. */
2134
2135 for (a = 0; a < 3; a++) {
2137 data, i, j, k, goal, stiffness, damping, q, dvec_null[a], dvec_pos[a], f);
2138 copy_v3_v3(dfdv[a], f);
2139
2141 data, i, j, k, goal, stiffness, damping, q, dvec_null[a], dvec_neg[a], f);
2142 sub_v3_v3(dfdv[a], f);
2143
2144 for (b = 0; b < 3; b++) {
2145 dfdv[a][b] /= delta;
2146 }
2147 }
2148}
2149
2151 int i,
2152 int j,
2153 int k,
2154 const float target[3],
2155 float stiffness,
2156 float damping)
2157{
2158 /* Angular springs roughly based on the bending model proposed by Baraff and Witkin in
2159 * "Large Steps in Cloth Simulation". */
2160
2161 float goal[3];
2162 float fj[3], fk[3];
2163 float dfj_dxi[3][3], dfj_dxj[3][3], dfk_dxi[3][3], dfk_dxj[3][3], dfk_dxk[3][3];
2164 float dfj_dvi[3][3], dfj_dvj[3][3], dfk_dvi[3][3], dfk_dvj[3][3], dfk_dvk[3][3];
2165
2166 const float vecnull[3] = {0.0f, 0.0f, 0.0f};
2167
2168 int block_ij = SIM_mass_spring_add_block(data, i, j);
2169 int block_jk = SIM_mass_spring_add_block(data, j, k);
2170 int block_ik = SIM_mass_spring_add_block(data, i, k);
2171
2172 world_to_root_v3(data, j, goal, target);
2173
2174 spring_hairbend_forces(data, i, j, k, goal, stiffness, damping, k, vecnull, vecnull, fk);
2175 negate_v3_v3(fj, fk); /* Counter-force. */
2176
2177 spring_hairbend_estimate_dfdx(data, i, j, k, goal, stiffness, damping, i, dfk_dxi);
2178 spring_hairbend_estimate_dfdx(data, i, j, k, goal, stiffness, damping, j, dfk_dxj);
2179 spring_hairbend_estimate_dfdx(data, i, j, k, goal, stiffness, damping, k, dfk_dxk);
2180 copy_m3_m3(dfj_dxi, dfk_dxi);
2181 negate_m3(dfj_dxi);
2182 copy_m3_m3(dfj_dxj, dfk_dxj);
2183 negate_m3(dfj_dxj);
2184
2185 spring_hairbend_estimate_dfdv(data, i, j, k, goal, stiffness, damping, i, dfk_dvi);
2186 spring_hairbend_estimate_dfdv(data, i, j, k, goal, stiffness, damping, j, dfk_dvj);
2187 spring_hairbend_estimate_dfdv(data, i, j, k, goal, stiffness, damping, k, dfk_dvk);
2188 copy_m3_m3(dfj_dvi, dfk_dvi);
2189 negate_m3(dfj_dvi);
2190 copy_m3_m3(dfj_dvj, dfk_dvj);
2191 negate_m3(dfj_dvj);
2192
2193 /* add forces and jacobians to the solver data */
2194
2195 add_v3_v3(data->F[j], fj);
2196 add_v3_v3(data->F[k], fk);
2197
2198 add_m3_m3m3(data->dFdX[j].m, data->dFdX[j].m, dfj_dxj);
2199 add_m3_m3m3(data->dFdX[k].m, data->dFdX[k].m, dfk_dxk);
2200
2201 add_m3_m3m3(data->dFdX[block_ij].m, data->dFdX[block_ij].m, dfj_dxi);
2202 add_m3_m3m3(data->dFdX[block_jk].m, data->dFdX[block_jk].m, dfk_dxj);
2203 add_m3_m3m3(data->dFdX[block_ik].m, data->dFdX[block_ik].m, dfk_dxi);
2204
2205 add_m3_m3m3(data->dFdV[j].m, data->dFdV[j].m, dfj_dvj);
2206 add_m3_m3m3(data->dFdV[k].m, data->dFdV[k].m, dfk_dvk);
2207
2208 add_m3_m3m3(data->dFdV[block_ij].m, data->dFdV[block_ij].m, dfj_dvi);
2209 add_m3_m3m3(data->dFdV[block_jk].m, data->dFdV[block_jk].m, dfk_dvj);
2210 add_m3_m3m3(data->dFdV[block_ik].m, data->dFdV[block_ik].m, dfk_dvi);
2211
2212/* XXX analytical calculation of derivatives below is incorrect.
2213 * This proved to be difficult, but for now just using the finite difference method for
2214 * estimating the jacobians should be sufficient.
2215 */
2216# if 0
2217 float edge_ij[3], dir_ij[3], grad_dir_ij[3][3];
2218 float edge_jk[3], dir_jk[3], grad_dir_jk[3][3];
2219 float dist[3], vel_jk[3], vel_jk_ortho[3], projvel[3];
2220 float target[3];
2221 float tmp[3][3];
2222 float fi[3], fj[3], fk[3];
2223 float dfi_dxi[3][3], dfj_dxi[3][3], dfj_dxj[3][3], dfk_dxi[3][3], dfk_dxj[3][3], dfk_dxk[3][3];
2224 float dfdvi[3][3];
2225
2226 /* TESTING */
2227 damping = 0.0f;
2228
2229 zero_v3(fi);
2230 zero_v3(fj);
2231 zero_v3(fk);
2232 zero_m3(dfi_dxi);
2233 zero_m3(dfj_dxi);
2234 zero_m3(dfk_dxi);
2235 zero_m3(dfk_dxj);
2236 zero_m3(dfk_dxk);
2237
2238 /* jacobian of direction vectors */
2239 spring_grad_dir(data, i, j, edge_ij, dir_ij, grad_dir_ij);
2240 spring_grad_dir(data, j, k, edge_jk, dir_jk, grad_dir_jk);
2241
2242 sub_v3_v3v3(vel_jk, data->V[k], data->V[j]);
2243
2244 /* bending force */
2245 mul_v3_v3fl(target, dir_ij, restlen);
2246 sub_v3_v3v3(dist, target, edge_jk);
2247 mul_v3_v3fl(fk, dist, stiffness);
2248
2249 /* damping force */
2250 madd_v3_v3v3fl(vel_jk_ortho, vel_jk, dir_jk, -dot_v3v3(vel_jk, dir_jk));
2251 madd_v3_v3fl(fk, vel_jk_ortho, damping);
2252
2253 /* XXX this only holds true as long as we assume straight rest shape!
2254 * eventually will become a bit more involved since the opposite segment
2255 * gets its own target, under condition of having equal torque on both sides.
2256 */
2257 copy_v3_v3(fi, fk);
2258
2259 /* counterforce on the middle point */
2260 sub_v3_v3(fj, fi);
2261 sub_v3_v3(fj, fk);
2262
2263 /* === derivatives === */
2264
2265 madd_m3_m3fl(dfk_dxi, grad_dir_ij, stiffness * restlen);
2266
2267 madd_m3_m3fl(dfk_dxj, grad_dir_ij, -stiffness * restlen);
2268 madd_m3_m3fl(dfk_dxj, I, stiffness);
2269
2270 madd_m3_m3fl(dfk_dxk, I, -stiffness);
2271
2272 copy_m3_m3(dfi_dxi, dfk_dxk);
2273 negate_m3(dfi_dxi);
2274
2275 /* dfj_dfi == dfi_dfj due to symmetry,
2276 * dfi_dfj == dfk_dfj due to fi == fk
2277 * XXX see comment above on future bent rest shapes
2278 */
2279 copy_m3_m3(dfj_dxi, dfk_dxj);
2280
2281 /* dfj_dxj == -(dfi_dxj + dfk_dxj) due to fj == -(fi + fk) */
2282 sub_m3_m3m3(dfj_dxj, dfj_dxj, dfj_dxi);
2283 sub_m3_m3m3(dfj_dxj, dfj_dxj, dfk_dxj);
2284
2285 /* add forces and jacobians to the solver data */
2286 add_v3_v3(data->F[i], fi);
2287 add_v3_v3(data->F[j], fj);
2288 add_v3_v3(data->F[k], fk);
2289
2290 add_m3_m3m3(data->dFdX[i].m, data->dFdX[i].m, dfi_dxi);
2291 add_m3_m3m3(data->dFdX[j].m, data->dFdX[j].m, dfj_dxj);
2292 add_m3_m3m3(data->dFdX[k].m, data->dFdX[k].m, dfk_dxk);
2293
2294 add_m3_m3m3(data->dFdX[block_ij].m, data->dFdX[block_ij].m, dfj_dxi);
2295 add_m3_m3m3(data->dFdX[block_jk].m, data->dFdX[block_jk].m, dfk_dxj);
2296 add_m3_m3m3(data->dFdX[block_ik].m, data->dFdX[block_ik].m, dfk_dxi);
2297# endif
2298
2299 return true;
2300}
2301
2303 int i,
2304 const float goal_x[3],
2305 const float goal_v[3],
2306 float stiffness,
2307 float damping)
2308{
2309 float root_goal_x[3], root_goal_v[3], extent[3], length, dir[3], vel[3];
2310 float f[3], dfdx[3][3], dfdv[3][3];
2311
2312 /* goal is in world space */
2313 world_to_root_v3(data, i, root_goal_x, goal_x);
2314 world_to_root_v3(data, i, root_goal_v, goal_v);
2315
2316 sub_v3_v3v3(extent, root_goal_x, data->X[i]);
2317 sub_v3_v3v3(vel, root_goal_v, data->V[i]);
2318 length = normalize_v3_v3(dir, extent);
2319
2320 if (length > ALMOST_ZERO) {
2321 mul_v3_v3fl(f, dir, stiffness * length);
2322
2323 /* Ascher & Boxman, p.21: Damping only during elongation
2324 * something wrong with it. */
2325 madd_v3_v3fl(f, dir, damping * dot_v3v3(vel, dir));
2326
2327 dfdx_spring(dfdx, dir, length, 0.0f, stiffness);
2328 dfdv_damp(dfdv, dir, damping);
2329
2330 add_v3_v3(data->F[i], f);
2331 add_m3_m3m3(data->dFdX[i].m, data->dFdX[i].m, dfdx);
2332 add_m3_m3m3(data->dFdV[i].m, data->dFdV[i].m, dfdv);
2333
2334 return true;
2335 }
2336
2337 return false;
2338}
2339
2340#endif /* IMPLICIT_SOLVER_BLENDER */
#define VECADDS(v1, v2, v3, bS)
Definition BKE_cloth.hh:155
#define DO_INLINE
Definition BKE_cloth.hh:27
#define VECSUBS(v1, v2, v3, bS)
Definition BKE_cloth.hh:169
#define VECADDSS(v1, v2, aS, v3, bS)
Definition BKE_cloth.hh:148
#define VECSUBADDSS(v1, v2, aS, v3, bS)
Definition BKE_cloth.hh:141
#define VECSUBMUL(v1, v2, aS)
Definition BKE_cloth.hh:162
#define ALMOST_ZERO
Definition BKE_cloth.hh:34
#define BLI_assert(a)
Definition BLI_assert.h:46
#define BLI_INLINE
#define M_PI
float volume_tri_tetrahedron_signed_v3_6x(const float v1[3], const float v2[3], const float v3[3])
Definition math_geom.cc:263
float normal_tri_v3(float n[3], const float v1[3], const float v2[3], const float v3[3])
Definition math_geom.cc:41
void sub_m3_m3m3(float R[3][3], const float A[3][3], const float B[3][3])
void negate_m3(float R[3][3])
void mul_m3_v3(const float M[3][3], float r[3])
void copy_m3_m3(float m1[3][3], const float m2[3][3])
void unit_m3(float m[3][3])
void mul_m3_fl(float R[3][3], float f)
void add_m3_m3m3(float R[3][3], const float A[3][3], const float B[3][3])
void zero_m3(float m[3][3])
void mul_v3_m3v3(float r[3], const float M[3][3], const float a[3])
void transpose_m3(float R[3][3])
void mul_transposed_m3_v3(const float M[3][3], float r[3])
void mul_m3_m3m3(float R[3][3], const float A[3][3], const float B[3][3])
MINLINE void madd_v3_v3fl(float r[3], const float a[3], float f)
MINLINE void sub_v3_v3(float r[3], const float a[3])
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 void negate_v3_v3(float r[3], const float a[3])
MINLINE void copy_v3_fl3(float v[3], float x, float y, float z)
MINLINE float dot_v3v3(const float a[3], const float b[3]) ATTR_WARN_UNUSED_RESULT
MINLINE void add_v3_v3v3(float r[3], const float a[3], const float b[3])
MINLINE void cross_v3_v3v3(float r[3], const float a[3], const float b[3])
MINLINE float normalize_v3_v3(float r[3], const float a[3])
MINLINE void madd_v3_v3v3fl(float r[3], const float a[3], const float b[3], float f)
MINLINE void zero_v3(float r[3])
MINLINE void mul_v3_v3fl(float r[3], const float a[3], float f)
MINLINE void add_v3_v3(float r[3], const float a[3])
MINLINE float normalize_v3(float n[3])
MINLINE float len_v3(const float a[3]) ATTR_WARN_UNUSED_RESULT
unsigned int uint
Platform independent time functions.
double BLI_time_now_seconds(void)
Definition time.cc:113
static double angle(const Eigen::Vector3d &v1, const Eigen::Vector3d &v2)
Definition IK_Math.h:117
Read Guarded memory(de)allocation.
@ SIM_SOLVER_SUCCESS
@ SIM_SOLVER_NO_CONVERGENCE
#define ZERO
Definition StrokeRep.cpp:88
BMesh const char void * data
ATTR_WARN_UNUSED_RESULT const BMVert * v2
ATTR_WARN_UNUSED_RESULT const BMLoop * l
ATTR_WARN_UNUSED_RESULT const BMVert * v
long long int int64_t
static DBVT_INLINE btScalar size(const btDbvtVolume &a)
Definition btDbvt.cpp:52
SIMD_FORCE_INLINE const btScalar & z() const
Return the z value.
Definition btQuadWord.h:117
SIMD_FORCE_INLINE const btScalar & w() const
Return the w value.
Definition btQuadWord.h:119
nullptr float
BLI_INLINE void print_lvector(const lVector3f &v)
static float verts[][3]
uint nor
#define filter
#define printf(...)
#define sin
#define cos
#define sqrt
float length(VecOp< float, D >) RET
BLI_INLINE void implicit_print_matrix_elem(float v)
Definition implicit.h:41
void SIM_mass_spring_force_gravity(Implicit_Data *data, int index, float mass, const float g[3])
float SIM_tri_area(Implicit_Data *data, int v1, int v2, int v3)
void SIM_mass_spring_set_vertex_mass(Implicit_Data *data, int index, float mass)
void SIM_mass_spring_clear_forces(Implicit_Data *data)
DO_INLINE void add_lfvector_lfvector(float(*to)[3], float(*fLongVectorA)[3], float(*fLongVectorB)[3], uint verts)
float[3] lfVector
BLI_INLINE void apply_spring(Implicit_Data *data, int i, int j, const float f[3], const float dfdx[3][3], const float dfdv[3][3])
void SIM_mass_spring_clear_constraints(Implicit_Data *data)
void SIM_mass_spring_get_velocity(Implicit_Data *data, int index, float v[3])
BLI_INLINE float fbstar_jacobi(float length, float L, float kb, float cb)
void SIM_mass_spring_force_pressure(Implicit_Data *data, int v1, int v2, int v3, float common_pressure, const float *vertex_pressure, const float weights[3])
DO_INLINE void submul_lfvectorS(float(*to)[3], float(*fLongVector)[3], float scalar, uint verts)
DO_INLINE lfVector * create_lfvector(uint verts)
Implicit_Data * SIM_mass_spring_solver_create(int numverts, int numsprings)
DO_INLINE void add_fmatrix_fmatrix(float to[3][3], const float matrixA[3][3], const float matrixB[3][3])
void SIM_mass_spring_add_constraint_ndof0(Implicit_Data *data, int index, const float dV[3])
DO_INLINE void mul_fvectorT_fvector(float to[3][3], const float vectorA[3], const float vectorB[3])
void SIM_mass_spring_force_extern(Implicit_Data *data, int i, const float f[3], float dfdx[3][3], float dfdv[3][3])
DO_INLINE void del_lfvector(float(*fLongVector)[3])
BLI_INLINE void cross_m3_v3m3(float r[3][3], const float v[3], const float m[3][3])
bool SIM_mass_spring_force_spring_bending_hair(Implicit_Data *data, int i, int j, int k, const float target[3], float stiffness, float damping)
BLI_INLINE float fb(float length, float L)
void SIM_mass_spring_set_velocity(Implicit_Data *data, int index, const float v[3])
DO_INLINE void mul_fvectorT_fvectorS(float to[3][3], float vectorA[3], float vectorB[3], float aS)
DO_INLINE void mul_fmatrix_S(float matrix[3][3], float scalar)
bool SIM_mass_spring_solve_positions(Implicit_Data *data, float dt)
DO_INLINE void initdiag_fmatrixS(float to[3][3], float aS)
BLI_INLINE float fbderiv(float length, float L)
BLI_INLINE void madd_m3_m3fl(float r[3][3], const float m[3][3], float f)
DO_INLINE void muladd_fmatrix_fvector(float to[3], const float matrix[3][3], const float from[3])
DO_INLINE void mul_bfmatrix_lfvector(float(*to)[3], fmatrix3x3 *from, lfVector *fLongVector)
BLI_INLINE void spring_hairbend_forces(Implicit_Data *data, int i, int j, int k, const float goal[3], float stiffness, float damping, int q, const float dx[3], const float dv[3], float r_f[3])
void SIM_mass_spring_force_vertex_wind(Implicit_Data *data, int v, float, const float(*winvec)[3])
bool SIM_mass_spring_force_spring_goal(Implicit_Data *data, int i, const float goal_x[3], const float goal_v[3], float stiffness, float damping)
void SIM_mass_spring_force_face_extern(Implicit_Data *data, int v1, int v2, int v3, const float(*forcevec)[3])
DO_INLINE void muladd_fmatrixT_fvector(float to[3], const float matrix[3][3], const float from[3])
bool SIM_mass_spring_solve_velocities(Implicit_Data *data, float dt, ImplicitSolverResult *result)
DO_INLINE void mul_fvector_fmatrix(float *to, const float *from, const float matrix[3][3])
BLI_INLINE void init_fmatrix(fmatrix3x3 *matrix, int r, int c)
void SIM_mass_spring_add_constraint_ndof2(Implicit_Data *data, int index, const float c1[3], const float dV[3])
BLI_INLINE bool spring_length(Implicit_Data *data, int i, int j, float r_extent[3], float r_dir[3], float *r_length, float r_vel[3])
static int SIM_mass_spring_add_block(Implicit_Data *data, int v1, int v2)
BLI_INLINE void edge_avg(lfVector *data, int i, int j, float r_avg[3])
void SIM_mass_spring_force_drag(Implicit_Data *data, float drag)
DO_INLINE void subadd_fmatrixS_fmatrixS(float to[3][3], const float matrixA[3][3], float aS, const float matrixB[3][3], float bS)
DO_INLINE void zero_lfvector(float(*to)[3], uint verts)
BLI_INLINE void root_to_world_m3(Implicit_Data *data, int index, float r[3][3], const float m[3][3])
void SIM_mass_spring_solver_free(Implicit_Data *id)
BLI_INLINE void cross_v3_identity(float r[3][3], const float v[3])
DO_INLINE void mul_fmatrix_fvector(float *to, const float matrix[3][3], const float from[3])
bool SIM_mass_spring_force_spring_linear(Implicit_Data *data, int i, int j, float restlen, float stiffness_tension, float damping_tension, float stiffness_compression, float damping_compression, bool resist_compress, bool new_compress, float clamp_force)
BLI_INLINE void outerproduct(float r[3][3], const float a[3], const float b[3])
bool SIM_mass_spring_force_spring_bending(Implicit_Data *data, int i, int j, float restlen, float kb, float cb)
DO_INLINE void mul_fvector_S(float to[3], const float from[3], float scalar)
DO_INLINE void init_lfvector(float(*fLongVector)[3], const float vector[3], uint verts)
void SIM_mass_spring_set_motion_state(Implicit_Data *data, int index, const float x[3], const float v[3])
void SIM_mass_spring_set_position(Implicit_Data *data, int index, const float x[3])
DO_INLINE void mul_lfvectorS(float(*to)[3], float(*fLongVector)[3], float scalar, uint verts)
void SIM_mass_spring_get_motion_state(Implicit_Data *data, int index, float x[3], float v[3])
DO_INLINE void sub_fmatrix_fmatrix(float to[3][3], const float matrixA[3][3], const float matrixB[3][3])
BLI_INLINE void dfdx_spring(float to[3][3], const float dir[3], float length, float L, float k)
DO_INLINE void add_lfvector_lfvectorS(float(*to)[3], float(*fLongVectorA)[3], float(*fLongVectorB)[3], float bS, uint verts)
void SIM_mass_spring_get_position(Implicit_Data *data, int index, float x[3])
void SIM_mass_spring_set_rest_transform(Implicit_Data *data, int index, float tfm[3][3])
DO_INLINE void cp_lfvector(float(*to)[3], float(*from)[3], uint verts)
static int cg_filtered(lfVector *ldV, fmatrix3x3 *lA, lfVector *lB, lfVector *z, fmatrix3x3 *S, ImplicitSolverResult *result)
DO_INLINE void sub_lfvector_lfvectorS(float(*to)[3], float(*fLongVectorA)[3], float(*fLongVectorB)[3], float bS, uint verts)
DO_INLINE void cp_fmatrix(float to[3][3], const float from[3][3])
BLI_INLINE void poly_norm(lfVector *data, int i, int j, int *inds, int len, float r_dir[3])
DO_INLINE void initdiag_bfmatrix(fmatrix3x3 *matrix, float m3[3][3])
void SIM_mass_spring_force_edge_wind(Implicit_Data *data, int v1, int v2, float radius1, float radius2, const float(*winvec)[3])
DO_INLINE void cp_bfmatrix(fmatrix3x3 *to, fmatrix3x3 *from)
BLI_INLINE void spring_grad_dir(Implicit_Data *data, int i, int j, float edge[3], float dir[3], float grad_dir[3][3])
void SIM_mass_spring_force_face_wind(Implicit_Data *data, int v1, int v2, int v3, const float(*winvec)[3])
static float calc_nor_area_tri(float nor[3], const float v1[3], const float v2[3], const float v3[3])
DO_INLINE fmatrix3x3 * create_bfmatrix(uint verts, uint springs)
BLI_INLINE void root_to_world_v3(Implicit_Data *data, int index, float r[3], const float v[3])
BLI_INLINE void poly_avg(lfVector *data, const int *inds, int len, float r_avg[3])
DO_INLINE void subadd_bfmatrixS_bfmatrixS(fmatrix3x3 *to, fmatrix3x3 *from, float aS, fmatrix3x3 *matrix, float bS)
void SIM_mass_spring_add_constraint_ndof1(Implicit_Data *data, int index, const float c1[3], const float c2[3], const float dV[3])
BLI_INLINE void world_to_root_m3(Implicit_Data *data, int index, float r[3][3], const float m[3][3])
BLI_INLINE void edge_norm(lfVector *data, int i, int j, float r_dir[3])
void SIM_mass_spring_set_new_velocity(Implicit_Data *data, int index, const float v[3])
BLI_INLINE void spring_hairbend_estimate_dfdv(Implicit_Data *data, int i, int j, int k, const float goal[3], float stiffness, float damping, int q, float dfdv[3][3])
DO_INLINE void sub_lfvector_lfvector(float(*to)[3], float(*fLongVectorA)[3], float(*fLongVectorB)[3], uint verts)
BLI_INLINE float fbstar(float length, float L, float kb, float cb)
void SIM_mass_spring_get_new_velocity(Implicit_Data *data, int index, float v[3])
DO_INLINE void del_bfmatrix(fmatrix3x3 *matrix)
void SIM_mass_spring_force_reference_frame(Implicit_Data *data, int index, const float acceleration[3], const float omega[3], const float domega_dt[3], float mass)
float SIM_tri_tetra_volume_signed_6x(Implicit_Data *data, int v1, int v2, int v3)
bool SIM_mass_spring_force_spring_angular(Implicit_Data *data, int i, int j, int *i_a, int *i_b, int len_a, int len_b, float restang, float stiffness, float damping)
void SIM_mass_spring_set_new_position(Implicit_Data *data, int index, const float x[3])
BLI_INLINE void spring_hairbend_estimate_dfdx(Implicit_Data *data, int i, int j, int k, const float goal[3], float stiffness, float damping, int q, float dfdx[3][3])
BLI_INLINE void world_to_root_v3(Implicit_Data *data, int index, float r[3], const float v[3])
BLI_INLINE void dfdv_damp(float to[3][3], const float dir[3], float damping)
void SIM_mass_spring_get_new_position(Implicit_Data *data, int index, float x[3])
void SIM_mass_spring_apply_result(Implicit_Data *data)
BLI_INLINE float bend_angle(const float dir_a[3], const float dir_b[3], const float dir_e[3])
DO_INLINE void add_lfvectorS_lfvectorS(float(*to)[3], float(*fLongVectorA)[3], float aS, float(*fLongVectorB)[3], float bS, uint verts)
BLI_INLINE void spring_angle(Implicit_Data *data, int i, int j, int *i_a, int *i_b, int len_a, int len_b, float r_dir_a[3], float r_dir_b[3], float *r_angle, float r_vel_a[3], float r_vel_b[3])
DO_INLINE void init_bfmatrix(fmatrix3x3 *matrix, float m3[3][3])
#define CLOTH_PARALLEL_LIMIT
static void edge_wind_vertex(const float dir[3], float length, float radius, const float wind[3], float f[3], float[3][3], float[3][3])
DO_INLINE float dot_lfvector(float(*fLongVectorA)[3], float(*fLongVectorB)[3], uint verts)
void * MEM_calloc_arrayN(size_t len, size_t size, const char *str)
Definition mallocn.cc:123
void * MEM_callocN(size_t len, const char *str)
Definition mallocn.cc:118
void MEM_freeN(void *vmemh)
Definition mallocn.cc:113
#define M
#define N
#define L
void parallel_invoke(Functions &&...functions)
Definition BLI_task.hh:221
Value parallel_deterministic_reduce(IndexRange range, int64_t grain_size, const Value &identity, const Function &function, const Reduction &reduction)
Definition BLI_task.hh:194
void parallel_for(const IndexRange range, const int64_t grain_size, const Function &function, const TaskSizeHints &size_hints=detail::TaskSizeHints_Static(1))
Definition BLI_task.hh:93
#define I
#define sqrtf
#define atan2f
fmatrix3x3 * bigI
fmatrix3x3 * Pinv
fmatrix3x3 * dFdX
fmatrix3x3 * dFdV
float m[3][3]
i
Definition text_draw.cc:230
uint len
CCL_NAMESPACE_BEGIN struct Window V