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