[Bf-blender-cvs] [d191a1cee7f] functions: start simplifying the emitter interface

Jacques Lucke noreply at git.blender.org
Wed Jul 10 17:17:38 CEST 2019


Commit: d191a1cee7f199310b4c508f8b746845092390a1
Author: Jacques Lucke
Date:   Wed Jul 10 12:27:18 2019 +0200
Branches: functions
https://developer.blender.org/rBd191a1cee7f199310b4c508f8b746845092390a1

start simplifying the emitter interface

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

M	source/blender/simulations/bparticles/core.cpp
M	source/blender/simulations/bparticles/core.hpp
M	source/blender/simulations/bparticles/emitters.cpp

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

diff --git a/source/blender/simulations/bparticles/core.cpp b/source/blender/simulations/bparticles/core.cpp
index 7b539d645c6..b129063af95 100644
--- a/source/blender/simulations/bparticles/core.cpp
+++ b/source/blender/simulations/bparticles/core.cpp
@@ -92,31 +92,11 @@ AttributesInfo &BlockAllocator::attributes_info(StringRef particle_type_name)
 /* Emitter Interface
  ******************************************/
 
-EmitterInterface::EmitterInterface(BlockAllocator &allocator, TimeSpan time_span)
-    : m_block_allocator(allocator), m_time_span(time_span)
+EmitterInterface::EmitterInterface(BlockAllocator &block_allocator, TimeSpan time_span)
+    : m_emit_manager(block_allocator), m_time_span(time_span)
 {
 }
 
-EmitterInterface::~EmitterInterface()
-{
-  for (TimeSpanEmitTarget *target : m_targets) {
-    delete target;
-  }
-}
-
-TimeSpanEmitTarget &EmitterInterface::request(StringRef particle_type_name, uint size)
-{
-  SmallVector<ParticlesBlock *> blocks;
-  SmallVector<Range<uint>> ranges;
-  m_block_allocator.allocate_block_ranges(particle_type_name, size, blocks, ranges);
-  AttributesInfo &attributes_info = m_block_allocator.attributes_info(particle_type_name);
-
-  auto *target = new TimeSpanEmitTarget(
-      particle_type_name, attributes_info, blocks, ranges, m_time_span);
-  m_targets.append(target);
-  return *target;
-}
-
 /* Action Interface
  **************************************/
 
