diff options
| author | Henrik Rydberg <rydberg@euromail.se> | 2010-08-06 21:11:17 +0200 |
|---|---|---|
| committer | Henrik Rydberg <rydberg@euromail.se> | 2010-08-06 21:11:17 +0200 |
| commit | 7db9d8d76ff61c4802ac15559dcea6fa54ff567a (patch) | |
| tree | b67e040c0abeb0ab1fca72754a767dc9ab851461 /src/grail-gestures.c | |
initial bzr repo at rev 27
Diffstat (limited to 'src/grail-gestures.c')
| -rw-r--r-- | src/grail-gestures.c | 188 |
1 files changed, 188 insertions, 0 deletions
diff --git a/src/grail-gestures.c b/src/grail-gestures.c new file mode 100644 index 0000000..e1bf622 --- /dev/null +++ b/src/grail-gestures.c | |||
| @@ -0,0 +1,188 @@ | |||
| 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 | |||
| 28 | static const float SN_MOVE = 200; | ||
| 29 | static const float SN_ZOOM = 200; | ||
| 30 | static const float SN_ROTATE = 200; | ||
| 31 | static const float BAR_MOVE = 4; | ||
| 32 | static const float BAR_ZOOM = 12; | ||
| 33 | static const float BAR_ROTATE = 12; | ||
| 34 | |||
| 35 | static 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 | |||
| 52 | static 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 | |||
| 70 | static 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 | |||
| 92 | static float move_filter(const struct filter_model *m, float val) | ||
| 93 | { | ||
| 94 | if (val > m->val - m->fuzz / 2 && val < m->val + m->fuzz / 2) | ||
| 95 | return m->val; | ||
| 96 | if (val > m->val - m->fuzz && val < m->val + m->fuzz) | ||
| 97 | return (m->val * 3 + val) / 4; | ||
| 98 | if (val > m->val - m->fuzz * 2 && val < m->val + m->fuzz * 2) | ||
| 99 | return (m->val + val) / 2; | ||
| 100 | return val; | ||
| 101 | } | ||
| 102 | |||
| 103 | static int move_reset(struct filter_model *m, float val) | ||
| 104 | { | ||
| 105 | m->delta = 0; | ||
| 106 | m->tickle = 0; | ||
| 107 | m->active = 0; | ||
| 108 | m->val = val; | ||
| 109 | m->orig = val; | ||
| 110 | } | ||
| 111 | |||
| 112 | static int move_update(struct filter_model *m, float val) | ||
| 113 | { | ||
| 114 | m->delta = val - m->val; | ||
| 115 | m->tickle = fabs(m->delta) > 0; | ||
| 116 | m->val = val; | ||
| 117 | if (!m->active && fabs(val - m->orig) > m->bar) | ||
| 118 | m->active = 1; | ||
| 119 | return m->active; | ||
| 120 | } | ||
| 121 | |||
| 122 | void gru_init_motion(struct grail *ge) | ||
| 123 | { | ||
| 124 | struct gesture_recognizer *gru = ge->gru; | ||
| 125 | struct move_model *state = &gru->move; | ||
| 126 | struct grail_coord min, max; | ||
| 127 | grail_get_units(&min, &max, ge); | ||
| 128 | float x = max.x - min.x; | ||
| 129 | float y = max.y - min.y; | ||
| 130 | float r = sqrt(x * x + y * y); | ||
| 131 | state->x.fuzz = x / SN_MOVE; | ||
| 132 | state->y.fuzz = y / SN_MOVE; | ||
| 133 | state->r.fuzz = r / SN_ZOOM; | ||
| 134 | state->a.fuzz = r / SN_ROTATE; | ||
| 135 | state->x.bar = BAR_MOVE * state->x.fuzz; | ||
| 136 | state->y.bar = BAR_MOVE * state->y.fuzz; | ||
| 137 | state->r.bar = BAR_ZOOM * state->r.fuzz; | ||
| 138 | state->a.bar = BAR_ROTATE * state->a.fuzz; | ||
| 139 | state->multi = 0; | ||
| 140 | } | ||
| 141 | |||
| 142 | void gru_motion(struct grail *ge, | ||
| 143 | const struct touch_frame *frame) | ||
| 144 | { | ||
| 145 | struct gesture_recognizer *gru = ge->gru; | ||
| 146 | struct move_model *state = &gru->move; | ||
| 147 | float x, y, r, a; | ||
| 148 | compute_position(&x, &y, frame); | ||
| 149 | if (frame->nactive < 2 || frame->ncreate || frame->ndestroy) { | ||
| 150 | r = compute_radius(x, y, frame); | ||
| 151 | a = 0; | ||
| 152 | move_reset(&state->x, x); | ||
| 153 | move_reset(&state->y, y); | ||
| 154 | move_reset(&state->r, r); | ||
| 155 | move_reset(&state->a, a); | ||
| 156 | state->multi = 0; | ||
| 157 | } else { | ||
| 158 | x = move_filter(&state->x, x); | ||
| 159 | y = move_filter(&state->y, y); | ||
| 160 | r = compute_radius(x, y, frame); | ||
| 161 | r = move_filter(&state->r, r); | ||
| 162 | a = state->a.val + compute_rotation(x, y, r, &gru->frame, frame); | ||
| 163 | //a = move_filter(&state->a, a); | ||
| 164 | move_update(&state->x, x); | ||
| 165 | move_update(&state->y, y); | ||
| 166 | move_update(&state->r, r); | ||
| 167 | move_update(&state->a, a); | ||
| 168 | state->multi = 1; | ||
| 169 | } | ||
| 170 | state->ntouch = frame->nactive; | ||
| 171 | state->time = frame->time; | ||
| 172 | } | ||
| 173 | |||
| 174 | void gru_event(struct grail *ge, int gid, | ||
| 175 | const struct move_model *move, | ||
| 176 | const grail_prop_t *prop, int nprop) | ||
| 177 | { | ||
| 178 | gin_gid_event(ge, gid, | ||
| 179 | move->x.val, move->y.val, move->ntouch, | ||
| 180 | prop, nprop, | ||
| 181 | GRAIL_STATUS_UPDATE); | ||
| 182 | } | ||
| 183 | |||
| 184 | void gru_end(struct grail *ge, int gid, const struct move_model *move) | ||
| 185 | { | ||
| 186 | gin_gid_end(ge, gid, move->x.val, move->y.val, move->ntouch); | ||
| 187 | } | ||
| 188 | |||
