[Bf-blender-cvs] [ee8dfc0062a] functions: replace time span with float interval

Jacques Lucke noreply at git.blender.org
Sun Dec 22 14:26:09 CET 2019


Commit: ee8dfc0062a63ae8c3efd05d3f9fb9fabb60a092
Author: Jacques Lucke
Date:   Sun Dec 22 14:21:25 2019 +0100
Branches: functions
https://developer.blender.org/rBee8dfc0062a63ae8c3efd05d3f9fb9fabb60a092

replace time span with float interval

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

A	source/blender/blenlib/BLI_float_interval.h
M	source/blender/simulations/bparticles/block_step_data.hpp
M	source/blender/simulations/bparticles/emitter_interface.hpp
M	source/blender/simulations/bparticles/emitters.cpp
M	source/blender/simulations/bparticles/events.cpp
M	source/blender/simulations/bparticles/offset_handlers.cpp
M	source/blender/simulations/bparticles/simulate.cpp
M	source/blender/simulations/bparticles/simulation_state.hpp
D	source/blender/simulations/bparticles/time_span.hpp

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

diff --git a/source/blender/blenlib/BLI_float_interval.h b/source/blender/blenlib/BLI_float_interval.h
new file mode 100644
index 00000000000..53e3a181ec7
--- /dev/null
+++ b/source/blender/blenlib/BLI_float_interval.h
@@ -0,0 +1,94 @@
+#ifndef __BLI_TIME_SPAN_H__
+#define __BLI_TIME_SPAN_H__
+
+#include "BLI_array_ref.h"
+
+namespace BLI {
+
+class FloatInterval {
+ private:
+  float m_start;
+  float m_size;
+
+ public:
+  FloatInterval(float start, float size) : m_start(start), m_size(size)
+  {
+    BLI_assert(size >= 0.0f);
+  }
+
+  float start() const
+  {
+    return m_start;
+  }
+
+  float size() const
+  {
+    return m_size;
+  }
+
+  float end() const
+  {
+    return m_start + m_size;
+  }
+
+  float value_at(float factor) const
+  {
+    return m_start + factor * m_size;
+  }
+
+  void value_at(ArrayRef<float> factors, MutableArrayRef<float> r_values)
+  {
+    BLI_assert(factors.size() == r_values.size());
+    for (uint i : factors.index_iterator()) {
+      r_values[i] = this->value_at(factors[i]);
+    }
+  }
+
+  void sample_linear(MutableArrayRef<float> r_values)
+  {
+    if (r_values.size() == 0) {
+      return;
+    }
+    if (r_values.size() == 1) {
+      r_values[0] = this->value_at(0.5f);
+    }
+    for (uint i : r_values.index_iterator()) {
+      float factor = (i - 1) / (float)r_values.size();
+      r_values[i] = this->value_at(factor);
+    }
+  }
+
+  float factor_of(float value) const
+  {
+    BLI_assert(m_size > 0.0f);
+    return (value - m_start) / m_size;
+  }
+
+  float safe_factor_of(float value) const
+  {
+    if (m_size > 0.0f) {
+      return this->factor_of(value);
+    }
+    else {
+      return 0.0f;
+    }
+  }
+
+  void uniform_sample_range(float samples_per_time,
+                            float &r_factor_start,
+                            float &r_factor_step) const
+  {
+    if (m_size == 0.0f) {
+      /* Just needs to be greater than one. */
+      r_factor_start = 2.0f;
+      return;
+    }
+    r_factor_step = 1 / (m_size * samples_per_time);
+    float time_start = std::ceil(m_start * samples_per_time) / samples_per_time;
+    r_factor_start = this->safe_factor_of(time_start);
+  }
+};
+
+}  // namespace BLI
+
+#endif /* __BLI_TIME_SPAN_H__ */
diff --git a/source/blender/simulations/bparticles/block_step_data.hpp b/source/blender/simulations/bparticles/block_step_data.hpp
index cee6ec171e8..62a1a41ee9f 100644
--- a/source/blender/simulations/bparticles/block_step_data.hpp
+++ b/source/blender/simulations/bparticles/block_step_data.hpp
@@ -1,11 +1,14 @@
 #pragma once
 
 #include "FN_attributes_ref.h"
