Blender V5.0
softbody.cc File Reference
#include <algorithm>
#include <cmath>
#include <cstdlib>
#include <cstring>
#include "CLG_log.h"
#include "MEM_guardedalloc.h"
#include "DNA_collection_types.h"
#include "DNA_curve_types.h"
#include "DNA_lattice_types.h"
#include "DNA_mesh_types.h"
#include "DNA_meshdata_types.h"
#include "DNA_object_force_types.h"
#include "DNA_object_types.h"
#include "DNA_scene_types.h"
#include "BLI_ghash.h"
#include "BLI_listbase.h"
#include "BLI_math_geom.h"
#include "BLI_math_matrix.h"
#include "BLI_math_vector.h"
#include "BLI_threads.h"
#include "BLI_time.h"
#include "BLI_utildefines.h"
#include "BKE_collision.h"
#include "BKE_curve.hh"
#include "BKE_customdata.hh"
#include "BKE_deform.hh"
#include "BKE_effect.h"
#include "BKE_global.hh"
#include "BKE_layer.hh"
#include "BKE_mesh.hh"
#include "BKE_modifier.hh"
#include "BKE_pointcache.h"
#include "BKE_scene.hh"
#include "BKE_softbody.h"
#include "DEG_depsgraph.hh"
#include "DEG_depsgraph_query.hh"

Go to the source code of this file.

Classes

struct  BodySpring
struct  BodyFace
struct  ReferenceVert
struct  ReferenceState
struct  SBScratch
struct  SB_thread_context
struct  CCDF_MinMax
struct  ccd_Mesh

Macros

#define MID_PRESERVE   1
#define SOFTGOALSNAP   0.999f
#define HEUNWARNLIMIT   1 /* 500 would be fine i think for detecting severe *stiff* stuff */
#define BSF_INTERSECT   1 /* edge intersects collider face */
#define SBF_DOFUZZY   1 /* Body-point do fuzzy. */
#define SBF_OUTOFCOLLISION   2 /* Body-point does not collide. */
#define BFF_INTERSECT   1 /* collider edge intrudes face. */
#define BFF_CLOSEVERT   2 /* collider vertex repulses face. */

Enumerations

enum  type_spring { SB_EDGE = 1 , SB_BEND = 2 , SB_STIFFQUAD = 3 , SB_HANDLE = 4 }

Functions

