[Bf-blender-cvs] [d0cbd393105] functions: initial force implementation

Jacques Lucke noreply at git.blender.org
Fri Jun 7 17:18:52 CEST 2019


Commit: d0cbd393105d21fbc6361f85933efd06ff37970a
Author: Jacques Lucke
Date:   Fri Jun 7 15:53:48 2019 +0200
Branches: functions
https://developer.blender.org/rBd0cbd393105d21fbc6361f85933efd06ff37970a

initial force implementation

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

M	source/blender/blenlib/CMakeLists.txt
M	source/blender/simulations/bparticles/c_wrapper.cpp
M	source/blender/simulations/bparticles/core.hpp
M	source/blender/simulations/bparticles/playground_solver.cpp
M	source/blender/simulations/bparticles/playground_solver.hpp

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

diff --git a/source/blender/blenlib/CMakeLists.txt b/source/blender/blenlib/CMakeLists.txt
index 8a36905f0c8..6ee3877d97a 100644
--- a/source/blender/blenlib/CMakeLists.txt
+++ b/source/blender/blenlib/CMakeLists.txt
@@ -242,6 +242,7 @@ set(SRC
   BLI_lazy_init.hpp
   intern/BLI_lazy_init.cpp
   BLI_listbase_wrapper.hpp
+  BLI_math.hpp
   BLI_mempool.hpp
   BLI_multimap.hpp
   BLI_multipool.hpp
diff --git a/source/blender/simulations/bparticles/c_wrapper.cpp b/source/blender/simulations/bparticles/c_wrapper.cpp
index 0a567e200b5..039ca9e3c44 100644
--- a/source/blender/simulations/bparticles/c_wrapper.cpp
+++ b/source/blender/simulations/bparticles/c_wrapper.cpp
@@ -17,14 +17,34 @@ using BParticles::Solver;
 using BParticles::StateBase;
 using BParticles::WrappedState;
 
+using BLI::ArrayRef;
+using BLI::Vec3;
+
 WRAPPERS(BParticles::Description *, BParticlesDescription);
 WRAPPERS(BParticles::Solver *, BParticlesSolver);
 WRAPPERS(BParticles::WrappedState *, BParticlesState);
 
-BParticlesDescription BParticles_playground_description(float control1, float UNUSED(control2))
+class TestForce : public BParticles::Force {
+ private:
+  float m_value_1, m_value_2;
+
+ public:
+  TestForce(float value_1, float value_2) : m_value_1(value_1), m_value_2(value_2)
+  {
+  }
+
+  void add_force(ArrayRef<Vec3> dst) override
+  {
+    for (uint i = 0; i < dst.size(); i++) {
+      dst[i].x += m_value_1;
+      dst[i].z += m_value_2;
+    }
+  };
+};
+
+BParticlesDescription BParticles_playground_description(float control1, float control2)
 {
-  Description *description = new Description();
-  description->m_gravity = control1;
+  Description *description = new Description({new TestForce(control1, control2)});
   return wrap(description);
 }
 void BParticles_description_free(BParticlesDescription description_c)
@@ -35,7 +55,7 @@ void BParticles_description_free(BParticlesDescription description_c)
 BParticlesSolver BParticles_solver_build(BParticlesDescription description_c)
 {
   Description *description = unwrap(description_c);
-  return wrap(BParticles::new_playground_solver(description));
+  return wrap(BParticles::new_playground_solver(*description));
 }
 void BParticles_solver_free(BParticlesSolver solver_c)
 {
diff --git a/source/blender/simulations/bparticles/core.hpp b/source/blender/simulations/bparticles/core.hpp
index 87f9fe854ee..d1c66252257 100644
--- a/source/blender/simulations/bparticles/core.hpp
+++ b/source/blender/simulations/bparticles/core.hpp
@@ -2,6 +2,8 @@
 
 #include <memory>
 
+#include "BLI_array_ref.hpp"
+#include "BLI_math.hpp"
 #include "BLI_utildefines.h"
 
 namespace BParticles {
@@ -10,11 +12,31 @@ class Solver;
 class WrappedState;
 class StateBase;
 
+using BLI::ArrayRef;
+using BLI::SmallVector;
+using BLI::Vec3;
+using std::unique_ptr;
+
+class Force {
+ public:
+  virtual void add_force(ArrayRef<Vec3> dst) = 0;
+};
+
 class Description {
+ private:
+  SmallVector<Force *> m_forces;
+
  public:
-  virtual ~Description();
+  Description(ArrayRef<Force *> forces) : m_forces(forces.to_small_vector())
+  {
+  }
+
+  ArrayRef<Force *> forces()
+  {
+    return m_forces;
+  }
 
-  float m_gravity = 0.0f;
+  virtual ~Description();
 };
 
 class Solver {
diff --git a/source/blender/simulations/bparticles/playground_solver.cpp b/source/blender/simulations/bparticles/playground_solver.cpp
index d9432d22113..774ba86b148 100644
--- a/source/blender/simulations/bparticles/playground_solver.cpp
+++ b/source/blender/simulations/bparticles/playground_solver.cpp
@@ -13,14 +13,14 @@ struct Vector {
 class SimpleSolver : public Solver {
 
   struct MyState : StateBase {
-    SmallVector<Vector> positions;
-    SmallVector<Vector> velocities;
+    SmallVector<Vec3> positions;
+    SmallVector<Vec3> velocities;
   };
 
-  Description *m_description;
+  Description &m_description;
 
  public:
-  SimpleSolver(Description *description) : m_description(description)
+  SimpleSolver(Description &description) : m_description(description)
   {
   }
 
@@ -32,17 +32,30 @@ class SimpleSolver : public Solver {
   void step(WrappedState &wrapped_state) override
   {
     MyState &state = wrapped_state.state<MyState>();
+    uint last_particle_amount = state.positions.size();
 
-    for (uint i = 0; i < state.positions.size(); i++) {
-      Vector &position = state.positions[i];
-      Vector &velocity = state.velocities[i];
+    for (uint i = 0; i < last_particle_amount; i++) {
+      Vec3 &position = state.positions[i];
+      Vec3 &velocity = state.velocities[i];
       position.x += velocity.x;
       position.y += velocity.y;
       position.z += velocity.z;
     }
 
-    for (Vector &velocity : state.velocities) {
-      velocity.z -= m_description->m_gravity / 100.0f;
+    SmallVector<Vec3> combined_force(last_particle_amount);
+    combined_force.fill({0, 0, 0});
+
+    for (Force *force : m_description.forces()) {
+      force->add_force(combined_force);
+    }
+
+    float time_step = 0.01f;
+    for (uint i = 0; i < last_particle_amount; i++) {
+      state.velocities[i] += combined_force[i] * time_step;
+    }
+
+    for (uint i = 0; i < last_particle_amount; i++) {
+      state.positions[i] += state.velocities[i] * time_step;
     }
 
     state.positions.append({(float)(rand() % 100) / 100.0f, 0, 1});
@@ -62,7 +75,7 @@ class SimpleSolver : public Solver {
   }
 };
 
-Solver *new_playground_solver(Description *description)
+Solver *new_playground_solver(Description &description)
 {
   return new SimpleSolver(description);
 }
diff --git a/source/blender/simulations/bparticles/playground_solver.hpp b/source/blender/simulations/bparticles/playground_solver.hpp
index c0495054cba..5b97b69ff34 100644
--- a/source/blender/simulations/bparticles/playground_solver.hpp
+++ b/source/blender/simulations/bparticles/playground_solver.hpp
@@ -3,5 +3,5 @@
 #include "core.hpp"
 
 namespace BParticles {
-Solver *new_playground_solver(Description *description);
+Solver *new_playground_solver(Description &description);
 }



More information about the Bf-blender-cvs mailing list