src/events/SDL_gesture.c
author Jim Grandpre <jim.tla@gmail.com>
Sat, 07 Aug 2010 11:32:11 -0400
changeset 4685 e0c3b09368a6
parent 4684 f47c2640c667
child 4686 463cd74304b9
permissions -rw-r--r--
Fixed Dollar Recognition.
jim@4657
     1
/*
jim@4657
     2
    SDL - Simple DirectMedia Layer
jim@4657
     3
    Copyright (C) 1997-2010 Sam Lantinga
jim@4657
     4
jim@4657
     5
    This library is free software; you can redistribute it and/or
jim@4657
     6
    modify it under the terms of the GNU Lesser General Public
jim@4657
     7
    License as published by the Free Software Foundation; either
jim@4657
     8
    version 2.1 of the License, or (at your option) any later version.
jim@4657
     9
jim@4657
    10
    This library is distributed in the hope that it will be useful,
jim@4657
    11
    but WITHOUT ANY WARRANTY; without even the implied warranty of
jim@4657
    12
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
jim@4657
    13
    Lesser General Public License for more details.
jim@4657
    14
jim@4657
    15
    You should have received a copy of the GNU Lesser General Public
jim@4659
    16
    License along with this library; if not, write to the Free Software    Founation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
jim@4657
    17
jim@4657
    18
    Sam Lantinga
jim@4657
    19
    slouken@libsdl.org
jim@4657
    20
*/
jim@4657
    21
#include "SDL_config.h"
jim@4657
    22
jim@4657
    23
/* General mouse handling code for SDL */
jim@4657
    24
jim@4657
    25
#include "SDL_events.h"
jim@4657
    26
#include "SDL_events_c.h"
jim@4657
    27
#include "SDL_gesture_c.h"
jim@4657
    28
jim@4657
    29
//TODO: Replace with malloc
jim@4681
    30
#define MAXFINGERS 5
jim@4657
    31
#define MAXTOUCHES 2
jim@4658
    32
#define MAXTEMPLATES 4
jim@4658
    33
#define MAXPATHSIZE 1024
jim@4658
    34
jim@4658
    35
#define DOLLARNPOINTS 64
jim@4658
    36
#define DOLLARSIZE 256
jim@4658
    37
jim@4684
    38
#define ENABLE_DOLLAR
jim@4684
    39
jim@4658
    40
//PHI = ((sqrt(5)-1)/2)
jim@4658
    41
#define PHI 0.618033989 
jim@4657
    42
jim@4657
    43
typedef struct {
jim@4657
    44
  float x,y;
jim@4657
    45
} Point;
jim@4657
    46
jim@4657
    47
jim@4657
    48
typedef struct {
jim@4657
    49
  Point p;
jim@4657
    50
  float pressure;
jimtla@4678
    51
  SDL_FingerID id;
jim@4657
    52
} Finger;
jim@4657
    53
jim@4658
    54
jim@4658
    55
typedef struct {
jim@4658
    56
  float length;
jim@4658
    57
  
jim@4658
    58
  int numPoints;
jim@4658
    59
  Point p[MAXPATHSIZE];
jim@4658
    60
} DollarPath;
jim@4658
    61
jim@4684
    62
/*
jim@4657
    63
typedef struct {
jim@4657
    64
  Finger f;
jim@4657
    65
  Point cv;
jim@4657
    66
  float dtheta,dDist;
jim@4658
    67
  DollarPath dollarPath;
jim@4657
    68
} TouchPoint;
jim@4684
    69
*/
jim@4659
    70
typedef struct {
jim@4659
    71
  Point path[DOLLARNPOINTS];
jim@4659
    72
  unsigned long hash;
jim@4659
    73
} DollarTemplate;
jim@4657
    74
jim@4657
    75
typedef struct {
jimtla@4678
    76
  SDL_GestureID id;
jim@4657
    77
  Point res;
jim@4657
    78
  Point centroid;
jim@4684
    79
  //TouchPoint gestureLast[MAXFINGERS];
jim@4684
    80
  DollarPath dollarPath;
jim@4683
    81
  Uint16 numDownFingers;
jim@4658
    82
jim@4658
    83
  int numDollarTemplates;
jim@4659
    84
  DollarTemplate dollarTemplate[MAXTEMPLATES];
jim@4659
    85
jim@4659
    86
  SDL_bool recording;
jim@4657
    87
} GestureTouch;
jim@4657
    88
jim@4657
    89
GestureTouch gestureTouch[MAXTOUCHES];
jim@4657
    90
int numGestureTouches = 0;
jim@4659
    91
SDL_bool recordAll;
jim@4659
    92
jim@4685
    93
