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