FastLED 3.9.15
Loading...
Searching...
No Matches

◆ update()

void fl::WaveSimulation2D_Real::update ( )

Definition at line 226 of file wave_simulation_real.cpp.

226 {
227 int16_t *curr = (whichGrid == 0 ? grid1.data() : grid2.data());
228 int16_t *next = (whichGrid == 0 ? grid2.data() : grid1.data());
229
230 // Update horizontal boundaries.
231 for (size_t j = 0; j < height + 2; ++j) {
232 if (mXCylindrical) {
233 curr[j * stride + 0] = curr[j * stride + width];
234 curr[j * stride + (width + 1)] = curr[j * stride + 1];
235 } else {
236 curr[j * stride + 0] = curr[j * stride + 1];
237 curr[j * stride + (width + 1)] = curr[j * stride + width];
238 }
239 }
240
241 // Update vertical boundaries.
242 for (size_t i = 0; i < width + 2; ++i) {
243 curr[0 * stride + i] = curr[1 * stride + i];
244 curr[(height + 1) * stride + i] = curr[height * stride + i];
245 }
246
247 // Compute the dampening factor as an integer: 2^(dampening).
248 int32_t dampening_factor = 1 << mDampening; // e.g., 6 -> 64
249 int32_t mCourantSq32 = static_cast<int32_t>(mCourantSq);
250
251 // Update each inner cell.
252 for (size_t j = 1; j <= height; ++j) {
253 for (size_t i = 1; i <= width; ++i) {
254 int index = j * stride + i;
255 // Laplacian: sum of four neighbors minus 4 times the center.
256 int32_t laplacian = (int32_t)curr[index + 1] + curr[index - 1] +
257 curr[index + stride] + curr[index - stride] -
258 ((int32_t)curr[index] << 2);
259 // Compute the new value:
260 // f = - next[index] + 2 * curr[index] + mCourantSq * laplacian
261 // The multiplication is in Q15, so we shift right by 15.
262 int32_t term = (mCourantSq32 * laplacian) >> 15;
263 int32_t f =
264 -(int32_t)next[index] + ((int32_t)curr[index] << 1) + term;
265
266 // Apply damping:
267 f = f - (f / dampening_factor);
268
269 // Clamp f to the Q15 range.
270 if (f > 32767)
271 f = 32767;
272 else if (f < -32768)
273 f = -32768;
274
275 next[index] = (int16_t)f;
276 }
277 }
278
279 if (mHalfDuplex) {
280 // Set negative values to zero.
281 for (size_t j = 1; j <= height; ++j) {
282 for (size_t i = 1; i <= width; ++i) {
283 int index = j * stride + i;
284 if (next[index] < 0) {
285 next[index] = 0;
286 }
287 }
288 }
289 }
290
291 // Swap the roles of the grids.
292 whichGrid ^= 1;
293}
fl::vector< int16_t, fl::allocator_psram< int16_t > > grid2
fl::vector< int16_t, fl::allocator_psram< int16_t > > grid1

References grid1, grid2, height, mCourantSq, mDampening, mHalfDuplex, mXCylindrical, stride, whichGrid, and width.