summaryrefslogtreecommitdiff
path: root/src/grail-gestures.c
blob: d79da96f712977bf2ff261d07f80d110aeec61ef (plain)
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
/*****************************************************************************
 *
 * grail - Gesture Recognition And Instantiation Library
 *
 * Copyright (C) 2010 Canonical Ltd.
 *
 * 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/>.
 *
 * Authors:
 *      Henrik Rydberg <rydberg@bitmath.org>
 *
 ****************************************************************************/

#include "grail-recognizer.h"
#include <math.h>

static const float SN_MOVE = 50;
static const float SN_ZOOM = 50;
static const float SN_ROTATE = 200;
static const float BAR_MOVE = 1;
static const float BAR_ZOOM = 2;
static const float BAR_ROTATE = 4;

static void compute_position(float *x, float *y,
			     const struct touch_frame *frame)
{
	int i, n = frame->nactive;
	*x = 0;
	*y = 0;
	if (n < 1)
		return;
	for (i = 0; i < n; i++) {
		const struct touch *t = frame->active[i];
		*x += t->prop[TP_POS_X];
		*y += t->prop[TP_POS_Y];
	}
	*x /= n;
	*y /= n;
}

static float compute_radius(float x, float y,
			    const struct touch_frame *frame)
{
	int i, n = frame->nactive;
	float r = 0, r2 = 0;
	if (n < 2)
		return r;
	for (i = 0; i < n; i++) {
		const struct touch *t = frame->active[i];
		float dx = t->prop[TP_POS_X] - x;
		float dy = t->prop[TP_POS_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 touch_frame *prev,
			      const struct touch_frame *frame)
{
	int i, n = frame->nactive;
	float da = 0, da2 = 0;
	if (n < 2)
		return da;
	for (i = 0; i < n; i++) {
		const struct touch *t = frame->active[i];
		const struct touch *ot = &prev->touch[t->slot];
		float dx = t->prop[TP_POS_X] - x;
		float dy = t->prop[TP_POS_Y] - y;
		float mx = t->prop[TP_POS_X] - ot->prop[TP_POS_X];
		float my = t->prop[TP_POS_Y] - ot->prop[TP_POS_Y];
		da2 += dx * my - dy * mx;
	}
	da2 /= n;
	da = da2 / r;
	return da;
}

static float move_filter(float x, float x0, float fuzz)
{
	if (x > x0 - fuzz / 2 && x < x0 + fuzz / 2)
		return x0;
	if (x > x0 - fuzz && x < x0 + fuzz)
		return (x0 * 3 + x) / 4;
	if (x > x0 - fuzz * 2 && x < x0 + fuzz * 2)
		return (x0 + x) / 2;
	return x;
}

void gru_init_motion(struct grail *ge, const struct touch_dev_caps *caps)
{
	struct gesture_recognizer *gru = ge->gru;
	struct move_model *state = &gru->move;
	float x = caps->info[TP_POS_X].maximum - caps->info[TP_POS_X].minimum;
	float y = caps->info[TP_POS_Y].maximum - caps->info[TP_POS_Y].minimum;
	float r = sqrt(x * x + y * y);
	state->xfuzz = x / SN_MOVE;
	state->yfuzz = y / SN_MOVE;
	state->rfuzz = r / SN_ZOOM;
	state->afuzz = r / SN_ROTATE;
	state->xbar = BAR_MOVE * state->xfuzz;
	state->ybar = BAR_MOVE * state->yfuzz;
	state->rbar = BAR_ZOOM * state->rfuzz;
	state->abar = BAR_ROTATE * state->afuzz;
}

void gru_reset_motion(struct grail *ge,
		      const struct touch_frame *frame)
{
	struct gesture_recognizer *gru = ge->gru;
	struct move_model *state = &gru->move;
	compute_position(&state->x, &state->y, frame);
	state->r = compute_radius(state->x, state->y, frame);
	state->a = 0;
	state->ntouch = frame->nactive;
}

void gru_compute_motion(struct grail *ge,
			const struct touch_frame *frame)
{
	struct gesture_inserter *gin = ge->gin;
	struct gesture_recognizer *gru = ge->gru;
	struct move_model *state = &gru->move;
	float x, y, r, a;
	compute_position(&x, &y, frame);
	x = move_filter(x, state->x, state->xfuzz);
	y = move_filter(y, state->y, state->yfuzz);
	r = compute_radius(x, y, frame);
	r = move_filter(r, state->r, state->rfuzz);
	a = state->a + compute_rotation(x, y, r, &gru->frame, frame);
	a = move_filter(a, state->a, state->afuzz);
	state->x = x;
	state->y = y;
	state->r = r;
	state->a = a;
	state->ntouch = frame->nactive;
}