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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
|
/*****************************************************************************
*
* grail - Gesture Recognition And Instantiation Library
*
* Copyright (C) 2010 Canonical Ltd.
* Copyright (C) 2010 Henrik Rydberg <rydberg@bitmath.org>
*
* This program is free software: you can redistribute it and/or modify it
* under the terms of the GNU General Public License as published by the
* Free Software Foundation, either version 3 of the License, or (at your
* option) any later version.
*
* This program is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* General Public License for more details.
*
* You should have received a copy of the GNU General Public License along
* with this program. If not, see <http://www.gnu.org/licenses/>.
*
****************************************************************************/
#include "grail-recognizer.h"
#include "grail-impl.h"
#include <math.h>
static const float FM_SN[DIM_FM] = { 1000, 1000, 1000, 1000 };
static const float FM_BAR[DIM_FM] = { 50, 50, 50, 50 };
static const grail_time_t FM_HOLD_MS[DIM_FM] = { 60, 60, 60, 60 };
static const grail_time_t FM_BAR_MS[DIM_FM] = { 300, 300, 500, 500 };
static const grail_time_t SAMPLE_MS = 10;
static const float EPS = 1e-3;
static void compute_position(float *x, float *y,
const struct utouch_frame *frame)
{
int i, n = frame->num_active;
*x = 0;
*y = 0;
if (n < 1)
return;
for (i = 0; i < n; i++) {
const struct utouch_contact *t = frame->active[i];
*x += t->x;
*y += t->y;
}
*x /= n;
*y /= n;
}
static float compute_radius(float x, float y,
const struct utouch_frame *frame)
{
int i, n = frame->num_active;
float r = 0, r2 = 0;
if (n < 2)
return r;
for (i = 0; i < n; i++) {
const struct utouch_contact *t = frame->active[i];
float dx = t->x - x;
float dy = t->y - y;
r2 += dx * dx + dy * dy;
}
r2 /= n;
r = sqrt(r2);
return r;
}
static float compute_rotation(float x, float y, float r,
const struct utouch_frame *frame)
{
int i, n = frame->num_active;
float da = 0, darc2 = 0;
if (n < 2)
return da;
for (i = 0; i < n; i++) {
const struct utouch_contact *t = frame->active[i];
const struct utouch_contact *ot = t->prev;
float dx = t->x - x;
float dy = t->y - y;
float mx = t->x - ot->x;
float my = t->y - ot->y;
darc2 += dx * my - dy * mx;
}
darc2 /= n;
da = darc2 / (r * r);
return da;
}
/* Response-augmented EWMA filter, courtesy of Vojtech Pavlik */
static float move_filter(const struct filter_model *m, float val)
{
if (val > m->value - m->fuzz / 2 && val < m->value + m->fuzz / 2)
return m->value;
if (val > m->value - m->fuzz && val < m->value + m->fuzz)
return (m->value * 3 + val) / 4;
if (val > m->value - m->fuzz * 2 && val < m->value + m->fuzz * 2)
return (m->value + val) / 2;
return val;
}
static void move_reset(struct move_model *m, int i, float x, grail_time_t t)
{
struct filter_model *fm = &m->fm[i];
fm->raw_delta = 0;
fm->action_delta = 0;
fm->velocity = 0;
fm->value = x;
fm->original = x;
fm->original_ms = t;
fm->sample = x;
fm->sample_ms = t;
m->tickle &= ~(1 << i);
m->active &= ~(1 << i);
m->timeout &= ~(1 << i);
}
static void move_update(struct move_model *m, int i, float x, grail_time_t t)
{
struct filter_model *fm = &m->fm[i];
float dt = t - fm->sample_ms;
fm->raw_delta = x - fm->value;
fm->action_delta = fm->raw_delta;
fm->value = x;
if (dt > SAMPLE_MS) {
fm->velocity = (x - fm->sample) / dt;
fm->sample = x;
fm->sample_ms = t;
}
if (fabs(fm->raw_delta) > EPS)
m->tickle |= (1 << i);
else
m->tickle &= ~(1 << i);
if (m->active & (1 << i))
return;
fm->action_delta = 0;
if (fabs(x - fm->original) > fm->bar) {
if (t - fm->original_ms > fm->hold_ms) {
m->active |= (1 << i);
fm->action_delta = x - fm->original;
}
} else if (t - fm->original_ms > fm->bar_ms) {
m->timeout |= (1 << i);
}
}
void gru_init_motion(struct grail *ge)
{
struct utouch_surface *s = utouch_frame_get_surface(ge->impl->fh);
struct gesture_recognizer *gru = ge->gru;
struct move_model *m = &gru->move;
float D[DIM_FM];
int i;
D[FM_X] = s->max_x - s->min_x;
D[FM_Y] = s->max_y - s->min_y;
D[FM_R] = sqrt(D[FM_X] * D[FM_X] + D[FM_Y] * D[FM_Y]);
D[FM_A] = 2 * M_PI;
for (i = 0; i < DIM_FM; i++) {
m->fm[i].fuzz = D[i] / FM_SN[i];
m->fm[i].bar = D[i] / FM_BAR[i];
m->fm[i].hold_ms = FM_HOLD_MS[i];
m->fm[i].bar_ms = FM_BAR_MS[i];
}
}
void gru_motion(struct grail *ge,
const struct utouch_frame *frame)
{
const struct utouch_surface *s = utouch_frame_get_surface(ge->impl->fh);
struct gesture_recognizer *gru = ge->gru;
struct move_model *m = &gru->move;
grail_time_t t = frame->time;
float x, y, r, a;
compute_position(&x, &y, frame);
if (frame->prev->revision != frame->revision) {
r = compute_radius(x, y, frame);
a = 0;
move_reset(m, FM_X, x, t);
move_reset(m, FM_Y, y, t);
move_reset(m, FM_R, r, t);
move_reset(m, FM_A, a, t);
m->single = 0;
m->multi = 0;
} else if (frame->num_active < 2) {
r = 0;
a = 0;
move_update(m, FM_X, x, t);
move_update(m, FM_Y, y, t);
move_reset(m, FM_R, r, t);
move_reset(m, FM_A, a, t);
m->single = 1;
m->multi = 0;
} else {
x = move_filter(&m->fm[FM_X], x);
y = move_filter(&m->fm[FM_Y], y);
r = compute_radius(x, y, frame);
r = move_filter(&m->fm[FM_R], r);
a = m->fm[FM_A].value;
if (!s->is_semi_mt) {
a += compute_rotation(x, y, r, frame);
a = move_filter(&m->fm[FM_A], a);
}
move_update(m, FM_X, x, t);
move_update(m, FM_Y, y, t);
move_update(m, FM_R, r, t);
move_update(m, FM_A, a, t);
m->single = 0;
m->multi = 1;
}
m->ntouch = frame->num_active;
m->time = t;
}
void gru_event(struct grail *ge, int gid,
const struct move_model *m,
const grail_prop_t *prop, int nprop)
{
gin_gid_event(ge, gid, m->fm[FM_X].value, m->fm[FM_Y].value, m->ntouch,
prop, nprop, 0);
}
void gru_end(struct grail *ge, int gid, const struct move_model *m,
const grail_prop_t *prop, int nprop)
{
gin_gid_end(ge, gid, m->fm[FM_X].value, m->fm[FM_Y].value, m->ntouch,
prop, nprop);
}
|