comparison src/events/SDL_gesture.c @ 4920:a4032241deb5

Fixed massive stack memory usage in the gesture functions
author Sam Lantinga <slouken@libsdl.org>
date Tue, 30 Nov 2010 18:07:31 -0800
parents 716b2cbf4c9e
children b530ef003506
comparison
equal deleted inserted replaced
4919:716b2cbf4c9e 4920:a4032241deb5
292 */ 292 */
293 return SDL_min(f1,f2); 293 return SDL_min(f1,f2);
294 } 294 }
295 295
296 //DollarPath contains raw points, plus (possibly) the calculated length 296 //DollarPath contains raw points, plus (possibly) the calculated length
297 int dollarNormalize(SDL_DollarPath path,SDL_FloatPoint *points) { 297 int dollarNormalize(const SDL_DollarPath *path,SDL_FloatPoint *points) {
298 int i; 298 int i;
299 float interval; 299 float interval;
300 float dist; 300 float dist;
301 int numPoints = 0; 301 int numPoints = 0;
302 SDL_FloatPoint centroid; 302 SDL_FloatPoint centroid;
303 float xmin,xmax,ymin,ymax; 303 float xmin,xmax,ymin,ymax;
304 float ang; 304 float ang;
305 float w,h; 305 float w,h;
306 float length = path->length;
306 307
307 //Calculate length if it hasn't already been done 308 //Calculate length if it hasn't already been done
308 if(path.length <= 0) { 309 if(length <= 0) {
309 for(i=1;i<path.numPoints;i++) { 310 for(i=1;i<path->numPoints;i++) {
310 float dx = path.p[i ].x - 311 float dx = path->p[i ].x -
311 path.p[i-1].x; 312 path->p[i-1].x;
312 float dy = path.p[i ].y - 313 float dy = path->p[i ].y -
313 path.p[i-1].y; 314 path->p[i-1].y;
314 path.length += (float)(SDL_sqrt(dx*dx+dy*dy)); 315 length += (float)(SDL_sqrt(dx*dx+dy*dy));
315 } 316 }
316 } 317 }
317 318
318 //Resample 319 //Resample
319 interval = path.length/(DOLLARNPOINTS - 1); 320 interval = length/(DOLLARNPOINTS - 1);
320 dist = interval; 321 dist = interval;
321 322
322 centroid.x = 0;centroid.y = 0; 323 centroid.x = 0;centroid.y = 0;
323 324
324 //printf("(%f,%f)\n",path.p[path.numPoints-1].x,path.p[path.numPoints-1].y); 325 //printf("(%f,%f)\n",path->p[path->numPoints-1].x,path->p[path->numPoints-1].y);
325 for(i = 1;i < path.numPoints;i++) { 326 for(i = 1;i < path->numPoints;i++) {
326 float d = (float)(SDL_sqrt((path.p[i-1].x-path.p[i].x)*(path.p[i-1].x-path.p[i].x)+ 327 float d = (float)(SDL_sqrt((path->p[i-1].x-path->p[i].x)*(path->p[i-1].x-path->p[i].x)+
327 (path.p[i-1].y-path.p[i].y)*(path.p[i-1].y-path.p[i].y))); 328 (path->p[i-1].y-path->p[i].y)*(path->p[i-1].y-path->p[i].y)));
328 //printf("d = %f dist = %f/%f\n",d,dist,interval); 329 //printf("d = %f dist = %f/%f\n",d,dist,interval);
329 while(dist + d > interval) { 330 while(dist + d > interval) {
330 points[numPoints].x = path.p[i-1].x + 331 points[numPoints].x = path->p[i-1].x +
331 ((interval-dist)/d)*(path.p[i].x-path.p[i-1].x); 332 ((interval-dist)/d)*(path->p[i].x-path->p[i-1].x);
332 points[numPoints].y = path.p[i-1].y + 333 points[numPoints].y = path->p[i-1].y +
333 ((interval-dist)/d)*(path.p[i].y-path.p[i-1].y); 334 ((interval-dist)/d)*(path->p[i].y-path->p[i-1].y);
334 centroid.x += points[numPoints].x; 335 centroid.x += points[numPoints].x;
335 centroid.y += points[numPoints].y; 336 centroid.y += points[numPoints].y;
336 numPoints++; 337 numPoints++;
337 338
338 dist -= interval; 339 dist -= interval;
342 if(numPoints < DOLLARNPOINTS-1) { 343 if(numPoints < DOLLARNPOINTS-1) {
343 SDL_SetError("ERROR: NumPoints = %i\n",numPoints); 344 SDL_SetError("ERROR: NumPoints = %i\n",numPoints);
344 return 0; 345 return 0;
345 } 346 }
346 //copy the last point 347 //copy the last point
347 points[DOLLARNPOINTS-1] = path.p[path.numPoints-1]; 348 points[DOLLARNPOINTS-1] = path->p[path->numPoints-1];
348 numPoints = DOLLARNPOINTS; 349 numPoints = DOLLARNPOINTS;
349 350
350 centroid.x /= numPoints; 351 centroid.x /= numPoints;
351 centroid.y /= numPoints; 352 centroid.y /= numPoints;
352 353
384 points[i].y = (points[i].y - centroid.y)*DOLLARSIZE/h; 385 points[i].y = (points[i].y - centroid.y)*DOLLARSIZE/h;
385 } 386 }
386 return numPoints; 387 return numPoints;
387 } 388 }
388 389
389 float dollarRecognize(SDL_DollarPath path,int *bestTempl,SDL_GestureTouch* touch) { 390 float dollarRecognize(const SDL_DollarPath *path,int *bestTempl,SDL_GestureTouch* touch) {
390 391
391 SDL_FloatPoint points[DOLLARNPOINTS]; 392 SDL_FloatPoint points[DOLLARNPOINTS];
392 int numPoints = dollarNormalize(path,points); 393 int numPoints = dollarNormalize(path,points);
393 int i; 394 int i;
394 float bestDiff = 10000; 395 float bestDiff = 10000;
526 inTouch->numDownFingers--; 527 inTouch->numDownFingers--;
527 528
528 #ifdef ENABLE_DOLLAR 529 #ifdef ENABLE_DOLLAR
529 if(inTouch->recording) { 530 if(inTouch->recording) {
530 inTouch->recording = SDL_FALSE; 531 inTouch->recording = SDL_FALSE;
531 dollarNormalize(inTouch->dollarPath,path); 532 dollarNormalize(&inTouch->dollarPath,path);
532 //PrintPath(path); 533 //PrintPath(path);
533 if(recordAll) { 534 if(recordAll) {
534 index = SDL_AddDollarGesture(NULL,path); 535 index = SDL_AddDollarGesture(NULL,path);
535 for(i = 0;i < SDL_numGestureTouches; i++) 536 for(i = 0;i < SDL_numGestureTouches; i++)
536 SDL_gestureTouch[i].recording = SDL_FALSE; 537 SDL_gestureTouch[i].recording = SDL_FALSE;
547 } 548 }
548 } 549 }
549 else { 550 else {
550 int bestTempl; 551 int bestTempl;
551 float error; 552 float error;
552 error = dollarRecognize(inTouch->dollarPath, 553 error = dollarRecognize(&inTouch->dollarPath,
553 &bestTempl,inTouch); 554 &bestTempl,inTouch);
554 if(bestTempl >= 0){ 555 if(bestTempl >= 0){
555 //Send Event 556 //Send Event
556 unsigned long gestureId = inTouch->dollarTemplate[bestTempl].hash; 557 unsigned long gestureId = inTouch->dollarTemplate[bestTempl].hash;
557 SDL_SendGestureDollar(inTouch,gestureId,error); 558 SDL_SendGestureDollar(inTouch,gestureId,error);