void SDL_PrintPath(Point *path) {
jim@4685
    94
  int i;
jim@4685
    95
  printf("Path:");
jim@4685
    96
  for(i=0;i<DOLLARNPOINTS;i++) {
jim@4685
    97
    printf(" (%f,%f)",path[i].x,path[i].y);
jim@4685
    98
  }
jim@4685
    99
  printf("\n");
jim@4685
   100
}
jim@4685
   101
jimtla@4678
   102
int SDL_RecordGesture(SDL_TouchID touchId) {
jim@4659
   103
  int i;
jim@4659
   104
  if(touchId < 0) recordAll = SDL_TRUE;
jim@4659
   105
  for(i = 0;i < numGestureTouches; i++) {
jim@4659
   106
    if((touchId < 0) || (gestureTouch[i].id == touchId)) {
jim@4659
   107
      gestureTouch[i].recording = SDL_TRUE;
jim@4659
   108
      if(touchId >= 0)
jim@4659
   109
	return 1;
jim@4659
   110
    }      
jim@4659
   111
  }
jim@4659
   112
  return (touchId < 0);
jim@4659
   113
}
jim@4659
   114
jim@4659
   115
unsigned long SDL_HashDollar(Point* points) {
jim@4659
   116
  unsigned long hash = 5381;
jim@4659
   117
  int i;
jim@4659
   118
  for(i = 0;i < DOLLARNPOINTS; i++) { 
jim@4659
   119
    hash = ((hash<<5) + hash) + points[i].x;
jim@4659
   120
    hash = ((hash<<5) + hash) + points[i].y;
jim@4659
   121
  }
jim@4659
   122
  return hash;
jim@4659
   123
}
jim@4659
   124
jim@4665
   125
jim@4665
   126
static int SaveTemplate(DollarTemplate *templ, SDL_RWops * src) {
jim@4664
   127
  if(src == NULL) return 0;
jim@4665
   128
jim@4659
   129
  int i;
jim@4664
   130
  
jim@4664
   131
  //No Longer storing the Hash, rehash on load
jim@4664
   132
  //if(SDL_RWops.write(src,&(templ->hash),sizeof(templ->hash),1) != 1) return 0;
jim@4665
   133
jim@4682
   134
  if(SDL_RWwrite(src,templ->path,
jim@4682
   135
		 sizeof(templ->path[0]),DOLLARNPOINTS) != DOLLARNPOINTS) 
jim@4682
   136
    return 0;
jim@4682
   137
jim@4664
   138
  return 1;
jim@4659
   139
}
jim@4659
   140
jim@4659
   141
jim@4664
   142
int SDL_SaveAllDollarTemplates(SDL_RWops *src) {  
jim@4659
   143
  int i,j,rtrn = 0;
jim@4659
   144
  for(i = 0; i < numGestureTouches; i++) {
jim@4659
   145
    GestureTouch* touch = &gestureTouch[i];
jim@4659
   146
    for(j = 0;j < touch->numDollarTemplates; j++) {
jim@4664
   147
	rtrn += SaveTemplate(&touch->dollarTemplate[i],src);
jim@4659
   148
    }
jim@4659
   149
  }
jim@4659
   150
  return rtrn;  
jim@4659
   151
}
jim@4659
   152
jimtla@4678
   153
int SDL_SaveDollarTemplate(SDL_GestureID gestureId, SDL_RWops *src) {
jim@4659
   154
  int i,j;
jim@4659
   155
  for(i = 0; i < numGestureTouches; i++) {
jim@4659
   156
    GestureTouch* touch = &gestureTouch[i];
jim@4659
   157
    for(j = 0;j < touch->numDollarTemplates; j++) {
jim@4659
   158
      if(touch->dollarTemplate[i].hash == gestureId) {
jim@4664
   159
	return SaveTemplate(&touch->dollarTemplate[i],src);
jim@4659
   160
      }
jim@4659
   161
    }
jim@4659
   162
  }
slouken@4663
   163
  SDL_SetError("Unknown gestureId");
slouken@4663
   164
  return -1;
slouken@4663
   165
}
slouken@4663
   166
slouken@4663
   167
//path is an already sampled set of points
slouken@4663
   168
//Returns the index of the gesture on success, or -1
slouken@4663
   169
