Blender  V3.3
BKE_cloth.h
Go to the documentation of this file.
1 /* SPDX-License-Identifier: GPL-2.0-or-later
2  * Copyright Blender Foundation. All rights reserved. */
3 #pragma once
4 
9 #include "BLI_math_inline.h"
10 #include <float.h>
11 
12 #ifdef __cplusplus
13 extern "C" {
14 #endif
15 
16 struct ClothModifierData;
18 struct Depsgraph;
19 struct GHash;
20 struct Mesh;
21 struct Object;
22 struct Scene;
23 
24 #define DO_INLINE MALWAYS_INLINE
25 
26 /* Goal defines. */
27 #define SOFTGOALSNAP 0.999f
28 
29 /* This is approximately the smallest number that can be
30  * represented by a float, given its precision. */
31 #define ALMOST_ZERO FLT_EPSILON
32 
33 /* Bits to or into the #ClothVertex.flags. */
34 typedef enum eClothVertexFlag {
36  CLOTH_VERT_FLAG_NOSELFCOLL = (1 << 1), /* vertex NOT used for self collisions */
37  CLOTH_VERT_FLAG_NOOBJCOLL = (1 << 2), /* vertex NOT used for object collisions */
39 
40 typedef struct ClothHairData {
41  float loc[3];
42  float rot[3][3];
43  float rest_target[3]; /* rest target direction for each segment */
44  float radius;
47 
48 typedef struct ClothSolverResult {
49  int status;
50 
55 
65 typedef struct Cloth {
66  struct ClothVertex *verts; /* The vertices that represent this cloth. */
67  struct LinkNode *springs; /* The springs connecting the mesh. */
68  unsigned int numsprings; /* The count of springs. */
69  unsigned int mvert_num; /* The number of verts == m * n. */
70  unsigned int primitive_num; /* Number of triangles for cloth and edges for hair. */
71  unsigned char old_solver_type; /* unused, only 1 solver here */
72  unsigned char pad2;
73  short pad3;
74  struct BVHTree *bvhtree; /* collision tree for this cloth object */
75  struct BVHTree *bvhselftree; /* collision tree for this cloth object */
76  struct MVertTri *tri;
77  struct Implicit_Data *implicit; /* our implicit solver connects to this pointer */
78  struct EdgeSet *edgeset; /* used for selfcollisions */
80  float initial_mesh_volume; /* Initial volume of the mesh. Used for pressure */
81  float average_acceleration[3]; /* Moving average of overall acceleration. */
82  struct MEdge *edges; /* Used for hair collisions. */
83  struct EdgeSet *sew_edge_graph; /* Sewing edges represented using a GHash */
85 
89 typedef struct ClothVertex {
90  int flags; /* General flags per vertex. */
91  float v[3]; /* The velocity of the point. */
92  float xconst[3]; /* constrained position */
93  float x[3]; /* The current position of this vertex. */
94  float xold[3]; /* The previous position of this vertex. */
95  float tx[3]; /* temporary position */
96  float txold[3]; /* temporary old position */
97  float tv[3]; /* temporary "velocity", mostly used as tv = tx-txold */
98  float mass; /* mass / weight of the vertex */
99  float goal; /* goal, from SB */
100  float impulse[3]; /* used in collision.c */
101  float xrest[3]; /* rest position of the vertex */
102  float dcvel[3]; /* delta velocities to be applied by collision response */
103  unsigned int impulse_count; /* same as above */
104  float avg_spring_len; /* average length of connected springs */
106  float bend_stiff;
107  float shear_stiff;
108  int spring_count; /* how many springs attached? */
109  float shrink_factor; /* how much to shrink this cloth */
110  float internal_stiff; /* internal spring stiffness scaling */
111  float pressure_factor; /* how much pressure should affect this vertex */
113 
117 typedef struct ClothSpring {
118  int ij; /* `Pij` from the paper, one end of the spring. */
119  int kl; /* `Pkl` from the paper, one end of the spring. */
120  int mn; /* For hair springs: third vertex index; For bending springs: edge index; */
121  int *pa; /* Array of vert indices for poly a (for bending springs). */
122  int *pb; /* Array of vert indices for poly b (for bending springs). */
123  int la; /* Length of `*pa`. */
124  int lb; /* Length of `*pb`. */
125  float restlen; /* The original length of the spring. */
126  float restang; /* The original angle of the bending springs. */
127  int type; /* Types defined in BKE_cloth.h ("springType"). */
128  int flags; /* Defined in BKE_cloth.h, e.g. deactivated due to tearing. */
129  float lin_stiffness; /* Linear stiffness factor from the vertex groups. */
130  float ang_stiffness; /* Angular stiffness factor from the vertex groups. */
131  float editrestlen;
132 
133  /* angular bending spring target and derivatives */
134  float target[3];
136 
137 /* Some macro enhancements for vector treatment. */
138 #define VECSUBADDSS(v1, v2, aS, v3, bS) \
139  { \
140  *(v1) -= *(v2)*aS + *(v3)*bS; \
141  *(v1 + 1) -= *(v2 + 1) * aS + *(v3 + 1) * bS; \
142  *(v1 + 2) -= *(v2 + 2) * aS + *(v3 + 2) * bS; \
143  } \
144  ((void)0)
145 #define VECADDSS(v1, v2, aS, v3, bS) \
146  { \
147  *(v1) = *(v2)*aS + *(v3)*bS; \
148  *(v1 + 1) = *(v2 + 1) * aS + *(v3 + 1) * bS; \
149  *(v1 + 2) = *(v2 + 2) * aS + *(v3 + 2) * bS; \
150  } \
151  ((void)0)
152 #define VECADDS(v1, v2, v3, bS) \
153  { \
154  *(v1) = *(v2) + *(v3)*bS; \
155  *(v1 + 1) = *(v2 + 1) + *(v3 + 1) * bS; \
156  *(v1 + 2) = *(v2 + 2) + *(v3 + 2) * bS; \
157  } \
158  ((void)0)
159 #define VECSUBMUL(v1, v2, aS) \
160  { \
161  *(v1) -= *(v2)*aS; \
162  *(v1 + 1) -= *(v2 + 1) * aS; \
163  *(v1 + 2) -= *(v2 + 2) * aS; \
164  } \
165  ((void)0)
166 #define VECSUBS(v1, v2, v3, bS) \
167  { \
168  *(v1) = *(v2) - *(v3)*bS; \
169  *(v1 + 1) = *(v2 + 1) - *(v3 + 1) * bS; \
170  *(v1 + 2) = *(v2 + 2) - *(v3 + 2) * bS; \
171  } \
172  ((void)0)
173 #define VECADDMUL(v1, v2, aS) \
174  { \
175  *(v1) += *(v2)*aS; \
176  *(v1 + 1) += *(v2 + 1) * aS; \
177  *(v1 + 2) += *(v2 + 2) * aS; \
178  } \
179  ((void)0)
180 
181 /* Spring types as defined in the paper. */
182 typedef enum {
191 
192 /* SPRING FLAGS */
193 typedef enum {
195  CLOTH_SPRING_FLAG_NEEDED = (1 << 2), /* Springs has values to be applied. */
197 
198 /* -------------------------------------------------------------------- */
199 /* collision.c */
200 
201 struct CollPair;
202 
203 typedef struct ColliderContacts {
204  struct Object *ob;
206 
210 
211 /* needed for implicit.c */
213  struct Object *ob,
214  struct ClothModifierData *clmd,
215  float step,
216  float dt);
217 
218 /* -------------------------------------------------------------------- */
219 /* cloth.c */
220 
221 /* Needed for modifier.c */
225 void cloth_free_modifier(struct ClothModifierData *clmd);
226 void clothModifier_do(struct ClothModifierData *clmd,
227  struct Depsgraph *depsgraph,
228  struct Scene *scene,
229  struct Object *ob,
230  struct Mesh *me,
231  float (*vertexCos)[3]);
232 
233 int cloth_uses_vgroup(struct ClothModifierData *clmd);
234 
235 /* Needed for collision.c */
236 void bvhtree_update_from_cloth(struct ClothModifierData *clmd, bool moving, bool self);
237 
238 /* Needed for button_object.c */
239 void cloth_clear_cache(struct Object *ob, struct ClothModifierData *clmd, float framenr);
240 
241 void cloth_parallel_transport_hair_frame(float mat[3][3],
242  const float dir_old[3],
243  const float dir_new[3]);
244 
245 #ifdef __cplusplus
246 }
247 #endif
int cloth_uses_vgroup(struct ClothModifierData *clmd)
Definition: cloth.c:593
void bvhtree_update_from_cloth(struct ClothModifierData *clmd, bool moving, bool self)
Definition: cloth.c:118
void cloth_free_modifier(struct ClothModifierData *clmd)
Definition: cloth.c:421
CLOTH_SPRING_TYPES
Definition: BKE_cloth.h:182
@ 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
struct ClothSpring ClothSpring
eClothVertexFlag
Definition: BKE_cloth.h:34
@ 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
int cloth_bvh_collision(struct Depsgraph *depsgraph, struct Object *ob, struct ClothModifierData *clmd, float step, float dt)
Definition: collision.c:1541
void clothModifier_do(struct ClothModifierData *clmd, struct Depsgraph *depsgraph, struct Scene *scene, struct Object *ob, struct Mesh *me, float(*vertexCos)[3])
Definition: cloth.c:314
void cloth_clear_cache(struct Object *ob, struct ClothModifierData *clmd, float framenr)
Definition: cloth.c:198
void cloth_parallel_transport_hair_frame(float mat[3][3], const float dir_old[3], const float dir_new[3])
Definition: cloth.c:1262
CLOTH_SPRINGS_FLAGS
Definition: BKE_cloth.h:193
@ CLOTH_SPRING_FLAG_DEACTIVATE
Definition: BKE_cloth.h:194
@ CLOTH_SPRING_FLAG_NEEDED
Definition: BKE_cloth.h:195
struct ClothSolverResult ClothSolverResult
struct ColliderContacts ColliderContacts
struct ClothVertex ClothVertex
struct Cloth Cloth
void cloth_free_modifier_extern(struct ClothModifierData *clmd)
Definition: cloth.c:491
struct ClothHairData ClothHairData
struct Depsgraph Depsgraph
Definition: DEG_depsgraph.h:35
Scene scene
const Depsgraph * depsgraph
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
float loc[3]
Definition: BKE_cloth.h:41
float radius
Definition: BKE_cloth.h:44
float avg_iterations
Definition: BKE_cloth.h:52
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 editrestlen
Definition: BKE_cloth.h:131
float restlen
Definition: BKE_cloth.h:125
float bend_stiff
Definition: BKE_cloth.h:106
float mass
Definition: BKE_cloth.h:98
float avg_spring_len
Definition: BKE_cloth.h:104
float impulse[3]
Definition: BKE_cloth.h:100
float tv[3]
Definition: BKE_cloth.h:97
float x[3]
Definition: BKE_cloth.h:93
float v[3]
Definition: BKE_cloth.h:91
unsigned int impulse_count
Definition: BKE_cloth.h:103
float dcvel[3]
Definition: BKE_cloth.h:102
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 pressure_factor
Definition: BKE_cloth.h:111
float xrest[3]
Definition: BKE_cloth.h:101
float tx[3]
Definition: BKE_cloth.h:95
float xconst[3]
Definition: BKE_cloth.h:92
float struct_stiff
Definition: BKE_cloth.h:105
float txold[3]
Definition: BKE_cloth.h:96
float shrink_factor
Definition: BKE_cloth.h:109
float xold[3]
Definition: BKE_cloth.h:94
struct LinkNode * springs
Definition: BKE_cloth.h:67
unsigned char pad2
Definition: BKE_cloth.h:72
short pad3
Definition: BKE_cloth.h:73
struct EdgeSet * sew_edge_graph
Definition: BKE_cloth.h:83
float initial_mesh_volume
Definition: BKE_cloth.h:80
float average_acceleration[3]
Definition: BKE_cloth.h:81
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
struct CollisionModifierData * collmd
Definition: BKE_cloth.h:205
struct CollPair * collisions
Definition: BKE_cloth.h:207
struct Object * ob
Definition: BKE_cloth.h:204