[Bf-blender-cvs] [aa8279648e2] master: Simulation: extract node tree parsing code to separate file

Jacques Lucke noreply at git.blender.org
Fri Jul 17 21:22:11 CEST 2020


Commit: aa8279648e22c8fc5ffc943036b77c47d1f8a764
Author: Jacques Lucke
Date:   Fri Jul 17 21:19:48 2020 +0200
Branches: master
https://developer.blender.org/rBaa8279648e22c8fc5ffc943036b77c47d1f8a764

Simulation: extract node tree parsing code to separate file

===================================================================

M	source/blender/simulation/CMakeLists.txt
A	source/blender/simulation/intern/simulation_collect_influences.cc
A	source/blender/simulation/intern/simulation_collect_influences.hh
M	source/blender/simulation/intern/simulation_solver.cc
M	source/blender/simulation/intern/simulation_solver.hh
M	source/blender/simulation/intern/simulation_update.cc

===================================================================

diff --git a/source/blender/simulation/CMakeLists.txt b/source/blender/simulation/CMakeLists.txt
index ec8880dd8cd..a19e96e1a91 100644
--- a/source/blender/simulation/CMakeLists.txt
+++ b/source/blender/simulation/CMakeLists.txt
@@ -41,13 +41,15 @@ set(SRC
   intern/hair_volume.cpp
   intern/implicit_blender.c
   intern/implicit_eigen.cpp
-  intern/simulation_solver.cc
   intern/particle_function.cc
+  intern/simulation_collect_influences.cc
+  intern/simulation_solver.cc
   intern/simulation_update.cc
 
   intern/ConstrainedConjugateGradient.h
   intern/eigen_utils.h
   intern/implicit.h
+  intern/simulation_collect_influences.hh
   intern/simulation_solver.hh
 
   SIM_mass_spring.h
diff --git a/source/blender/simulation/intern/simulation_update.cc b/source/blender/simulation/intern/simulation_collect_influences.cc
similarity index 57%
copy from source/blender/simulation/intern/simulation_update.cc
copy to source/blender/simulation/intern/simulation_collect_influences.cc
index 18d9ea8b853..3feb0ccce5b 100644
--- a/source/blender/simulation/intern/simulation_update.cc
+++ b/source/blender/simulation/intern/simulation_collect_influences.cc
@@ -14,59 +14,19 @@
  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  */
 
+#include "simulation_collect_influences.hh"
 #include "SIM_particle_function.hh"
-#include "SIM_simulation_update.hh"
-
-#include "BKE_customdata.h"
-#include "BKE_simulation.h"
-
-#include "DNA_scene_types.h"
-#include "DNA_simulation_types.h"
-
-#include "DEG_depsgraph_query.h"
-
-#include "BLI_array.hh"
-#include "BLI_float3.hh"
-#include "BLI_listbase.h"
-#include "BLI_map.hh"
-#include "BLI_rand.h"
-#include "BLI_vector.hh"
-
-#include "NOD_node_tree_multi_function.hh"
 
 #include "FN_attributes_ref.hh"
 #include "FN_multi_function_network_evaluation.hh"
 #include "FN_multi_function_network_optimization.hh"
 
-#include "simulation_solver.hh"
-
-extern "C" {
-void WM_clipboard_text_set(const char *buf, bool selection);
-}
+#include "NOD_node_tree_multi_function.hh"
 
 namespace blender::sim {
 
-static void copy_states_to_cow(Simulation *simulation_orig, Simulation *simulation_cow)
-{
-  BKE_simulation_state_remove_all(simulation_cow);
-  simulation_cow->current_frame = simulation_orig->current_frame;
-
-  LISTBASE_FOREACH (SimulationState *, state_orig, &simulation_orig->states) {
-    switch ((eSimulationStateType)state_orig->type) {
-      case SIM_STATE_TYPE_PARTICLES: {
-        ParticleSimulationState *particle_state_orig = (ParticleSimulationState *)state_orig;
-        ParticleSimulationState *particle_state_cow = (ParticleSimulationState *)
-            BKE_simulation_state_add(simulation_cow, SIM_STATE_TYPE_PARTICLES, state_orig->name);
-        particle_state_cow->tot_particles = particle_state_orig->tot_particles;
-        CustomData_copy(&particle_state_orig->attributes,
-                        &particle_state_cow->attributes,
-                        CD_MASK_ALL,
-                        CD_DUPLICATE,
-                        particle_state_orig->tot_particles);
-        break;
-      }
-    }
-  }
+extern "C" {
+void WM_clipboard_text_set(const char *buf, bool selection);
 }
 
 static Map<const fn::MFOutputSocket *, std::string> deduplicate_attribute_nodes(
@@ -128,87 +88,6 @@ static Map<const fn::MFOutputSocket *, std::string> deduplicate_attribute_nodes(
   return attribute_inputs;
 }
 
-static std::string dnode_to_path(const nodes::DNode &dnode)
-{
-  std::string path;
-  for (const nodes::DParentNode *parent = dnode.parent(); parent; parent = parent->parent()) {
-    path = parent->node_ref().name() + "/" + path;
-  }
-  path = path + dnode.name();
-  return path;
-}
-
-static void remove_unused_states(Simulation *simulation, const VectorSet<std::string> &state_names)
-{
-  LISTBASE_FOREACH_MUTABLE (SimulationState *, state, &simulation->states) {
-    if (!state_names.contains(state->name)) {
-      BKE_simulation_state_remove(simulation, state);
-    }
-  }
-}
-
-static void reset_states(Simulation *simulation)
-{
-  LISTBASE_FOREACH (SimulationState *, state, &simulation->states) {
-    switch ((eSimulationStateType)state->type) {
-      case SIM_STATE_TYPE_PARTICLES: {
-        ParticleSimulationState *particle_state = (ParticleSimulationState *)state;
-        CustomData_free(&particle_state->attributes, particle_state->tot_particles);
-        particle_state->tot_particles = 0;
-        break;
-      }
-    }
-  }
-}
-
-static SimulationState *try_find_state_by_name(Simulation *simulation, StringRef name)
-{
-  LISTBASE_FOREACH (SimulationState *, state, &simulation->states) {
-    if (state->name == name) {
-      return state;
-    }
-  }
-  return nullptr;
-}
-
-static void add_missing_particle_states(Simulation *simulation, Span<std::string> state_names)
-{
-  for (StringRefNull name : state_names) {
-    SimulationState *state = try_find_state_by_name(simulation, name);
-    if (state != nullptr) {
-      BLI_assert(state->type == SIM_STATE_TYPE_PARTICLES);
-      continue;
-    }
-
-    BKE_simulation_state_add(simulation, SIM_STATE_TYPE_PARTICLES, name.c_str());
-  }
-}
-
-static void reinitialize_empty_simulation_states(Simulation *simulation,
-                                                 const nodes::DerivedNodeTree &tree)
-{
-  VectorSet<std::string> state_names;
-  for (const nodes::DNode *dnode : tree.nodes_by_type("SimulationNodeParticleSimulation")) {
-    state_names.add(dnode_to_path(*dnode));
-  }
-
-  remove_unused_states(simulation, state_names);
-  reset_states(simulation);
-  add_missing_particle_states(simulation, state_names);
-}
-
-static void update_simulation_state_list(Simulation *simulation,
-                                         const nodes::DerivedNodeTree &tree)
-{
-  VectorSet<std::string> state_names;
-  for (const nodes::DNode *dnode : tree.nodes_by_type("SimulationNodeParticleSimulation")) {
-    state_names.add(dnode_to_path(*dnode));
-  }
-
-  remove_unused_states(simulation, state_names);
-  add_missing_particle_states(simulation, state_names);
-}
-
 class ParticleAttributeInput : public ParticleFunctionInput {
  private:
   std::string attribute_name_;
@@ -324,8 +203,7 @@ static Vector<const ParticleForce *> create_forces_for_particle_simulation(
   return forces;
 }
 
-static void collect_forces(Simulation &simulation,
-                           nodes::MFNetworkTreeMap &network_map,
+static void collect_forces(nodes::MFNetworkTreeMap &network_map,
                            ResourceCollector &resources,
                            const Map<const fn::MFOutputSocket *, std::string> &attribute_inputs,
                            SimulationInfluences &r_influences)
@@ -335,33 +213,15 @@ static void collect_forces(Simulation &simulation,
     std::string name = dnode_to_path(*dnode);
     Vector<const ParticleForce *> forces = create_forces_for_particle_simulation(
         *dnode, network_map, resources, attribute_inputs);
-    ParticleSimulationState *state = (ParticleSimulationState *)try_find_state_by_name(&simulation,
-                                                                                       name);
-    r_influences.particle_forces.add_new(state, std::move(forces));
+    r_influences.particle_forces.add_new(std::move(name), std::move(forces));
   }
 }
 
-void update_simulation_in_depsgraph(Depsgraph *depsgraph,
-                                    Scene *scene_cow,
-                                    Simulation *simulation_cow)
+void collect_simulation_influences(const nodes::DerivedNodeTree &tree,
+                                   ResourceCollector &resources,
+                                   SimulationInfluences &r_influences)
 {
-  int current_frame = scene_cow->r.cfra;
-  if (simulation_cow->current_frame == current_frame) {
-    return;
-  }
-
-  /* Below we modify the original state/cache. Only the active depsgraph is allowed to do that. */
-  if (!DEG_is_active(depsgraph)) {
-    return;
-  }
-
-  Simulation *simulation_orig = (Simulation *)DEG_get_original_id(&simulation_cow->id);
-
-  nodes::NodeTreeRefMap tree_refs;
-  /* TODO: Use simulation_cow, but need to add depsgraph relations before that. */
-  const nodes::DerivedNodeTree tree{simulation_orig->nodetree, tree_refs};
-  fn::MFNetwork network;
-  ResourceCollector resources;
+  fn::MFNetwork &network = resources.construct<fn::MFNetwork>(AT);
   nodes::MFNetworkTreeMap network_map = insert_node_tree_into_mf_network(network, tree, resources);
   Map<const fn::MFOutputSocket *, std::string> attribute_inputs = deduplicate_attribute_nodes(
       network, network_map, tree);
@@ -370,27 +230,7 @@ void update_simulation_in_depsgraph(Depsgraph *depsgraph,
   fn::mf_network_optimization::dead_node_removal(network);
   // WM_clipboard_text_set(network.to_dot().c_str(), false);
 
-  SimulationInfluences simulation_influences;
-  collect_forces(
-      *simulation_orig, network_map, resources, attribute_inputs, simulation_influences);
-
-  if (current_frame == 1) {
-    reinitialize_empty_simulation_states(simulation_orig, tree);
-
-    initialize_simulation_states(*simulation_orig, *depsgraph, simulation_influences);
-    simulation_orig->current_frame = 1;
-
-    copy_states_to_cow(simulation_orig, simulation_cow);
-  }
-  else if (current_frame == simulation_orig->current_frame + 1) {
-    update_simulation_state_list(simulation_orig, tree);
-
-    float time_step = 1.0f / 24.0f;
-    solve_simulation_time_step(*simulation_orig, *depsgraph, simulation_influences, time_step);
-    simulation_orig->current_frame = current_frame;
-
-    copy_states_to_cow(simulation_orig, simulation_cow);
-  }
+  collect_forces(network_map, resources, attribute_inputs, r_influences);
 }
 
 }  // namespace blender::sim
diff --git a/source/blender/simulation/intern/simulation_collect_influences.hh b/source/blender/simulation/intern/simulation_collect_influences.hh
new file mode 100644
index 00000000000..a02a6320419
--- /dev/null
+++ b/source/blender/simulation/intern/simulation_collect_influences.hh
@@ -0,0 +1,45 @@
+/*
+ * This program is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU General Public License
+ * as published by the Free Softw

@@ Diff output truncated at 10240 characters. @@



More information about the Bf-blender-cvs mailing list