src/events/SDL_gesture.c
changeset 4663 56a2d70de945
parent 4659 063b9455bd1a
child 4665 c2493813a2f4
     1.1 --- a/src/events/SDL_gesture.c	Tue Jul 13 18:31:09 2010 -0400
     1.2 +++ b/src/events/SDL_gesture.c	Thu Jul 15 06:51:16 2010 -0700
     1.3 @@ -110,13 +110,14 @@
     1.4    return hash;
     1.5  }
     1.6  
     1.7 -int SaveTemplate(DollarTemplate *templ, FILE *fp) {
     1.8 +static int SaveTemplate(DollarTemplate *templ, FILE *fp) {
     1.9    int i;
    1.10    fprintf(fp,"%lu ",templ->hash);
    1.11    for(i = 0;i < DOLLARNPOINTS;i++) {
    1.12      fprintf(fp,"%i %i ",(int)templ->path[i].x,(int)templ->path[i].y);
    1.13    }
    1.14    fprintf(fp,"\n");
    1.15 +  return 0;
    1.16  }
    1.17  
    1.18  
    1.19 @@ -141,6 +142,36 @@
    1.20        }
    1.21      }
    1.22    }
    1.23 +  SDL_SetError("Unknown gestureId");
    1.24 +  return -1;
    1.25 +}
    1.26 +
    1.27 +//path is an already sampled set of points
    1.28 +//Returns the index of the gesture on success, or -1
    1.29 +static int SDL_AddDollarGesture(GestureTouch* inTouch,Point* path) {
    1.30 +  if(inTouch == NULL) {
    1.31 +    if(numGestureTouches == 0) return -1;
    1.32 +    int i = 0;
    1.33 +    for(i = 0;i < numGestureTouches; i++) {
    1.34 +      inTouch = &gestureTouch[i];
    1.35 +      if(inTouch->numDollarTemplates < MAXTEMPLATES) {
    1.36 +	DollarTemplate *templ = 
    1.37 +	  &inTouch->dollarTemplate[inTouch->numDollarTemplates];
    1.38 +	memcpy(templ->path,path,DOLLARNPOINTS*sizeof(Point));
    1.39 +	templ->hash = SDL_HashDollar(templ->path);
    1.40 +	inTouch->numDollarTemplates++;
    1.41 +      }
    1.42 +    }
    1.43 +    return inTouch->numDollarTemplates - 1;
    1.44 +  }else if(inTouch->numDollarTemplates < MAXTEMPLATES) {
    1.45 +    DollarTemplate *templ = 
    1.46 +      &inTouch->dollarTemplate[inTouch->numDollarTemplates];
    1.47 +    memcpy(templ->path,path,DOLLARNPOINTS*sizeof(Point));
    1.48 +    templ->hash = SDL_HashDollar(templ->path);
    1.49 +    inTouch->numDollarTemplates++;
    1.50 +    return inTouch->numDollarTemplates - 1;
    1.51 +  }
    1.52 +  return -1;
    1.53  }
    1.54  
    1.55  int SDL_LoadDollarTemplates(int touchId, FILE *fp) {
    1.56 @@ -182,37 +213,6 @@
    1.57  }
    1.58  
    1.59  
    1.60 -//path is an already sampled set of points
    1.61 -//Returns the index of the gesture on success, or -1
    1.62 -int SDL_AddDollarGesture(GestureTouch* inTouch,Point* path) {
    1.63 -  if(inTouch == NULL) {
    1.64 -    if(numGestureTouches == 0) return -1;
    1.65 -    int i = 0;
    1.66 -    for(i = 0;i < numGestureTouches; i++) {
    1.67 -      inTouch = &gestureTouch[i];
    1.68 -      if(inTouch->numDollarTemplates < MAXTEMPLATES) {
    1.69 -	DollarTemplate *templ = 
    1.70 -	  &inTouch->dollarTemplate[inTouch->numDollarTemplates];
    1.71 -	memcpy(templ->path,path,DOLLARNPOINTS*sizeof(Point));
    1.72 -	templ->hash = SDL_HashDollar(templ->path);
    1.73 -	inTouch->numDollarTemplates++;
    1.74 -      }
    1.75 -    }
    1.76 -    return inTouch->numDollarTemplates - 1;
    1.77 -  }else if(inTouch->numDollarTemplates < MAXTEMPLATES) {
    1.78 -    DollarTemplate *templ = 
    1.79 -      &inTouch->dollarTemplate[inTouch->numDollarTemplates];
    1.80 -    memcpy(templ->path,path,DOLLARNPOINTS*sizeof(Point));
    1.81 -    templ->hash = SDL_HashDollar(templ->path);
    1.82 -    inTouch->numDollarTemplates++;
    1.83 -    return inTouch->numDollarTemplates - 1;
    1.84 -  }
    1.85 -  return -1;
    1.86 -}
    1.87 -
    1.88 -
    1.89 -
    1.90 -
    1.91  float dollarDifference(Point* points,Point* templ,float ang) {
    1.92    //  Point p[DOLLARNPOINTS];
    1.93    float dist = 0;
    1.94 @@ -264,21 +264,6 @@
    1.95    return SDL_min(f1,f2);  
    1.96  }
    1.97  
    1.98 -float dollarRecognize(DollarPath path,int *bestTempl,GestureTouch* touch) {
    1.99 -
   1.100 -  Point points[DOLLARNPOINTS];
   1.101 -  int numPoints = dollarNormalize(path,points);
   1.102 -  int i;
   1.103 - 
   1.104 -  int bestDiff = 10000;
   1.105 -  *bestTempl = -1;
   1.106 -  for(i = 0;i < touch->numDollarTemplates;i++) {
   1.107 -    int diff = bestDollarDifference(points,touch->dollarTemplate[i].path);
   1.108 -    if(diff < bestDiff) {bestDiff = diff; *bestTempl = i;}
   1.109 -  }
   1.110 -  return bestDiff;
   1.111 -}
   1.112 -
   1.113  //DollarPath contains raw points, plus (possibly) the calculated length
   1.114  int dollarNormalize(DollarPath path,Point *points) {
   1.115    int i;
   1.116 @@ -359,6 +344,21 @@
   1.117    return numPoints;
   1.118  }
   1.119  
   1.120 +float dollarRecognize(DollarPath path,int *bestTempl,GestureTouch* touch) {
   1.121 +	
   1.122 +	Point points[DOLLARNPOINTS];
   1.123 +	int numPoints = dollarNormalize(path,points);
   1.124 +	int i;
   1.125 +	
   1.126 +	int bestDiff = 10000;
   1.127 +	*bestTempl = -1;
   1.128 +	for(i = 0;i < touch->numDollarTemplates;i++) {
   1.129 +		int diff = bestDollarDifference(points,touch->dollarTemplate[i].path);
   1.130 +		if(diff < bestDiff) {bestDiff = diff; *bestTempl = i;}
   1.131 +	}
   1.132 +	return bestDiff;
   1.133 +}
   1.134 +
   1.135  int SDL_GestureAddTouch(SDL_Touch* touch) { 
   1.136    if(numGestureTouches >= MAXTOUCHES) return -1;
   1.137