static int SDL_AddDollarGesture(GestureTouch* inTouch,Point* path) {
slouken@4663
   170
  if(inTouch == NULL) {
slouken@4663
   171
    if(numGestureTouches == 0) return -1;
slouken@4663
   172
    int i = 0;
slouken@4663
   173
    for(i = 0;i < numGestureTouches; i++) {
slouken@4663
   174
      inTouch = &gestureTouch[i];
slouken@4663
   175
      if(inTouch->numDollarTemplates < MAXTEMPLATES) {
slouken@4663
   176
	DollarTemplate *templ = 
slouken@4663
   177
	  &inTouch->dollarTemplate[inTouch->numDollarTemplates];
slouken@4663
   178
	memcpy(templ->path,path,DOLLARNPOINTS*sizeof(Point));
slouken@4663
   179
	templ->hash = SDL_HashDollar(templ->path);
slouken@4663
   180
	inTouch->numDollarTemplates++;
slouken@4663
   181
      }
slouken@4663
   182
    }
slouken@4663
   183
    return inTouch->numDollarTemplates - 1;
slouken@4663
   184
  }else if(inTouch->numDollarTemplates < MAXTEMPLATES) {
slouken@4663
   185
    DollarTemplate *templ = 
slouken@4663
   186
      &inTouch->dollarTemplate[inTouch->numDollarTemplates];
slouken@4663
   187
    memcpy(templ->path,path,DOLLARNPOINTS*sizeof(Point));
slouken@4663
   188
    templ->hash = SDL_HashDollar(templ->path);
slouken@4663
   189
    inTouch->numDollarTemplates++;
slouken@4663
   190
    return inTouch->numDollarTemplates - 1;
slouken@4663
   191
  }
slouken@4663
   192
  return -1;
jim@4659
   193
}
jim@4659
   194
jimtla@4678
   195
int SDL_LoadDollarTemplates(SDL_TouchID touchId, SDL_RWops *src) {
jim@4664
   196
  if(src == NULL) return 0;
jim@4659
   197
  int i,loaded = 0;
jim@4659
   198
  GestureTouch *touch = NULL;
jim@4659
   199
  if(touchId >= 0) {
jim@4659
   200
    for(i = 0;i < numGestureTouches; i++)
jim@4659
   201
      if(gestureTouch[i].id == touchId)
jim@4659
   202
	touch = &gestureTouch[i];
jim@4659
   203
    if(touch == NULL) return -1;
jim@4659
   204
  }
jim@4659
   205
jim@4664
   206
  while(1) {
jim@4659
   207
    DollarTemplate templ;
jim@4664
   208
    //fscanf(fp,"%lu ",&templ.hash);
jim@4664
   209
    /*
jim@4659
   210
    for(i = 0;i < DOLLARNPOINTS; i++) {		
jim@4659
   211
      int x,y;
jim@4659
   212
      if(fscanf(fp,"%i %i ",&x,&y) != 2) break;
jim@4659
   213
      templ.path[i].x = x;
jim@4659
   214
      templ.path[i].y = y;
jim@4659
   215
    }
jim@4659
   216
    fscanf(fp,"\n");
jim@4664
   217
    */
jim@4664
   218
    if(SDL_RWread(src,templ.path,sizeof(templ.path[0]),DOLLARNPOINTS) < DOLLARNPOINTS) break;
jim@4659
   219
jim@4659
   220
    if(touchId >= 0) {
jim@4664
   221
      printf("Adding loaded gesture to 1 touch\n");
jim@4664
   222
      if(SDL_AddDollarGesture(touch,templ.path)) loaded++;
jim@4659
   223
    }
jim@4659
   224
    else {
jim@4664
   225
      printf("Adding to: %i touches\n",numGestureTouches);
jim@4659
   226
      for(i = 0;i < numGestureTouches; i++) {
jim@4664
   227
	touch = &gestureTouch[i];
jim@4664
   228
	printf("Adding loaded gesture to + touches\n");
jim@4664
   229
	//TODO: What if this fails?
jim@4664
   230
	SDL_AddDollarGesture(touch,templ.path);	
jim@4659
   231
      }
jim@4659
   232
      loaded++;
jim@4659
   233
    }
jim@4659
   234
  }
jim@4659
   235
jim@4664
   236
  return loaded; 
jim@4659
   237
}
jim@4659
   238
jim@4659
   239
jim@4658
   240
float dollarDifference(Point* points,Point* templ,float ang) {
jim@4658
   241
  //  Point p[DOLLARNPOINTS];
jim@4658
   242
  float dist = 0;
jim@4658
   243
  Point p;
jim@4658
   244
  int i;
jim@4658
   245
  for(i = 0; i < DOLLARNPOINTS; i++) {
jim@4658
   246
    p.x = points[i].x * cos(ang) - points[i].y * sin(ang);
jim@4658
   247
    p.y = points[i].x * sin(ang) + points[i].y * cos(ang);
jim@4658
   248
    dist += sqrt((p.x-templ[i].x)*(p.x-templ[i].x)+
jim@4658
   249
		 (p.y-templ[i].y)*(p.y-templ[i].y));
jim@4658
   250
  }
jim@4658
   251
  return dist/DOLLARNPOINTS;
jim@4658
   252
  
jim@4658
   253
}
jim@4658
   254
jim@4658
   255
