/***************************************************************************** * * grail - Gesture Recognition And Instantiation Library * * Copyright (C) 2010 Canonical Ltd. * Copyright (C) 2010 Henrik Rydberg * * 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 . * ****************************************************************************/ #include "grail-recognizer.h" #include static const float FM_SN[DIM_FM] = { 200, 200, 200, 1000 }; static const float FM_BAR[DIM_FM] = { 50, 50, 40, 50 }; 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(const struct filter_model *m, float val) { if (val > m->val - m->fuzz / 2 && val < m->val + m->fuzz / 2) return m->val; if (val > m->val - m->fuzz && val < m->val + m->fuzz) return (m->val * 3 + val) / 4; if (val > m->val - m->fuzz * 2 && val < m->val + m->fuzz * 2) return (m->val + val) / 2; return val; } static void move_reset(struct move_model *m, int i, float val) { struct filter_model *fm = &m->fm[i]; fm->delta = 0; fm->val = val; fm->orig = val; m->tickle &= ~(1 << i); m->active &= ~(1 << i); } static void move_update(struct move_model *m, int i, float val) { struct filter_model *fm = &m->fm[i]; fm->delta = val - fm->val; fm->val = val; if (fabs(fm->delta) > 0.5) m->tickle |= (1 << i); else m->tickle &= ~(1 << i); if (m->active & (1 << i)) return; fm->delta = val - fm->orig; if (fabs(fm->delta) > fm->bar) m->active |= (1 << i); else fm->delta = 0; } void gru_init_motion(struct grail *ge) { struct gesture_recognizer *gru = ge->gru; struct move_model *m = &gru->move; struct grail_coord min, max; float D[DIM_FM]; int i; grail_get_units(&min, &max, ge); D[FM_X] = max.x - min.x; D[FM_Y] = max.y - min.y; D[FM_R] = sqrt(D[FM_X] * D[FM_X] + D[FM_Y] * D[FM_Y]); D[FM_A] = D[FM_R]; 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]; } } void gru_motion(struct grail *ge, const struct touch_frame *frame) { struct gesture_recognizer *gru = ge->gru; struct move_model *m = &gru->move; float x, y, r, a; compute_position(&x, &y, frame); if (frame->ncreate || frame->ndestroy) { r = compute_radius(x, y, frame); a = 0; move_reset(m, FM_X, x); move_reset(m, FM_Y, y); move_reset(m, FM_R, r); move_reset(m, FM_A, a); m->single = 0; m->multi = 0; } else if (frame->nactive < 2) { r = 0; a = 0; move_update(m, FM_X, x); move_update(m, FM_Y, y); move_reset(m, FM_R, r); move_reset(m, FM_A, a); 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].val; a += compute_rotation(x, y, r, &gru->frame, frame); a = move_filter(&m->fm[FM_A], a); move_update(m, FM_X, x); move_update(m, FM_Y, y); move_update(m, FM_R, r); move_update(m, FM_A, a); m->single = 0; m->multi = 1; } m->ntouch = frame->nactive; m->time = frame->time; } 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].val, m->fm[FM_Y].val, m->ntouch, prop, nprop, 0); } void gru_end(struct grail *ge, int gid, const struct move_model *m) { gin_gid_end(ge, gid, m->fm[FM_X].val, m->fm[FM_Y].val, m->ntouch); }