ProtoTracer  1.0
Real-time 3D rendering and animation engine
Loading...
Searching...
No Matches
MaxFilter.tpp
Go to the documentation of this file.
1#pragma once
2
3template<size_t memory>
4MaxFilter<memory>::MaxFilter() {
5 // Initialize the maxValues array to 0.0f
6 for (int i = 0; i < maxMemory; i++) {
7 maxValues[i] = 0.0f;
8 }
9}
10
11template<size_t memory>
12void MaxFilter<memory>::ShiftArray(uint8_t mem, float* arr) {
13 for (uint8_t i = 0; i < mem; i++) {
14 arr[i] = arr[i + 1];
15 }
16
17 arr[mem - 1] = 0.0;
18}
19
20template<size_t memory>
21float MaxFilter<memory>::Filter(float value) {
22 float avg = 0;
23
24 if (currentAmount < memory) {
25 values[currentAmount++] = value;
26 } else {
27 ShiftArray(memory, values); // pop first
28 values[memory - 1] = value;
29 }
30
31 float currentMax = values[0]; // 1st element
32 for (uint8_t i = 1; i < currentAmount; i++) {
33 if (values[i] > currentMax) currentMax = values[i];
34 }
35
36 if (maxValues[maxMemory - 1] != currentMax) { // the current max is different than the first, shift back and add
37 ShiftArray(maxMemory, maxValues); // pop first
38 maxValues[maxMemory - 1] = currentMax;
39 }
40
41 for (uint8_t i = 0; i < maxMemory; i++) {
42 avg = avg + maxValues[i];
43 }
44
45 return avg / maxMemory;
46}