float bestDollarDifference(Point* points,Point* templ) {
jim@4658
   256
  //------------BEGIN DOLLAR BLACKBOX----------------//
jim@4658
   257
  //-TRANSLATED DIRECTLY FROM PSUDEO-CODE AVAILABLE AT-//
jim@4658
   258
  //-"http://depts.washington.edu/aimgroup/proj/dollar/"-//
jim@4658
   259
  float ta = -M_PI/4;
jim@4658
   260
  float tb = M_PI/4;
jim@4658
   261
  float dt = M_PI/90;
jim@4658
   262
  float x1 = PHI*ta + (1-PHI)*tb;
jim@4658
   263
  float f1 = dollarDifference(points,templ,x1);
jim@4658
   264
  float x2 = (1-PHI)*ta + PHI*tb;
jim@4658
   265
  float f2 = dollarDifference(points,templ,x2);
jim@4658
   266
  while(abs(ta-tb) > dt) {
jim@4658
   267
    if(f1 < f2) {
jim@4658
   268
      tb = x2;
jim@4658
   269
      x2 = x1;
jim@4658
   270
      f2 = f1;
jim@4658
   271
      x1 = PHI*ta + (1-PHI)*tb;
jim@4658
   272
      f1 = dollarDifference(points,templ,x1);
jim@4658
   273
    }
jim@4658
   274
    else {
jim@4658
   275
      ta = x1;
jim@4658
   276
      x1 = x2;
jim@4658
   277
      f1 = f2;
jim@4658
   278
      x2 = (1-PHI)*ta + PHI*tb;
jim@4658
   279
      f2 = dollarDifference(points,templ,x2);
jim@4658
   280
    }
jim@4658
   281
  }
jim@4658
   282
  /*
jim@4658
   283
  if(f1 <= f2)
jim@4658
   284
    printf("Min angle (x1): %f\n",x1);
jim@4658
   285
  else if(f1 >  f2)
jim@4658
   286
    printf("Min angle (x2): %f\n",x2);
jim@4658
   287
  */
jim@4658
   288
  return SDL_min(f1,f2);  
jim@4658
   289
}
jim@4658
   290
jim@4658
   291
//DollarPath contains raw points, plus (possibly) the calculated length
jim@4658
   292
