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

◆ update()

void fl::WaveSimulation2D_Real::update ( )

Definition at line 208 of file wave_simulation_real.cpp.

208 {
209 int16_t *curr = (whichGrid == 0 ? grid1.get() : grid2.get());
210 int16_t *next = (whichGrid == 0 ? grid2.get() : grid1.get());
211
212 // Update horizontal boundaries.
213 for (size_t j = 0; j < height + 2; ++j) {
214 curr[j * stride + 0] = curr[j * stride + 1];
215 curr[j * stride + (width + 1)] = curr[j * stride + width];
216 }
217
218 // Update vertical boundaries.
219 for (size_t i = 0; i < width + 2; ++i) {
220 curr[0 * stride + i] = curr[1 * stride + i];
221 curr[(height + 1) * stride + i] = curr[height * stride + i];
222 }
223
224 // Compute the dampening factor as an integer: 2^(dampening).
225 int32_t dampening_factor = 1 << mDampening; // e.g., 6 -> 64
226 int32_t mCourantSq32 = static_cast<int32_t>(mCourantSq);
227
228 // Update each inner cell.
229 for (size_t j = 1; j <= height; ++j) {
230 for (size_t i = 1; i <= width; ++i) {
231 int index = j * stride + i;
232 // Laplacian: sum of four neighbors minus 4 times the center.
233 int32_t laplacian = (int32_t)curr[index + 1] + curr[index - 1] +
234 curr[index + stride] + curr[index - stride] -
235 ((int32_t)curr[index] << 2);
236 // Compute the new value:
237 // f = - next[index] + 2 * curr[index] + mCourantSq * laplacian
238 // The multiplication is in Q15, so we shift right by 15.
239 int32_t term = (mCourantSq32 * laplacian) >> 15;
240 int32_t f =
241 -(int32_t)next[index] + ((int32_t)curr[index] << 1) + term;
242
243 // Apply damping:
244 f = f - (f / dampening_factor);
245
246 // Clamp f to the Q15 range.
247 if (f > 32767)
248 f = 32767;
249 else if (f < -32768)
250 f = -32768;
251
252 next[index] = (int16_t)f;
253 }
254 }
255
256 if (mHalfDuplex) {
257 // Set negative values to zero.
258 for (size_t j = 1; j <= height; ++j) {
259 for (size_t i = 1; i <= width; ++i) {
260 int index = j * stride + i;
261 if (next[index] < 0) {
262 next[index] = 0;
263 }
264 }
265 }
266 }
267
268 // Swap the roles of the grids.
269 whichGrid ^= 1;
270}
fl::scoped_array< int16_t > grid1
fl::scoped_array< int16_t > grid2

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