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