Fixed Dollar Recognition.
authorJim Grandpre <jim.tla@gmail.com>
Sat, 07 Aug 2010 11:32:11 -0400
changeset 4685e0c3b09368a6
parent 4684 f47c2640c667
child 4686 463cd74304b9
Fixed Dollar Recognition.
src/events/SDL_gesture.c
     1.1 --- a/src/events/SDL_gesture.c	Fri Aug 06 01:55:38 2010 -0400
     1.2 +++ b/src/events/SDL_gesture.c	Sat Aug 07 11:32:11 2010 -0400
     1.3 @@ -90,6 +90,15 @@
     1.4  int numGestureTouches = 0;
     1.5  SDL_bool recordAll;
     1.6  
     1.7 +void SDL_PrintPath(Point *path) {
     1.8 +  int i;
     1.9 +  printf("Path:");
    1.10 +  for(i=0;i<DOLLARNPOINTS;i++) {
    1.11 +    printf(" (%f,%f)",path[i].x,path[i].y);
    1.12 +  }
    1.13 +  printf("\n");
    1.14 +}
    1.15 +
    1.16  int SDL_RecordGesture(SDL_TouchID touchId) {
    1.17    int i;
    1.18    if(touchId < 0) recordAll = SDL_TRUE;
    1.19 @@ -283,6 +292,7 @@
    1.20  int dollarNormalize(DollarPath path,Point *points) {
    1.21    int i;
    1.22    //Calculate length if it hasn't already been done
    1.23 +  printf("length: %f\n",path.length);
    1.24    if(path.length <= 0) {
    1.25      for(i=1;i<path.numPoints;i++) {
    1.26        float dx = path.p[i  ].x - 
    1.27 @@ -292,14 +302,16 @@
    1.28        path.length += sqrt(dx*dx+dy*dy);
    1.29      }
    1.30    }
    1.31 -
    1.32 +  printf("New length: %f\n",path.length);
    1.33  
    1.34    //Resample
    1.35    float interval = path.length/(DOLLARNPOINTS - 1);
    1.36 -  float dist = 0;
    1.37 +  float dist = interval;
    1.38  
    1.39    int numPoints = 0;
    1.40 -  Point centroid; centroid.x = 0;centroid.y = 0;
    1.41 +  Point centroid; 
    1.42 +  centroid.x = 0;centroid.y = 0;
    1.43 +  
    1.44    //printf("(%f,%f)\n",path.p[path.numPoints-1].x,path.p[path.numPoints-1].y);
    1.45    for(i = 1;i < path.numPoints;i++) {
    1.46      float d = sqrt((path.p[i-1].x-path.p[i].x)*(path.p[i-1].x-path.p[i].x)+
    1.47 @@ -318,7 +330,14 @@
    1.48      }
    1.49      dist += d;
    1.50    }
    1.51 -  if(numPoints < 1) return 0;
    1.52 +  if(numPoints < DOLLARNPOINTS-1) {
    1.53 +    printf("ERROR: NumPoints = %i\n",numPoints); 
    1.54 +    return 0;
    1.55 +  }
    1.56 +  //copy the last point
    1.57 +  points[DOLLARNPOINTS-1] = path.p[path.numPoints-1];
    1.58 +  numPoints = DOLLARNPOINTS;
    1.59 +
    1.60    centroid.x /= numPoints;
    1.61    centroid.y /= numPoints;
    1.62   
    1.63 @@ -363,6 +382,7 @@
    1.64  	
    1.65  	Point points[DOLLARNPOINTS];
    1.66  	int numPoints = dollarNormalize(path,points);
    1.67 +	SDL_PrintPath(points);
    1.68  	int i;
    1.69  	
    1.70  	int bestDiff = 10000;
    1.71 @@ -467,6 +487,7 @@
    1.72  	inTouch->recording = SDL_FALSE;
    1.73  	Point path[DOLLARNPOINTS];
    1.74  	dollarNormalize(inTouch->dollarPath,path);
    1.75 +	SDL_PrintPath(path);
    1.76  	int index;
    1.77  	if(recordAll) {
    1.78  	  index = SDL_AddDollarGesture(NULL,path);
    1.79 @@ -515,7 +536,11 @@
    1.80        if(path->numPoints < MAXPATHSIZE) {
    1.81  	path->p[path->numPoints].x = inTouch->centroid.x;
    1.82  	path->p[path->numPoints].y = inTouch->centroid.y;
    1.83 -	path->length += sqrt(dx*dx + dy*dy);
    1.84 +	float pathDx = 
    1.85 +	  (path->p[path->numPoints].x-path->p[path->numPoints-1].x);
    1.86 +	float pathDy = 
    1.87 +	  (path->p[path->numPoints].y-path->p[path->numPoints-1].y);
    1.88 +	path->length += sqrt(pathDx*pathDx + pathDy*pathDy);
    1.89  	path->numPoints++;
    1.90        }
    1.91  #endif
    1.92 @@ -526,7 +551,8 @@
    1.93        lastCentroid = inTouch->centroid;
    1.94        
    1.95        inTouch->centroid.x += dx/inTouch->numDownFingers;
    1.96 -      inTouch->centroid.y += dy/inTouch->numDownFingers;    
    1.97 +      inTouch->centroid.y += dy/inTouch->numDownFingers;
    1.98 +      //printf("Centrid : (%f,%f)\n",inTouch->centroid.x,inTouch->centroid.y);
    1.99        if(inTouch->numDownFingers > 1) {
   1.100  	Point lv; //Vector from centroid to last x,y position
   1.101  	Point v; //Vector from centroid to current x,y position