diff options
author | Jacques Lucke <mail@jlucke.com> | 2019-12-22 16:21:25 +0300 |
---|---|---|
committer | Jacques Lucke <mail@jlucke.com> | 2019-12-22 16:21:25 +0300 |
commit | ee8dfc0062a63ae8c3efd05d3f9fb9fabb60a092 (patch) | |
tree | 8d45415592bf2e950d9257545ad3f46fbc437094 /source/blender/simulations/bparticles | |
parent | 335b5bc540dd94a630b7155a88b3e18ff53db37b (diff) |
replace time span with float interval
Diffstat (limited to 'source/blender/simulations/bparticles')
8 files changed, 33 insertions, 144 deletions
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( ParticleAllocator &particle_allocator, ArrayRef<AttributesBlock *> blocks, StringMap<ParticleSystemInfo> &systems_to_simulate, - TimeSpan time_span, + FloatInterval time_span, SimulationState &simulation_state) { tbb::parallel_for((uint)0, blocks.size(), [&](uint block_index) { @@ -359,7 +358,7 @@ BLI_NOINLINE static void simulate_blocks_for_time_span( ParticleSystemInfo &system_info = systems_to_simulate.lookup(particle_system_name); LargeScopedArray<float> remaining_durations(block.used_size()); - remaining_durations.fill(time_span.duration()); + remaining_durations.fill(time_span.size()); simulate_particle_chunk(simulation_state, particle_allocator, @@ -432,7 +431,7 @@ BLI_NOINLINE static void simulate_all_existing_blocks( SimulationState &simulation_state, StringMap<ParticleSystemInfo> &systems_to_simulate, ParticleAllocator &particle_allocator, - TimeSpan time_span) + FloatInterval time_span) { Vector<AttributesBlock *> blocks = get_all_blocks_to_simulate(simulation_state.particles(), systems_to_simulate); @@ -443,7 +442,7 @@ BLI_NOINLINE static void simulate_all_existing_blocks( BLI_NOINLINE static void create_particles_from_emitters(SimulationState &simulation_state, ParticleAllocator &particle_allocator, ArrayRef<Emitter *> emitters, - TimeSpan time_span) + FloatInterval time_span) { for (Emitter *emitter : emitters) { EmitterInterface interface(simulation_state, particle_allocator, time_span); @@ -458,7 +457,7 @@ void simulate_particles(SimulationState &simulation_state, SCOPED_TIMER(__func__); ParticlesState &particles_state = simulation_state.particles(); - TimeSpan simulation_time_span = simulation_state.time().current_update_time(); + FloatInterval simulation_time_span = simulation_state.time().current_update_time(); Vector<AttributesBlock *> newly_created_blocks; { diff --git a/source/blender/simulations/bparticles/simulation_state.hpp b/source/blender/simulations/bparticles/simulation_state.hpp index a3f4302eace..1a686afcd52 100644 --- a/source/blender/simulations/bparticles/simulation_state.hpp +++ b/source/blender/simulations/bparticles/simulation_state.hpp @@ -1,11 +1,14 @@ #pragma once +#include "BLI_float_interval.h" + #include "particles_state.hpp" #include "world_state.hpp" -#include "time_span.hpp" namespace BParticles { +using BLI::FloatInterval; + class SimulationTimeState { private: bool m_is_updating = false; @@ -20,10 +23,10 @@ class SimulationTimeState { return m_is_updating; } - TimeSpan current_update_time() const + FloatInterval current_update_time() const { BLI_assert(m_is_updating); - return TimeSpan(m_update_start_time, m_update_duration); + return FloatInterval(m_update_start_time, m_update_duration); } uint current_update_index() const diff --git a/source/blender/simulations/bparticles/time_span.hpp b/source/blender/simulations/bparticles/time_span.hpp deleted file mode 100644 index a8cabcafb72..00000000000 --- a/source/blender/simulations/bparticles/time_span.hpp +++ /dev/null @@ -1,115 +0,0 @@ -#pragma once - -#include "BLI_array_ref.h" - -namespace BParticles { - -using BLI::ArrayRef; -using BLI::MutableArrayRef; - -/** - * Contains a time range defined by a start time and non-zero duration. The times are measured in - * seconds. - */ -struct TimeSpan { - private: - float m_start, m_duration; - - public: - TimeSpan(float start, float duration) : m_start(start), m_duration(duration) - { - } - - /** - * Get the beginning of the time span. - */ - float start() const - { - return m_start; - } - - /** - * Get the duration of the time span. - */ - float duration() const - { - return m_duration; - } - - /** - * Get the end of the time span. - */ - float end() const - { - return m_start + m_duration; - } - - /** - * Compute a point in time within this time step. Usually 0 <= t <= 1. - */ - float interpolate(float t) const - { - return m_start + t * m_duration; - } - - void interpolate(ArrayRef<float> times, MutableArrayRef<float> r_results) - { - BLI_assert(times.size() == r_results.size()); - for (uint i : times.index_iterator()) { - r_results[i] = this->interpolate(times[i]); - } - } - - void sample_linear(MutableArrayRef<float> r_results) - { - if (r_results.size() == 0) { - return; - } - if (r_results.size() == 1) { - r_results[0] = this->interpolate(0.5f); - } - for (uint i : r_results.index_iterator()) { - float factor = (i - 1) / (float)r_results.size(); - r_results[i] = this->interpolate(factor); - } - } - - /** - * The reverse of interpolate. - * Asserts when the duration is 0. - */ - float get_factor(float time) const - { - BLI_assert(m_duration > 0.0f); - return (time - m_start) / m_duration; - } - - /** - * Same as get_factor, but returns zero when the duration is zero. - */ - float get_factor_safe(float time) const - { - if (m_duration > 0) { - return this->get_factor(time); - } - else { - return 0.0f; - } - } - - void uniform_sample_range(float samples_per_time, - float &r_factor_start, - float &r_factor_step) const - { - if (m_duration == 0) { - /* Just needs to be greater than one. */ - r_factor_start = 2.0f; - return; - } - r_factor_step = 1 / (m_duration * samples_per_time); - float time_start = std::ceil(m_start * samples_per_time) / samples_per_time; - r_factor_start = this->get_factor_safe(time_start); - } -}; - -} // namespace BParticles |