src/events/SDL_gesture.c
changeset 4919 716b2cbf4c9e
parent 4868 d6adaafcfb10
child 4920 a4032241deb5
     1.1 --- a/src/events/SDL_gesture.c	Tue Nov 30 12:38:46 2010 -0800
     1.2 +++ b/src/events/SDL_gesture.c	Tue Nov 30 17:58:51 2010 -0800
     1.3 @@ -97,7 +97,7 @@
     1.4      if((touchId < 0) || (SDL_gestureTouch[i].id == touchId)) {
     1.5        SDL_gestureTouch[i].recording = SDL_TRUE;
     1.6        if(touchId >= 0)
     1.7 -	return 1;
     1.8 +        return 1;
     1.9      }      
    1.10    }
    1.11    return (touchId < 0);
    1.12 @@ -122,7 +122,7 @@
    1.13    //if(SDL_RWops.write(src,&(templ->hash),sizeof(templ->hash),1) != 1) return 0;
    1.14    
    1.15    if(SDL_RWwrite(src,templ->path,
    1.16 -		 sizeof(templ->path[0]),DOLLARNPOINTS) != DOLLARNPOINTS) 
    1.17 +                 sizeof(templ->path[0]),DOLLARNPOINTS) != DOLLARNPOINTS) 
    1.18      return 0;
    1.19  
    1.20    return 1;
    1.21 @@ -134,7 +134,7 @@
    1.22    for(i = 0; i < SDL_numGestureTouches; i++) {
    1.23      SDL_GestureTouch* touch = &SDL_gestureTouch[i];
    1.24      for(j = 0;j < touch->numDollarTemplates; j++) {
    1.25 -	rtrn += SaveTemplate(&touch->dollarTemplate[i],src);
    1.26 +        rtrn += SaveTemplate(&touch->dollarTemplate[i],src);
    1.27      }
    1.28    }
    1.29    return rtrn;  
    1.30 @@ -146,7 +146,7 @@
    1.31      SDL_GestureTouch* touch = &SDL_gestureTouch[i];
    1.32      for(j = 0;j < touch->numDollarTemplates; j++) {
    1.33        if(touch->dollarTemplate[i].hash == gestureId) {
    1.34 -	return SaveTemplate(&touch->dollarTemplate[i],src);
    1.35 +        return SaveTemplate(&touch->dollarTemplate[i],src);
    1.36        }
    1.37      }
    1.38    }
    1.39 @@ -166,18 +166,18 @@
    1.40        inTouch = &SDL_gestureTouch[i];
    1.41  
    1.42      dollarTemplate = 
    1.43 -	(SDL_DollarTemplate *)SDL_realloc(inTouch->dollarTemplate,
    1.44 -		    (inTouch->numDollarTemplates + 1) * 
    1.45 -		    sizeof(SDL_DollarTemplate));
    1.46 +        (SDL_DollarTemplate *)SDL_realloc(inTouch->dollarTemplate,
    1.47 +                    (inTouch->numDollarTemplates + 1) * 
    1.48 +                    sizeof(SDL_DollarTemplate));
    1.49        if(!dollarTemplate) {
    1.50 -	SDL_OutOfMemory();
    1.51 -	return -1;
    1.52 +        SDL_OutOfMemory();
    1.53 +        return -1;
    1.54        }
    1.55 -	
    1.56 +        
    1.57        inTouch->dollarTemplate = dollarTemplate;
    1.58  
    1.59      templ = 
    1.60 -	&inTouch->dollarTemplate[inTouch->numDollarTemplates];
    1.61 +        &inTouch->dollarTemplate[inTouch->numDollarTemplates];
    1.62        SDL_memcpy(templ->path,path,DOLLARNPOINTS*sizeof(SDL_FloatPoint));
    1.63        templ->hash = SDL_HashDollar(templ->path);
    1.64        inTouch->numDollarTemplates++;    
    1.65 @@ -186,8 +186,8 @@
    1.66    } else {
    1.67      SDL_DollarTemplate* dollarTemplate = 
    1.68        ( SDL_DollarTemplate *)SDL_realloc(inTouch->dollarTemplate,
    1.69 -		  (inTouch->numDollarTemplates + 1) * 
    1.70 -		  sizeof(SDL_DollarTemplate));
    1.71 +                  (inTouch->numDollarTemplates + 1) * 
    1.72 +                  sizeof(SDL_DollarTemplate));
    1.73      if(!dollarTemplate) {
    1.74        SDL_OutOfMemory();
    1.75        return -1;
    1.76 @@ -212,7 +212,7 @@
    1.77    if(touchId >= 0) {
    1.78      for(i = 0;i < SDL_numGestureTouches; i++)
    1.79        if(SDL_gestureTouch[i].id == touchId)
    1.80 -	touch = &SDL_gestureTouch[i];
    1.81 +        touch = &SDL_gestureTouch[i];
    1.82      if(touch == NULL) return -1;
    1.83    }
    1.84  
    1.85 @@ -229,10 +229,10 @@
    1.86      else {
    1.87        //printf("Adding to: %i touches\n",SDL_numGestureTouches);
    1.88        for(i = 0;i < SDL_numGestureTouches; i++) {
    1.89 -	touch = &SDL_gestureTouch[i];
    1.90 -	//printf("Adding loaded gesture to + touches\n");
    1.91 -	//TODO: What if this fails?
    1.92 -	SDL_AddDollarGesture(touch,templ.path);	
    1.93 +        touch = &SDL_gestureTouch[i];
    1.94 +        //printf("Adding loaded gesture to + touches\n");
    1.95 +        //TODO: What if this fails?
    1.96 +        SDL_AddDollarGesture(touch,templ.path);        
    1.97        }
    1.98        loaded++;
    1.99      }
   1.100 @@ -251,7 +251,7 @@
   1.101      p.x = (float)(points[i].x * SDL_cos(ang) - points[i].y * SDL_sin(ang));
   1.102      p.y = (float)(points[i].x * SDL_sin(ang) + points[i].y * SDL_cos(ang));
   1.103      dist += (float)(SDL_sqrt((p.x-templ[i].x)*(p.x-templ[i].x)+
   1.104 -		 (p.y-templ[i].y)*(p.y-templ[i].y)));
   1.105 +                 (p.y-templ[i].y)*(p.y-templ[i].y)));
   1.106    }
   1.107    return dist/DOLLARNPOINTS;
   1.108    
   1.109 @@ -308,9 +308,9 @@
   1.110    if(path.length <= 0) {
   1.111      for(i=1;i<path.numPoints;i++) {
   1.112        float dx = path.p[i  ].x - 
   1.113 -	         path.p[i-1].x;
   1.114 +                 path.p[i-1].x;
   1.115        float dy = path.p[i  ].y - 
   1.116 -	         path.p[i-1].y;
   1.117 +                 path.p[i-1].y;
   1.118        path.length += (float)(SDL_sqrt(dx*dx+dy*dy));
   1.119      }
   1.120    }
   1.121 @@ -324,13 +324,13 @@
   1.122    //printf("(%f,%f)\n",path.p[path.numPoints-1].x,path.p[path.numPoints-1].y);
   1.123    for(i = 1;i < path.numPoints;i++) {
   1.124      float d = (float)(SDL_sqrt((path.p[i-1].x-path.p[i].x)*(path.p[i-1].x-path.p[i].x)+
   1.125 -		             (path.p[i-1].y-path.p[i].y)*(path.p[i-1].y-path.p[i].y)));
   1.126 +                             (path.p[i-1].y-path.p[i].y)*(path.p[i-1].y-path.p[i].y)));
   1.127      //printf("d = %f dist = %f/%f\n",d,dist,interval);
   1.128      while(dist + d > interval) {
   1.129        points[numPoints].x = path.p[i-1].x + 
   1.130 -	((interval-dist)/d)*(path.p[i].x-path.p[i-1].x);
   1.131 +        ((interval-dist)/d)*(path.p[i].x-path.p[i-1].x);
   1.132        points[numPoints].y = path.p[i-1].y + 
   1.133 -	((interval-dist)/d)*(path.p[i].y-path.p[i-1].y);
   1.134 +        ((interval-dist)/d)*(path.p[i].y-path.p[i-1].y);
   1.135        centroid.x += points[numPoints].x;
   1.136        centroid.y += points[numPoints].y;
   1.137        numPoints++;
   1.138 @@ -358,9 +358,9 @@
   1.139    ymax = centroid.y;
   1.140    
   1.141    ang = (float)(SDL_atan2(centroid.y - points[0].y,
   1.142 -		    centroid.x - points[0].x));
   1.143 +                    centroid.x - points[0].x));
   1.144  
   1.145 -  for(i = 0;i<numPoints;i++) {					       
   1.146 +  for(i = 0;i<numPoints;i++) {                                               
   1.147      float px = points[i].x;
   1.148      float py = points[i].y;
   1.149      points[i].x = (float)((px - centroid.x)*SDL_cos(ang) - 
   1.150 @@ -387,25 +387,25 @@
   1.151  }
   1.152  
   1.153  float dollarRecognize(SDL_DollarPath path,int *bestTempl,SDL_GestureTouch* touch) {
   1.154 -	
   1.155 -	SDL_FloatPoint points[DOLLARNPOINTS];
   1.156 -	int numPoints = dollarNormalize(path,points);
   1.157 -	int i;
   1.158 -	float bestDiff = 10000;
   1.159 +        
   1.160 +        SDL_FloatPoint points[DOLLARNPOINTS];
   1.161 +        int numPoints = dollarNormalize(path,points);
   1.162 +        int i;
   1.163 +        float bestDiff = 10000;
   1.164  
   1.165 -	//PrintPath(points);
   1.166 -	*bestTempl = -1;
   1.167 -	for(i = 0;i < touch->numDollarTemplates;i++) {
   1.168 -		float diff = bestDollarDifference(points,touch->dollarTemplate[i].path);
   1.169 -		if(diff < bestDiff) {bestDiff = diff; *bestTempl = i;}
   1.170 -	}
   1.171 -	return bestDiff;
   1.172 +        //PrintPath(points);
   1.173 +        *bestTempl = -1;
   1.174 +        for(i = 0;i < touch->numDollarTemplates;i++) {
   1.175 +                float diff = bestDollarDifference(points,touch->dollarTemplate[i].path);
   1.176 +                if(diff < bestDiff) {bestDiff = diff; *bestTempl = i;}
   1.177 +        }
   1.178 +        return bestDiff;
   1.179  }
   1.180  
   1.181  int SDL_GestureAddTouch(SDL_Touch* touch) {  
   1.182    SDL_GestureTouch *gestureTouch = (SDL_GestureTouch *)SDL_realloc(SDL_gestureTouch,
   1.183 -					       (SDL_numGestureTouches + 1) *
   1.184 -					       sizeof(SDL_GestureTouch));
   1.185 +                                               (SDL_numGestureTouches + 1) *
   1.186 +                                               sizeof(SDL_GestureTouch));
   1.187  
   1.188    if(!gestureTouch) {
   1.189      SDL_OutOfMemory();
   1.190 @@ -464,7 +464,7 @@
   1.191  }
   1.192  
   1.193  int SDL_SendGestureDollar(SDL_GestureTouch* touch,
   1.194 -			  SDL_GestureID gestureId,float error) {
   1.195 +                          SDL_GestureID gestureId,float error) {
   1.196    SDL_Event event;
   1.197    event.dgesture.type = SDL_DOLLARGESTURE;
   1.198    event.dgesture.touchId = touch->id;
   1.199 @@ -513,7 +513,7 @@
   1.200      if(inTouch == NULL) return;
   1.201      
   1.202      //printf("@ (%i,%i) with res: (%i,%i)\n",(int)event->tfinger.x,
   1.203 -    //	   (int)event->tfinger.y,
   1.204 +    //           (int)event->tfinger.y,
   1.205      //   (int)inTouch->res.x,(int)inTouch->res.y);
   1.206  
   1.207      
   1.208 @@ -527,44 +527,44 @@
   1.209        
   1.210  #ifdef ENABLE_DOLLAR
   1.211        if(inTouch->recording) {
   1.212 -	inTouch->recording = SDL_FALSE;	
   1.213 -	dollarNormalize(inTouch->dollarPath,path);
   1.214 -	//PrintPath(path);
   1.215 -	if(recordAll) {
   1.216 -	  index = SDL_AddDollarGesture(NULL,path);
   1.217 -	  for(i = 0;i < SDL_numGestureTouches; i++)
   1.218 -	    SDL_gestureTouch[i].recording = SDL_FALSE;
   1.219 -	}
   1.220 -	else {
   1.221 -	  index = SDL_AddDollarGesture(inTouch,path);
   1.222 -	}
   1.223 -	
   1.224 -	if(index >= 0) {
   1.225 -	  SDL_SendDollarRecord(inTouch,inTouch->dollarTemplate[index].hash);
   1.226 -	}
   1.227 -	else {
   1.228 -	  SDL_SendDollarRecord(inTouch,-1);
   1.229 -	}
   1.230 +        inTouch->recording = SDL_FALSE;        
   1.231 +        dollarNormalize(inTouch->dollarPath,path);
   1.232 +        //PrintPath(path);
   1.233 +        if(recordAll) {
   1.234 +          index = SDL_AddDollarGesture(NULL,path);
   1.235 +          for(i = 0;i < SDL_numGestureTouches; i++)
   1.236 +            SDL_gestureTouch[i].recording = SDL_FALSE;
   1.237 +        }
   1.238 +        else {
   1.239 +          index = SDL_AddDollarGesture(inTouch,path);
   1.240 +        }
   1.241 +        
   1.242 +        if(index >= 0) {
   1.243 +          SDL_SendDollarRecord(inTouch,inTouch->dollarTemplate[index].hash);
   1.244 +        }
   1.245 +        else {
   1.246 +          SDL_SendDollarRecord(inTouch,-1);
   1.247 +        }
   1.248        }
   1.249 -      else {	
   1.250 -	int bestTempl;
   1.251 -	float error;
   1.252 -	error = dollarRecognize(inTouch->dollarPath,
   1.253 -				&bestTempl,inTouch);
   1.254 -	if(bestTempl >= 0){
   1.255 -	  //Send Event
   1.256 -	  unsigned long gestureId = inTouch->dollarTemplate[bestTempl].hash;
   1.257 -	  SDL_SendGestureDollar(inTouch,gestureId,error);
   1.258 -	  //printf ("%s\n",);("Dollar error: %f\n",error);
   1.259 -	}
   1.260 +      else {        
   1.261 +        int bestTempl;
   1.262 +        float error;
   1.263 +        error = dollarRecognize(inTouch->dollarPath,
   1.264 +                                &bestTempl,inTouch);
   1.265 +        if(bestTempl >= 0){
   1.266 +          //Send Event
   1.267 +          unsigned long gestureId = inTouch->dollarTemplate[bestTempl].hash;
   1.268 +          SDL_SendGestureDollar(inTouch,gestureId,error);
   1.269 +          //printf ("%s\n",);("Dollar error: %f\n",error);
   1.270 +        }
   1.271        }
   1.272  #endif 
   1.273        //inTouch->gestureLast[j] = inTouch->gestureLast[inTouch->numDownFingers];
   1.274        if(inTouch->numDownFingers > 0) {
   1.275 -	inTouch->centroid.x = (inTouch->centroid.x*(inTouch->numDownFingers+1)-
   1.276 -			       x)/inTouch->numDownFingers;
   1.277 -	inTouch->centroid.y = (inTouch->centroid.y*(inTouch->numDownFingers+1)-
   1.278 -			       y)/inTouch->numDownFingers;
   1.279 +        inTouch->centroid.x = (inTouch->centroid.x*(inTouch->numDownFingers+1)-
   1.280 +                               x)/inTouch->numDownFingers;
   1.281 +        inTouch->centroid.y = (inTouch->centroid.y*(inTouch->numDownFingers+1)-
   1.282 +                               y)/inTouch->numDownFingers;
   1.283        }
   1.284      }
   1.285      else if(event->type == SDL_FINGERMOTION) {
   1.286 @@ -574,14 +574,14 @@
   1.287  #ifdef ENABLE_DOLLAR
   1.288        SDL_DollarPath* path = &inTouch->dollarPath;
   1.289        if(path->numPoints < MAXPATHSIZE) {
   1.290 -	path->p[path->numPoints].x = inTouch->centroid.x;
   1.291 -	path->p[path->numPoints].y = inTouch->centroid.y;
   1.292 -	pathDx = 
   1.293 -	  (path->p[path->numPoints].x-path->p[path->numPoints-1].x);
   1.294 -	pathDy = 
   1.295 -	  (path->p[path->numPoints].y-path->p[path->numPoints-1].y);
   1.296 -	path->length += (float)SDL_sqrt(pathDx*pathDx + pathDy*pathDy);
   1.297 -	path->numPoints++;
   1.298 +        path->p[path->numPoints].x = inTouch->centroid.x;
   1.299 +        path->p[path->numPoints].y = inTouch->centroid.y;
   1.300 +        pathDx = 
   1.301 +          (path->p[path->numPoints].x-path->p[path->numPoints-1].x);
   1.302 +        pathDy = 
   1.303 +          (path->p[path->numPoints].y-path->p[path->numPoints-1].y);
   1.304 +        path->length += (float)SDL_sqrt(pathDx*pathDx + pathDy*pathDy);
   1.305 +        path->numPoints++;
   1.306        }
   1.307  #endif
   1.308        lastP.x = x - dx;
   1.309 @@ -592,46 +592,46 @@
   1.310        inTouch->centroid.y += dy/inTouch->numDownFingers;
   1.311        //printf("Centrid : (%f,%f)\n",inTouch->centroid.x,inTouch->centroid.y);
   1.312        if(inTouch->numDownFingers > 1) {
   1.313 -	SDL_FloatPoint lv; //Vector from centroid to last x,y position
   1.314 -	SDL_FloatPoint v; //Vector from centroid to current x,y position
   1.315 -	//lv = inTouch->gestureLast[j].cv;
   1.316 -	lv.x = lastP.x - lastCentroid.x;
   1.317 -	lv.y = lastP.y - lastCentroid.y;
   1.318 -	lDist = (float)SDL_sqrt(lv.x*lv.x + lv.y*lv.y);
   1.319 -	//printf("lDist = %f\n",lDist);
   1.320 -	v.x = x - inTouch->centroid.x;
   1.321 -	v.y = y - inTouch->centroid.y;
   1.322 -	//inTouch->gestureLast[j].cv = v;
   1.323 -	Dist = (float)SDL_sqrt(v.x*v.x+v.y*v.y);
   1.324 -	// SDL_cos(dTheta) = (v . lv)/(|v| * |lv|)
   1.325 -	
   1.326 -	//Normalize Vectors to simplify angle calculation
   1.327 -	lv.x/=lDist;
   1.328 -	lv.y/=lDist;
   1.329 -	v.x/=Dist;
   1.330 -	v.y/=Dist;
   1.331 -	dtheta = (float)SDL_atan2(lv.x*v.y - lv.y*v.x,lv.x*v.x + lv.y*v.y);
   1.332 -	
   1.333 -	dDist = (Dist - lDist);
   1.334 -	if(lDist == 0) {dDist = 0;dtheta = 0;} //To avoid impossible values
   1.335 -	
   1.336 -	//inTouch->gestureLast[j].dDist = dDist;
   1.337 -	//inTouch->gestureLast[j].dtheta = dtheta;
   1.338 -	
   1.339 -	//printf("dDist = %f, dTheta = %f\n",dDist,dtheta);
   1.340 -	//gdtheta = gdtheta*.9 + dtheta*.1;
   1.341 -	//gdDist  =  gdDist*.9 +  dDist*.1
   1.342 -	//knob.r += dDist/numDownFingers;
   1.343 -	//knob.ang += dtheta;
   1.344 -	//printf("thetaSum = %f, distSum = %f\n",gdtheta,gdDist);
   1.345 -	//printf("id: %i dTheta = %f, dDist = %f\n",j,dtheta,dDist);
   1.346 -	SDL_SendGestureMulti(inTouch,dtheta,dDist);
   1.347 +        SDL_FloatPoint lv; //Vector from centroid to last x,y position
   1.348 +        SDL_FloatPoint v; //Vector from centroid to current x,y position
   1.349 +        //lv = inTouch->gestureLast[j].cv;
   1.350 +        lv.x = lastP.x - lastCentroid.x;
   1.351 +        lv.y = lastP.y - lastCentroid.y;
   1.352 +        lDist = (float)SDL_sqrt(lv.x*lv.x + lv.y*lv.y);
   1.353 +        //printf("lDist = %f\n",lDist);
   1.354 +        v.x = x - inTouch->centroid.x;
   1.355 +        v.y = y - inTouch->centroid.y;
   1.356 +        //inTouch->gestureLast[j].cv = v;
   1.357 +        Dist = (float)SDL_sqrt(v.x*v.x+v.y*v.y);
   1.358 +        // SDL_cos(dTheta) = (v . lv)/(|v| * |lv|)
   1.359 +        
   1.360 +        //Normalize Vectors to simplify angle calculation
   1.361 +        lv.x/=lDist;
   1.362 +        lv.y/=lDist;
   1.363 +        v.x/=Dist;
   1.364 +        v.y/=Dist;
   1.365 +        dtheta = (float)SDL_atan2(lv.x*v.y - lv.y*v.x,lv.x*v.x + lv.y*v.y);
   1.366 +        
   1.367 +        dDist = (Dist - lDist);
   1.368 +        if(lDist == 0) {dDist = 0;dtheta = 0;} //To avoid impossible values
   1.369 +        
   1.370 +        //inTouch->gestureLast[j].dDist = dDist;
   1.371 +        //inTouch->gestureLast[j].dtheta = dtheta;
   1.372 +        
   1.373 +        //printf("dDist = %f, dTheta = %f\n",dDist,dtheta);
   1.374 +        //gdtheta = gdtheta*.9 + dtheta*.1;
   1.375 +        //gdDist  =  gdDist*.9 +  dDist*.1
   1.376 +        //knob.r += dDist/numDownFingers;
   1.377 +        //knob.ang += dtheta;
   1.378 +        //printf("thetaSum = %f, distSum = %f\n",gdtheta,gdDist);
   1.379 +        //printf("id: %i dTheta = %f, dDist = %f\n",j,dtheta,dDist);
   1.380 +        SDL_SendGestureMulti(inTouch,dtheta,dDist);
   1.381        }
   1.382        else {
   1.383 -	//inTouch->gestureLast[j].dDist = 0;
   1.384 -	//inTouch->gestureLast[j].dtheta = 0;
   1.385 -	//inTouch->gestureLast[j].cv.x = 0;
   1.386 -	//inTouch->gestureLast[j].cv.y = 0;
   1.387 +        //inTouch->gestureLast[j].dDist = 0;
   1.388 +        //inTouch->gestureLast[j].dtheta = 0;
   1.389 +        //inTouch->gestureLast[j].cv.x = 0;
   1.390 +        //inTouch->gestureLast[j].cv.y = 0;
   1.391        }
   1.392        //inTouch->gestureLast[j].f.p.x = x;
   1.393        //inTouch->gestureLast[j].f.p.y = y;
   1.394 @@ -643,9 +643,9 @@
   1.395  
   1.396        inTouch->numDownFingers++;
   1.397        inTouch->centroid.x = (inTouch->centroid.x*(inTouch->numDownFingers - 1)+ 
   1.398 -			     x)/inTouch->numDownFingers;
   1.399 +                             x)/inTouch->numDownFingers;
   1.400        inTouch->centroid.y = (inTouch->centroid.y*(inTouch->numDownFingers - 1)+
   1.401 -			     y)/inTouch->numDownFingers;
   1.402 +                             y)/inTouch->numDownFingers;
   1.403        //printf("Finger Down: (%f,%f). Centroid: (%f,%f\n",x,y,
   1.404        //     inTouch->centroid.x,inTouch->centroid.y);
   1.405