int dollarNormalize(DollarPath path,Point *points) {
jim@4658
   293
  int i;
jim@4658
   294
  //Calculate length if it hasn't already been done
jim@4685
   295
  printf("length: %f\n",path.length);
jim@4658
   296
  if(path.length <= 0) {
jim@4658
   297
    for(i=1;i<path.numPoints;i++) {
jim@4658
   298
      float dx = path.p[i  ].x - 
jim@4658
   299
	         path.p[i-1].x;
jim@4658
   300
      float dy = path.p[i  ].y - 
jim@4658
   301
	         path.p[i-1].y;
jim@4658
   302
      path.length += sqrt(dx*dx+dy*dy);
jim@4658
   303
    }
jim@4658
   304
  }
jim@4685
   305
  printf("New length: %f\n",path.length);
jim@4658
   306
jim@4658
   307
  //Resample
jim@4658
   308
  float interval = path.length/(DOLLARNPOINTS - 1);
jim@4685
   309
  float dist = interval;
jim@4658
   310
jim@4658
   311
  int numPoints = 0;
jim@4685
   312
  Point centroid; 
jim@4685
   313
  centroid.x = 0;centroid.y = 0;
jim@4685
   314
  
jim@4658
   315
  //printf("(%f,%f)\n",path.p[path.numPoints-1].x,path.p[path.numPoints-1].y);
jim@4658
   316
  for(i = 1;i < path.numPoints;i++) {
jim@4658
   317
    float d = sqrt((path.p[i-1].x-path.p[i].x)*(path.p[i-1].x-path.p[i].x)+
jim@4658
   318
		   (path.p[i-1].y-path.p[i].y)*(path.p[i-1].y-path.p[i].y));
jim@4658
   319
    //printf("d = %f dist = %f/%f\n",d,dist,interval);
jim@4658
   320
    while(dist + d > interval) {
jim@4658
   321
      points[numPoints].x = path.p[i-1].x + 
jim@4658
   322
	((interval-dist)/d)*(path.p[i].x-path.p[i-1].x);
jim@4658
   323
      points[numPoints].y = path.p[i-1].y + 
jim@4658
   324
	((interval-dist)/d)*(path.p[i].y-path.p[i-1].y);
jim@4658
   325
      centroid.x += points[numPoints].x;
jim@4658
   326
      centroid.y += points[numPoints].y;
jim@4658
   327
      numPoints++;
jim@4658
   328
jim@4658
   329
      dist -= interval;
jim@4658
   330
    }
jim@4658
   331
    dist += d;
jim@4658
   332
  }
jim@4685
   333
  if(numPoints < DOLLARNPOINTS-1) {
jim@4685
   334
    printf("ERROR: NumPoints = %i\n",numPoints); 
jim@4685
   335
    return 0;
jim@4685
   336
  }
jim@4685
   337
  //copy the last point
jim@4685
   338
  points[DOLLARNPOINTS-1] = path.p[path.numPoints-1];
jim@4685
   339
  numPoints = DOLLARNPOINTS;
jim@4685
   340
jim@4658
   341
  centroid.x /= numPoints;
jim@4658
   342
  centroid.y /= numPoints;
jim@4658
   343
 
jim@4658
   344
  //printf("Centroid (%f,%f)",centroid.x,centroid.y);
jim@4658
   345
  //Rotate Points so point 0 is left of centroid and solve for the bounding box
jim@4658
   346
  float xmin,xmax,ymin,ymax;
jim@4658
   347
  xmin = centroid.x;
jim@4658
   348
  xmax = centroid.x;
jim@4658
   349
  ymin = centroid.y;
jim@4658
   350
  ymax = centroid.y;
jim@4658
   351
  
jim@4658
   352
  float ang = atan2(centroid.y - points[0].y,
jim@4658
   353
		    centroid.x - points[0].x);
jim@4658
   354
jim@4658
   355
  for(i = 0;i<numPoints;i++) {					       
jim@4658
   356
    float px = points[i].x;
jim@4658
   357
    float py = points[i].y;
jim@4658
   358
    points[i].x = (px - centroid.x)*cos(ang) - 
jim@4658
   359
                  (py - centroid.y)*sin(ang) + centroid.x;
jim@4658
   360
    points[i].y = (px - centroid.x)*sin(ang) + 
jim@4658
   361
                  (py - centroid.y)*cos(ang) + centroid.y;
jim@4658
   362
jim@4658
   363
jim@4658
   364
    if(points[i].x < xmin) xmin = points[i].x;
jim@4658
   365
    if(points[i].x > xmax) xmax = points[i].x; 
jim@4658
   366
    if(points[i].y < ymin) ymin = points[i].y;
jim@4658
   367
    if(points[i].y > ymax) ymax = points[i].y;
jim@4658
   368
  }
jim@4658
   369
jim@4658
   370
  //Scale points to DOLLARSIZE, and translate to the origin
jim@4658
   371
  float w = xmax-xmin;
jim@4658
   372
  float h = ymax-ymin;
jim@4658
   373
jim@4658
   374
  for(i=0;i<numPoints;i++) {
jim@4658
   375
    points[i].x = (points[i].x - centroid.x)*DOLLARSIZE/w;
jim@4658
   376
    points[i].y = (points[i].y - centroid.y)*DOLLARSIZE/h;
jim@4658
   377
  }  
jim@4658
   378
  return numPoints;
jim@4658
   379
}
jim@4658
   380
slouken@4663
   381
float dollarRecognize(DollarPath path,int *bestTempl,GestureTouch* touch) {
slouken@4663
   382
	
slouken@4663
   383
	Point points[DOLLARNPOINTS];
slouken@4663
   384
	int numPoints = dollarNormalize(path,points);
jim@4685
   385
	SDL_PrintPath(points);
slouken@4663
   386
	int i;
slouken@4663
   387
	
slouken@4663
   388
	int bestDiff = 10000;
slouken@4663
   389
	*bestTempl = -1;
slouken@4663
   390
	for(i = 0;i < touch->numDollarTemplates;i++) {
slouken@4663
   391
		int diff = bestDollarDifference(points,touch->dollarTemplate[i].path);
slouken@4663
   392
		if(diff < bestDiff) {bestDiff = diff; *bestTempl = i;}
slouken@4663
   393
	}
slouken@4663
   394
	return bestDiff;
slouken@4663
   395
}
slouken@4663
   396
jim@4657
   397
int SDL_GestureAddTouch(SDL_Touch* touch) { 
jim@4657
   398
  if(numGestureTouches >= MAXTOUCHES) return -1;
jim@4657
   399
  
jim@4657
   400
  gestureTouch[numGestureTouches].res.x = touch->xres;
jim@4657
   401
  gestureTouch[numGestureTouches].res.y = touch->yres;
jim@4657
   402
  gestureTouch[numGestureTouches].numDownFingers = 0;
jim@4657
   403
jim@4657
   404
  gestureTouch[numGestureTouches].res.x = touch->xres;
jim@4657
   405
  gestureTouch[numGestureTouches].id = touch->id;
jim@4657
   406
jim@4658
   407
  gestureTouch[numGestureTouches].numDollarTemplates = 0;
jim@4658
   408
jim@4659
   409
  gestureTouch[numGestureTouches].recording = SDL_FALSE;
jim@4659
   410
jim@4657
   411
  numGestureTouches++;
jim@4657
   412
  return 0;
jim@4657
   413
}
jim@4657
   414
