Started trying to build gesture code for iPhone

This commit is contained in:
Sam Lantinga 2010-07-15 06:51:16 -07:00
parent 987edb15b3
commit 92be476e14
2 changed files with 71 additions and 47 deletions

View file

@ -110,13 +110,14 @@ unsigned long SDL_HashDollar(Point* points) {
return hash;
}
int SaveTemplate(DollarTemplate *templ, FILE *fp) {
static int SaveTemplate(DollarTemplate *templ, FILE *fp) {
int i;
fprintf(fp,"%lu ",templ->hash);
for(i = 0;i < DOLLARNPOINTS;i++) {
fprintf(fp,"%i %i ",(int)templ->path[i].x,(int)templ->path[i].y);
}
fprintf(fp,"\n");
return 0;
}
@ -141,6 +142,36 @@ int SDL_SaveDollarTemplate(unsigned long gestureId, FILE *fp) {
}
}
}
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(GestureTouch* inTouch,Point* path) {
if(inTouch == NULL) {
if(numGestureTouches == 0) return -1;
int i = 0;
for(i = 0;i < numGestureTouches; i++) {
inTouch = &gestureTouch[i];
if(inTouch->numDollarTemplates < MAXTEMPLATES) {
DollarTemplate *templ =
&inTouch->dollarTemplate[inTouch->numDollarTemplates];
memcpy(templ->path,path,DOLLARNPOINTS*sizeof(Point));
templ->hash = SDL_HashDollar(templ->path);
inTouch->numDollarTemplates++;
}
}
return inTouch->numDollarTemplates - 1;
}else if(inTouch->numDollarTemplates < MAXTEMPLATES) {
DollarTemplate *templ =
&inTouch->dollarTemplate[inTouch->numDollarTemplates];
memcpy(templ->path,path,DOLLARNPOINTS*sizeof(Point));
templ->hash = SDL_HashDollar(templ->path);
inTouch->numDollarTemplates++;
return inTouch->numDollarTemplates - 1;
}
return -1;
}
int SDL_LoadDollarTemplates(int touchId, FILE *fp) {
@ -182,37 +213,6 @@ int SDL_LoadDollarTemplates(int touchId, FILE *fp) {
}
//path is an already sampled set of points
//Returns the index of the gesture on success, or -1
int SDL_AddDollarGesture(GestureTouch* inTouch,Point* path) {
if(inTouch == NULL) {
if(numGestureTouches == 0) return -1;
int i = 0;
for(i = 0;i < numGestureTouches; i++) {
inTouch = &gestureTouch[i];
if(inTouch->numDollarTemplates < MAXTEMPLATES) {
DollarTemplate *templ =
&inTouch->dollarTemplate[inTouch->numDollarTemplates];
memcpy(templ->path,path,DOLLARNPOINTS*sizeof(Point));
templ->hash = SDL_HashDollar(templ->path);
inTouch->numDollarTemplates++;
}
}
return inTouch->numDollarTemplates - 1;
}else if(inTouch->numDollarTemplates < MAXTEMPLATES) {
DollarTemplate *templ =
&inTouch->dollarTemplate[inTouch->numDollarTemplates];
memcpy(templ->path,path,DOLLARNPOINTS*sizeof(Point));
templ->hash = SDL_HashDollar(templ->path);
inTouch->numDollarTemplates++;
return inTouch->numDollarTemplates - 1;
}
return -1;
}
float dollarDifference(Point* points,Point* templ,float ang) {
// Point p[DOLLARNPOINTS];
float dist = 0;
@ -264,21 +264,6 @@ float bestDollarDifference(Point* points,Point* templ) {
return SDL_min(f1,f2);
}
float dollarRecognize(DollarPath path,int *bestTempl,GestureTouch* touch) {
Point points[DOLLARNPOINTS];
int numPoints = dollarNormalize(path,points);
int i;
int bestDiff = 10000;
*bestTempl = -1;
for(i = 0;i < touch->numDollarTemplates;i++) {
int diff = bestDollarDifference(points,touch->dollarTemplate[i].path);
if(diff < bestDiff) {bestDiff = diff; *bestTempl = i;}
}
return bestDiff;
}
//DollarPath contains raw points, plus (possibly) the calculated length
int dollarNormalize(DollarPath path,Point *points) {
int i;
@ -359,6 +344,21 @@ int dollarNormalize(DollarPath path,Point *points) {
return numPoints;
}
float dollarRecognize(DollarPath path,int *bestTempl,GestureTouch* touch) {
Point points[DOLLARNPOINTS];
int numPoints = dollarNormalize(path,points);
int i;
int bestDiff = 10000;
*bestTempl = -1;
for(i = 0;i < touch->numDollarTemplates;i++) {
int diff = bestDollarDifference(points,touch->dollarTemplate[i].path);
if(diff < bestDiff) {bestDiff = diff; *bestTempl = i;}
}
return bestDiff;
}
int SDL_GestureAddTouch(SDL_Touch* touch) {
if(numGestureTouches >= MAXTOUCHES) return -1;