summaryrefslogtreecommitdiff
path: root/src/grail-gestures.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/grail-gestures.c')
-rw-r--r--src/grail-gestures.c150
1 files changed, 150 insertions, 0 deletions
diff --git a/src/grail-gestures.c b/src/grail-gestures.c
new file mode 100644
index 0000000..d79da96
--- /dev/null
+++ b/src/grail-gestures.c
@@ -0,0 +1,150 @@
1/*****************************************************************************
2 *
3 * grail - Gesture Recognition And Instantiation Library
4 *
5 * Copyright (C) 2010 Canonical Ltd.
6 *
7 * This program is free software: you can redistribute it and/or modify it
8 * under the terms of the GNU General Public License as published by the
9 * Free Software Foundation, either version 3 of the License, or (at your
10 * option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful, but
13 * WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License along
18 * with this program. If not, see <http://www.gnu.org/licenses/>.
19 *
20 * Authors:
21 * Henrik Rydberg <rydberg@bitmath.org>
22 *
23 ****************************************************************************/
24
25#include "grail-recognizer.h"
26#include <math.h>
27
28static const float SN_MOVE = 50;
29static const float SN_ZOOM = 50;
30static const float SN_ROTATE = 200;
31static const float BAR_MOVE = 1;
32static const float BAR_ZOOM = 2;
33static const float BAR_ROTATE = 4;
34
35static void compute_position(float *x, float *y,
36 const struct touch_frame *frame)
37{
38 int i, n = frame->nactive;
39 *x = 0;
40 *y = 0;
41 if (n < 1)
42 return;
43 for (i = 0; i < n; i++) {
44 const struct touch *t = frame->active[i];
45 *x += t->prop[TP_POS_X];
46 *y += t->prop[TP_POS_Y];
47 }
48 *x /= n;
49 *y /= n;
50}
51
52static float compute_radius(float x, float y,
53 const struct touch_frame *frame)
54{
55 int i, n = frame->nactive;
56 float r = 0, r2 = 0;
57 if (n < 2)
58 return r;
59 for (i = 0; i < n; i++) {
60 const struct touch *t = frame->active[i];
61 float dx = t->prop[TP_POS_X] - x;
62 float dy = t->prop[TP_POS_Y] - y;
63 r2 += dx * dx + dy * dy;
64 }
65 r2 /= n;
66 r = sqrt(r2);
67 return r;
68}
69
70static float compute_rotation(float x, float y, float r,
71 const struct touch_frame *prev,
72 const struct touch_frame *frame)
73{
74 int i, n = frame->nactive;
75 float da = 0, da2 = 0;
76 if (n < 2)
77 return da;
78 for (i = 0; i < n; i++) {
79 const struct touch *t = frame->active[i];
80 const struct touch *ot = &prev->touch[t->slot];
81 float dx = t->prop[TP_POS_X] - x;
82 float dy = t->prop[TP_POS_Y] - y;
83 float mx = t->prop[TP_POS_X] - ot->prop[TP_POS_X];
84 float my = t->prop[TP_POS_Y] - ot->prop[TP_POS_Y];
85 da2 += dx * my - dy * mx;
86 }
87 da2 /= n;
88 da = da2 / r;
89 return da;
90}
91
92static float move_filter(float x, float x0, float fuzz)
93{
94 if (x > x0 - fuzz / 2 && x < x0 + fuzz / 2)
95 return x0;
96 if (x > x0 - fuzz && x < x0 + fuzz)
97 return (x0 * 3 + x) / 4;
98 if (x > x0 - fuzz * 2 && x < x0 + fuzz * 2)
99 return (x0 + x) / 2;
100 return x;
101}
102
103void gru_init_motion(struct grail *ge, const struct touch_dev_caps *caps)
104{
105 struct gesture_recognizer *gru = ge->gru;
106 struct move_model *state = &gru->move;
107 float x = caps->info[TP_POS_X].maximum - caps->info[TP_POS_X].minimum;
108 float y = caps->info[TP_POS_Y].maximum - caps->info[TP_POS_Y].minimum;
109 float r = sqrt(x * x + y * y);
110 state->xfuzz = x / SN_MOVE;
111 state->yfuzz = y / SN_MOVE;
112 state->rfuzz = r / SN_ZOOM;
113 state->afuzz = r / SN_ROTATE;
114 state->xbar = BAR_MOVE * state->xfuzz;
115 state->ybar = BAR_MOVE * state->yfuzz;
116 state->rbar = BAR_ZOOM * state->rfuzz;
117 state->abar = BAR_ROTATE * state->afuzz;
118}
119
120void gru_reset_motion(struct grail *ge,
121 const struct touch_frame *frame)
122{
123 struct gesture_recognizer *gru = ge->gru;
124 struct move_model *state = &gru->move;
125 compute_position(&state->x, &state->y, frame);
126 state->r = compute_radius(state->x, state->y, frame);
127 state->a = 0;
128 state->ntouch = frame->nactive;
129}
130
131void gru_compute_motion(struct grail *ge,
132 const struct touch_frame *frame)
133{
134 struct gesture_inserter *gin = ge->gin;
135 struct gesture_recognizer *gru = ge->gru;
136 struct move_model *state = &gru->move;
137 float x, y, r, a;
138 compute_position(&x, &y, frame);
139 x = move_filter(x, state->x, state->xfuzz);
140 y = move_filter(y, state->y, state->yfuzz);
141 r = compute_radius(x, y, frame);
142 r = move_filter(r, state->r, state->rfuzz);
143 a = state->a + compute_rotation(x, y, r, &gru->frame, frame);
144 a = move_filter(a, state->a, state->afuzz);
145 state->x = x;
146 state->y = y;
147 state->r = r;
148 state->a = a;
149 state->ntouch = frame->nactive;
150}