static void free_softbody_intern (SoftBody *sb)
static float sb_grav_force_scale (Object *)
static float sb_fric_force_scale (Object *)
static float sb_time_scale (Object *ob)
static float _final_goal (Object *ob, BodyPoint *bp)
static float _final_mass (Object *ob, BodyPoint *bp)
static ccd_Meshccd_mesh_make (Object *ob)
static void ccd_mesh_update (Object *ob, ccd_Mesh *pccd_M)
static void ccd_mesh_free (ccd_Mesh *ccdm)
static void ccd_build_deflector_hash_single (GHash *hash, Object *ob)
static void ccd_build_deflector_hash (Depsgraph *depsgraph, Collection *collection, Object *vertexowner, GHash *hash)
static void ccd_update_deflector_hash_single (GHash *hash, Object *ob)
static void ccd_update_deflector_hash (Depsgraph *depsgraph, Collection *collection, Object *vertexowner, GHash *hash)
static int count_mesh_quads (Mesh *mesh)
static void add_mesh_quad_diag_springs (Object *ob)
static void add_2nd_order_roller (Object *ob, float, int *counter, int addsprings)
static void add_2nd_order_springs (Object *ob, float stiffness)
static void add_bp_springlist (BodyPoint *bp, int springID)
static void build_bps_springlist (Object *ob)
static void calculate_collision_balls (Object *ob)
static void renew_softbody (Object *ob, int totpoint, int totspring)
static void free_softbody_baked (SoftBody *sb)
static void free_scratch (SoftBody *sb)
static int query_external_colliders (Depsgraph *depsgraph, Collection *collection)
static int sb_detect_aabb_collisionCached (float[3], Object *vertexowner, float)
static int sb_detect_face_pointCached (const float face_v1[3], const float face_v2[3], const float face_v3[3], float *damp, float force[3], Object *vertexowner, float time)
static int sb_detect_face_collisionCached (const float face_v1[3], const float face_v2[3], const float face_v3[3], float *damp, float force[3], Object *vertexowner, float time)
static void scan_for_ext_face_forces (Object *ob, float timenow)
static int sb_detect_edge_collisionCached (const float edge_v1[3], const float edge_v2[3], float *damp, float force[3], Object *vertexowner, float time)
static void _scan_for_ext_spring_forces (Scene *scene, Object *ob, float timenow, int ifirst, int ilast, ListBase *effectors)
static void * exec_scan_for_ext_spring_forces (void *data)
static void sb_sfesf_threads_run (Depsgraph *depsgraph, Scene *scene, Object *ob, float timenow, int totsprings, int *ptr_to_break_func(void))
static int choose_winner (float *w, float *pos, float *a, float *b, float *c, float *ca, float *cb, float *cc)
static int sb_detect_vertex_collisionCached (float opco[3], float facenormal[3], float *damp, float force[3], Object *vertexowner, float time, float vel[3], float *intrusion)
static int sb_deflect_face (Object *ob, float *actpos, float *facenormal, float *force, float *cf, float time, float *vel, float *intrusion)
static void sb_spring_force (Object *ob, int bpi, BodySpring *bs, float iks, float)
static int _softbody_calc_forces_slice_in_a_thread (Scene *scene, Object *ob, float forcetime, float timenow, int ifirst, int ilast, int *ptr_to_break_func(void), ListBase *effectors, int do_deflector, float fieldfactor, float windfactor)
static void * exec_softbody_calc_forces (void *data)
static void sb_cf_threads_run (Scene *scene, Object *ob, float forcetime, float timenow, int totpoint, int *ptr_to_break_func(void), ListBase *effectors, int do_deflector, float fieldfactor, float windfactor)
static void softbody_calc_forces (Depsgraph *depsgraph, Scene *scene, Object *ob, float forcetime, float timenow)
static void softbody_apply_forces (Object *ob, float forcetime, int mode, float *err, int mid_flags)
static void softbody_restore_prev_step (Object *ob)
static void softbody_apply_goalsnap (Object *ob)
static void apply_spring_memory (Object *ob)
static void interpolate_exciter (Object *ob, int timescale, int time)
static void springs_from_mesh (Object *ob)
static void mesh_to_softbody (Object *ob)
static void mesh_faces_to_scratch (Object *ob)
static void reference_to_scratch (Object *ob)
static float globallen (float *v1, float *v2, Object *ob)
static void makelatticesprings (Lattice *lt, BodySpring *bs, int dostiff, Object *ob)
static void lattice_to_softbody (Object *ob)
static void curve_surf_to_softbody (Object *ob)
static void softbody_to_object (Object *ob, float(*vertexCos)[3], int numVerts, int local)
static void sb_new_scratch (SoftBody *sb)
SoftBodysbNew ()
void sbFree (Object *ob)
void sbFreeSimulation (SoftBody *sb)
void sbObjectToSoftbody (Object *ob)
static bool object_has_edges (const Object *ob)
void sbSetInterruptCallBack (int(*f)())
static void softbody_update_positions (Object *ob, SoftBody *sb, float(*vertexCos)[3], int numVerts)
void SB_estimate_transform (Object *ob, float lloc[3], float lrot[3][3], float lscale[3][3])
static void softbody_reset (Object *ob, SoftBody *sb, float(*vertexCos)[3], int numVerts)
static void softbody_step (Depsgraph *depsgraph, Scene *scene, Object *ob, SoftBody *sb, float dtime)
static void sbStoreLastFrame (Depsgraph *depsgraph, Object *object, float framenr)
void sbObjectStep (Depsgraph *depsgraph, Scene *scene, Object *ob, float cfra, float(*vertexCos)[3], int numVerts)

Variables

static CLG_LogRef LOG = {"physics.softbody"}
static int(* SB_localInterruptCallBack )() = nullptr
static float SoftHeunTol = 1.0f
static const int CCD_SAFETY = 190561

Macro Definition Documentation

◆ BFF_CLOSEVERT

#define BFF_CLOSEVERT   2 /* collider vertex repulses face. */

Definition at line 144 of file softbody.cc.

Referenced by scan_for_ext_face_forces().

◆ BFF_INTERSECT

#define BFF_INTERSECT   1 /* collider edge intrudes face. */

Definition at line 143 of file softbody.cc.

Referenced by scan_for_ext_face_forces().

◆ BSF_INTERSECT

#define BSF_INTERSECT   1 /* edge intersects collider face */

