Blender V5.3
SIM_mass_spring.cc
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2004-2023 Blender Authors
2 *
3 * SPDX-License-Identifier: GPL-2.0-or-later */
4
8
9#include "MEM_guardedalloc.h"
10
11#include "DNA_cloth_types.h"
12#include "DNA_modifier_types.h"
14#include "DNA_object_types.h"
15#include "DNA_scene_types.h"
16
17#include "BLI_linklist.hh"
18#include "BLI_math_geom_c.hh"
19#include "BLI_math_vector.hh"
20#include "BLI_math_vector_c.hh"
21#include "BLI_utildefines.hh"
22
23#include "BKE_cloth.hh"
24#include "BKE_collision.h"
25#include "BKE_effect.h"
26
27#include "SIM_mass_spring.h"
28#include "implicit.h"
29
30#include "DEG_depsgraph.hh"
32
33namespace blender {
34
35static float I3[3][3] = {{1.0, 0.0, 0.0}, {0.0, 1.0, 0.0}, {0.0, 0.0, 1.0}};
36
37/* Number of off-diagonal non-zero matrix blocks.
38 * Basically there is one of these for each vertex-vertex interaction.
39 */
41{
42 LinkNode *link;
43 int nondiag = 0;
44
45 for (link = cloth->springs; link; link = link->next) {
46 ClothSpring *spring = static_cast<ClothSpring *>(link->link);
47 switch (spring->type) {
49 /* angular bending combines 3 vertices */
50 nondiag += 3;
51 break;
52
53 default:
54 /* all other springs depend on 2 vertices only */
55 nondiag += 1;
56 break;
57 }
58 }
59
60 return nondiag;
61}
62
64 const int3 &vert_tri,
65 float *r_weights)
66{
67 /* We have custom vertex weights for pressure. */
68 if (clmd->sim_parms->vgroup_pressure > 0) {
69 Cloth *cloth = clmd->clothObject;
70 ClothVertex *verts = cloth->verts;
71
72 for (uint j = 0; j < 3; j++) {
73 r_weights[j] = verts[vert_tri[j]].pressure_factor;
74
75 /* Skip the entire triangle if it has a zero weight. */
76 if (r_weights[j] == 0.0f) {
77 return false;
78 }
79 }
80 }
81
82 return true;
83}
84
86 const float gradient_vector[3],
87 float *r_vertex_pressure)
88{
89 Cloth *cloth = clmd->clothObject;
90 Implicit_Data *data = cloth->implicit;
91 uint mvert_num = cloth->mvert_num;
92 float pt[3];
93
94 for (uint i = 0; i < mvert_num; i++) {
96 r_vertex_pressure[i] = dot_v3v3(pt, gradient_vector);
97 }
98}
99
101{
102 /* Calculate the (closed) cloth volume. */
103 Cloth *cloth = clmd->clothObject;
104 const int3 *vert_tris = cloth->vert_tris;
105 Implicit_Data *data = cloth->implicit;
106 float weights[3] = {1.0f, 1.0f, 1.0f};
107 float vol = 0;
108
109 /* Early exit for hair, as it never has volume. */
110 if (clmd->hairdata) {
111 return 0.0f;
112 }
113
114 for (uint i = 0; i < cloth->primitive_num; i++) {
115 const int3 tri = vert_tris[i];
116
117 if (cloth_get_pressure_weights(clmd, tri, weights)) {
118 vol += SIM_tri_tetra_volume_signed_6x(data, tri[0], tri[1], tri[2]);
119 }
120 }
121
122 /* We need to divide by 6 to get the actual volume. */
123 vol = vol / 6.0f;
124
125 return vol;
126}
127
129{
130 /* Calculate the (closed) cloth volume. */
131 Cloth *cloth = clmd->clothObject;
132 const int3 *vert_tris = cloth->vert_tris;
133 const ClothVertex *v = cloth->verts;
134 float weights[3] = {1.0f, 1.0f, 1.0f};
135 float vol = 0;
136
137 /* Early exit for hair, as it never has volume. */
138 if (clmd->hairdata) {
139 return 0.0f;
140 }
141
142 for (uint i = 0; i < cloth->primitive_num; i++) {
143 const int3 tri = vert_tris[i];
144
145 if (cloth_get_pressure_weights(clmd, tri, weights)) {
147 v[tri[0]].xrest, v[tri[1]].xrest, v[tri[2]].xrest);
148 }
149 }
150
151 /* We need to divide by 6 to get the actual volume. */
152 vol = vol / 6.0f;
153
154 return vol;
155}
156
157static float cloth_calc_average_pressure(ClothModifierData *clmd, const float *vertex_pressure)
158{
159 Cloth *cloth = clmd->clothObject;
160 const int3 *vert_tris = cloth->vert_tris;
161 Implicit_Data *data = cloth->implicit;
162 float weights[3] = {1.0f, 1.0f, 1.0f};
163 float total_force = 0;
164 float total_area = 0;
165
166 for (uint i = 0; i < cloth->primitive_num; i++) {
167 const int3 tri = vert_tris[i];
168
169 if (cloth_get_pressure_weights(clmd, tri, weights)) {
170 float area = SIM_tri_area(data, tri[0], tri[1], tri[2]);
171
172 total_force += (vertex_pressure[tri[0]] + vertex_pressure[tri[1]] +
173 vertex_pressure[tri[2]]) *
174 area / 3.0f;
175 total_area += area;
176 }
177 }
178
179 return total_force / total_area;
180}
181
183{
184 Cloth *cloth = clmd->clothObject;
185 ClothVertex *verts = cloth->verts;
186 const float ZERO[3] = {0.0f, 0.0f, 0.0f};
188 uint i, nondiag;
189
191 cloth->implicit = id = SIM_mass_spring_solver_create(cloth->mvert_num, nondiag);
192
193 for (i = 0; i < cloth->mvert_num; i++) {
194 SIM_mass_spring_set_implicit_vertex_mass(id, i, verts[i].mass);
195 }
196
197 for (i = 0; i < cloth->mvert_num; i++) {
198 SIM_mass_spring_set_motion_state(id, i, verts[i].x, ZERO);
199 }
200
201 return 1;
202}
203
208
210{
211 Cloth *cloth = clmd->clothObject;
212
213 if (cloth->implicit) {
215 cloth->implicit = nullptr;
216 }
217}
218
220{
221 Cloth *cloth = clmd->clothObject;
222 ClothVertex *verts = cloth->verts;
223 uint mvert_num = cloth->mvert_num, i;
224 ClothHairData *cloth_hairdata = clmd->hairdata;
225 Implicit_Data *id = cloth->implicit;
226
227 for (i = 0; i < mvert_num; i++) {
228 if (cloth_hairdata) {
229 ClothHairData *root = &cloth_hairdata[i];
231 }
232 else {
234 }
235
236 SIM_mass_spring_set_motion_state(id, i, verts[i].x, verts[i].v);
237 }
238}
239
241{
242 Cloth *cloth = clmd->clothObject;
243
244 cloth->initial_mesh_volume = cloth_calc_rest_volume(clmd);
245}
246
247/* Init constraint matrix
248 * This is part of the modified CG method suggested by Baraff/Witkin in
249 * "Large Steps in Cloth Simulation" (SIGGRAPH 1998)
250 */
252{
253 Cloth *cloth = clmd->clothObject;
254 Implicit_Data *data = cloth->implicit;
255 ClothVertex *verts = cloth->verts;
256 int mvert_num = cloth->mvert_num;
257 int v;
258
259 const float ZERO[3] = {0.0f, 0.0f, 0.0f};
260
262
263 for (v = 0; v < mvert_num; v++) {
264 if (verts[v].flags & CLOTH_VERT_FLAG_PINNED) {
265 /* pinned vertex constraints */
266 SIM_mass_spring_add_constraint_ndof0(data, v, ZERO); /* velocity is defined externally */
267 }
268
269 verts[v].impulse_count = 0;
270 }
271}
272
279 Object * /*ob*/, ClothModifierData *clmd, float (*initial_cos)[3], float /*step*/, float dt)
280{
281 Cloth *cloth = clmd->clothObject;
283 "cos cloth_calc_helper_forces");
284 float *masses = MEM_new_array_zeroed<float>(cloth->mvert_num, "cos cloth_calc_helper_forces");
285 LinkNode *node;
286 ClothSpring *spring;
287 ClothVertex *cv;
288 int i, steps;
289
290 cv = cloth->verts;
291 for (i = 0; i < cloth->mvert_num; i++, cv++) {
292 copy_v3_v3(cos[i], cv->tx);
293
294 if (cv->goal == 1.0f || len_squared_v3v3(initial_cos[i], cv->tx) != 0.0f) {
295 masses[i] = 1e+10;
296 }
297 else {
298 masses[i] = cv->mass;
299 }
300 }
301
302 steps = 55;
303 for (i = 0; i < steps; i++) {
304 for (node = cloth->springs; node; node = node->next) {
305 // ClothVertex *cv1, *cv2; /* UNUSED */
306 int v1, v2;
307 float len, c, l, vec[3];
308
309 spring = static_cast<ClothSpring *>(node->link);
311 continue;
312 }
313
314 v1 = spring->ij;
315 v2 = spring->kl;
316 // cv1 = cloth->verts + v1; /* UNUSED. */
317 // cv2 = cloth->verts + v2; /* UNUSED. */
318 len = len_v3v3(cos[v1], cos[v2]);
319
320 sub_v3_v3v3(vec, cos[v1], cos[v2]);
321 normalize_v3(vec);
322
323 c = (len - spring->restlen);
324 if (c == 0.0f) {
325 continue;
326 }
327
328 l = c / ((1.0f / masses[v1]) + (1.0f / masses[v2]));
329
330 mul_v3_fl(vec, -(1.0f / masses[v1]) * l);
331 add_v3_v3(cos[v1], vec);
332
333 sub_v3_v3v3(vec, cos[v2], cos[v1]);
334 normalize_v3(vec);
335
336 mul_v3_fl(vec, -(1.0f / masses[v2]) * l);
337 add_v3_v3(cos[v2], vec);
338 }
339 }
340
341 cv = cloth->verts;
342 for (i = 0; i < cloth->mvert_num; i++, cv++) {
343 float vec[3];
344
345 /* Compute forces. */
346 sub_v3_v3v3(vec, cos[i], cv->tx);
347 mul_v3_fl(vec, cv->mass * dt * 20.0f);
348 add_v3_v3(cv->tv, vec);
349 // copy_v3_v3(cv->tx, cos[i]);
350 }
351
352 MEM_delete(cos);
353 MEM_delete(masses);
354
355 return 1;
356}
357
359{
360 Cloth *cloth = clmd->clothObject;
361 ClothSimSettings *parms = clmd->sim_parms;
362 Implicit_Data *data = cloth->implicit;
363 bool using_angular = parms->bending_model == CLOTH_BENDING_ANGULAR;
364 bool resist_compress = (parms->flags & CLOTH_SIMSETTINGS_FLAG_RESIST_SPRING_COMPRESS) &&
365 !using_angular;
366
368
369 /* Calculate force of bending springs. */
370 if ((s->type & CLOTH_SPRING_TYPE_BENDING) && using_angular) {
371#ifdef CLOTH_FORCE_SPRING_BEND
372 float k, scaling;
373
375
376 scaling = parms->bending + s->ang_stiffness * fabsf(parms->max_bend - parms->bending);
377 /* Multiplying by 0.1, just to scale the forces to more reasonable values. */
378 k = scaling * s->restlen * 0.1f;
379
381 data, s->ij, s->kl, s->pa, s->pb, s->la, s->lb, s->restang, k, parms->bending_damping);
382#endif
383 }
384
385 /* Calculate force of structural + shear springs. */
386 if (s->type &
388 {
389#ifdef CLOTH_FORCE_SPRING_STRUCTURAL
390 float k_tension, scaling_tension;
391
393
394 scaling_tension = parms->tension +
395 s->lin_stiffness * fabsf(parms->max_tension - parms->tension);
396 k_tension = scaling_tension / (parms->avg_spring_len + FLT_EPSILON);
397
399 /* TODO: verify, half verified (couldn't see error)
400 * sewing springs usually have a large distance at first so clamp the force so we don't get
401 * tunneling through collision objects. */
403 s->ij,
404 s->kl,
405 s->restlen,
406 k_tension,
407 parms->tension_damp,
408 0.0f,
409 0.0f,
410 false,
411 false,
412 parms->max_sewing);
413 }
414 else if (s->type & CLOTH_SPRING_TYPE_STRUCTURAL) {
415 float k_compression, scaling_compression;
416 scaling_compression = parms->compression +
417 s->lin_stiffness * fabsf(parms->max_compression - parms->compression);
418 k_compression = scaling_compression / (parms->avg_spring_len + FLT_EPSILON);
419
421 s->ij,
422 s->kl,
423 s->restlen,
424 k_tension,
425 parms->tension_damp,
426 k_compression,
427 parms->compression_damp,
428 resist_compress,
429 using_angular,
430 0.0f);
431 }
432 else {
433 /* CLOTH_SPRING_TYPE_INTERNAL */
435
436 scaling_tension = parms->internal_tension +
437 s->lin_stiffness *
439 k_tension = scaling_tension / (parms->avg_spring_len + FLT_EPSILON);
440 float scaling_compression = parms->internal_compression +
442 parms->internal_compression);
443 float k_compression = scaling_compression / (parms->avg_spring_len + FLT_EPSILON);
444
445 float k_tension_damp = parms->tension_damp;
446 float k_compression_damp = parms->compression_damp;
447
448 if (k_tension == 0.0f) {
449 /* No damping so it behaves as if no tension spring was there at all. */
450 k_tension_damp = 0.0f;
451 }
452
453 if (k_compression == 0.0f) {
454 /* No damping so it behaves as if no compression spring was there at all. */
455 k_compression_damp = 0.0f;
456 }
457
459 s->ij,
460 s->kl,
461 s->restlen,
462 k_tension,
463 k_tension_damp,
464 k_compression,
465 k_compression_damp,
466 resist_compress,
467 using_angular,
468 0.0f);
469 }
470#endif
471 }
472 else if (s->type & CLOTH_SPRING_TYPE_SHEAR) {
473#ifdef CLOTH_FORCE_SPRING_SHEAR
474 float k, scaling;
475
477
478 scaling = parms->shear + s->lin_stiffness * fabsf(parms->max_shear - parms->shear);
479 k = scaling / (parms->avg_spring_len + FLT_EPSILON);
480
482 s->ij,
483 s->kl,
484 s->restlen,
485 k,
486 parms->shear_damp,
487 0.0f,
488 0.0f,
489 resist_compress,
490 false,
491 0.0f);
492#endif
493 }
494 else if (s->type & CLOTH_SPRING_TYPE_BENDING) { /* calculate force of bending springs */
495#ifdef CLOTH_FORCE_SPRING_BEND
496 float kb, cb, scaling;
497
499
500 scaling = parms->bending + s->lin_stiffness * fabsf(parms->max_bend - parms->bending);
501 kb = scaling / (20.0f * (parms->avg_spring_len + FLT_EPSILON));
502
503 /* Fix for #45084 for cloth stiffness must have cb proportional to kb */
504 cb = kb * parms->bending_damping;
505
507#endif
508 }
509 else if (s->type & CLOTH_SPRING_TYPE_BENDING_HAIR) {
510#ifdef CLOTH_FORCE_SPRING_BEND
511 float kb, cb, scaling;
512
514
515 /* XXX WARNING: angular bending springs for hair apply stiffness factor as an overall factor,
516 * unlike cloth springs! this is crap, but needed due to cloth/hair mixing ... max_bend factor
517 * is not even used for hair, so ...
518 */
519 scaling = s->lin_stiffness * parms->bending;
520 kb = scaling / (20.0f * (parms->avg_spring_len + FLT_EPSILON));
521
522 /* Fix for #45084 for cloth stiffness must have cb proportional to kb */
523 cb = kb * parms->bending_damping;
524
525 /* XXX assuming same restlen for ij and jk segments here,
526 * this can be done correctly for hair later. */
528
529# if 0
530 {
531 float x_kl[3], x_mn[3], v[3], d[3];
532
535
536 BKE_sim_debug_data_add_dot(clmd->debug_data, x_kl, 0.9, 0.9, 0.9, "target", 7980, s->kl);
538 clmd->debug_data, x_kl, x_mn, 0.8, 0.8, 0.8, "target", 7981, s->kl);
539
540 copy_v3_v3(d, s->target);
542 clmd->debug_data, x_kl, d, 0.8, 0.8, 0.2, "target", 7982, s->kl);
543
544 // copy_v3_v3(d, s->target_ij);
545 // BKE_sim_debug_data_add_vector(clmd->debug_data, x, d, 1, 0.4, 0.4, "target", 7983, s->kl);
546 }
547# endif
548#endif
549 }
550}
551
552static void hair_get_boundbox(ClothModifierData *clmd, float3 &gmin, float3 &gmax)
553{
554 Cloth *cloth = clmd->clothObject;
555 Implicit_Data *data = cloth->implicit;
556 uint mvert_num = cloth->mvert_num;
557 int i;
558
559 INIT_MINMAX(gmin, gmax);
560 for (i = 0; i < mvert_num; i++) {
561 float3 x;
563 math::min_max(x, gmin, gmax);
564 }
565}
566
567static void cloth_calc_force(Scene *scene,
568 ClothModifierData *clmd,
569 float /*frame*/,
570 ListBaseT<EffectorCache> *effectors,
571 float time)
572{
573 /* Collect forces and derivatives: F, dFdX, dFdV. */
574 Cloth *cloth = clmd->clothObject;
575 ClothSimSettings *parms = clmd->sim_parms;
576 Implicit_Data *data = cloth->implicit;
577 uint i = 0;
578 float drag = clmd->sim_parms->Cvi * 0.01f; /* viscosity of air scaled in percent */
579 float gravity[3] = {0.0f, 0.0f, 0.0f};
580 const int3 *vert_tris = cloth->vert_tris;
581 uint mvert_num = cloth->mvert_num;
582 ClothVertex *vert;
583
584#ifdef CLOTH_FORCE_GRAVITY
585 /* global acceleration (gravitation) */
587 /* scale gravity force */
588 mul_v3_v3fl(gravity,
590 0.001f * clmd->sim_parms->effector_weights->global_gravity);
591 }
592
593 vert = cloth->verts;
594 for (i = 0; i < cloth->mvert_num; i++, vert++) {
595 SIM_mass_spring_force_gravity(data, i, vert->mass, gravity);
596
597 /* Vertex goal springs */
598 if (!(vert->flags & CLOTH_VERT_FLAG_PINNED) && (vert->goal > FLT_EPSILON)) {
599 float goal_x[3], goal_v[3];
600 float k;
601
602 /* divide by time_scale to prevent goal vertices' delta locations from being multiplied */
603 interp_v3_v3v3(goal_x, vert->xold, vert->xconst, time / clmd->sim_parms->time_scale);
604 sub_v3_v3v3(goal_v, vert->xconst, vert->xold); /* distance covered over dt==1 */
605
606 k = vert->goal * clmd->sim_parms->goalspring /
608
610 data, i, goal_x, goal_v, k, clmd->sim_parms->goalfrict * 0.01f);
611 }
612 }
613#endif
614
615 // cloth_calc_volume_force(clmd);
616
617#ifdef CLOTH_FORCE_DRAG
619#endif
620 /* handle pressure forces (making sure that this never gets computed for hair). */
621 if ((parms->flags & CLOTH_SIMSETTINGS_FLAG_PRESSURE) && (clmd->hairdata == nullptr)) {
622 /* The difference in pressure between the inside and outside of the mesh. */
623 float pressure_difference = 0.0f;
624 float volume_factor = 1.0f;
625
626 float init_vol;
628 init_vol = clmd->sim_parms->target_volume;
629 }
630 else {
631 init_vol = cloth->initial_mesh_volume;
632 }
633
634 /* Check if we need to calculate the volume of the mesh. */
635 if (init_vol > 1E-6f) {
636 float f;
637 float vol = cloth_calc_volume(clmd);
638
639 /* If the volume is the same don't apply any pressure. */
640 volume_factor = init_vol / vol;
641 pressure_difference = volume_factor - 1;
642
643 /* Calculate an artificial maximum value for cloth pressure. */
644 f = fabs(clmd->sim_parms->uniform_pressure_force) + 200.0f;
645
646 /* Clamp the cloth pressure to the calculated maximum value. */
647 CLAMP_MAX(pressure_difference, f);
648 }
649
650 pressure_difference += clmd->sim_parms->uniform_pressure_force;
651 pressure_difference *= clmd->sim_parms->pressure_factor;
652
653 /* Compute the hydrostatic pressure gradient if enabled. */
654 float fluid_density = clmd->sim_parms->fluid_density * 1000; /* kg/l -> kg/m3 */
655 float *hydrostatic_pressure = nullptr;
656
657 if (fabs(fluid_density) > 1e-6f) {
658 float hydrostatic_vector[3];
659 copy_v3_v3(hydrostatic_vector, gravity);
660
661 /* When the fluid is inside the object, factor in the acceleration of
662 * the object into the pressure field, as gravity is indistinguishable
663 * from acceleration from the inside. */
664 if (fluid_density > 0) {
665 sub_v3_v3(hydrostatic_vector, cloth->average_acceleration);
666
667 /* Preserve the total mass by scaling density to match the change in volume. */
668 fluid_density *= volume_factor;
669 }
670
671 mul_v3_fl(hydrostatic_vector, fluid_density);
672
673 /* Compute an array of per-vertex hydrostatic pressure, and subtract the average. */
674 hydrostatic_pressure = MEM_new_array_uninitialized<float>(mvert_num,
675 "hydrostatic pressure gradient");
676
677 cloth_calc_pressure_gradient(clmd, hydrostatic_vector, hydrostatic_pressure);
678
679 pressure_difference -= cloth_calc_average_pressure(clmd, hydrostatic_pressure);
680 }
681
682 /* Apply pressure. */
683 if (hydrostatic_pressure || fabs(pressure_difference) > 1E-6f) {
684 float weights[3] = {1.0f, 1.0f, 1.0f};
685
686 for (i = 0; i < cloth->primitive_num; i++) {
687 const int3 tri = vert_tris[i];
688
689 if (cloth_get_pressure_weights(clmd, tri, weights)) {
691 data, tri[0], tri[1], tri[2], pressure_difference, hydrostatic_pressure, weights);
692 }
693 }
694 }
695
696 if (hydrostatic_pressure) {
697 MEM_delete(hydrostatic_pressure);
698 }
699 }
700
701 /* handle external forces like wind */
702 if (effectors) {
703 bool is_not_hair = (clmd->hairdata == nullptr) && (cloth->primitive_num > 0);
704 bool has_wind = false, has_force = false;
705
706 /* cache per-vertex forces to avoid redundant calculation */
707 float (*winvec)[3] = MEM_new_array_zeroed<float[3]>(mvert_num * 2, "effector forces");
708 float (*forcevec)[3] = is_not_hair ? winvec + mvert_num : winvec;
709
710 for (i = 0; i < cloth->mvert_num; i++) {
711 float x[3], v[3];
712 EffectedPoint epoint;
713
715 pd_point_from_loc(scene, x, v, i, &epoint);
716 BKE_effectors_apply(effectors,
717 nullptr,
719 &epoint,
720 forcevec[i],
721 winvec[i],
722 nullptr);
723
724 has_wind = has_wind || !is_zero_v3(winvec[i]);
725 has_force = has_force || !is_zero_v3(forcevec[i]);
726 }
727
728 /* Hair has only edges. */
729 if (is_not_hair) {
730 for (i = 0; i < cloth->primitive_num; i++) {
731 const int3 tri = vert_tris[i];
732 if (has_wind) {
733 SIM_mass_spring_force_face_wind(data, tri[0], tri[1], tri[2], winvec);
734 }
735 if (has_force) {
736 SIM_mass_spring_force_face_extern(data, tri[0], tri[1], tri[2], forcevec);
737 }
738 }
739 }
740 else {
741#if 0
742 ClothHairData *hairdata = clmd->hairdata;
743 ClothHairData *hair_ij, *hair_kl;
744
745 for (LinkNode *link = cloth->springs; link; link = link->next) {
746 ClothSpring *spring = (ClothSpring *)link->link;
747 if (spring->type == CLOTH_SPRING_TYPE_STRUCTURAL) {
748 if (hairdata) {
749 hair_ij = &hairdata[spring->ij];
750 hair_kl = &hairdata[spring->kl];
752 data, spring->ij, spring->kl, hair_ij->radius, hair_kl->radius, winvec);
753 }
754 else {
755 SIM_mass_spring_force_edge_wind(data, spring->ij, spring->kl, 1.0f, 1.0f, winvec);
756 }
757 }
758 }
759#else
760 ClothHairData *hairdata = clmd->hairdata;
761
762 vert = cloth->verts;
763 for (i = 0; i < cloth->mvert_num; i++, vert++) {
764 if (hairdata) {
765 ClothHairData *hair = &hairdata[i];
767 }
768 else {
769 SIM_mass_spring_force_vertex_wind(data, i, 1.0f, winvec);
770 }
771 }
772#endif
773 }
774
775 MEM_delete(winvec);
776 }
777
778 /* calculate spring forces */
779 for (LinkNode *link = cloth->springs; link; link = link->next) {
780 ClothSpring *spring = static_cast<ClothSpring *>(link->link);
781 /* only handle active springs */
782 if (!(spring->flags & CLOTH_SPRING_FLAG_DEACTIVATE)) {
783 cloth_calc_spring_force(clmd, spring);
784 }
785 }
786}
787
788/* returns vertices' motion state */
790 float cell_scale,
791 const float cell_offset[3],
792 int index,
793 float x[3],
794 float v[3])
795{
798
799 mul_v3_fl(x, cell_scale);
800 add_v3_v3(x, cell_offset);
801}
802
803/* returns next spring forming a continuous hair sequence */
805{
806 ClothSpring *spring = static_cast<ClothSpring *>(spring_link->link);
807 LinkNode *next = spring_link->next;
808 if (next) {
809 ClothSpring *next_spring = static_cast<ClothSpring *>(next->link);
810 if (next_spring->type == CLOTH_SPRING_TYPE_STRUCTURAL && next_spring->kl == spring->ij) {
811 return next;
812 }
813 }
814 return nullptr;
815}
816
817/* XXX this is nasty: cloth meshes do not explicitly store
818 * the order of hair segments!
819 * We have to rely on the spring build function for now,
820 * which adds structural springs in reverse order:
821 * (3,4), (2,3), (1,2)
822 * This is currently the only way to figure out hair geometry inside this code ...
823 */
825 const float cell_scale,
826 const float cell_offset[3],
827 Cloth *cloth,
828 LinkNode *spring_link)
829{
830 Implicit_Data *data = cloth->implicit;
831 LinkNode *next_spring_link = nullptr; /* return value */
832 ClothSpring *spring1, *spring2, *spring3;
833 // ClothVertex *verts = cloth->verts;
834 // ClothVertex *vert3, *vert4;
835 float x1[3], v1[3], x2[3], v2[3], x3[3], v3[3], x4[3], v4[3];
836 float dir1[3], dir2[3], dir3[3];
837
838 spring1 = nullptr;
839 spring2 = nullptr;
840 spring3 = static_cast<ClothSpring *>(spring_link->link);
841
842 zero_v3(x1);
843 zero_v3(v1);
844 zero_v3(dir1);
845 zero_v3(x2);
846 zero_v3(v2);
847 zero_v3(dir2);
848
849 // vert3 = &verts[spring3->kl];
850 cloth_get_grid_location(data, cell_scale, cell_offset, spring3->kl, x3, v3);
851 // vert4 = &verts[spring3->ij];
852 cloth_get_grid_location(data, cell_scale, cell_offset, spring3->ij, x4, v4);
853 sub_v3_v3v3(dir3, x4, x3);
854 normalize_v3(dir3);
855
856 while (spring_link) {
857 /* move on */
858 spring1 = spring2;
859 spring2 = spring3;
860
861 // vert3 = vert4;
862
863 copy_v3_v3(x1, x2);
864 copy_v3_v3(v1, v2);
865 copy_v3_v3(x2, x3);
866 copy_v3_v3(v2, v3);
867 copy_v3_v3(x3, x4);
868 copy_v3_v3(v3, v4);
869
870 copy_v3_v3(dir1, dir2);
871 copy_v3_v3(dir2, dir3);
872
873 /* read next segment */
874 next_spring_link = spring_link->next;
875 spring_link = hair_spring_next(spring_link);
876
877 if (spring_link) {
878 spring3 = static_cast<ClothSpring *>(spring_link->link);
879 // vert4 = &verts[spring3->ij];
880 cloth_get_grid_location(data, cell_scale, cell_offset, spring3->ij, x4, v4);
881 sub_v3_v3v3(dir3, x4, x3);
882 normalize_v3(dir3);
883 }
884 else {
885 spring3 = nullptr;
886 // vert4 = nullptr;
887 zero_v3(x4);
888 zero_v3(v4);
889 zero_v3(dir3);
890 }
891
893 x1,
894 v1,
895 x2,
896 v2,
897 x3,
898 v3,
899 x4,
900 v4,
901 spring1 ? dir1 : nullptr,
902 dir2,
903 spring3 ? dir3 : nullptr);
904 }
905
906 return next_spring_link;
907}
908
910{
911#if 0
912 Implicit_Data *data = cloth->implicit;
913 int mvert_num = cloth->mvert_num;
914 ClothVertex *vert;
915 int i;
916
917 for (i = 0, vert = cloth->verts; i < mvert_num; i++, vert++) {
918 float x[3], v[3];
919
920 cloth_get_vertex_motion_state(data, vert, x, v);
922 }
923#else
924 LinkNode *link;
925 float cellsize, gmin[3], cell_scale, cell_offset[3];
926
927 /* scale and offset for transforming vertex locations into grid space
928 * (cell size is 0..1, gmin becomes origin)
929 */
930 SIM_hair_volume_grid_geometry(grid, &cellsize, nullptr, gmin, nullptr);
931 cell_scale = cellsize > 0.0f ? 1.0f / cellsize : 0.0f;
932 mul_v3_v3fl(cell_offset, gmin, cell_scale);
933 negate_v3(cell_offset);
934
935 link = cloth->springs;
936 while (link) {
937 ClothSpring *spring = static_cast<ClothSpring *>(link->link);
938 if (spring->type == CLOTH_SPRING_TYPE_STRUCTURAL) {
939 link = cloth_continuum_add_hair_segments(grid, cell_scale, cell_offset, cloth, link);
940 }
941 else {
942 link = link->next;
943 }
944 }
945#endif
947}
948
949static void cloth_continuum_step(ClothModifierData *clmd, float dt)
950{
951 ClothSimSettings *parms = clmd->sim_parms;
952 Cloth *cloth = clmd->clothObject;
953 Implicit_Data *data = cloth->implicit;
954 int mvert_num = cloth->mvert_num;
955
956 const float fluid_factor = 0.95f; /* blend between PIC and FLIP methods */
957 float smoothfac = parms->velocity_smooth;
958 /* XXX FIXME arbitrary factor!!! this should be based on some intuitive value instead,
959 * like number of hairs per cell and time decay instead of "strength"
960 */
961 float density_target = parms->density_target;
962 float density_strength = parms->density_strength;
963 float3 gmin, gmax;
964 int i;
965
966 /* clear grid info */
968 zero_v3(clmd->hair_grid_min);
969 zero_v3(clmd->hair_grid_max);
970 clmd->hair_grid_cellsize = 0.0f;
971
972 hair_get_boundbox(clmd, gmin, gmax);
973
974 /* gather velocities & density */
975 if (smoothfac > 0.0f || density_strength > 0.0f) {
977 clmd->sim_parms->voxel_cell_size, gmin, gmax);
978
980
981 /* main hair continuum solver */
982 SIM_hair_volume_solve_divergence(grid, dt, density_target, density_strength);
983
984 for (i = 0; i < mvert_num; i++) {
985 float x[3], v[3], nv[3];
986
987 /* calculate volumetric velocity influence */
990
991 SIM_hair_volume_grid_velocity(grid, x, v, fluid_factor, nv);
992
993 interp_v3_v3v3(nv, v, nv, smoothfac);
994
995 /* apply on hair data */
997 }
998
999 /* store basic grid info in the modifier data */
1001 &clmd->hair_grid_cellsize,
1002 clmd->hair_grid_res,
1003 clmd->hair_grid_min,
1004 clmd->hair_grid_max);
1005
1006#if 0 /* DEBUG hair velocity vector field */
1007 {
1008 const int size = 64;
1009 int i, j;
1010 float offset[3], a[3], b[3];
1011 const int axis = 0;
1012 const float shift = 0.0f;
1013
1014 copy_v3_v3(offset, clmd->hair_grid_min);
1015 zero_v3(a);
1016 zero_v3(b);
1017
1018 offset[axis] = shift * clmd->hair_grid_cellsize;
1019 a[(axis + 1) % 3] = clmd->hair_grid_max[(axis + 1) % 3] -
1020 clmd->hair_grid_min[(axis + 1) % 3];
1021 b[(axis + 2) % 3] = clmd->hair_grid_max[(axis + 2) % 3] -
1022 clmd->hair_grid_min[(axis + 2) % 3];
1023
1024 BKE_sim_debug_data_clear_category(clmd->debug_data, "grid velocity");
1025 for (j = 0; j < size; j++) {
1026 for (i = 0; i < size; i++) {
1027 float x[3], v[3], gvel[3], gvel_smooth[3], gdensity;
1028
1029 madd_v3_v3v3fl(x, offset, a, float(i) / float(size - 1));
1030 madd_v3_v3fl(x, b, float(j) / float(size - 1));
1031 zero_v3(v);
1032
1034 grid, x, &gdensity, gvel, gvel_smooth, nullptr, nullptr);
1035
1036# if 0
1038 clmd->debug_data, x, gdensity, 0.7, 0.3, 1, "grid density", i, j, 3111);
1039# endif
1040 if (!is_zero_v3(gvel) || !is_zero_v3(gvel_smooth)) {
1041 float dvel[3];
1042 sub_v3_v3v3(dvel, gvel_smooth, gvel);
1043# if 0
1045 clmd->debug_data, x, gvel, 0.4, 0, 1, "grid velocity", i, j, 3112);
1047 clmd->debug_data, x, gvel_smooth, 0.6, 1, 1, "grid velocity", i, j, 3113);
1048# endif
1050 clmd->debug_data, x, dvel, 0.4, 1, 0.7, "grid velocity", i, j, 3114);
1051# if 0
1052 if (gdensity > 0.0f) {
1053 float col0[3] = {0.0, 0.0, 0.0};
1054 float col1[3] = {0.0, 1.0, 0.0};
1055 float col[3];
1056
1057 interp_v3_v3v3(col,
1058 col0,
1059 col1,
1060 std::clamp(gdensity * clmd->sim_parms->density_strength, 0.0, 1.0));
1061# if 0
1062 BKE_sim_debug_data_add_circle(clmd->debug_data,
1063 x,
1064 gdensity * clmd->sim_parms->density_strength,
1065 0,
1066 1,
1067 0.4,
1068 "grid velocity",
1069 i,
1070 j,
1071 3115);
1073 clmd->debug_data, x, col[0], col[1], col[2], "grid velocity", i, j, 3115);
1074# endif
1076 clmd->debug_data, x, 0.01f, col[0], col[1], col[2], "grid velocity", i, j, 3115);
1077 }
1078# endif
1079 }
1080 }
1081 }
1082 }
1083#endif
1084
1086 }
1087}
1088
1089#if 0
1090static void cloth_calc_volume_force(ClothModifierData *clmd)
1091{
1092 ClothSimSettings *parms = clmd->sim_parms;
1093 Cloth *cloth = clmd->clothObject;
1094 Implicit_Data *data = cloth->implicit;
1095 int mvert_num = cloth->mvert_num;
1096 ClothVertex *vert;
1097
1098 /* 2.0f is an experimental value that seems to give good results */
1099 float smoothfac = 2.0f * parms->velocity_smooth;
1100 float collfac = 2.0f * parms->collider_friction;
1101 float pressfac = parms->pressure;
1102 float minpress = parms->pressure_threshold;
1103 float gmin[3], gmax[3];
1104 int i;
1105
1106 hair_get_boundbox(clmd, gmin, gmax);
1107
1108 /* gather velocities & density */
1109 if (smoothfac > 0.0f || pressfac > 0.0f) {
1110 HairVertexGrid *vertex_grid = SIM_hair_volume_create_vertex_grid(
1111 clmd->sim_parms->voxel_res, gmin, gmax);
1112
1113 vert = cloth->verts;
1114 for (i = 0; i < mvert_num; i++, vert++) {
1115 float x[3], v[3];
1116
1117 if (vert->solver_index < 0) {
1118 copy_v3_v3(x, vert->x);
1119 copy_v3_v3(v, vert->v);
1120 }
1121 else {
1122 SIM_mass_spring_get_motion_state(data, vert->solver_index, x, v);
1123 }
1124 SIM_hair_volume_add_vertex(vertex_grid, x, v);
1125 }
1127
1128 vert = cloth->verts;
1129 for (i = 0; i < mvert_num; i++, vert++) {
1130 float x[3], v[3], f[3], dfdx[3][3], dfdv[3][3];
1131
1132 if (vert->solver_index < 0) {
1133 continue;
1134 }
1135
1136 /* calculate volumetric forces */
1137 SIM_mass_spring_get_motion_state(data, vert->solver_index, x, v);
1139 vertex_grid, x, v, smoothfac, pressfac, minpress, f, dfdx, dfdv);
1140 /* apply on hair data */
1141 SIM_mass_spring_force_extern(data, vert->solver_index, f, dfdx, dfdv);
1142 }
1143
1145 }
1146}
1147#endif
1148
1150{
1151 Cloth *cloth = clmd->clothObject;
1152 Implicit_Data *data = cloth->implicit;
1153 int i, mvert_num = cloth->mvert_num;
1154 float total[3] = {0.0f, 0.0f, 0.0f};
1155
1156 for (i = 0; i < mvert_num; i++) {
1157 float v[3], nv[3];
1158
1161
1162 sub_v3_v3(nv, v);
1163 add_v3_v3(total, nv);
1164 }
1165
1166 mul_v3_fl(total, 1.0f / dt / mvert_num);
1167
1168 /* Smooth the data using a running average to prevent instability.
1169 * This is effectively an abstraction of the wave propagation speed in fluid. */
1170 interp_v3_v3v3(cloth->average_acceleration, total, cloth->average_acceleration, powf(0.25f, dt));
1171}
1172
1174 Depsgraph *depsgraph, Object *ob, ClothModifierData *clmd, float step, float dt)
1175{
1176 Cloth *cloth = clmd->clothObject;
1177 Implicit_Data *id = cloth->implicit;
1178 ClothVertex *verts = cloth->verts;
1179 int mvert_num = cloth->mvert_num;
1180 const float time_multiplier = 1.0f / (clmd->sim_parms->dt * clmd->sim_parms->timescale);
1181 int i;
1182
1183 if (!(clmd->coll_parms->flags &
1185 {
1186 return;
1187 }
1188
1189 if (!clmd->clothObject->bvhtree) {
1190 return;
1191 }
1192
1194
1195 /* Update verts to current positions. */
1196 for (i = 0; i < mvert_num; i++) {
1197 SIM_mass_spring_get_new_position(id, i, verts[i].tx);
1198
1199 sub_v3_v3v3(verts[i].tv, verts[i].tx, verts[i].txold);
1200 zero_v3(verts[i].dcvel);
1201 }
1202
1204 depsgraph, ob, clmd, step / clmd->sim_parms->timescale, dt / clmd->sim_parms->timescale))
1205 {
1206 for (i = 0; i < mvert_num; i++) {
1207 if ((clmd->sim_parms->vgroup_mass > 0) && (verts[i].flags & CLOTH_VERT_FLAG_PINNED)) {
1208 continue;
1209 }
1210
1211 SIM_mass_spring_get_new_velocity(id, i, verts[i].tv);
1212 madd_v3_v3fl(verts[i].tv, verts[i].dcvel, time_multiplier);
1213 SIM_mass_spring_set_new_velocity(id, i, verts[i].tv);
1214 }
1215 }
1216}
1217
1219{
1220 ClothSolverResult *sres = clmd->solver_result;
1221
1222 sres->status = 0;
1223 sres->max_error = sres->min_error = sres->avg_error = 0.0f;
1224 sres->max_iterations = sres->min_iterations = 0;
1225 sres->avg_iterations = 0.0f;
1226}
1227
1229{
1230 ClothSolverResult *sres = clmd->solver_result;
1231
1232 if (sres->status) { /* already initialized ? */
1233 /* error only makes sense for successful iterations */
1234 if (result->status == SIM_SOLVER_SUCCESS) {
1235 sres->min_error = min_ff(sres->min_error, result->error);
1236 sres->max_error = max_ff(sres->max_error, result->error);
1237 sres->avg_error += result->error * dt;
1238 }
1239
1240 sres->min_iterations = min_ii(sres->min_iterations, result->iterations);
1241 sres->max_iterations = max_ii(sres->max_iterations, result->iterations);
1242 sres->avg_iterations += float(result->iterations) * dt;
1243 }
1244 else {
1245 /* error only makes sense for successful iterations */
1246 if (result->status == SIM_SOLVER_SUCCESS) {
1247 sres->min_error = sres->max_error = result->error;
1248 sres->avg_error += result->error * dt;
1249 }
1250
1251 sres->min_iterations = sres->max_iterations = result->iterations;
1252 sres->avg_iterations += float(result->iterations) * dt;
1253 }
1254
1255 sres->status |= result->status;
1256}
1257
1258int SIM_cloth_solve(Depsgraph *depsgraph,
1259 Object *ob,
1260 float frame,
1261 ClothModifierData *clmd,
1262 ListBaseT<EffectorCache> *effectors)
1263{
1264 /* Hair currently is a cloth sim in disguise ...
1265 * Collision detection and volumetrics work differently then.
1266 * Bad design, TODO
1267 */
1268 Scene *scene = DEG_get_evaluated_scene(depsgraph);
1269 const bool is_hair = (clmd->hairdata != nullptr);
1270
1271 uint i = 0;
1272 float step = 0.0f, tf = clmd->sim_parms->timescale;
1273 Cloth *cloth = clmd->clothObject;
1274 ClothVertex *verts = cloth->verts /*, *cv*/;
1275 uint mvert_num = cloth->mvert_num;
1276 float dt = clmd->sim_parms->dt * clmd->sim_parms->timescale;
1277 Implicit_Data *id = cloth->implicit;
1278
1279 /* Hydrostatic pressure gradient of the fluid inside the object is affected by acceleration. */
1280 bool use_acceleration = (clmd->sim_parms->flags & CLOTH_SIMSETTINGS_FLAG_PRESSURE) &&
1281 (clmd->sim_parms->fluid_density > 0);
1282
1284
1285 if (!clmd->solver_result) {
1286 clmd->solver_result = MEM_new_zeroed<ClothSolverResult>("cloth solver result");
1287 }
1288 cloth_clear_result(clmd);
1289
1290 if (clmd->sim_parms->vgroup_mass > 0) { /* Do goal stuff. */
1291 for (i = 0; i < mvert_num; i++) {
1292 /* update velocities with constrained velocities from pinned verts */
1293 if (verts[i].flags & CLOTH_VERT_FLAG_PINNED) {
1294 float v[3];
1295 sub_v3_v3v3(v, verts[i].xconst, verts[i].xold);
1296 // mul_v3_fl(v, clmd->sim_parms->stepsPerFrame);
1297 /* divide by time_scale to prevent constrained velocities from being multiplied */
1298 mul_v3_fl(v, 1.0f / clmd->sim_parms->time_scale);
1300 }
1301 }
1302 }
1303
1304 if (!use_acceleration) {
1305 zero_v3(cloth->average_acceleration);
1306 }
1307
1308 while (step < tf) {
1310
1311 /* setup vertex constraints for pinned vertices */
1313
1314 /* initialize forces to zero */
1316
1317 /* calculate forces */
1318 cloth_calc_force(scene, clmd, frame, effectors, step);
1319
1320 /* calculate new velocity and position */
1322 cloth_record_result(clmd, &result, dt);
1323
1324 /* Calculate collision impulses. */
1325 cloth_solve_collisions(depsgraph, ob, clmd, step, dt);
1326
1327 if (is_hair) {
1328 cloth_continuum_step(clmd, dt);
1329 }
1330
1331 if (use_acceleration) {
1333 }
1334
1337
1338 /* move pinned verts to correct position */
1339 for (i = 0; i < mvert_num; i++) {
1340 if (clmd->sim_parms->vgroup_mass > 0) {
1341 if (verts[i].flags & CLOTH_VERT_FLAG_PINNED) {
1342 float x[3];
1343 /* divide by time_scale to prevent pinned vertices'
1344 * delta locations from being multiplied */
1346 x, verts[i].xold, verts[i].xconst, (step + dt) / clmd->sim_parms->time_scale);
1348 }
1349 }
1350
1351 SIM_mass_spring_get_motion_state(id, i, verts[i].txold, nullptr);
1352 }
1353
1354 step += dt;
1355 }
1356
1357 /* copy results back to cloth data */
1358 for (i = 0; i < mvert_num; i++) {
1359 SIM_mass_spring_get_motion_state(id, i, verts[i].x, verts[i].v);
1360 copy_v3_v3(verts[i].txold, verts[i].x);
1361 }
1362
1363 return 1;
1364}
1365
1366} // namespace blender
#define BKE_sim_debug_data_add_dot(p, r, g, b, category,...)
Definition BKE_effect.h:244
#define BKE_sim_debug_data_add_vector(p, d, r, g, b, category,...)
Definition BKE_effect.h:264
#define BKE_sim_debug_data_add_line(p1, p2, r, g, b, category,...)
Definition BKE_effect.h:258
#define BKE_sim_debug_data_add_circle(p, radius, r, g, b, category,...)
Definition BKE_effect.h:251
Object is a sort of wrapper for general info.
Read Blender memory allocation and freeing.
#define UNUSED_FUNCTION(x)
#define CLAMP_MAX(a, c)
#define INIT_MINMAX(min, max)
#define BLI_INLINE
#define ELEM(...)
#define powf(x, y)
#define FLT_EPSILON
#define cos
VecBase< float, D > step(VecOp< float, D >, VecOp< float, D >) RET
ccl_device_inline dual1 len(const ccl_private dual< T > &a)
Definition math_dual.h:263
ccl_device_inline dual3 fabs(const ccl_private dual3 &a)
Definition math_dual.h:241
ccl_device_inline void copy_v3_v3(ccl_private float *r, const float3 val)
void min_max(const T &value, T &min, T &max)
BMesh Iterators.
MINLINE float min_ff(float a, float b)
MINLINE void madd_v3_v3v3fl(float r[3], const float a[3], const float b[3], float f)
static void cloth_solve_collisions(Depsgraph *depsgraph, Object *ob, ClothModifierData *clmd, float step, float dt)
BLI_assert(slot->slot_subtype.map==BMO_OP_SLOT_SUBTYPE_MAP_FLT)
MINLINE float len_squared_v3v3(const float a[3], const float b[3]) ATTR_WARN_UNUSED_RESULT
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)
static float cloth_calc_average_pressure(ClothModifierData *clmd, const float *vertex_pressure)
static void cloth_calc_average_acceleration(ClothModifierData *clmd, float dt)
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)
int SIM_cloth_solve(Depsgraph *depsgraph, Object *ob, float frame, ClothModifierData *clmd, ListBaseT< EffectorCache > *effectors)
static void cloth_setup_constraints(ClothModifierData *clmd)
MINLINE void copy_v3_v3(float r[3], const float a[3])
unsigned int uint
Definition BKE_text.h:55
void * MEM_new_array_uninitialized(size_t len, size_t size, const char *str) ATTR_WARN_UNUSED_RESULT ATTR_ALLOC_SIZE(1
void SIM_mass_spring_force_edge_wind(struct Implicit_Data *data, int v1, int v2, float radius1, float radius2, const float(*winvec)[3])
@ CLOTH_BENDING_ANGULAR
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])
static float cloth_calc_volume(ClothModifierData *clmd)
static float cloth_calc_rest_volume(ClothModifierData *clmd)
void SIM_mass_spring_set_implicit_vertex_mass(Implicit_Data *data, int index, float mass)
void SIM_mass_spring_add_constraint_ndof0(struct Implicit_Data *data, int index, const float dV[3])
ATTR_WARN_UNUSED_RESULT const BMLoop * l
Implicit_Data * SIM_mass_spring_solver_create(int numverts, int numsprings)
float SIM_tri_area(struct Implicit_Data *data, int v1, int v2, int v3)
void SIM_hair_volume_normalize_vertex_grid(HairGrid *grid)
void SIM_mass_spring_force_vertex_wind(struct Implicit_Data *data, int v, float radius, const float(*winvec)[3])
void SIM_hair_volume_free_vertex_grid(HairGrid *grid)
void BKE_effectors_apply(ListBaseT< EffectorCache > *effectors, ListBaseT< ColliderCache > *colliders, struct EffectorWeights *weights, struct EffectedPoint *point, float *force, float *wind_force, float *impulse)
Definition effect.cc:1134
void SIM_mass_spring_set_rest_transform(struct Implicit_Data *data, int index, float tfm[3][3])
BLI_INLINE LinkNode * hair_spring_next(LinkNode *spring_link)
static int cloth_count_nondiag_blocks(Cloth *cloth)
MINLINE int max_ii(int a, int b)
ATTR_WARN_UNUSED_RESULT const BMVert * v
static bool cloth_get_pressure_weights(ClothModifierData *clmd, const int3 &vert_tri, float *r_weights)
void SIM_hair_volume_grid_interpolate(HairGrid *grid, const float x[3], float *density, float velocity[3], float velocity_smooth[3], float density_gradient[3], float velocity_gradient[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)
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)
@ CLOTH_SPRING_TYPE_BENDING
Definition BKE_cloth.hh:190
@ CLOTH_SPRING_TYPE_STRUCTURAL
Definition BKE_cloth.hh:188
@ CLOTH_SPRING_TYPE_SHEAR
Definition BKE_cloth.hh:189
@ CLOTH_SPRING_TYPE_SEWING
Definition BKE_cloth.hh:192
@ CLOTH_SPRING_TYPE_INTERNAL
Definition BKE_cloth.hh:194
@ CLOTH_SPRING_TYPE_BENDING_HAIR
Definition BKE_cloth.hh:193
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)
void SIM_cloth_solver_set_volume(ClothModifierData *clmd)
MINLINE void madd_v3_v3fl(float r[3], const float a[3], float f)
void SIM_hair_volume_vertex_grid_forces(HairGrid *grid, const float x[3], const float v[3], float smoothfac, float pressurefac, float minpressure, float f[3], float dfdx[3][3], float dfdv[3][3])
static void cloth_clear_result(ClothModifierData *clmd)
VecBase< int32_t, 3 > int3
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)
BMesh const char void * data
static float ZERO[3][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])
static void cloth_continuum_fill_grid(HairGrid *grid, Cloth *cloth)
MINLINE void negate_v3(float r[3])
void SIM_hair_volume_grid_velocity(HairGrid *grid, const float x[3], const float v[3], float fluid_factor, float r_v[3])
void SIM_cloth_solver_free(ClothModifierData *clmd)
void SIM_mass_spring_apply_result(struct Implicit_Data *data)
void interp_v3_v3v3(float r[3], const float a[3], const float b[3], float t)
void SIM_mass_spring_force_drag(struct Implicit_Data *data, float drag)
MINLINE void zero_v3(float r[3])
void SIM_hair_volume_add_vertex(HairGrid *grid, const float x[3], const float v[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)
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])
int cloth_bvh_collision(Depsgraph *depsgraph, Object *ob, ClothModifierData *clmd, float step, float dt)
static void cloth_record_result(ClothModifierData *clmd, ImplicitSolverResult *result, float dt)
bool SIM_mass_spring_force_spring_bending(struct Implicit_Data *data, int i, int j, float restlen, float kb, float cb)
MINLINE void mul_v3_v3fl(float r[3], const float a[3], float f)
static void cloth_calc_force(Scene *scene, ClothModifierData *clmd, float, ListBaseT< EffectorCache > *effectors, float time)
MINLINE float normalize_v3(float n[3])
MINLINE void add_v3_v3(float r[3], const float a[3])
void SIM_hair_volume_add_segment(HairGrid *grid, const float[3], const float[3], const float x2[3], const float v2[3], const float x3[3], const float v3[3], const float[3], const float[3], const float[3], const float[3], const float[3])
@ CLOTH_SIMSETTINGS_FLAG_PRESSURE_VOL
@ CLOTH_SIMSETTINGS_FLAG_RESIST_SPRING_COMPRESS
@ CLOTH_SIMSETTINGS_FLAG_PRESSURE
MINLINE void sub_v3_v3v3(float r[3], const float a[3], const float b[3])
void pd_point_from_loc(struct Scene *scene, float *loc, float *vel, int index, struct EffectedPoint *point)
Definition effect.cc:424
HairGrid * SIM_hair_volume_create_vertex_grid(float cellsize, const float gmin[3], const float gmax[3])
void SIM_mass_spring_set_velocity(struct Implicit_Data *data, int index, const float v[3])
ATTR_WARN_UNUSED_RESULT const BMVert * v2
MINLINE float max_ff(float a, float b)
void SIM_mass_spring_solver_free(Implicit_Data *id)
static LinkNode * cloth_continuum_add_hair_segments(HairGrid *grid, const float cell_scale, const float cell_offset[3], Cloth *cloth, LinkNode *spring_link)
BLI_INLINE void cloth_get_grid_location(Implicit_Data *data, float cell_scale, const float cell_offset[3], int index, float x[3], float v[3])
BLI_INLINE void cloth_calc_spring_force(ClothModifierData *clmd, ClothSpring *s)
@ CLOTH_VERT_FLAG_PINNED
Definition BKE_cloth.hh:40
void SIM_cloth_solver_set_positions(ClothModifierData *clmd)
@ CLOTH_SPRING_FLAG_DEACTIVATE
Definition BKE_cloth.hh:199
@ CLOTH_SPRING_FLAG_NEEDED
Definition BKE_cloth.hh:200
static void cloth_continuum_step(ClothModifierData *clmd, float dt)
static int UNUSED_FUNCTION cloth_calc_helper_forces(Object *, ClothModifierData *clmd, float(*initial_cos)[3], float, float dt)
int SIM_cloth_solver_init(Object *, ClothModifierData *clmd)
void SIM_mass_spring_set_position(struct Implicit_Data *data, int index, const float x[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_hair_volume_solve_divergence(HairGrid *grid, float, float target_density, float target_strength)
VecBase< float, 3 > float3
void SIM_mass_spring_force_face_wind(struct Implicit_Data *data, int v1, int v2, int v3, const float(*winvec)[3])
void BKE_sim_debug_data_clear_category(const char *category)
Definition effect.cc:1398
MINLINE float len_v3v3(const float a[3], const float b[3]) ATTR_WARN_UNUSED_RESULT
static float I3[3][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
Scene * DEG_get_evaluated_scene(const Depsgraph *graph)
@ CLOTH_COLLSETTINGS_FLAG_ENABLED
@ CLOTH_COLLSETTINGS_FLAG_SELF
MINLINE int min_ii(int a, int b)
MINLINE float dot_v3v3(const float a[3], const float b[3]) ATTR_WARN_UNUSED_RESULT
MINLINE void zero_v3_int(int r[3])
void SIM_hair_volume_grid_geometry(HairGrid *grid, float *cellsize, int res[3], float gmin[3], float gmax[3])
static void hair_get_boundbox(ClothModifierData *clmd, float3 &gmin, float3 &gmax)
void SIM_mass_spring_get_position(struct Implicit_Data *data, int index, float x[3])
void SIM_mass_spring_force_face_extern(struct Implicit_Data *data, int v1, int v2, int v3, const float(*forcevec)[3])
MINLINE bool is_zero_v3(const float v[3]) ATTR_WARN_UNUSED_RESULT
void SIM_mass_spring_get_new_position(struct Implicit_Data *data, int index, float x[3])
ATTR_WARN_UNUSED_RESULT const BMVert const BMEdge * e
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 * MEM_new_array_zeroed(size_t len, size_t size, const char *str) ATTR_WARN_UNUSED_RESULT ATTR_ALLOC_SIZE(1
static void cloth_calc_pressure_gradient(ClothModifierData *clmd, const float gradient_vector[3], float *r_vertex_pressure)
void vert(const int vert_id, float4 &position)
#define fabsf
KERNEL_FEATURE_LIGHT_PASSES KERNEL_FEATURE_PATH_GUIDING float
CLOTH_COLLISIONSETTINGS_FLAGS flags
struct ClothSolverResult * solver_result
struct ClothHairData * hairdata
struct ClothSimSettings * sim_parms
struct ClothCollSettings * coll_parms
struct EffectorWeights * effector_weights
CLOTH_SIMSETTINGS_FLAGS flags
CLOTH_BENDING_MODEL bending_model
unsigned int impulse_count
Definition BKE_cloth.hh:108
BVHTree * bvhtree
Definition BKE_cloth.hh:79
struct LinkNode * next
struct PhysicsSettings physics_settings