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

◆ update()

void fl::WaveSimulation1D_Real::update ( )

Definition at line 108 of file wave_simulation_real.cpp.

108 {
109 int16_t *curr = (whichGrid == 0) ? grid1.get() : grid2.get();
110 int16_t *next = (whichGrid == 0) ? grid2.get() : grid1.get();
111
112 // Update boundaries with a Neumann (zero-gradient) condition:
113 curr[0] = curr[1];
114 curr[length + 1] = curr[length];
115
116 // Compute dampening factor as an integer value: 2^(mDampenening)
117 int32_t dampening_factor = 1 << mDampenening;
118
119 int32_t mCourantSq32 = static_cast<int32_t>(mCourantSq);
120 // Iterate over each inner cell.
121 for (size_t i = 1; i < length + 1; i++) {
122 // Compute the 1D Laplacian:
123 // lap = curr[i+1] - 2 * curr[i] + curr[i-1]
124 int32_t lap =
125 (int32_t)curr[i + 1] - ((int32_t)curr[i] << 1) + curr[i - 1];
126
127 // Multiply the Laplacian by the simulation speed using Q15 arithmetic:
128 int32_t term = (mCourantSq32 * lap) >> 15;
129
130 // Compute the new value:
131 // f = -next[i] + 2 * curr[i] + term
132 int32_t f = -(int32_t)next[i] + ((int32_t)curr[i] << 1) + term;
133
134 // Apply damping:
135 f = f - (f / dampening_factor);
136
137 // Clamp the result to the Q15 range [-32768, 32767].
138 if (f > 32767)
139 f = 32767;
140 else if (f < -32768)
141 f = -32768;
142
143 next[i] = (int16_t)f;
144 }
145
146 if (mHalfDuplex) {
147 // Set the negative values to zero.
148 for (size_t i = 1; i < length + 1; i++) {
149 if (next[i] < 0) {
150 next[i] = 0;
151 }
152 }
153 }
154
155 // Toggle the active grid.
156 whichGrid ^= 1;
157}
fl::scoped_array< int16_t > grid2
fl::scoped_array< int16_t > grid1

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