| 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 /* |
|
| 135 gchar val = '0' + prev_bin; |
|
| 136 printf ("%c", val);fflush (stdout); |
|
| 137 g_string_append (&sequence, &val); |
|
| 138 */ |
|
| 139 |
|
| 140 first_bin = FALSE; |
|
| 141 sequence[sequence_count++] = '0' + prev_bin; |
|
| 142 /* printf ("DEBUG:: adding sequence: %d\n", prev_bin); */ |
|
| 143 |
|
| 144 } |
|
| 145 |
|
| 146 /* restart counting points in the new bin */ |
|
| 147 bin_count=0; |
|
| 148 prev_bin = current_bin; |
|
| 149 } |
|
| 150 |
|
| 151 /* move to next point, freeing current point from list */ |
|
| 152 |
|
| 153 free (crt_elem->data); |
|
| 154 crt_elem = g_slist_next (crt_elem); |
|
| 155 } |
|
| 156 /* add the last run of points to the sequence */ |
|
| 157 sequence[sequence_count++] = '0' + current_bin; |
|
| 158 /* printf ("DEBUG:: adding final sequence: %d\n", current_bin); */ |
|
| 159 |
|
| 160 _gstroke_init (metrics); |
|
| 161 |
|
| 162 { |
|
| 163 /* FIXME: get rid of this block |
|
| 164 gchar val = '0' + current_bin; |
|
| 165 printf ("%c\n", val);fflush (stdout); |
|
| 166 g_string_append (&sequence, '\0'); |
|
| 167 */ |
|
| 168 sequence[sequence_count] = '\0'; |
|
| 169 } |
|
| 170 |
|
| 171 return TRUE; |
|
| 172 } |
|
| 173 |
|
| 174 /* my plan is to make a stroke training program where you can enter all of |
|
| 175 the variations of slop that map to a canonical set of strokes. When the |
|
| 176 application calls gstroke_canonical, it gets one of the recognized strokes, |
|
| 177 or "", if it's not a recognized variation. I will probably use a hash |
|
| 178 table. Right now, it just passes the values through to gstroke_trans */ |
|
| 179 gint |
|
| 180 _gstroke_canonical (gchar *sequence, struct gstroke_metrics *metrics) |
|
| 181 { |
|
| 182 return _gstroke_trans (sequence, metrics); |
|
| 183 } |
|
| 184 |
|
| 185 |
|
| 186 void |
|
| 187 _gstroke_record (gint x, gint y, struct gstroke_metrics *metrics) |
|
| 188 { |
|
| 189 p_point new_point_p; |
|
| 190 gint delx, dely; |
|
| 191 float ix, iy; |
|
| 192 |
|
| 193 g_return_if_fail( metrics != NULL ); |
|
| 194 |
|
| 195 #if 0 |
|
| 196 printf ("%d:%d ", x, y); fflush (stdout); |
|
| 197 #endif |
|
| 198 |
|
| 199 if (metrics->point_count < GSTROKE_MAX_POINTS) { |
|
| 200 new_point_p = (p_point) g_malloc (sizeof (struct s_point)); |
|
| 201 |
|
| 202 if (metrics->pointList == NULL) { |
|
| 203 |
|
| 204 /* first point in list - initialize metrics */ |
|
| 205 metrics->min_x = 10000; |
|
| 206 metrics->min_y = 10000; |
|
| 207 metrics->max_x = -1; |
|
| 208 metrics->max_y = -1; |
|
| 209 |
|
| 210 metrics->pointList = (GSList*) g_malloc (sizeof (GSList)); |
|
| 211 |
|
| 212 metrics->pointList->data = new_point_p; |
|
| 213 metrics->pointList->next = NULL; |
|
| 214 metrics->point_count = 0; |
|
| 215 |
|
| 216 } else { |
|
| 217 |
|
| 218 #define LAST_POINT ((p_point)(g_slist_last (metrics->pointList)->data)) |
|
| 219 |
|
| 220 /* interpolate between last and current point */ |
|
| 221 delx = x - LAST_POINT->x; |
|
| 222 dely = y - LAST_POINT->y; |
|
| 223 |
|
| 224 if (abs(delx) > abs(dely)) { /* step by the greatest delta direction */ |
|
| 225 iy = LAST_POINT->y; |
|
| 226 |
|
| 227 /* go from the last point to the current, whatever direction it may be */ |
|
| 228 for (ix = LAST_POINT->x; (delx > 0) ? (ix < x) : (ix > x); ix += (delx > 0) ? 1 : -1) { |
|
| 229 |
|
| 230 /* step the other axis by the correct increment */ |
|
| 231 iy += fabs(((float) dely / (float) delx)) * (float) ((dely < 0) ? -1.0 : 1.0); |
|
| 232 |
|
| 233 /* add the interpolated point */ |
|
| 234 new_point_p->x = ix; |
|
| 235 new_point_p->y = iy; |
|
| 236 metrics->pointList = g_slist_append (metrics->pointList, new_point_p); |
|
| 237 |
|
| 238 /* update metrics */ |
|
| 239 if (((gint) ix) < metrics->min_x) metrics->min_x = (gint) ix; |
|
| 240 if (((gint) ix) > metrics->max_x) metrics->max_x = (gint) ix; |
|
| 241 if (((gint) iy) < metrics->min_y) metrics->min_y = (gint) iy; |
|
| 242 if (((gint) iy) > metrics->max_y) metrics->max_y = (gint) iy; |
|
| 243 metrics->point_count++; |
|
| 244 |
|
| 245 new_point_p = (p_point) malloc (sizeof(struct s_point)); |
|
| 246 } |
|
| 247 } else { /* same thing, but for dely larger than delx case... */ |
|
| 248 ix = LAST_POINT->x; |
|
| 249 |
|
| 250 /* go from the last point to the current, whatever direction it may be |
|
| 251 */ |
|
| 252 for (iy = LAST_POINT->y; (dely > 0) ? (iy < y) : (iy > y); iy += (dely > 0) ? 1 : -1) { |
|
| 253 |
|
| 254 /* step the other axis by the correct increment */ |
|
| 255 ix += fabs(((float) delx / (float) dely)) * (float) ((delx < 0) ? -1.0 : 1.0); |
|
| 256 |
|
| 257 /* add the interpolated point */ |
|
| 258 new_point_p->y = iy; |
|
| 259 new_point_p->x = ix; |
|
| 260 metrics->pointList = g_slist_append(metrics->pointList, new_point_p); |
|
| 261 |
|
| 262 /* update metrics */ |
|
| 263 if (((gint) ix) < metrics->min_x) metrics->min_x = (gint) ix; |
|
| 264 if (((gint) ix) > metrics->max_x) metrics->max_x = (gint) ix; |
|
| 265 if (((gint) iy) < metrics->min_y) metrics->min_y = (gint) iy; |
|
| 266 if (((gint) iy) > metrics->max_y) metrics->max_y = (gint) iy; |
|
| 267 metrics->point_count++; |
|
| 268 |
|
| 269 new_point_p = (p_point) malloc (sizeof(struct s_point)); |
|
| 270 } |
|
| 271 } |
|
| 272 |
|
| 273 /* add the sampled point */ |
|
| 274 metrics->pointList = g_slist_append(metrics->pointList, new_point_p); |
|
| 275 } |
|
| 276 |
|
| 277 /* record the sampled point values */ |
|
| 278 new_point_p->x = x; |
|
| 279 new_point_p->y = y; |
|
| 280 |
|
| 281 #if 0 |
|
| 282 { |
|
| 283 GSList *crt = metrics->pointList; |
|
| 284 printf ("Record "); |
|
| 285 while (crt != NULL) |
|
| 286 { |
|
| 287 printf ("(%d,%d)", ((p_point)crt->data)->x, ((p_point)crt->data)->y); |
|
| 288 crt = g_slist_next (crt); |
|
| 289 } |
|
| 290 printf ("\n"); |
|
| 291 } |
|
| 292 #endif |
|
| 293 } |
|
| 294 } |
|