[Bf-blender-cvs] [cf81da25475] functions: refactor towards supporting multiple particle types

Jacques Lucke noreply at git.blender.org
Fri Jun 21 16:56:34 CEST 2019


Commit: cf81da2547543506d3de31a0d148dd349f469107
Author: Jacques Lucke
Date:   Fri Jun 21 16:03:30 2019 +0200
Branches: functions
https://developer.blender.org/rBcf81da2547543506d3de31a0d148dd349f469107

refactor towards supporting multiple particle types

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

M	source/blender/simulations/bparticles/c_wrapper.cpp
M	source/blender/simulations/bparticles/core.cpp
M	source/blender/simulations/bparticles/core.hpp
M	source/blender/simulations/bparticles/particles_container.hpp
M	source/blender/simulations/bparticles/simulate.cpp

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

diff --git a/source/blender/simulations/bparticles/c_wrapper.cpp b/source/blender/simulations/bparticles/c_wrapper.cpp
index a7258526bf4..5283393cb58 100644
--- a/source/blender/simulations/bparticles/c_wrapper.cpp
+++ b/source/blender/simulations/bparticles/c_wrapper.cpp
@@ -44,8 +44,8 @@ BParticlesState BParticles_new_empty_state()
   ParticlesState *state = new ParticlesState();
 
   AttributesInfo info{{"Kill State"}, {"Birth Time"}, {"Position", "Velocity"}};
-  ParticlesContainer *container = new ParticlesContainer(info, 1000);
-  state->m_container = container;
+  auto &containers = state->particle_containers();
+  containers.add_new(0, new ParticlesContainer(info, 1000));
 
   return wrap(state);
 }
@@ -114,16 +114,28 @@ class ModifierParticleType : public ParticleType {
 class ModifierStepDescription : public StepDescription {
  public:
   float m_duration;
-  ModifierParticleType m_type;
+  SmallMap<uint, ModifierParticleType *> m_types;
+
+  ~ModifierStepDescription()
+  {
+    for (auto *type : m_types.values()) {
+      delete type;
+    }
+  }
 
   float step_duration() override
   {
     return m_duration;
   }
 
-  ParticleType &particle_type() override
+  ArrayRef<uint> particle_type_ids() override
+  {
+    return {0};
+  }
+
+  ParticleType &particle_type(uint type_id) override
   {
-    return m_type;
+    return *m_types.lookup(type_id);
   }
 };
 
@@ -136,31 +148,39 @@ void BParticles_simulate_modifier(NodeParticlesModifierData *npmd,
   ParticlesState &state = *unwrap(state_c);
   ModifierStepDescription description;
   description.m_duration = 1.0f / 24.0f;
+
+  auto *type = new ModifierParticleType();
+  description.m_types.add_new(0, type);
+
   if (npmd->emitter_object) {
-    description.m_type.m_emitters.append(EMITTER_mesh_surface((Mesh *)npmd->emitter_object->data,
-                                                              npmd->emitter_object->obmat,
-                                                              npmd->control1)
-                                             .release());
+    type->m_emitters.append(EMITTER_mesh_surface((Mesh *)npmd->emitter_object->data,
+                                                 npmd->emitter_object->obmat,
+                                                 npmd->control1)
+                                .release());
   }
   BVHTreeFromMesh treedata = {0};
   if (npmd->collision_object) {
     BKE_bvhtree_from_mesh_get(
         &treedata, (Mesh *)npmd->collision_object->data, BVHTREE_FROM_LOOPTRI, 4);
-    description.m_type.m_influences.m_events.append(
+    type->m_influences.m_events.append(
         EVENT_mesh_collection(&treedata, npmd->collision_object->obmat).release());
-    description.m_type.m_influences.m_actions.append(ACTION_kill().release());
+    type->m_influences.m_actions.append(ACTION_kill().release());
   }
-  description.m_type.m_influences.m_forces.append(FORCE_directional({0, 0, -2}).release());
-  description.m_type.m_influences.m_events.append(EVENT_age_reached(3.0f).release());
-  description.m_type.m_influences.m_actions.append(ACTION_move({0, 1, 0}).release());
+  type->m_influences.m_forces.append(FORCE_directional({0, 0, -2}).release());
+  type->m_influences.m_events.append(EVENT_age_reached(3.0f).release());
+  type->m_influences.m_actions.append(ACTION_move({0, 1, 0}).release());
   simulate_step(state, description);
 
   if (npmd->collision_object) {
     free_bvhtree_from_mesh(&treedata);
   }
 
-  std::cout << "Active Blocks: " << state.m_container->active_blocks().size() << "\n";
-  std::cout << " Particle Amount: " << BParticles_state_particle_count(state_c) << "\n";
+  auto &containers = state.particle_containers();
+  for (auto item : containers.items()) {
+    std::cout << "Particle Type: " << item.key << "\n";
+    std::cout << "  Particles: " << item.value->count_active() << "\n";
+    std::cout << "  Blocks: " << item.value->active_blocks().size() << "\n";
+  }
 }
 
 uint BParticles_state_particle_count(BParticlesState state_c)
@@ -168,10 +188,9 @@ uint BParticles_state_particle_count(BParticlesState state_c)
   ParticlesState &state = *unwrap(state_c);
 
   uint count = 0;
-  for (auto *block : state.m_container->active_blocks()) {
-    count += block->active_amount();
+  for (ParticlesContainer *container : state.particle_containers().values()) {
+    count += container->count_active();
   }
-
   return count;
 }
 
@@ -180,9 +199,11 @@ void BParticles_state_get_positions(BParticlesState state_c, float (*dst)[3])
   ParticlesState &state = *unwrap(state_c);
 
   uint index = 0;
-  for (auto *block : state.m_container->active_blocks()) {
-    auto positions = block->slice_active().get_float3("Position");
-    memcpy(dst + index, positions.begin(), sizeof(float3) * positions.size());
-    index += positions.size();
+  for (ParticlesContainer *container : state.particle_containers().values()) {
+    for (auto *block : container->active_blocks()) {
+      auto positions = block->slice_active().get_float3("Position");
+      memcpy(dst + index, positions.begin(), sizeof(float3) * positions.size());
+      index += positions.size();
+    }
   }
 }
diff --git a/source/blender/simulations/bparticles/core.cpp b/source/blender/simulations/bparticles/core.cpp
index d1658691e11..b6d46be5950 100644
--- a/source/blender/simulations/bparticles/core.cpp
+++ b/source/blender/simulations/bparticles/core.cpp
@@ -32,7 +32,6 @@ StepDescription::~StepDescription()
 
 ParticlesState::~ParticlesState()
 {
-  delete m_container;
 }
 
 }  // namespace BParticles
diff --git a/source/blender/simulations/bparticles/core.hpp b/source/blender/simulations/bparticles/core.hpp
index 187fe4bf110..6a023b2aad7 100644
--- a/source/blender/simulations/bparticles/core.hpp
+++ b/source/blender/simulations/bparticles/core.hpp
@@ -196,16 +196,25 @@ class StepDescription {
   virtual ~StepDescription();
 
   virtual float step_duration() = 0;
-  virtual ParticleType &particle_type() = 0;
+
+  virtual ArrayRef<uint> particle_type_ids() = 0;
+  virtual ParticleType &particle_type(uint type_id) = 0;
 };
 
 class ParticlesState {
+ private:
+  SmallMap<uint, ParticlesContainer *> m_particle_containers;
+
  public:
-  ParticlesContainer *m_container;
   float m_current_time = 0.0f;
 
   ParticlesState() = default;
   ~ParticlesState();
+
+  SmallMap<uint, ParticlesContainer *> &particle_containers()
+  {
+    return m_particle_containers;
+  }
 };
 
 }  // namespace BParticles
