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