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