-#include "time_span.hpp"
+
+#include "BLI_float_interval.h"
+
 #include "simulation_state.hpp"
 
 namespace BParticles {
 
+using BLI::FloatInterval;
 using FN::AttributesRef;
 
 struct BlockStepData {
@@ -65,10 +68,10 @@ class BlockStepDataAccess {
     return m_step_data.step_end_time;
   }
 
-  TimeSpan time_span(uint pindex)
+  FloatInterval time_span(uint pindex)
   {
     float duration = m_step_data.remaining_durations[pindex];
-    return TimeSpan(m_step_data.step_end_time - duration, duration);
+    return FloatInterval(m_step_data.step_end_time - duration, duration);
   }
 };
 
diff --git a/source/blender/simulations/bparticles/emitter_interface.hpp b/source/blender/simulations/bparticles/emitter_interface.hpp
index c0cc52c4b27..651eb7e1cd6 100644
--- a/source/blender/simulations/bparticles/emitter_interface.hpp
+++ b/source/blender/simulations/bparticles/emitter_interface.hpp
@@ -2,7 +2,6 @@
 
 #include "particle_allocator.hpp"
 #include "simulation_state.hpp"
-#include "time_span.hpp"
 
 namespace BParticles {
 
@@ -10,12 +9,12 @@ class EmitterInterface {
  private:
   SimulationState &m_simulation_state;
   ParticleAllocator &m_particle_allocator;
-  TimeSpan m_time_span;
+  FloatInterval m_time_span;
 
  public:
   EmitterInterface(SimulationState &simulation_state,
                    ParticleAllocator &particle_allocator,
-                   TimeSpan time_span)
+                   FloatInterval time_span)
       : m_simulation_state(simulation_state),
         m_particle_allocator(particle_allocator),
         m_time_span(time_span)
@@ -32,7 +31,7 @@ class EmitterInterface {
   /**
    * Time span that new particles should be emitted in.
    */
-  TimeSpan time_span()
+  FloatInterval time_span()
   {
     return m_time_span;
   }
diff --git a/source/blender/simulations/bparticles/emitters.cpp b/source/blender/simulations/bparticles/emitters.cpp
index 5f9a4260319..763dfb1da69 100644
--- a/source/blender/simulations/bparticles/emitters.cpp
+++ b/source/blender/simulations/bparticles/emitters.cpp
@@ -39,7 +39,7 @@ void PointEmitter::emit(EmitterInterface &interface)
     new_positions[i] = m_position.interpolate(t);
     new_velocities[i] = m_velocity.interpolate(t);
     new_sizes[i] = m_size.interpolate(t);
-    birth_times[i] = interface.time_span().interpolate(t);
+    birth_times[i] = interface.time_span().value_at(t);
   }
 
   for (StringRef type : m_systems_to_emit) {
@@ -297,14 +297,14 @@ void SurfaceEmitter::emit(EmitterInterface &interface)
     float3 position_before_birth = transforms_before_birth[i].transform_position(
         local_positions[i]);
     surface_velocities[i] = (positions_at_birth[i] - position_before_birth) / epsilon /
-                            interface.time_span().duration();
+                            interface.time_span().size();
   }
 
   LargeScopedArray<float3> world_normals(particles_to_emit);
   float4x4::transform_directions(transforms_at_birth, local_normals, world_normals);
 
   LargeScopedArray<float> birth_times(particles_to_emit);
-  interface.time_span().interpolate(birth_moments, birth_times);
+  interface.time_span().value_at(birth_moments, birth_times);
 
   LargeScopedArray<SurfaceHook> emit_hooks(particles_to_emit);
   BKE::ObjectIDHandle object_handle(m_object);
@@ -378,11 +378,11 @@ void CustomEmitter::emit(EmitterInterface &interface)
     }
   }
 
-  TimeSpan time_span = interface.time_span();
+  FloatInterval time_span = interface.time_span();
   FN::EmitterTimeInfoContext time_context;
   time_context.begin = time_span.start();
   time_context.end = time_span.end();
-  time_context.duration = time_span.duration();
+  time_context.duration = time_span.size();
   time_context.step = interface.time_step();
 
   FN::MFContextBuilder context_builder;