@@ -175,15 +155,6 @@ InstantEmitTarget::InstantEmitTarget(StringRef particle_type_name,
 {
 }
 
-TimeSpanEmitTarget::TimeSpanEmitTarget(StringRef particle_type_name,
-                                       AttributesInfo &attributes_info,
-                                       ArrayRef<ParticlesBlock *> blocks,
-                                       ArrayRef<Range<uint>> ranges,
-                                       TimeSpan time_span)
-    : EmitTargetBase(particle_type_name, attributes_info, blocks, ranges), m_time_span(time_span)
-{
-}
-
 void EmitTargetBase::set_elements(uint index, void *data)
 {
   AttributeType type = m_attributes_info.type_of(index);
@@ -288,30 +259,19 @@ void EmitTargetBase::fill_float3(StringRef name, float3 value)
   this->fill_float3(index, value);
 }
 
-void TimeSpanEmitTarget::set_birth_moment(float time_factor)
-{
-  BLI_assert(time_factor >= 0.0 && time_factor <= 1.0f);
-  this->fill_float("Birth Time", m_time_span.interpolate(time_factor));
-}
+/* EmitManager
+ *****************************************/
 
-void TimeSpanEmitTarget::set_randomized_birth_moments()
+std::unique_ptr<EmitTargetBase> EmitManager::request(StringRef particle_type_name, uint size)
 {
-  SmallVector<float> birth_times(m_size);
-  for (uint i = 0; i < m_size; i++) {
-    float factor = (rand() % 10000) / 10000.0f;
-    birth_times[i] = m_time_span.interpolate(factor);
-  }
-  this->set_float("Birth Time", birth_times);
-}
+  SmallVector<ParticlesBlock *> blocks;
+  SmallVector<Range<uint>> ranges;
+  m_block_allocator.allocate_block_ranges(particle_type_name, size, blocks, ranges);
 
-void TimeSpanEmitTarget::set_birth_moments(ArrayRef<float> time_factors)
-{
-  BLI_assert(time_factors.size() == m_size);
-  SmallVector<float> birth_times(time_factors.size());
-  for (uint i = 0; i < m_size; i++) {
-    birth_times[i] = m_time_span.interpolate(time_factors[i]);
-  }
-  this->set_float("Birth Time", birth_times);
+  AttributesInfo &attributes_info = m_block_allocator.attributes_info(particle_type_name);
+
+  EmitTargetBase *target = new EmitTargetBase(particle_type_name, attributes_info, blocks, ranges);
+  return std::unique_ptr<EmitTargetBase>(target);
 }
 
 /* EventFilterInterface
diff --git a/source/blender/simulations/bparticles/core.hpp b/source/blender/simulations/bparticles/core.hpp
index 666b0cc61df..d610b150cf0 100644
--- a/source/blender/simulations/bparticles/core.hpp
+++ b/source/blender/simulations/bparticles/core.hpp
@@ -330,34 +330,18 @@ class InstantEmitTarget : public EmitTargetBase {
                     ArrayRef<Range<uint>> ranges);
 };
 
-/**
- * A specialized emit target for the case when the emitter can create particles within a time span.
- */
-class TimeSpanEmitTarget : public EmitTargetBase {
+class EmitManager {
  private:
-  TimeSpan m_time_span;
+  BlockAllocator &m_block_allocator;
 
  public:
-  TimeSpanEmitTarget(StringRef particle_type_name,
-                     AttributesInfo &attributes_info,
-                     ArrayRef<ParticlesBlock *> blocks,
-                     ArrayRef<Range<uint>> ranges,
-                     TimeSpan time_span);
-
-  /**
-   * Set a factor [0, 1] that determines when in the time span all particles are born.
-   */
-  void set_birth_moment(float time_factor);
-
-  /**
-   * Randomize the birth times within a time span.
-   */
-  void set_randomized_birth_moments();
+  EmitManager(BlockAllocator &block_allocator) : m_block_allocator(block_allocator)
+  {
+  }
+  EmitManager(EmitManager &other) = delete;
+  EmitManager(EmitManager &&other) = delete;
 
-  /**
-   * Set custom bith moments. All values should be in [0, 1].
-   */
-  void set_birth_moments(ArrayRef<float> time_factors);
+  std::unique_ptr<EmitTargetBase> request(StringRef particle_type_name, uint size);
 };
 
 /**
@@ -365,23 +349,14 @@ class TimeSpanEmitTarget : public EmitTargetBase {
  */
 class EmitterInterface {
  private:
-  BlockAllocator &m_block_allocator;
-  SmallVector<TimeSpanEmitTarget *> m_targets;
+  EmitManager m_emit_manager;
   TimeSpan m_time_span;
 
  public:
-  EmitterInterface(BlockAllocator &allocator, TimeSpan time_span);
-  ~EmitterInterface();
+  EmitterInterface(BlockAllocator &block_allocator, TimeSpan time_span);
+  ~EmitterInterface() = default;
 
-  /**
-   * Access emit targets created by the emitter.
-   */
-  ArrayRef<TimeSpanEmitTarget *> targets();
-
-  /**
-   * Get a new emit target with the given size and particle type.
-   */
-  TimeSpanEmitTarget &request(StringRef particle_type_name, uint size);
+  EmitManager &emit_manager();
 
   /**
    * Time span that new particles should be emitted in.
@@ -761,9 +736,9 @@ inline StringRefNull EmitTargetBase::particle_type_name()
 /* EmitterInterface inline functions
  ***********************************************/
 
-inline ArrayRef<TimeSpanEmitTarget *> EmitterInterface::targets()
+inline EmitManager &EmitterInterface::emit_manager()
 {
-  return m_targets;
+  return m_emit_manager;
 }
 
 inline TimeSpan EmitterInterface::time_span()
diff --git a/source/blender/simulations/bparticles/emitters.cpp b/source/blender/simulations/bparticles/emitters.cpp
index c4b7fd1e0ac..66ed29755c8 100644
--- a/source/blender/simulations/bparticles/emitters.cpp
+++ b/source/blender/simulations/bparticles/emitters.cpp
@@ -30,10 +30,10 @@ class PointEmitter : public Emitter {
 
   void emit(EmitterInterface &interface) override
   {
-    auto &target = interface.request(m_particle_type_name, 1);
-    target.set_float3("Position", {m_point});
-    target.set_float3("Velocity", {float3{-1, -1, 0}});
-    target.set_birth_moment(1.0f);
+    auto target = interface.emit_manager().request(m_particle_type_name, 1);
+    target->set_float3("Position", {m_point});
+    target->set_float3("Velocity", {float3{-1, -1, 0}});
+    target->fill_float("Birth Time", interface.time_span().end());
   }
 };
 
@@ -95,7 +95,7 @@ class SurfaceEmitter : public Emitter {
     SmallVector<float3> positions;
     SmallVector<float3> velocities;
     SmallVector<float> sizes;
-    SmallVector<float> birth_moments;
+    SmallVector<float> birth_times;
 
     for (uint i = 0; i < particles_to_emit; i++) {
       MLoopTri triangle = triangles[rand() % triangle_amount];
@@ -125,15 +125,15 @@ class SurfaceEmitter : public Emitter {
       positions.append(point_at_birth);
       velocities.append(normal_velocity * normal_velocity_factor +
                         emitter_velocity * emitter_velocity_factor);
-      birth_moments.append(birth_moment);
+      birth_times.append(interface.time_span().interpolate(birth_moment));
       sizes.append(size);
     }
 
-    auto &target = interface.request(m_particle_type_name, positions.size());
-    target.set_float3("Position", positions);
-    target.set_float3("Velocity", velocities);
-    target.set_float("Size", sizes);
-    target.set_birth_moments(birth_moments);
+    auto target = interface.emit_manager().request(m_particle_type_name, positions.size());
+    target->set_float3("Position", positions);
+    target->set_float3("Velocity", velocities);
+    target->set_float("Size", sizes);
+    target->set_float("Birth Time", birth_times);
   }
 
   float3 random_point_in_triangle(float3 a, float3 b, float3 c)



More information about the Bf-blender-cvs mailing list