◆ HEUNWARNLIMIT

#define HEUNWARNLIMIT   1 /* 500 would be fine i think for detecting severe *stiff* stuff */

Definition at line 135 of file softbody.cc.

Referenced by softbody_step().

◆ MID_PRESERVE

#define MID_PRESERVE   1

Definition at line 129 of file softbody.cc.

Referenced by softbody_apply_forces(), and softbody_step().

◆ SBF_DOFUZZY

#define SBF_DOFUZZY   1 /* Body-point do fuzzy. */

◆ SBF_OUTOFCOLLISION

#define SBF_OUTOFCOLLISION   2 /* Body-point does not collide. */

Definition at line 141 of file softbody.cc.

Referenced by _softbody_calc_forces_slice_in_a_thread(), and curve_surf_to_softbody().

◆ SOFTGOALSNAP

#define SOFTGOALSNAP   0.999f

Definition at line 131 of file softbody.cc.

Enumeration Type Documentation

◆ type_spring

Enumerator
SB_EDGE 
SB_BEND 
SB_STIFFQUAD 
SB_HANDLE 

Definition at line 77 of file softbody.cc.

Function Documentation

◆ _final_goal()

◆ _final_mass()

◆ _scan_for_ext_spring_forces()

◆ _softbody_calc_forces_slice_in_a_thread()

int _softbody_calc_forces_slice_in_a_thread ( Scene * scene,
Object * ob,
float forcetime,
float timenow,
int ifirst,
int ilast,
int * ptr_to_break_funcvoid,
ListBase * effectors,
int do_deflector,
float fieldfactor,
float windfactor )
static

◆ add_2nd_order_roller()

void add_2nd_order_roller ( Object * ob,
float ,
int * counter,
int addsprings )
static

◆ add_2nd_order_springs()

void add_2nd_order_springs ( Object * ob,
float stiffness )
static

◆ add_bp_springlist()

void add_bp_springlist ( BodyPoint * bp,
int springID )
static

Definition at line 708 of file softbody.cc.

References MEM_calloc_arrayN(), MEM_freeN(), BodyPoint::nofsprings, and BodyPoint::springs.

Referenced by build_bps_springlist().

◆ add_mesh_quad_diag_springs()

◆ apply_spring_memory()

void apply_spring_memory ( Object * ob)
static

◆ build_bps_springlist()

void build_bps_springlist ( Object * ob)
static

Do this once when sb is build it is O(N^2) so scanning for springs every iteration is too expensive.

Definition at line 731 of file softbody.cc.

References add_bp_springlist(), b, SoftBody::bpoint, SoftBody::bspring, MEM_freeN(), Object::soft, BodyPoint::springs, SoftBody::totpoint, SoftBody::totspring, BodySpring::v1, and BodySpring::v2.

Referenced by curve_surf_to_softbody(), lattice_to_softbody(), and mesh_to_softbody().

◆ calculate_collision_balls()

◆ ccd_build_deflector_hash()

void ccd_build_deflector_hash ( Depsgraph * depsgraph,
Collection * collection,
Object * vertexowner,
GHash * hash )
static
Note
collection overrides scene when not NULL.

Definition at line 505 of file softbody.cc.

References BKE_collision_objects_create(), BKE_collision_objects_free(), ccd_build_deflector_hash_single(), depsgraph, eModifierType_Collision, hash, i, OB_MESH, and Object::type.

Referenced by softbody_step().

◆ ccd_build_deflector_hash_single()

void ccd_build_deflector_hash_single ( GHash * hash,
Object * ob )
static

◆ ccd_mesh_free()

void ccd_mesh_free ( ccd_Mesh * ccdm)
static

◆ ccd_mesh_make()

◆ ccd_mesh_update()

◆ ccd_update_deflector_hash()

void ccd_update_deflector_hash ( Depsgraph * depsgraph,
Collection * collection,
Object * vertexowner,
GHash * hash )
static
Note
collection overrides scene when not NULL.

Definition at line 542 of file softbody.cc.

References BKE_collision_objects_create(), BKE_collision_objects_free(), ccd_update_deflector_hash_single(), depsgraph, eModifierType_Collision, hash, i, OB_MESH, and Object::type.

Referenced by softbody_step().

◆ ccd_update_deflector_hash_single()

void ccd_update_deflector_hash_single ( GHash * hash,
Object * ob )
static

◆ choose_winner()

