Commit 311a3e94 authored by Ryan C. Gordon's avatar Ryan C. Gordon

Merge from https://bitbucket.org/keestux/sdl ... SDL_gesture code cleanup.

parents a61775dd 8d5ba0eb
......@@ -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;
......@@ -80,33 +80,36 @@ int SDL_numGestureTouches = 0;
SDL_bool recordAll;
#if 0
static void PrintPath(SDL_FloatPoint *path) {
static void PrintPath(SDL_FloatPoint *path)
{
int i;
printf("Path:");
for(i=0;i<DOLLARNPOINTS;i++) {
for (i=0; i<DOLLARNPOINTS; i++) {
printf(" (%f,%f)",path[i].x,path[i].y);
}
printf("\n");
}
#endif
int SDL_RecordGesture(SDL_TouchID touchId) {
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)) {
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)
if (touchId >= 0)
return 1;
}
}
return (touchId < 0);
}
static unsigned long SDL_HashDollar(SDL_FloatPoint* points) {
static 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;
}
......@@ -114,14 +117,15 @@ static unsigned long SDL_HashDollar(SDL_FloatPoint* points) {
}
static int SaveTemplate(SDL_DollarTemplate *templ, SDL_RWops * src) {
if(src == NULL) return 0;
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,
if (SDL_RWwrite(src,templ->path,
sizeof(templ->path[0]),DOLLARNPOINTS) != DOLLARNPOINTS)
return 0;
......@@ -129,23 +133,25 @@ static int SaveTemplate(SDL_DollarTemplate *templ, SDL_RWops * src) {
}
int SDL_SaveAllDollarTemplates(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);
}
}
return rtrn;
}
int SDL_SaveDollarTemplate(SDL_GestureID gestureId, 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);
}
}
......@@ -156,79 +162,77 @@ 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 i = 0;
if(inTouch == NULL) {
if(SDL_numGestureTouches == 0) return -1;
for(i = 0;i < SDL_numGestureTouches; i++) {
inTouch = &SDL_gestureTouch[i];
int index;
index = inTouch->numDollarTemplates;
dollarTemplate =
(SDL_DollarTemplate *)SDL_realloc(inTouch->dollarTemplate,
(inTouch->numDollarTemplates + 1) *
(index + 1) *
sizeof(SDL_DollarTemplate));
if(!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 = &inTouch->dollarTemplate[index];
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;
return index;
}
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;
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];
index = SDL_AddDollarGesture_one(inTouch, path);
if (index < 0)
return -1;
}
// Use the index of the last one added.
return index;
} else {
return SDL_AddDollarGesture_one(inTouch, path);
}
return -1;
}
int SDL_LoadDollarTemplates(SDL_TouchID touchId, SDL_RWops *src) {
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) >= 0)
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?
......@@ -242,12 +246,13 @@ int SDL_LoadDollarTemplates(SDL_TouchID touchId, SDL_RWops *src) {
}
static float dollarDifference(SDL_FloatPoint* points,SDL_FloatPoint* templ,float ang) {
static 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++) {
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)+
......@@ -257,7 +262,8 @@ static float dollarDifference(SDL_FloatPoint* points,SDL_FloatPoint* templ,float
}
static float bestDollarDifference(SDL_FloatPoint* points,SDL_FloatPoint* templ) {
static 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/"-//
......@@ -268,8 +274,8 @@ static 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;
......@@ -285,16 +291,17 @@ static float bestDollarDifference(SDL_FloatPoint* points,SDL_FloatPoint* templ)
}
}
/*
if(f1 <= f2)
if (f1 <= f2)
printf("Min angle (x1): %f\n",x1);
else if(f1 > f2)
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
static int dollarNormalize(const SDL_DollarPath *path,SDL_FloatPoint *points) {
static int dollarNormalize(const SDL_DollarPath *path,SDL_FloatPoint *points)
{
int i;
float interval;
float dist;
......@@ -306,12 +313,10 @@ static 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;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;
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));
}
}
......@@ -323,11 +328,11 @@ static 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 +
......@@ -340,7 +345,7 @@ static 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;
}
......@@ -361,7 +366,7 @@ static 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<numPoints;i++) {
for (i = 0; i<numPoints; i++) {
float px = points[i].x;
float py = points[i].y;
points[i].x = (float)((px - centroid.x)*SDL_cos(ang) -
......@@ -370,24 +375,25 @@ static int dollarNormalize(const SDL_DollarPath *path,SDL_FloatPoint *points) {
(py - centroid.y)*SDL_cos(ang) + centroid.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;
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;i<numPoints;i++) {
for (i=0; i<numPoints; i++) {
points[i].x = (points[i].x - centroid.x)*DOLLARSIZE/w;
points[i].y = (points[i].y - centroid.y)*DOLLARSIZE/h;
}
return numPoints;
}
static float dollarRecognize(const SDL_DollarPath *path,int *bestTempl,SDL_GestureTouch* touch) {
static float dollarRecognize(const SDL_DollarPath *path,int *bestTempl,SDL_GestureTouch* touch)
{
SDL_FloatPoint points[DOLLARNPOINTS];
int i;
......@@ -397,19 +403,20 @@ static float dollarRecognize(const SDL_DollarPath *path,int *bestTempl,SDL_Gestu
//PrintPath(points);
*bestTempl = -1;
for(i = 0;i < touch->numDollarTemplates;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;
}
int SDL_GestureAddTouch(SDL_Touch* touch) {
int SDL_GestureAddTouch(SDL_Touch* touch)
{
SDL_GestureTouch *gestureTouch = (SDL_GestureTouch *)SDL_realloc(SDL_gestureTouch,
(SDL_numGestureTouches + 1) *
sizeof(SDL_GestureTouch));
if(!gestureTouch) {
if (!gestureTouch) {
SDL_OutOfMemory();
return -1;
}
......@@ -431,16 +438,19 @@ int SDL_GestureAddTouch(SDL_Touch* touch) {
return 0;
}
static SDL_GestureTouch * SDL_GetGestureTouch(SDL_TouchID id) {
static 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;
}
static int SDL_SendGestureMulti(SDL_GestureTouch* touch,float dTheta,float dDist) {
int SDL_SendGestureMulti(SDL_GestureTouch* touch,float dTheta,float dDist)
{
SDL_Event event;
event.mgesture.type = SDL_MULTIGESTURE;
event.mgesture.touchId = touch->id;
......@@ -453,7 +463,8 @@ static int SDL_SendGestureMulti(SDL_GestureTouch* touch,float dTheta,float dDist
}
static int SDL_SendGestureDollar(SDL_GestureTouch* touch,
SDL_GestureID gestureId,float error) {
SDL_GestureID gestureId,float error)
{
SDL_Event event;
event.dgesture.type = SDL_DOLLARGESTURE;
event.dgesture.touchId = touch->id;
......@@ -470,7 +481,8 @@ static int SDL_SendGestureDollar(SDL_GestureTouch* touch,
}
static int SDL_SendDollarRecord(SDL_GestureTouch* touch,SDL_GestureID gestureId) {
static int SDL_SendDollarRecord(SDL_GestureTouch* touch,SDL_GestureID gestureId)
{
SDL_Event event;
event.dgesture.type = SDL_DOLLARRECORD;
event.dgesture.touchId = touch->id;
......@@ -493,13 +505,13 @@ void SDL_GestureProcessEvent(SDL_Event* event)
float dtheta;
float dDist;
if(event->type == SDL_FINGERMOTION ||
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,
......@@ -511,24 +523,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 {
......@@ -540,7 +552,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);
......@@ -549,20 +561,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 =
......@@ -580,7 +592,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;
......@@ -602,7 +614,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;
......@@ -628,7 +640,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)+
......@@ -648,5 +660,4 @@ void SDL_GestureProcessEvent(SDL_Event* event)
}
}
/* vi: set ts=4 sw=4 expandtab: */
/* vi: set ts=4 sw=4 expandtab: */
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment