Depsgraph: remove EvaluationContext, pass Depsgraph instead.
[blender.git] / source / blender / blenkernel / intern / rigidbody.c
index 011971b6677fdd918636e79eee44157ea384ef59..ab8f9dcb2e4b02566287079adfa48abb4d468c69 100644 (file)
@@ -66,6 +66,8 @@
 #include "BKE_rigidbody.h"
 #include "BKE_scene.h"
 
+#include "DEG_depsgraph.h"
+
 /* ************************************** */
 /* Memory Management */
 
@@ -92,7 +94,7 @@ void BKE_rigidbody_free_world(RigidBodyWorld *rbw)
        if (rbw->physics_world) {
                /* free physics references, we assume that all physics objects in will have been added to the world */
                if (rbw->constraints) {
-                       FOREACH_GROUP_OBJECT(rbw->constraints, object)
+                       FOREACH_GROUP_OBJECT_BEGIN(rbw->constraints, object)
                        {
                                if (object->rigidbody_constraint) {
                                        RigidBodyCon *rbc = object->rigidbody_constraint;
@@ -101,11 +103,11 @@ void BKE_rigidbody_free_world(RigidBodyWorld *rbw)
                                        }
                                }
                        }
-                       FOREACH_GROUP_OBJECT_END
+                       FOREACH_GROUP_OBJECT_END;
                }
 
                if (rbw->group) {
-                       FOREACH_GROUP_OBJECT(rbw->group, object)
+                       FOREACH_GROUP_OBJECT_BEGIN(rbw->group, object)
                        {
                                if (object->rigidbody_object) {
                                        RigidBodyOb *rbo = object->rigidbody_object;
@@ -114,7 +116,7 @@ void BKE_rigidbody_free_world(RigidBodyWorld *rbw)
                                        }
                                }
                        }
-                       FOREACH_GROUP_OBJECT_END
+                       FOREACH_GROUP_OBJECT_END;
                }
                /* free dynamics world */
                RB_dworld_delete(rbw->physics_world);
