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