5#ifndef MODES_ANIM_FADEOUT_HPP
6#define MODES_ANIM_FADEOUT_HPP
12namespace lampda::modes::anims {
30 template<
typename ContextType>
31 static bool depop_one(ContextType& ctx,
const etl::bitset<LampTy::ledCount>& maskedLeds, uint32_t& pixelId)
33 const auto bufferSize = ctx.lamp.ledCount;
36 for (int16_t y = ctx.lamp.maxHeight; y >= 0; y--)
39 uint8_t pixelSetCount = 0;
40 for (uint16_t x = 0; x <= ctx.lamp.maxWidth; x++)
43 if (buffIndex < 0 or buffIndex >= bufferSize)
45 if (not maskedLeds[buffIndex])
50 if (pixelSetCount > 0)
53 uint8_t selectedLed = lmpd_map<uint8_t>(rand(), 0, RAND_MAX, 0, pixelSetCount);
54 for (uint16_t x = 0; x <= ctx.lamp.maxWidth; x++)
57 if (tempPixelId < 0 or tempPixelId >= bufferSize)
59 if (not maskedLeds[tempPixelId])
63 pixelId = tempPixelId;
85 template<
typename ContextType>
86 static bool has_pixel_below(ContextType& ctx,
const etl::bitset<LampTy::ledCount>& maskedLeds, uint16_t x, int16_t y)
88 const auto bufferSize = ctx.lamp.ledCount;
90 if (y >= ctx.lamp.maxHeight or (y + 1) > ctx.lamp.maxHeight)
97 if (pixelId >= 0 and pixelId < bufferSize)
99 if (not maskedLeds[pixelId])
104 if (pixelIdOffseted >= 0 and pixelIdOffseted < bufferSize)
106 if (not maskedLeds[pixelIdOffseted])
112 template<
typename ContextType>
113 static bool depop_one(ContextType& ctx,
const etl::bitset<LampTy::ledCount>& maskedLeds, uint32_t& pixelId)
115 const auto bufferSize = ctx.lamp.ledCount;
116 uint8_t orphanCount = 0;
118 int16_t minYLine = ctx.lamp.maxHeight;
119 int16_t maxYLine = 0;
122 for (int16_t y = ctx.lamp.maxHeight; y >= 0; y--)
124 bool hasSetPixels =
false;
125 bool hasOrphanPixels =
false;
126 for (uint16_t x = 0; x <= ctx.lamp.maxWidth; x++)
129 if (tempPixelId < 0 or tempPixelId >= bufferSize)
131 if (not maskedLeds[tempPixelId])
136 if (not has_pixel_below<ContextType>(ctx, maskedLeds, x, y))
139 hasOrphanPixels =
true;
141 minYLine = min<int16_t>(minYLine, y);
142 maxYLine = max<int16_t>(maxYLine, y);
147 if (hasSetPixels and not hasOrphanPixels)
153 if (orphanCount <= 0)
159 uint8_t selectedLed = lmpd_map<uint8_t>(rand(), 0, RAND_MAX, 0, orphanCount);
162 for (int16_t y = maxYLine; y >= minYLine; y--)
164 for (uint16_t x = 0; x <= ctx.lamp.maxWidth; x++)
167 if (tempPixelId < 0 or tempPixelId >= bufferSize)
169 if (not maskedLeds[tempPixelId])
172 if (not has_pixel_below<ContextType>(ctx, maskedLeds, x, y))
174 if (selectedLed == 0)
176 pixelId = tempPixelId;
194 void reset(
auto& ctx)
200 particuleSystem.
reset();
203 latestProgress = -1.0;
204 latestProgressTime = 0;
205 particlesToDepopPerIteration = 0.0;
207 particlesDropped = 0;
213 depopRate += particlesToDepopPerIteration;
216 const float fractional = modf(depopRate, &integer);
217 if (integer >= 1.0 and particlesDropped < ctx.lamp.ledCount)
220 for (uint8_t depopIndex = 0; depopIndex < integer; depopIndex++)
223 depop_one_particles(ctx);
227 depopRate = fractional;
231 for (uint16_t i = 0; i < maskedLeds.size(); i++)
234 ctx.lamp.mask_led_at_index(i,
true);
241 static constexpr bool shouldKeepInLampBounds =
false;
243 utils::vec3d(0.0, 0.0, -9.81 / 4.0), ctx.lamp.frameDurationMs / 1000.0, shouldKeepInLampBounds);
247 static constexpr bool forceDisplayParticles =
true;
248 particuleSystem.
show(ctx.lamp, forceDisplayParticles);
259 const float progress = expectedProgress / gracePeriod;
261 const float updateDuration =
static_cast<float>((ctx.lamp.now - latestProgressTime) / 1000.0);
262 if (updateDuration <= 0.0f)
265 const float progressPerSecond = (progress - latestProgress) / updateDuration;
266 bool isValidCall = latestProgress >= 0 and updateDuration > 0;
271 particlesToDepopPerIteration = 0.0;
272 latestProgress = 0.0;
276 else if (latestProgress != progress)
278 latestProgress = progress;
279 latestProgressTime = ctx.lamp.now;
290 const uint16_t particleMax = ctx.lamp.ledCount;
291 const float frameFrequency = 1000.0f / ctx.lamp.frameDurationMs;
293 const float particlesLeft = particleMax - particlesDropped;
294 const float trueProgress = 1.0f - particlesLeft /
static_cast<float>(particleMax);
297 const float expectedParticlesDropped = particleMax * progress;
298 const float particleDeficit = expectedParticlesDropped -
static_cast<float>(particlesDropped);
300 const float remainingProgressRatio = 1.0f - progress;
304 if (remainingProgressRatio > 0.0f && particlesLeft > 0.0f)
307 float normalDepopRate = progressPerSecond * particlesLeft / frameFrequency;
311 float deficitCatchupRate = 0.0f;
312 if (progressPerSecond > 0.0f)
314 const float timeToFinish = remainingProgressRatio / progressPerSecond;
315 deficitCatchupRate = particleDeficit / timeToFinish / frameFrequency;
319 particlesToDepopPerIteration = normalDepopRate + deficitCatchupRate;
321 else if (particlesLeft > 0.0f && progress >= 1.0f)
324 particlesToDepopPerIteration = particlesLeft;
328 particlesToDepopPerIteration = 0.0;
332 const float maxParticlesToDepop = particlesLeft;
333 if (particlesToDepopPerIteration > maxParticlesToDepop)
334 particlesToDepopPerIteration = maxParticlesToDepop;
338 static bool recycle_particules_if_too_far(
const Particle& p)
340 static constexpr float bounds = 3.0;
344 bool depop_one_particles(
auto& ctx)
346 const auto bufferSize = ctx.lamp.ledCount;
350 if (not DropMode::template depop_one<
decltype(ctx)>(ctx, maskedLeds, pixelId) or pixelId < 0 or
351 pixelId >= bufferSize)
358 const uint32_t color = ctx.lamp.getPixelColor(pixelId);
360 maskedLeds.set(pixelId);
361 particlesDropped += 1;
374 std::ignore = isCreated;
379 static constexpr float gracePeriod = 0.99f;
381 float latestProgress = -1.0f;
382 uint32_t latestProgressTime = 0;
384 float particlesToDepopPerIteration = 0.0f;
385 float depopRate = 0.0f;
386 size_t particlesDropped = 0;
388 modes::ParticleSystem<20> particuleSystem;
389 etl::bitset<LampTy::ledCount> maskedLeds;
void iterate_no_collisions(const utils::vec3d &accelerationCartesian, const float deltaTime_s, const bool shouldContrain=true)
Advance the particle simulation, ignoring collisions.
Definition: particle_system.hpp:124
uint16_t get_number_of_active() const
Return the number of active particles.
Definition: particle_system.hpp:276
uint16_t depop_particules(const ShouldDepopFunction &shouldDepopFunction)
Filter particles depending on condition.
Definition: particle_system.hpp:179
bool init_deferred_particules(uint8_t maxParticlesToPop, const PositionGeneratorFunction &positionGeneratorFunction)
Init the particles from a initialization function, in a timed defered way.
Definition: particle_system.hpp:80
void reset()
reset system to zero count
Definition: particle_system.hpp:38
uint16_t show(const SampleColorFunction &sample_color, LampTy &lamp, const bool forceDisplay=false) const
Display this particle system.
Definition: particle_system.hpp:241
void set_max_particle_count(const uint16_t _particleCount)
Call when you want to change the particle count of the simulation Can be called once when starting th...
Definition: particle_system.hpp:49
void lampda_print(const char *format,...)
C linkage to print functions.
Definition: text_out.cpp:336
static constexpr uint16_t to_strip(uint16_t, uint16_t)
convert grid coordinates to strip index
Definition: lamp_type.hpp:1224
Define a particle system, with particle based logic.
Define a particle in 3D space. it has a position and speed.
Definition: particle.hpp:44
float z_mm
height, in millimeters.
Definition: particle.hpp:267
Drop mode policy: remove particles line by line from bottom to top Only pixels from the lowest non-em...
Definition: fadeout.hpp:25
static bool depop_one(ContextType &ctx, const etl::bitset< LampTy::ledCount > &maskedLeds, uint32_t &pixelId)
Find and depop one particle according to this mode.
Definition: fadeout.hpp:31
Drop mode policy: remove only "orphan" particles A particle can only be dropped if there is no other ...
Definition: fadeout.hpp:81
static bool has_pixel_below(ContextType &ctx, const etl::bitset< LampTy::ledCount > &maskedLeds, uint16_t x, int16_t y)
Check if a pixel at (x, y) has a pixel below it.
Definition: fadeout.hpp:86
Make an animation disapear with gravity.
Definition: fadeout.hpp:193
void update_progress_rate(auto &ctx, const float expectedProgress)
Update the drop rate.
Definition: fadeout.hpp:256
static constexpr float lampHeight_mm
Computation of the lamp height.
Definition: lamp_type.hpp:447
static constexpr uint16_t maxWidth
(indexable) Width of "pixel space" w/ lamp taken as a LED matrix
Definition: lamp_type.hpp:376
3d vector in any space
Definition: vector_math.h:54