Commit: 65292981a42b3feb5885fa65a2f440d718ef213a
Author: Jacques Lucke
Date:   Mon Jul 8 16:50:28 2019 +0200
Branches: functions
https://developer.blender.org/rB65292981a42b3feb5885fa65a2f440d718ef213a

improve ActionInterface

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

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

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

diff --git a/source/blender/simulations/bparticles/actions.cpp 
b/source/blender/simulations/bparticles/actions.cpp
index fd3b94084fa..9309037ae1f 100644
--- a/source/blender/simulations/bparticles/actions.cpp
+++ b/source/blender/simulations/bparticles/actions.cpp
@@ -8,6 +8,32 @@ Action::~Action()
 {
 }
 
+void ActionInterface::execute_action_for_subset(ArrayRef<uint> indices,
+                                                std::unique_ptr<Action> 
&action)
+{
+  EventExecuteInterface &interface = m_event_execute_interface;
+
+  ParticleSet &particles = interface.particles();
+  auto current_times = interface.current_times();
+
+  SmallVector<float> sub_current_times;
+  SmallVector<uint> particle_indices;
+  for (uint i : indices) {
+    particle_indices.append(particles.get_particle_index(i));
+    sub_current_times.append(current_times[i]);
+  }
+
+  ParticleSet sub_particles(particles.block(), particle_indices);
+  EventExecuteInterface sub_execute_interface(sub_particles,
+                                              interface.block_allocator(),
+                                              sub_current_times,
+                                              interface.event_storage(),
+                                              interface.attribute_offsets(),
+                                              interface.step_end_time());
+  ActionInterface sub_interface(sub_execute_interface, m_event_info);
+  action->execute(sub_interface);
+}
+
 class NoneAction : public Action {
   void execute(ActionInterface &UNUSED(interface)) override
   {
@@ -32,12 +58,10 @@ class ChangeDirectionAction : public Action {
 
   void execute(ActionInterface &interface) override
   {
-    EventExecuteInterface &execute_interface = interface.execute_interface();
-
-    ParticleSet particles = execute_interface.particles();
+    ParticleSet particles = interface.particles();
     auto velocities = particles.attributes().get_float3("Velocity");
-    auto position_offsets = 
execute_interface.attribute_offsets().get_float3("Position");
-    auto velocity_offsets = 
execute_interface.attribute_offsets().get_float3("Velocity");
+    auto position_offsets = 
interface.attribute_offsets().get_float3("Position");
+    auto velocity_offsets = 
interface.attribute_offsets().get_float3("Velocity");
 
     auto caller = m_compute_inputs.get_caller(particles.attributes(), 
interface.event_info());
 
@@ -53,7 +77,7 @@ class ChangeDirectionAction : public Action {
       float3 direction = fn_out.get<float3>(0);
 
       velocities[pindex] = direction;
-      position_offsets[pindex] = direction * 
execute_interface.remaining_time_in_step(i);
+      position_offsets[pindex] = direction * 
interface.remaining_time_in_step(i);
       velocity_offsets[pindex] = float3(0);
     }
 
@@ -64,7 +88,7 @@ class ChangeDirectionAction : public Action {
 class KillAction : public Action {
   void execute(ActionInterface &interface) override
   {
-    
interface.execute_interface().kill(interface.execute_interface().particles().indices());
+    interface.kill(interface.particles().indices());
   }
 };
 
@@ -98,7 +122,7 @@ class ExplodeAction : public Action {
 
   void execute(ActionInterface &interface) override
   {
-    ParticleSet &particles = interface.execute_interface().particles();
+    ParticleSet &particles = interface.particles();
 
     auto positions = particles.attributes().get_float3("Position");
 
@@ -127,8 +151,7 @@ class ExplodeAction : public Action {
       }
     }
 
-    auto &target = 
interface.execute_interface().request_emit_target(m_new_particle_name,
-                                                                     
original_indices);
+    auto &target = interface.request_emit_target(m_new_particle_name, 
original_indices);
     target.set_float3("Position", new_positions);
     target.set_float3("Velocity", new_velocities);
 
@@ -153,13 +176,28 @@ class ConditionAction : public Action {
 
   void execute(ActionInterface &interface) override
   {
-    EventExecuteInterface &execute_interface = interface.execute_interface();
+    ParticleSet particles = interface.particles();
+    SmallVector<bool> conditions(particles.size());
+    this->compute_conditions(interface, conditions);
 
     SmallVector<uint> true_indices, false_indices;
-    SmallVector<float> true_times, false_times;
+    for (uint i : particles.range()) {
+      if (conditions[i]) {
+        true_indices.append(i);
+      }
+      else {
+        false_indices.append(i);
+      }
+    }
+
+    interface.execute_action_for_subset(true_indices, m_true_action);
+    interface.execute_action_for_subset(false_indices, m_false_action);
+  }
 
-    ParticleSet particles = execute_interface.particles();
-    auto current_times = execute_interface.current_times();
+  void compute_conditions(ActionInterface &interface, ArrayRef<bool> 
r_conditions)
+  {
+    ParticleSet particles = interface.particles();
+    BLI_assert(particles.size() == r_conditions.size());
 
     auto caller = m_compute_inputs.get_caller(particles.attributes(), 
interface.event_info());
     FN_TUPLE_CALL_ALLOC_TUPLES(caller.body(), fn_in, fn_out);
@@ -170,35 +208,8 @@ class ConditionAction : public Action {
       uint pindex = particles.get_particle_index(i);
       caller.call(fn_in, fn_out, execution_context, pindex);
       bool condition = fn_out.get<bool>(0);
-      if (condition) {
-        true_indices.append(pindex);
-        true_times.append(current_times[i]);
-      }
-      else {
-        false_indices.append(pindex);
-        false_times.append(current_times[i]);
-      }
+      r_conditions[i] = condition;
     }
-
-    ParticleSet true_particles(particles.block(), true_indices);
-    EventExecuteInterface true_execute_interface(true_particles,
-                                                 
execute_interface.block_allocator(),
-                                                 true_times,
-                                                 
execute_interface.event_storage(),
-                                                 
execute_interface.attribute_offsets(),
-                                                 
execute_interface.step_end_time());
-    ActionInterface true_interface(true_execute_interface, 
interface.event_info());
-    m_true_action->execute(true_interface);
-
-    ParticleSet false_particles(particles.block(), false_indices);
-    EventExecuteInterface false_execute_interface(false_particles,
-                                                  
execute_interface.block_allocator(),
-                                                  false_times,
-                                                  
execute_interface.event_storage(),
-                                                  
execute_interface.attribute_offsets(),
-                                                  
execute_interface.step_end_time());
-    ActionInterface false_interface(false_execute_interface, 
interface.event_info());
-    m_false_action->execute(false_interface);
   }
 };
 
diff --git a/source/blender/simulations/bparticles/actions.hpp 
b/source/blender/simulations/bparticles/actions.hpp
index 782f5965bb6..d33d20e3e47 100644
--- a/source/blender/simulations/bparticles/actions.hpp
+++ b/source/blender/simulations/bparticles/actions.hpp
@@ -17,6 +17,7 @@ class EventInfo {
 };
 
 class ParticleFunction;
+class Action;
 
 class ParticleFunctionCaller {
  private:
@@ -64,8 +65,8 @@ class ParticleFunction {
 
     for (uint i = 0; i < m_function->input_amount(); i++) {
       StringRef input_name = m_function->input_name(i);
-      void *ptr;
-      uint stride;
+      void *ptr = nullptr;
+      uint stride = 0;
       if (input_name.startswith("Event")) {
         StringRef event_attribute_name = input_name.drop_prefix("Event: ");
         ptr = event_info.get_info_array(event_attribute_name);
@@ -110,6 +111,40 @@ class ActionInterface {
   {
     return m_event_info;
   }
+
+  ParticleSet &particles()
+  {
+    return m_event_execute_interface.particles();
+  }
+
+  AttributeArrays attribute_offsets()
+  {
+    return m_event_execute_interface.attribute_offsets();
+  }
+
+  float remaining_time_in_step(uint index)
+  {
+    return m_event_execute_interface.step_end_time() -
+           m_event_execute_interface.current_times()[index];
+  }
+
+  ArrayRef<float> current_times()
+  {
+    return m_event_execute_interface.current_times();
+  }
+
+  void kill(ArrayRef<uint> particle_indices)
+  {
+    m_event_execute_interface.kill(particle_indices);
+  }
+
+  InstantEmitTarget &request_emit_target(StringRef particle_type_name,
+                                         ArrayRef<uint> original_indices)
+  {
+    return m_event_execute_interface.request_emit_target(particle_type_name, 
original_indices);
+  }
+
+  void execute_action_for_subset(ArrayRef<uint> indices, 
std::unique_ptr<Action> &action);
 };
 
 class Action {
diff --git a/source/blender/simulations/bparticles/core.hpp 
b/source/blender/simulations/bparticles/core.hpp
index fd8d0347066..666b0cc61df 100644
--- a/source/blender/simulations/bparticles/core.hpp
+++ b/source/blender/simulations/bparticles/core.hpp
@@ -572,11 +572,6 @@ class EventExecuteInterface {
    */
   float step_end_time();
 
-  /**
-   * Get the remaining time a particle in the current step.
-   */
-  float remaining_time_in_step(uint index);
-
   /**
    * Get the data stored in the Event->filter() function for a particle index.
    */
@@ -941,11 +936,6 @@ inline float EventExecuteInterface::step_end_time()
   return m_step_end_time;
 }
 
-inline float EventExecuteInterface::remaining_time_in_step(uint index)
-{
-  return m_step_end_time - m_current_times[index];
-}
-
 template<typename T> inline T &EventExecuteInterface::get_storage(uint pindex)
 {
   BLI_STATIC_ASSERT(std::is_trivial<T>::value, "");

_______________________________________________
Bf-blender-cvs mailing list
[email protected]
https://lists.blender.org/mailman/listinfo/bf-blender-cvs

Reply via email to