Blender  V2.93
BKE_cloth.h
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 #pragma once
20 
25 #include "BLI_math_inline.h"
26 #include <float.h>
27 
28 #ifdef __cplusplus
29 extern "C" {
30 #endif
31 
32 struct ClothModifierData;
34 struct Depsgraph;
35 struct GHash;
36 struct Mesh;
37 struct Object;
38 struct Scene;
39 
40 #define DO_INLINE MALWAYS_INLINE
41 
42 /* goal defines */
43 #define SOFTGOALSNAP 0.999f
44 
45 /* This is approximately the smallest number that can be
46  * represented by a float, given its precision. */
47 #define ALMOST_ZERO FLT_EPSILON
48 
49 /* Bits to or into the ClothVertex.flags. */
50 typedef enum eClothVertexFlag {
52  CLOTH_VERT_FLAG_NOSELFCOLL = (1 << 1), /* vertex NOT used for self collisions */
53  CLOTH_VERT_FLAG_NOOBJCOLL = (1 << 2), /* vertex NOT used for object collisions */
55 
56 typedef struct ClothHairData {
57  float loc[3];
58  float rot[3][3];
59  float rest_target[3]; /* rest target direction for each segment */
60  float radius;
63 
64 typedef struct ClothSolverResult {
65  int status;
66 
71 
81 typedef struct Cloth {
82  struct ClothVertex *verts; /* The vertices that represent this cloth. */
83  struct LinkNode *springs; /* The springs connecting the mesh. */
84  unsigned int numsprings; /* The count of springs. */
85  unsigned int mvert_num; /* The number of verts == m * n. */
86  unsigned int primitive_num; /* Number of triangles for cloth and edges for hair. */
87  unsigned char old_solver_type; /* unused, only 1 solver here */
88  unsigned char pad2;
89  short pad3;
90  struct BVHTree *bvhtree; /* collision tree for this cloth object */
91  struct BVHTree *bvhselftree; /* collision tree for this cloth object */
92  struct MVertTri *tri;
93  struct Implicit_Data *implicit; /* our implicit solver connects to this pointer */
94  struct EdgeSet *edgeset; /* used for selfcollisions */
96  float initial_mesh_volume; /* Initial volume of the mesh. Used for pressure */
97  float average_acceleration[3]; /* Moving average of overall acceleration. */
98  struct MEdge *edges; /* Used for hair collisions. */
99  struct EdgeSet *sew_edge_graph; /* Sewing edges represented using a GHash */
101 
105 typedef struct ClothVertex {
106  int flags; /* General flags per vertex. */
107  float v[3]; /* The velocity of the point. */
108  float xconst[3]; /* constrained position */
109  float x[3]; /* The current position of this vertex. */
110  float xold[3]; /* The previous position of this vertex.*/
111  float tx[3]; /* temporary position */
112  float txold[3]; /* temporary old position */
113  float tv[3]; /* temporary "velocity", mostly used as tv = tx-txold */
114  float mass; /* mass / weight of the vertex */
115  float goal; /* goal, from SB */
116  float impulse[3]; /* used in collision.c */
117  float xrest[3]; /* rest position of the vertex */
118  float dcvel[3]; /* delta velocities to be applied by collision response */
119  unsigned int impulse_count; /* same as above */
120  float avg_spring_len; /* average length of connected springs */
122  float bend_stiff;
123  float shear_stiff;
124  int spring_count; /* how many springs attached? */
125  float shrink_factor; /* how much to shrink this cloth */
126  float internal_stiff; /* internal spring stiffness scaling */
127  float pressure_factor; /* how much pressure should affect this vertex */
129 
133 typedef struct ClothSpring {
134  int ij; /* `Pij` from the paper, one end of the spring. */
135  int kl; /* `Pkl` from the paper, one end of the spring. */
136  int mn; /* For hair springs: third vertex index; For bending springs: edge index; */
137  int *pa; /* Array of vert indices for poly a (for bending springs). */
138  int *pb; /* Array of vert indices for poly b (for bending springs). */
139  int la; /* Length of `*pa`. */
140  int lb; /* Length of `*pb`. */
141  float restlen; /* The original length of the spring. */
142  float restang; /* The original angle of the bending springs. */
143  int type; /* Types defined in BKE_cloth.h ("springType"). */
144  int flags; /* Defined in BKE_cloth.h, e.g. deactivated due to tearing. */
145  float lin_stiffness; /* Linear stiffness factor from the vertex groups. */
146  float ang_stiffness; /* Angular stiffness factor from the vertex groups. */
147  float editrestlen;
148 
149  /* angular bending spring target and derivatives */
150  float target[3];
152 
153 // some macro enhancements for vector treatment
154 #define VECSUBADDSS(v1, v2, aS, v3, bS) \
155  { \
156  *(v1) -= *(v2)*aS + *(v3)*bS; \
157  *(v1 + 1) -= *(v2 + 1) * aS + *(v3 + 1) * bS; \
158  *(v1 + 2) -= *(v2 + 2) * aS + *(v3 + 2) * bS; \
159  } \
160  ((void)0)
161 #define VECADDSS(v1, v2, aS, v3, bS) \
162  { \
163  *(v1) = *(v2)*aS + *(v3)*bS; \
164  *(v1 + 1) = *(v2 + 1) * aS + *(v3 + 1) * bS; \
165  *(v1 + 2) = *(v2 + 2) * aS + *(v3 + 2) * bS; \
166  } \
167  ((void)0)
168 #define VECADDS(v1, v2, v3, bS) \
169  { \
170  *(v1) = *(v2) + *(v3)*bS; \
171  *(v1 + 1) = *(v2 + 1) + *(v3 + 1) * bS; \
172  *(v1 + 2) = *(v2 + 2) + *(v3 + 2) * bS; \
173  } \
174  ((void)0)
175 #define VECSUBMUL(v1, v2, aS) \
176  { \
177  *(v1) -= *(v2)*aS; \
178  *(v1 + 1) -= *(v2 + 1) * aS; \
179  *(v1 + 2) -= *(v2 + 2) * aS; \
180  } \
181  ((void)0)
182 #define VECSUBS(v1, v2, v3, bS) \
183  { \
184  *(v1) = *(v2) - *(v3)*bS; \
185  *(v1 + 1) = *(v2 + 1) - *(v3 + 1) * bS; \
186  *(v1 + 2) = *(v2 + 2) - *(v3 + 2) * bS; \
187  } \
188  ((void)0)
189 #define VECADDMUL(v1, v2, aS) \
190  { \
191  *(v1) += *(v2)*aS; \
192  *(v1 + 1) += *(v2 + 1) * aS; \
193  *(v1 + 2) += *(v2 + 2) * aS; \
194  } \
195  ((void)0)
196 
197 /* Spring types as defined in the paper.*/
198 typedef enum {
207 
208 /* SPRING FLAGS */
209 typedef enum {
211  CLOTH_SPRING_FLAG_NEEDED = (1 << 2), /* Springs has values to be applied. */
213 
215 // collision.c
217 
218 struct CollPair;
219 
220 typedef struct ColliderContacts {
221  struct Object *ob;
223 
227 
228 // needed for implicit.c
230  struct Object *ob,
231  struct ClothModifierData *clmd,
232  float step,
233  float dt);
234 
236 
238 // cloth.c
240 
241 // needed for modifier.c
243 void cloth_free_modifier(struct ClothModifierData *clmd);
244 void clothModifier_do(struct ClothModifierData *clmd,
245  struct Depsgraph *depsgraph,
246  struct Scene *scene,
247  struct Object *ob,
248  struct Mesh *me,
249  float (*vertexCos)[3]);
250 
251 int cloth_uses_vgroup(struct ClothModifierData *clmd);
252 
253 // needed for collision.c
254 void bvhtree_update_from_cloth(struct ClothModifierData *clmd, bool moving, bool self);
255 
256 // needed for button_object.c
257 void cloth_clear_cache(struct Object *ob, struct ClothModifierData *clmd, float framenr);
258 
259 void cloth_parallel_transport_hair_frame(float mat[3][3],
260  const float dir_old[3],
261  const float dir_new[3]);
262 
264 
265 #ifdef __cplusplus
266 }
267 #endif
int cloth_uses_vgroup(struct ClothModifierData *clmd)
Definition: cloth.c:611
void bvhtree_update_from_cloth(struct ClothModifierData *clmd, bool moving, bool self)
Definition: cloth.c:134
void cloth_free_modifier(struct ClothModifierData *clmd)
Definition: cloth.c:430
CLOTH_SPRING_TYPES
Definition: BKE_cloth.h:198
@ 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
struct ClothSpring ClothSpring
eClothVertexFlag
Definition: BKE_cloth.h:50
@ 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
int cloth_bvh_collision(struct Depsgraph *depsgraph, struct Object *ob, struct ClothModifierData *clmd, float step, float dt)
Definition: collision.c:1565
void clothModifier_do(struct ClothModifierData *clmd, struct Depsgraph *depsgraph, struct Scene *scene, struct Object *ob, struct Mesh *me, float(*vertexCos)[3])
Definition: cloth.c:322
void cloth_clear_cache(struct Object *ob, struct ClothModifierData *clmd, float framenr)
Definition: cloth.c:214
void cloth_parallel_transport_hair_frame(float mat[3][3], const float dir_old[3], const float dir_new[3])
Definition: cloth.c:1279
CLOTH_SPRINGS_FLAGS
Definition: BKE_cloth.h:209
@ CLOTH_SPRING_FLAG_DEACTIVATE
Definition: BKE_cloth.h:210
@ CLOTH_SPRING_FLAG_NEEDED
Definition: BKE_cloth.h:211
struct ClothSolverResult ClothSolverResult
struct ColliderContacts ColliderContacts
struct ClothVertex ClothVertex
struct Cloth Cloth
void cloth_free_modifier_extern(struct ClothModifierData *clmd)
Definition: cloth.c:505
struct ClothHairData ClothHairData
struct Depsgraph Depsgraph
Definition: DEG_depsgraph.h:51
Scene scene
const Depsgraph * depsgraph
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
float loc[3]
Definition: BKE_cloth.h:57
float radius
Definition: BKE_cloth.h:60
float avg_iterations
Definition: BKE_cloth.h:68
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 editrestlen
Definition: BKE_cloth.h:147
float restlen
Definition: BKE_cloth.h:141
float bend_stiff
Definition: BKE_cloth.h:122
float mass
Definition: BKE_cloth.h:114
float avg_spring_len
Definition: BKE_cloth.h:120
float impulse[3]
Definition: BKE_cloth.h:116
float tv[3]
Definition: BKE_cloth.h:113
float x[3]
Definition: BKE_cloth.h:109
float v[3]
Definition: BKE_cloth.h:107
unsigned int impulse_count
Definition: BKE_cloth.h:119
float dcvel[3]
Definition: BKE_cloth.h:118
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 pressure_factor
Definition: BKE_cloth.h:127
float xrest[3]
Definition: BKE_cloth.h:117
float tx[3]
Definition: BKE_cloth.h:111
float xconst[3]
Definition: BKE_cloth.h:108
float struct_stiff
Definition: BKE_cloth.h:121
float txold[3]
Definition: BKE_cloth.h:112
float shrink_factor
Definition: BKE_cloth.h:125
float xold[3]
Definition: BKE_cloth.h:110
struct LinkNode * springs
Definition: BKE_cloth.h:83
unsigned char pad2
Definition: BKE_cloth.h:88
short pad3
Definition: BKE_cloth.h:89
struct EdgeSet * sew_edge_graph
Definition: BKE_cloth.h:99
float initial_mesh_volume
Definition: BKE_cloth.h:96
float average_acceleration[3]
Definition: BKE_cloth.h:97
struct BVHTree * bvhtree
Definition: BKE_cloth.h:90
struct Implicit_Data * implicit
Definition: BKE_cloth.h:93
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
struct CollisionModifierData * collmd
Definition: BKE_cloth.h:222
struct CollPair * collisions
Definition: BKE_cloth.h:224
struct Object * ob
Definition: BKE_cloth.h:221