[Bf-blender-cvs] [1cc5f834426] functions: introduce force interface

Jacques Lucke noreply at git.blender.org
Wed Jul 24 19:12:02 CEST 2019


Commit: 1cc5f8344261a237d1eb63268c5aec6b9fc51851
Author: Jacques Lucke
Date:   Wed Jul 24 15:46:30 2019 +0200
Branches: functions
https://developer.blender.org/rB1cc5f8344261a237d1eb63268c5aec6b9fc51851

introduce force interface

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

M	source/blender/simulations/CMakeLists.txt
A	source/blender/simulations/bparticles/force_interface.cpp
A	source/blender/simulations/bparticles/force_interface.hpp
M	source/blender/simulations/bparticles/forces.cpp
M	source/blender/simulations/bparticles/forces.hpp
M	source/blender/simulations/bparticles/integrator.cpp
M	source/blender/simulations/bparticles/integrator.hpp

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

diff --git a/source/blender/simulations/CMakeLists.txt b/source/blender/simulations/CMakeLists.txt
index 6d4c5721526..49d081d890e 100644
--- a/source/blender/simulations/CMakeLists.txt
+++ b/source/blender/simulations/CMakeLists.txt
@@ -54,6 +54,8 @@ set(SRC
   bparticles/offset_handlers.cpp
   bparticles/particle_function.hpp
   bparticles/particle_function.cpp
+  bparticles/force_interface.hpp
+  bparticles/force_interface.cpp
 )
 
 set(LIB
diff --git a/source/blender/simulations/bparticles/force_interface.cpp b/source/blender/simulations/bparticles/force_interface.cpp
new file mode 100644
index 00000000000..def469c58bb
--- /dev/null
+++ b/source/blender/simulations/bparticles/force_interface.cpp
@@ -0,0 +1,4 @@
+#include "force_interface.hpp"
+
+namespace ForceInterface {
+}
diff --git a/source/blender/simulations/bparticles/force_interface.hpp b/source/blender/simulations/bparticles/force_interface.hpp
new file mode 100644
index 00000000000..c89a4b2a2f5
--- /dev/null
+++ b/source/blender/simulations/bparticles/force_interface.hpp
@@ -0,0 +1,37 @@
+#pragma once
+
+#include "particles_container.hpp"
+
+namespace BParticles {
+
+class ForceInterface {
+ private:
+  ParticlesBlock &m_block;
+  ArrayAllocator &m_array_allocator;
+  ArrayRef<float3> m_destination;
+
+ public:
+  ForceInterface(ParticlesBlock &block,
+                 ArrayAllocator &array_allocator,
+                 ArrayRef<float3> destination)
+      : m_block(block), m_array_allocator(array_allocator), m_destination(destination)
+  {
+  }
+
+  ParticlesBlock &block()
+  {
+    return m_block;
+  }
+
+  ArrayAllocator &array_allocator()
+  {
+    return m_array_allocator;
+  }
+
+  ArrayRef<float3> combined_destination()
+  {
+    return m_destination;
+  }
+};
+
+}  // namespace BParticles
diff --git a/source/blender/simulations/bparticles/forces.cpp b/source/blender/simulations/bparticles/forces.cpp
index 28d00e9355d..1f9081dbe23 100644
--- a/source/blender/simulations/bparticles/forces.cpp
+++ b/source/blender/simulations/bparticles/forces.cpp
@@ -8,8 +8,11 @@ Force::~Force()
 {
 }
 
-void GravityForce::add_force(ParticlesBlock &block, ArrayRef<float3> r_force)
+void GravityForce::add_force(ForceInterface &interface)
 {
+  ParticlesBlock &block = interface.block();
+  ArrayRef<float3> destination = interface.combined_destination();
+
   FN_TUPLE_CALL_ALLOC_TUPLES(m_compute_acceleration_body, fn_in, fn_out);
 
   FN::ExecutionStack stack;
@@ -20,12 +23,15 @@ void GravityForce::add_force(ParticlesBlock &block, ArrayRef<float3> r_force)
   float3 acceleration = fn_out.get<float3>(0);
 
   for (uint i = 0; i < block.active_amount(); i++) {
-    r_force[i] += acceleration;
+    destination[i] += acceleration;
   }
 };
 
-void TurbulenceForce::add_force(ParticlesBlock &block, ArrayRef<float3> r_force)
+void TurbulenceForce::add_force(ForceInterface &interface)
 {
+  ParticlesBlock &block = interface.block();
+  ArrayRef<float3> destination = interface.combined_destination();
+
   auto positions = block.attributes().get_float3("Position");
 
   FN_TUPLE_CALL_ALLOC_TUPLES(m_compute_strength_body, fn_in, fn_out);
@@ -40,7 +46,7 @@ void TurbulenceForce::add_force(ParticlesBlock &block, ArrayRef<float3> r_force)
     float x = (BLI_gNoise(0.5f, pos.x, pos.y, pos.z + 1000.0f, false, 1) - 0.5f) * strength.x;
     float y = (BLI_gNoise(0.5f, pos.x, pos.y + 1000.0f, pos.z, false, 1) - 0.5f) * strength.y;
     float z = (BLI_gNoise(0.5f, pos.x + 1000.0f, pos.y, pos.z, false, 1) - 0.5f) * strength.z;
-    r_force[pindex] += {x, y, z};
+    destination[pindex] += {x, y, z};
   }
 }
 
