comparison src/events/SDL_gesture.c @ 4685:e0c3b09368a6

Fixed Dollar Recognition.
author Jim Grandpre <jim.tla@gmail.com>
date Sat, 07 Aug 2010 11:32:11 -0400
parents f47c2640c667
children 463cd74304b9
comparison
equal deleted inserted replaced
4684:f47c2640c667 4685:e0c3b09368a6
87 } GestureTouch; 87 } GestureTouch;
88 88
89 GestureTouch gestureTouch[MAXTOUCHES]; 89 GestureTouch gestureTouch[MAXTOUCHES];
90 int numGestureTouches = 0; 90 int numGestureTouches = 0;
91 SDL_bool recordAll; 91 SDL_bool recordAll;
92
93 void SDL_PrintPath(Point *path) {
94 int i;
95 printf("Path:");
96 for(i=0;i<DOLLARNPOINTS;i++) {
97 printf(" (%f,%f)",path[i].x,path[i].y);
98 }
99 printf("\n");
100 }
92 101
93 int SDL_RecordGesture(SDL_TouchID touchId) { 102 int SDL_RecordGesture(SDL_TouchID touchId) {
94 int i; 103 int i;
95 if(touchId < 0) recordAll = SDL_TRUE; 104 if(touchId < 0) recordAll = SDL_TRUE;
96 for(i = 0;i < numGestureTouches; i++) { 105 for(i = 0;i < numGestureTouches; i++) {
281 290
282 //DollarPath contains raw points, plus (possibly) the calculated length 291 //DollarPath contains raw points, plus (possibly) the calculated length
283 int dollarNormalize(DollarPath path,Point *points) { 292 int dollarNormalize(DollarPath path,Point *points) {
284 int i; 293 int i;
285 //Calculate length if it hasn't already been done 294 //Calculate length if it hasn't already been done
295 printf("length: %f\n",path.length);
286 if(path.length <= 0) { 296 if(path.length <= 0) {
287 for(i=1;i<path.numPoints;i++) { 297 for(i=1;i<path.numPoints;i++) {
288 float dx = path.p[i ].x - 298 float dx = path.p[i ].x -
289 path.p[i-1].x; 299 path.p[i-1].x;
290 float dy = path.p[i ].y - 300 float dy = path.p[i ].y -
291 path.p[i-1].y; 301 path.p[i-1].y;
292 path.length += sqrt(dx*dx+dy*dy); 302 path.length += sqrt(dx*dx+dy*dy);
293 } 303 }
294 } 304 }
295 305 printf("New length: %f\n",path.length);
296 306
297 //Resample 307 //Resample
298 float interval = path.length/(DOLLARNPOINTS - 1); 308 float interval = path.length/(DOLLARNPOINTS - 1);
299 float dist = 0; 309 float dist = interval;
300 310
301 int numPoints = 0; 311 int numPoints = 0;
302 Point centroid; centroid.x = 0;centroid.y = 0; 312 Point centroid;
313 centroid.x = 0;centroid.y = 0;
314
303 //printf("(%f,%f)\n",path.p[path.numPoints-1].x,path.p[path.numPoints-1].y); 315 //printf("(%f,%f)\n",path.p[path.numPoints-1].x,path.p[path.numPoints-1].y);
304 for(i = 1;i < path.numPoints;i++) { 316 for(i = 1;i < path.numPoints;i++) {
305 float d = sqrt((path.p[i-1].x-path.p[i].x)*(path.p[i-1].x-path.p[i].x)+ 317 float d = sqrt((path.p[i-1].x-path.p[i].x)*(path.p[i-1].x-path.p[i].x)+
306 (path.p[i-1].y-path.p[i].y)*(path.p[i-1].y-path.p[i].y)); 318 (path.p[i-1].y-path.p[i].y)*(path.p[i-1].y-path.p[i].y));
307 //printf("d = %f dist = %f/%f\n",d,dist,interval); 319 //printf("d = %f dist = %f/%f\n",d,dist,interval);
316 328
317 dist -= interval; 329 dist -= interval;
318 } 330 }
319 dist += d; 331 dist += d;
320 } 332 }
321 if(numPoints < 1) return 0; 333 if(numPoints < DOLLARNPOINTS-1) {
334 printf("ERROR: NumPoints = %i\n",numPoints);
335 return 0;
336 }
337 //copy the last point
338 points[DOLLARNPOINTS-1] = path.p[path.numPoints-1];
339 numPoints = DOLLARNPOINTS;
340
322 centroid.x /= numPoints; 341 centroid.x /= numPoints;
323 centroid.y /= numPoints; 342 centroid.y /= numPoints;
324 343
325 //printf("Centroid (%f,%f)",centroid.x,centroid.y); 344 //printf("Centroid (%f,%f)",centroid.x,centroid.y);
326 //Rotate Points so point 0 is left of centroid and solve for the bounding box 345 //Rotate Points so point 0 is left of centroid and solve for the bounding box
361 380
362 float dollarRecognize(DollarPath path,int *bestTempl,GestureTouch* touch) { 381 float dollarRecognize(DollarPath path,int *bestTempl,GestureTouch* touch) {
363 382
364 Point points[DOLLARNPOINTS]; 383 Point points[DOLLARNPOINTS];
365 int numPoints = dollarNormalize(path,points); 384 int numPoints = dollarNormalize(path,points);
385 SDL_PrintPath(points);
366 int i; 386 int i;
367 387
368 int bestDiff = 10000; 388 int bestDiff = 10000;
369 *bestTempl = -1; 389 *bestTempl = -1;
370 for(i = 0;i < touch->numDollarTemplates;i++) { 390 for(i = 0;i < touch->numDollarTemplates;i++) {
465 #ifdef ENABLE_DOLLAR 485 #ifdef ENABLE_DOLLAR
466 if(inTouch->recording) { 486 if(inTouch->recording) {
467 inTouch->recording = SDL_FALSE; 487 inTouch->recording = SDL_FALSE;
468 Point path[DOLLARNPOINTS]; 488 Point path[DOLLARNPOINTS];
469 dollarNormalize(inTouch->dollarPath,path); 489 dollarNormalize(inTouch->dollarPath,path);
490 SDL_PrintPath(path);
470 int index; 491 int index;
471 if(recordAll) { 492 if(recordAll) {
472 index = SDL_AddDollarGesture(NULL,path); 493 index = SDL_AddDollarGesture(NULL,path);
473 int i; 494 int i;
474 for(i = 0;i < numGestureTouches; i++) 495 for(i = 0;i < numGestureTouches; i++)
513 #ifdef ENABLE_DOLLAR 534 #ifdef ENABLE_DOLLAR
514 DollarPath* path = &inTouch->dollarPath; 535 DollarPath* path = &inTouch->dollarPath;
515 if(path->numPoints < MAXPATHSIZE) { 536 if(path->numPoints < MAXPATHSIZE) {
516 path->p[path->numPoints].x = inTouch->centroid.x; 537 path->p[path->numPoints].x = inTouch->centroid.x;
517 path->p[path->numPoints].y = inTouch->centroid.y; 538 path->p[path->numPoints].y = inTouch->centroid.y;
518 path->length += sqrt(dx*dx + dy*dy); 539 float pathDx =
540 (path->p[path->numPoints].x-path->p[path->numPoints-1].x);
541 float pathDy =
542 (path->p[path->numPoints].y-path->p[path->numPoints-1].y);
543 path->length += sqrt(pathDx*pathDx + pathDy*pathDy);
519 path->numPoints++; 544 path->numPoints++;
520 } 545 }
521 #endif 546 #endif
522 Point lastP; 547 Point lastP;
523 lastP.x = x - dx; 548 lastP.x = x - dx;
524 lastP.y = y - dy; 549 lastP.y = y - dy;
525 Point lastCentroid; 550 Point lastCentroid;
526 lastCentroid = inTouch->centroid; 551 lastCentroid = inTouch->centroid;
527 552
528 inTouch->centroid.x += dx/inTouch->numDownFingers; 553 inTouch->centroid.x += dx/inTouch->numDownFingers;
529 inTouch->centroid.y += dy/inTouch->numDownFingers; 554 inTouch->centroid.y += dy/inTouch->numDownFingers;
555 //printf("Centrid : (%f,%f)\n",inTouch->centroid.x,inTouch->centroid.y);
530 if(inTouch->numDownFingers > 1) { 556 if(inTouch->numDownFingers > 1) {
531 Point lv; //Vector from centroid to last x,y position 557 Point lv; //Vector from centroid to last x,y position
532 Point v; //Vector from centroid to current x,y position 558 Point v; //Vector from centroid to current x,y position
533 //lv = inTouch->gestureLast[j].cv; 559 //lv = inTouch->gestureLast[j].cv;
534 lv.x = lastP.x - lastCentroid.x; 560 lv.x = lastP.x - lastCentroid.x;