Wed, 22 Jan 2003 13:57:27 +0000
[gaim-migrate @ 4656]
Added a mouse gestures plugin. Yes, you heard me.
| 4390 | 1 | /* |
| 2 | libgstroke - a GNOME stroke interface library | |
| 3 | Copyright (c) 1996,1997,1998,1999,2000,2001 Mark F. Willey, ETLA Technical | |
| 4 | ||
| 5 | See the file COPYING for distribution information. | |
| 6 | ||
| 7 | This file contains the stroke recognition algorithm. | |
| 8 | */ | |
| 9 | ||
| 10 | #include "config.h" | |
| 11 | ||
| 12 | #include <unistd.h> | |
| 13 | #include <stdlib.h> | |
| 14 | #include <stdio.h> | |
| 15 | #include <math.h> | |
| 16 | #include <glib.h> | |
| 17 | #include <gtk/gtk.h> | |
| 18 | #include "gstroke.h" | |
| 19 | #include "gstroke-internal.h" | |
| 20 | ||
| 21 | ||
| 22 | void | |
| 23 | _gstroke_init (struct gstroke_metrics *metrics) | |
| 24 | { | |
| 25 | if (metrics->pointList != NULL) { | |
| 26 | /* FIXME: does this free the data too?*/ | |
| 27 | g_slist_free (metrics->pointList); | |
| 28 | metrics->pointList = NULL; | |
| 29 | metrics->point_count = 0; | |
| 30 | } | |
| 31 | } | |
| 32 | ||
| 33 | /* figure out which bin the point falls in */ | |
| 34 | static gint | |
| 35 | _gstroke_bin (p_point point_p, gint bound_x_1, gint bound_x_2, | |
| 36 | gint bound_y_1, gint bound_y_2) | |
| 37 | { | |
| 38 | ||
| 39 | gint bin_num = 1; | |
| 40 | ||
| 41 | if (point_p->x > bound_x_1) bin_num += 1; | |
| 42 | if (point_p->x > bound_x_2) bin_num += 1; | |
| 43 | if (point_p->y > bound_y_1) bin_num += 3; | |
| 44 | if (point_p->y > bound_y_2) bin_num += 3; | |
| 45 | ||
| 46 | return bin_num; | |
| 47 | } | |
| 48 | ||
| 49 | gint | |
| 50 | _gstroke_trans (gchar *sequence, struct gstroke_metrics *metrics) | |
| 51 | { | |
| 52 | GSList *crt_elem; | |
| 53 | /* number of bins recorded in the stroke */ | |
| 54 | guint sequence_count = 0; | |
| 55 | ||
| 56 | /* points-->sequence translation scratch variables */ | |
| 57 | gint prev_bin = 0; | |
| 58 | gint current_bin = 0; | |
| 59 | gint bin_count = 0; | |
| 60 | ||
| 61 | /* flag indicating the start of a stroke - always count it in the sequence */ | |
| 62 | gint first_bin = TRUE; | |
| 63 | ||
| 64 | /* bin boundary and size variables */ | |
| 65 | gint delta_x, delta_y; | |
| 66 | gint bound_x_1, bound_x_2; | |
| 67 | gint bound_y_1, bound_y_2; | |
| 68 | ||
| 69 | ||
| 70 | /* determine size of grid */ | |
| 71 | delta_x = metrics->max_x - metrics->min_x; | |
| 72 | delta_y = metrics->max_y - metrics->min_y; | |
| 73 | ||
| 74 | /* calculate bin boundary positions */ | |
| 75 | bound_x_1 = metrics->min_x + (delta_x / 3); | |
| 76 | bound_x_2 = metrics->min_x + 2 * (delta_x / 3); | |
| 77 | ||
| 78 | bound_y_1 = metrics->min_y + (delta_y / 3); | |
| 79 | bound_y_2 = metrics->min_y + 2 * (delta_y / 3); | |
| 80 | ||
| 81 | if (delta_x > GSTROKE_SCALE_RATIO * delta_y) { | |
| 82 | bound_y_1 = (metrics->max_y + metrics->min_y - delta_x) / 2 + (delta_x / 3); | |
| 83 | bound_y_2 = (metrics->max_y + metrics->min_y - delta_x) / 2 + 2 * (delta_x / 3); | |
| 84 | } else if (delta_y > GSTROKE_SCALE_RATIO * delta_x) { | |
| 85 | bound_x_1 = (metrics->max_x + metrics->min_x - delta_y) / 2 + (delta_y / 3); | |
| 86 | bound_x_2 = (metrics->max_x + metrics->min_x - delta_y) / 2 + 2 * (delta_y / 3); | |
| 87 | } | |
| 88 | ||
| 89 | #if 0 | |
| 90 | printf ("DEBUG:: point count: %d\n", metrics->point_count); | |
| 91 | printf ("DEBUG:: metrics->min_x: %d\n", metrics->min_x); | |
| 92 | printf ("DEBUG:: metrics->max_x: %d\n", metrics->max_x); | |
| 93 | printf ("DEBUG:: metrics->min_y: %d\n", metrics->min_y); | |
| 94 | printf ("DEBUG:: metrics->max_y: %d\n", metrics->max_y); | |
| 95 | printf ("DEBUG:: delta_x: %d\n", delta_x); | |
| 96 | printf ("DEBUG:: delta_y: %d\n", delta_y); | |
| 97 | printf ("DEBUG:: bound_x_1: %d\n", bound_x_1); | |
| 98 | printf ("DEBUG:: bound_x_2: %d\n", bound_x_2); | |
| 99 | printf ("DEBUG:: bound_y_1: %d\n", bound_y_1); | |
| 100 | printf ("DEBUG:: bound_y_2: %d\n", bound_y_2); | |
| 101 | #endif | |
| 102 | ||
| 103 | /* | |
| 104 | build string by placing points in bins, collapsing bins and | |
| 105 | discarding those with too few points... */ | |
| 106 | ||
| 107 | crt_elem = metrics->pointList; | |
| 108 | while (crt_elem != NULL) | |
| 109 | { | |
| 110 | /* figure out which bin the point falls in */ | |
| 111 | ||
| 112 | /*printf ("X = %d Y = %d\n", ((p_point)crt_elem->data)->x, | |
| 113 | ((p_point)crt_elem->data)->y); */ | |
| 114 | ||
| 115 | ||
| 116 | current_bin = _gstroke_bin ((p_point)crt_elem->data, bound_x_1, | |
| 117 | bound_x_2, bound_y_1, bound_y_2); | |
| 118 | ||
| 119 | /* if this is the first point, consider it the previous bin, too. */ | |
| 120 | if (prev_bin == 0) | |
| 121 | prev_bin = current_bin; | |
| 122 | ||
| 123 | /*printf ("DEBUG:: current bin: %d x=%d y = %d\n", current_bin, | |
| 124 | ((p_point)crt_elem->data)->x, | |
| 125 | ((p_point)crt_elem->data)->y); */ | |
| 126 | ||
| 127 | if (prev_bin == current_bin) | |
| 128 | bin_count++; | |
| 129 | else { | |
| 130 | /* we are moving to a new bin -- consider adding to the sequence */ | |
| 131 | if ((bin_count > (metrics->point_count * GSTROKE_BIN_COUNT_PERCENT)) | |
| 132 | || (first_bin == TRUE)) { | |
| 133 | ||
| 134 | //gchar val = '0' + prev_bin; | |
| 135 | //printf ("%c", val);fflush (stdout); | |
| 136 | //g_string_append (&sequence, &val); | |
| 137 | ||
| 138 | first_bin = FALSE; | |
| 139 | sequence[sequence_count++] = '0' + prev_bin; | |
| 140 | /* printf ("DEBUG:: adding sequence: %d\n", prev_bin); */ | |
| 141 | ||
| 142 | } | |
| 143 | ||
| 144 | /* restart counting points in the new bin */ | |
| 145 | bin_count=0; | |
| 146 | prev_bin = current_bin; | |
| 147 | } | |
| 148 | ||
| 149 | /* move to next point, freeing current point from list */ | |
| 150 | ||
| 151 | free (crt_elem->data); | |
| 152 | crt_elem = g_slist_next (crt_elem); | |
| 153 | } | |
| 154 | /* add the last run of points to the sequence */ | |
| 155 | sequence[sequence_count++] = '0' + current_bin; | |
| 156 | /* printf ("DEBUG:: adding final sequence: %d\n", current_bin); */ | |
| 157 | ||
| 158 | _gstroke_init (metrics); | |
| 159 | ||
| 160 | { | |
| 161 | // FIXME: get rid of this block | |
| 162 | //gchar val = '0' + current_bin; | |
| 163 | //printf ("%c\n", val);fflush (stdout); | |
| 164 | //g_string_append (&sequence, '\0'); | |
| 165 | sequence[sequence_count] = '\0'; | |
| 166 | } | |
| 167 | ||
| 168 | return TRUE; | |
| 169 | } | |
| 170 | ||
| 171 | /* my plan is to make a stroke training program where you can enter all of | |
| 172 | the variations of slop that map to a canonical set of strokes. When the | |
| 173 | application calls gstroke_canonical, it gets one of the recognized strokes, | |
| 174 | or "", if it's not a recognized variation. I will probably use a hash | |
| 175 | table. Right now, it just passes the values through to gstroke_trans */ | |
| 176 | gint | |
| 177 | _gstroke_canonical (gchar *sequence, struct gstroke_metrics *metrics) | |
| 178 | { | |
| 179 | return _gstroke_trans (sequence, metrics); | |
| 180 | } | |
| 181 | ||
| 182 | ||
| 183 | void | |
| 184 | _gstroke_record (gint x, gint y, struct gstroke_metrics *metrics) | |
| 185 | { | |
| 186 | p_point new_point_p; | |
| 187 | gint delx, dely; | |
| 188 | float ix, iy; | |
| 189 | ||
| 190 | #if 0 | |
| 191 | printf ("%d:%d ", x, y); fflush (stdout); | |
| 192 | #endif | |
| 193 | ||
| 194 | if (metrics->point_count < GSTROKE_MAX_POINTS) { | |
| 195 | new_point_p = (p_point) g_malloc (sizeof (struct s_point)); | |
| 196 | ||
| 197 | if (metrics->pointList == NULL) { | |
| 198 | ||
| 199 | /* first point in list - initialize metrics */ | |
| 200 | metrics->min_x = 10000; | |
| 201 | metrics->min_y = 10000; | |
| 202 | metrics->max_x = -1; | |
| 203 | metrics->max_y = -1; | |
| 204 | ||
| 205 | metrics->pointList = (GSList*) g_malloc (sizeof (GSList)); | |
| 206 | ||
| 207 | metrics->pointList->data = new_point_p; | |
| 208 | metrics->pointList->next = NULL; | |
| 209 | metrics->point_count = 0; | |
| 210 | ||
| 211 | } else { | |
| 212 | ||
| 213 | #define LAST_POINT ((p_point)(g_slist_last (metrics->pointList)->data)) | |
| 214 | ||
| 215 | /* interpolate between last and current point */ | |
| 216 | delx = x - LAST_POINT->x; | |
| 217 | dely = y - LAST_POINT->y; | |
| 218 | ||
| 219 | if (abs(delx) > abs(dely)) { /* step by the greatest delta direction */ | |
| 220 | iy = LAST_POINT->y; | |
| 221 | ||
| 222 | /* go from the last point to the current, whatever direction it may be */ | |
| 223 | for (ix = LAST_POINT->x; (delx > 0) ? (ix < x) : (ix > x); ix += (delx > 0) ? 1 : -1) { | |
| 224 | ||
| 225 | /* step the other axis by the correct increment */ | |
| 226 | iy += fabs(((float) dely / (float) delx)) * (float) ((dely < 0) ? -1.0 : 1.0); | |
| 227 | ||
| 228 | /* add the interpolated point */ | |
| 229 | new_point_p->x = ix; | |
| 230 | new_point_p->y = iy; | |
| 231 | g_slist_append (metrics->pointList, new_point_p); | |
| 232 | ||
| 233 | /* update metrics */ | |
| 234 | if (((gint) ix) < metrics->min_x) metrics->min_x = (gint) ix; | |
| 235 | if (((gint) ix) > metrics->max_x) metrics->max_x = (gint) ix; | |
| 236 | if (((gint) iy) < metrics->min_y) metrics->min_y = (gint) iy; | |
| 237 | if (((gint) iy) > metrics->max_y) metrics->max_y = (gint) iy; | |
| 238 | metrics->point_count++; | |
| 239 | ||
| 240 | new_point_p = (p_point) malloc (sizeof(struct s_point)); | |
| 241 | } | |
| 242 | } else { /* same thing, but for dely larger than delx case... */ | |
| 243 | ix = LAST_POINT->x; | |
| 244 | ||
| 245 | /* go from the last point to the current, whatever direction it may be | |
| 246 | */ | |
| 247 | for (iy = LAST_POINT->y; (dely > 0) ? (iy < y) : (iy > y); iy += (dely > 0) ? 1 : -1) { | |
| 248 | ||
| 249 | /* step the other axis by the correct increment */ | |
| 250 | ix += fabs(((float) delx / (float) dely)) * (float) ((delx < 0) ? -1.0 : 1.0); | |
| 251 | ||
| 252 | /* add the interpolated point */ | |
| 253 | new_point_p->y = iy; | |
| 254 | new_point_p->x = ix; | |
| 255 | g_slist_append(metrics->pointList, new_point_p); | |
| 256 | ||
| 257 | /* update metrics */ | |
| 258 | if (((gint) ix) < metrics->min_x) metrics->min_x = (gint) ix; | |
| 259 | if (((gint) ix) > metrics->max_x) metrics->max_x = (gint) ix; | |
| 260 | if (((gint) iy) < metrics->min_y) metrics->min_y = (gint) iy; | |
| 261 | if (((gint) iy) > metrics->max_y) metrics->max_y = (gint) iy; | |
| 262 | metrics->point_count++; | |
| 263 | ||
| 264 | new_point_p = (p_point) malloc (sizeof(struct s_point)); | |
| 265 | } | |
| 266 | } | |
| 267 | ||
| 268 | /* add the sampled point */ | |
| 269 | g_slist_append(metrics->pointList, new_point_p); | |
| 270 | } | |
| 271 | ||
| 272 | /* record the sampled point values */ | |
| 273 | new_point_p->x = x; | |
| 274 | new_point_p->y = y; | |
| 275 | ||
| 276 | #if 0 | |
| 277 | { | |
| 278 | GSList *crt = metrics->pointList; | |
| 279 | printf ("Record "); | |
| 280 | while (crt != NULL) | |
| 281 | { | |
| 282 | printf ("(%d,%d)", ((p_point)crt->data)->x, ((p_point)crt->data)->y); | |
| 283 | crt = g_slist_next (crt); | |
| 284 | } | |
| 285 | printf ("\n"); | |
| 286 | } | |
| 287 | #endif | |
| 288 | } | |
| 289 | } |