[Bf-blender-cvs] [3d797d0070d] functions: splitup bparticles code into multiple files

Jacques Lucke noreply at git.blender.org
Fri Jun 7 12:34:37 CEST 2019


Commit: 3d797d0070d1b0b64cf5d9dd9e6fec2728c51579
Author: Jacques Lucke
Date:   Fri Jun 7 12:17:22 2019 +0200
Branches: functions
https://developer.blender.org/rB3d797d0070d1b0b64cf5d9dd9e6fec2728c51579

splitup bparticles code into multiple files

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

M	source/blender/modifiers/intern/MOD_nodeparticles.c
M	source/blender/simulations/BParticles.h
M	source/blender/simulations/CMakeLists.txt
A	source/blender/simulations/bparticles/c_wrapper.cpp
A	source/blender/simulations/bparticles/core.cpp
A	source/blender/simulations/bparticles/core.hpp
A	source/blender/simulations/bparticles/playground_solver.cpp
A	source/blender/simulations/bparticles/playground_solver.hpp
D	source/blender/simulations/playground.cpp

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

diff --git a/source/blender/modifiers/intern/MOD_nodeparticles.c b/source/blender/modifiers/intern/MOD_nodeparticles.c
index fc9e2c02a64..27ff9ce2182 100644
--- a/source/blender/modifiers/intern/MOD_nodeparticles.c
+++ b/source/blender/modifiers/intern/MOD_nodeparticles.c
@@ -107,7 +107,7 @@ static Mesh *applyModifier(ModifierData *md,
   BParticlesSolver new_solver = BParticles_solver_build(new_description);
 
   if (current_frame == runtime->last_simulated_frame + 1) {
-    BParticles_state_adapt(new_solver, &runtime->state);
+    BParticles_state_adapt(new_solver, runtime->state);
 
     BParticles_solver_free(runtime->solver);
     BParticles_description_free(runtime->description);
diff --git a/source/blender/simulations/BParticles.h b/source/blender/simulations/BParticles.h
index c4a893e627d..43a75da55f3 100644
--- a/source/blender/simulations/BParticles.h
+++ b/source/blender/simulations/BParticles.h
@@ -19,7 +19,7 @@ BParticlesSolver BParticles_solver_build(BParticlesDescription description);
 void BParticles_solver_free(BParticlesSolver solver);
 
 BParticlesState BParticles_state_init(BParticlesSolver solver);
-void BParticles_state_adapt(BParticlesSolver new_solver, BParticlesState *state_to_adapt);
+void BParticles_state_adapt(BParticlesSolver new_solver, BParticlesState state_to_adapt);
 void BParticles_state_step(BParticlesSolver solver, BParticlesState state);
 void BParticles_state_free(BParticlesState state);
 
diff --git a/source/blender/simulations/CMakeLists.txt b/source/blender/simulations/CMakeLists.txt
index 78fbfcf9cd3..f4c9c9c680c 100644
--- a/source/blender/simulations/CMakeLists.txt
+++ b/source/blender/simulations/CMakeLists.txt
@@ -13,7 +13,13 @@ set(INC_SYS
 
 
 set(SRC
-   playground.cpp
+  BParticles.h
+
+  bparticles/core.hpp
+  bparticles/core.cpp
+  bparticles/playground_solver.hpp
+  bparticles/playground_solver.cpp
+  bparticles/c_wrapper.cpp
 )
 
 set(LIB
diff --git a/source/blender/simulations/bparticles/c_wrapper.cpp b/source/blender/simulations/bparticles/c_wrapper.cpp
new file mode 100644
index 00000000000..e66d6079ce5
--- /dev/null
+++ b/source/blender/simulations/bparticles/c_wrapper.cpp
@@ -0,0 +1,79 @@
+#include "BParticles.h"
+#include "core.hpp"
+#include "playground_solver.hpp"
+
+#define WRAPPERS(T1, T2) \
+  inline T1 unwrap(T2 value) \
+  { \
+    return (T1)value; \
+  } \
+  inline T2 wrap(T1 value) \
+  { \
+    return (T2)value; \
+  }
+
+using BParticles::Description;
+using BParticles::Solver;
+using BParticles::StateBase;
+using BParticles::WrappedState;
+
+WRAPPERS(BParticles::Description *, BParticlesDescription);
+WRAPPERS(BParticles::Solver *, BParticlesSolver);
+WRAPPERS(BParticles::WrappedState *, BParticlesState);
+
+BParticlesDescription BParticles_playground_description()
+{
+  return wrap(new Description());
+}
+void BParticles_description_free(BParticlesDescription description_c)
+{
+  delete unwrap(description_c);
+}
+
+BParticlesSolver BParticles_solver_build(BParticlesDescription UNUSED(description_c))
+{
+  return wrap(BParticles::new_playground_solver());
+}
+void BParticles_solver_free(BParticlesSolver solver_c)
+{
+  delete unwrap(solver_c);
+}
+
+BParticlesState BParticles_state_init(BParticlesSolver solver_c)
+{
+  Solver *solver = unwrap(solver_c);
+  return wrap(solver->init());
+}
+void BParticles_state_adapt(BParticlesSolver new_solver_c, BParticlesState state_to_adapt_c)
+{
+  Solver *new_solver = unwrap(new_solver_c);
+  WrappedState *wrapped_state = unwrap(state_to_adapt_c);
+  BParticles::adapt_state(new_solver, wrapped_state);
+}
+void BParticles_state_step(BParticlesSolver solver_c, BParticlesState state_c)
+{
+  Solver *solver = unwrap(solver_c);
+  WrappedState *wrapped_state = unwrap(state_c);
+
+  BLI_assert(solver == &wrapped_state->solver());
+  solver->step(*wrapped_state);
+}
+void BParticles_state_free(BParticlesState state_c)
+{
+  delete unwrap(state_c);
+}
+
+uint BParticles_state_particle_count(BParticlesSolver solver_c, BParticlesState state_c)
+{
+  Solver *solver = unwrap(solver_c);
+  WrappedState *wrapped_state = unwrap(state_c);
+  return solver->particle_amount(*wrapped_state);
+}
+void BParticles_state_get_positions(BParticlesSolver solver_c,
+                                    BParticlesState state_c,
+                                    float (*dst)[3])
+{
+  Solver *solver = unwrap(solver_c);
+  WrappedState *wrapped_state = unwrap(state_c);
+  solver->get_positions(*wrapped_state, dst);
+}
diff --git a/source/blender/simulations/bparticles/core.cpp b/source/blender/simulations/bparticles/core.cpp
new file mode 100644
index 00000000000..8a61345ec08
--- /dev/null
+++ b/source/blender/simulations/bparticles/core.cpp
@@ -0,0 +1,22 @@
+#include "core.hpp"
+
+namespace BParticles {
+
+Description::~Description()
+{
+}
+
+Solver::~Solver()
+{
+}
+
+StateBase::~StateBase()
+{
+}
+
+void adapt_state(Solver *new_solver, WrappedState *wrapped_state)
+{
+  wrapped_state->m_solver = new_solver;
+}
+
+}  // namespace BParticles
diff --git a/source/blender/simulations/bparticles/core.hpp b/source/blender/simulations/bparticles/core.hpp
new file mode 100644
index 00000000000..7677d3bcc80
--- /dev/null
+++ b/source/blender/simulations/bparticles/core.hpp
@@ -0,0 +1,70 @@
+#pragma once
+
+#include <memory>
+
+#include "BLI_utildefines.h"
+
+namespace BParticles {
+class Description;
+class Solver;
+class WrappedState;
+class StateBase;
+
+class Description {
+ public:
+  virtual ~Description();
+};
+
+class Solver {
+ public:
+  virtual ~Solver();
+
+  virtual WrappedState *init() = 0;
+  virtual void step(WrappedState &wrapped_state) = 0;
+
+  virtual uint particle_amount(WrappedState &wrapped_state) = 0;
+  virtual void get_positions(WrappedState &wrapped_state, float (*dst)[3]) = 0;
+};
+
+class StateBase {
+ public:
+  virtual ~StateBase();
+};
+
+class WrappedState final {
+ private:
+  Solver *m_solver;
+  std::unique_ptr<StateBase> m_state;
+
+ public:
+  WrappedState(Solver *solver, std::unique_ptr<StateBase> state)
+      : m_solver(solver), m_state(std::move(state))
+  {
+    BLI_assert(solver);
+    BLI_assert(m_state.get() != NULL);
+  }
+
+  WrappedState(WrappedState &other) = delete;
+  WrappedState(WrappedState &&other) = delete;
+  WrappedState &operator=(WrappedState &other) = delete;
+  WrappedState &operator=(WrappedState &&other) = delete;
+
+  Solver &solver() const
+  {
+    BLI_assert(m_solver);
+    return *m_solver;
+  }
+
+  template<typename T> T &state() const
+  {
+    T *state = dynamic_cast<T *>(m_state.get());
+    BLI_assert(state);
+    return *state;
+  }
+
+  friend void adapt_state(Solver *, WrappedState *);
+};
+
+void adapt_state(Solver *new_solver, WrappedState *wrapped_state);
+
+}  // namespace BParticles
diff --git a/source/blender/simulations/bparticles/playground_solver.cpp b/source/blender/simulations/bparticles/playground_solver.cpp
new file mode 100644
index 00000000000..1ed0d92deb6
--- /dev/null
+++ b/source/blender/simulations/bparticles/playground_solver.cpp
@@ -0,0 +1,57 @@
+#include "BLI_small_vector.hpp"
+
+#include "playground_solver.hpp"
+
+namespace BParticles {
+
+using BLI::SmallVector;
+
+struct Vector {
+  float x, y, z;
+};
+
+class SimpleSolver : public Solver {
+
+  struct MyState : StateBase {
+    SmallVector<Vector> positions;
+  };
+
+ public:
+  SimpleSolver()
+  {
+  }
+
+  WrappedState *init() override
+  {
+    MyState *state = new MyState();
+    return new WrappedState(this, std::unique_ptr<MyState>(state));
+  }
+
+  void step(WrappedState &wrapped_state) override
+  {
+    MyState &state = wrapped_state.state<MyState>();
+    for (Vector &position : state.positions) {
+      position.x += 0.1f;
+    }
+    state.positions.append({0, 0, 1});
+  }
+
+  uint particle_amount(WrappedState &wrapped_state) override
+  {
+    MyState &state = wrapped_state.state<MyState>();
+    return state.positions.size();
+  }
+
+  void get_positions(WrappedState &wrapped_state, float (*dst)[3]) override
+  {
+    MyState &state = wrapped_state.state<MyState>();
+    memcpy(dst, state.positions.begin(), state.positions.size() * sizeof(Vector));
+  }
+};
+
+Solver *new_playground_solver()
+{
+  return new SimpleSolver();
+}
+
+}  // namespace BParticles
diff --git a/source/blender/simulations/bparticles/playground_solver.hpp b/source/blender/simulations/bparticles/playground_solver.hpp
new file mode 100644
index 00000000000..21575ba3136
--- /dev/null
+++ b/source/blender/simulations/bparticles/playground_solver.hpp
@@ -0,0 +1,7 @@
+#pragma once
+
+#include "core.hpp"
+
+namespace BParticles {
+Solver *new_playground_solver();
+}
diff --git a/source/blender/simulations/playground.cpp b/source/blender/simulations/playground.cpp
deleted file mode 100644
index 0d674e3e2a7..00000000000
--- a/source/blender/simulations/playground.cpp
+++ /dev/null
@@ -1,202 +0,0 @@
-#include "BLI_math.h"
-#include "BLI_small_vector.hpp"
-
-#include "BParticles.h"
-
-using BLI::SmallVector;
-
-#define WRAPPERS(T1, T2) \
-  inline T1 unwrap(T2 value) \
-  { \
-    return (T1)value; \
-  } \
-  inline T2 wrap(T1 value) \
-  { \
-    return (T2)value; \
-  }
-
-namespace BParticles {
-
-class Description;
-class Solver;
-class WrappedState;
-class StateBase;
-
-class Description {
- public:
-  virtual ~Description();
-};
-
-class Solver {
- public:
-  virtual ~Solver();
-
-  virtual WrappedState *init() = 0;
-  virtual void step(WrappedState &wrapped_state) = 0;
-
-  virtual uint particle_amount(WrappedState &wrapped_state) = 0;
-  virtual void get_positions(WrappedState &wrapped_state, float (*dst)[3]) = 0;
-};
-
-class StateBase {
- public:
-  virtual ~StateBase();
-};
-
-class WrappedState final {
- private:
-  Solver *m_solver;
-  std::unique_ptr<StateBase> m_state;
-
- public:
-  WrappedState(Solver *solver, std::unique_ptr<StateBase> state)
-      : m_solver(solver), m_state(std::move(state))
-  {
-    BLI_assert(solver);
-    BLI_assert(m_state.get() != NULL);
-  }
-
-  WrappedState(WrappedState &other) = delete;
-  WrappedState(WrappedState &&other) = delete;
-  WrappedState &operator=(WrappedState &other) = delete;
-  WrappedState &operator=(WrappedState &&other) = delete;
-
-  Solver &solver() const
-  {
-    BLI_

@@ Diff output truncated at 10240 characters. @@



More information about the Bf-blender-cvs mailing list