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

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