Qt
Internal/Contributor docs for the Qt SDK. Note: These are NOT official API docs; those are found at https://doc.qt.io/
Loading...
Searching...
No Matches
qtouchfilter_p.h
Go to the documentation of this file.
1// Copyright (C) 2016 Jolla Ltd, author: <gunnar.sletta@jollamobile.com>
2// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only
3#ifndef QTOUCHFILTER_P_H
4#define QTOUCHFILTER_P_H
5
6#include <private/qglobal_p.h>
7
8//
9// W A R N I N G
10// -------------
11//
12// This file is not part of the Qt API. It exists purely as an
13// implementation detail. This header file may change from version to
14// version without notice, or even be removed.
15//
16// We mean it.
17//
18
19QT_BEGIN_NAMESPACE
20
21struct QTouchFilter
22{
23 QTouchFilter();
24
25 void initialize(float pos, float velocity);
26 void update(float pos, float velocity, float timeDelta);
27
28 float position() const { return x.x; }
29 float velocity() const { return x.y; }
30
31private:
32 struct vec2 {
33 vec2(float x = 0.0f, float y = 0.0f) : x(x), y(y) { }
34 float x, y;
35
36 vec2 operator-(vec2 v) {
37 return vec2(x - v.x, y - v.y);
38 }
39
40 vec2 operator+(vec2 v) {
41 return vec2(x + v.x, y + v.y);
42 }
43 };
44
45 struct mat2 {
46 float a, b, c, d;
47 mat2(float a = 1.0f, float b = 0.0f, float c = 0.0f, float d = 1.0f)
48 : a(a)
49 , b(b)
50 , c(c)
51 , d(d)
52 {
53 }
54
55 mat2 transposed() const {
56 return mat2(a, c,
57 b, d);
58 }
59
60 mat2 inverted() const {
61 float det = 1.0f / (a * d - b * c);
62 return mat2( d * det, -b * det,
63 -c * det, a * det);
64 }
65
66 mat2 operator+(mat2 m) const {
67 return mat2(a + m.a, b + m.b,
68 c + m.c, d + m.d);
69 }
70
71 mat2 operator-(mat2 m) const {
72 return mat2(a - m.a, b - m.b,
73 c - m.c, d - m.d);
74 }
75
76 vec2 operator*(vec2 v) const {
77 return vec2(a * v.x + b * v.y,
78 c * v.x + d * v.y);
79 }
80
81 mat2 operator*(mat2 M) const {
82 return mat2(a * M.a + b * M.c,
83 a * M.b + b * M.d,
84 c * M.a + d * M.c,
85 c * M.b + d * M.d);
86 }
87 };
88
89 vec2 x;
90 mat2 A;
91 mat2 P;
92 mat2 Q;
93 mat2 R;
94 mat2 H;
95};
96
97inline QTouchFilter::QTouchFilter()
98{
99}
100
101inline void QTouchFilter::initialize(float pos, float velocity)
102{
103 x = vec2(pos, velocity);
104
105 P = mat2(0.0f, 0.0f,
106 0.0f, 0.0f);
107
108 Q = mat2(0.0f, 0.0f,
109 0.0f, 0.1f);
110 R = mat2(0.1f, 0.0f,
111 0.0f, 0.1f);
112}
113
114inline void QTouchFilter::update(float pos, float velocity, float dT)
115{
116 A.b = dT;
117
118 // Prediction setp
119 x = A * x;
120 P = A * P * A.transposed() + Q;
121
122 // Correction step (complete with H)
123 // mat2 S = H * P * H.transposed() + R;
124 // mat2 K = P * H.transposed() * S.inverted();
125 // vec2 m(pos, velocity);
126 // vec2 y = m - H * x;
127 // x = x + K * y;
128 // P = (mat2() - K * H) * P;
129
130 // Correction step (without H as H is currently set to I, so we can ignore
131 // it in the calculations...)
132 mat2 S = P + R;
133 mat2 K = P * S.inverted();
134 vec2 m(pos, velocity);
135 vec2 y = m - x;
136 x = x + K * y;
137 P = (mat2() - K) * P;
138
139}
140
141QT_END_NAMESPACE
142
143#endif // QTOUCHFILTER_P_H