Added some files I had previosuly missed

This commit is contained in:
Jim Grandpre 2010-07-11 01:15:39 -04:00
parent 746dc79478
commit 99ed0692e1
9 changed files with 316 additions and 319 deletions

View file

@ -2,6 +2,7 @@
#include <SDL.h>
#include <math.h>
#include <SDL_touch.h>
#include <SDL_gesture.h>
#define PI 3.1415926535897
#define PHI ((sqrt(5)-1)/2)
@ -159,171 +160,6 @@ void drawKnob(SDL_Surface* screen,Knob k) {
}
void drawDollarPath(SDL_Surface* screen,Point* points,int numPoints,
int rad,unsigned int col){
int i;
for(i=0;i<numPoints;i++) {
drawCircle(screen,points[i].x+screen->w/2,
points[i].y+screen->h/2,
rad,col);
}
}
float dollarDifference(Point* points,Point* templ,float ang) {
// Point p[DOLLARNPOINTS];
float dist = 0;
Point p;
int i;
for(i = 0; i < DOLLARNPOINTS; i++) {
p.x = points[i].x * cos(ang) - points[i].y * sin(ang);
p.y = points[i].x * sin(ang) + points[i].y * cos(ang);
dist += 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(Point* points,Point* templ) {
//------------BEGIN DOLLAR BLACKBOX----------------//
//-TRANSLATED DIRECTLY FROM PSUDEO-CODE AVAILABLE AT-//
//-"http://depts.washington.edu/aimgroup/proj/dollar/"-//
float ta = -PI/4;
float tb = PI/4;
float dt = PI/90;
float x1 = PHI*ta + (1-PHI)*tb;
float f1 = dollarDifference(points,templ,x1);
float x2 = (1-PHI)*ta + PHI*tb;
float f2 = dollarDifference(points,templ,x2);
while(abs(ta-tb) > dt) {
if(f1 < f2) {
tb = x2;
x2 = x1;
f2 = f1;
x1 = PHI*ta + (1-PHI)*tb;
f1 = dollarDifference(points,templ,x1);
}
else {
ta = x1;
x1 = x2;
f1 = f2;
x2 = (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);
}
float dollarRecognize(SDL_Surface* screen, DollarPath path,int *bestTempl) {
Point points[DOLLARNPOINTS];
int numPoints = dollarNormalize(path,points);
int i;
int k;
/*
for(k = 0;k<DOLLARNPOINTS;k++) {
printf("(%f,%f)\n",points[k].x,
points[k].y);
}
*/
drawDollarPath(screen,points,numPoints,-15,0xFF6600);
int bestDiff = 10000;
*bestTempl = -1;
for(i = 0;i < numDollarTemplates;i++) {
int diff = bestDollarDifference(points,dollarTemplate[i]);
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;
//Calculate length if it hasn't already been done
if(path.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;
path.length += sqrt(dx*dx+dy*dy);
}
}
//Resample
float interval = path.length/(DOLLARNPOINTS - 1);
float dist = 0;
int numPoints = 0;
Point centroid; 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 = 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 < 1) return 0;
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
float xmin,xmax,ymin,ymax;
xmin = centroid.x;
xmax = centroid.x;
ymin = centroid.y;
ymax = centroid.y;
float ang = atan2(centroid.y - points[0].y,
centroid.x - points[0].x);
for(i = 0;i<numPoints;i++) {
float px = points[i].x;
float py = points[i].y;
points[i].x = (px - centroid.x)*cos(ang) -
(py - centroid.y)*sin(ang) + centroid.x;
points[i].y = (px - centroid.x)*sin(ang) +
(py - centroid.y)*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;
}
//Scale points to DOLLARSIZE, and translate to the origin
float w = xmax-xmin;
float h = ymax-ymin;
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;
}
void DrawScreen(SDL_Surface* screen, int h)
{
int x, y, xm,ym,c;
@ -369,138 +205,20 @@ void DrawScreen(SDL_Surface* screen, int h)
//SDL_Finger* inFinger = SDL_GetFinger(inTouch,event.tfinger.fingerId);
float x = ((float)event.tfinger.x)/inTouch->xres;
float y = ((float)event.tfinger.y)/inTouch->yres;
int j,empty = -1;
for(j = 0;j<MAXFINGERS;j++) {
if(gestureLast[j].f.id == event.tfinger.fingerId) {
if(event.type == SDL_FINGERUP) {
numDownFingers--;
if(numDownFingers <= 1) {
gdtheta = 0;
gdDist = 0;
}
if(!keystat[32]){ //spacebar
int bestTempl;
float error = dollarRecognize(screen,dollarPath[j],&bestTempl);
if(bestTempl >= 0){
drawDollarPath(screen,dollarTemplate[bestTempl]
,DOLLARNPOINTS,-15,0x0066FF);
printf("Dollar error: %f\n",error);
}
}
else if(numDollarTemplates < MAXTEMPLATES) {
dollarNormalize(dollarPath[j],
dollarTemplate[numDollarTemplates]);
/*
int k;
for(k = 0;k<DOLLARNPOINTS;k++) {
printf("(%f,%f)\n",dollarTemplate[numDollarTemplates][i].x,
dollarTemplate[numDollarTemplates][i].y);
}*/
numDollarTemplates++;
}
gestureLast[j].f.id = -1;
break;
}
else {
dollarPath[j].p[dollarPath[j].numPoints].x = x;
dollarPath[j].p[dollarPath[j].numPoints].y = y;
float dx = (dollarPath[j].p[dollarPath[j].numPoints ].x-
dollarPath[j].p[dollarPath[j].numPoints-1].x);
float dy = (dollarPath[j].p[dollarPath[j].numPoints ].y-
dollarPath[j].p[dollarPath[j].numPoints-1].y);
dollarPath[j].length += sqrt(dx*dx + dy*dy);
dollarPath[j].numPoints++;
centroid.x = centroid.x + dx/numDownFingers;
centroid.y = centroid.y + dy/numDownFingers;
if(numDownFingers > 1) {
Point lv; //Vector from centroid to last x,y position
Point v; //Vector from centroid to current x,y position
lv.x = gestureLast[j].cv.x;
lv.y = gestureLast[j].cv.y;
float lDist = sqrt(lv.x*lv.x + lv.y*lv.y);
v.x = x - centroid.x;
v.y = y - centroid.y;
gestureLast[j].cv = v;
float Dist = sqrt(v.x*v.x+v.y*v.y);
// cos(dTheta) = (v . lv)/(|v| * |lv|)
lv.x/=lDist;
lv.y/=lDist;
v.x/=Dist;
v.y/=Dist;
float dtheta = atan2(lv.x*v.y - lv.y*v.x,lv.x*v.x + lv.y*v.y);
float dDist = (lDist - Dist);
gestureLast[j].dDist = dDist;
gestureLast[j].dtheta = dtheta;
//gdtheta = gdtheta*.9 + dtheta*.1;
//gdDist = gdDist*.9 + dDist*.1
gdtheta += dtheta;
gdDist += dDist;
//printf("thetaSum = %f, distSum = %f\n",gdtheta,gdDist);
//printf("id: %i dTheta = %f, dDist = %f\n",j,dtheta,dDist);
}
else {
gestureLast[j].dDist = 0;
gestureLast[j].dtheta = 0;
gestureLast[j].cv.x = 0;
gestureLast[j].cv.y = 0;
}
gestureLast[j].f.p.x = x;
gestureLast[j].f.p.y = y;
break;
//pressure?
}
}
else if(gestureLast[j].f.id == -1 && empty == -1) {
empty = j;
}
}
if(j >= MAXFINGERS && empty >= 0) {
// printf("Finger Down!!!\n");
numDownFingers++;
centroid.x = (centroid.x*(numDownFingers - 1) + x)/numDownFingers;
centroid.y = (centroid.y*(numDownFingers - 1) + y)/numDownFingers;
j = empty;
gestureLast[j].f.id = event.tfinger.fingerId;
gestureLast[j].f.p.x = x;
gestureLast[j].f.p.y = y;
dollarPath[j].length = 0;
dollarPath[j].p[0].x = x;
dollarPath[j].p[0].y = y;
dollarPath[j].numPoints = 1;
}
float y = ((float)event.tfinger.y)/inTouch->yres;
//draw the touch:
if(gestureLast[j].f.id < 0) continue; //Finger up. Or some error...
unsigned int c = colors[gestureLast[j].f.id%7];
unsigned int c = colors[event.tfinger.touchId%7];
unsigned int col =
((unsigned int)(c*(.1+.85))) |
((unsigned int)((0xFF*(1-((float)age)/EVENT_BUF_SIZE))) & 0xFF)<<24;
x = gestureLast[j].f.p.x;
y = gestureLast[j].f.p.y;
if(event.type == SDL_FINGERMOTION)
drawCircle(screen,x*screen->w,y*screen->h,5,col);
else if(event.type == SDL_FINGERDOWN)
drawCircle(screen,x*screen->w,y*screen->h,-10,col);
/*
//if there is a centroid, draw it
if(numDownFingers > 1) {
unsigned int col =
@ -508,6 +226,7 @@ void DrawScreen(SDL_Surface* screen, int h)
((unsigned int)((0xFF*(1-((float)age)/EVENT_BUF_SIZE))) & 0xFF)<<24;
drawCircle(screen,centroid.x*screen->w,centroid.y*screen->h,5,col);
}
*/
}
}
@ -582,6 +301,22 @@ int main(int argc, char* argv[])
case SDL_KEYDOWN:
//printf("%i\n",event.key.keysym.sym);
keystat[event.key.keysym.sym] = 1;
if(event.key.keysym.sym == 32) {
SDL_RecordGesture(-1);
}
else if(event.key.keysym.sym == 115) {
FILE *fp;
fp = fopen("gestureSave","w");
SDL_SaveAllDollarTemplates(fp);
fclose(fp);
}
else if(event.key.keysym.sym == 108) {
FILE *fp;
fp = fopen("gestureSave","r");
printf("Loaded: %i\n",SDL_LoadDollarTemplates(-1,fp));
fclose(fp);
}
//keypress = 1;
break;
case SDL_KEYUP:
@ -665,20 +400,21 @@ int main(int argc, char* argv[])
knob.p.y = event.mgesture.y;
knob.ang += event.mgesture.dTheta;
knob.r += event.mgesture.dDist;
break;
case SDL_DOLLARGESTURE:
printf("Gesture %lu performed, error: %f\n",
event.dgesture.gestureId,
event.dgesture.error);
break;
case SDL_DOLLARRECORD:
printf("Recorded gesture: %lu\n",event.dgesture.gestureId);
break;
}
//And draw
}
DrawScreen(screen,h);
//printf("c: (%f,%f)\n",centroid.x,centroid.y);
//printf("numDownFingers: %i\n",numDownFingers);
//for(i=0;i<512;i++)
// if(keystat[i]) printf("%i\n",i);
DrawScreen(screen,h);
for(i = 0; i < 256; i++)
if(keystat[i])
printf("Key %i down\n",i);
}
SDL_Quit();

Binary file not shown.

Binary file not shown.