src/events/SDL_gesture.c
changeset 4868 d6adaafcfb10
parent 4865 fff50e86c891
child 4919 716b2cbf4c9e
     1.1 --- a/src/events/SDL_gesture.c	Sun Aug 29 12:00:09 2010 -0700
     1.2 +++ b/src/events/SDL_gesture.c	Sun Aug 29 14:22:22 2010 -0700
     1.3 @@ -44,7 +44,6 @@
     1.4  
     1.5  #define ENABLE_DOLLAR
     1.6  
     1.7 -//PHI = ((sqrt(5)-1)/2)
     1.8  #define PHI 0.618033989 
     1.9  
    1.10  typedef struct {
    1.11 @@ -80,7 +79,8 @@
    1.12  int SDL_numGestureTouches = 0;
    1.13  SDL_bool recordAll;
    1.14  
    1.15 -void SDL_PrintPath(SDL_FloatPoint *path) {
    1.16 +#if 0
    1.17 +static void PrintPath(SDL_FloatPoint *path) {
    1.18    int i;
    1.19    printf("Path:");
    1.20    for(i=0;i<DOLLARNPOINTS;i++) {
    1.21 @@ -88,6 +88,7 @@
    1.22    }
    1.23    printf("\n");
    1.24  }
    1.25 +#endif
    1.26  
    1.27  int SDL_RecordGesture(SDL_TouchID touchId) {
    1.28    int i;
    1.29 @@ -177,7 +178,7 @@
    1.30  
    1.31      templ = 
    1.32  	&inTouch->dollarTemplate[inTouch->numDollarTemplates];
    1.33 -      memcpy(templ->path,path,DOLLARNPOINTS*sizeof(SDL_FloatPoint));
    1.34 +      SDL_memcpy(templ->path,path,DOLLARNPOINTS*sizeof(SDL_FloatPoint));
    1.35        templ->hash = SDL_HashDollar(templ->path);
    1.36        inTouch->numDollarTemplates++;    
    1.37      }
    1.38 @@ -196,7 +197,7 @@
    1.39  
    1.40      templ = 
    1.41        &inTouch->dollarTemplate[inTouch->numDollarTemplates];
    1.42 -    memcpy(templ->path,path,DOLLARNPOINTS*sizeof(SDL_FloatPoint));
    1.43 +    SDL_memcpy(templ->path,path,DOLLARNPOINTS*sizeof(SDL_FloatPoint));
    1.44      templ->hash = SDL_HashDollar(templ->path);
    1.45      inTouch->numDollarTemplates++;
    1.46      return inTouch->numDollarTemplates - 1;
    1.47 @@ -222,14 +223,14 @@
    1.48         DOLLARNPOINTS) break;
    1.49  
    1.50      if(touchId >= 0) {
    1.51 -      printf("Adding loaded gesture to 1 touch\n");
    1.52 +      //printf("Adding loaded gesture to 1 touch\n");
    1.53        if(SDL_AddDollarGesture(touch,templ.path)) loaded++;
    1.54      }
    1.55      else {
    1.56 -      printf("Adding to: %i touches\n",SDL_numGestureTouches);
    1.57 +      //printf("Adding to: %i touches\n",SDL_numGestureTouches);
    1.58        for(i = 0;i < SDL_numGestureTouches; i++) {
    1.59  	touch = &SDL_gestureTouch[i];
    1.60 -	printf("Adding loaded gesture to + touches\n");
    1.61 +	//printf("Adding loaded gesture to + touches\n");
    1.62  	//TODO: What if this fails?
    1.63  	SDL_AddDollarGesture(touch,templ.path);	
    1.64        }
    1.65 @@ -247,9 +248,9 @@
    1.66    SDL_FloatPoint p;
    1.67    int i;
    1.68    for(i = 0; i < DOLLARNPOINTS; i++) {
    1.69 -    p.x = (float)(points[i].x * cos(ang) - points[i].y * sin(ang));
    1.70 -    p.y = (float)(points[i].x * sin(ang) + points[i].y * cos(ang));
    1.71 -    dist += (float)(sqrt((p.x-templ[i].x)*(p.x-templ[i].x)+
    1.72 +    p.x = (float)(points[i].x * SDL_cos(ang) - points[i].y * SDL_sin(ang));
    1.73 +    p.y = (float)(points[i].x * SDL_sin(ang) + points[i].y * SDL_cos(ang));
    1.74 +    dist += (float)(SDL_sqrt((p.x-templ[i].x)*(p.x-templ[i].x)+
    1.75  		 (p.y-templ[i].y)*(p.y-templ[i].y)));
    1.76    }
    1.77    return dist/DOLLARNPOINTS;
    1.78 @@ -267,7 +268,7 @@
    1.79    float f1 = dollarDifference(points,templ,x1);
    1.80    float x2 = (float)((1-PHI)*ta + PHI*tb);
    1.81    float f2 = dollarDifference(points,templ,x2);
    1.82 -  while(fabs(ta-tb) > dt) {
    1.83 +  while(SDL_fabs(ta-tb) > dt) {
    1.84      if(f1 < f2) {
    1.85        tb = x2;
    1.86        x2 = x1;
    1.87 @@ -310,7 +311,7 @@
    1.88  	         path.p[i-1].x;
    1.89        float dy = path.p[i  ].y - 
    1.90  	         path.p[i-1].y;
    1.91 -      path.length += (float)(sqrt(dx*dx+dy*dy));
    1.92 +      path.length += (float)(SDL_sqrt(dx*dx+dy*dy));
    1.93      }
    1.94    }
    1.95  
    1.96 @@ -322,7 +323,7 @@
    1.97    
    1.98    //printf("(%f,%f)\n",path.p[path.numPoints-1].x,path.p[path.numPoints-1].y);
    1.99    for(i = 1;i < path.numPoints;i++) {
   1.100 -    float d = (float)(sqrt((path.p[i-1].x-path.p[i].x)*(path.p[i-1].x-path.p[i].x)+
   1.101 +    float d = (float)(SDL_sqrt((path.p[i-1].x-path.p[i].x)*(path.p[i-1].x-path.p[i].x)+
   1.102  		             (path.p[i-1].y-path.p[i].y)*(path.p[i-1].y-path.p[i].y)));
   1.103      //printf("d = %f dist = %f/%f\n",d,dist,interval);
   1.104      while(dist + d > interval) {
   1.105 @@ -339,7 +340,7 @@
   1.106      dist += d;
   1.107    }
   1.108    if(numPoints < DOLLARNPOINTS-1) {
   1.109 -    printf("ERROR: NumPoints = %i\n",numPoints); 
   1.110 +    SDL_SetError("ERROR: NumPoints = %i\n",numPoints); 
   1.111      return 0;
   1.112    }
   1.113    //copy the last point
   1.114 @@ -356,16 +357,16 @@
   1.115    ymin = centroid.y;
   1.116    ymax = centroid.y;
   1.117    
   1.118 -  ang = (float)(atan2(centroid.y - points[0].y,
   1.119 +  ang = (float)(SDL_atan2(centroid.y - points[0].y,
   1.120  		    centroid.x - points[0].x));
   1.121  
   1.122    for(i = 0;i<numPoints;i++) {					       
   1.123      float px = points[i].x;
   1.124      float py = points[i].y;
   1.125 -    points[i].x = (float)((px - centroid.x)*cos(ang) - 
   1.126 -                  (py - centroid.y)*sin(ang) + centroid.x);
   1.127 -    points[i].y = (float)((px - centroid.x)*sin(ang) + 
   1.128 -                  (py - centroid.y)*cos(ang) + centroid.y);
   1.129 +    points[i].x = (float)((px - centroid.x)*SDL_cos(ang) - 
   1.130 +                  (py - centroid.y)*SDL_sin(ang) + centroid.x);
   1.131 +    points[i].y = (float)((px - centroid.x)*SDL_sin(ang) + 
   1.132 +                  (py - centroid.y)*SDL_cos(ang) + centroid.y);
   1.133  
   1.134  
   1.135      if(points[i].x < xmin) xmin = points[i].x;
   1.136 @@ -389,10 +390,10 @@
   1.137  	
   1.138  	SDL_FloatPoint points[DOLLARNPOINTS];
   1.139  	int numPoints = dollarNormalize(path,points);
   1.140 -	//SDL_PrintPath(points);
   1.141  	int i;
   1.142 -	
   1.143  	float bestDiff = 10000;
   1.144 +
   1.145 +	//PrintPath(points);
   1.146  	*bestTempl = -1;
   1.147  	for(i = 0;i < touch->numDollarTemplates;i++) {
   1.148  		float diff = bestDollarDifference(points,touch->dollarTemplate[i].path);
   1.149 @@ -430,10 +431,10 @@
   1.150  
   1.151  int SDL_GestureRemoveTouch(SDL_TouchID id) {
   1.152    int i;
   1.153 -  for(i = 0;i < SDL_numGestureTouches; i++) {
   1.154 -    if(SDL_gestureTouch[i].id == id) {
   1.155 +  for (i = 0; i < SDL_numGestureTouches; i++) {
   1.156 +    if (SDL_gestureTouch[i].id == id) {
   1.157        SDL_numGestureTouches--;
   1.158 -      SDL_gestureTouch[i] = SDL_gestureTouch[SDL_numGestureTouches];
   1.159 +      SDL_memcpy(&SDL_gestureTouch[i], &SDL_gestureTouch[SDL_numGestureTouches], sizeof(SDL_gestureTouch[i]));
   1.160        return 1;
   1.161      }
   1.162    }
   1.163 @@ -528,7 +529,7 @@
   1.164        if(inTouch->recording) {
   1.165  	inTouch->recording = SDL_FALSE;	
   1.166  	dollarNormalize(inTouch->dollarPath,path);
   1.167 -	//SDL_PrintPath(path);
   1.168 +	//PrintPath(path);
   1.169  	if(recordAll) {
   1.170  	  index = SDL_AddDollarGesture(NULL,path);
   1.171  	  for(i = 0;i < SDL_numGestureTouches; i++)
   1.172 @@ -579,7 +580,7 @@
   1.173  	  (path->p[path->numPoints].x-path->p[path->numPoints-1].x);
   1.174  	pathDy = 
   1.175  	  (path->p[path->numPoints].y-path->p[path->numPoints-1].y);
   1.176 -	path->length += (float)sqrt(pathDx*pathDx + pathDy*pathDy);
   1.177 +	path->length += (float)SDL_sqrt(pathDx*pathDx + pathDy*pathDy);
   1.178  	path->numPoints++;
   1.179        }
   1.180  #endif
   1.181 @@ -596,20 +597,20 @@
   1.182  	//lv = inTouch->gestureLast[j].cv;
   1.183  	lv.x = lastP.x - lastCentroid.x;
   1.184  	lv.y = lastP.y - lastCentroid.y;
   1.185 -	lDist = (float)sqrt(lv.x*lv.x + lv.y*lv.y);
   1.186 +	lDist = (float)SDL_sqrt(lv.x*lv.x + lv.y*lv.y);
   1.187  	//printf("lDist = %f\n",lDist);
   1.188  	v.x = x - inTouch->centroid.x;
   1.189  	v.y = y - inTouch->centroid.y;
   1.190  	//inTouch->gestureLast[j].cv = v;
   1.191 -	Dist = (float)sqrt(v.x*v.x+v.y*v.y);
   1.192 -	// cos(dTheta) = (v . lv)/(|v| * |lv|)
   1.193 +	Dist = (float)SDL_sqrt(v.x*v.x+v.y*v.y);
   1.194 +	// SDL_cos(dTheta) = (v . lv)/(|v| * |lv|)
   1.195  	
   1.196  	//Normalize Vectors to simplify angle calculation
   1.197  	lv.x/=lDist;
   1.198  	lv.y/=lDist;
   1.199  	v.x/=Dist;
   1.200  	v.y/=Dist;
   1.201 -	dtheta = (float)atan2(lv.x*v.y - lv.y*v.x,lv.x*v.x + lv.y*v.y);
   1.202 +	dtheta = (float)SDL_atan2(lv.x*v.y - lv.y*v.x,lv.x*v.x + lv.y*v.y);
   1.203  	
   1.204  	dDist = (Dist - lDist);
   1.205  	if(lDist == 0) {dDist = 0;dtheta = 0;} //To avoid impossible values