@@ -702,6 +704,39 @@ static void rigidbody_validate_sim_object(RigidBodyWorld *rbw, Object *ob, bool
 
 /* --------------------- */
 
+static void rigidbody_constraint_set_limits(RigidBodyCon *rbc, void (*set_limits)(rbConstraint*,int,float,float))
+{
+       if (rbc->flag & RBC_FLAG_USE_LIMIT_LIN_X)
+               set_limits(rbc->physics_constraint, RB_LIMIT_LIN_X, rbc->limit_lin_x_lower, rbc->limit_lin_x_upper);
+       else
+               set_limits(rbc->physics_constraint, RB_LIMIT_LIN_X, 0.0f, -1.0f);
+
+       if (rbc->flag & RBC_FLAG_USE_LIMIT_LIN_Y)
+               set_limits(rbc->physics_constraint, RB_LIMIT_LIN_Y, rbc->limit_lin_y_lower, rbc->limit_lin_y_upper);
+       else
+               set_limits(rbc->physics_constraint, RB_LIMIT_LIN_Y, 0.0f, -1.0f);
+
+       if (rbc->flag & RBC_FLAG_USE_LIMIT_LIN_Z)
+               set_limits(rbc->physics_constraint, RB_LIMIT_LIN_Z, rbc->limit_lin_z_lower, rbc->limit_lin_z_upper);
+       else
+               set_limits(rbc->physics_constraint, RB_LIMIT_LIN_Z, 0.0f, -1.0f);
+
+       if (rbc->flag & RBC_FLAG_USE_LIMIT_ANG_X)
+               set_limits(rbc->physics_constraint, RB_LIMIT_ANG_X, rbc->limit_ang_x_lower, rbc->limit_ang_x_upper);
+       else
+               set_limits(rbc->physics_constraint, RB_LIMIT_ANG_X, 0.0f, -1.0f);
+
+       if (rbc->flag & RBC_FLAG_USE_LIMIT_ANG_Y)
+               set_limits(rbc->physics_constraint, RB_LIMIT_ANG_Y, rbc->limit_ang_y_lower, rbc->limit_ang_y_upper);
+       else
+               set_limits(rbc->physics_constraint, RB_LIMIT_ANG_Y, 0.0f, -1.0f);
+
+       if (rbc->flag & RBC_FLAG_USE_LIMIT_ANG_Z)
+               set_limits(rbc->physics_constraint, RB_LIMIT_ANG_Z, rbc->limit_ang_z_lower, rbc->limit_ang_z_upper);
+       else
+               set_limits(rbc->physics_constraint, RB_LIMIT_ANG_Z, 0.0f, -1.0f);
+}
+
 /**
  * Create physics sim representation of constraint given rigid body constraint settings
  *
@@ -820,40 +855,13 @@ static void rigidbody_validate_sim_constraint(RigidBodyWorld *rbw, Object *ob, b
                                        RB_constraint_set_damping_6dof_spring(rbc->physics_constraint, RB_LIMIT_ANG_Z, rbc->spring_damping_ang_z);
 
                                        RB_constraint_set_equilibrium_6dof_spring(rbc->physics_constraint);
-                                       ATTR_FALLTHROUGH;
-                               case RBC_TYPE_6DOF:
-                                       if (rbc->type == RBC_TYPE_6DOF) /* a litte awkward but avoids duplicate code for limits */
-                                               rbc->physics_constraint = RB_constraint_new_6dof(loc, rot, rb1, rb2);
-
-                                       if (rbc->flag & RBC_FLAG_USE_LIMIT_LIN_X)
-                                               RB_constraint_set_limits_6dof(rbc->physics_constraint, RB_LIMIT_LIN_X, rbc->limit_lin_x_lower, rbc->limit_lin_x_upper);
-                                       else
-                                               RB_constraint_set_limits_6dof(rbc->physics_constraint, RB_LIMIT_LIN_X, 0.0f, -1.0f);
 
-                                       if (rbc->flag & RBC_FLAG_USE_LIMIT_LIN_Y)
-                                               RB_constraint_set_limits_6dof(rbc->physics_constraint, RB_LIMIT_LIN_Y, rbc->limit_lin_y_lower, rbc->limit_lin_y_upper);
-                                       else
-                                               RB_constraint_set_limits_6dof(rbc->physics_constraint, RB_LIMIT_LIN_Y, 0.0f, -1.0f);
-
-                                       if (rbc->flag & RBC_FLAG_USE_LIMIT_LIN_Z)
-                                               RB_constraint_set_limits_6dof(rbc->physics_constraint, RB_LIMIT_LIN_Z, rbc->limit_lin_z_lower, rbc->limit_lin_z_upper);
-                                       else
-                                               RB_constraint_set_limits_6dof(rbc->physics_constraint, RB_LIMIT_LIN_Z, 0.0f, -1.0f);
-
-                                       if (rbc->flag & RBC_FLAG_USE_LIMIT_ANG_X)
-                                               RB_constraint_set_limits_6dof(rbc->physics_constraint, RB_LIMIT_ANG_X, rbc->limit_ang_x_lower, rbc->limit_ang_x_upper);
-                                       else
-                                               RB_constraint_set_limits_6dof(rbc->physics_constraint, RB_LIMIT_ANG_X, 0.0f, -1.0f);
-
-                                       if (rbc->flag & RBC_FLAG_USE_LIMIT_ANG_Y)
-                                               RB_constraint_set_limits_6dof(rbc->physics_constraint, RB_LIMIT_ANG_Y, rbc->limit_ang_y_lower, rbc->limit_ang_y_upper);
-                                       else
-                                               RB_constraint_set_limits_6dof(rbc->physics_constraint, RB_LIMIT_ANG_Y, 0.0f, -1.0f);
+                                       rigidbody_constraint_set_limits(rbc, RB_constraint_set_limits_6dof_spring);
+                                       break;
+                               case RBC_TYPE_6DOF:
+                                       rbc->physics_constraint = RB_constraint_new_6dof(loc, rot, rb1, rb2);
 
-                                       if (rbc->flag & RBC_FLAG_USE_LIMIT_ANG_Z)
-                                               RB_constraint_set_limits_6dof(rbc->physics_constraint, RB_LIMIT_ANG_Z, rbc->limit_ang_z_lower, rbc->limit_ang_z_upper);
-                                       else
-                                               RB_constraint_set_limits_6dof(rbc->physics_constraint, RB_LIMIT_ANG_Z, 0.0f, -1.0f);
+                                       rigidbody_constraint_set_limits(rbc, RB_constraint_set_limits_6dof);
                                        break;
                                case RBC_TYPE_MOTOR:
                                        rbc->physics_constraint = RB_constraint_new_motor(loc, rot, rb1, rb2);
@@ -1148,7 +1156,7 @@ void BKE_rigidbody_remove_object(Scene *scene, Object *ob)
 
                /* remove object from rigid body constraints */
                if (rbw->constraints) {
-                       FOREACH_GROUP_OBJECT(rbw->constraints, obt)
+                       FOREACH_GROUP_OBJECT_BEGIN(rbw->constraints, obt)
                        {
                                if (obt && obt->rigidbody_constraint) {
                                        rbc = obt->rigidbody_constraint;
@@ -1157,7 +1165,7 @@ void BKE_rigidbody_remove_object(Scene *scene, Object *ob)
                                        }
                                }
                        }
-                       FOREACH_GROUP_OBJECT_END
+                       FOREACH_GROUP_OBJECT_END;
                }
        }
 