int choose_winner ( float * w,
float * pos,
float * a,
float * b,
float * c,
float * ca,
float * cb,
float * cc )
static

Definition at line 1580 of file softbody.cc.

References b, copy_v3_v3(), dot_v3v3(), fabsf, pos, and w().

Referenced by sb_detect_vertex_collisionCached().

◆ count_mesh_quads()

int count_mesh_quads ( Mesh * mesh)
static

Definition at line 568 of file softbody.cc.

References faces, i, result, and size().

Referenced by add_mesh_quad_diag_springs().

◆ curve_surf_to_softbody()

◆ exec_scan_for_ext_spring_forces()

◆ exec_softbody_calc_forces()

◆ free_scratch()

◆ free_softbody_baked()

void free_softbody_baked ( SoftBody * sb)
static

Definition at line 864 of file softbody.cc.

References SoftBody::keys, MEM_freeN(), MEM_SAFE_FREE, and SoftBody::totkey.

Referenced by free_softbody_intern().

◆ free_softbody_intern()

◆ globallen()

float globallen ( float * v1,
float * v2,
Object * ob )
static

helper function to get proper spring length when object is re-scaled

Definition at line 2810 of file softbody.cc.

References copy_v3_v3(), len_v3v3(), mul_m4_v3(), and v2.

Referenced by curve_surf_to_softbody(), and makelatticesprings().

◆ interpolate_exciter()

void interpolate_exciter ( Object * ob,
int timescale,
int time )
static

◆ lattice_to_softbody()

◆ makelatticesprings()

void makelatticesprings ( Lattice * lt,
BodySpring * bs,
int dostiff,
Object * ob )
static

◆ mesh_faces_to_scratch()

◆ mesh_to_softbody()

◆ object_has_edges()

bool object_has_edges ( const Object * ob)
static

Definition at line 3196 of file softbody.cc.

References Object::data, OB_LATTICE, OB_MESH, and Object::type.

Referenced by sbObjectStep().

◆ query_external_colliders()

int query_external_colliders ( Depsgraph * depsgraph,
Collection * collection )
static
Note
collection overrides scene when not NULL.

Definition at line 961 of file softbody.cc.

References BKE_collision_objects_create(), BKE_collision_objects_free(), depsgraph, and eModifierType_Collision.

Referenced by softbody_calc_forces().

◆ reference_to_scratch()

◆ renew_softbody()

◆ sb_cf_threads_run()

◆ sb_deflect_face()

int sb_deflect_face ( Object * ob,
float * actpos,
float * facenormal,
float * force,
float * cf,
float time,
float * vel,
float * intrusion )
static

◆ sb_detect_aabb_collisionCached()

◆ sb_detect_edge_collisionCached()

◆ sb_detect_face_collisionCached()

◆ sb_detect_face_pointCached()

◆ sb_detect_vertex_collisionCached()

◆ SB_estimate_transform()

void SB_estimate_transform ( Object * ob,
float lloc[3],
float lrot[3][3],
float lscale[3][3] )

A precise position vector denoting the motion of the center of mass give a rotation/scale matrix using averaging method, that's why estimate and not calculate see: this is kind of reverse engineering: having to states of a point cloud and recover what happened our advantage here we know the identity of the vertex there are others methods giving other results.

Parameters
obAny object that can do soft-body e.g. mesh, lattice, curve.
llocOutput of the calculated location (or NULL).
lrotOutput of the calculated rotation (or NULL).
lscaleOutput for the calculated scale (or NULL).

For velocity & 2nd order stuff see: vcloud_estimate_transform_v3.

Definition at line 3238 of file softbody.cc.

References SoftBody::bpoint, copy_m3_m3(), copy_v3_v3(), float, ReferenceState::ivert, SoftBody::lcom, SoftBody::lrot, SoftBody::lscale, MEM_calloc_arrayN(), MEM_freeN(), BodyPoint::pos, ReferenceVert::pos, SBScratch::Ref, SoftBody::scratch, Object::soft, SoftBody::totpoint, and vcloud_estimate_transform_v3().

Referenced by softbody_reset(), and softbody_to_object().

◆ sb_fric_force_scale()

float sb_fric_force_scale ( Object * )
static

Re-scaling unit of drag [1 / sec] to somehow reasonable put it to a function here, so we can add user options later without touching simulation code.

Definition at line 169 of file softbody.cc.

