[Bf-blender-cvs] [dbc50aa1566] functions: initial deletion or particles and compression of blocks

Jacques Lucke noreply at git.blender.org
Fri Jun 7 23:28:20 CEST 2019


Commit: dbc50aa156669ebc3a33f4e9e451cf7920bcc94d
Author: Jacques Lucke
Date:   Fri Jun 7 22:43:29 2019 +0200
Branches: functions
https://developer.blender.org/rBdbc50aa156669ebc3a33f4e9e451cf7920bcc94d

initial deletion or particles and compression of blocks

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

M	source/blender/simulations/bparticles/particles_container.cpp
M	source/blender/simulations/bparticles/particles_container.hpp
M	source/blender/simulations/bparticles/playground_solver.cpp

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

diff --git a/source/blender/simulations/bparticles/particles_container.cpp b/source/blender/simulations/bparticles/particles_container.cpp
index 6f4ce5c37fa..3e59b4cfe0a 100644
--- a/source/blender/simulations/bparticles/particles_container.cpp
+++ b/source/blender/simulations/bparticles/particles_container.cpp
@@ -107,7 +107,7 @@ void ParticlesBlock::Compress(ArrayRef<ParticlesBlock *> blocks)
     return a->active_amount() < b->active_amount();
   });
 
-  uint last_non_full = blocks.size();
+  uint last_non_full = blocks.size() - 1;
 
   for (uint i = 0; i < blocks.size(); i++) {
     while (i < last_non_full) {
diff --git a/source/blender/simulations/bparticles/particles_container.hpp b/source/blender/simulations/bparticles/particles_container.hpp
index f68d727c8c8..be3ba7b3253 100644
--- a/source/blender/simulations/bparticles/particles_container.hpp
+++ b/source/blender/simulations/bparticles/particles_container.hpp
@@ -61,6 +61,7 @@ class ParticlesBlock {
   uint &active_amount();
   uint inactive_amount();
   bool is_full();
+  bool is_empty();
   uint next_inactive_index();
   uint size();
 
@@ -73,6 +74,8 @@ class ParticlesBlock {
   float *float_buffer(StringRef name);
   Vec3 *vec3_buffer(StringRef name);
 
+  void move(uint old_index, uint new_index);
+
   static void MoveUntilFull(ParticlesBlock *from, ParticlesBlock *to);
   static void Compress(ArrayRef<ParticlesBlock *> blocks);
 };
@@ -148,6 +151,11 @@ inline bool ParticlesBlock::is_full()
   return m_active_amount == this->size();
 }
 
+inline bool ParticlesBlock::is_empty()
+{
+  return m_active_amount == 0;
+}
+
 inline uint ParticlesBlock::next_inactive_index()
 {
   return m_active_amount;
@@ -190,4 +198,14 @@ inline Vec3 *ParticlesBlock::vec3_buffer(StringRef name)
   return m_vec3_buffers[index];
 }
 
+inline void ParticlesBlock::move(uint old_index, uint new_index)
+{
+  for (float *buffer : m_float_buffers) {
+    buffer[new_index] = buffer[old_index];
+  }
+  for (Vec3 *buffer : m_vec3_buffers) {
+    buffer[new_index] = buffer[old_index];
+  }
+}
+
 }  // namespace BParticles
diff --git a/source/blender/simulations/bparticles/playground_solver.cpp b/source/blender/simulations/bparticles/playground_solver.cpp
index 4611dc5a340..dba1776934e 100644
--- a/source/blender/simulations/bparticles/playground_solver.cpp
+++ b/source/blender/simulations/bparticles/playground_solver.cpp
@@ -32,7 +32,7 @@ class SimpleSolver : public Solver {
   StateBase *init() override
   {
     MyState *state = new MyState();
-    state->particles = new ParticlesContainer(50, {}, {"Position", "Velocity"});
+    state->particles = new ParticlesContainer(10, {"Age"}, {"Position", "Velocity"});
     return state;
   }
 
@@ -42,9 +42,11 @@ class SimpleSolver : public Solver {
 
     Vec3 *positions = block->vec3_buffer("Position");
     Vec3 *velocities = block->vec3_buffer("Velocity");
+    float *age = block->float_buffer("Age");
 
     for (uint i = 0; i < active_amount; i++) {
       positions[i] += velocities[i];
+      age[i] += 1;
     }
 
     SmallVector<Vec3> combined_force(active_amount);
@@ -60,6 +62,32 @@ class SimpleSolver : public Solver {
     for (uint i = 0; i < active_amount; i++) {
       velocities[i] += combined_force[i] * time_step;
     }
+
+    if (rand() % 10 == 0) {
+      for (uint i = 0; i < active_amount; i++) {
+        age[i] = rand() % 70;
+      }
+    }
+  }
+
+  void delete_old_particles(ParticlesBlock *block)
+  {
+    float *age = block->float_buffer("Age");
+
+    uint index = 0;
+    while (index < block->active_amount()) {
+      if (age[index] < 50) {
+        index++;
+        continue;
+      }
+      if (age[block->active_amount() - 1] > 50) {
+        block->active_amount() -= 1;
+        continue;
+      }
+      block->move(block->active_amount() - 1, index);
+      index++;
+      block->active_amount() -= 1;
+    }
   }
 
   void emit_new_particles(ParticlesContainer &particles)
@@ -79,9 +107,25 @@ class SimpleSolver : public Solver {
     uint index = non_full_block->next_inactive_index();
     non_full_block->vec3_buffer("Position")[index] = {(float)(rand() % 100) / 100.0f, 0, 1};
     non_full_block->vec3_buffer("Velocity")[index] = {0, 0.1, 0};
+    non_full_block->float_buffer("Age")[index] = 0;
     non_full_block->active_amount()++;
   }
 
+  void compress_all_blocks(ParticlesContainer &particles)
+  {
+    SmallVector<ParticlesBlock *> blocks;
+    for (auto block : particles.active_blocks()) {
+      blocks.append(block);
+    }
+    ParticlesBlock::Compress(blocks);
+
+    for (auto block : blocks) {
+      if (block->is_empty()) {
+        particles.release_block(block);
+      }
+    }
+  }
+
   void step(WrappedState &wrapped_state) override
   {
     MyState &state = wrapped_state.state<MyState>();
@@ -90,9 +134,14 @@ class SimpleSolver : public Solver {
 
     for (ParticlesBlock *block : particles.active_blocks()) {
       this->step_block(block);
+      this->delete_old_particles(block);
     }
 
     this->emit_new_particles(particles);
+    this->compress_all_blocks(particles);
+
+    std::cout << "Particle Amount: " << this->particle_amount(wrapped_state) << "\n";
+    std::cout << "Block amount: " << particles.active_blocks().size() << "\n";
   }
 
   uint particle_amount(WrappedState &wrapped_state) override



More information about the Bf-blender-cvs mailing list