Blender V4.3
implicit_eigen.cc
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2015-2023 Blender Authors
2 *
3 * SPDX-License-Identifier: GPL-2.0-or-later */
4
9#include "implicit.h"
10
11#ifdef IMPLICIT_SOLVER_EIGEN
12
13// #define USE_EIGEN_CORE
14# define USE_EIGEN_CONSTRAINED_CG
15
16# ifdef __GNUC__
17# pragma GCC diagnostic push
18/* XXX suppress verbose warnings in eigen */
19// # pragma GCC diagnostic ignored "-Wlogical-op"
20# endif
21
22# ifndef IMPLICIT_ENABLE_EIGEN_DEBUG
23# ifdef NDEBUG
24# define IMPLICIT_NDEBUG
25# endif
26# define NDEBUG
27# endif
28
29# include <Eigen/Sparse>
30# include <Eigen/src/Core/util/DisableStupidWarnings.h>
31
32# ifdef USE_EIGEN_CONSTRAINED_CG
34# endif
35
36# ifndef IMPLICIT_ENABLE_EIGEN_DEBUG
37# ifndef IMPLICIT_NDEBUG
38# undef NDEBUG
39# else
40# undef IMPLICIT_NDEBUG
41# endif
42# endif
43
44# ifdef __GNUC__
45# pragma GCC diagnostic pop
46# endif
47
48# include "MEM_guardedalloc.h"
49
50extern "C" {
52# include "DNA_object_types.h"
53# include "DNA_scene_types.h"
54# include "DNA_texture_types.h"
55
56# include "BLI_linklist.h"
57# include "BLI_utildefines.h"
58
59# include "BKE_cloth.hh"
60# include "BKE_collision.h"
61# include "BKE_effect.h"
62# include "BKE_global.hh"
63
64# include "SIM_mass_spring.h"
65}
66
67typedef float Scalar;
68
69static float I[3][3] = {{1, 0, 0}, {0, 1, 0}, {0, 0, 1}};
70
71/* slightly extended Eigen vector class
72 * with conversion to/from plain C float array
73 */
74class fVector : public Eigen::Vector3f {
75 public:
76 typedef float *ctype;
77
78 fVector() {}
79
80 fVector(const ctype &v)
81 {
82 for (int k = 0; k < 3; k++) {
83 coeffRef(k) = v[k];
84 }
85 }
86
87 fVector &operator=(const ctype &v)
88 {
89 for (int k = 0; k < 3; k++) {
90 coeffRef(k) = v[k];
91 }
92 return *this;
93 }
94
95 operator ctype()
96 {
97 return data();
98 }
99};
100
101/* slightly extended Eigen matrix class
102 * with conversion to/from plain C float array
103 */
104class fMatrix : public Eigen::Matrix3f {
105 public:
106 typedef float (*ctype)[3];
107
108 fMatrix() {}
109
110 fMatrix(const ctype &v)
111 {
112 for (int k = 0; k < 3; k++) {
113 for (int l = 0; l < 3; l++) {
114 coeffRef(l, k) = v[k][l];
115 }
116 }
117 }
118
119 fMatrix &operator=(const ctype &v)
120 {
121 for (int k = 0; k < 3; k++) {
122 for (int l = 0; l < 3; l++) {
123 coeffRef(l, k) = v[k][l];
124 }
125 }
126 return *this;
127 }
128
129 operator ctype()
130 {
131 return (ctype)data();
132 }
133};
134
135/* Extension of dense Eigen vectors,
136 * providing 3-float block access for blenlib math functions
137 */
138class lVector : public Eigen::VectorXf {
139 public:
140 typedef Eigen::VectorXf base_t;
141
142 lVector() {}
143
144 template<typename T> lVector &operator=(T rhs)
145 {
146 base_t::operator=(rhs);
147 return *this;
148 }
149
150 float *v3(int vertex)
151 {
152 return &coeffRef(3 * vertex);
153 }
154
155 const float *v3(int vertex) const
156 {
157 return &coeffRef(3 * vertex);
158 }
159};
160
161typedef Eigen::Triplet<Scalar> Triplet;
162typedef std::vector<Triplet> TripletList;
163
164typedef Eigen::SparseMatrix<Scalar> lMatrix;
165
166/* Constructor type that provides more convenient handling of Eigen triplets
167 * for efficient construction of sparse 3x3 block matrices.
168 * This should be used for building lMatrix instead of writing to such lMatrix directly (which is
169 * very inefficient). After all elements have been defined using the set() method, the actual
170 * matrix can be filled using construct().
171 */
172struct lMatrixCtor {
173 lMatrixCtor() {}
174
175 void reset()
176 {
177 m_trips.clear();
178 }
179
180 void reserve(int numverts)
181 {
182 /* reserve for diagonal entries */
183 m_trips.reserve(numverts * 9);
184 }
185
186 void add(int i, int j, const fMatrix &m)
187 {
188 i *= 3;
189 j *= 3;
190 for (int k = 0; k < 3; k++) {
191 for (int l = 0; l < 3; l++) {
192 m_trips.push_back(Triplet(i + k, j + l, m.coeff(l, k)));
193 }
194 }
195 }
196
197 void sub(int i, int j, const fMatrix &m)
198 {
199 i *= 3;
200 j *= 3;
201 for (int k = 0; k < 3; k++) {
202 for (int l = 0; l < 3; l++) {
203 m_trips.push_back(Triplet(i + k, j + l, -m.coeff(l, k)));
204 }
205 }
206 }
207
208 inline void construct(lMatrix &m)
209 {
210 m.setFromTriplets(m_trips.begin(), m_trips.end());
211 m_trips.clear();
212 }
213
214 private:
215 TripletList m_trips;
216};
217
218# ifdef USE_EIGEN_CORE
219typedef Eigen::ConjugateGradient<lMatrix, Eigen::Lower, Eigen::DiagonalPreconditioner<Scalar>>
221# endif
222# ifdef USE_EIGEN_CONSTRAINED_CG
224 Eigen::Lower,
225 lMatrix,
226 Eigen::DiagonalPreconditioner<Scalar>>
227 ConstraintConjGrad;
228# endif
229using Eigen::ComputationInfo;
230
231static void print_lvector(const lVector &v)
232{
233 for (int i = 0; i < v.rows(); i++) {
234 if (i > 0 && i % 3 == 0) {
235 printf("\n");
236 }
237
238 printf("%f,\n", v[i]);
239 }
240}
241
242static void print_lmatrix(const lMatrix &m)
243{
244 for (int j = 0; j < m.rows(); j++) {
245 if (j > 0 && j % 3 == 0) {
246 printf("\n");
247 }
248
249 for (int i = 0; i < m.cols(); i++) {
250 if (i > 0 && i % 3 == 0) {
251 printf(" ");
252 }
253
254 implicit_print_matrix_elem(m.coeff(j, i));
255 }
256 printf("\n");
257 }
258}
259
260BLI_INLINE void lMatrix_reserve_elems(lMatrix &m, int num)
261{
262 m.reserve(Eigen::VectorXi::Constant(m.cols(), num));
263}
264
265BLI_INLINE float *lVector_v3(lVector &v, int vertex)
266{
267 return v.data() + 3 * vertex;
268}
269
270BLI_INLINE const float *lVector_v3(const lVector &v, int vertex)
271{
272 return v.data() + 3 * vertex;
273}
274
275# if 0
276BLI_INLINE void triplets_m3(TripletList &tlist, float m[3][3], int i, int j)
277{
278 i *= 3;
279 j *= 3;
280 for (int l = 0; l < 3; l++) {
281 for (int k = 0; k < 3; k++) {
282 tlist.push_back(Triplet(i + k, j + l, m[k][l]));
283 }
284 }
285}
286
287BLI_INLINE void triplets_m3fl(TripletList &tlist, float m[3][3], int i, int j, float factor)
288{
289 i *= 3;
290 j *= 3;
291 for (int l = 0; l < 3; l++) {
292 for (int k = 0; k < 3; k++) {
293 tlist.push_back(Triplet(i + k, j + l, m[k][l] * factor));
294 }
295 }
296}
297
298BLI_INLINE void lMatrix_add_triplets(lMatrix &r, const TripletList &tlist)
299{
300 lMatrix t(r.rows(), r.cols());
301 t.setFromTriplets(tlist.begin(), tlist.end());
302 r += t;
303}
304
305BLI_INLINE void lMatrix_madd_triplets(lMatrix &r, const TripletList &tlist, float f)
306{
307 lMatrix t(r.rows(), r.cols());
308 t.setFromTriplets(tlist.begin(), tlist.end());
309 r += f * t;
310}
311
312BLI_INLINE void lMatrix_sub_triplets(lMatrix &r, const TripletList &tlist)
313{
314 lMatrix t(r.rows(), r.cols());
315 t.setFromTriplets(tlist.begin(), tlist.end());
316 r -= t;
317}
318# endif
319
320BLI_INLINE void outerproduct(float r[3][3], const float a[3], const float b[3])
321{
322 mul_v3_v3fl(r[0], a, b[0]);
323 mul_v3_v3fl(r[1], a, b[1]);
324 mul_v3_v3fl(r[2], a, b[2]);
325}
326
327BLI_INLINE void cross_m3_v3m3(float r[3][3], const float v[3], float m[3][3])
328{
329 cross_v3_v3v3(r[0], v, m[0]);
330 cross_v3_v3v3(r[1], v, m[1]);
331 cross_v3_v3v3(r[2], v, m[2]);
332}
333
334BLI_INLINE void cross_v3_identity(float r[3][3], const float v[3])
335{
336 r[0][0] = 0.0f;
337 r[1][0] = v[2];
338 r[2][0] = -v[1];
339 r[0][1] = -v[2];
340 r[1][1] = 0.0f;
341 r[2][1] = v[0];
342 r[0][2] = v[1];
343 r[1][2] = -v[0];
344 r[2][2] = 0.0f;
345}
346
347BLI_INLINE void madd_m3_m3fl(float r[3][3], float m[3][3], float f)
348{
349 r[0][0] += m[0][0] * f;
350 r[0][1] += m[0][1] * f;
351 r[0][2] += m[0][2] * f;
352 r[1][0] += m[1][0] * f;
353 r[1][1] += m[1][1] * f;
354 r[1][2] += m[1][2] * f;
355 r[2][0] += m[2][0] * f;
356 r[2][1] += m[2][1] * f;
357 r[2][2] += m[2][2] * f;
358}
359
360BLI_INLINE void madd_m3_m3m3fl(float r[3][3], float a[3][3], float b[3][3], float f)
361{
362 r[0][0] = a[0][0] + b[0][0] * f;
363 r[0][1] = a[0][1] + b[0][1] * f;
364 r[0][2] = a[0][2] + b[0][2] * f;
365 r[1][0] = a[1][0] + b[1][0] * f;
366 r[1][1] = a[1][1] + b[1][1] * f;
367 r[1][2] = a[1][2] + b[1][2] * f;
368 r[2][0] = a[2][0] + b[2][0] * f;
369 r[2][1] = a[2][1] + b[2][1] * f;
370 r[2][2] = a[2][2] + b[2][2] * f;
371}
372
373struct Implicit_Data {
374 typedef std::vector<fMatrix> fMatrixVector;
375
376 Implicit_Data(int numverts)
377 {
378 resize(numverts);
379 }
380
381 void resize(int numverts)
382 {
383 this->numverts = numverts;
384 int tot = 3 * numverts;
385
386 M.resize(tot, tot);
387 F.resize(tot);
388 dFdX.resize(tot, tot);
389 dFdV.resize(tot, tot);
390
391 tfm.resize(numverts, I);
392
393 X.resize(tot);
394 Xnew.resize(tot);
395 V.resize(tot);
396 Vnew.resize(tot);
397
398 A.resize(tot, tot);
399 B.resize(tot);
400
401 dV.resize(tot);
402 z.resize(tot);
403 S.resize(tot, tot);
404
405 iM.reserve(numverts);
406 idFdX.reserve(numverts);
407 idFdV.reserve(numverts);
408 iS.reserve(numverts);
409 }
410
411 int numverts;
412
413 /* inputs */
414 lMatrix M; /* masses */
415 lVector F; /* forces */
416 lMatrix dFdX, dFdV; /* force jacobians */
417
418 fMatrixVector tfm; /* local coordinate transform */
419
420 /* motion state data */
421 lVector X, Xnew; /* positions */
422 lVector V, Vnew; /* velocities */
423
424 /* internal solver data */
425 lVector B; /* B for A*dV = B */
426 lMatrix A; /* A for A*dV = B */
427
428 lVector dV; /* velocity change (solution of A*dV = B) */
429 lVector z; /* target velocity in constrained directions */
430 lMatrix S; /* filtering matrix for constraints */
431
432 /* temporary constructors */
433 lMatrixCtor iM; /* masses */
434 lMatrixCtor idFdX, idFdV; /* force jacobians */
435 lMatrixCtor iS; /* filtering matrix for constraints */
436};
437
438Implicit_Data *SIM_mass_spring_solver_create(int numverts, int numsprings)
439{
440 Implicit_Data *id = new Implicit_Data(numverts);
441 return id;
442}
443
445{
446 if (id) {
447 delete id;
448 }
449}
450
452{
453 if (id) {
454 return id->numverts;
455 }
456 else {
457 return 0;
458 }
459}
460
461/* ==== Transformation from/to root reference frames ==== */
462
463BLI_INLINE void world_to_root_v3(Implicit_Data *data, int index, float r[3], const float v[3])
464{
465 copy_v3_v3(r, v);
466 mul_transposed_m3_v3(data->tfm[index], r);
467}
468
469BLI_INLINE void root_to_world_v3(Implicit_Data *data, int index, float r[3], const float v[3])
470{
471 mul_v3_m3v3(r, data->tfm[index], v);
472}
473
474BLI_INLINE void world_to_root_m3(Implicit_Data *data, int index, float r[3][3], float m[3][3])
475{
476 float trot[3][3];
477 copy_m3_m3(trot, data->tfm[index]);
478 transpose_m3(trot);
479 mul_m3_m3m3(r, trot, m);
480}
481
482BLI_INLINE void root_to_world_m3(Implicit_Data *data, int index, float r[3][3], float m[3][3])
483{
484 mul_m3_m3m3(r, data->tfm[index], m);
485}
486
487/* ================================ */
488
490{
491# ifdef USE_EIGEN_CORE
492 typedef ConjugateGradient solver_t;
493# endif
494# ifdef USE_EIGEN_CONSTRAINED_CG
495 typedef ConstraintConjGrad solver_t;
496# endif
497
498 data->iM.construct(data->M);
499 data->idFdX.construct(data->dFdX);
500 data->idFdV.construct(data->dFdV);
501 data->iS.construct(data->S);
502
503 solver_t cg;
504 cg.setMaxIterations(100);
505 cg.setTolerance(0.01f);
506
507# ifdef USE_EIGEN_CONSTRAINED_CG
508 cg.filter() = data->S;
509# endif
510
511 data->A = data->M - dt * data->dFdV - dt * dt * data->dFdX;
512 cg.compute(data->A);
513
514 data->B = dt * data->F + dt * dt * data->dFdX * data->V;
515
516# ifdef IMPLICIT_PRINT_SOLVER_INPUT_OUTPUT
517 printf("==== A ====\n");
518 print_lmatrix(id->A);
519 printf("==== z ====\n");
520 print_lvector(id->z);
521 printf("==== B ====\n");
522 print_lvector(id->B);
523 printf("==== S ====\n");
524 print_lmatrix(id->S);
525# endif
526
527# ifdef USE_EIGEN_CORE
528 data->dV = cg.solve(data->B);
529# endif
530# ifdef USE_EIGEN_CONSTRAINED_CG
531 data->dV = cg.solveWithGuess(data->B, data->z);
532# endif
533
534# ifdef IMPLICIT_PRINT_SOLVER_INPUT_OUTPUT
535 printf("==== dV ====\n");
536 print_lvector(id->dV);
537 printf("========\n");
538# endif
539
540 data->Vnew = data->V + data->dV;
541
542 switch (cg.info()) {
543 case Eigen::Success:
544 result->status = SIM_SOLVER_SUCCESS;
545 break;
546 case Eigen::NoConvergence:
547 result->status = SIM_SOLVER_NO_CONVERGENCE;
548 break;
549 case Eigen::InvalidInput:
550 result->status = SIM_SOLVER_INVALID_INPUT;
551 break;
552 case Eigen::NumericalIssue:
553 result->status = SIM_SOLVER_NUMERICAL_ISSUE;
554 break;
555 }
556
557 result->iterations = cg.iterations();
558 result->error = cg.error();
559
560 return cg.info() == Eigen::Success;
561}
562
564{
565 data->Xnew = data->X + data->Vnew * dt;
566 return true;
567}
568
569/* ================================ */
570
572{
573 data->X = data->Xnew;
574 data->V = data->Vnew;
575}
576
577void SIM_mass_spring_set_vertex_mass(Implicit_Data *data, int index, float mass)
578{
579 float m[3][3];
580 copy_m3_m3(m, I);
581 mul_m3_fl(m, mass);
582 data->iM.add(index, index, m);
583}
584
585void SIM_mass_spring_set_rest_transform(Implicit_Data *data, int index, float tfm[3][3])
586{
587# ifdef CLOTH_ROOT_FRAME
588 copy_m3_m3(data->tfm[index], tfm);
589# else
590 unit_m3(data->tfm[index]);
591 (void)tfm;
592# endif
593}
594
596 int index,
597 const float x[3],
598 const float v[3])
599{
600 world_to_root_v3(data, index, data->X.v3(index), x);
601 world_to_root_v3(data, index, data->V.v3(index), v);
602}
603
604void SIM_mass_spring_set_position(Implicit_Data *data, int index, const float x[3])
605{
606 world_to_root_v3(data, index, data->X.v3(index), x);
607}
608
609void SIM_mass_spring_set_velocity(Implicit_Data *data, int index, const float v[3])
610{
611 world_to_root_v3(data, index, data->V.v3(index), v);
612}
613
615 int index,
616 float x[3],
617 float v[3])
618{
619 if (x) {
620 root_to_world_v3(data, index, x, data->X.v3(index));
621 }
622 if (v) {
623 root_to_world_v3(data, index, v, data->V.v3(index));
624 }
625}
626
627void SIM_mass_spring_get_position(struct Implicit_Data *data, int index, float x[3])
628{
629 root_to_world_v3(data, index, x, data->X.v3(index));
630}
631
632void SIM_mass_spring_get_new_velocity(Implicit_Data *data, int index, float v[3])
633{
634 root_to_world_v3(data, index, v, data->V.v3(index));
635}
636
637void SIM_mass_spring_set_new_velocity(Implicit_Data *data, int index, const float v[3])
638{
639 world_to_root_v3(data, index, data->V.v3(index), v);
640}
641
643{
644 int numverts = data->numverts;
645 for (int i = 0; i < numverts; i++) {
646 data->iS.add(i, i, I);
647 zero_v3(data->z.v3(i));
648 }
649}
650
651void SIM_mass_spring_add_constraint_ndof0(Implicit_Data *data, int index, const float dV[3])
652{
653 data->iS.sub(index, index, I);
654
655 world_to_root_v3(data, index, data->z.v3(index), dV);
656}
657
659 Implicit_Data *data, int index, const float c1[3], const float c2[3], const float dV[3])
660{
661 float m[3][3], p[3], q[3], u[3], cmat[3][3];
662
663 world_to_root_v3(data, index, p, c1);
664 outerproduct(cmat, p, p);
665 copy_m3_m3(m, cmat);
666
667 world_to_root_v3(data, index, q, c2);
668 outerproduct(cmat, q, q);
669 add_m3_m3m3(m, m, cmat);
670
671 /* XXX not sure but multiplication should work here */
672 data->iS.sub(index, index, m);
673 // mul_m3_m3m3(data->S[index].m, data->S[index].m, m);
674
675 world_to_root_v3(data, index, u, dV);
676 add_v3_v3(data->z.v3(index), u);
677}
678
680 int index,
681 const float c1[3],
682 const float dV[3])
683{
684 float m[3][3], p[3], u[3], cmat[3][3];
685
686 world_to_root_v3(data, index, p, c1);
687 outerproduct(cmat, p, p);
688 copy_m3_m3(m, cmat);
689
690 data->iS.sub(index, index, m);
691 // mul_m3_m3m3(data->S[index].m, data->S[index].m, m);
692
693 world_to_root_v3(data, index, u, dV);
694 add_v3_v3(data->z.v3(index), u);
695}
696
698{
699 data->F.setZero();
700 data->dFdX.setZero();
701 data->dFdV.setZero();
702}
703
705 int index,
706 const float acceleration[3],
707 const float omega[3],
708 const float domega_dt[3],
709 float mass)
710{
711# ifdef CLOTH_ROOT_FRAME
712 float acc[3], w[3], dwdt[3];
713 float f[3], dfdx[3][3], dfdv[3][3];
714 float euler[3], coriolis[3], centrifugal[3], rotvel[3];
715 float deuler[3][3], dcoriolis[3][3], dcentrifugal[3][3], drotvel[3][3];
716
717 world_to_root_v3(data, index, acc, acceleration);
718 world_to_root_v3(data, index, w, omega);
719 world_to_root_v3(data, index, dwdt, domega_dt);
720
721 cross_v3_v3v3(euler, dwdt, data->X.v3(index));
722 cross_v3_v3v3(coriolis, w, data->V.v3(index));
723 mul_v3_fl(coriolis, 2.0f);
724 cross_v3_v3v3(rotvel, w, data->X.v3(index));
725 cross_v3_v3v3(centrifugal, w, rotvel);
726
727 sub_v3_v3v3(f, acc, euler);
728 sub_v3_v3(f, coriolis);
729 sub_v3_v3(f, centrifugal);
730
731 mul_v3_fl(f, mass); /* F = m * a */
732
733 cross_v3_identity(deuler, dwdt);
734 cross_v3_identity(dcoriolis, w);
735 mul_m3_fl(dcoriolis, 2.0f);
736 cross_v3_identity(drotvel, w);
737 cross_m3_v3m3(dcentrifugal, w, drotvel);
738
739 add_m3_m3m3(dfdx, deuler, dcentrifugal);
740 negate_m3(dfdx);
741 mul_m3_fl(dfdx, mass);
742
743 copy_m3_m3(dfdv, dcoriolis);
744 negate_m3(dfdv);
745 mul_m3_fl(dfdv, mass);
746
747 add_v3_v3(data->F.v3(index), f);
748 data->idFdX.add(index, index, dfdx);
749 data->idFdV.add(index, index, dfdv);
750# else
751 (void)data;
752 (void)index;
753 (void)acceleration;
754 (void)omega;
755 (void)domega_dt;
756# endif
757}
758
759void SIM_mass_spring_force_gravity(Implicit_Data *data, int index, float mass, const float g[3])
760{
761 /* force = mass * acceleration (in this case: gravity) */
762 float f[3];
763 world_to_root_v3(data, index, f, g);
764 mul_v3_fl(f, mass);
765
766 add_v3_v3(data->F.v3(index), f);
767}
768
769void SIM_mass_spring_force_drag(Implicit_Data *data, float drag)
770{
771 int numverts = data->numverts;
772 for (int i = 0; i < numverts; i++) {
773 float tmp[3][3];
774
775 /* NOTE: Uses root space velocity, no need to transform. */
776 madd_v3_v3fl(data->F.v3(i), data->V.v3(i), -drag);
777
778 copy_m3_m3(tmp, I);
779 mul_m3_fl(tmp, -drag);
780 data->idFdV.add(i, i, tmp);
781 }
782}
783
785 struct Implicit_Data *data, int i, const float f[3], float dfdx[3][3], float dfdv[3][3])
786{
787 float tf[3], tdfdx[3][3], tdfdv[3][3];
788 world_to_root_v3(data, i, tf, f);
789 world_to_root_m3(data, i, tdfdx, dfdx);
790 world_to_root_m3(data, i, tdfdv, dfdv);
791
792 add_v3_v3(data->F.v3(i), tf);
793 data->idFdX.add(i, i, tdfdx);
794 data->idFdV.add(i, i, tdfdv);
795}
796
797static float calc_nor_area_tri(float nor[3],
798 const float v1[3],
799 const float v2[3],
800 const float v3[3])
801{
802 float n1[3], n2[3];
803
804 sub_v3_v3v3(n1, v1, v2);
805 sub_v3_v3v3(n2, v2, v3);
806
807 cross_v3_v3v3(nor, n1, n2);
808 return normalize_v3(nor) / 2.0f;
809}
810
811/* XXX does not support force jacobians yet,
812 * since the effector system does not provide them either. */
814 Implicit_Data *data, int v1, int v2, int v3, const float (*winvec)[3])
815{
816 const float effector_scale = 0.02f;
817 float win[3], nor[3], area;
818 float factor;
819
820 /* calculate face normal and area */
821 area = calc_nor_area_tri(nor, data->X.v3(v1), data->X.v3(v2), data->X.v3(v3));
822 factor = effector_scale * area / 3.0f;
823
824 world_to_root_v3(data, v1, win, winvec[v1]);
825 madd_v3_v3fl(data->F.v3(v1), nor, factor * dot_v3v3(win, nor));
826
827 world_to_root_v3(data, v2, win, winvec[v2]);
828 madd_v3_v3fl(data->F.v3(v2), nor, factor * dot_v3v3(win, nor));
829
830 world_to_root_v3(data, v3, win, winvec[v3]);
831 madd_v3_v3fl(data->F.v3(v3), nor, factor * dot_v3v3(win, nor));
832}
833
834void SIM_mass_spring_force_edge_wind(Implicit_Data *data, int v1, int v2, const float (*winvec)[3])
835{
836 const float effector_scale = 0.01;
837 float win[3], dir[3], nor[3], length;
838
839 sub_v3_v3v3(dir, data->X.v3(v1), data->X.v3(v2));
840 length = normalize_v3(dir);
841
842 world_to_root_v3(data, v1, win, winvec[v1]);
843 madd_v3_v3v3fl(nor, win, dir, -dot_v3v3(win, dir));
844 madd_v3_v3fl(data->F.v3(v1), nor, effector_scale * length);
845
846 world_to_root_v3(data, v2, win, winvec[v2]);
847 madd_v3_v3v3fl(nor, win, dir, -dot_v3v3(win, dir));
848 madd_v3_v3fl(data->F.v3(v2), nor, effector_scale * length);
849}
850
851BLI_INLINE void dfdx_spring(float to[3][3], const float dir[3], float length, float L, float k)
852{
853 /* dir is unit length direction, rest is spring's restlength, k is spring constant. */
854 // return ((I - outerprod(dir, dir)) * Min(1.0f, rest / length) - I) * -k;
855 outerproduct(to, dir, dir);
856 sub_m3_m3m3(to, I, to);
857
858 mul_m3_fl(to, (L / length));
859 sub_m3_m3m3(to, to, I);
860 mul_m3_fl(to, k);
861}
862
863/* unused */
864# if 0
865BLI_INLINE void dfdx_damp(float to[3][3],
866 const float dir[3],
867 float length,
868 const float vel[3],
869 float rest,
870 float damping)
871{
872 /* Inner spring damping vel is the relative velocity of the endpoints. */
873 // return (I - outerprod(dir, dir)) * (-damping * -(dot(dir, vel) / Max(length, rest)));
874 mul_fvectorT_fvector(to, dir, dir);
875 sub_fmatrix_fmatrix(to, I, to);
876 mul_fmatrix_S(to, (-damping * -(dot_v3v3(dir, vel) / std::max(length, rest))));
877}
878# endif
879
880BLI_INLINE void dfdv_damp(float to[3][3], const float dir[3], float damping)
881{
882 /* Derivative of force with regards to velocity. */
883 outerproduct(to, dir, dir);
884 mul_m3_fl(to, -damping);
885}
886
887BLI_INLINE float fb(float length, float L)
888{
889 float x = length / L;
890 return (-11.541f * powf(x, 4) + 34.193f * powf(x, 3) - 39.083f * powf(x, 2) + 23.116f * x -
891 9.713f);
892}
893
894BLI_INLINE float fbderiv(float length, float L)
895{
896 float x = length / L;
897
898 return (-46.164f * powf(x, 3) + 102.579f * powf(x, 2) - 78.166f * x + 23.116f);
899}
900
901BLI_INLINE float fbstar(float length, float L, float kb, float cb)
902{
903 float tempfb_fl = kb * fb(length, L);
904 float fbstar_fl = cb * (length - L);
905
906 if (tempfb_fl < fbstar_fl) {
907 return fbstar_fl;
908 }
909 else {
910 return tempfb_fl;
911 }
912}
913
914/* Function to calculate bending spring force (taken from Choi & Co). */
915BLI_INLINE float fbstar_jacobi(float length, float L, float kb, float cb)
916{
917 float tempfb_fl = kb * fb(length, L);
918 float fbstar_fl = cb * (length - L);
919
920 if (tempfb_fl < fbstar_fl) {
921 return -cb;
922 }
923 else {
924 return -kb * fbderiv(length, L);
925 }
926}
927
928/* calculate elongation */
930 int i,
931 int j,
932 float r_extent[3],
933 float r_dir[3],
934 float *r_length,
935 float r_vel[3])
936{
937 sub_v3_v3v3(r_extent, data->X.v3(j), data->X.v3(i));
938 sub_v3_v3v3(r_vel, data->V.v3(j), data->V.v3(i));
939 *r_length = len_v3(r_extent);
940
941 if (*r_length > ALMOST_ZERO) {
942# if 0
943 if (length > L) {
944 if ((clmd->sim_parms->flags & CSIMSETT_FLAG_TEARING_ENABLED) &&
945 (((length - L) * 100.0f / L) > clmd->sim_parms->maxspringlen))
946 {
947 /* cut spring! */
948 s->flags |= CSPRING_FLAG_DEACTIVATE;
949 return false;
950 }
951 }
952# endif
953 mul_v3_v3fl(r_dir, r_extent, 1.0f / (*r_length));
954 }
955 else {
956 zero_v3(r_dir);
957 }
958
959 return true;
960}
961
963 Implicit_Data *data, int i, int j, const float f[3], float dfdx[3][3], float dfdv[3][3])
964{
965 add_v3_v3(data->F.v3(i), f);
966 sub_v3_v3(data->F.v3(j), f);
967
968 data->idFdX.add(i, i, dfdx);
969 data->idFdX.add(j, j, dfdx);
970 data->idFdX.sub(i, j, dfdx);
971 data->idFdX.sub(j, i, dfdx);
972
973 data->idFdV.add(i, i, dfdv);
974 data->idFdV.add(j, j, dfdv);
975 data->idFdV.sub(i, j, dfdv);
976 data->idFdV.sub(j, i, dfdv);
977}
978
980 int i,
981 int j,
982 float restlen,
983 float stiffness,
984 float damping,
985 bool no_compress,
986 float clamp_force,
987 float r_f[3],
988 float r_dfdx[3][3],
989 float r_dfdv[3][3])
990{
991 float extent[3], length, dir[3], vel[3];
992
993 /* calculate elongation */
994 spring_length(data, i, j, extent, dir, &length, vel);
995
996 if (length > restlen || no_compress) {
997 float stretch_force, f[3], dfdx[3][3], dfdv[3][3];
998
999 stretch_force = stiffness * (length - restlen);
1000 if (clamp_force > 0.0f && stretch_force > clamp_force) {
1001 stretch_force = clamp_force;
1002 }
1003 mul_v3_v3fl(f, dir, stretch_force);
1004
1005 /* Ascher & Boxman, p.21: Damping only during elongation
1006 * something wrong with it... */
1007 madd_v3_v3fl(f, dir, damping * dot_v3v3(vel, dir));
1008
1009 dfdx_spring(dfdx, dir, length, restlen, stiffness);
1010 dfdv_damp(dfdv, dir, damping);
1011
1012 apply_spring(data, i, j, f, dfdx, dfdv);
1013
1014 if (r_f) {
1015 copy_v3_v3(r_f, f);
1016 }
1017 if (r_dfdx) {
1018 copy_m3_m3(r_dfdx, dfdx);
1019 }
1020 if (r_dfdv) {
1021 copy_m3_m3(r_dfdv, dfdv);
1022 }
1023
1024 return true;
1025 }
1026 else {
1027 if (r_f) {
1028 zero_v3(r_f);
1029 }
1030 if (r_dfdx) {
1031 zero_m3(r_dfdx);
1032 }
1033 if (r_dfdv) {
1034 zero_m3(r_dfdv);
1035 }
1036
1037 return false;
1038 }
1039}
1040
1041/* See "Stable but Responsive Cloth" (Choi, Ko 2005) */
1043 int i,
1044 int j,
1045 float restlen,
1046 float kb,
1047 float cb,
1048 float r_f[3],
1049 float r_dfdx[3][3],
1050 float r_dfdv[3][3])
1051{
1052 float extent[3], length, dir[3], vel[3];
1053
1054 /* calculate elongation */
1055 spring_length(data, i, j, extent, dir, &length, vel);
1056
1057 if (length < restlen) {
1058 float f[3], dfdx[3][3], dfdv[3][3];
1059
1060 mul_v3_v3fl(f, dir, fbstar(length, restlen, kb, cb));
1061
1062 outerproduct(dfdx, dir, dir);
1063 mul_m3_fl(dfdx, fbstar_jacobi(length, restlen, kb, cb));
1064
1065 /* XXX damping not supported */
1066 zero_m3(dfdv);
1067
1068 apply_spring(data, i, j, f, dfdx, dfdv);
1069
1070 if (r_f) {
1071 copy_v3_v3(r_f, f);
1072 }
1073 if (r_dfdx) {
1074 copy_m3_m3(r_dfdx, dfdx);
1075 }
1076 if (r_dfdv) {
1077 copy_m3_m3(r_dfdv, dfdv);
1078 }
1079
1080 return true;
1081 }
1082 else {
1083 if (r_f) {
1084 zero_v3(r_f);
1085 }
1086 if (r_dfdx) {
1087 zero_m3(r_dfdx);
1088 }
1089 if (r_dfdv) {
1090 zero_m3(r_dfdv);
1091 }
1092
1093 return false;
1094 }
1095}
1096
1097/* Jacobian of a direction vector.
1098 * Basically the part of the differential orthogonal to the direction,
1099 * inversely proportional to the length of the edge.
1100 *
1101 * dD_ij/dx_i = -dD_ij/dx_j = (D_ij * D_ij^T - I) / len_ij
1102 */
1104 Implicit_Data *data, int i, int j, float edge[3], float dir[3], float grad_dir[3][3])
1105{
1106 float length;
1107
1108 sub_v3_v3v3(edge, data->X.v3(j), data->X.v3(i));
1109 length = normalize_v3_v3(dir, edge);
1110
1111 if (length > ALMOST_ZERO) {
1112 outerproduct(grad_dir, dir, dir);
1113 sub_m3_m3m3(grad_dir, I, grad_dir);
1114 mul_m3_fl(grad_dir, 1.0f / length);
1115 }
1116 else {
1117 zero_m3(grad_dir);
1118 }
1119}
1120
1121BLI_INLINE void spring_angbend_forces(Implicit_Data *data,
1122 int i,
1123 int j,
1124 int k,
1125 const float goal[3],
1126 float stiffness,
1127 float damping,
1128 int q,
1129 const float dx[3],
1130 const float dv[3],
1131 float r_f[3])
1132{
1133 float edge_ij[3], dir_ij[3];
1134 float edge_jk[3], dir_jk[3];
1135 float vel_ij[3], vel_jk[3], vel_ortho[3];
1136 float f_bend[3], f_damp[3];
1137 float fk[3];
1138 float dist[3];
1139
1140 zero_v3(fk);
1141
1142 sub_v3_v3v3(edge_ij, data->X.v3(j), data->X.v3(i));
1143 if (q == i) {
1144 sub_v3_v3(edge_ij, dx);
1145 }
1146 if (q == j) {
1147 add_v3_v3(edge_ij, dx);
1148 }
1149 normalize_v3_v3(dir_ij, edge_ij);
1150
1151 sub_v3_v3v3(edge_jk, data->X.v3(k), data->X.v3(j));
1152 if (q == j) {
1153 sub_v3_v3(edge_jk, dx);
1154 }
1155 if (q == k) {
1156 add_v3_v3(edge_jk, dx);
1157 }
1158 normalize_v3_v3(dir_jk, edge_jk);
1159
1160 sub_v3_v3v3(vel_ij, data->V.v3(j), data->V.v3(i));
1161 if (q == i) {
1162 sub_v3_v3(vel_ij, dv);
1163 }
1164 if (q == j) {
1165 add_v3_v3(vel_ij, dv);
1166 }
1167
1168 sub_v3_v3v3(vel_jk, data->V.v3(k), data->V.v3(j));
1169 if (q == j) {
1170 sub_v3_v3(vel_jk, dv);
1171 }
1172 if (q == k) {
1173 add_v3_v3(vel_jk, dv);
1174 }
1175
1176 /* bending force */
1177 sub_v3_v3v3(dist, goal, edge_jk);
1178 mul_v3_v3fl(f_bend, dist, stiffness);
1179
1180 add_v3_v3(fk, f_bend);
1181
1182 /* damping force */
1183 madd_v3_v3v3fl(vel_ortho, vel_jk, dir_jk, -dot_v3v3(vel_jk, dir_jk));
1184 mul_v3_v3fl(f_damp, vel_ortho, damping);
1185
1186 sub_v3_v3(fk, f_damp);
1187
1188 copy_v3_v3(r_f, fk);
1189}
1190
1191/* Finite Differences method for estimating the jacobian of the force */
1192BLI_INLINE void spring_angbend_estimate_dfdx(Implicit_Data *data,
1193 int i,
1194 int j,
1195 int k,
1196 const float goal[3],
1197 float stiffness,
1198 float damping,
1199 int q,
1200 float dfdx[3][3])
1201{
1202 const float delta = 0.00001f; /* TODO: find a good heuristic for this. */
1203 float dvec_null[3][3], dvec_pos[3][3], dvec_neg[3][3];
1204 float f[3];
1205 int a, b;
1206
1207 zero_m3(dvec_null);
1208 unit_m3(dvec_pos);
1209 mul_m3_fl(dvec_pos, delta * 0.5f);
1210 copy_m3_m3(dvec_neg, dvec_pos);
1211 negate_m3(dvec_neg);
1212
1213 /* XXX TODO: offset targets to account for position dependency. */
1214
1215 for (a = 0; a < 3; a++) {
1216 spring_angbend_forces(
1217 data, i, j, k, goal, stiffness, damping, q, dvec_pos[a], dvec_null[a], f);
1218 copy_v3_v3(dfdx[a], f);
1219
1220 spring_angbend_forces(
1221 data, i, j, k, goal, stiffness, damping, q, dvec_neg[a], dvec_null[a], f);
1222 sub_v3_v3(dfdx[a], f);
1223
1224 for (b = 0; b < 3; b++) {
1225 dfdx[a][b] /= delta;
1226 }
1227 }
1228}
1229
1230/* Finite Differences method for estimating the jacobian of the force */
1231BLI_INLINE void spring_angbend_estimate_dfdv(Implicit_Data *data,
1232 int i,
1233 int j,
1234 int k,
1235 const float goal[3],
1236 float stiffness,
1237 float damping,
1238 int q,
1239 float dfdv[3][3])
1240{
1241 const float delta = 0.00001f; /* TODO: find a good heuristic for this. */
1242 float dvec_null[3][3], dvec_pos[3][3], dvec_neg[3][3];
1243 float f[3];
1244 int a, b;
1245
1246 zero_m3(dvec_null);
1247 unit_m3(dvec_pos);
1248 mul_m3_fl(dvec_pos, delta * 0.5f);
1249 copy_m3_m3(dvec_neg, dvec_pos);
1250 negate_m3(dvec_neg);
1251
1252 /* XXX TODO: offset targets to account for position dependency. */
1253
1254 for (a = 0; a < 3; a++) {
1255 spring_angbend_forces(
1256 data, i, j, k, goal, stiffness, damping, q, dvec_null[a], dvec_pos[a], f);
1257 copy_v3_v3(dfdv[a], f);
1258
1259 spring_angbend_forces(
1260 data, i, j, k, goal, stiffness, damping, q, dvec_null[a], dvec_neg[a], f);
1261 sub_v3_v3(dfdv[a], f);
1262
1263 for (b = 0; b < 3; b++) {
1264 dfdv[a][b] /= delta;
1265 }
1266 }
1267}
1268
1269/* Angular spring that pulls the vertex toward the local target
1270 * See "Artistic Simulation of Curly Hair" (Pixar technical memo #12-03a)
1271 */
1272bool SIM_mass_spring_force_spring_bending_angular(Implicit_Data *data,
1273 int i,
1274 int j,
1275 int k,
1276 const float target[3],
1277 float stiffness,
1278 float damping)
1279{
1280 float goal[3];
1281 float fj[3], fk[3];
1282 float dfj_dxi[3][3], dfj_dxj[3][3], dfk_dxi[3][3], dfk_dxj[3][3], dfk_dxk[3][3];
1283 float dfj_dvi[3][3], dfj_dvj[3][3], dfk_dvi[3][3], dfk_dvj[3][3], dfk_dvk[3][3];
1284
1285 const float vecnull[3] = {0.0f, 0.0f, 0.0f};
1286
1287 world_to_root_v3(data, j, goal, target);
1288
1289 spring_angbend_forces(data, i, j, k, goal, stiffness, damping, k, vecnull, vecnull, fk);
1290 negate_v3_v3(fj, fk); /* counterforce */
1291
1292 spring_angbend_estimate_dfdx(data, i, j, k, goal, stiffness, damping, i, dfk_dxi);
1293 spring_angbend_estimate_dfdx(data, i, j, k, goal, stiffness, damping, j, dfk_dxj);
1294 spring_angbend_estimate_dfdx(data, i, j, k, goal, stiffness, damping, k, dfk_dxk);
1295 copy_m3_m3(dfj_dxi, dfk_dxi);
1296 negate_m3(dfj_dxi);
1297 copy_m3_m3(dfj_dxj, dfk_dxj);
1298 negate_m3(dfj_dxj);
1299
1300 spring_angbend_estimate_dfdv(data, i, j, k, goal, stiffness, damping, i, dfk_dvi);
1301 spring_angbend_estimate_dfdv(data, i, j, k, goal, stiffness, damping, j, dfk_dvj);
1302 spring_angbend_estimate_dfdv(data, i, j, k, goal, stiffness, damping, k, dfk_dvk);
1303 copy_m3_m3(dfj_dvi, dfk_dvi);
1304 negate_m3(dfj_dvi);
1305 copy_m3_m3(dfj_dvj, dfk_dvj);
1306 negate_m3(dfj_dvj);
1307
1308 /* add forces and jacobians to the solver data */
1309
1310 add_v3_v3(data->F.v3(j), fj);
1311 add_v3_v3(data->F.v3(k), fk);
1312
1313 data->idFdX.add(j, j, dfj_dxj);
1314 data->idFdX.add(k, k, dfk_dxk);
1315
1316 data->idFdX.add(i, j, dfj_dxi);
1317 data->idFdX.add(j, i, dfj_dxi);
1318 data->idFdX.add(j, k, dfk_dxj);
1319 data->idFdX.add(k, j, dfk_dxj);
1320 data->idFdX.add(i, k, dfk_dxi);
1321 data->idFdX.add(k, i, dfk_dxi);
1322
1323 data->idFdV.add(j, j, dfj_dvj);
1324 data->idFdV.add(k, k, dfk_dvk);
1325
1326 data->idFdV.add(i, j, dfj_dvi);
1327 data->idFdV.add(j, i, dfj_dvi);
1328 data->idFdV.add(j, k, dfk_dvj);
1329 data->idFdV.add(k, j, dfk_dvj);
1330 data->idFdV.add(i, k, dfk_dvi);
1331 data->idFdV.add(k, i, dfk_dvi);
1332
1333 /* XXX analytical calculation of derivatives below is incorrect.
1334 * This proved to be difficult, but for now just using the finite difference method for
1335 * estimating the jacobians should be sufficient.
1336 */
1337# if 0
1338 float edge_ij[3], dir_ij[3], grad_dir_ij[3][3];
1339 float edge_jk[3], dir_jk[3], grad_dir_jk[3][3];
1340 float dist[3], vel_jk[3], vel_jk_ortho[3], projvel[3];
1341 float target[3];
1342 float tmp[3][3];
1343 float fi[3], fj[3], fk[3];
1344 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];
1345 float dfdvi[3][3];
1346
1347 /* TESTING */
1348 damping = 0.0f;
1349
1350 zero_v3(fi);
1351 zero_v3(fj);
1352 zero_v3(fk);
1353 zero_m3(dfi_dxi);
1354 zero_m3(dfj_dxi);
1355 zero_m3(dfk_dxi);
1356 zero_m3(dfk_dxj);
1357 zero_m3(dfk_dxk);
1358
1359 /* jacobian of direction vectors */
1360 spring_grad_dir(data, i, j, edge_ij, dir_ij, grad_dir_ij);
1361 spring_grad_dir(data, j, k, edge_jk, dir_jk, grad_dir_jk);
1362
1363 sub_v3_v3v3(vel_jk, data->V[k], data->V[j]);
1364
1365 /* bending force */
1366 mul_v3_v3fl(target, dir_ij, restlen);
1367 sub_v3_v3v3(dist, target, edge_jk);
1368 mul_v3_v3fl(fk, dist, stiffness);
1369
1370 /* damping force */
1371 madd_v3_v3v3fl(vel_jk_ortho, vel_jk, dir_jk, -dot_v3v3(vel_jk, dir_jk));
1372 madd_v3_v3fl(fk, vel_jk_ortho, damping);
1373
1374 /* XXX this only holds true as long as we assume straight rest shape!
1375 * eventually will become a bit more involved since the opposite segment
1376 * gets its own target, under condition of having equal torque on both sides.
1377 */
1378 copy_v3_v3(fi, fk);
1379
1380 /* counterforce on the middle point */
1381 sub_v3_v3(fj, fi);
1382 sub_v3_v3(fj, fk);
1383
1384 /* === derivatives === */
1385
1386 madd_m3_m3fl(dfk_dxi, grad_dir_ij, stiffness * restlen);
1387
1388 madd_m3_m3fl(dfk_dxj, grad_dir_ij, -stiffness * restlen);
1389 madd_m3_m3fl(dfk_dxj, I, stiffness);
1390
1391 madd_m3_m3fl(dfk_dxk, I, -stiffness);
1392
1393 copy_m3_m3(dfi_dxi, dfk_dxk);
1394 negate_m3(dfi_dxi);
1395
1396 /* dfj_dfi == dfi_dfj due to symmetry,
1397 * dfi_dfj == dfk_dfj due to fi == fk
1398 * XXX see comment above on future bent rest shapes
1399 */
1400 copy_m3_m3(dfj_dxi, dfk_dxj);
1401
1402 /* dfj_dxj == -(dfi_dxj + dfk_dxj) due to fj == -(fi + fk) */
1403 sub_m3_m3m3(dfj_dxj, dfj_dxj, dfj_dxi);
1404 sub_m3_m3m3(dfj_dxj, dfj_dxj, dfk_dxj);
1405
1406 /* add forces and jacobians to the solver data */
1407 add_v3_v3(data->F[i], fi);
1408 add_v3_v3(data->F[j], fj);
1409 add_v3_v3(data->F[k], fk);
1410
1411 add_m3_m3m3(data->dFdX[i].m, data->dFdX[i].m, dfi_dxi);
1412 add_m3_m3m3(data->dFdX[j].m, data->dFdX[j].m, dfj_dxj);
1413 add_m3_m3m3(data->dFdX[k].m, data->dFdX[k].m, dfk_dxk);
1414
1415 add_m3_m3m3(data->dFdX[block_ij].m, data->dFdX[block_ij].m, dfj_dxi);
1416 add_m3_m3m3(data->dFdX[block_jk].m, data->dFdX[block_jk].m, dfk_dxj);
1417 add_m3_m3m3(data->dFdX[block_ik].m, data->dFdX[block_ik].m, dfk_dxi);
1418# endif
1419
1420 return true;
1421}
1422
1424 int i,
1425 const float goal_x[3],
1426 const float goal_v[3],
1427 float stiffness,
1428 float damping,
1429 float r_f[3],
1430 float r_dfdx[3][3],
1431 float r_dfdv[3][3])
1432{
1433 float root_goal_x[3], root_goal_v[3], extent[3], length, dir[3], vel[3];
1434 float f[3], dfdx[3][3], dfdv[3][3];
1435
1436 /* goal is in world space */
1437 world_to_root_v3(data, i, root_goal_x, goal_x);
1438 world_to_root_v3(data, i, root_goal_v, goal_v);
1439
1440 sub_v3_v3v3(extent, root_goal_x, data->X.v3(i));
1441 sub_v3_v3v3(vel, root_goal_v, data->V.v3(i));
1442 length = normalize_v3_v3(dir, extent);
1443
1444 if (length > ALMOST_ZERO) {
1445 mul_v3_v3fl(f, dir, stiffness * length);
1446
1447 /* Ascher & Boxman, p.21: Damping only during elongation
1448 * something wrong with it... */
1449 madd_v3_v3fl(f, dir, damping * dot_v3v3(vel, dir));
1450
1451 dfdx_spring(dfdx, dir, length, 0.0f, stiffness);
1452 dfdv_damp(dfdv, dir, damping);
1453
1454 add_v3_v3(data->F.v3(i), f);
1455 data->idFdX.add(i, i, dfdx);
1456 data->idFdV.add(i, i, dfdv);
1457
1458 if (r_f) {
1459 copy_v3_v3(r_f, f);
1460 }
1461 if (r_dfdx) {
1462 copy_m3_m3(r_dfdx, dfdx);
1463 }
1464 if (r_dfdv) {
1465 copy_m3_m3(r_dfdv, dfdv);
1466 }
1467
1468 return true;
1469 }
1470 else {
1471 if (r_f) {
1472 zero_v3(r_f);
1473 }
1474 if (r_dfdx) {
1475 zero_m3(r_dfdx);
1476 }
1477 if (r_dfdv) {
1478 zero_m3(r_dfdv);
1479 }
1480
1481 return false;
1482 }
1483}
1484
1485#endif /* IMPLICIT_SOLVER_EIGEN */
#define ALMOST_ZERO
Definition BKE_cloth.hh:34
#define BLI_INLINE
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 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 madd_m3_m3m3fl(float R[3][3], const float A[3][3], const float B[3][3], float f)
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 float dot_v3v3(const float a[3], const float b[3]) ATTR_WARN_UNUSED_RESULT
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
Object is a sort of wrapper for general info.
Read Guarded memory(de)allocation.
int SIM_mass_spring_solver_numvert(struct Implicit_Data *id)
@ SIM_SOLVER_SUCCESS
@ SIM_SOLVER_INVALID_INPUT
@ SIM_SOLVER_NUMERICAL_ISSUE
@ 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
btGeneric6DofConstraint & operator=(btGeneric6DofConstraint &other)
SIMD_FORCE_INLINE const btScalar & w() const
Return the w value.
Definition btQuadWord.h:119
void reset()
clear internal cached data and reset random seed
SIMD_FORCE_INLINE btScalar length() const
Return the length of the vector.
Definition btVector3.h:257
A conjugate gradient solver for sparse self-adjoint problems with additional constraints.
BLI_INLINE void madd_m3_m3fl(float r[3][3], const float m[3][3], float f)
Definition cloth.cc:1249
local_group_size(16, 16) .push_constant(Type b
local_group_size(16, 16) .push_constant(Type rhs
#define printf
#define powf(x, y)
draw_view in_light_buf[] float
Eigen::ConjugateGradient< lMatrix, Eigen::Lower, Eigen::DiagonalPreconditioner< Scalar > > ConjugateGradient
std::vector< Triplet > TripletList
Eigen::VectorXf lVector
Definition eigen_utils.h:93
BLI_INLINE void print_lmatrix(const lMatrix &m)
Eigen::SparseMatrix< Scalar > lMatrix
BLI_INLINE void print_lvector(const lVector3f &v)
float Scalar
Definition eigen_utils.h:27
Eigen::Triplet< Scalar > Triplet
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])
void SIM_mass_spring_get_motion_state(struct Implicit_Data *data, int index, float x[3], float v[3])
void SIM_mass_spring_add_constraint_ndof0(struct Implicit_Data *data, int index, const float dV[3])
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)
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_set_new_velocity(struct Implicit_Data *data, int index, const float v[3])
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)
void SIM_mass_spring_set_vertex_mass(struct Implicit_Data *data, int index, float mass)
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_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)
bool SIM_mass_spring_force_spring_bending(struct Implicit_Data *data, int i, int j, float restlen, float kb, float cb)
void SIM_mass_spring_apply_result(struct Implicit_Data *data)
void SIM_mass_spring_set_position(struct Implicit_Data *data, int index, const float x[3])
void SIM_mass_spring_add_constraint_ndof2(struct Implicit_Data *data, int index, const float c1[3], const float dV[3])
BLI_INLINE void implicit_print_matrix_elem(float v)
Definition implicit.h:47
void SIM_mass_spring_force_face_wind(struct Implicit_Data *data, int v1, int v2, int v3, const float(*winvec)[3])
void SIM_mass_spring_get_position(struct Implicit_Data *data, int index, float x[3])
bool SIM_mass_spring_solve_velocities(struct Implicit_Data *data, float dt, struct ImplicitSolverResult *result)
void SIM_mass_spring_force_drag(struct Implicit_Data *data, float drag)
void SIM_mass_spring_clear_constraints(struct Implicit_Data *data)
bool SIM_mass_spring_solve_positions(struct Implicit_Data *data, float dt)
void SIM_mass_spring_get_new_velocity(struct Implicit_Data *data, int index, float v[3])
void SIM_mass_spring_set_velocity(struct Implicit_Data *data, int index, const float v[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 SIM_mass_spring_force_gravity(struct Implicit_Data *data, int index, float mass, const float g[3])
void SIM_mass_spring_set_rest_transform(struct Implicit_Data *data, int index, float tfm[3][3])
void SIM_mass_spring_clear_forces(struct Implicit_Data *data)
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])
BLI_INLINE float fbstar_jacobi(float length, float L, float kb, float cb)
Implicit_Data * SIM_mass_spring_solver_create(int numverts, int numsprings)
DO_INLINE void mul_fvectorT_fvector(float to[3][3], const float vectorA[3], const float vectorB[3])
BLI_INLINE void cross_m3_v3m3(float r[3][3], const float v[3], const float m[3][3])
BLI_INLINE float fb(float length, float L)
DO_INLINE void mul_fmatrix_S(float matrix[3][3], float scalar)
BLI_INLINE float fbderiv(float length, float L)
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])
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])
BLI_INLINE void outerproduct(float r[3][3], const float a[3], const float b[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)
BLI_INLINE void spring_grad_dir(Implicit_Data *data, int i, int j, float edge[3], float dir[3], float grad_dir[3][3])
static float calc_nor_area_tri(float nor[3], const float v1[3], const float v2[3], const float v3[3])
BLI_INLINE void root_to_world_v3(Implicit_Data *data, int index, float r[3], const float v[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 float fbstar(float length, float L, float kb, float cb)
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)
#define L
static void add(blender::Map< std::string, std::string > &messages, Message &msg)
Definition msgfmt.cc:227
#define I
Frequency::GEOMETRY nor[]
fmatrix3x3 * dFdX
fmatrix3x3 * dFdV