jimtla@4678
   415
GestureTouch * SDL_GetGestureTouch(SDL_TouchID id) {
jim@4657
   416
  int i;
jim@4657
   417
  for(i = 0;i < numGestureTouches; i++) {
jim@4657
   418
    //printf("%i ?= %i\n",gestureTouch[i].id,id);
jim@4657
   419
    if(gestureTouch[i].id == id) return &gestureTouch[i];
jim@4657
   420
  }
jim@4657
   421
  return NULL;
jim@4657
   422
}
jim@4657
   423
jim@4657
   424
int SDL_SendGestureMulti(GestureTouch* touch,float dTheta,float dDist) {
jim@4657
   425
  SDL_Event event;
jim@4657
   426
  event.mgesture.type = SDL_MULTIGESTURE;
jim@4657
   427
  event.mgesture.touchId = touch->id;
jim@4657
   428
  event.mgesture.x = touch->centroid.x;
jim@4657
   429
  event.mgesture.y = touch->centroid.y;
jim@4657
   430
  event.mgesture.dTheta = dTheta;
jim@4657
   431
  event.mgesture.dDist = dDist;  
jim@4683
   432
  event.mgesture.numFingers = touch->numDownFingers;
jim@4657
   433
  return SDL_PushEvent(&event) > 0;
jim@4657
   434
}
jim@4657
   435
jimtla@4678
   436
int SDL_SendGestureDollar(GestureTouch* touch,
jimtla@4678
   437
			  SDL_GestureID gestureId,float error) {
jim@4658
   438
  SDL_Event event;
jim@4658
   439
  event.dgesture.type = SDL_DOLLARGESTURE;
jim@4658
   440
  event.dgesture.touchId = touch->id;
jim@4658
   441
  /*
jim@4658
   442
    //TODO: Add this to give location of gesture?
jim@4658
   443
  event.mgesture.x = touch->centroid.x;
jim@4658
   444
  event.mgesture.y = touch->centroid.y;
jim@4658
   445
  */
jim@4658
   446
  event.dgesture.gestureId = gestureId;
jim@4658
   447
  event.dgesture.error = error;  
jim@4658
   448
  return SDL_PushEvent(&event) > 0;
jim@4658
   449
}
jim@4658
   450
jim@4659
   451
jimtla@4678
   452
int SDL_SendDollarRecord(GestureTouch* touch,SDL_GestureID gestureId) {
jim@4659
   453
  SDL_Event event;
jim@4659
   454
  event.dgesture.type = SDL_DOLLARRECORD;
jim@4659
   455
  event.dgesture.touchId = touch->id;
jim@4659
   456
  event.dgesture.gestureId = gestureId;
jim@4659
   457
jim@4659
   458
  return SDL_PushEvent(&event) > 0;
jim@4659
   459
}
jim@4659
   460
jim@4659
   461
jim@4657
   462