diff --git a/source/blender/simulations/bparticles/particles_container.hpp b/source/blender/simulations/bparticles/particles_container.hpp
index 6870ad3fc75..43ce91d5335 100644
--- a/source/blender/simulations/bparticles/particles_container.hpp
+++ b/source/blender/simulations/bparticles/particles_container.hpp
@@ -34,6 +34,7 @@ class ParticlesContainer {
   ~ParticlesContainer();
 
   uint block_size() const;
+  uint count_active() const;
 
   AttributesInfo &attributes();
 
@@ -81,6 +82,15 @@ inline uint ParticlesContainer::block_size() const
   return m_block_size;
 }
 
+inline uint ParticlesContainer::count_active() const
+{
+  uint count = 0;
+  for (ParticlesBlock *block : m_blocks) {
+    count += block->active_amount();
+  }
+  return count;
+}
+
 inline AttributesInfo &ParticlesContainer::attributes()
 {
   return m_attributes;
diff --git a/source/blender/simulations/bparticles/simulate.cpp b/source/blender/simulations/bparticles/simulate.cpp
index 2c58dc024c9..210129845ca 100644
--- a/source/blender/simulations/bparticles/simulate.cpp
+++ b/source/blender/simulations/bparticles/simulate.cpp
@@ -437,8 +437,8 @@ void simulate_step(ParticlesState &state, StepDescription &description)
   TimeSpan time_span{state.m_current_time, description.step_duration()};
   state.m_current_time = time_span.end();
 
-  ParticlesContainer &particles = *state.m_container;
-  ParticleType &type = description.particle_type();
+  ParticlesContainer &particles = *state.particle_containers().lookup(0);
+  ParticleType &type = description.particle_type(0);
 
   step_individual_particles(
       particles.active_blocks().to_small_vector(), time_span, type.influences());



More information about the Bf-blender-cvs mailing list