Blender V5.3
cloth.cc
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: Blender Authors
2 *
3 * SPDX-License-Identifier: GPL-2.0-or-later */
4
8
9#include <algorithm>
10
11#include "MEM_guardedalloc.h"
12
13#include "DNA_cloth_types.h"
14#include "DNA_mesh_types.h"
15#include "DNA_meshdata_types.h"
16#include "DNA_object_types.h"
17#include "DNA_scene_types.h"
18
19#include "BLI_linklist.hh"
20#include "BLI_math_geom_c.hh"
21#include "BLI_math_matrix_c.hh"
23#include "BLI_math_vector.hh"
24#include "BLI_math_vector_c.hh"
25#include "BLI_rand_c.hh"
26
27#include "DEG_depsgraph.hh"
29
30#include "BKE_bvhutils.hh"
31#include "BKE_cloth.hh"
32#include "BKE_customdata.hh"
33#include "BKE_effect.h"
34#include "BKE_global.hh"
35#include "BKE_lib_id.hh"
36#include "BKE_mesh.hh"
37#include "BKE_modifier.hh"
38#include "BKE_pointcache.h"
39
40#include "SIM_mass_spring.h"
41
42// #include "BLI_time.hh" /* timing for debug prints */
43
44namespace blender {
45
46/* ********** cloth engine ******* */
47/* Prototypes for internal functions.
48 */
49static void cloth_to_object(Object *ob, ClothModifierData *clmd, float (*vertexCos)[3]);
50static void cloth_from_mesh(ClothModifierData *clmd, const Object *ob, const Mesh *mesh);
51static bool cloth_from_object(
52 Object *ob, ClothModifierData *clmd, const Mesh *mesh, float framenr, int first);
54static void cloth_update_verts(Object *ob, ClothModifierData *clmd, const Mesh *mesh);
56static bool cloth_build_springs(ClothModifierData *clmd, const Mesh *mesh);
57static void cloth_apply_vgroup(ClothModifierData *clmd, const Mesh *mesh);
58
64
65/* -------------------------------------------------------------------- */
68
70{
71 if (!clmd) {
72 return nullptr;
73 }
74
75 Cloth *cloth = clmd->clothObject;
76
77 if (!cloth) {
78 return nullptr;
79 }
80
81 ClothVertex *verts = cloth->verts;
82 const int3 *vert_tris = cloth->vert_tris;
83
84 /* in the moment, return zero if no faces there */
85 if (!cloth->primitive_num) {
86 return nullptr;
87 }
88
89 /* Create quad-tree with k=26. */
90 BVHTree *bvhtree = BLI_bvhtree_new(cloth->primitive_num, epsilon, 4, 26);
91
92 /* fill tree */
93 if (clmd->hairdata == nullptr) {
94 for (int i = 0; i < cloth->primitive_num; i++) {
95 float co[3][3];
96
97 copy_v3_v3(co[0], verts[vert_tris[i][0]].xold);
98 copy_v3_v3(co[1], verts[vert_tris[i][1]].xold);
99 copy_v3_v3(co[2], verts[vert_tris[i][2]].xold);
100
101 BLI_bvhtree_insert(bvhtree, i, co[0], 3);
102 }
103 }
104 else {
105 const int2 *edges = cloth->edges;
106
107 for (int i = 0; i < cloth->primitive_num; i++) {
108 float co[2][3];
109
110 copy_v3_v3(co[0], verts[edges[i][0]].xold);
111 copy_v3_v3(co[1], verts[edges[i][1]].xold);
112
113 BLI_bvhtree_insert(bvhtree, i, co[0], 2);
114 }
115 }
116
117 /* balance tree */
118 BLI_bvhtree_balance(bvhtree);
119
120 return bvhtree;
121}
122
123void bvhtree_update_from_cloth(ClothModifierData *clmd, bool moving, bool self)
124{
125 uint i = 0;
126 Cloth *cloth = clmd->clothObject;
127 BVHTree *bvhtree;
128 ClothVertex *verts = cloth->verts;
129
130 BLI_assert(!(clmd->hairdata != nullptr && self));
131
132 if (self) {
133 bvhtree = cloth->bvhselftree;
134 }
135 else {
136 bvhtree = cloth->bvhtree;
137 }
138
139 if (!bvhtree) {
140 return;
141 }
142
143 const int3 *vert_tris = cloth->vert_tris;
144
145 /* update vertex position in bvh tree */
146 if (clmd->hairdata == nullptr) {
147 if (verts && vert_tris) {
148 for (i = 0; i < cloth->primitive_num; i++) {
149 float co[3][3], co_moving[3][3];
150 bool ret;
151
152 /* copy new locations into array */
153 if (moving) {
154 copy_v3_v3(co[0], verts[vert_tris[i][0]].txold);
155 copy_v3_v3(co[1], verts[vert_tris[i][1]].txold);
156 copy_v3_v3(co[2], verts[vert_tris[i][2]].txold);
157
158 /* update moving positions */
159 copy_v3_v3(co_moving[0], verts[vert_tris[i][0]].tx);
160 copy_v3_v3(co_moving[1], verts[vert_tris[i][1]].tx);
161 copy_v3_v3(co_moving[2], verts[vert_tris[i][2]].tx);
162
163 ret = BLI_bvhtree_update_node(bvhtree, i, co[0], co_moving[0], 3);
164 }
165 else {
166 copy_v3_v3(co[0], verts[vert_tris[i][0]].tx);
167 copy_v3_v3(co[1], verts[vert_tris[i][1]].tx);
168 copy_v3_v3(co[2], verts[vert_tris[i][2]].tx);
169
170 ret = BLI_bvhtree_update_node(bvhtree, i, co[0], nullptr, 3);
171 }
172
173 /* check if tree is already full */
174 if (ret == false) {
175 break;
176 }
177 }
178
180 }
181 }
182 else {
183 if (verts) {
184 const int2 *edges = reinterpret_cast<const int2 *>(cloth->edges);
185
186 for (i = 0; i < cloth->primitive_num; i++) {
187 float co[2][3];
188
189 copy_v3_v3(co[0], verts[edges[i][0]].tx);
190 copy_v3_v3(co[1], verts[edges[i][1]].tx);
191
192 if (!BLI_bvhtree_update_node(bvhtree, i, co[0], nullptr, 2)) {
193 break;
194 }
195 }
196
198 }
199 }
200}
201
203
204/* -------------------------------------------------------------------- */
207
208void cloth_clear_cache(Object *ob, ClothModifierData *clmd, float framenr)
209{
210 PTCacheID pid;
211
212 BKE_ptcache_id_from_cloth(&pid, ob, clmd);
213
214 /* don't do anything as long as we're in editmode! */
215 if (pid.cache->edit && ob->mode & OB_MODE_PARTICLE_EDIT) {
216 return;
217 }
218
220}
221
222static bool do_init_cloth(Object *ob, ClothModifierData *clmd, const Mesh *result, int framenr)
223{
224 PointCache *cache;
225
226 cache = clmd->point_cache;
227
228 /* initialize simulation data if it didn't exist already */
229 if (clmd->clothObject == nullptr) {
230 if (!cloth_from_object(ob, clmd, result, framenr, 1)) {
232 BKE_modifier_set_error(ob, &(clmd->modifier), "Cannot initialize cloth");
233 return false;
234 }
235
236 if (clmd->clothObject == nullptr) {
238 BKE_modifier_set_error(ob, &(clmd->modifier), "Null cloth object");
239 return false;
240 }
241
243
244 ClothSimSettings *parms = clmd->sim_parms;
247 {
249 }
250
251 clmd->clothObject->last_frame = MINFRAME - 1;
252 clmd->sim_parms->dt = 1.0f / clmd->sim_parms->stepsPerFrame;
253 }
254
255 return true;
256}
257
258static int do_step_cloth(
259 Depsgraph *depsgraph, Object *ob, ClothModifierData *clmd, const Mesh *result, int framenr)
260{
261 /* simulate 1 frame forward */
262 ClothVertex *verts = nullptr;
263 Cloth *cloth;
264 ListBaseT<EffectorCache> *effectors = nullptr;
265 uint i = 0;
266 int ret = 0;
267 bool vert_mass_changed = false;
268
269 cloth = clmd->clothObject;
270 verts = cloth->verts;
271 const Span<float3> positions = result->vert_positions();
272 vert_mass_changed = verts->mass != clmd->sim_parms->mass;
273
274 /* force any pinned verts to their constrained location. */
275 for (i = 0; i < clmd->clothObject->mvert_num; i++, verts++) {
276 /* save the previous position. */
277 copy_v3_v3(verts->xold, verts->xconst);
278 copy_v3_v3(verts->txold, verts->x);
279
280 /* Get the current position. */
281 copy_v3_v3(verts->xconst, positions[i]);
282 mul_m4_v3(ob->object_to_world().ptr(), verts->xconst);
283
284 if (vert_mass_changed) {
285 verts->mass = clmd->sim_parms->mass;
287 }
288 }
289
290 effectors = BKE_effectors_create(
291 depsgraph, ob, nullptr, clmd->sim_parms->effector_weights, false);
292
294 cloth_update_verts(ob, clmd, result);
295 }
296
297 /* Support for dynamic vertex groups, changing from frame to frame */
299
301 (clmd->sim_parms->vgroup_shrink > 0) || (clmd->sim_parms->shrink_min != 0.0f))
302 {
304 }
305
307
308 // TIMEIT_START(cloth_step)
309
310 /* call the solver. */
311 ret = SIM_cloth_solve(depsgraph, ob, framenr, clmd, effectors);
312
313 // TIMEIT_END(cloth_step)
314
315 BKE_effectors_free(effectors);
316
317 // printf ( "%f\n", ( float ) tval() );
318
319 return ret;
320}
321
328 Depsgraph *depsgraph,
329 Scene *scene,
330 Object *ob,
331 const Mesh *mesh,
332 float (*vertexCos)[3])
333{
334 PointCache *cache;
335 PTCacheID pid;
336 float timescale;
337 int framenr, startframe, endframe;
338 int cache_result;
339
340 framenr = DEG_get_ctime(depsgraph);
341 cache = clmd->point_cache;
342
343 BKE_ptcache_id_from_cloth(&pid, ob, clmd);
344 BKE_ptcache_id_time(&pid, scene, framenr, &startframe, &endframe, &timescale);
345 clmd->sim_parms->timescale = timescale * clmd->sim_parms->time_scale;
346
347 if (clmd->sim_parms->reset ||
348 (clmd->clothObject && mesh->verts_num != clmd->clothObject->mvert_num))
349 {
350 clmd->sim_parms->reset = 0;
351 cache->flag |= PTCACHE_OUTDATED;
353 BKE_ptcache_validate(cache, 0);
354 cache->last_exact = 0;
355 cache->flag &= ~PTCACHE_REDO_NEEDED;
356 }
357
358 /* simulation is only active during a specific period */
359 if (framenr < startframe) {
361 return;
362 }
363 framenr = std::min(framenr, endframe);
364
365 /* initialize simulation data if it didn't exist already */
366 if (!do_init_cloth(ob, clmd, mesh, framenr)) {
367 return;
368 }
369
370 if (framenr == startframe) {
372 do_init_cloth(ob, clmd, mesh, framenr);
373 BKE_ptcache_validate(cache, framenr);
374 cache->flag &= ~PTCACHE_REDO_NEEDED;
375 clmd->clothObject->last_frame = framenr;
376 return;
377 }
378
379 /* Since implicit sharing is introduced, mesh data can be moved to other places.
380 * Therefore some fields in simulation data need to be updated accordingly */
381 clmd->clothObject->edges = mesh->edges().data();
382
383 /* try to read from cache */
384 bool can_simulate = (framenr == clmd->clothObject->last_frame + 1) &&
385 !(cache->flag & PTCACHE_BAKED);
386
387 cache_result = BKE_ptcache_read(&pid, float(framenr) + scene->r.subframe, can_simulate);
388
389 if (cache_result == PTCACHE_READ_EXACT || cache_result == PTCACHE_READ_INTERPOLATED ||
390 (!can_simulate && cache_result == PTCACHE_READ_OLD))
391 {
393 cloth_to_object(ob, clmd, vertexCos);
394
395 BKE_ptcache_validate(cache, framenr);
396
397 if (cache_result == PTCACHE_READ_INTERPOLATED && cache->flag & PTCACHE_REDO_NEEDED) {
398 BKE_ptcache_write(&pid, framenr);
399 }
400
401 clmd->clothObject->last_frame = framenr;
402
403 return;
404 }
405 if (cache_result == PTCACHE_READ_OLD) {
407 }
408 else if (
409 /* 2.4x disabled lib, but this can be used in some cases, testing further - campbell */
410 /*ob->id.lib ||*/ (cache->flag & PTCACHE_BAKED))
411 {
412 /* if baked and nothing in cache, do nothing */
414 return;
415 }
416
417 /* if on second frame, write cache for first frame */
418 if (cache->simframe == startframe && (cache->flag & PTCACHE_OUTDATED || cache->last_exact == 0))
419 {
420 BKE_ptcache_write(&pid, startframe);
421 }
422
423 clmd->sim_parms->timescale *= framenr - cache->simframe;
424
425 /* do simulation */
426 BKE_ptcache_validate(cache, framenr);
427
428 if (!do_step_cloth(depsgraph, ob, clmd, mesh, framenr)) {
430 }
431 else {
432 BKE_ptcache_write(&pid, framenr);
433 }
434
435 cloth_to_object(ob, clmd, vertexCos);
436 clmd->clothObject->last_frame = framenr;
437}
438
440
441/* -------------------------------------------------------------------- */
444
446{
447 Cloth *cloth = nullptr;
448
449 if (!clmd) {
450 return;
451 }
452
453 cloth = clmd->clothObject;
454
455 if (cloth) {
457
458 /* Free the verts. */
459 MEM_SAFE_DELETE(cloth->verts);
460 cloth->mvert_num = 0;
461
462 /* Free the springs. */
463 if (cloth->springs != nullptr) {
464 LinkNode *search = cloth->springs;
465 while (search) {
466 ClothSpring *spring = static_cast<ClothSpring *>(search->link);
467
468 MEM_SAFE_DELETE(spring->pa);
469 MEM_SAFE_DELETE(spring->pb);
470
471 MEM_delete(spring);
472 search = search->next;
473 }
474 BLI_linklist_free(cloth->springs, nullptr);
475
476 cloth->springs = nullptr;
477 }
478
479 cloth->springs = nullptr;
480 cloth->numsprings = 0;
481
482 /* free BVH collision tree */
483 if (cloth->bvhtree) {
484 BLI_bvhtree_free(cloth->bvhtree);
485 }
486
487 if (cloth->bvhselftree && cloth->bvhselftree != cloth->bvhtree) {
488 BLI_bvhtree_free(cloth->bvhselftree);
489 }
490
491 /* we save our faces for collision objects */
492 if (cloth->vert_tris) {
493 MEM_delete(cloth->vert_tris);
494 }
495
496#if 0
497 if (clmd->clothObject->facemarks) {
498 MEM_delete(clmd->clothObject->facemarks);
499 }
500#endif
501 MEM_delete(cloth);
502 clmd->clothObject = nullptr;
503 }
504}
505
507{
508 Cloth *cloth = nullptr;
509 if (G.debug & G_DEBUG_SIMDATA) {
510 printf("cloth_free_modifier_extern\n");
511 }
512
513 if (!clmd) {
514 return;
515 }
516
517 cloth = clmd->clothObject;
518
519 if (cloth) {
520 if (G.debug & G_DEBUG_SIMDATA) {
521 printf("cloth_free_modifier_extern in\n");
522 }
523
525
526 /* Free the verts. */
527 MEM_SAFE_DELETE(cloth->verts);
528 cloth->mvert_num = 0;
529
530 /* Free the springs. */
531 if (cloth->springs != nullptr) {
532 LinkNode *search = cloth->springs;
533 while (search) {
534 ClothSpring *spring = static_cast<ClothSpring *>(search->link);
535
536 MEM_SAFE_DELETE(spring->pa);
537 MEM_SAFE_DELETE(spring->pb);
538
539 MEM_delete(spring);
540 search = search->next;
541 }
542 BLI_linklist_free(cloth->springs, nullptr);
543
544 cloth->springs = nullptr;
545 }
546
547 cloth->springs = nullptr;
548 cloth->numsprings = 0;
549
550 /* free BVH collision tree */
551 if (cloth->bvhtree) {
552 BLI_bvhtree_free(cloth->bvhtree);
553 }
554
555 if (cloth->bvhselftree && cloth->bvhselftree != cloth->bvhtree) {
556 BLI_bvhtree_free(cloth->bvhselftree);
557 }
558
559 /* we save our faces for collision objects */
560 if (cloth->vert_tris) {
561 MEM_delete(cloth->vert_tris);
562 }
563
564#if 0
565 if (clmd->clothObject->facemarks) {
566 MEM_delete(clmd->clothObject->facemarks);
567 }
568#endif
569 MEM_delete(cloth);
570 clmd->clothObject = nullptr;
571 }
572}
573
575
576/* -------------------------------------------------------------------- */
579
583static void cloth_to_object(Object *ob, ClothModifierData *clmd, float (*vertexCos)[3])
584{
585 uint i = 0;
586 Cloth *cloth = clmd->clothObject;
587
588 if (clmd->clothObject) {
589 /* Inverse matrix is not up to date. */
590 invert_m4_m4(ob->runtime->world_to_object.ptr(), ob->object_to_world().ptr());
591
592 for (i = 0; i < cloth->mvert_num; i++) {
593 copy_v3_v3(vertexCos[i], cloth->verts[i].x);
594 mul_m4_v3(ob->world_to_object().ptr(), vertexCos[i]); /* cloth is in global coords */
595 }
596 }
597}
598
600{
601 return (((clmd->coll_parms->flags & CLOTH_COLLSETTINGS_FLAG_SELF) &&
602 (clmd->coll_parms->vgroup_selfcol > 0)) ||
604 (clmd->coll_parms->vgroup_objcol > 0)) ||
605 (clmd->sim_parms->vgroup_pressure > 0) || (clmd->sim_parms->vgroup_struct > 0) ||
606 (clmd->sim_parms->vgroup_bend > 0) || (clmd->sim_parms->vgroup_shrink > 0) ||
607 (clmd->sim_parms->vgroup_intern > 0) || (clmd->sim_parms->vgroup_mass > 0) ||
608 (clmd->sim_parms->vgroup_shear > 0));
609}
610
615{
616 if (!clmd || !mesh) {
617 return;
618 }
619
620 int mvert_num = mesh->verts_num;
621
622 ClothVertex *verts = clmd->clothObject->verts;
623
624 const Span<MDeformVert> dverts = mesh->deform_verts();
625
626 if (cloth_uses_vgroup(clmd)) {
627 for (int i = 0; i < mvert_num; i++, verts++) {
628
629 /* Reset Goal values to standard */
630 if (clmd->sim_parms->vgroup_mass > 0) {
631 verts->goal = clmd->sim_parms->defgoal;
632 }
633 else {
634 verts->goal = 0.0f;
635 }
636
637 /* Compute base cloth shrink weight */
638 verts->shrink_factor = 0.0f;
639
640 /* Reset vertex flags */
643
644 if (!dverts.is_empty()) {
645 const MDeformVert *dvert = &dverts[i];
646 for (int j = 0; j < dvert->totweight; j++) {
647 if (dvert->dw[j].def_nr == (clmd->sim_parms->vgroup_mass - 1)) {
648 verts->goal = dvert->dw[j].weight;
649
650 // goalfac = 1.0f; /* UNUSED */
651
652 /* Kicking goal factor to simplify things...who uses that anyway? */
653 // ABS (clmd->sim_parms->maxgoal - clmd->sim_parms->mingoal);
654
655 verts->goal = pow4f(verts->goal);
656 if (verts->goal >= SOFTGOALSNAP) {
658 }
659 }
660
661 if (dvert->dw[j].def_nr == (clmd->sim_parms->vgroup_struct - 1)) {
662 verts->struct_stiff = dvert->dw[j].weight;
663 }
664
665 if (dvert->dw[j].def_nr == (clmd->sim_parms->vgroup_shear - 1)) {
666 verts->shear_stiff = dvert->dw[j].weight;
667 }
668
669 if (dvert->dw[j].def_nr == (clmd->sim_parms->vgroup_bend - 1)) {
670 verts->bend_stiff = dvert->dw[j].weight;
671 }
672
673 if (dvert->dw[j].def_nr == (clmd->coll_parms->vgroup_selfcol - 1)) {
674 if (dvert->dw[j].weight > 0.0f) {
676 }
677 }
678
679 if (dvert->dw[j].def_nr == (clmd->coll_parms->vgroup_objcol - 1)) {
680 if (dvert->dw[j].weight > 0.0f) {
682 }
683 }
684
685 if (dvert->dw[j].def_nr == (clmd->sim_parms->vgroup_shrink - 1)) {
686 /* Used for linear interpolation between min and max
687 * shrink factor based on weight. */
688 verts->shrink_factor = dvert->dw[j].weight;
689 }
690
691 if (dvert->dw[j].def_nr == (clmd->sim_parms->vgroup_intern - 1)) {
692 /* Used to define the stiffness weight on the internal spring connected to this vertex.
693 */
694 verts->internal_stiff = dvert->dw[j].weight;
695 }
696
697 if (dvert->dw[j].def_nr == (clmd->sim_parms->vgroup_pressure - 1)) {
698 /* Used to define how much the pressure settings should affect the given vertex. */
699 verts->pressure_factor = dvert->dw[j].weight;
700 }
701 }
702 }
703 }
704 }
705}
706
707static float cloth_shrink_factor(ClothModifierData *clmd, ClothVertex *verts, int i1, int i2)
708{
709 /* Linear interpolation between min and max shrink factor based on weight. */
710 float base = 1.0f - clmd->sim_parms->shrink_min;
711 float shrink_factor_delta = clmd->sim_parms->shrink_min - clmd->sim_parms->shrink_max;
712
713 float k1 = base + shrink_factor_delta * verts[i1].shrink_factor;
714 float k2 = base + shrink_factor_delta * verts[i2].shrink_factor;
715
716 /* Use geometrical mean to average two factors since it behaves better
717 * for diagonals when a rectangle transforms into a trapezoid. */
718 return sqrtf(k1 * k2);
719}
720
722 Object *ob, ClothModifierData *clmd, const Mesh *mesh, float /*framenr*/, int first)
723{
724 int i = 0;
725 ClothVertex *verts = nullptr;
726 const float (*shapekey_rest)[3] = nullptr;
727 const float tnull[3] = {0, 0, 0};
728
729 /* If we have a clothObject, free it. */
730 if (clmd->clothObject != nullptr) {
732 if (G.debug & G_DEBUG_SIMDATA) {
733 printf("cloth_free_modifier cloth_from_object\n");
734 }
735 }
736
737 /* Allocate a new cloth object. */
738 clmd->clothObject = MEM_new<Cloth>(__func__);
739 if (clmd->clothObject) {
740 clmd->clothObject->old_solver_type = 255;
741 }
742 else {
743 BKE_modifier_set_error(ob, &(clmd->modifier), "Out of memory on allocating cloth object");
744 return false;
745 }
746
747 /* mesh input objects need Mesh */
748 if (!mesh) {
749 return false;
750 }
751
752 cloth_from_mesh(clmd, ob, mesh);
753
754 /* create springs */
755 clmd->clothObject->springs = nullptr;
756 clmd->clothObject->numsprings = -1;
757
758 if (clmd->sim_parms->shapekey_rest &&
760 {
761 shapekey_rest = static_cast<const float (*)[3]>(
763 }
764
765 const Span<float3> positions = mesh->vert_positions();
766
767 verts = clmd->clothObject->verts;
768
769 /* set initial values */
770 for (i = 0; i < mesh->verts_num; i++, verts++) {
771 if (first) {
772 copy_v3_v3(verts->x, positions[i]);
773
774 mul_m4_v3(ob->object_to_world().ptr(), verts->x);
775
776 if (shapekey_rest) {
777 copy_v3_v3(verts->xrest, shapekey_rest[i]);
778 mul_m4_v3(ob->object_to_world().ptr(), verts->xrest);
779 }
780 else {
781 copy_v3_v3(verts->xrest, verts->x);
782 }
783 }
784
785 /* no GUI interface yet */
786 verts->mass = clmd->sim_parms->mass;
787 verts->impulse_count = 0;
788
789 if (clmd->sim_parms->vgroup_mass > 0) {
790 verts->goal = clmd->sim_parms->defgoal;
791 }
792 else {
793 verts->goal = 0.0f;
794 }
795
796 verts->shrink_factor = 0.0f;
797
798 verts->flags = 0;
799 copy_v3_v3(verts->xold, verts->x);
800 copy_v3_v3(verts->xconst, verts->x);
801 copy_v3_v3(verts->txold, verts->x);
802 copy_v3_v3(verts->tx, verts->x);
803 mul_v3_fl(verts->v, 0.0f);
804
805 verts->impulse_count = 0;
806 copy_v3_v3(verts->impulse, tnull);
807 }
808
809 /* apply / set vertex groups */
810 /* has to be happen before springs are build! */
812
813 if (!cloth_build_springs(clmd, mesh)) {
815 BKE_modifier_set_error(ob, &(clmd->modifier), "Cannot build springs");
816 return false;
817 }
818
819 /* init our solver */
820 SIM_cloth_solver_init(ob, clmd);
821
822 if (!first) {
824 }
825
827
828 if (compare_ff(clmd->coll_parms->selfepsilon, clmd->coll_parms->epsilon, 1e-6f)) {
829 /* Share the BVH tree if the epsilon is the same. */
831 }
832 else {
834 }
835
836 return true;
837}
838
839static void cloth_from_mesh(ClothModifierData *clmd, const Object *ob, const Mesh *mesh)
840{
841 const Span<int> corner_verts = mesh->corner_verts();
842 const Span<int3> corner_tris = mesh->corner_tris();
843 const uint mvert_num = mesh->verts_num;
844
845 /* Allocate our vertices. */
846 clmd->clothObject->mvert_num = mvert_num;
848 __func__);
849 if (clmd->clothObject->verts == nullptr) {
851 BKE_modifier_set_error(ob, &(clmd->modifier), "Out of memory on allocating vertices");
852 return;
853 }
854
855 /* save face information */
856 if (clmd->hairdata == nullptr) {
857 clmd->clothObject->primitive_num = corner_tris.size();
858 }
859 else {
860 clmd->clothObject->primitive_num = mesh->edges_num;
861 }
862
863 clmd->clothObject->vert_tris = MEM_new_array_uninitialized<int3>(size_t(corner_tris.size()),
864 __func__);
865 if (clmd->clothObject->vert_tris == nullptr) {
867 BKE_modifier_set_error(ob, &(clmd->modifier), "Out of memory on allocating triangles");
868 return;
869 }
871 corner_verts, corner_tris, {clmd->clothObject->vert_tris, corner_tris.size()});
872
873 clmd->clothObject->edges = mesh->edges().data();
874
875 /* Free the springs since they can't be correct if the vertices
876 * changed.
877 */
878 if (clmd->clothObject->springs != nullptr) {
879 MEM_delete(clmd->clothObject->springs);
880 }
881}
882
884
885/* -------------------------------------------------------------------- */
888
890{
891 if (v0 < v1) {
892 spring->ij = v0;
893 spring->kl = v1;
894 }
895 else {
896 spring->ij = v1;
897 spring->kl = v0;
898 }
899}
900
901static void cloth_free_edgelist(LinkNodePair *edgelist, uint mvert_num)
902{
903 if (edgelist) {
904 for (uint i = 0; i < mvert_num; i++) {
905 BLI_linklist_free(edgelist[i].list, nullptr);
906 }
907
908 MEM_delete(edgelist);
909 }
910}
911
913 LinkNodePair *edgelist,
914 BendSpringRef *spring_ref)
915{
916 if (cloth->springs != nullptr) {
917 LinkNode *search = cloth->springs;
918 while (search) {
919 ClothSpring *spring = static_cast<ClothSpring *>(search->link);
920
921 MEM_SAFE_DELETE(spring->pa);
922 MEM_SAFE_DELETE(spring->pb);
923
924 MEM_delete(spring);
925 search = search->next;
926 }
927 BLI_linklist_free(cloth->springs, nullptr);
928
929 cloth->springs = nullptr;
930 }
931
932 cloth_free_edgelist(edgelist, cloth->mvert_num);
933
934 MEM_SAFE_DELETE(spring_ref);
935
936 cloth->edgeset.clear();
937}
938
940 ClothVertex *verts, int i, int j, const int *inds, int len, float r_dir[3])
941{
942 float cent[3] = {0};
943 float fact = 1.0f / len;
944
945 for (int x = 0; x < len; x++) {
946 madd_v3_v3fl(cent, verts[inds[x]].xrest, fact);
947 }
948
949 normal_tri_v3(r_dir, verts[i].xrest, verts[j].xrest, cent);
950}
951
953 ClothVertex *verts, int i, int j, int *i_a, int *i_b, int len_a, int len_b)
954{
955 float dir_a[3], dir_b[3];
956 float tmp[3], vec_e[3];
957 float sin, cos;
958
959 /* Poly vectors. */
960 cloth_bend_poly_dir(verts, j, i, i_a, len_a, dir_a);
961 cloth_bend_poly_dir(verts, i, j, i_b, len_b, dir_b);
962
963 /* Edge vector. */
964 sub_v3_v3v3(vec_e, verts[i].xrest, verts[j].xrest);
965 normalize_v3(vec_e);
966
967 /* Compute angle. */
968 cos = dot_v3v3(dir_a, dir_b);
969
970 cross_v3_v3v3(tmp, dir_a, dir_b);
971 sin = dot_v3v3(tmp, vec_e);
972
973 return atan2f(sin, cos);
974}
975
977{
978 Cloth *cloth = clmd->clothObject;
979 LinkNode *search = nullptr;
980 float hair_frame[3][3], dir_old[3], dir_new[3];
981 int prev_mn; /* to find hair chains */
982
983 if (!clmd->hairdata) {
984 return;
985 }
986
987 /* XXX NOTE: we need to propagate frames from the root up,
988 * but structural hair springs are stored in reverse order.
989 * The bending springs however are then inserted in the same
990 * order as vertices again ...
991 * This messy situation can be resolved when solver data is
992 * generated directly from a dedicated hair system.
993 */
994
995 prev_mn = -1;
996 for (search = cloth->springs; search; search = search->next) {
997 ClothSpring *spring = static_cast<ClothSpring *>(search->link);
998 ClothHairData *hair_ij, *hair_kl;
999 bool is_root = spring->kl != prev_mn;
1000
1001 if (spring->type != CLOTH_SPRING_TYPE_BENDING_HAIR) {
1002 continue;
1003 }
1004
1005 hair_ij = &clmd->hairdata[spring->ij];
1006 hair_kl = &clmd->hairdata[spring->kl];
1007 if (is_root) {
1008 /* initial hair frame from root orientation */
1009 copy_m3_m3(hair_frame, hair_ij->rot);
1010 /* surface normal is the initial direction,
1011 * parallel transport then keeps it aligned to the hair direction
1012 */
1013 copy_v3_v3(dir_new, hair_frame[2]);
1014 }
1015
1016 copy_v3_v3(dir_old, dir_new);
1017 sub_v3_v3v3(dir_new, cloth->verts[spring->mn].x, cloth->verts[spring->kl].x);
1018 normalize_v3(dir_new);
1019
1020 /* get local targets for kl/mn vertices by putting rest targets into the current frame,
1021 * then multiply with the rest length to get the actual goals
1022 */
1023
1024 mul_v3_m3v3(spring->target, hair_frame, hair_kl->rest_target);
1025 mul_v3_fl(spring->target, spring->restlen);
1026
1027 /* move frame to next hair segment */
1028 cloth_parallel_transport_hair_frame(hair_frame, dir_old, dir_new);
1029
1030 prev_mn = spring->mn;
1031 }
1032}
1033
1035{
1036 Cloth *cloth = clmd->clothObject;
1037 LinkNode *search = nullptr;
1038 float hair_frame[3][3], dir_old[3], dir_new[3];
1039 int prev_mn; /* to find hair roots */
1040
1041 if (!clmd->hairdata) {
1042 return;
1043 }
1044
1045 /* XXX NOTE: we need to propagate frames from the root up,
1046 * but structural hair springs are stored in reverse order.
1047 * The bending springs however are then inserted in the same
1048 * order as vertices again ...
1049 * This messy situation can be resolved when solver data is
1050 * generated directly from a dedicated hair system.
1051 */
1052
1053 prev_mn = -1;
1054 for (search = cloth->springs; search; search = search->next) {
1055 ClothSpring *spring = static_cast<ClothSpring *>(search->link);
1056 ClothHairData *hair_ij, *hair_kl;
1057 bool is_root = spring->kl != prev_mn;
1058
1059 if (spring->type != CLOTH_SPRING_TYPE_BENDING_HAIR) {
1060 continue;
1061 }
1062
1063 hair_ij = &clmd->hairdata[spring->ij];
1064 hair_kl = &clmd->hairdata[spring->kl];
1065 if (is_root) {
1066 /* initial hair frame from root orientation */
1067 copy_m3_m3(hair_frame, hair_ij->rot);
1068 /* surface normal is the initial direction,
1069 * parallel transport then keeps it aligned to the hair direction
1070 */
1071 copy_v3_v3(dir_new, hair_frame[2]);
1072 }
1073
1074 copy_v3_v3(dir_old, dir_new);
1075 sub_v3_v3v3(dir_new, cloth->verts[spring->mn].xrest, cloth->verts[spring->kl].xrest);
1076 normalize_v3(dir_new);
1077
1078 /* dir expressed in the hair frame defines the rest target direction */
1079 copy_v3_v3(hair_kl->rest_target, dir_new);
1080 mul_transposed_m3_v3(hair_frame, hair_kl->rest_target);
1081
1082 /* move frame to next hair segment */
1083 cloth_parallel_transport_hair_frame(hair_frame, dir_old, dir_new);
1084
1085 prev_mn = spring->mn;
1086 }
1087}
1088
1089/* update stiffness if vertex group values are changing from frame to frame */
1091{
1092 Cloth *cloth = clmd->clothObject;
1093 LinkNode *search = nullptr;
1094
1095 search = cloth->springs;
1096 while (search) {
1097 ClothSpring *spring = static_cast<ClothSpring *>(search->link);
1098
1099 spring->lin_stiffness = 0.0f;
1100
1102 if (spring->type & CLOTH_SPRING_TYPE_BENDING) {
1103 spring->ang_stiffness = (cloth->verts[spring->kl].bend_stiff +
1104 cloth->verts[spring->ij].bend_stiff) /
1105 2.0f;
1106 }
1107 }
1108
1109 if (spring->type & CLOTH_SPRING_TYPE_STRUCTURAL) {
1110 spring->lin_stiffness = (cloth->verts[spring->kl].struct_stiff +
1111 cloth->verts[spring->ij].struct_stiff) /
1112 2.0f;
1113 }
1114 else if (spring->type & CLOTH_SPRING_TYPE_SHEAR) {
1115 spring->lin_stiffness = (cloth->verts[spring->kl].shear_stiff +
1116 cloth->verts[spring->ij].shear_stiff) /
1117 2.0f;
1118 }
1119 else if (spring->type == CLOTH_SPRING_TYPE_BENDING) {
1120 spring->lin_stiffness = (cloth->verts[spring->kl].bend_stiff +
1121 cloth->verts[spring->ij].bend_stiff) /
1122 2.0f;
1123 }
1124 else if (spring->type & CLOTH_SPRING_TYPE_INTERNAL) {
1125 spring->lin_stiffness = (cloth->verts[spring->kl].internal_stiff +
1126 cloth->verts[spring->ij].internal_stiff) /
1127 2.0f;
1128 }
1129 else if (spring->type == CLOTH_SPRING_TYPE_BENDING_HAIR) {
1130 ClothVertex *v1 = &cloth->verts[spring->ij];
1131 ClothVertex *v2 = &cloth->verts[spring->kl];
1132 if (clmd->hairdata) {
1133 /* copy extra hair data to generic cloth vertices */
1134 v1->bend_stiff = clmd->hairdata[spring->ij].bending_stiffness;
1135 v2->bend_stiff = clmd->hairdata[spring->kl].bending_stiffness;
1136 }
1137 spring->lin_stiffness = (v1->bend_stiff + v2->bend_stiff) / 2.0f;
1138 }
1139 else if (spring->type == CLOTH_SPRING_TYPE_GOAL) {
1140 /* WARNING: Appending NEW goal springs does not work
1141 * because implicit solver would need reset! */
1142
1143 /* Activate / Deactivate existing springs */
1144 if (!(cloth->verts[spring->ij].flags & CLOTH_VERT_FLAG_PINNED) &&
1145 (cloth->verts[spring->ij].goal > ALMOST_ZERO))
1146 {
1148 }
1149 else {
1151 }
1152 }
1153
1154 search = search->next;
1155 }
1156
1158}
1159
1160/* Update rest verts, for dynamically deformable cloth */
1161static void cloth_update_verts(Object *ob, ClothModifierData *clmd, const Mesh *mesh)
1162{
1163 uint i = 0;
1164 const Span<float3> positions = mesh->vert_positions();
1165 ClothVertex *verts = clmd->clothObject->verts;
1166
1167 /* vertex count is already ensured to match */
1168 for (i = 0; i < mesh->verts_num; i++, verts++) {
1169 copy_v3_v3(verts->xrest, positions[i]);
1170 mul_m4_v3(ob->object_to_world().ptr(), verts->xrest);
1171 }
1172}
1173
1174/* Write rest vert locations to a copy of the mesh. */
1176{
1177 Mesh *new_mesh = BKE_mesh_copy_for_eval(*mesh);
1178 ClothVertex *verts = clmd->clothObject->verts;
1179 MutableSpan<float3> positions = new_mesh->vert_positions_for_write();
1180
1181 /* vertex count is already ensured to match */
1182 for (const int i : positions.index_range()) {
1183 positions[i] = verts[i].xrest;
1184 }
1185 new_mesh->tag_positions_changed();
1186
1187 return new_mesh;
1188}
1189
1190/* Update spring rest length, for dynamically deformable cloth */
1192{
1193 Cloth *cloth = clmd->clothObject;
1194 LinkNode *search = cloth->springs;
1195 uint struct_springs = 0;
1196 uint i = 0;
1197 uint mvert_num = uint(mesh->verts_num);
1198 float shrink_factor;
1199
1200 clmd->sim_parms->avg_spring_len = 0.0f;
1201
1202 for (i = 0; i < mvert_num; i++) {
1203 cloth->verts[i].avg_spring_len = 0.0f;
1204 }
1205
1206 while (search) {
1207 ClothSpring *spring = static_cast<ClothSpring *>(search->link);
1208
1209 if (spring->type != CLOTH_SPRING_TYPE_SEWING) {
1212 {
1213 shrink_factor = cloth_shrink_factor(clmd, cloth->verts, spring->ij, spring->kl);
1214 }
1215 else {
1216 shrink_factor = 1.0f;
1217 }
1218
1219 spring->restlen = len_v3v3(cloth->verts[spring->kl].xrest, cloth->verts[spring->ij].xrest) *
1220 shrink_factor;
1221
1222 if (spring->type & CLOTH_SPRING_TYPE_BENDING) {
1223 spring->restang = cloth_spring_angle(
1224 cloth->verts, spring->ij, spring->kl, spring->pa, spring->pb, spring->la, spring->lb);
1225 }
1226 }
1227
1228 if (spring->type & CLOTH_SPRING_TYPE_STRUCTURAL) {
1229 clmd->sim_parms->avg_spring_len += spring->restlen;
1230 cloth->verts[spring->ij].avg_spring_len += spring->restlen;
1231 cloth->verts[spring->kl].avg_spring_len += spring->restlen;
1232 struct_springs++;
1233 }
1234
1235 search = search->next;
1236 }
1237
1238 if (struct_springs > 0) {
1239 clmd->sim_parms->avg_spring_len /= struct_springs;
1240 }
1241
1242 for (i = 0; i < mvert_num; i++) {
1243 if (cloth->verts[i].spring_count > 0) {
1244 cloth->verts[i].avg_spring_len = cloth->verts[i].avg_spring_len * 0.49f /
1245 float(cloth->verts[i].spring_count);
1246 }
1247 }
1248}
1249
1250BLI_INLINE void cross_identity_v3(float r[3][3], const float v[3])
1251{
1252 zero_m3(r);
1253 r[0][1] = v[2];
1254 r[0][2] = -v[1];
1255 r[1][0] = -v[2];
1256 r[1][2] = v[0];
1257 r[2][0] = v[1];
1258 r[2][1] = -v[0];
1259}
1260
1261BLI_INLINE void madd_m3_m3fl(float r[3][3], const float m[3][3], float f)
1262{
1263 r[0][0] += m[0][0] * f;
1264 r[0][1] += m[0][1] * f;
1265 r[0][2] += m[0][2] * f;
1266 r[1][0] += m[1][0] * f;
1267 r[1][1] += m[1][1] * f;
1268 r[1][2] += m[1][2] * f;
1269 r[2][0] += m[2][0] * f;
1270 r[2][1] += m[2][1] * f;
1271 r[2][2] += m[2][2] * f;
1272}
1273
1275 const float dir_old[3],
1276 const float dir_new[3])
1277{
1278 float rot[3][3];
1279
1280 /* rotation between segments */
1281 rotation_between_vecs_to_mat3(rot, dir_old, dir_new);
1282
1283 /* rotate the frame */
1284 mul_m3_m3m3(mat, rot, mat);
1285}
1286
1287/* Add a shear and a bend spring between two verts within a face. */
1289 LinkNodePair *edgelist,
1290 const Span<int> corner_verts,
1291 const OffsetIndices<int> faces,
1292 int i,
1293 int j,
1294 int k)
1295{
1296 Cloth *cloth = clmd->clothObject;
1297 ClothSpring *spring;
1298 const int *tmp_corner;
1299 float shrink_factor;
1300 int x, y;
1301
1302 /* Combined shear/bend properties. */
1303 spring = MEM_new_zeroed<ClothSpring>("cloth spring");
1304
1305 if (!spring) {
1306 return false;
1307 }
1308
1309 spring_verts_ordered_set(spring, corner_verts[faces[i][j]], corner_verts[faces[i][k]]);
1310
1311 shrink_factor = cloth_shrink_factor(clmd, cloth->verts, spring->ij, spring->kl);
1312 spring->restlen = len_v3v3(cloth->verts[spring->kl].xrest, cloth->verts[spring->ij].xrest) *
1313 shrink_factor;
1314 spring->type |= CLOTH_SPRING_TYPE_SHEAR;
1315 spring->lin_stiffness = (cloth->verts[spring->kl].shear_stiff +
1316 cloth->verts[spring->ij].shear_stiff) /
1317 2.0f;
1318
1319 if (edgelist) {
1320 BLI_linklist_append(&edgelist[spring->ij], spring);
1321 BLI_linklist_append(&edgelist[spring->kl], spring);
1322 }
1323
1324 /* Bending specific properties. */
1327
1328 spring->la = k - j + 1;
1329 spring->lb = faces[i].size() - k + j + 1;
1330
1331 spring->pa = MEM_new_array_uninitialized<int>(size_t(spring->la), "spring poly");
1332 if (!spring->pa) {
1333 return false;
1334 }
1335
1336 spring->pb = MEM_new_array_uninitialized<int>(size_t(spring->lb), "spring poly");
1337 if (!spring->pb) {
1338 return false;
1339 }
1340
1341 tmp_corner = &corner_verts[faces[i].start()];
1342
1343 for (x = 0; x < spring->la; x++) {
1344 spring->pa[x] = tmp_corner[j + x];
1345 }
1346
1347 for (x = 0; x <= j; x++) {
1348 spring->pb[x] = tmp_corner[x];
1349 }
1350
1351 for (y = k; y < faces[i].size(); x++, y++) {
1352 spring->pb[x] = tmp_corner[y];
1353 }
1354
1355 spring->mn = -1;
1356
1357 spring->restang = cloth_spring_angle(
1358 cloth->verts, spring->ij, spring->kl, spring->pa, spring->pb, spring->la, spring->lb);
1359
1360 spring->ang_stiffness = (cloth->verts[spring->ij].bend_stiff +
1361 cloth->verts[spring->kl].bend_stiff) /
1362 2.0f;
1363 }
1364
1365 BLI_linklist_prepend(&cloth->springs, spring);
1366
1367 return true;
1368}
1369
1370BLI_INLINE bool cloth_bend_set_poly_vert_array(int **poly, int len, const int *corner_verts)
1371{
1372 int *p = MEM_new_array_uninitialized<int>(size_t(len), "spring poly");
1373
1374 if (!p) {
1375 return false;
1376 }
1377
1378 for (int i = 0; i < len; i++) {
1379 p[i] = corner_verts[i];
1380 }
1381
1382 *poly = p;
1383
1384 return true;
1385}
1386
1388 const Span<float3> vert_positions,
1389 const Span<int> corner_verts,
1390 const Span<int3> corner_tris,
1391 const Span<float3> vert_normals,
1392 uint v_idx,
1393 RNG *rng,
1394 float max_length,
1395 float max_diversion,
1396 bool check_normal,
1397 int *r_tar_v_idx)
1398{
1399 float co[3], no[3], new_co[3];
1400
1401 copy_v3_v3(co, vert_positions[v_idx]);
1402 negate_v3_v3(no, vert_normals[v_idx]);
1403
1404 float vec_len = sin(max_diversion);
1405 float offset[3];
1406
1407 offset[0] = 0.5f - BLI_rng_get_float(rng);
1408 offset[1] = 0.5f - BLI_rng_get_float(rng);
1409 offset[2] = 0.5f - BLI_rng_get_float(rng);
1410
1411 normalize_v3(offset);
1412 mul_v3_fl(offset, vec_len);
1413 add_v3_v3(no, offset);
1414 normalize_v3(no);
1415
1416 /* Nudge the start point so we do not hit it with the ray. */
1417 copy_v3_v3(new_co, no);
1418 mul_v3_fl(new_co, FLT_EPSILON);
1419 add_v3_v3(new_co, co);
1420
1421 if (max_length == 0.0f) {
1422 max_length = FLT_MAX;
1423 }
1424
1425 const bke::bvh::Ray ray(new_co, no, max_length);
1426 const std::optional<bke::bvh::RayHit> rayhit = treedata.ray_intersect(ray);
1427 if (!rayhit) {
1428 return false;
1429 }
1430
1431 int vert_idx = -1;
1432
1433 if (rayhit->distance <= max_length) {
1434 if (check_normal && dot_v3v3(math::normalize(rayhit->normal), no) < 0.0f) {
1435 /* We hit a point that points in the same direction as our starting point. */
1436 return false;
1437 }
1438
1439 float min_len = FLT_MAX;
1440 const int3 &tri = corner_tris[rayhit->index];
1441
1442 for (int i = 0; i < 3; i++) {
1443 int tmp_vert_idx = corner_verts[tri[i]];
1444 if (tmp_vert_idx == v_idx) {
1445 /* We managed to hit ourselves. */
1446 return false;
1447 }
1448
1449 float len = len_v3v3(co, rayhit->position(ray));
1450 if (len < min_len) {
1451 min_len = len;
1452 vert_idx = tmp_vert_idx;
1453 }
1454 }
1455
1456 *r_tar_v_idx = vert_idx;
1457 return true;
1458 }
1459
1460 return false;
1461}
1462
1464{
1465 using namespace blender::bke;
1466 Cloth *cloth = clmd->clothObject;
1467 ClothSpring *spring = nullptr, *tspring = nullptr, *tspring2 = nullptr;
1468 uint struct_springs = 0, shear_springs = 0, bend_springs = 0, struct_springs_real = 0;
1469 uint mvert_num = uint(mesh->verts_num);
1470 uint numedges = uint(mesh->edges_num);
1471 uint numface = uint(mesh->faces_num);
1472 float shrink_factor;
1473 const Span<int2> edges = mesh->edges();
1474 const OffsetIndices faces = mesh->faces();
1475 const Span<int> corner_verts = mesh->corner_verts();
1476 const Span<int> corner_edges = mesh->corner_edges();
1477 int index2 = 0; /* our second vertex index */
1478 LinkNodePair *edgelist = nullptr;
1479 LinkNode *search = nullptr, *search2 = nullptr;
1480 BendSpringRef *spring_ref = nullptr;
1481
1482 /* error handling */
1483 if (numedges == 0) {
1484 return false;
1485 }
1486
1487 /* NOTE: handling ownership of springs and edgeset is quite sloppy
1488 * currently they are never initialized but assert just to be sure */
1489 BLI_assert(cloth->springs == nullptr);
1490 BLI_assert(cloth->edgeset.is_empty());
1491
1492 cloth->springs = nullptr;
1493
1495 spring_ref = MEM_new_array_zeroed<BendSpringRef>(numedges, __func__);
1496
1497 if (!spring_ref) {
1498 return false;
1499 }
1500 }
1501 else {
1502 edgelist = MEM_new_array_zeroed<LinkNodePair>(mvert_num, __func__);
1503
1504 if (!edgelist) {
1505 return false;
1506 }
1507 }
1508
1509 bool use_internal_springs = (clmd->sim_parms->flags & CLOTH_SIMSETTINGS_FLAG_INTERNAL_SPRINGS);
1510
1511 if (use_internal_springs && numface > 0) {
1512 int tar_v_idx;
1513 Mesh *tmp_mesh = nullptr;
1514 RNG *rng;
1515
1516 /* If using the rest shape key, it's necessary to make a copy of the mesh. */
1517 if (clmd->sim_parms->shapekey_rest &&
1519 {
1520 tmp_mesh = cloth_make_rest_mesh(clmd, mesh);
1521 }
1522
1523 const Mesh &mesh_to_use = tmp_mesh ? *tmp_mesh : *mesh;
1524
1525 Set<OrderedEdge> existing_vert_pairs;
1526 const bke::bvh::Tree &treedata = mesh_to_use.bvh_tris();
1527 rng = BLI_rng_new_srandom(0);
1528
1529 const Span<float3> vert_positions = mesh_to_use.vert_positions();
1530 const Span<int> corner_verts = mesh_to_use.corner_verts();
1531 const Span<int3> corner_tris = mesh_to_use.corner_tris();
1532 const Span<float3> vert_normals = mesh_to_use.vert_normals();
1533 for (int i = 0; i < mvert_num; i++) {
1535 treedata,
1536 vert_positions,
1537 corner_verts,
1538 corner_tris,
1539 vert_normals,
1540 i,
1541 rng,
1545 &tar_v_idx))
1546 {
1547 if (existing_vert_pairs.contains({i, tar_v_idx})) {
1548 /* We have already created a spring between these verts! */
1549 continue;
1550 }
1551
1552 existing_vert_pairs.add({i, tar_v_idx});
1553
1554 spring = MEM_new_zeroed<ClothSpring>("cloth spring");
1555
1556 if (spring) {
1557 spring_verts_ordered_set(spring, i, tar_v_idx);
1558
1559 shrink_factor = cloth_shrink_factor(clmd, cloth->verts, spring->ij, spring->kl);
1560 spring->restlen = len_v3v3(cloth->verts[spring->kl].xrest,
1561 cloth->verts[spring->ij].xrest) *
1562 shrink_factor;
1563 spring->lin_stiffness = (cloth->verts[spring->kl].internal_stiff +
1564 cloth->verts[spring->ij].internal_stiff) /
1565 2.0f;
1567
1568 spring->flags = 0;
1569
1570 BLI_linklist_prepend(&cloth->springs, spring);
1571
1572 if (spring_ref) {
1573 spring_ref[i].spring = spring;
1574 }
1575 }
1576 else {
1577 cloth_free_errorsprings(cloth, edgelist, spring_ref);
1578 if (tmp_mesh) {
1579 BKE_id_free(nullptr, &tmp_mesh->id);
1580 }
1581 BLI_rng_free(rng);
1582 return false;
1583 }
1584 }
1585 }
1586 existing_vert_pairs.clear();
1587 if (tmp_mesh) {
1588 BKE_id_free(nullptr, &tmp_mesh->id);
1589 }
1590 BLI_rng_free(rng);
1591 }
1592
1593 clmd->sim_parms->avg_spring_len = 0.0f;
1594 for (int i = 0; i < mvert_num; i++) {
1595 cloth->verts[i].avg_spring_len = 0.0f;
1596 }
1597
1599 /* cloth->sew_edge_graph should not exist before this */
1600 BLI_assert(cloth->sew_edge_graph.is_empty());
1601 }
1602
1603 /* Structural springs. */
1604 BitVector<> loose_edges(edges.size());
1605 mesh->loose_edges().to_bits(loose_edges);
1606 for (int i = 0; i < numedges; i++) {
1607 spring = MEM_new_zeroed<ClothSpring>("cloth spring");
1608
1609 if (spring) {
1610 spring_verts_ordered_set(spring, edges[i][0], edges[i][1]);
1611 if (clmd->sim_parms->flags & CLOTH_SIMSETTINGS_FLAG_SEW && loose_edges[i]) {
1612 /* handle sewing (loose edges will be pulled together) */
1613 spring->restlen = 0.0f;
1614 spring->lin_stiffness = 1.0f;
1616
1617 cloth->sew_edge_graph.add({edges[i][0], edges[i][1]});
1618 }
1619 else {
1620 shrink_factor = cloth_shrink_factor(clmd, cloth->verts, spring->ij, spring->kl);
1621 spring->restlen = len_v3v3(cloth->verts[spring->kl].xrest,
1622 cloth->verts[spring->ij].xrest) *
1623 shrink_factor;
1624 spring->lin_stiffness = (cloth->verts[spring->kl].struct_stiff +
1625 cloth->verts[spring->ij].struct_stiff) /
1626 2.0f;
1628
1629 clmd->sim_parms->avg_spring_len += spring->restlen;
1630 cloth->verts[spring->ij].avg_spring_len += spring->restlen;
1631 cloth->verts[spring->kl].avg_spring_len += spring->restlen;
1632 cloth->verts[spring->ij].spring_count++;
1633 cloth->verts[spring->kl].spring_count++;
1634 struct_springs_real++;
1635 }
1636
1637 spring->flags = 0;
1638 struct_springs++;
1639
1640 BLI_linklist_prepend(&cloth->springs, spring);
1641
1642 if (spring_ref) {
1643 spring_ref[i].spring = spring;
1644 }
1645 }
1646 else {
1647 cloth_free_errorsprings(cloth, edgelist, spring_ref);
1648 return false;
1649 }
1650 }
1651
1652 if (struct_springs_real > 0) {
1653 clmd->sim_parms->avg_spring_len /= struct_springs_real;
1654 }
1655
1656 for (int i = 0; i < mvert_num; i++) {
1657 if (cloth->verts[i].spring_count > 0) {
1658 cloth->verts[i].avg_spring_len = cloth->verts[i].avg_spring_len * 0.49f /
1659 float(cloth->verts[i].spring_count);
1660 }
1661 }
1662
1663 cloth->edgeset.reserve(numedges);
1664
1665 if (numface) {
1666 for (int i = 0; i < numface; i++) {
1667 /* Shear springs. */
1668 /* Triangle faces already have shear springs due to structural geometry. */
1669 if (faces[i].size() > 3) {
1670 for (int j = 1; j < faces[i].size() - 1; j++) {
1671 if (j > 1) {
1672 if (cloth_add_shear_bend_spring(clmd, edgelist, corner_verts, faces, i, 0, j)) {
1673 shear_springs++;
1674
1676 bend_springs++;
1677 }
1678 }
1679 else {
1680 cloth_free_errorsprings(cloth, edgelist, spring_ref);
1681 return false;
1682 }
1683 }
1684
1685 for (int k = j + 2; k < faces[i].size(); k++) {
1686 if (cloth_add_shear_bend_spring(clmd, edgelist, corner_verts, faces, i, j, k)) {
1687 shear_springs++;
1688
1690 bend_springs++;
1691 }
1692 }
1693 else {
1694 cloth_free_errorsprings(cloth, edgelist, spring_ref);
1695 return false;
1696 }
1697 }
1698 }
1699 }
1700
1701 /* Angular bending springs along struct springs. */
1703 for (int j = 0; j < faces[i].size(); j++) {
1704 const int edge_i = corner_edges[faces[i][j]];
1705 BendSpringRef *curr_ref = &spring_ref[edge_i];
1706 curr_ref->face++;
1707
1708 /* First poly found for this edge, store poly index. */
1709 if (curr_ref->face == 1) {
1710 curr_ref->index = i;
1711 }
1712 /* Second poly found for this edge, add bending data. */
1713 else if (curr_ref->face == 2) {
1714 spring = curr_ref->spring;
1715
1717
1718 spring->la = faces[curr_ref->index].size();
1719 spring->lb = faces[i].size();
1720
1722 &spring->pa, spring->la, &corner_verts[faces[curr_ref->index].start()]) ||
1724 &spring->pb, spring->lb, &corner_verts[faces[i].start()]))
1725 {
1726 cloth_free_errorsprings(cloth, edgelist, spring_ref);
1727 return false;
1728 }
1729
1730 spring->mn = edge_i;
1731
1732 spring->restang = cloth_spring_angle(cloth->verts,
1733 spring->ij,
1734 spring->kl,
1735 spring->pa,
1736 spring->pb,
1737 spring->la,
1738 spring->lb);
1739
1740 spring->ang_stiffness = (cloth->verts[spring->ij].bend_stiff +
1741 cloth->verts[spring->kl].bend_stiff) /
1742 2.0f;
1743
1744 bend_springs++;
1745 }
1746 /* Third poly found for this edge, remove bending data. */
1747 else if (curr_ref->face == 3) {
1748 spring = curr_ref->spring;
1749
1751 MEM_delete(spring->pa);
1752 MEM_delete(spring->pb);
1753 spring->pa = nullptr;
1754 spring->pb = nullptr;
1755
1756 bend_springs--;
1757 }
1758 }
1759 }
1760 }
1761
1762 /* Linear bending springs. */
1764 search2 = cloth->springs;
1765
1766 for (int i = struct_springs; i < struct_springs + shear_springs; i++) {
1767 if (!search2) {
1768 break;
1769 }
1770
1771 tspring2 = static_cast<ClothSpring *>(search2->link);
1772 search = edgelist[tspring2->kl].list;
1773
1774 while (search) {
1775 tspring = static_cast<ClothSpring *>(search->link);
1776 index2 = ((tspring->ij == tspring2->kl) ? (tspring->kl) : (tspring->ij));
1777
1778 /* Check for existing spring. */
1779 /* Check also if start-point is equal to endpoint. */
1780 if ((index2 != tspring2->ij) && !cloth->edgeset.contains({tspring2->ij, index2})) {
1781 spring = MEM_new_zeroed<ClothSpring>("cloth spring");
1782
1783 if (!spring) {
1784 cloth_free_errorsprings(cloth, edgelist, spring_ref);
1785 return false;
1786 }
1787
1788 spring_verts_ordered_set(spring, tspring2->ij, index2);
1789 shrink_factor = cloth_shrink_factor(clmd, cloth->verts, spring->ij, spring->kl);
1790 spring->restlen = len_v3v3(cloth->verts[spring->kl].xrest,
1791 cloth->verts[spring->ij].xrest) *
1792 shrink_factor;
1794 spring->lin_stiffness = (cloth->verts[spring->kl].bend_stiff +
1795 cloth->verts[spring->ij].bend_stiff) /
1796 2.0f;
1797 cloth->edgeset.add({spring->ij, spring->kl});
1798 bend_springs++;
1799
1800 BLI_linklist_prepend(&cloth->springs, spring);
1801 }
1802
1803 search = search->next;
1804 }
1805
1806 search2 = search2->next;
1807 }
1808 }
1809 }
1810 else if (struct_springs > 2) {
1811 if (G.debug_value != 1112) {
1812 search = cloth->springs;
1813 search2 = search->next;
1814 while (search && search2) {
1815 tspring = static_cast<ClothSpring *>(search->link);
1816 tspring2 = static_cast<ClothSpring *>(search2->link);
1817
1818 if (tspring->ij == tspring2->kl) {
1819 spring = MEM_new_zeroed<ClothSpring>("cloth spring");
1820
1821 if (!spring) {
1822 cloth_free_errorsprings(cloth, edgelist, spring_ref);
1823 return false;
1824 }
1825
1826 spring->ij = tspring2->ij;
1827 spring->kl = tspring->ij;
1828 spring->mn = tspring->kl;
1829 spring->restlen = len_v3v3(cloth->verts[spring->kl].xrest,
1830 cloth->verts[spring->ij].xrest);
1832 spring->lin_stiffness = (cloth->verts[spring->kl].bend_stiff +
1833 cloth->verts[spring->ij].bend_stiff) /
1834 2.0f;
1835 bend_springs++;
1836
1837 BLI_linklist_prepend(&cloth->springs, spring);
1838 }
1839
1840 search = search->next;
1841 search2 = search2->next;
1842 }
1843 }
1844 else {
1845 /* bending springs for hair strands
1846 * The current algorithm only goes through the edges in order of the mesh edges list
1847 * and makes springs between the outer vert of edges sharing a vertex. This works just
1848 * fine for hair, but not for user generated string meshes. This could/should be later
1849 * extended to work with non-ordered edges so that it can be used for general "rope
1850 * dynamics" without the need for the vertices or edges to be ordered through the length
1851 * of the strands. -jahka */
1852 search = cloth->springs;
1853 search2 = search->next;
1854 while (search && search2) {
1855 tspring = static_cast<ClothSpring *>(search->link);
1856 tspring2 = static_cast<ClothSpring *>(search2->link);
1857
1858 if (tspring->ij == tspring2->kl) {
1859 spring = MEM_new_zeroed<ClothSpring>("cloth spring");
1860
1861 if (!spring) {
1862 cloth_free_errorsprings(cloth, edgelist, spring_ref);
1863 return false;
1864 }
1865
1866 spring->ij = tspring2->ij;
1867 spring->kl = tspring->kl;
1868 spring->restlen = len_v3v3(cloth->verts[spring->kl].xrest,
1869 cloth->verts[spring->ij].xrest);
1871 spring->lin_stiffness = (cloth->verts[spring->kl].bend_stiff +
1872 cloth->verts[spring->ij].bend_stiff) /
1873 2.0f;
1874 bend_springs++;
1875
1876 BLI_linklist_prepend(&cloth->springs, spring);
1877 }
1878
1879 search = search->next;
1880 search2 = search2->next;
1881 }
1882 }
1883
1885 }
1886
1887 /* NOTE: the edges may already exist so run reinsert. */
1888
1889 /* Insert other near springs in `edgeset` AFTER bending springs are calculated
1890 * (for self-collision). */
1891 for (int i = 0; i < numedges; i++) { /* struct springs */
1892 cloth->edgeset.add({edges[i][0], edges[i][1]});
1893 }
1894
1895 for (int i = 0; i < numface; i++) { /* edge springs */
1896 if (faces[i].size() == 4) {
1897 cloth->edgeset.add({corner_verts[faces[i].start() + 0], corner_verts[faces[i].start() + 2]});
1898 cloth->edgeset.add({corner_verts[faces[i].start() + 1], corner_verts[faces[i].start() + 3]});
1899 }
1900 }
1901
1902 MEM_SAFE_DELETE(spring_ref);
1903
1904 cloth->numsprings = struct_springs + shear_springs + bend_springs;
1905
1906 cloth_free_edgelist(edgelist, mvert_num);
1907
1908#if 0
1909 if (G.debug_value > 0) {
1910 printf("avg_len: %f\n", clmd->sim_parms->avg_spring_len);
1911 }
1912#endif
1913
1914 return true;
1915}
1916
1918
1919} // namespace blender
#define SOFTGOALSNAP
Definition BKE_cloth.hh:32
#define ALMOST_ZERO
Definition BKE_cloth.hh:36
CustomData interface, see also DNA_customdata_types.h.
#define PTCACHE_CLEAR_AFTER
#define PTCACHE_READ_INTERPOLATED
#define PTCACHE_READ_OLD
#define PTCACHE_RESET_OUTDATED
#define PTCACHE_READ_EXACT
Random number functions.
Object is a sort of wrapper for general info.
#define MINFRAME
Read Blender memory allocation and freeing.
#define BLI_INLINE
constexpr IndexRange index_range() const
Definition BLI_span.hh:676
bool contains(const Key &key) const
Definition BLI_set.hh:310
bool add(const Key &key)
Definition BLI_set.hh:248
void clear()
Definition BLI_set.hh:570
constexpr int64_t size() const
Definition BLI_span.hh:256
constexpr bool is_empty() const
Definition BLI_span.hh:264
std::optional< RayHit > ray_intersect(const Ray &ray) const
Definition bvh.cc:439
#define rot(x, k)
#define FLT_MAX
#define FLT_EPSILON
#define sin
#define cos
ccl_device_inline dual1 len(const ccl_private dual< T > &a)
Definition math_dual.h:263
void vert_tris_from_corner_tris(Span< int > corner_verts, Span< int3 > corner_tris, MutableSpan< int3 > vert_tris)
MatBase< T, NumCol, NumRow > normalize(const MatBase< T, NumCol, NumRow > &a)
BMesh Iterators.
BLI_assert(slot->slot_subtype.map==BMO_OP_SLOT_SUBTYPE_MAP_FLT)
int BKE_ptcache_read(PTCacheID *pid, float cfra, bool no_extrapolate_old)
MINLINE void negate_v3_v3(float r[3], const float a[3])
int cloth_uses_vgroup(ClothModifierData *clmd)
Definition cloth.cc:599
static void cloth_apply_vgroup(ClothModifierData *clmd, const Mesh *mesh)
Definition cloth.cc:614
void BKE_ptcache_id_clear(PTCacheID *pid, int mode, unsigned int cfra)
void BKE_modifier_set_error(const Object *ob, ModifierData *md, const char *format,...) ATTR_PRINTF_FORMAT(3
int SIM_cloth_solve(Depsgraph *depsgraph, Object *ob, float frame, ClothModifierData *clmd, ListBaseT< EffectorCache > *effectors)
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
@ CLOTH_BENDING_LINEAR
@ CLOTH_BENDING_ANGULAR
BLI_INLINE void cross_identity_v3(float r[3][3], const float v[3])
Definition cloth.cc:1250
void SIM_mass_spring_set_implicit_vertex_mass(Implicit_Data *data, int index, float mass)
void copy_m3_m3(float m1[3][3], const float m2[3][3])
Mesh * BKE_mesh_copy_for_eval(const Mesh &source)
struct RNG * BLI_rng_new_srandom(unsigned int seed)
Definition rand.cc:48
void BLI_bvhtree_free(BVHTree *tree)
int BKE_ptcache_id_reset(struct Scene *scene, PTCacheID *pid, int mode)
static bool cloth_from_object(Object *ob, ClothModifierData *clmd, const Mesh *mesh, float framenr, int first)
Definition cloth.cc:721
void cloth_free_modifier(ClothModifierData *clmd)
Definition cloth.cc:445
bool invert_m4_m4(float inverse[4][4], const float mat[4][4])
void BLI_bvhtree_balance(BVHTree *tree)
static bool find_internal_spring_target_vertex(const bke::bvh::Tree &treedata, const Span< float3 > vert_positions, const Span< int > corner_verts, const Span< int3 > corner_tris, const Span< float3 > vert_normals, uint v_idx, RNG *rng, float max_length, float max_diversion, bool check_normal, int *r_tar_v_idx)
Definition cloth.cc:1387
ATTR_WARN_UNUSED_RESULT const BMVert * v
float normal_tri_v3(float n[3], const float v1[3], const float v2[3], const float v3[3])
Definition math_geom.cc:45
void BKE_ptcache_id_from_cloth(PTCacheID *pid, struct Object *ob, struct ClothModifierData *clmd)
bool BLI_bvhtree_update_node(BVHTree *tree, int index, const float co[3], const float co_moving[3], int numpoints)
MINLINE float pow4f(float x)
MINLINE void mul_v3_fl(float r[3], float f)
void void BLI_linklist_prepend(LinkNode **listp, void *ptr) ATTR_NONNULL(1)
@ 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
@ CLOTH_SPRING_TYPE_GOAL
Definition BKE_cloth.hh:191
@ G_DEBUG_SIMDATA
Global G
Definition blender.cc:57
static int do_step_cloth(Depsgraph *depsgraph, Object *ob, ClothModifierData *clmd, const Mesh *result, int framenr)
Definition cloth.cc:258
void SIM_cloth_solver_set_volume(ClothModifierData *clmd)
void clothModifier_do(ClothModifierData *clmd, Depsgraph *depsgraph, Scene *scene, Object *ob, const Mesh *mesh, float(*vertexCos)[3])
Definition cloth.cc:327
void mul_transposed_m3_v3(const float M[3][3], float r[3])
static Mesh * cloth_make_rest_mesh(ClothModifierData *clmd, const Mesh *mesh)
Definition cloth.cc:1175
void mul_m4_v3(const float M[4][4], float r[3])
MINLINE void madd_v3_v3fl(float r[3], const float a[3], float f)
void BLI_bvhtree_update_tree(BVHTree *tree)
BLI_INLINE void spring_verts_ordered_set(ClothSpring *spring, int v0, int v1)
Definition cloth.cc:889
VecBase< int32_t, 2 > int2
VecBase< int32_t, 3 > int3
static void cloth_update_verts(Object *ob, ClothModifierData *clmd, const Mesh *mesh)
Definition cloth.cc:1161
void * MEM_new_zeroed(size_t len, const char *str) ATTR_WARN_UNUSED_RESULT ATTR_ALLOC_SIZE(1) ATTR_NONNULL(2)
MINLINE void cross_v3_v3v3(float r[3], const float a[3], const float b[3])
static void cloth_free_errorsprings(Cloth *cloth, LinkNodePair *edgelist, BendSpringRef *spring_ref)
Definition cloth.cc:912
int BKE_ptcache_write(PTCacheID *pid, unsigned int cfra)
@ OB_MODE_PARTICLE_EDIT
void cloth_parallel_transport_hair_frame(float mat[3][3], const float dir_old[3], const float dir_new[3])
Definition cloth.cc:1274
static float cloth_spring_angle(ClothVertex *verts, int i, int j, int *i_a, int *i_b, int len_a, int len_b)
Definition cloth.cc:952
static void cloth_update_springs(ClothModifierData *clmd)
Definition cloth.cc:1090
static BVHTree * bvhtree_build_from_cloth(ClothModifierData *clmd, float epsilon)
Definition cloth.cc:69
BVHTree * BLI_bvhtree_new(int maxsize, float epsilon, char tree_type, char axis)
void SIM_cloth_solver_free(ClothModifierData *clmd)
void BKE_ptcache_id_time(PTCacheID *pid, struct Scene *scene, float cfra, int *startframe, int *endframe, float *timescale)
void BLI_rng_free(struct RNG *rng) ATTR_NONNULL(1)
Definition rand.cc:55
const void * CustomData_get_layer(const CustomData *data, eCustomDataType type)
void BKE_id_free(Main *bmain, void *idv)
void cloth_clear_cache(Object *ob, ClothModifierData *clmd, float framenr)
Definition cloth.cc:208
static void cloth_update_spring_lengths(ClothModifierData *clmd, const Mesh *mesh)
Definition cloth.cc:1191
float BLI_rng_get_float(struct RNG *rng) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL(1)
Definition rand.cc:90
void bvhtree_update_from_cloth(ClothModifierData *clmd, bool moving, bool self)
Definition cloth.cc:123
void BKE_ptcache_validate(struct PointCache *cache, int framenr)
MINLINE float normalize_v3(float n[3])
static bool do_init_cloth(Object *ob, ClothModifierData *clmd, const Mesh *result, int framenr)
Definition cloth.cc:222
MINLINE void add_v3_v3(float r[3], const float a[3])
@ CLOTH_SIMSETTINGS_FLAG_INTERNAL_SPRINGS_NORMAL
@ CLOTH_SIMSETTINGS_FLAG_DYNAMIC_BASEMESH
@ CLOTH_SIMSETTINGS_FLAG_INTERNAL_SPRINGS
@ CLOTH_SIMSETTINGS_FLAG_PRESSURE_VOL
@ CLOTH_SIMSETTINGS_FLAG_SEW
@ CLOTH_SIMSETTINGS_FLAG_PRESSURE
void cloth_free_modifier_extern(ClothModifierData *clmd)
Definition cloth.cc:506
MINLINE void sub_v3_v3v3(float r[3], const float a[3], const float b[3])
void void void void BLI_linklist_append(LinkNodePair *list_pair, void *ptr) ATTR_NONNULL(1)
void BLI_linklist_free(LinkNode *list, LinkNodeFreeFP freefunc)
ATTR_WARN_UNUSED_RESULT const BMVert * v2
ListBaseT< EffectorCache > * BKE_effectors_create(struct Depsgraph *depsgraph, struct Object *ob_src, struct ParticleSystem *psys_src, struct EffectorWeights *weights, bool use_rotation)
Definition effect.cc:316
@ CLOTH_VERT_FLAG_NOSELFCOLL
Definition BKE_cloth.hh:41
@ CLOTH_VERT_FLAG_NOOBJCOLL
Definition BKE_cloth.hh:42
@ CLOTH_VERT_FLAG_PINNED
Definition BKE_cloth.hh:40
void SIM_cloth_solver_set_positions(ClothModifierData *clmd)
void BLI_bvhtree_insert(BVHTree *tree, int index, const float co[3], int numpoints)
@ CLOTH_SPRING_FLAG_DEACTIVATE
Definition BKE_cloth.hh:199
static bool cloth_build_springs(ClothModifierData *clmd, const Mesh *mesh)
Definition cloth.cc:1463
int SIM_cloth_solver_init(Object *, ClothModifierData *clmd)
void zero_m3(float m[3][3])
static void cloth_from_mesh(ClothModifierData *clmd, const Object *ob, const Mesh *mesh)
Definition cloth.cc:839
void mul_m3_m3m3(float R[3][3], const float A[3][3], const float B[3][3])
MINLINE float len_v3v3(const float a[3], const float b[3]) ATTR_WARN_UNUSED_RESULT
BLI_INLINE void madd_m3_m3fl(float r[3][3], const float m[3][3], float f)
Definition cloth.cc:1261
void mul_v3_m3v3(float r[3], const float M[3][3], const float a[3])
@ CLOTH_COLLSETTINGS_FLAG_ENABLED
@ CLOTH_COLLSETTINGS_FLAG_SELF
MINLINE float dot_v3v3(const float a[3], const float b[3]) ATTR_WARN_UNUSED_RESULT
float DEG_get_ctime(const Depsgraph *graph)
void BKE_ptcache_invalidate(struct PointCache *cache)
BLI_INLINE void cloth_bend_poly_dir(ClothVertex *verts, int i, int j, const int *inds, int len, float r_dir[3])
Definition cloth.cc:939
static void cloth_free_edgelist(LinkNodePair *edgelist, uint mvert_num)
Definition cloth.cc:901
static void cloth_hair_update_bending_targets(ClothModifierData *clmd)
Definition cloth.cc:976
BLI_INLINE bool cloth_bend_set_poly_vert_array(int **poly, int len, const int *corner_verts)
Definition cloth.cc:1370
void rotation_between_vecs_to_mat3(float m[3][3], const float v1[3], const float v2[3])
MINLINE int compare_ff(float a, float b, float max_diff)
void BKE_effectors_free(ListBaseT< EffectorCache > *lb)
Definition effect.cc:371
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_to_object(Object *ob, ClothModifierData *clmd, float(*vertexCos)[3])
Definition cloth.cc:583
static void cloth_hair_update_bending_rest_targets(ClothModifierData *clmd)
Definition cloth.cc:1034
static bool cloth_add_shear_bend_spring(ClothModifierData *clmd, LinkNodePair *edgelist, const Span< int > corner_verts, const OffsetIndices< int > faces, int i, int j, int k)
Definition cloth.cc:1288
static float cloth_shrink_factor(ClothModifierData *clmd, ClothVertex *verts, int i1, int i2)
Definition cloth.cc:707
#define sqrtf
#define atan2f
KERNEL_FEATURE_LIGHT_PASSES KERNEL_FEATURE_PATH_GUIDING float
ClothSpring * spring
Definition cloth.cc:62
CLOTH_COLLISIONSETTINGS_FLAGS flags
struct ClothHairData * hairdata
struct ClothSimSettings * sim_parms
struct PointCache * point_cache
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
unsigned char old_solver_type
Definition BKE_cloth.hh:76
unsigned int numsprings
Definition BKE_cloth.hh:73
BVHTree * bvhtree
Definition BKE_cloth.hh:79
unsigned int mvert_num
Definition BKE_cloth.hh:74
const int2 * edges
Definition BKE_cloth.hh:87
unsigned int primitive_num
Definition BKE_cloth.hh:75
ClothVertex * verts
Definition BKE_cloth.hh:71
int3 * vert_tris
Definition BKE_cloth.hh:81
BVHTree * bvhselftree
Definition BKE_cloth.hh:80
LinkNode * springs
Definition BKE_cloth.hh:72
struct LinkNode * next
struct MDeformWeight * dw
const c_style_mat & ptr() const
bke::ObjectRuntime * runtime
struct PointCache * cache
struct PTCacheEdit * edit
struct RenderData r