void SDL_GestureProcessEvent(SDL_Event* event)
jim@4657
   463
{
jim@4657
   464
  if(event->type == SDL_FINGERMOTION || 
jim@4657
   465
     event->type == SDL_FINGERDOWN ||
jim@4657
   466
     event->type == SDL_FINGERUP) {
jim@4657
   467
    GestureTouch* inTouch = SDL_GetGestureTouch(event->tfinger.touchId);
jim@4683
   468
    
jim@4657
   469
    //Shouldn't be possible
jim@4657
   470
    if(inTouch == NULL) return;
jim@4657
   471
    
jim@4683
   472
    //printf("@ (%i,%i) with res: (%i,%i)\n",(int)event->tfinger.x,
jim@4683
   473
    //	   (int)event->tfinger.y,
jim@4683
   474
    //   (int)inTouch->res.x,(int)inTouch->res.y);
jim@4683
   475
jim@4657
   476
    
jim@4683
   477
    float x = ((float)event->tfinger.x)/(float)inTouch->res.x;
jim@4683
   478
    float y = ((float)event->tfinger.y)/(float)inTouch->res.y;   
jim@4658
   479
jim@4658
   480
jim@4683
   481
    //Finger Up
jim@4683
   482
    if(event->type == SDL_FINGERUP) {
jim@4683
   483
      inTouch->numDownFingers--;
jim@4683
   484
      
jim@4683
   485
#ifdef ENABLE_DOLLAR
jim@4683
   486
      if(inTouch->recording) {
jim@4683
   487
	inTouch->recording = SDL_FALSE;
jim@4683
   488
	Point path[DOLLARNPOINTS];
jim@4684
   489
	dollarNormalize(inTouch->dollarPath,path);
jim@4685
   490
	SDL_PrintPath(path);
jim@4683
   491
	int index;
jim@4683
   492
	if(recordAll) {
jim@4683
   493
	  index = SDL_AddDollarGesture(NULL,path);
jim@4683
   494
	  int i;
jim@4683
   495
	  for(i = 0;i < numGestureTouches; i++)
jim@4683
   496
	    gestureTouch[i].recording = SDL_FALSE;
jim@4657
   497
	}
jim@4657
   498
	else {
jim@4683
   499
	  index = SDL_AddDollarGesture(inTouch,path);
jim@4657
   500
	}
jim@4683
   501
	
jim@4683
   502
	if(index >= 0) {
jim@4683
   503
	  SDL_SendDollarRecord(inTouch,inTouch->dollarTemplate[index].hash);
jim@4683
   504
	}
jim@4683
   505
	else {
jim@4683
   506
	  SDL_SendDollarRecord(inTouch,-1);
jim@4683
   507
	}
jim@4683
   508
      }
jim@4683
   509
      else {	
jim@4683
   510
	int bestTempl;
jim@4683
   511
	float error;
jim@4684
   512
	error = dollarRecognize(inTouch->dollarPath,
jim@4683
   513
				&bestTempl,inTouch);
jim@4683
   514
	if(bestTempl >= 0){
jim@4683
   515
	  //Send Event
jim@4683
   516
	  unsigned long gestureId = inTouch->dollarTemplate[bestTempl].hash;
jim@4683
   517
	  SDL_SendGestureDollar(inTouch,gestureId,error);
jim@4684
   518
	  //printf ("%s\n",);("Dollar error: %f\n",error);
jim@4683
   519
	}
jim@4683
   520
      }
jim@4683
   521
#endif 
jim@4683
   522
      //inTouch->gestureLast[j] = inTouch->gestureLast[inTouch->numDownFingers];
jim@4683
   523
      if(inTouch->numDownFingers > 0) {
jim@4683
   524
	inTouch->centroid.x = (inTouch->centroid.x*(inTouch->numDownFingers+1)-
jim@4683
   525
			       x)/inTouch->numDownFingers;
jim@4683
   526
	inTouch->centroid.y = (inTouch->centroid.y*(inTouch->numDownFingers+1)-
jim@4683
   527
			       y)/inTouch->numDownFingers;
jim@4683
   528
      }
jim@4683
   529
    }
jim@4683
   530
    else if(event->type == SDL_FINGERMOTION) {
jim@4683
   531
      float dx = ((float)event->tfinger.dx)/(float)inTouch->res.x;
jim@4683
   532
      float dy = ((float)event->tfinger.dy)/(float)inTouch->res.y;
jim@4683
   533
      //printf("dx,dy: (%f,%f)\n",dx,dy); 
jim@4683
   534
#ifdef ENABLE_DOLLAR
jim@4684
   535
      DollarPath* path = &inTouch->dollarPath;
jim@4683
   536
      if(path->numPoints < MAXPATHSIZE) {
jim@4684
   537
	path->p[path->numPoints].x = inTouch->centroid.x;
jim@4684
   538
	path->p[path->numPoints].y = inTouch->centroid.y;
jim@4685
   539
	float pathDx = 
jim@4685
   540
	  (path->p[path->numPoints].x-path->p[path->numPoints-1].x);
jim@4685
   541
	float pathDy = 
jim@4685
   542
	  (path->p[path->numPoints].y-path->p[path->numPoints-1].y);
jim@4685
   543
	path->length += sqrt(pathDx*pathDx + pathDy*pathDy);
jim@4683
   544
	path->numPoints++;
jim@4683
   545
      }
jim@4683
   546
#endif
jim@4683
   547
      Point lastP;
jim@4683
   548
      lastP.x = x - dx;
jim@4683
   549
      lastP.y = y - dy;
jim@4683
   550
      Point lastCentroid;
jim@4683
   551
      lastCentroid = inTouch->centroid;
jim@4683
   552
      
jim@4683
   553
      inTouch->centroid.x += dx/inTouch->numDownFingers;
jim@4685
   554
      inTouch->centroid.y += dy/inTouch->numDownFingers;
jim@4685
   555
      //printf("Centrid : (%f,%f)\n",inTouch->centroid.x,inTouch->centroid.y);
jim@4683
   556
      if(inTouch->numDownFingers > 1) {
jim@4683
   557
	Point lv; //Vector from centroid to last x,y position
jim@4683
   558
	Point v; //Vector from centroid to current x,y position
jim@4683
   559
	//lv = inTouch->gestureLast[j].cv;
jim@4683
   560
	lv.x = lastP.x - lastCentroid.x;
jim@4683
   561
	lv.y = lastP.y - lastCentroid.y;
jim@4683
   562
	float lDist = sqrt(lv.x*lv.x + lv.y*lv.y);
jim@4683
   563
	//printf("lDist = %f\n",lDist);
jim@4683
   564
	v.x = x - inTouch->centroid.x;
jim@4683
   565
	v.y = y - inTouch->centroid.y;
jim@4683
   566
	//inTouch->gestureLast[j].cv = v;
jim@4683
   567
	float Dist = sqrt(v.x*v.x+v.y*v.y);
jim@4683
   568
	// cos(dTheta) = (v . lv)/(|v| * |lv|)
jim@4683
   569
	
jim@4683
   570
	//Normalize Vectors to simplify angle calculation
jim@4683
   571
	lv.x/=lDist;
jim@4683
   572
	lv.y/=lDist;
jim@4683
   573
	v.x/=Dist;
jim@4683
   574
	v.y/=Dist;
jim@4683
   575
	float dtheta = atan2(lv.x*v.y - lv.y*v.x,lv.x*v.x + lv.y*v.y);
jim@4683
   576
	
jim@4683
   577
	float dDist = (Dist - lDist);
jim@4683
   578
	if(lDist == 0) {dDist = 0;dtheta = 0;} //To avoid impossible values
jim@4683
   579
	
jim@4683
   580
	//inTouch->gestureLast[j].dDist = dDist;
jim@4683
   581
	//inTouch->gestureLast[j].dtheta = dtheta;
jim@4683
   582
	
jim@4683
   583
	//printf("dDist = %f, dTheta = %f\n",dDist,dtheta);
jim@4683
   584
	//gdtheta = gdtheta*.9 + dtheta*.1;
jim@4683
   585
	//gdDist  =  gdDist*.9 +  dDist*.1
jim@4683
   586
	//knob.r += dDist/numDownFingers;
jim@4683
   587
	//knob.ang += dtheta;
jim@4683
   588
	//printf("thetaSum = %f, distSum = %f\n",gdtheta,gdDist);
jim@4683
   589
	//printf("id: %i dTheta = %f, dDist = %f\n",j,dtheta,dDist);
jim@4683
   590
	SDL_SendGestureMulti(inTouch,dtheta,dDist);
jim@4683
   591
      }
jim@4683
   592
      else {
jim@4683
   593
	//inTouch->gestureLast[j].dDist = 0;
jim@4683
   594
	//inTouch->gestureLast[j].dtheta = 0;
jim@4683
   595
	//inTouch->gestureLast[j].cv.x = 0;
jim@4683
   596
	//inTouch->gestureLast[j].cv.y = 0;
jim@4683
   597
      }
jim@4683
   598
      //inTouch->gestureLast[j].f.p.x = x;
jim@4683
   599
      //inTouch->gestureLast[j].f.p.y = y;
jim@4683
   600
      //break;
jim@4683
   601
      //pressure?
jim@4657
   602
    }
jim@4657
   603
    
jim@4683
   604
    if(event->type == SDL_FINGERDOWN) {
jim@4683
   605
jim@4657
   606
      inTouch->numDownFingers++;
jim@4657
   607
      inTouch->centroid.x = (inTouch->centroid.x*(inTouch->numDownFingers - 1)+ 
jim@4657
   608
			     x)/inTouch->numDownFingers;
jim@4657
   609
      inTouch->centroid.y = (inTouch->centroid.y*(inTouch->numDownFingers - 1)+
jim@4657
   610
			     y)/inTouch->numDownFingers;
jim@4683
   611
      printf("Finger Down: (%f,%f). Centroid: (%f,%f\n",x,y,
jim@4683
   612
	     inTouch->centroid.x,inTouch->centroid.y);
jim@4683
   613
      /*
jim@4683
   614
	inTouch->gestureLast[j].f.id = event->tfinger.fingerId;
jim@4683
   615
	inTouch->gestureLast[j].f.p.x  = x;
jim@4683
   616
	inTouch->gestureLast[j].f.p.y  = y;	
jim@4683
   617
	inTouch->gestureLast[j].cv.x = 0;
jim@4683
   618
	inTouch->gestureLast[j].cv.y = 0;
jim@4683
   619
      */
jim@4684
   620
#ifdef ENABLE_DOLLAR
jim@4684
   621
      inTouch->dollarPath.length = 0;
jim@4684
   622
      inTouch->dollarPath.p[0].x = x;
jim@4684
   623
      inTouch->dollarPath.p[0].y = y;
jim@4684
   624
      inTouch->dollarPath.numPoints = 1;
jim@4683
   625
#endif
jim@4657
   626
    }
jim@4657
   627
  }
jim@4683
   628
}
jim@4683
   629
jim@4657
   630
  /* vi: set ts=4 sw=4 expandtab: */
jim@4657
   631