[Bf-blender-cvs] [25582aef61c] master: Simulation: separate code from node tree parsing and solver
Jacques Lucke
noreply at git.blender.org
Fri Jul 17 21:22:11 CEST 2020
Commit: 25582aef61ce6fdf8e6e9b8f86e5bff294370f3d
Author: Jacques Lucke
Date: Fri Jul 17 20:51:52 2020 +0200
Branches: master
https://developer.blender.org/rB25582aef61ce6fdf8e6e9b8f86e5bff294370f3d
Simulation: separate code from node tree parsing and solver
===================================================================
M source/blender/simulation/CMakeLists.txt
A source/blender/simulation/intern/simulation_solver.cc
A 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 fa4838b0875..ec8880dd8cd 100644
--- a/source/blender/simulation/CMakeLists.txt
+++ b/source/blender/simulation/CMakeLists.txt
@@ -38,15 +38,18 @@ set(INC_SYS
set(SRC
intern/SIM_mass_spring.cpp
- intern/ConstrainedConjugateGradient.h
- intern/eigen_utils.h
intern/hair_volume.cpp
- intern/implicit.h
intern/implicit_blender.c
intern/implicit_eigen.cpp
+ intern/simulation_solver.cc
intern/particle_function.cc
intern/simulation_update.cc
+ intern/ConstrainedConjugateGradient.h
+ intern/eigen_utils.h
+ intern/implicit.h
+ intern/simulation_solver.hh
+
SIM_mass_spring.h
SIM_particle_function.hh
SIM_simulation_update.hh
diff --git a/source/blender/simulation/intern/simulation_solver.cc b/source/blender/simulation/intern/simulation_solver.cc
new file mode 100644
index 00000000000..289e586682e
--- /dev/null
+++ b/source/blender/simulation/intern/simulation_solver.cc
@@ -0,0 +1,138 @@
+/*
+ * 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 Software Foundation; either version 2
+ * of the License, or (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
+ */
+
+#include "simulation_solver.hh"
+
+#include "BKE_customdata.h"
+
+#include "BLI_rand.hh"
+
+namespace blender::sim {
+
+ParticleForce::~ParticleForce()
+{
+}
+
+class CustomDataAttributesRef {
+ private:
+ Vector<void *> buffers_;
+ uint size_;
+ std::unique_ptr<fn::AttributesInfo> info_;
+
+ public:
+ CustomDataAttributesRef(CustomData &custom_data, uint size)
+ {
+ fn::AttributesInfoBuilder builder;
+ for (const CustomDataLayer &layer : Span(custom_data.layers, custom_data.totlayer)) {
+ buffers_.append(layer.data);
+ switch (layer.type) {
+ case CD_PROP_INT32: {
+ builder.add<int32_t>(layer.name, 0);
+ break;
+ }
+ case CD_PROP_FLOAT3: {
+ builder.add<float3>(layer.name, {0, 0, 0});
+ break;
+ }
+ }
+ }
+ info_ = std::make_unique<fn::AttributesInfo>(builder);
+ size_ = size;
+ }
+
+ operator fn::MutableAttributesRef()
+ {
+ return fn::MutableAttributesRef(*info_, buffers_, size_);
+ }
+
+ operator fn::AttributesRef() const
+ {
+ return fn::AttributesRef(*info_, buffers_, size_);
+ }
+};
+
+static void ensure_attributes_exist(ParticleSimulationState *state)
+{
+ if (CustomData_get_layer_named(&state->attributes, CD_PROP_FLOAT3, "Position") == nullptr) {
+ CustomData_add_layer_named(
+ &state->attributes, CD_PROP_FLOAT3, CD_CALLOC, nullptr, state->tot_particles, "Position");
+ }
+ if (CustomData_get_layer_named(&state->attributes, CD_PROP_FLOAT3, "Velocity") == nullptr) {
+ CustomData_add_layer_named(
+ &state->attributes, CD_PROP_FLOAT3, CD_CALLOC, nullptr, state->tot_particles, "Velocity");
+ }
+ if (CustomData_get_layer_named(&state->attributes, CD_PROP_INT32, "ID") == nullptr) {
+ CustomData_add_layer_named(
+ &state->attributes, CD_PROP_INT32, CD_CALLOC, nullptr, state->tot_particles, "ID");
+ }
+}
+
+void initialize_simulation_states(Simulation &simulation,
+ Depsgraph &UNUSED(depsgraph),
+ const SimulationInfluences &UNUSED(influences))
+{
+ RandomNumberGenerator rng;
+
+ LISTBASE_FOREACH (ParticleSimulationState *, state, &simulation.states) {
+ state->tot_particles = 1000;
+ CustomData_realloc(&state->attributes, state->tot_particles);
+ ensure_attributes_exist(state);
+
+ CustomDataAttributesRef custom_data_attributes{state->attributes, (uint)state->tot_particles};
+
+ fn::MutableAttributesRef attributes = custom_data_attributes;
+ MutableSpan<float3> positions = attributes.get<float3>("Position");
+ MutableSpan<float3> velocities = attributes.get<float3>("Velocity");
+ MutableSpan<int32_t> ids = attributes.get<int32_t>("ID");
+
+ for (uint i : positions.index_range()) {
+ positions[i] = {i / 100.0f, 0, 0};
+ velocities[i] = {0, rng.get_float() - 0.5f, rng.get_float() - 0.5f};
+ ids[i] = i;
+ }
+ }
+}
+
+void solve_simulation_time_step(Simulation &simulation,
+ Depsgraph &UNUSED(depsgraph),
+ const SimulationInfluences &influences,
+ float time_step)
+{
+ LISTBASE_FOREACH (ParticleSimulationState *, state, &simulation.states) {
+ ensure_attributes_exist(state);
+
+ CustomDataAttributesRef custom_data_attributes{state->attributes, (uint)state->tot_particles};
+
+ fn::MutableAttributesRef attributes = custom_data_attributes;
+ MutableSpan<float3> positions = attributes.get<float3>("Position");
+ MutableSpan<float3> velocities = attributes.get<float3>("Velocity");
+
+ Array<float3> force_vectors{(uint)state->tot_particles, {0, 0, 0}};
+ const Vector<const ParticleForce *> *forces = influences.particle_forces.lookup_ptr(state);
+ if (forces != nullptr) {
+ for (const ParticleForce *force : *forces) {
+ force->add_force(attributes, force_vectors);
+ }
+ }
+
+ for (uint i : positions.index_range()) {
+ velocities[i] += force_vectors[i] * time_step;
+ positions[i] += velocities[i] * time_step;
+ }
+ }
+}
+
+} // namespace blender::sim
diff --git a/source/blender/simulation/intern/simulation_solver.hh b/source/blender/simulation/intern/simulation_solver.hh
new file mode 100644
index 00000000000..c96cfee52da
--- /dev/null
+++ b/source/blender/simulation/intern/simulation_solver.hh
@@ -0,0 +1,53 @@
+/*
+ * 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 Software Foundation; either version 2
+ * of the License, or (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
+ */
+
+#ifndef __SIM_SIMULATION_SOLVER_HH__
+#define __SIM_SIMULATION_SOLVER_HH__
+
+#include "BLI_float3.hh"
+#include "BLI_span.hh"
+
+#include "DNA_simulation_types.h"
+
+#include "FN_attributes_ref.hh"
+
+struct Depsgraph;
+
+namespace blender::sim {
+
+class ParticleForce {
+ public:
+ virtual ~ParticleForce();
+ virtual void add_force(fn::AttributesRef attributes,
+ MutableSpan<float3> r_combined_force) const = 0;
+};
+
+struct SimulationInfluences {
+ Map<const ParticleSimulationState *, Vector<const ParticleForce *>> particle_forces;
+};
+
+void initialize_simulation_states(Simulation &simulation,
+ Depsgraph &depsgraph,
+ const SimulationInfluences &influences);
+
+void solve_simulation_time_step(Simulation &simulation,
+ Depsgraph &depsgraph,
+ const SimulationInfluences &influences,
+ float time_step);
+
+} // namespace blender::sim
+
+#endif /* __SIM_SIMULATION_SOLVER_HH__ */
diff --git a/source/blender/simulation/intern/simulation_update.cc b/source/blender/simulation/intern/simulation_update.cc
index d682b018b11..18d9ea8b853 100644
--- a/source/blender/simulation/intern/simulation_update.cc
+++ b/source/blender/simulation/intern/simulation_update.cc
@@ -38,28 +38,14 @@
#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);
}
namespace blender::sim {
-static void ensure_attributes_exist(ParticleSimulationState *state)
-{
- if (CustomData_get_layer_named(&state->attributes, CD_PROP_FLOAT3, "Position") == nullptr) {
- CustomData_add_layer_named(
- &state->attributes, CD_PROP_FLOAT3, CD_CALLOC, nullptr, state->tot_particles, "Position");
- }
- if (CustomData_get_layer_named(&state->attributes, CD_PROP_FLOAT3, "Velocity") == nullptr) {
- CustomData_add_layer_named(
- &state->attributes, CD_PROP_FLOAT3, CD_CALLOC, nullptr, state->tot_particles, "Velocity");
- }
- if (CustomData_get_layer_named(&state->attributes, CD_PROP_INT32, "ID") == nullptr) {
- CustomData_add_layer_named(
- &state->attributes, CD_PROP_INT32, CD_CALLOC, nullptr, state->tot_particles, "ID");
- }
-}
-
static void copy_states_to_cow(Simulation *simulation_orig, Simulation *simulation_cow)
{
BKE_simulation_state_remove_all(simulation_cow);
@@ -142,44 +128,6 @@ static Map<const fn::MFOutputSocket *, std::string> deduplicate_attribute_nodes(
return attribute_inputs;
}
-class CustomDataAttributesRef {
- private:
- Vector<void *> buffers_;
- uint size_;
- std::unique_ptr<fn::AttributesInfo> info_;
-
- public:
- CustomDataAttributesRef(CustomData &custom_data, uint size)
- {
- fn::AttributesInfoBuilder builder;
- for (const CustomDataLayer &layer : Span(custom_data.layers, custom_data.totlayer)) {
- buffers_.append(layer.data);
- switch (layer.type) {
- case CD_PROP_INT32: {
- builder.add<int32_t>(layer.name, 0);
- break;
- }
- case CD_PROP_FLOAT3: {
- builder.add<float3>(layer.name, {0, 0, 0});
- break;
- }
- }
- }
- i
@@ Diff output truncated at 10240 characters. @@
More information about the Bf-blender-cvs
mailing list