diff --git a/source/blender/simulations/bparticles/forces.hpp b/source/blender/simulations/bparticles/forces.hpp
index fe44ea9cc1a..55725e47d92 100644
--- a/source/blender/simulations/bparticles/forces.hpp
+++ b/source/blender/simulations/bparticles/forces.hpp
@@ -2,13 +2,14 @@
 
 #include "step_description.hpp"
 #include "actions.hpp"
+#include "force_interface.hpp"
 
 namespace BParticles {
 
 class Force {
  public:
   virtual ~Force() = 0;
-  virtual void add_force(ParticlesBlock &block, ArrayRef<float3> r_force) = 0;
+  virtual void add_force(ForceInterface &interface) = 0;
 };
 
 class GravityForce : public Force {
@@ -23,7 +24,7 @@ class GravityForce : public Force {
     m_compute_acceleration_body = m_compute_acceleration_fn->body<TupleCallBody>();
   }
 
-  void add_force(ParticlesBlock &block, ArrayRef<float3> r_force) override;
+  void add_force(ForceInterface &interface) override;
 };
 
 class TurbulenceForce : public Force {
@@ -37,7 +38,7 @@ class TurbulenceForce : public Force {
     m_compute_strength_body = m_compute_strength_fn->body<TupleCallBody>();
   }
 
-  void add_force(ParticlesBlock &block, ArrayRef<float3> r_force) override;
+  void add_force(ForceInterface &interface) override;
 };
 
 }  // namespace BParticles
diff --git a/source/blender/simulations/bparticles/integrator.cpp b/source/blender/simulations/bparticles/integrator.cpp
index e67ae383c77..01a9652e1d9 100644
--- a/source/blender/simulations/bparticles/integrator.cpp
+++ b/source/blender/simulations/bparticles/integrator.cpp
@@ -53,7 +53,7 @@ void EulerIntegrator::integrate(IntegratorInterface &interface)
   ArrayRef<float> durations = interface.durations();
 
   ArrayAllocator::Array<float3> combined_force(interface.array_allocator());
-  this->compute_combined_force(block, combined_force);
+  this->compute_combined_force(block, interface.array_allocator(), combined_force);
 
   auto last_velocities = block.attributes().get_float3("Velocity");
 
@@ -64,12 +64,15 @@ void EulerIntegrator::integrate(IntegratorInterface &interface)
 }
 
 BLI_NOINLINE void EulerIntegrator::compute_combined_force(ParticlesBlock &block,
+                                                          ArrayAllocator &array_allocator,
                                                           ArrayRef<float3> r_force)
 {
   r_force.fill({0, 0, 0});
 
+  ForceInterface interface(block, array_allocator, r_force);
+
   for (Force *force : m_forces) {
-    force->add_force(block, r_force);
+    force->add_force(interface);
   }
 }
 
diff --git a/source/blender/simulations/bparticles/integrator.hpp b/source/blender/simulations/bparticles/integrator.hpp
index ea68689810b..fb9c36bbdca 100644
--- a/source/blender/simulations/bparticles/integrator.hpp
+++ b/source/blender/simulations/bparticles/integrator.hpp
@@ -28,7 +28,9 @@ class EulerIntegrator : public Integrator {
   void integrate(IntegratorInterface &interface) override;
 
  private:
-  void compute_combined_force(ParticlesBlock &block, ArrayRef<float3> r_force);
+  void compute_combined_force(ParticlesBlock &block,
+                              ArrayAllocator &array_allocator,
+                              ArrayRef<float3> r_force);
 
   void compute_offsets(ArrayRef<float> durations,
                        ArrayRef<float3> last_velocities,



More information about the Bf-blender-cvs mailing list