Referenced by _softbody_calc_forces_slice_in_a_thread(), and sb_spring_force().

◆ sb_grav_force_scale()

float sb_grav_force_scale ( Object * )
static

Since unit of g is [m/sec^2] and F = mass * g we re-scale unit mass of node to 1 gram put it to a function here, so we can add user options later without touching simulation code.

Definition at line 160 of file softbody.cc.

Referenced by _softbody_calc_forces_slice_in_a_thread().

◆ sb_new_scratch()

◆ sb_sfesf_threads_run()

◆ sb_spring_force()

◆ sb_time_scale()

float sb_time_scale ( Object * ob)
static

Defining the frames to real time relation.

Definition at line 177 of file softbody.cc.

References SoftBody::physics_speed, and Object::soft.

Referenced by softbody_apply_forces().

◆ sbFree()

◆ sbFreeSimulation()

void sbFreeSimulation ( struct SoftBody * sb)

Frees simulation data to reset simulation.

Definition at line 3184 of file softbody.cc.

References free_softbody_intern().

Referenced by BKE_ptcache_id_reset().

◆ sbNew()

◆ sbObjectStep()

◆ sbObjectToSoftbody()

void sbObjectToSoftbody ( struct Object * ob)

Makes totally fresh start situation, resets time.

Definition at line 3189 of file softbody.cc.

References free_softbody_intern(), and Object::soft.

◆ sbSetInterruptCallBack()

void sbSetInterruptCallBack ( int(* )(void))

Soft-body global visible functions. Links the soft-body module to a 'test for Interrupt' function, pass NULL to clear the callback.

Definition at line 3208 of file softbody.cc.

References SB_localInterruptCallBack.

◆ sbStoreLastFrame()

void sbStoreLastFrame ( Depsgraph * depsgraph,
Object * object,
float framenr )
static

Definition at line 3494 of file softbody.cc.

References DEG_get_original(), DEG_is_active(), depsgraph, SoftBody::last_frame, and Object::soft.

Referenced by sbObjectStep().

◆ scan_for_ext_face_forces()

◆ softbody_apply_forces()

void softbody_apply_forces ( Object * ob,
float forcetime,
int mode,
float * err,
int mid_flags )
static

◆ softbody_apply_goalsnap()

void softbody_apply_goalsnap ( Object * ob)
static

Care for body-points taken out of the 'ordinary' solver step because they are screwed to goal by bolts they just need to move along with the goal in time we need to adjust them on sub frame timing in solver so now when frame is done .. put them to the position at the end of frame.

Definition at line 2569 of file softbody.cc.

References _final_goal(), SoftBody::bpoint, copy_v3_v3(), BodyPoint::origT, BodyPoint::pos, BodyPoint::prevpos, Object::soft, SOFTGOALSNAP, and SoftBody::totpoint.

Referenced by softbody_step().

◆ softbody_calc_forces()

◆ softbody_reset()

◆ softbody_restore_prev_step()

void softbody_restore_prev_step ( Object * ob)
static

◆ softbody_step()

◆ softbody_to_object()

void softbody_to_object ( Object * ob,
float(*) vertexCos[3],
int numVerts,
int local )
static

◆ softbody_update_positions()

void softbody_update_positions ( Object * ob,
SoftBody * sb,
float(*) vertexCos[3],
int numVerts )
static

◆ springs_from_mesh()

Variable Documentation

◆ CCD_SAFETY

const int CCD_SAFETY = 190561
static

Definition at line 253 of file softbody.cc.

Referenced by ccd_mesh_free(), and ccd_mesh_make().

◆ LOG

CLG_LogRef LOG = {"physics.softbody"}
static

variables on the UI for now

float mediafrict;  friction to env
float nodemass;    softbody mass of *vertex*
float grav;        softbody amount of gravitation to apply

float goalspring;  softbody goal springs
float goalfrict;   softbody goal springs friction
float mingoal;     quick limits for goal
float maxgoal;

float inspring;    softbody inner springs
float infrict;     softbody inner springs friction

Definition at line 70 of file softbody.cc.

◆ SB_localInterruptCallBack

int(* SB_localInterruptCallBack) () ( ) = nullptr
static

Definition at line 73 of file softbody.cc.

Referenced by sbSetInterruptCallBack(), and softbody_step().

◆ SoftHeunTol

float SoftHeunTol = 1.0f
static

Definition at line 147 of file softbody.cc.

Referenced by softbody_step().