35static float I3[3][3] = {{1.0, 0.0, 0.0}, {0.0, 1.0, 0.0}, {0.0, 0.0, 1.0}};
45 for (link = cloth->springs; link; link = link->
next) {
47 switch (spring->
type) {
72 for (
uint j = 0; j < 3; j++) {
76 if (r_weights[j] == 0.0f) {
86 const float gradient_vector[3],
87 float *r_vertex_pressure)
91 uint mvert_num = cloth->mvert_num;
94 for (
uint i = 0; i < mvert_num; i++) {
96 r_vertex_pressure[i] =
dot_v3v3(pt, gradient_vector);
106 float weights[3] = {1.0f, 1.0f, 1.0f};
114 for (
uint i = 0; i < cloth->primitive_num; i++) {
134 float weights[3] = {1.0f, 1.0f, 1.0f};
142 for (
uint i = 0; i < cloth->primitive_num; i++) {
147 v[tri[0]].xrest,
v[tri[1]].xrest,
v[tri[2]].xrest);
162 float weights[3] = {1.0f, 1.0f, 1.0f};
163 float total_force = 0;
164 float total_area = 0;
166 for (
uint i = 0; i < cloth->primitive_num; i++) {
170 float area =
SIM_tri_area(data, tri[0], tri[1], tri[2]);
172 total_force += (vertex_pressure[tri[0]] + vertex_pressure[tri[1]] +
173 vertex_pressure[tri[2]]) *
179 return total_force / total_area;
186 const float ZERO[3] = {0.0f, 0.0f, 0.0f};
193 for (i = 0; i < cloth->mvert_num; i++) {
197 for (i = 0; i < cloth->mvert_num; i++) {
213 if (cloth->implicit) {
215 cloth->implicit =
nullptr;
223 uint mvert_num = cloth->mvert_num, i;
227 for (i = 0; i < mvert_num; i++) {
228 if (cloth_hairdata) {
256 int mvert_num = cloth->mvert_num;
259 const float ZERO[3] = {0.0f, 0.0f, 0.0f};
263 for (
v = 0;
v < mvert_num;
v++) {
269 verts[
v].impulse_count = 0;
281 Cloth *cloth = clmd->clothObject;
283 "cos cloth_calc_helper_forces");
284 float *masses = (
float *)
MEM_callocN(
sizeof(
float) * cloth->mvert_num,
285 "cos cloth_calc_helper_forces");
292 for (i = 0; i < cloth->mvert_num; i++, cv++) {
299 masses[i] = cv->
mass;
304 for (i = 0; i <
steps; i++) {
305 for (node = cloth->springs;
node; node = node->next) {
308 float len, c,
l, vec[3];
329 l = c / ((1.0f / masses[v1]) + (1.0f / masses[
v2]));
343 for (i = 0; i < cloth->mvert_num; i++, cv++) {
368 s->flags &= ~CLOTH_SPRING_FLAG_NEEDED;
372#ifdef CLOTH_FORCE_SPRING_BEND
379 k = scaling * s->restlen * 0.1f;
382 data, s->ij, s->kl, s->pa, s->pb, s->la, s->lb, s->restang, k, parms->
bending_damping);
390#ifdef CLOTH_FORCE_SPRING_STRUCTURAL
391 float k_tension, scaling_tension;
395 scaling_tension = parms->
tension +
397 k_tension = scaling_tension / (parms->
avg_spring_len + FLT_EPSILON);
416 float k_compression, scaling_compression;
419 k_compression = scaling_compression / (parms->
avg_spring_len + FLT_EPSILON);
440 k_tension = scaling_tension / (parms->
avg_spring_len + FLT_EPSILON);
444 float k_compression = scaling_compression / (parms->
avg_spring_len + FLT_EPSILON);
449 if (k_tension == 0.0f) {
451 k_tension_damp = 0.0f;
454 if (k_compression == 0.0f) {
456 k_compression_damp = 0.0f;
474#ifdef CLOTH_FORCE_SPRING_SHEAR
496#ifdef CLOTH_FORCE_SPRING_BEND
497 float kb, cb, scaling;
511#ifdef CLOTH_FORCE_SPRING_BEND
512 float kb, cb, scaling;
520 scaling = s->lin_stiffness * parms->
bending;
532 float x_kl[3], x_mn[3],
v[3], d[3];
539 clmd->debug_data, x_kl, x_mn, 0.8, 0.8, 0.8,
"target", 7981, s->kl);
543 clmd->debug_data, x_kl, d, 0.8, 0.8, 0.2,
"target", 7982, s->kl);
559 uint mvert_num = cloth->mvert_num;
563 for (i = 0; i < mvert_num; i++) {
579 float gravity[3] = {0.0f, 0.0f, 0.0f};
581 uint mvert_num = cloth->mvert_num;
584#ifdef CLOTH_FORCE_GRAVITY
589 scene->physics_settings.gravity,
594 for (i = 0; i < cloth->mvert_num; i++, vert++) {
599 float goal_x[3], goal_v[3];
617#ifdef CLOTH_FORCE_DRAG
623 float pressure_difference = 0.0f;
624 float volume_factor = 1.0f;
631 init_vol = cloth->initial_mesh_volume;
635 if (init_vol > 1E-6f) {
640 volume_factor = init_vol / vol;
641 pressure_difference = volume_factor - 1;
655 float *hydrostatic_pressure =
nullptr;
657 if (
fabs(fluid_density) > 1e-6f) {
658 float hydrostatic_vector[3];
664 if (fluid_density > 0) {
665 sub_v3_v3(hydrostatic_vector, cloth->average_acceleration);
668 fluid_density *= volume_factor;
671 mul_v3_fl(hydrostatic_vector, fluid_density);
674 hydrostatic_pressure = (
float *)
MEM_mallocN(
sizeof(
float) * mvert_num,
675 "hydrostatic pressure gradient");
683 if (hydrostatic_pressure ||
fabs(pressure_difference) > 1E-6f) {
684 float weights[3] = {1.0f, 1.0f, 1.0f};
686 for (i = 0; i < cloth->primitive_num; i++) {
691 data, tri[0], tri[1], tri[2], pressure_difference, hydrostatic_pressure, weights);
696 if (hydrostatic_pressure) {
703 bool is_not_hair = (clmd->
hairdata ==
nullptr) && (cloth->primitive_num > 0);
704 bool has_wind =
false, has_force =
false;
709 float(*forcevec)[3] = is_not_hair ? winvec + mvert_num : winvec;
711 for (i = 0; i < cloth->mvert_num; i++) {
725 has_wind = has_wind || !
is_zero_v3(winvec[i]);
726 has_force = has_force || !
is_zero_v3(forcevec[i]);
731 for (i = 0; i < cloth->primitive_num; i++) {
746 for (
LinkNode *link = cloth->springs; link; link = link->next) {
750 hair_ij = &hairdata[spring->
ij];
751 hair_kl = &hairdata[spring->
kl];
764 for (i = 0; i < cloth->mvert_num; i++, vert++) {
780 for (
LinkNode *link = cloth->springs; link; link = link->next) {
792 const float cell_offset[3],
826 const float cell_scale,
827 const float cell_offset[3],
832 LinkNode *next_spring_link =
nullptr;
836 float x1[3], v1[3], x2[3],
v2[3], x3[3], v3[3], x4[3], v4[3];
837 float dir1[3], dir2[3], dir3[3];
857 while (spring_link) {
875 next_spring_link = spring_link->
next;
902 spring1 ? dir1 :
nullptr,
904 spring3 ? dir3 :
nullptr);
907 return next_spring_link;
914 int mvert_num = cloth->mvert_num;
918 for (i = 0, vert = cloth->verts; i < mvert_num; i++, vert++) {
921 cloth_get_vertex_motion_state(data, vert, x,
v);
926 float cellsize, gmin[3], cell_scale, cell_offset[3];
932 cell_scale = cellsize > 0.0f ? 1.0f / cellsize : 0.0f;
936 link = cloth->springs;
955 int mvert_num = cloth->mvert_num;
958 const float fluid_factor = 0.95f;
977 if (smoothfac > 0.0f || density_strength > 0.0f) {
986 for (i = 0, vert = cloth->verts; i < mvert_num; i++, vert++) {
987 float x[3],
v[3], nv[3];
1010 const int size = 64;
1012 float offset[3], a[3],
b[3];
1014 const float shift = 0.0f;
1027 for (j = 0; j <
size; j++) {
1028 for (i = 0; i <
size; i++) {
1029 float x[3],
v[3], gvel[3], gvel_smooth[3], gdensity;
1036 grid, x, &gdensity, gvel, gvel_smooth,
nullptr,
nullptr);
1040 clmd->debug_data, x, gdensity, 0.7, 0.3, 1,
"grid density", i, j, 3111);
1047 clmd->debug_data, x, gvel, 0.4, 0, 1,
"grid velocity", i, j, 3112);
1049 clmd->debug_data, x, gvel_smooth, 0.6, 1, 1,
"grid velocity", i, j, 3113);
1052 clmd->debug_data, x, dvel, 0.4, 1, 0.7,
"grid velocity", i, j, 3114);
1054 if (gdensity > 0.0f) {
1055 float col0[3] = {0.0, 0.0, 0.0};
1056 float col1[3] = {0.0, 1.0, 0.0};
1075 clmd->debug_data, x,
col[0],
col[1],
col[2],
"grid velocity", i, j, 3115);
1078 clmd->debug_data, x, 0.01f,
col[0],
col[1],
col[2],
"grid velocity", i, j, 3115);
1097 int mvert_num = cloth->mvert_num;
1103 float pressfac = parms->pressure;
1104 float minpress = parms->pressure_threshold;
1105 float gmin[3], gmax[3];
1111 if (smoothfac > 0.0f || pressfac > 0.0f) {
1113 clmd->
sim_parms->voxel_res, gmin, gmax);
1115 vert = cloth->verts;
1116 for (i = 0; i < mvert_num; i++, vert++) {
1119 if (vert->solver_index < 0) {
1130 vert = cloth->verts;
1131 for (i = 0; i < mvert_num; i++, vert++) {
1132 float x[3],
v[3], f[3], dfdx[3][3], dfdv[3][3];
1134 if (vert->solver_index < 0) {
1141 vertex_grid, x,
v, smoothfac, pressfac, minpress, f, dfdx, dfdv);
1155 int i, mvert_num = cloth->mvert_num;
1156 float total[3] = {0.0f, 0.0f, 0.0f};
1158 for (i = 0; i < mvert_num; i++) {
1168 mul_v3_fl(total, 1.0f / dt / mvert_num);
1172 interp_v3_v3v3(cloth->average_acceleration, total, cloth->average_acceleration,
powf(0.25f, dt));
1181 int mvert_num = cloth->mvert_num;
1198 for (i = 0; i < mvert_num; i++) {
1208 for (i = 0; i < mvert_num; i++) {
1257 sres->
status |= result->status;
1268 const bool is_hair = (clmd->
hairdata !=
nullptr);
1274 uint mvert_num = cloth->mvert_num;
1285 clmd->
solver_result = MEM_cnew<ClothSolverResult>(
"cloth solver result");
1290 for (i = 0; i < mvert_num; i++) {
1303 if (!use_acceleration) {
1304 zero_v3(cloth->average_acceleration);
1330 if (use_acceleration) {
1338 for (i = 0; i < mvert_num; i++) {
1357 for (i = 0; i < mvert_num; i++) {
@ CLOTH_SPRING_TYPE_SEWING
@ CLOTH_SPRING_TYPE_SHEAR
@ CLOTH_SPRING_TYPE_BENDING_HAIR
@ CLOTH_SPRING_TYPE_STRUCTURAL
@ CLOTH_SPRING_TYPE_BENDING
@ CLOTH_SPRING_TYPE_INTERNAL
@ CLOTH_SPRING_FLAG_DEACTIVATE
@ CLOTH_SPRING_FLAG_NEEDED
int cloth_bvh_collision(Depsgraph *depsgraph, Object *ob, ClothModifierData *clmd, float step, float dt)
#define BKE_sim_debug_data_add_dot(p, r, g, b, category,...)
void BKE_sim_debug_data_clear_category(const char *category)
#define BKE_sim_debug_data_add_vector(p, d, r, g, b, category,...)
#define BKE_sim_debug_data_add_line(p1, p2, r, g, b, category,...)
void BKE_effectors_apply(struct ListBase *effectors, struct ListBase *colliders, struct EffectorWeights *weights, struct EffectedPoint *point, float *force, float *wind_force, float *impulse)
#define BKE_sim_debug_data_add_circle(p, radius, r, g, b, category,...)
void pd_point_from_loc(struct Scene *scene, float *loc, float *vel, int index, struct EffectedPoint *point)
MINLINE float max_ff(float a, float b)
MINLINE int min_ii(int a, int b)
MINLINE float min_ff(float a, float b)
MINLINE int max_ii(int a, int b)
float volume_tri_tetrahedron_signed_v3_6x(const float v1[3], const float v2[3], const float v3[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 void sub_v3_v3(float r[3], const float a[3])
MINLINE float len_squared_v3v3(const float a[3], const float b[3]) ATTR_WARN_UNUSED_RESULT
MINLINE void sub_v3_v3v3(float r[3], const float a[3], const float b[3])
MINLINE void mul_v3_fl(float r[3], float f)
MINLINE void copy_v3_v3(float r[3], const float a[3])
MINLINE float dot_v3v3(const float a[3], const float b[3]) ATTR_WARN_UNUSED_RESULT
void interp_v3_v3v3(float r[3], const float a[3], const float b[3], float t)
MINLINE void zero_v3_int(int r[3])
MINLINE void negate_v3(float r[3])
MINLINE bool is_zero_v3(const float v[3]) ATTR_WARN_UNUSED_RESULT
MINLINE void madd_v3_v3v3fl(float r[3], const float a[3], const float b[3], float f)
MINLINE void zero_v3(float r[3])
MINLINE void mul_v3_v3fl(float r[3], const float a[3], float f)
MINLINE void add_v3_v3(float r[3], const float a[3])
MINLINE float normalize_v3(float n[3])
#define UNUSED_FUNCTION(x)
#define INIT_MINMAX(min, max)
Scene * DEG_get_evaluated_scene(const Depsgraph *graph)
@ CLOTH_COLLSETTINGS_FLAG_ENABLED
@ CLOTH_COLLSETTINGS_FLAG_SELF
@ CLOTH_SIMSETTINGS_FLAG_PRESSURE_VOL
@ CLOTH_SIMSETTINGS_FLAG_PRESSURE
@ CLOTH_SIMSETTINGS_FLAG_RESIST_SPRING_COMPRESS
Object is a sort of wrapper for general info.
Read Guarded memory(de)allocation.
void SIM_cloth_solver_free(ClothModifierData *clmd)
BLI_INLINE void cloth_get_grid_location(Implicit_Data *data, float cell_scale, const float cell_offset[3], int index, float x[3], float v[3])
static void cloth_continuum_step(ClothModifierData *clmd, float dt)
static int UNUSED_FUNCTION cloth_calc_helper_forces(Object *, ClothModifierData *clmd, float(*initial_cos)[3], float, float dt)
static void cloth_continuum_fill_grid(HairGrid *grid, Cloth *cloth)
static void cloth_clear_result(ClothModifierData *clmd)
static float cloth_calc_average_pressure(ClothModifierData *clmd, const float *vertex_pressure)
static float cloth_calc_rest_volume(ClothModifierData *clmd)
static void cloth_calc_pressure_gradient(ClothModifierData *clmd, const float gradient_vector[3], float *r_vertex_pressure)
static int cloth_count_nondiag_blocks(Cloth *cloth)
int SIM_cloth_solver_init(Object *, ClothModifierData *clmd)
static void hair_get_boundbox(ClothModifierData *clmd, blender::float3 &gmin, blender::float3 &gmax)
static void cloth_calc_average_acceleration(ClothModifierData *clmd, float dt)
static bool cloth_get_pressure_weights(ClothModifierData *clmd, const blender::int3 &vert_tri, float *r_weights)
static void cloth_record_result(ClothModifierData *clmd, ImplicitSolverResult *result, float dt)
BLI_INLINE LinkNode * hair_spring_next(LinkNode *spring_link)
static void cloth_calc_force(Scene *scene, ClothModifierData *clmd, float, ListBase *effectors, float time)
BLI_INLINE void cloth_calc_spring_force(ClothModifierData *clmd, ClothSpring *s)
void SIM_cloth_solver_set_volume(ClothModifierData *clmd)
static void cloth_setup_constraints(ClothModifierData *clmd)
static float cloth_calc_volume(ClothModifierData *clmd)
void SIM_mass_spring_set_implicit_vertex_mass(Implicit_Data *data, int index, float mass)
static void cloth_solve_collisions(Depsgraph *depsgraph, Object *ob, ClothModifierData *clmd, float step, float dt)
void SIM_cloth_solver_set_positions(ClothModifierData *clmd)
int SIM_cloth_solve(Depsgraph *depsgraph, Object *ob, float frame, ClothModifierData *clmd, ListBase *effectors)
static LinkNode * cloth_continuum_add_hair_segments(HairGrid *grid, const float cell_scale, const float cell_offset[3], Cloth *cloth, LinkNode *spring_link)
ATTR_WARN_UNUSED_RESULT const BMVert * v2
ATTR_WARN_UNUSED_RESULT const BMLoop * l
ATTR_WARN_UNUSED_RESULT const BMVert const BMEdge * e
ATTR_WARN_UNUSED_RESULT const BMVert * v
static DBVT_INLINE btScalar size(const btDbvtVolume &a)
local_group_size(16, 16) .push_constant(Type b
const Depsgraph * depsgraph
draw_view in_light_buf[] float
void SIM_hair_volume_add_segment(HairGrid *grid, const float[3], const float[3], const float x2[3], const float v2[3], const float x3[3], const float v3[3], const float[3], const float[3], const float[3], const float[3], const float[3])
void SIM_hair_volume_grid_velocity(HairGrid *grid, const float x[3], const float v[3], float fluid_factor, float r_v[3])
void SIM_hair_volume_grid_interpolate(HairGrid *grid, const float x[3], float *density, float velocity[3], float velocity_smooth[3], float density_gradient[3], float velocity_gradient[3][3])
void SIM_hair_volume_grid_geometry(HairGrid *grid, float *cellsize, int res[3], float gmin[3], float gmax[3])
void SIM_hair_volume_free_vertex_grid(HairGrid *grid)
bool SIM_hair_volume_solve_divergence(HairGrid *grid, float, float target_density, float target_strength)
void SIM_hair_volume_add_vertex(HairGrid *grid, const float x[3], const float v[3])
HairGrid * SIM_hair_volume_create_vertex_grid(float cellsize, const float gmin[3], const float gmax[3])
void SIM_hair_volume_normalize_vertex_grid(HairGrid *grid)
void SIM_hair_volume_vertex_grid_forces(HairGrid *grid, const float x[3], const float v[3], float smoothfac, float pressurefac, float minpressure, float f[3], float dfdx[3][3], float dfdv[3][3])
void SIM_mass_spring_force_vertex_wind(struct Implicit_Data *data, int v, float radius, const float(*winvec)[3])
float SIM_tri_tetra_volume_signed_6x(struct Implicit_Data *data, int v1, int v2, int v3)
void SIM_mass_spring_get_motion_state(struct Implicit_Data *data, int index, float x[3], float v[3])
void SIM_mass_spring_add_constraint_ndof0(struct Implicit_Data *data, int index, const float dV[3])
void SIM_mass_spring_force_edge_wind(struct Implicit_Data *data, int v1, int v2, float radius1, float radius2, const float(*winvec)[3])
void SIM_mass_spring_set_new_velocity(struct Implicit_Data *data, int index, const float v[3])
bool SIM_mass_spring_force_spring_goal(struct Implicit_Data *data, int i, const float goal_x[3], const float goal_v[3], float stiffness, float damping)
float SIM_tri_area(struct Implicit_Data *data, int v1, int v2, int v3)
void SIM_mass_spring_set_vertex_mass(struct Implicit_Data *data, int index, float mass)
void SIM_mass_spring_force_face_extern(struct Implicit_Data *data, int v1, int v2, int v3, const float(*forcevec)[3])
void SIM_mass_spring_get_velocity(struct Implicit_Data *data, int index, float v[3])
void SIM_mass_spring_set_motion_state(struct Implicit_Data *data, int index, const float x[3], const float v[3])
bool SIM_mass_spring_force_spring_linear(struct Implicit_Data *data, int i, int j, float restlen, float stiffness_tension, float damping_tension, float stiffness_compression, float damping_compression, bool resist_compress, bool new_compress, float clamp_force)
void SIM_mass_spring_get_new_position(struct Implicit_Data *data, int index, float x[3])
bool SIM_mass_spring_force_spring_bending(struct Implicit_Data *data, int i, int j, float restlen, float kb, float cb)
void SIM_mass_spring_apply_result(struct Implicit_Data *data)
void SIM_mass_spring_set_position(struct Implicit_Data *data, int index, const float x[3])
bool SIM_mass_spring_force_spring_bending_hair(struct Implicit_Data *data, int i, int j, int k, const float target[3], float stiffness, float damping)
void SIM_mass_spring_force_pressure(struct Implicit_Data *data, int v1, int v2, int v3, float common_pressure, const float *vertex_pressure, const float weights[3])
void SIM_mass_spring_force_face_wind(struct Implicit_Data *data, int v1, int v2, int v3, const float(*winvec)[3])
void SIM_mass_spring_get_position(struct Implicit_Data *data, int index, float x[3])
bool SIM_mass_spring_solve_velocities(struct Implicit_Data *data, float dt, struct ImplicitSolverResult *result)
void SIM_mass_spring_force_drag(struct Implicit_Data *data, float drag)
void SIM_mass_spring_clear_constraints(struct Implicit_Data *data)
bool SIM_mass_spring_solve_positions(struct Implicit_Data *data, float dt)
void SIM_mass_spring_get_new_velocity(struct Implicit_Data *data, int index, float v[3])
void SIM_mass_spring_set_velocity(struct Implicit_Data *data, int index, const float v[3])
bool SIM_mass_spring_force_spring_angular(struct Implicit_Data *data, int i, int j, int *i_a, int *i_b, int len_a, int len_b, float restang, float stiffness, float damping)
void SIM_mass_spring_force_extern(struct Implicit_Data *data, int i, const float f[3], float dfdx[3][3], float dfdv[3][3])
void SIM_mass_spring_force_gravity(struct Implicit_Data *data, int index, float mass, const float g[3])
void SIM_mass_spring_set_rest_transform(struct Implicit_Data *data, int index, float tfm[3][3])
void SIM_mass_spring_clear_forces(struct Implicit_Data *data)
Implicit_Data * SIM_mass_spring_solver_create(int numverts, int numsprings)
void SIM_mass_spring_solver_free(Implicit_Data *id)
void *(* MEM_mallocN)(size_t len, const char *str)
void MEM_freeN(void *vmemh)
void *(* MEM_callocN)(size_t len, const char *str)
ccl_device_inline float2 fabs(const float2 a)
ccl_device_inline float3 cos(float3 v)
void min_max(const T &value, T &min, T &max)
struct ClothSolverResult * solver_result
struct ClothHairData * hairdata
struct Cloth * clothObject
struct ClothSimSettings * sim_parms
struct ClothCollSettings * coll_parms
struct EffectorWeights * effector_weights
float uniform_pressure_force
float max_internal_tension
float max_internal_compression
float internal_compression
float initial_mesh_volume