@@ -1201,12 +1209,12 @@ static void rigidbody_update_ob_array(RigidBodyWorld *rbw)
        }
 
        i = 0;
-       FOREACH_GROUP_OBJECT(rbw->group, object)
+       FOREACH_GROUP_OBJECT_BEGIN(rbw->group, object)
        {
                rbw->objects[i] = object;
                i++;
        }
-       FOREACH_GROUP_OBJECT_END
+       FOREACH_GROUP_OBJECT_END;
 }
 
 static void rigidbody_update_sim_world(Scene *scene, RigidBodyWorld *rbw)
@@ -1229,7 +1237,7 @@ static void rigidbody_update_sim_world(Scene *scene, RigidBodyWorld *rbw)
        rigidbody_update_ob_array(rbw);
 }
 
-static void rigidbody_update_sim_ob(const struct EvaluationContext *eval_ctx, Scene *scene, RigidBodyWorld *rbw, Object *ob, RigidBodyOb *rbo)
+static void rigidbody_update_sim_ob(struct Depsgraph *depsgraph, Scene *scene, RigidBodyWorld *rbw, Object *ob, RigidBodyOb *rbo)
 {
        float loc[3];
        float rot[4];
@@ -1277,7 +1285,7 @@ static void rigidbody_update_sim_ob(const struct EvaluationContext *eval_ctx, Sc
                ListBase *effectors;
 
                /* get effectors present in the group specified by effector_weights */
-               effectors = pdInitEffectors(eval_ctx, scene, ob, NULL, effector_weights, true);
+               effectors = pdInitEffectors(depsgraph, scene, ob, NULL, effector_weights, true);
                if (effectors) {
                        float eff_force[3] = {0.0f, 0.0f, 0.0f};
                        float eff_loc[3], eff_vel[3];
@@ -1318,7 +1326,7 @@ static void rigidbody_update_sim_ob(const struct EvaluationContext *eval_ctx, Sc
  *
  * \param rebuild Rebuild entire simulation
  */
-static void rigidbody_update_simulation(const struct EvaluationContext *eval_ctx, Scene *scene, RigidBodyWorld *rbw, bool rebuild)
+static void rigidbody_update_simulation(struct Depsgraph *depsgraph, Scene *scene, RigidBodyWorld *rbw, bool rebuild)
 {
        /* update world */
        if (rebuild)
@@ -1332,7 +1340,7 @@ static void rigidbody_update_simulation(const struct EvaluationContext *eval_ctx
         * Memory management needs redesign here, this is just a dirty workaround.
         */
        if (rebuild && rbw->constraints) {
-               FOREACH_GROUP_OBJECT(rbw->constraints, ob)
+               FOREACH_GROUP_OBJECT_BEGIN(rbw->constraints, ob)
                {
                        RigidBodyCon *rbc = ob->rigidbody_constraint;
                        if (rbc && rbc->physics_constraint) {
@@ -1341,17 +1349,17 @@ static void rigidbody_update_simulation(const struct EvaluationContext *eval_ctx
                                rbc->physics_constraint = NULL;
                        }
                }
-               FOREACH_GROUP_OBJECT_END
+               FOREACH_GROUP_OBJECT_END;
        }
 
        /* update objects */
-       FOREACH_GROUP_OBJECT(rbw->group, ob)
+       FOREACH_GROUP_OBJECT_BEGIN(rbw->group, ob)
        {
                if (ob->type == OB_MESH) {
                        /* validate that we've got valid object set up here... */
                        RigidBodyOb *rbo = ob->rigidbody_object;
                        /* update transformation matrix of the object so we don't get a frame of lag for simple animations */
-                       BKE_object_where_is_calc(eval_ctx, scene, ob);
+                       BKE_object_where_is_calc(depsgraph, scene, ob);
 
                        if (rbo == NULL) {
                                /* Since this object is included in the sim group but doesn't have
@@ -1385,21 +1393,21 @@ static void rigidbody_update_simulation(const struct EvaluationContext *eval_ctx
                        }
 
                        /* update simulation object... */
-                       rigidbody_update_sim_ob(eval_ctx, scene, rbw, ob, rbo);
+                       rigidbody_update_sim_ob(depsgraph, scene, rbw, ob, rbo);
                }
        }
-       FOREACH_GROUP_OBJECT_END
+       FOREACH_GROUP_OBJECT_END;
        
        /* update constraints */
        if (rbw->constraints == NULL) /* no constraints, move on */
                return;
 
-       FOREACH_GROUP_OBJECT(rbw->constraints, ob)
+       FOREACH_GROUP_OBJECT_BEGIN(rbw->constraints, ob)
        {
                /* validate that we've got valid object set up here... */
                RigidBodyCon *rbc = ob->rigidbody_constraint;
                /* update transformation matrix of the object so we don't get a frame of lag for simple animations */
-               BKE_object_where_is_calc(eval_ctx, scene, ob);
+               BKE_object_where_is_calc(depsgraph, scene, ob);
 
                if (rbc == NULL) {
                        /* Since this object is included in the group but doesn't have
@@ -1422,12 +1430,12 @@ static void rigidbody_update_simulation(const struct EvaluationContext *eval_ctx
                        rbc->flag &= ~RBC_FLAG_NEEDS_VALIDATE;
                }
        }
-       FOREACH_GROUP_OBJECT_END
+       FOREACH_GROUP_OBJECT_END;
 }
 
 static void rigidbody_update_simulation_post_step(RigidBodyWorld *rbw)
 {
-       FOREACH_GROUP_BASE(rbw->group, base)
+       FOREACH_GROUP_BASE_BEGIN(rbw->group, base)
        {
                Object *ob = base->object;
                RigidBodyOb *rbo = ob->rigidbody_object;
@@ -1555,7 +1563,7 @@ void BKE_rigidbody_cache_reset(RigidBodyWorld *rbw)
 
 /* Rebuild rigid body world */
 /* NOTE: this needs to be called before frame update to work correctly */
-void BKE_rigidbody_rebuild_world(const struct EvaluationContext *eval_ctx, Scene *scene, float ctime)
+void BKE_rigidbody_rebuild_world(struct Depsgraph *depsgraph, Scene *scene, float ctime)
 {
        RigidBodyWorld *rbw = scene->rigidbody_world;
        PointCache *cache;
@@ -1574,7 +1582,7 @@ void BKE_rigidbody_rebuild_world(const struct EvaluationContext *eval_ctx, Scene
        if (ctime == startframe + 1 && rbw->ltime == startframe) {
                if (cache->flag & PTCACHE_OUTDATED) {
                        BKE_ptcache_id_reset(scene, &pid, PTCACHE_RESET_OUTDATED);
-                       rigidbody_update_simulation(eval_ctx, scene, rbw, true);
+                       rigidbody_update_simulation(depsgraph, scene, rbw, true);
                        BKE_ptcache_validate(cache, (int)ctime);
                        cache->last_exact = 0;
                        cache->flag &= ~PTCACHE_REDO_NEEDED;
@@ -1583,7 +1591,7 @@ void BKE_rigidbody_rebuild_world(const struct EvaluationContext *eval_ctx, Scene
 }
 
 /* Run RigidBody simulation for the specified physics world */
-void BKE_rigidbody_do_simulation(const struct EvaluationContext *eval_ctx, Scene *scene, float ctime)
+void BKE_rigidbody_do_simulation(struct Depsgraph *depsgraph, Scene *scene, float ctime)
 {
        float timestep;
        RigidBodyWorld *rbw = scene->rigidbody_world;
@@ -1628,7 +1636,7 @@ void BKE_rigidbody_do_simulation(const struct EvaluationContext *eval_ctx, Scene
                }
 
                /* update and validate simulation */
-               rigidbody_update_simulation(eval_ctx, scene, rbw, false);
+               rigidbody_update_simulation(depsgraph, scene, rbw, false);
 
                /* calculate how much time elapsed since last step in seconds */
                timestep = 1.0f / (float)FPS * (ctime - rbw->ltime) * rbw->time_scale;
@@ -1672,8 +1680,8 @@ void BKE_rigidbody_sync_transforms(RigidBodyWorld *rbw, Object *ob, float ctime)
 void BKE_rigidbody_aftertrans_update(Object *ob, float loc[3], float rot[3], float quat[4], float rotAxis[3], float rotAngle) {}
 bool BKE_rigidbody_check_sim_running(RigidBodyWorld *rbw, float ctime) { return false; }
 void BKE_rigidbody_cache_reset(RigidBodyWorld *rbw) {}
-void BKE_rigidbody_rebuild_world(const struct EvaluationContext *eval_ctx, Scene *scene, float ctime) {}
-void BKE_rigidbody_do_simulation(const struct EvaluationContext *eval_ctx, Scene *scene, float ctime) {}
+void BKE_rigidbody_rebuild_world(struct Depsgraph *depsgraph, Scene *scene, float ctime) {}
+void BKE_rigidbody_do_simulation(struct Depsgraph *depsgraph, Scene *scene, float ctime) {}
 
 #ifdef __GNUC__
 #  pragma GCC diagnostic pop
@@ -1684,47 +1692,35 @@ void BKE_rigidbody_do_simulation(const struct EvaluationContext *eval_ctx, Scene
 /* -------------------- */
 /* Depsgraph evaluation */
 
-void BKE_rigidbody_rebuild_sim(const struct EvaluationContext *eval_ctx,
+void BKE_rigidbody_rebuild_sim(struct Depsgraph *depsgraph,
                                Scene *scene)
 {
        float ctime = BKE_scene_frame_get(scene);
-
-       if (G.debug & G_DEBUG_DEPSGRAPH_EVAL) {
-               printf("%s at %f\n", __func__, ctime);
-       }
-
+       DEG_debug_print_eval_time(__func__, scene->id.name, scene, ctime);
        /* rebuild sim data (i.e. after resetting to start of timeline) */
        if (BKE_scene_check_rigidbody_active(scene)) {
-               BKE_rigidbody_rebuild_world(eval_ctx, scene, ctime);
+               BKE_rigidbody_rebuild_world(depsgraph, scene, ctime);
        }
 }
 
-void BKE_rigidbody_eval_simulation(const struct EvaluationContext *eval_ctx,
+void BKE_rigidbody_eval_simulation(struct Depsgraph *depsgraph,
                                    Scene *scene)
 {
        float ctime = BKE_scene_frame_get(scene);
-
-       if (G.debug & G_DEBUG_DEPSGRAPH_EVAL) {
-               printf("%s at %f\n", __func__, ctime);
-       }
-
+       DEG_debug_print_eval_time(__func__, scene->id.name, scene, ctime);
        /* evaluate rigidbody sim */
        if (BKE_scene_check_rigidbody_active(scene)) {
-               BKE_rigidbody_do_simulation(eval_ctx, scene, ctime);
+               BKE_rigidbody_do_simulation(depsgraph, scene, ctime);
        }
 }
 
-void BKE_rigidbody_object_sync_transforms(const struct EvaluationContext *UNUSED(eval_ctx),
+void BKE_rigidbody_object_sync_transforms(struct Depsgraph *UNUSED(depsgraph),
                                           Scene *scene,
                                           Object *ob)
 {
        RigidBodyWorld *rbw = scene->rigidbody_world;
        float ctime = BKE_scene_frame_get(scene);
-
-       if (G.debug & G_DEBUG_DEPSGRAPH_EVAL) {
-               printf("%s on %s\n", __func__, ob->id.name);
-       }
-
+       DEG_debug_print_eval_time(__func__, ob->id.name, ob, ctime);
        /* read values pushed into RBO from sim/cache... */
        BKE_rigidbody_sync_transforms(rbw, ob, ctime);
 }