@@ -427,7 +427,7 @@ void CustomEmitter::emit(EmitterInterface &interface)
       case BirthTimeModes::Random: {
         LargeScopedArray<float> birth_times(new_particles.total_size());
         for (uint i = 0; i < particle_count; i++) {
-          birth_times[i] = time_span.interpolate(random_float());
+          birth_times[i] = time_span.value_at(random_float());
         }
         new_particles.set<float>("Birth Time", birth_times);
         break;
diff --git a/source/blender/simulations/bparticles/events.cpp b/source/blender/simulations/bparticles/events.cpp
index 4e53c1aed4f..ea1d759ece7 100644
--- a/source/blender/simulations/bparticles/events.cpp
+++ b/source/blender/simulations/bparticles/events.cpp
@@ -31,14 +31,14 @@ void AgeReachedEvent::filter(EventFilterInterface &interface)
     float age_at_end = end_time - birth_time;
 
     if (age_at_end >= trigger_age) {
-      TimeSpan time_span = interface.time_span(pindex);
+      FloatInterval time_span = interface.time_span(pindex);
 
-      float age_at_start = age_at_end - time_span.duration();
+      float age_at_start = age_at_end - time_span.size();
       if (trigger_age < age_at_start) {
         interface.trigger_particle(pindex, 0.0f);
       }
       else {
-        float time_factor = time_span.get_factor_safe(birth_time + trigger_age);
+        float time_factor = time_span.safe_factor_of(birth_time + trigger_age);
         CLAMP(time_factor, 0.0f, 1.0f);
         interface.trigger_particle(pindex, time_factor);
       }
diff --git a/source/blender/simulations/bparticles/offset_handlers.cpp b/source/blender/simulations/bparticles/offset_handlers.cpp
index b3ce0fdbc16..0bea3cd4b7e 100644
--- a/source/blender/simulations/bparticles/offset_handlers.cpp
+++ b/source/blender/simulations/bparticles/offset_handlers.cpp
@@ -22,7 +22,7 @@ void CreateTrailHandler::execute(OffsetHandlerInterface &interface)
       continue;
     }
 
-    TimeSpan time_span = interface.time_span(pindex);
+    FloatInterval time_span = interface.time_span(pindex);
 
     rgba_f color = colors[pindex];
 
@@ -31,7 +31,7 @@ void CreateTrailHandler::execute(OffsetHandlerInterface &interface)
 
     float3 total_offset = position_offsets[pindex] * interface.time_factors()[pindex];
     for (float factor = factor_start; factor < 1.0f; factor += factor_step) {
-      float time = time_span.interpolate(factor);
+      float time = time_span.value_at(factor);
       new_positions.append(positions[pindex] + total_offset * factor);
       new_birth_times.append(time);
       new_colors.append(color);
@@ -60,14 +60,14 @@ void SizeOverTimeHandler::execute(OffsetHandlerInterface &interface)
     float final_size = inputs.get_single<float>("Final Size", 0, pindex);
     float final_age = inputs.get_single<float>("Final Age", 1, pindex);
 
-    TimeSpan time_span = interface.time_span(pindex);
+    FloatInterval time_span = interface.time_span(pindex);
     float age = time_span.start() - birth_times[pindex];
     float time_until_end = final_age - age;
     if (time_until_end <= 0.0f) {
       continue;
     }
 
-    float factor = std::min(time_span.duration() / time_until_end, 1.0f);
+    float factor = std::min(time_span.size() / time_until_end, 1.0f);
     float new_size = final_size * factor + sizes[pindex] * (1.0f - factor);
     sizes[pindex] = new_size;
   }
diff --git a/source/blender/simulations/bparticles/simulate.cpp b/source/blender/simulations/bparticles/simulate.cpp
index d62a72c9132..1452cb9b6d7 100644
--- a/source/blender/simulations/bparticles/simulate.cpp
+++ b/source/blender/simulations/bparticles/simulate.cpp
@@ -7,7 +7,6 @@
 #include "FN_cpp_type.h"
 
 #include "simulate.hpp"
-#include "time_span.hpp"
 
 // #ifdef WITH_TBB
 #include "tbb/tbb.h"
@@ -348,7 +347,7 @@ BLI_NOINLINE static void simulate_blocks_for_time_span

@@ Diff output truncated at 10240 characters. @@



More information about the Bf-blender-cvs mailing list