From 428e61a43e8268cec1b1ad59235ac405c907de20 Mon Sep 17 00:00:00 2001 From: Kees Bakker Date: Thu, 27 Oct 2011 21:26:44 +0200 Subject: [PATCH 1/5] Reformat SDL_gesture.c (part 1, reindent using Emacs, c-basic-offset 4) --- src/events/SDL_gesture.c | 1103 +++++++++++++++++++------------------- 1 file changed, 560 insertions(+), 543 deletions(-) diff --git a/src/events/SDL_gesture.c b/src/events/SDL_gesture.c index 2d6185355..40a2c71be 100644 --- a/src/events/SDL_gesture.c +++ b/src/events/SDL_gesture.c @@ -11,11 +11,11 @@ freely, subject to the following restrictions: 1. The origin of this software must not be misrepresented; you must not - claim that you wrote the original software. If you use this software - in a product, an acknowledgment in the product documentation would be - appreciated but is not required. + claim that you wrote the original software. If you use this software + in a product, an acknowledgment in the product documentation would be + appreciated but is not required. 2. Altered source versions must be plainly marked as such, and must not be - misrepresented as being the original software. + misrepresented as being the original software. 3. This notice may not be removed or altered from any source distribution. */ @@ -36,7 +36,7 @@ #define MAXPATHSIZE 1024 - + #define DOLLARNPOINTS 64 @@ -44,35 +44,35 @@ #define ENABLE_DOLLAR -#define PHI 0.618033989 +#define PHI 0.618033989 typedef struct { - float x,y; + float x,y; } SDL_FloatPoint; typedef struct { - float length; - - int numPoints; - SDL_FloatPoint p[MAXPATHSIZE]; + float length; + + int numPoints; + SDL_FloatPoint p[MAXPATHSIZE]; } SDL_DollarPath; typedef struct { - SDL_FloatPoint path[DOLLARNPOINTS]; - unsigned long hash; + SDL_FloatPoint path[DOLLARNPOINTS]; + unsigned long hash; } SDL_DollarTemplate; typedef struct { - SDL_GestureID id; - SDL_FloatPoint res; - SDL_FloatPoint centroid; - SDL_DollarPath dollarPath; - Uint16 numDownFingers; + SDL_GestureID id; + SDL_FloatPoint res; + SDL_FloatPoint centroid; + SDL_DollarPath dollarPath; + Uint16 numDownFingers; - int numDollarTemplates; - SDL_DollarTemplate *dollarTemplate; + int numDollarTemplates; + SDL_DollarTemplate *dollarTemplate; - SDL_bool recording; + SDL_bool recording; } SDL_GestureTouch; SDL_GestureTouch *SDL_gestureTouch; @@ -80,586 +80,603 @@ int SDL_numGestureTouches = 0; SDL_bool recordAll; #if 0 -static void PrintPath(SDL_FloatPoint *path) { - int i; - printf("Path:"); - for(i=0;i= 0) - return 1; - } - } - return (touchId < 0); -} - -unsigned long SDL_HashDollar(SDL_FloatPoint* points) { - unsigned long hash = 5381; - int i; - for(i = 0;i < DOLLARNPOINTS; i++) { - hash = ((hash<<5) + hash) + (unsigned long)points[i].x; - hash = ((hash<<5) + hash) + (unsigned long)points[i].y; - } - return hash; -} - - -static int SaveTemplate(SDL_DollarTemplate *templ, SDL_RWops * src) { - if(src == NULL) return 0; - - - //No Longer storing the Hash, rehash on load - //if(SDL_RWops.write(src,&(templ->hash),sizeof(templ->hash),1) != 1) return 0; - - if(SDL_RWwrite(src,templ->path, - sizeof(templ->path[0]),DOLLARNPOINTS) != DOLLARNPOINTS) - return 0; - - return 1; -} - - -int SDL_SaveAllDollarTemplates(SDL_RWops *src) { - int i,j,rtrn = 0; - for(i = 0; i < SDL_numGestureTouches; i++) { - SDL_GestureTouch* touch = &SDL_gestureTouch[i]; - for(j = 0;j < touch->numDollarTemplates; j++) { - rtrn += SaveTemplate(&touch->dollarTemplate[i],src); +int SDL_RecordGesture(SDL_TouchID touchId) +{ + int i; + if(touchId < 0) recordAll = SDL_TRUE; + for(i = 0;i < SDL_numGestureTouches; i++) { + if((touchId < 0) || (SDL_gestureTouch[i].id == touchId)) { + SDL_gestureTouch[i].recording = SDL_TRUE; + if(touchId >= 0) + return 1; + } } - } - return rtrn; + return (touchId < 0); } -int SDL_SaveDollarTemplate(SDL_GestureID gestureId, SDL_RWops *src) { - int i,j; - for(i = 0; i < SDL_numGestureTouches; i++) { - SDL_GestureTouch* touch = &SDL_gestureTouch[i]; - for(j = 0;j < touch->numDollarTemplates; j++) { - if(touch->dollarTemplate[i].hash == gestureId) { - return SaveTemplate(&touch->dollarTemplate[i],src); - } +unsigned long SDL_HashDollar(SDL_FloatPoint* points) +{ + unsigned long hash = 5381; + int i; + for(i = 0;i < DOLLARNPOINTS; i++) { + hash = ((hash<<5) + hash) + (unsigned long)points[i].x; + hash = ((hash<<5) + hash) + (unsigned long)points[i].y; } - } - SDL_SetError("Unknown gestureId"); - return -1; + return hash; +} + + +static int SaveTemplate(SDL_DollarTemplate *templ, SDL_RWops * src) +{ + if(src == NULL) return 0; + + + //No Longer storing the Hash, rehash on load + //if(SDL_RWops.write(src,&(templ->hash),sizeof(templ->hash),1) != 1) return 0; + + if(SDL_RWwrite(src,templ->path, + sizeof(templ->path[0]),DOLLARNPOINTS) != DOLLARNPOINTS) + return 0; + + return 1; +} + + +int SDL_SaveAllDollarTemplates(SDL_RWops *src) +{ + int i,j,rtrn = 0; + for(i = 0; i < SDL_numGestureTouches; i++) { + SDL_GestureTouch* touch = &SDL_gestureTouch[i]; + for(j = 0;j < touch->numDollarTemplates; j++) { + rtrn += SaveTemplate(&touch->dollarTemplate[i],src); + } + } + return rtrn; +} + +int SDL_SaveDollarTemplate(SDL_GestureID gestureId, SDL_RWops *src) +{ + int i,j; + for(i = 0; i < SDL_numGestureTouches; i++) { + SDL_GestureTouch* touch = &SDL_gestureTouch[i]; + for(j = 0;j < touch->numDollarTemplates; j++) { + if(touch->dollarTemplate[i].hash == gestureId) { + return SaveTemplate(&touch->dollarTemplate[i],src); + } + } + } + SDL_SetError("Unknown gestureId"); + return -1; } //path is an already sampled set of points //Returns the index of the gesture on success, or -1 -static int SDL_AddDollarGesture(SDL_GestureTouch* inTouch,SDL_FloatPoint* path) { - SDL_DollarTemplate* dollarTemplate; - SDL_DollarTemplate *templ; - int i = 0; - if(inTouch == NULL) { - if(SDL_numGestureTouches == 0) return -1; - for(i = 0;i < SDL_numGestureTouches; i++) { - inTouch = &SDL_gestureTouch[i]; +static int SDL_AddDollarGesture(SDL_GestureTouch* inTouch,SDL_FloatPoint* path) +{ + SDL_DollarTemplate* dollarTemplate; + SDL_DollarTemplate *templ; + int i = 0; + if(inTouch == NULL) { + if(SDL_numGestureTouches == 0) return -1; + for(i = 0;i < SDL_numGestureTouches; i++) { + inTouch = &SDL_gestureTouch[i]; - dollarTemplate = - (SDL_DollarTemplate *)SDL_realloc(inTouch->dollarTemplate, - (inTouch->numDollarTemplates + 1) * - sizeof(SDL_DollarTemplate)); - if(!dollarTemplate) { - SDL_OutOfMemory(); - return -1; - } - - inTouch->dollarTemplate = dollarTemplate; + dollarTemplate = + (SDL_DollarTemplate *)SDL_realloc(inTouch->dollarTemplate, + (inTouch->numDollarTemplates + 1) * + sizeof(SDL_DollarTemplate)); + if(!dollarTemplate) { + SDL_OutOfMemory(); + return -1; + } - templ = - &inTouch->dollarTemplate[inTouch->numDollarTemplates]; - SDL_memcpy(templ->path,path,DOLLARNPOINTS*sizeof(SDL_FloatPoint)); - templ->hash = SDL_HashDollar(templ->path); - inTouch->numDollarTemplates++; + inTouch->dollarTemplate = dollarTemplate; + + templ = + &inTouch->dollarTemplate[inTouch->numDollarTemplates]; + SDL_memcpy(templ->path,path,DOLLARNPOINTS*sizeof(SDL_FloatPoint)); + templ->hash = SDL_HashDollar(templ->path); + inTouch->numDollarTemplates++; + } + return inTouch->numDollarTemplates - 1; + } else { + SDL_DollarTemplate* dollarTemplate = + ( SDL_DollarTemplate *)SDL_realloc(inTouch->dollarTemplate, + (inTouch->numDollarTemplates + 1) * + sizeof(SDL_DollarTemplate)); + if(!dollarTemplate) { + SDL_OutOfMemory(); + return -1; + } + + inTouch->dollarTemplate = dollarTemplate; + + templ = + &inTouch->dollarTemplate[inTouch->numDollarTemplates]; + SDL_memcpy(templ->path,path,DOLLARNPOINTS*sizeof(SDL_FloatPoint)); + templ->hash = SDL_HashDollar(templ->path); + inTouch->numDollarTemplates++; + return inTouch->numDollarTemplates - 1; } - return inTouch->numDollarTemplates - 1; - } else { - SDL_DollarTemplate* dollarTemplate = - ( SDL_DollarTemplate *)SDL_realloc(inTouch->dollarTemplate, - (inTouch->numDollarTemplates + 1) * - sizeof(SDL_DollarTemplate)); - if(!dollarTemplate) { - SDL_OutOfMemory(); - return -1; - } - - inTouch->dollarTemplate = dollarTemplate; - - templ = - &inTouch->dollarTemplate[inTouch->numDollarTemplates]; - SDL_memcpy(templ->path,path,DOLLARNPOINTS*sizeof(SDL_FloatPoint)); - templ->hash = SDL_HashDollar(templ->path); - inTouch->numDollarTemplates++; - return inTouch->numDollarTemplates - 1; - } - return -1; + return -1; } -int SDL_LoadDollarTemplates(SDL_TouchID touchId, SDL_RWops *src) { - int i,loaded = 0; - SDL_GestureTouch *touch = NULL; - if(src == NULL) return 0; - if(touchId >= 0) { - for(i = 0;i < SDL_numGestureTouches; i++) - if(SDL_gestureTouch[i].id == touchId) - touch = &SDL_gestureTouch[i]; - if(touch == NULL) return -1; - } - - while(1) { - SDL_DollarTemplate templ; - - if(SDL_RWread(src,templ.path,sizeof(templ.path[0]),DOLLARNPOINTS) < - DOLLARNPOINTS) break; - +int SDL_LoadDollarTemplates(SDL_TouchID touchId, SDL_RWops *src) +{ + int i,loaded = 0; + SDL_GestureTouch *touch = NULL; + if(src == NULL) return 0; if(touchId >= 0) { - //printf("Adding loaded gesture to 1 touch\n"); - if(SDL_AddDollarGesture(touch,templ.path)) loaded++; + for(i = 0;i < SDL_numGestureTouches; i++) + if(SDL_gestureTouch[i].id == touchId) + touch = &SDL_gestureTouch[i]; + if(touch == NULL) return -1; } - else { - //printf("Adding to: %i touches\n",SDL_numGestureTouches); - for(i = 0;i < SDL_numGestureTouches; i++) { - touch = &SDL_gestureTouch[i]; - //printf("Adding loaded gesture to + touches\n"); - //TODO: What if this fails? - SDL_AddDollarGesture(touch,templ.path); - } - loaded++; - } - } - return loaded; + while(1) { + SDL_DollarTemplate templ; + + if(SDL_RWread(src,templ.path,sizeof(templ.path[0]),DOLLARNPOINTS) < + DOLLARNPOINTS) break; + + if(touchId >= 0) { + //printf("Adding loaded gesture to 1 touch\n"); + if(SDL_AddDollarGesture(touch,templ.path)) loaded++; + } + else { + //printf("Adding to: %i touches\n",SDL_numGestureTouches); + for(i = 0;i < SDL_numGestureTouches; i++) { + touch = &SDL_gestureTouch[i]; + //printf("Adding loaded gesture to + touches\n"); + //TODO: What if this fails? + SDL_AddDollarGesture(touch,templ.path); + } + loaded++; + } + } + + return loaded; } -float dollarDifference(SDL_FloatPoint* points,SDL_FloatPoint* templ,float ang) { - // SDL_FloatPoint p[DOLLARNPOINTS]; - float dist = 0; - SDL_FloatPoint p; - int i; - for(i = 0; i < DOLLARNPOINTS; i++) { - p.x = (float)(points[i].x * SDL_cos(ang) - points[i].y * SDL_sin(ang)); - p.y = (float)(points[i].x * SDL_sin(ang) + points[i].y * SDL_cos(ang)); - dist += (float)(SDL_sqrt((p.x-templ[i].x)*(p.x-templ[i].x)+ - (p.y-templ[i].y)*(p.y-templ[i].y))); - } - return dist/DOLLARNPOINTS; - +float dollarDifference(SDL_FloatPoint* points,SDL_FloatPoint* templ,float ang) +{ + // SDL_FloatPoint p[DOLLARNPOINTS]; + float dist = 0; + SDL_FloatPoint p; + int i; + for(i = 0; i < DOLLARNPOINTS; i++) { + p.x = (float)(points[i].x * SDL_cos(ang) - points[i].y * SDL_sin(ang)); + p.y = (float)(points[i].x * SDL_sin(ang) + points[i].y * SDL_cos(ang)); + dist += (float)(SDL_sqrt((p.x-templ[i].x)*(p.x-templ[i].x)+ + (p.y-templ[i].y)*(p.y-templ[i].y))); + } + return dist/DOLLARNPOINTS; + } -float bestDollarDifference(SDL_FloatPoint* points,SDL_FloatPoint* templ) { - //------------BEGIN DOLLAR BLACKBOX----------------// - //-TRANSLATED DIRECTLY FROM PSUDEO-CODE AVAILABLE AT-// - //-"http://depts.washington.edu/aimgroup/proj/dollar/"-// - double ta = -M_PI/4; - double tb = M_PI/4; - double dt = M_PI/90; - float x1 = (float)(PHI*ta + (1-PHI)*tb); - float f1 = dollarDifference(points,templ,x1); - float x2 = (float)((1-PHI)*ta + PHI*tb); - float f2 = dollarDifference(points,templ,x2); - while(SDL_fabs(ta-tb) > dt) { - if(f1 < f2) { - tb = x2; - x2 = x1; - f2 = f1; - x1 = (float)(PHI*ta + (1-PHI)*tb); - f1 = dollarDifference(points,templ,x1); +float bestDollarDifference(SDL_FloatPoint* points,SDL_FloatPoint* templ) +{ + //------------BEGIN DOLLAR BLACKBOX----------------// + //-TRANSLATED DIRECTLY FROM PSUDEO-CODE AVAILABLE AT-// + //-"http://depts.washington.edu/aimgroup/proj/dollar/"-// + double ta = -M_PI/4; + double tb = M_PI/4; + double dt = M_PI/90; + float x1 = (float)(PHI*ta + (1-PHI)*tb); + float f1 = dollarDifference(points,templ,x1); + float x2 = (float)((1-PHI)*ta + PHI*tb); + float f2 = dollarDifference(points,templ,x2); + while(SDL_fabs(ta-tb) > dt) { + if(f1 < f2) { + tb = x2; + x2 = x1; + f2 = f1; + x1 = (float)(PHI*ta + (1-PHI)*tb); + f1 = dollarDifference(points,templ,x1); + } + else { + ta = x1; + x1 = x2; + f1 = f2; + x2 = (float)((1-PHI)*ta + PHI*tb); + f2 = dollarDifference(points,templ,x2); + } } - else { - ta = x1; - x1 = x2; - f1 = f2; - x2 = (float)((1-PHI)*ta + PHI*tb); - f2 = dollarDifference(points,templ,x2); - } - } - /* - if(f1 <= f2) - printf("Min angle (x1): %f\n",x1); - else if(f1 > f2) - printf("Min angle (x2): %f\n",x2); - */ - return SDL_min(f1,f2); + /* + if(f1 <= f2) + printf("Min angle (x1): %f\n",x1); + else if(f1 > f2) + printf("Min angle (x2): %f\n",x2); + */ + return SDL_min(f1,f2); } //DollarPath contains raw points, plus (possibly) the calculated length -int dollarNormalize(const SDL_DollarPath *path,SDL_FloatPoint *points) { - int i; - float interval; - float dist; - int numPoints = 0; - SDL_FloatPoint centroid; - float xmin,xmax,ymin,ymax; - float ang; - float w,h; - float length = path->length; +int dollarNormalize(const SDL_DollarPath *path,SDL_FloatPoint *points) +{ + int i; + float interval; + float dist; + int numPoints = 0; + SDL_FloatPoint centroid; + float xmin,xmax,ymin,ymax; + float ang; + float w,h; + float length = path->length; - //Calculate length if it hasn't already been done - if(length <= 0) { - for(i=1;inumPoints;i++) { - float dx = path->p[i ].x - - path->p[i-1].x; - float dy = path->p[i ].y - - path->p[i-1].y; - length += (float)(SDL_sqrt(dx*dx+dy*dy)); - } - } - - //Resample - interval = length/(DOLLARNPOINTS - 1); - dist = interval; - - centroid.x = 0;centroid.y = 0; - - //printf("(%f,%f)\n",path->p[path->numPoints-1].x,path->p[path->numPoints-1].y); - for(i = 1;i < path->numPoints;i++) { - float d = (float)(SDL_sqrt((path->p[i-1].x-path->p[i].x)*(path->p[i-1].x-path->p[i].x)+ - (path->p[i-1].y-path->p[i].y)*(path->p[i-1].y-path->p[i].y))); - //printf("d = %f dist = %f/%f\n",d,dist,interval); - while(dist + d > interval) { - points[numPoints].x = path->p[i-1].x + - ((interval-dist)/d)*(path->p[i].x-path->p[i-1].x); - points[numPoints].y = path->p[i-1].y + - ((interval-dist)/d)*(path->p[i].y-path->p[i-1].y); - centroid.x += points[numPoints].x; - centroid.y += points[numPoints].y; - numPoints++; - - dist -= interval; - } - dist += d; - } - if(numPoints < DOLLARNPOINTS-1) { - SDL_SetError("ERROR: NumPoints = %i\n",numPoints); - return 0; - } - //copy the last point - points[DOLLARNPOINTS-1] = path->p[path->numPoints-1]; - numPoints = DOLLARNPOINTS; - - centroid.x /= numPoints; - centroid.y /= numPoints; - - //printf("Centroid (%f,%f)",centroid.x,centroid.y); - //Rotate Points so point 0 is left of centroid and solve for the bounding box - xmin = centroid.x; - xmax = centroid.x; - ymin = centroid.y; - ymax = centroid.y; - - ang = (float)(SDL_atan2(centroid.y - points[0].y, - centroid.x - points[0].x)); - - for(i = 0;i xmax) xmax = points[i].x; - if(points[i].y < ymin) ymin = points[i].y; - if(points[i].y > ymax) ymax = points[i].y; - } - - //Scale points to DOLLARSIZE, and translate to the origin - w = xmax-xmin; - h = ymax-ymin; - - for(i=0;inumDollarTemplates;i++) { - float diff = bestDollarDifference(points,touch->dollarTemplate[i].path); - if(diff < bestDiff) {bestDiff = diff; *bestTempl = i;} + //Calculate length if it hasn't already been done + if(length <= 0) { + for(i=1;inumPoints;i++) { + float dx = path->p[i ].x - + path->p[i-1].x; + float dy = path->p[i ].y - + path->p[i-1].y; + length += (float)(SDL_sqrt(dx*dx+dy*dy)); } - return bestDiff; -} - -int SDL_GestureAddTouch(SDL_Touch* touch) { - SDL_GestureTouch *gestureTouch = (SDL_GestureTouch *)SDL_realloc(SDL_gestureTouch, - (SDL_numGestureTouches + 1) * - sizeof(SDL_GestureTouch)); - - if(!gestureTouch) { - SDL_OutOfMemory(); - return -1; - } - - SDL_gestureTouch = gestureTouch; - - SDL_gestureTouch[SDL_numGestureTouches].res.x = touch->xres; - SDL_gestureTouch[SDL_numGestureTouches].res.y = touch->yres; - SDL_gestureTouch[SDL_numGestureTouches].numDownFingers = 0; - - SDL_gestureTouch[SDL_numGestureTouches].res.x = touch->xres; - SDL_gestureTouch[SDL_numGestureTouches].id = touch->id; - - SDL_gestureTouch[SDL_numGestureTouches].numDollarTemplates = 0; - - SDL_gestureTouch[SDL_numGestureTouches].recording = SDL_FALSE; - - SDL_numGestureTouches++; - return 0; -} - -int SDL_GestureRemoveTouch(SDL_TouchID id) { - int i; - for (i = 0; i < SDL_numGestureTouches; i++) { - if (SDL_gestureTouch[i].id == id) { - SDL_numGestureTouches--; - SDL_memcpy(&SDL_gestureTouch[i], &SDL_gestureTouch[SDL_numGestureTouches], sizeof(SDL_gestureTouch[i])); - return 1; } - } - return -1; + + //Resample + interval = length/(DOLLARNPOINTS - 1); + dist = interval; + + centroid.x = 0;centroid.y = 0; + + //printf("(%f,%f)\n",path->p[path->numPoints-1].x,path->p[path->numPoints-1].y); + for(i = 1;i < path->numPoints;i++) { + float d = (float)(SDL_sqrt((path->p[i-1].x-path->p[i].x)*(path->p[i-1].x-path->p[i].x)+ + (path->p[i-1].y-path->p[i].y)*(path->p[i-1].y-path->p[i].y))); + //printf("d = %f dist = %f/%f\n",d,dist,interval); + while(dist + d > interval) { + points[numPoints].x = path->p[i-1].x + + ((interval-dist)/d)*(path->p[i].x-path->p[i-1].x); + points[numPoints].y = path->p[i-1].y + + ((interval-dist)/d)*(path->p[i].y-path->p[i-1].y); + centroid.x += points[numPoints].x; + centroid.y += points[numPoints].y; + numPoints++; + + dist -= interval; + } + dist += d; + } + if(numPoints < DOLLARNPOINTS-1) { + SDL_SetError("ERROR: NumPoints = %i\n",numPoints); + return 0; + } + //copy the last point + points[DOLLARNPOINTS-1] = path->p[path->numPoints-1]; + numPoints = DOLLARNPOINTS; + + centroid.x /= numPoints; + centroid.y /= numPoints; + + //printf("Centroid (%f,%f)",centroid.x,centroid.y); + //Rotate Points so point 0 is left of centroid and solve for the bounding box + xmin = centroid.x; + xmax = centroid.x; + ymin = centroid.y; + ymax = centroid.y; + + ang = (float)(SDL_atan2(centroid.y - points[0].y, + centroid.x - points[0].x)); + + for(i = 0;i xmax) xmax = points[i].x; + if(points[i].y < ymin) ymin = points[i].y; + if(points[i].y > ymax) ymax = points[i].y; + } + + //Scale points to DOLLARSIZE, and translate to the origin + w = xmax-xmin; + h = ymax-ymin; + + for(i=0;inumDollarTemplates;i++) { + float diff = bestDollarDifference(points,touch->dollarTemplate[i].path); + if(diff < bestDiff) {bestDiff = diff; *bestTempl = i;} + } + return bestDiff; +} + +int SDL_GestureAddTouch(SDL_Touch* touch) +{ + SDL_GestureTouch *gestureTouch = (SDL_GestureTouch *)SDL_realloc(SDL_gestureTouch, + (SDL_numGestureTouches + 1) * + sizeof(SDL_GestureTouch)); + + if(!gestureTouch) { + SDL_OutOfMemory(); + return -1; + } + + SDL_gestureTouch = gestureTouch; + + SDL_gestureTouch[SDL_numGestureTouches].res.x = touch->xres; + SDL_gestureTouch[SDL_numGestureTouches].res.y = touch->yres; + SDL_gestureTouch[SDL_numGestureTouches].numDownFingers = 0; + + SDL_gestureTouch[SDL_numGestureTouches].res.x = touch->xres; + SDL_gestureTouch[SDL_numGestureTouches].id = touch->id; + + SDL_gestureTouch[SDL_numGestureTouches].numDollarTemplates = 0; + + SDL_gestureTouch[SDL_numGestureTouches].recording = SDL_FALSE; + + SDL_numGestureTouches++; + return 0; +} + +int SDL_GestureRemoveTouch(SDL_TouchID id) +{ + int i; + for (i = 0; i < SDL_numGestureTouches; i++) { + if (SDL_gestureTouch[i].id == id) { + SDL_numGestureTouches--; + SDL_memcpy(&SDL_gestureTouch[i], &SDL_gestureTouch[SDL_numGestureTouches], sizeof(SDL_gestureTouch[i])); + return 1; + } + } + return -1; } -SDL_GestureTouch * SDL_GetGestureTouch(SDL_TouchID id) { - int i; - for(i = 0;i < SDL_numGestureTouches; i++) { - //printf("%i ?= %i\n",SDL_gestureTouch[i].id,id); - if(SDL_gestureTouch[i].id == id) return &SDL_gestureTouch[i]; - } - return NULL; +SDL_GestureTouch * SDL_GetGestureTouch(SDL_TouchID id) +{ + int i; + for(i = 0;i < SDL_numGestureTouches; i++) { + //printf("%i ?= %i\n",SDL_gestureTouch[i].id,id); + if(SDL_gestureTouch[i].id == id) return &SDL_gestureTouch[i]; + } + return NULL; } -int SDL_SendGestureMulti(SDL_GestureTouch* touch,float dTheta,float dDist) { - SDL_Event event; - event.mgesture.type = SDL_MULTIGESTURE; - event.mgesture.touchId = touch->id; - event.mgesture.x = touch->centroid.x; - event.mgesture.y = touch->centroid.y; - event.mgesture.dTheta = dTheta; - event.mgesture.dDist = dDist; - event.mgesture.numFingers = touch->numDownFingers; - return SDL_PushEvent(&event) > 0; +int SDL_SendGestureMulti(SDL_GestureTouch* touch,float dTheta,float dDist) +{ + SDL_Event event; + event.mgesture.type = SDL_MULTIGESTURE; + event.mgesture.touchId = touch->id; + event.mgesture.x = touch->centroid.x; + event.mgesture.y = touch->centroid.y; + event.mgesture.dTheta = dTheta; + event.mgesture.dDist = dDist; + event.mgesture.numFingers = touch->numDownFingers; + return SDL_PushEvent(&event) > 0; } int SDL_SendGestureDollar(SDL_GestureTouch* touch, - SDL_GestureID gestureId,float error) { - SDL_Event event; - event.dgesture.type = SDL_DOLLARGESTURE; - event.dgesture.touchId = touch->id; - /* + SDL_GestureID gestureId,float error) +{ + SDL_Event event; + event.dgesture.type = SDL_DOLLARGESTURE; + event.dgesture.touchId = touch->id; + /* //TODO: Add this to give location of gesture? - event.mgesture.x = touch->centroid.x; - event.mgesture.y = touch->centroid.y; - */ - event.dgesture.gestureId = gestureId; - event.dgesture.error = error; - //A finger came up to trigger this event. - event.dgesture.numFingers = touch->numDownFingers + 1; - return SDL_PushEvent(&event) > 0; + event.mgesture.x = touch->centroid.x; + event.mgesture.y = touch->centroid.y; + */ + event.dgesture.gestureId = gestureId; + event.dgesture.error = error; + //A finger came up to trigger this event. + event.dgesture.numFingers = touch->numDownFingers + 1; + return SDL_PushEvent(&event) > 0; } -int SDL_SendDollarRecord(SDL_GestureTouch* touch,SDL_GestureID gestureId) { - SDL_Event event; - event.dgesture.type = SDL_DOLLARRECORD; - event.dgesture.touchId = touch->id; - event.dgesture.gestureId = gestureId; - return SDL_PushEvent(&event) > 0; +int SDL_SendDollarRecord(SDL_GestureTouch* touch,SDL_GestureID gestureId) +{ + SDL_Event event; + event.dgesture.type = SDL_DOLLARRECORD; + event.dgesture.touchId = touch->id; + event.dgesture.gestureId = gestureId; + return SDL_PushEvent(&event) > 0; } void SDL_GestureProcessEvent(SDL_Event* event) { - float x,y; - SDL_FloatPoint path[DOLLARNPOINTS]; - int index; - int i; - float pathDx, pathDy; - SDL_FloatPoint lastP; - SDL_FloatPoint lastCentroid; - float lDist; - float Dist; - float dtheta; - float dDist; + float x,y; + SDL_FloatPoint path[DOLLARNPOINTS]; + int index; + int i; + float pathDx, pathDy; + SDL_FloatPoint lastP; + SDL_FloatPoint lastCentroid; + float lDist; + float Dist; + float dtheta; + float dDist; - if(event->type == SDL_FINGERMOTION || - event->type == SDL_FINGERDOWN || - event->type == SDL_FINGERUP) { - SDL_GestureTouch* inTouch = SDL_GetGestureTouch(event->tfinger.touchId); - - //Shouldn't be possible - if(inTouch == NULL) return; - - //printf("@ (%i,%i) with res: (%i,%i)\n",(int)event->tfinger.x, - // (int)event->tfinger.y, - // (int)inTouch->res.x,(int)inTouch->res.y); + if(event->type == SDL_FINGERMOTION || + event->type == SDL_FINGERDOWN || + event->type == SDL_FINGERUP) { + SDL_GestureTouch* inTouch = SDL_GetGestureTouch(event->tfinger.touchId); - - x = ((float)event->tfinger.x)/(float)inTouch->res.x; - y = ((float)event->tfinger.y)/(float)inTouch->res.y; + //Shouldn't be possible + if(inTouch == NULL) return; + + //printf("@ (%i,%i) with res: (%i,%i)\n",(int)event->tfinger.x, + // (int)event->tfinger.y, + // (int)inTouch->res.x,(int)inTouch->res.y); - //Finger Up - if(event->type == SDL_FINGERUP) { - inTouch->numDownFingers--; - + x = ((float)event->tfinger.x)/(float)inTouch->res.x; + y = ((float)event->tfinger.y)/(float)inTouch->res.y; + + + //Finger Up + if(event->type == SDL_FINGERUP) { + inTouch->numDownFingers--; + #ifdef ENABLE_DOLLAR - if(inTouch->recording) { - inTouch->recording = SDL_FALSE; - dollarNormalize(&inTouch->dollarPath,path); - //PrintPath(path); - if(recordAll) { - index = SDL_AddDollarGesture(NULL,path); - for(i = 0;i < SDL_numGestureTouches; i++) - SDL_gestureTouch[i].recording = SDL_FALSE; - } - else { - index = SDL_AddDollarGesture(inTouch,path); - } - - if(index >= 0) { - SDL_SendDollarRecord(inTouch,inTouch->dollarTemplate[index].hash); - } - else { - SDL_SendDollarRecord(inTouch,-1); - } - } - else { - int bestTempl; - float error; - error = dollarRecognize(&inTouch->dollarPath, - &bestTempl,inTouch); - if(bestTempl >= 0){ - //Send Event - unsigned long gestureId = inTouch->dollarTemplate[bestTempl].hash; - SDL_SendGestureDollar(inTouch,gestureId,error); - //printf ("%s\n",);("Dollar error: %f\n",error); - } - } -#endif - //inTouch->gestureLast[j] = inTouch->gestureLast[inTouch->numDownFingers]; - if(inTouch->numDownFingers > 0) { - inTouch->centroid.x = (inTouch->centroid.x*(inTouch->numDownFingers+1)- - x)/inTouch->numDownFingers; - inTouch->centroid.y = (inTouch->centroid.y*(inTouch->numDownFingers+1)- - y)/inTouch->numDownFingers; - } - } - else if(event->type == SDL_FINGERMOTION) { - float dx = ((float)event->tfinger.dx)/(float)inTouch->res.x; - float dy = ((float)event->tfinger.dy)/(float)inTouch->res.y; - //printf("dx,dy: (%f,%f)\n",dx,dy); -#ifdef ENABLE_DOLLAR - SDL_DollarPath* path = &inTouch->dollarPath; - if(path->numPoints < MAXPATHSIZE) { - path->p[path->numPoints].x = inTouch->centroid.x; - path->p[path->numPoints].y = inTouch->centroid.y; - pathDx = - (path->p[path->numPoints].x-path->p[path->numPoints-1].x); - pathDy = - (path->p[path->numPoints].y-path->p[path->numPoints-1].y); - path->length += (float)SDL_sqrt(pathDx*pathDx + pathDy*pathDy); - path->numPoints++; - } + if(inTouch->recording) { + inTouch->recording = SDL_FALSE; + dollarNormalize(&inTouch->dollarPath,path); + //PrintPath(path); + if(recordAll) { + index = SDL_AddDollarGesture(NULL,path); + for(i = 0;i < SDL_numGestureTouches; i++) + SDL_gestureTouch[i].recording = SDL_FALSE; + } + else { + index = SDL_AddDollarGesture(inTouch,path); + } + + if(index >= 0) { + SDL_SendDollarRecord(inTouch,inTouch->dollarTemplate[index].hash); + } + else { + SDL_SendDollarRecord(inTouch,-1); + } + } + else { + int bestTempl; + float error; + error = dollarRecognize(&inTouch->dollarPath, + &bestTempl,inTouch); + if(bestTempl >= 0){ + //Send Event + unsigned long gestureId = inTouch->dollarTemplate[bestTempl].hash; + SDL_SendGestureDollar(inTouch,gestureId,error); + //printf ("%s\n",);("Dollar error: %f\n",error); + } + } #endif - lastP.x = x - dx; - lastP.y = y - dy; - lastCentroid = inTouch->centroid; - - inTouch->centroid.x += dx/inTouch->numDownFingers; - inTouch->centroid.y += dy/inTouch->numDownFingers; - //printf("Centrid : (%f,%f)\n",inTouch->centroid.x,inTouch->centroid.y); - if(inTouch->numDownFingers > 1) { - SDL_FloatPoint lv; //Vector from centroid to last x,y position - SDL_FloatPoint v; //Vector from centroid to current x,y position - //lv = inTouch->gestureLast[j].cv; - lv.x = lastP.x - lastCentroid.x; - lv.y = lastP.y - lastCentroid.y; - lDist = (float)SDL_sqrt(lv.x*lv.x + lv.y*lv.y); - //printf("lDist = %f\n",lDist); - v.x = x - inTouch->centroid.x; - v.y = y - inTouch->centroid.y; - //inTouch->gestureLast[j].cv = v; - Dist = (float)SDL_sqrt(v.x*v.x+v.y*v.y); - // SDL_cos(dTheta) = (v . lv)/(|v| * |lv|) - - //Normalize Vectors to simplify angle calculation - lv.x/=lDist; - lv.y/=lDist; - v.x/=Dist; - v.y/=Dist; - dtheta = (float)SDL_atan2(lv.x*v.y - lv.y*v.x,lv.x*v.x + lv.y*v.y); - - dDist = (Dist - lDist); - if(lDist == 0) {dDist = 0;dtheta = 0;} //To avoid impossible values - - //inTouch->gestureLast[j].dDist = dDist; - //inTouch->gestureLast[j].dtheta = dtheta; - - //printf("dDist = %f, dTheta = %f\n",dDist,dtheta); - //gdtheta = gdtheta*.9 + dtheta*.1; - //gdDist = gdDist*.9 + dDist*.1 - //knob.r += dDist/numDownFingers; - //knob.ang += dtheta; - //printf("thetaSum = %f, distSum = %f\n",gdtheta,gdDist); - //printf("id: %i dTheta = %f, dDist = %f\n",j,dtheta,dDist); - SDL_SendGestureMulti(inTouch,dtheta,dDist); - } - else { - //inTouch->gestureLast[j].dDist = 0; - //inTouch->gestureLast[j].dtheta = 0; - //inTouch->gestureLast[j].cv.x = 0; - //inTouch->gestureLast[j].cv.y = 0; - } - //inTouch->gestureLast[j].f.p.x = x; - //inTouch->gestureLast[j].f.p.y = y; - //break; - //pressure? - } - - if(event->type == SDL_FINGERDOWN) { + //inTouch->gestureLast[j] = inTouch->gestureLast[inTouch->numDownFingers]; + if(inTouch->numDownFingers > 0) { + inTouch->centroid.x = (inTouch->centroid.x*(inTouch->numDownFingers+1)- + x)/inTouch->numDownFingers; + inTouch->centroid.y = (inTouch->centroid.y*(inTouch->numDownFingers+1)- + y)/inTouch->numDownFingers; + } + } + else if(event->type == SDL_FINGERMOTION) { + float dx = ((float)event->tfinger.dx)/(float)inTouch->res.x; + float dy = ((float)event->tfinger.dy)/(float)inTouch->res.y; + //printf("dx,dy: (%f,%f)\n",dx,dy); +#ifdef ENABLE_DOLLAR + SDL_DollarPath* path = &inTouch->dollarPath; + if(path->numPoints < MAXPATHSIZE) { + path->p[path->numPoints].x = inTouch->centroid.x; + path->p[path->numPoints].y = inTouch->centroid.y; + pathDx = + (path->p[path->numPoints].x-path->p[path->numPoints-1].x); + pathDy = + (path->p[path->numPoints].y-path->p[path->numPoints-1].y); + path->length += (float)SDL_sqrt(pathDx*pathDx + pathDy*pathDy); + path->numPoints++; + } +#endif + lastP.x = x - dx; + lastP.y = y - dy; + lastCentroid = inTouch->centroid; - inTouch->numDownFingers++; - inTouch->centroid.x = (inTouch->centroid.x*(inTouch->numDownFingers - 1)+ - x)/inTouch->numDownFingers; - inTouch->centroid.y = (inTouch->centroid.y*(inTouch->numDownFingers - 1)+ - y)/inTouch->numDownFingers; - //printf("Finger Down: (%f,%f). Centroid: (%f,%f\n",x,y, - // inTouch->centroid.x,inTouch->centroid.y); + inTouch->centroid.x += dx/inTouch->numDownFingers; + inTouch->centroid.y += dy/inTouch->numDownFingers; + //printf("Centrid : (%f,%f)\n",inTouch->centroid.x,inTouch->centroid.y); + if(inTouch->numDownFingers > 1) { + SDL_FloatPoint lv; //Vector from centroid to last x,y position + SDL_FloatPoint v; //Vector from centroid to current x,y position + //lv = inTouch->gestureLast[j].cv; + lv.x = lastP.x - lastCentroid.x; + lv.y = lastP.y - lastCentroid.y; + lDist = (float)SDL_sqrt(lv.x*lv.x + lv.y*lv.y); + //printf("lDist = %f\n",lDist); + v.x = x - inTouch->centroid.x; + v.y = y - inTouch->centroid.y; + //inTouch->gestureLast[j].cv = v; + Dist = (float)SDL_sqrt(v.x*v.x+v.y*v.y); + // SDL_cos(dTheta) = (v . lv)/(|v| * |lv|) + + //Normalize Vectors to simplify angle calculation + lv.x/=lDist; + lv.y/=lDist; + v.x/=Dist; + v.y/=Dist; + dtheta = (float)SDL_atan2(lv.x*v.y - lv.y*v.x,lv.x*v.x + lv.y*v.y); + + dDist = (Dist - lDist); + if(lDist == 0) {dDist = 0;dtheta = 0;} //To avoid impossible values + + //inTouch->gestureLast[j].dDist = dDist; + //inTouch->gestureLast[j].dtheta = dtheta; + + //printf("dDist = %f, dTheta = %f\n",dDist,dtheta); + //gdtheta = gdtheta*.9 + dtheta*.1; + //gdDist = gdDist*.9 + dDist*.1 + //knob.r += dDist/numDownFingers; + //knob.ang += dtheta; + //printf("thetaSum = %f, distSum = %f\n",gdtheta,gdDist); + //printf("id: %i dTheta = %f, dDist = %f\n",j,dtheta,dDist); + SDL_SendGestureMulti(inTouch,dtheta,dDist); + } + else { + //inTouch->gestureLast[j].dDist = 0; + //inTouch->gestureLast[j].dtheta = 0; + //inTouch->gestureLast[j].cv.x = 0; + //inTouch->gestureLast[j].cv.y = 0; + } + //inTouch->gestureLast[j].f.p.x = x; + //inTouch->gestureLast[j].f.p.y = y; + //break; + //pressure? + } + + if(event->type == SDL_FINGERDOWN) { + + inTouch->numDownFingers++; + inTouch->centroid.x = (inTouch->centroid.x*(inTouch->numDownFingers - 1)+ + x)/inTouch->numDownFingers; + inTouch->centroid.y = (inTouch->centroid.y*(inTouch->numDownFingers - 1)+ + y)/inTouch->numDownFingers; + //printf("Finger Down: (%f,%f). Centroid: (%f,%f\n",x,y, + // inTouch->centroid.x,inTouch->centroid.y); #ifdef ENABLE_DOLLAR - inTouch->dollarPath.length = 0; - inTouch->dollarPath.p[0].x = x; - inTouch->dollarPath.p[0].y = y; - inTouch->dollarPath.numPoints = 1; + inTouch->dollarPath.length = 0; + inTouch->dollarPath.p[0].x = x; + inTouch->dollarPath.p[0].y = y; + inTouch->dollarPath.numPoints = 1; #endif + } } - } } - /* vi: set ts=4 sw=4 expandtab: */ - +/* vi: set ts=4 sw=4 expandtab: */ From 3c27fa1effd8dd598dce64a2094f010fe178bd6f Mon Sep 17 00:00:00 2001 From: Kees Bakker Date: Thu, 27 Oct 2011 22:23:09 +0200 Subject: [PATCH 2/5] Reformat SDL_gesture.c (part 2, space after keywords) --- src/events/SDL_gesture.c | 143 +++++++++++++++++++-------------------- 1 file changed, 71 insertions(+), 72 deletions(-) diff --git a/src/events/SDL_gesture.c b/src/events/SDL_gesture.c index 40a2c71be..bae3d6723 100644 --- a/src/events/SDL_gesture.c +++ b/src/events/SDL_gesture.c @@ -84,7 +84,7 @@ static void PrintPath(SDL_FloatPoint *path) { int i; printf("Path:"); - for(i=0;i= 0) + if (touchId >= 0) return 1; } } @@ -109,7 +109,7 @@ unsigned long SDL_HashDollar(SDL_FloatPoint* points) { unsigned long hash = 5381; int i; - for(i = 0;i < DOLLARNPOINTS; i++) { + for (i = 0; i < DOLLARNPOINTS; i++) { hash = ((hash<<5) + hash) + (unsigned long)points[i].x; hash = ((hash<<5) + hash) + (unsigned long)points[i].y; } @@ -119,14 +119,14 @@ unsigned long SDL_HashDollar(SDL_FloatPoint* points) static int SaveTemplate(SDL_DollarTemplate *templ, SDL_RWops * src) { - if(src == NULL) return 0; + if (src == NULL) return 0; //No Longer storing the Hash, rehash on load //if(SDL_RWops.write(src,&(templ->hash),sizeof(templ->hash),1) != 1) return 0; - if(SDL_RWwrite(src,templ->path, - sizeof(templ->path[0]),DOLLARNPOINTS) != DOLLARNPOINTS) + if (SDL_RWwrite(src,templ->path, + sizeof(templ->path[0]),DOLLARNPOINTS) != DOLLARNPOINTS) return 0; return 1; @@ -136,9 +136,9 @@ static int SaveTemplate(SDL_DollarTemplate *templ, SDL_RWops * src) int SDL_SaveAllDollarTemplates(SDL_RWops *src) { int i,j,rtrn = 0; - for(i = 0; i < SDL_numGestureTouches; i++) { + for (i = 0; i < SDL_numGestureTouches; i++) { SDL_GestureTouch* touch = &SDL_gestureTouch[i]; - for(j = 0;j < touch->numDollarTemplates; j++) { + for (j = 0; j < touch->numDollarTemplates; j++) { rtrn += SaveTemplate(&touch->dollarTemplate[i],src); } } @@ -148,10 +148,10 @@ int SDL_SaveAllDollarTemplates(SDL_RWops *src) int SDL_SaveDollarTemplate(SDL_GestureID gestureId, SDL_RWops *src) { int i,j; - for(i = 0; i < SDL_numGestureTouches; i++) { + for (i = 0; i < SDL_numGestureTouches; i++) { SDL_GestureTouch* touch = &SDL_gestureTouch[i]; - for(j = 0;j < touch->numDollarTemplates; j++) { - if(touch->dollarTemplate[i].hash == gestureId) { + for (j = 0; j < touch->numDollarTemplates; j++) { + if (touch->dollarTemplate[i].hash == gestureId) { return SaveTemplate(&touch->dollarTemplate[i],src); } } @@ -167,16 +167,16 @@ static int SDL_AddDollarGesture(SDL_GestureTouch* inTouch,SDL_FloatPoint* path) SDL_DollarTemplate* dollarTemplate; SDL_DollarTemplate *templ; int i = 0; - if(inTouch == NULL) { - if(SDL_numGestureTouches == 0) return -1; - for(i = 0;i < SDL_numGestureTouches; i++) { + if (inTouch == NULL) { + if (SDL_numGestureTouches == 0) return -1; + for (i = 0; i < SDL_numGestureTouches; i++) { inTouch = &SDL_gestureTouch[i]; dollarTemplate = (SDL_DollarTemplate *)SDL_realloc(inTouch->dollarTemplate, (inTouch->numDollarTemplates + 1) * sizeof(SDL_DollarTemplate)); - if(!dollarTemplate) { + if (!dollarTemplate) { SDL_OutOfMemory(); return -1; } @@ -195,7 +195,7 @@ static int SDL_AddDollarGesture(SDL_GestureTouch* inTouch,SDL_FloatPoint* path) ( SDL_DollarTemplate *)SDL_realloc(inTouch->dollarTemplate, (inTouch->numDollarTemplates + 1) * sizeof(SDL_DollarTemplate)); - if(!dollarTemplate) { + if (!dollarTemplate) { SDL_OutOfMemory(); return -1; } @@ -216,27 +216,27 @@ int SDL_LoadDollarTemplates(SDL_TouchID touchId, SDL_RWops *src) { int i,loaded = 0; SDL_GestureTouch *touch = NULL; - if(src == NULL) return 0; - if(touchId >= 0) { - for(i = 0;i < SDL_numGestureTouches; i++) - if(SDL_gestureTouch[i].id == touchId) + if (src == NULL) return 0; + if (touchId >= 0) { + for (i = 0; i < SDL_numGestureTouches; i++) + if (SDL_gestureTouch[i].id == touchId) touch = &SDL_gestureTouch[i]; - if(touch == NULL) return -1; + if (touch == NULL) return -1; } - while(1) { + while (1) { SDL_DollarTemplate templ; - if(SDL_RWread(src,templ.path,sizeof(templ.path[0]),DOLLARNPOINTS) < + if (SDL_RWread(src,templ.path,sizeof(templ.path[0]),DOLLARNPOINTS) < DOLLARNPOINTS) break; - if(touchId >= 0) { + if (touchId >= 0) { //printf("Adding loaded gesture to 1 touch\n"); - if(SDL_AddDollarGesture(touch,templ.path)) loaded++; + if (SDL_AddDollarGesture(touch,templ.path)) loaded++; } else { //printf("Adding to: %i touches\n",SDL_numGestureTouches); - for(i = 0;i < SDL_numGestureTouches; i++) { + for (i = 0; i < SDL_numGestureTouches; i++) { touch = &SDL_gestureTouch[i]; //printf("Adding loaded gesture to + touches\n"); //TODO: What if this fails? @@ -256,7 +256,7 @@ float dollarDifference(SDL_FloatPoint* points,SDL_FloatPoint* templ,float ang) float dist = 0; SDL_FloatPoint p; int i; - for(i = 0; i < DOLLARNPOINTS; i++) { + for (i = 0; i < DOLLARNPOINTS; i++) { p.x = (float)(points[i].x * SDL_cos(ang) - points[i].y * SDL_sin(ang)); p.y = (float)(points[i].x * SDL_sin(ang) + points[i].y * SDL_cos(ang)); dist += (float)(SDL_sqrt((p.x-templ[i].x)*(p.x-templ[i].x)+ @@ -278,8 +278,8 @@ float bestDollarDifference(SDL_FloatPoint* points,SDL_FloatPoint* templ) float f1 = dollarDifference(points,templ,x1); float x2 = (float)((1-PHI)*ta + PHI*tb); float f2 = dollarDifference(points,templ,x2); - while(SDL_fabs(ta-tb) > dt) { - if(f1 < f2) { + while (SDL_fabs(ta-tb) > dt) { + if (f1 < f2) { tb = x2; x2 = x1; f2 = f1; @@ -295,10 +295,10 @@ float bestDollarDifference(SDL_FloatPoint* points,SDL_FloatPoint* templ) } } /* - if(f1 <= f2) - printf("Min angle (x1): %f\n",x1); - else if(f1 > f2) - printf("Min angle (x2): %f\n",x2); + if (f1 <= f2) + printf("Min angle (x1): %f\n",x1); + else if (f1 > f2) + printf("Min angle (x2): %f\n",x2); */ return SDL_min(f1,f2); } @@ -317,12 +317,10 @@ int dollarNormalize(const SDL_DollarPath *path,SDL_FloatPoint *points) float length = path->length; //Calculate length if it hasn't already been done - if(length <= 0) { - for(i=1;inumPoints;i++) { - float dx = path->p[i ].x - - path->p[i-1].x; - float dy = path->p[i ].y - - path->p[i-1].y; + if (length <= 0) { + for (i=1;i < path->numPoints; i++) { + float dx = path->p[i ].x - path->p[i-1].x; + float dy = path->p[i ].y - path->p[i-1].y; length += (float)(SDL_sqrt(dx*dx+dy*dy)); } } @@ -334,11 +332,11 @@ int dollarNormalize(const SDL_DollarPath *path,SDL_FloatPoint *points) centroid.x = 0;centroid.y = 0; //printf("(%f,%f)\n",path->p[path->numPoints-1].x,path->p[path->numPoints-1].y); - for(i = 1;i < path->numPoints;i++) { + for (i = 1; i < path->numPoints; i++) { float d = (float)(SDL_sqrt((path->p[i-1].x-path->p[i].x)*(path->p[i-1].x-path->p[i].x)+ (path->p[i-1].y-path->p[i].y)*(path->p[i-1].y-path->p[i].y))); //printf("d = %f dist = %f/%f\n",d,dist,interval); - while(dist + d > interval) { + while (dist + d > interval) { points[numPoints].x = path->p[i-1].x + ((interval-dist)/d)*(path->p[i].x-path->p[i-1].x); points[numPoints].y = path->p[i-1].y + @@ -351,7 +349,7 @@ int dollarNormalize(const SDL_DollarPath *path,SDL_FloatPoint *points) } dist += d; } - if(numPoints < DOLLARNPOINTS-1) { + if (numPoints < DOLLARNPOINTS-1) { SDL_SetError("ERROR: NumPoints = %i\n",numPoints); return 0; } @@ -372,7 +370,7 @@ int dollarNormalize(const SDL_DollarPath *path,SDL_FloatPoint *points) ang = (float)(SDL_atan2(centroid.y - points[0].y, centroid.x - points[0].x)); - for(i = 0;i xmax) xmax = points[i].x; - if(points[i].y < ymin) ymin = points[i].y; - if(points[i].y > ymax) ymax = points[i].y; + if (points[i].x < xmin) xmin = points[i].x; + if (points[i].x > xmax) xmax = points[i].x; + if (points[i].y < ymin) ymin = points[i].y; + if (points[i].y > ymax) ymax = points[i].y; } //Scale points to DOLLARSIZE, and translate to the origin w = xmax-xmin; h = ymax-ymin; - for(i=0;inumDollarTemplates;i++) { + for (i = 0; i < touch->numDollarTemplates; i++) { float diff = bestDollarDifference(points,touch->dollarTemplate[i].path); - if(diff < bestDiff) {bestDiff = diff; *bestTempl = i;} + if (diff < bestDiff) {bestDiff = diff; *bestTempl = i;} } return bestDiff; } @@ -422,7 +420,7 @@ int SDL_GestureAddTouch(SDL_Touch* touch) (SDL_numGestureTouches + 1) * sizeof(SDL_GestureTouch)); - if(!gestureTouch) { + if (!gestureTouch) { SDL_OutOfMemory(); return -1; } @@ -461,9 +459,10 @@ int SDL_GestureRemoveTouch(SDL_TouchID id) SDL_GestureTouch * SDL_GetGestureTouch(SDL_TouchID id) { int i; - for(i = 0;i < SDL_numGestureTouches; i++) { + for (i = 0; i < SDL_numGestureTouches; i++) { //printf("%i ?= %i\n",SDL_gestureTouch[i].id,id); - if(SDL_gestureTouch[i].id == id) return &SDL_gestureTouch[i]; + if (SDL_gestureTouch[i].id == id) + return &SDL_gestureTouch[i]; } return NULL; } @@ -524,13 +523,13 @@ void SDL_GestureProcessEvent(SDL_Event* event) float dtheta; float dDist; - if(event->type == SDL_FINGERMOTION || - event->type == SDL_FINGERDOWN || - event->type == SDL_FINGERUP) { + if (event->type == SDL_FINGERMOTION || + event->type == SDL_FINGERDOWN || + event->type == SDL_FINGERUP) { SDL_GestureTouch* inTouch = SDL_GetGestureTouch(event->tfinger.touchId); //Shouldn't be possible - if(inTouch == NULL) return; + if (inTouch == NULL) return; //printf("@ (%i,%i) with res: (%i,%i)\n",(int)event->tfinger.x, // (int)event->tfinger.y, @@ -542,24 +541,24 @@ void SDL_GestureProcessEvent(SDL_Event* event) //Finger Up - if(event->type == SDL_FINGERUP) { + if (event->type == SDL_FINGERUP) { inTouch->numDownFingers--; #ifdef ENABLE_DOLLAR - if(inTouch->recording) { + if (inTouch->recording) { inTouch->recording = SDL_FALSE; dollarNormalize(&inTouch->dollarPath,path); //PrintPath(path); - if(recordAll) { + if (recordAll) { index = SDL_AddDollarGesture(NULL,path); - for(i = 0;i < SDL_numGestureTouches; i++) + for (i = 0; i < SDL_numGestureTouches; i++) SDL_gestureTouch[i].recording = SDL_FALSE; } else { index = SDL_AddDollarGesture(inTouch,path); } - if(index >= 0) { + if (index >= 0) { SDL_SendDollarRecord(inTouch,inTouch->dollarTemplate[index].hash); } else { @@ -571,7 +570,7 @@ void SDL_GestureProcessEvent(SDL_Event* event) float error; error = dollarRecognize(&inTouch->dollarPath, &bestTempl,inTouch); - if(bestTempl >= 0){ + if (bestTempl >= 0){ //Send Event unsigned long gestureId = inTouch->dollarTemplate[bestTempl].hash; SDL_SendGestureDollar(inTouch,gestureId,error); @@ -580,20 +579,20 @@ void SDL_GestureProcessEvent(SDL_Event* event) } #endif //inTouch->gestureLast[j] = inTouch->gestureLast[inTouch->numDownFingers]; - if(inTouch->numDownFingers > 0) { + if (inTouch->numDownFingers > 0) { inTouch->centroid.x = (inTouch->centroid.x*(inTouch->numDownFingers+1)- x)/inTouch->numDownFingers; inTouch->centroid.y = (inTouch->centroid.y*(inTouch->numDownFingers+1)- y)/inTouch->numDownFingers; } } - else if(event->type == SDL_FINGERMOTION) { + else if (event->type == SDL_FINGERMOTION) { float dx = ((float)event->tfinger.dx)/(float)inTouch->res.x; float dy = ((float)event->tfinger.dy)/(float)inTouch->res.y; //printf("dx,dy: (%f,%f)\n",dx,dy); #ifdef ENABLE_DOLLAR SDL_DollarPath* path = &inTouch->dollarPath; - if(path->numPoints < MAXPATHSIZE) { + if (path->numPoints < MAXPATHSIZE) { path->p[path->numPoints].x = inTouch->centroid.x; path->p[path->numPoints].y = inTouch->centroid.y; pathDx = @@ -611,7 +610,7 @@ void SDL_GestureProcessEvent(SDL_Event* event) inTouch->centroid.x += dx/inTouch->numDownFingers; inTouch->centroid.y += dy/inTouch->numDownFingers; //printf("Centrid : (%f,%f)\n",inTouch->centroid.x,inTouch->centroid.y); - if(inTouch->numDownFingers > 1) { + if (inTouch->numDownFingers > 1) { SDL_FloatPoint lv; //Vector from centroid to last x,y position SDL_FloatPoint v; //Vector from centroid to current x,y position //lv = inTouch->gestureLast[j].cv; @@ -633,7 +632,7 @@ void SDL_GestureProcessEvent(SDL_Event* event) dtheta = (float)SDL_atan2(lv.x*v.y - lv.y*v.x,lv.x*v.x + lv.y*v.y); dDist = (Dist - lDist); - if(lDist == 0) {dDist = 0;dtheta = 0;} //To avoid impossible values + if (lDist == 0) {dDist = 0;dtheta = 0;} //To avoid impossible values //inTouch->gestureLast[j].dDist = dDist; //inTouch->gestureLast[j].dtheta = dtheta; @@ -659,7 +658,7 @@ void SDL_GestureProcessEvent(SDL_Event* event) //pressure? } - if(event->type == SDL_FINGERDOWN) { + if (event->type == SDL_FINGERDOWN) { inTouch->numDownFingers++; inTouch->centroid.x = (inTouch->centroid.x*(inTouch->numDownFingers - 1)+ From 9f6e4ab9ab2688a61aa2c146de70c8a49f4dbb10 Mon Sep 17 00:00:00 2001 From: Kees Bakker Date: Thu, 27 Oct 2011 22:52:03 +0200 Subject: [PATCH 3/5] Refactor SDL_AddDollarGesture --- src/events/SDL_gesture.c | 65 +++++++++++++++++++--------------------- 1 file changed, 30 insertions(+), 35 deletions(-) diff --git a/src/events/SDL_gesture.c b/src/events/SDL_gesture.c index bae3d6723..92e6c6de9 100644 --- a/src/events/SDL_gesture.c +++ b/src/events/SDL_gesture.c @@ -162,52 +162,47 @@ int SDL_SaveDollarTemplate(SDL_GestureID gestureId, SDL_RWops *src) //path is an already sampled set of points //Returns the index of the gesture on success, or -1 -static int SDL_AddDollarGesture(SDL_GestureTouch* inTouch,SDL_FloatPoint* path) +static int SDL_AddDollarGesture_one(SDL_GestureTouch* inTouch, SDL_FloatPoint* path) { SDL_DollarTemplate* dollarTemplate; SDL_DollarTemplate *templ; + int index; + + index = inTouch->numDollarTemplates; + dollarTemplate = + (SDL_DollarTemplate *)SDL_realloc(inTouch->dollarTemplate, + (index + 1) * + sizeof(SDL_DollarTemplate)); + if (!dollarTemplate) { + SDL_OutOfMemory(); + return -1; + } + inTouch->dollarTemplate = dollarTemplate; + + templ = &inTouch->dollarTemplate[index]; + SDL_memcpy(templ->path, path, DOLLARNPOINTS*sizeof(SDL_FloatPoint)); + templ->hash = SDL_HashDollar(templ->path); + inTouch->numDollarTemplates++; + + return index; +} + +static int SDL_AddDollarGesture(SDL_GestureTouch* inTouch, SDL_FloatPoint* path) +{ + int index; int i = 0; if (inTouch == NULL) { if (SDL_numGestureTouches == 0) return -1; for (i = 0; i < SDL_numGestureTouches; i++) { inTouch = &SDL_gestureTouch[i]; - - dollarTemplate = - (SDL_DollarTemplate *)SDL_realloc(inTouch->dollarTemplate, - (inTouch->numDollarTemplates + 1) * - sizeof(SDL_DollarTemplate)); - if (!dollarTemplate) { - SDL_OutOfMemory(); + index = SDL_AddDollarGesture_one(inTouch, path); + if (index < 0) return -1; - } - - inTouch->dollarTemplate = dollarTemplate; - - templ = - &inTouch->dollarTemplate[inTouch->numDollarTemplates]; - SDL_memcpy(templ->path,path,DOLLARNPOINTS*sizeof(SDL_FloatPoint)); - templ->hash = SDL_HashDollar(templ->path); - inTouch->numDollarTemplates++; } - return inTouch->numDollarTemplates - 1; + // Use the index of the last one added. + return index; } else { - SDL_DollarTemplate* dollarTemplate = - ( SDL_DollarTemplate *)SDL_realloc(inTouch->dollarTemplate, - (inTouch->numDollarTemplates + 1) * - sizeof(SDL_DollarTemplate)); - if (!dollarTemplate) { - SDL_OutOfMemory(); - return -1; - } - - inTouch->dollarTemplate = dollarTemplate; - - templ = - &inTouch->dollarTemplate[inTouch->numDollarTemplates]; - SDL_memcpy(templ->path,path,DOLLARNPOINTS*sizeof(SDL_FloatPoint)); - templ->hash = SDL_HashDollar(templ->path); - inTouch->numDollarTemplates++; - return inTouch->numDollarTemplates - 1; + return SDL_AddDollarGesture_one(inTouch, path); } return -1; } From 9e6ee9e79af50c5a11166c8e7d80a0f773e9efa9 Mon Sep 17 00:00:00 2001 From: Kees Bakker Date: Thu, 27 Oct 2011 22:53:28 +0200 Subject: [PATCH 4/5] Correct usage of SDL_AddDollarGesture (return value >= 0 on success) --- src/events/SDL_gesture.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/events/SDL_gesture.c b/src/events/SDL_gesture.c index 92e6c6de9..a30615227 100644 --- a/src/events/SDL_gesture.c +++ b/src/events/SDL_gesture.c @@ -227,7 +227,8 @@ int SDL_LoadDollarTemplates(SDL_TouchID touchId, SDL_RWops *src) if (touchId >= 0) { //printf("Adding loaded gesture to 1 touch\n"); - if (SDL_AddDollarGesture(touch,templ.path)) loaded++; + if (SDL_AddDollarGesture(touch, templ.path) >= 0) + loaded++; } else { //printf("Adding to: %i touches\n",SDL_numGestureTouches); From 8d5ba0eb269134e92d9389fdbe7b1ee8591969d7 Mon Sep 17 00:00:00 2001 From: Kees Bakker Date: Thu, 27 Oct 2011 23:04:50 +0200 Subject: [PATCH 5/5] Type of SDL_GestureTouch.id is SDL_TouchID (only for SDL_gesture.c internal) --- src/events/SDL_gesture.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/events/SDL_gesture.c b/src/events/SDL_gesture.c index a30615227..1cb789073 100644 --- a/src/events/SDL_gesture.c +++ b/src/events/SDL_gesture.c @@ -63,7 +63,7 @@ typedef struct { } SDL_DollarTemplate; typedef struct { - SDL_GestureID id; + SDL_TouchID id; SDL_FloatPoint res; SDL_FloatPoint centroid; SDL_DollarPath dollarPath;