-
Notifications
You must be signed in to change notification settings - Fork 5
/
HUDSensorFilter.qml
70 lines (56 loc) · 1.82 KB
/
HUDSensorFilter.qml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
import QtQuick 2.15
QtObject {
property var values: []
property real sum: 0
property real size: 1
property bool enabled: size > 1
property bool isAzimuthFilter: false
property real criticalAngularRange: 30
onSizeChanged: {
reset(sum / values.length);
}
function update(value) {
if (!enabled) {
return value;
}
if (values.length == size) {
sum -= values.shift();
}
if (!isAzimuthFilter) {
sum += value;
values.push(value);
return sum / values.length;
}
// prevent jumps in angular data if crossing the 360 - 0 boundary
var adjustedValue = values[values.length-1] - value > 360 - criticalAngularRange
? value + 360
: value - values[values.length-1] > 360 - criticalAngularRange
? value - 360
: value;
sum += adjustedValue;
values.push(adjustedValue);
// correct stored values so that they stay within an acceptable range, do this
// only once we are outside of the critical region
if (adjustedValue > 360 + criticalAngularRange + 90) {
for (var i=0; i<values.length; i++) {
values[i] -= 360;
}
sum -= values.length * 360;
} else if (adjustedValue < - criticalAngularRange - 90) {
for (var j=0; j<values.length; j++) {
values[j] += 360;
}
sum += values.length * 360;
}
return (sum / values.length + 360) % 360;
}
function reset(value) {
if (enabled && value) {
values = [];
for (var i = 0; i < size; i++) {
values[i] = value;
}
sum = size * value;
}
}
}