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

◆ update()

void fl::WaveSimulation1D_Real::update ( )

Definition at line 90 of file wave_simulation_real.cpp.

90 {
91 int16_t *curr = (whichGrid == 0) ? grid1.get() : grid2.get();
92 int16_t *next = (whichGrid == 0) ? grid2.get() : grid1.get();
93
94 // Update boundaries with a Neumann (zero-gradient) condition:
95 curr[0] = curr[1];
96 curr[length + 1] = curr[length];
97
98 // Compute dampening factor as an integer value: 2^(mDampenening)
99 int32_t dampening_factor = 1 << mDampenening;
100
101 int32_t mCourantSq32 = static_cast<int32_t>(mCourantSq);
102 // Iterate over each inner cell.
103 for (size_t i = 1; i < length + 1; i++) {
104 // Compute the 1D Laplacian:
105 // lap = curr[i+1] - 2 * curr[i] + curr[i-1]
106 int32_t lap =
107 (int32_t)curr[i + 1] - ((int32_t)curr[i] << 1) + curr[i - 1];
108
109 // Multiply the Laplacian by the simulation speed using Q15 arithmetic:
110 int32_t term = (mCourantSq32 * lap) >> 15;
111
112 // Compute the new value:
113 // f = -next[i] + 2 * curr[i] + term
114 int32_t f = -(int32_t)next[i] + ((int32_t)curr[i] << 1) + term;
115
116 // Apply damping:
117 f = f - (f / dampening_factor);
118
119 // Clamp the result to the Q15 range [-32768, 32767].
120 if (f > 32767)
121 f = 32767;
122 else if (f < -32768)
123 f = -32768;
124
125 next[i] = (int16_t)f;
126 }
127
128 if (mHalfDuplex) {
129 // Set the negative values to zero.
130 for (size_t i = 1; i < length + 1; i++) {
131 if (next[i] < 0) {
132 next[i] = 0;
133 }
134 }
135 }
136
137 // Toggle the active grid.
138 whichGrid ^= 1;
139}
fl::scoped_array< int16_t > grid2
fl::scoped_array< int16_t > grid1

References grid1, grid2, length, mCourantSq, mDampenening, mHalfDuplex, and whichGrid.