src/events/SDL_gesture.c
author Jim Grandpre <jim.tla@gmail.com>
Fri, 06 Aug 2010 01:55:38 -0400
changeset 4684 f47c2640c667
parent 4683 15dfe42edbfd
child 4685 e0c3b09368a6
permissions -rw-r--r--
Re-enabled dollar gesture
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
jimtla@4678
    93
int SDL_RecordGesture(SDL_TouchID touchId) {
jim@4659
    94
  int i;
jim@4659
    95
  if(touchId < 0) recordAll = SDL_TRUE;
jim@4659
    96
  for(i = 0;i < numGestureTouches; i++) {
jim@4659
    97
    if((touchId < 0) || (gestureTouch[i].id == touchId)) {
jim@4659
    98
      gestureTouch[i].recording = SDL_TRUE;
jim@4659
    99
      if(touchId >= 0)
jim@4659
   100
	return 1;
jim@4659
   101
    }      
jim@4659
   102
  }
jim@4659
   103
  return (touchId < 0);
jim@4659
   104
}
jim@4659
   105
jim@4659
   106
unsigned long SDL_HashDollar(Point* points) {
jim@4659
   107
  unsigned long hash = 5381;
jim@4659
   108
  int i;
jim@4659
   109
  for(i = 0;i < DOLLARNPOINTS; i++) { 
jim@4659
   110
    hash = ((hash<<5) + hash) + points[i].x;
jim@4659
   111
    hash = ((hash<<5) + hash) + points[i].y;
jim@4659
   112
  }
jim@4659
   113
  return hash;
jim@4659
   114
}
jim@4659
   115
jim@4665
   116
jim@4665
   117
static int SaveTemplate(DollarTemplate *templ, SDL_RWops * src) {
jim@4664
   118
  if(src == NULL) return 0;
jim@4665
   119
jim@4659
   120
  int i;
jim@4664
   121
  
jim@4664
   122
  //No Longer storing the Hash, rehash on load
jim@4664
   123
  //if(SDL_RWops.write(src,&(templ->hash),sizeof(templ->hash),1) != 1) return 0;
jim@4665
   124
jim@4682
   125
  if(SDL_RWwrite(src,templ->path,
jim@4682
   126
		 sizeof(templ->path[0]),DOLLARNPOINTS) != DOLLARNPOINTS) 
jim@4682
   127
    return 0;
jim@4682
   128
jim@4664
   129
  return 1;
jim@4659
   130
}
jim@4659
   131
jim@4659
   132
jim@4664
   133
int SDL_SaveAllDollarTemplates(SDL_RWops *src) {  
jim@4659
   134
  int i,j,rtrn = 0;
jim@4659
   135
  for(i = 0; i < numGestureTouches; i++) {
jim@4659
   136
    GestureTouch* touch = &gestureTouch[i];
jim@4659
   137
    for(j = 0;j < touch->numDollarTemplates; j++) {
jim@4664
   138
	rtrn += SaveTemplate(&touch->dollarTemplate[i],src);
jim@4659
   139
    }
jim@4659
   140
  }
jim@4659
   141
  return rtrn;  
jim@4659
   142
}
jim@4659
   143
jimtla@4678
   144
int SDL_SaveDollarTemplate(SDL_GestureID gestureId, SDL_RWops *src) {
jim@4659
   145
  int i,j;
jim@4659
   146
  for(i = 0; i < numGestureTouches; i++) {
jim@4659
   147
    GestureTouch* touch = &gestureTouch[i];
jim@4659
   148
    for(j = 0;j < touch->numDollarTemplates; j++) {
jim@4659
   149
      if(touch->dollarTemplate[i].hash == gestureId) {
jim@4664
   150
	return SaveTemplate(&touch->dollarTemplate[i],src);
jim@4659
   151
      }
jim@4659
   152
    }
jim@4659
   153
  }
slouken@4663
   154
  SDL_SetError("Unknown gestureId");
slouken@4663
   155
  return -1;
slouken@4663
   156
}
slouken@4663
   157
slouken@4663
   158
//path is an already sampled set of points
slouken@4663
   159
//Returns the index of the gesture on success, or -1
slouken@4663
   160
static int SDL_AddDollarGesture(GestureTouch* inTouch,Point* path) {
slouken@4663
   161
  if(inTouch == NULL) {
slouken@4663
   162
    if(numGestureTouches == 0) return -1;
slouken@4663
   163
    int i = 0;
slouken@4663
   164
    for(i = 0;i < numGestureTouches; i++) {
slouken@4663
   165
      inTouch = &gestureTouch[i];
slouken@4663
   166
      if(inTouch->numDollarTemplates < MAXTEMPLATES) {
slouken@4663
   167
	DollarTemplate *templ = 
slouken@4663
   168
	  &inTouch->dollarTemplate[inTouch->numDollarTemplates];
slouken@4663
   169
	memcpy(templ->path,path,DOLLARNPOINTS*sizeof(Point));
slouken@4663
   170
	templ->hash = SDL_HashDollar(templ->path);
slouken@4663
   171
	inTouch->numDollarTemplates++;
slouken@4663
   172
      }
slouken@4663
   173
    }
slouken@4663
   174
    return inTouch->numDollarTemplates - 1;
slouken@4663
   175
  }else 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
    return inTouch->numDollarTemplates - 1;
slouken@4663
   182
  }
slouken@4663
   183
  return -1;
jim@4659
   184
}
jim@4659
   185
jimtla@4678
   186
int SDL_LoadDollarTemplates(SDL_TouchID touchId, SDL_RWops *src) {
jim@4664
   187
  if(src == NULL) return 0;
jim@4659
   188
  int i,loaded = 0;
jim@4659
   189
  GestureTouch *touch = NULL;
jim@4659
   190
  if(touchId >= 0) {
jim@4659
   191
    for(i = 0;i < numGestureTouches; i++)
jim@4659
   192
      if(gestureTouch[i].id == touchId)
jim@4659
   193
	touch = &gestureTouch[i];
jim@4659
   194
    if(touch == NULL) return -1;
jim@4659
   195
  }
jim@4659
   196
jim@4664
   197
  while(1) {
jim@4659
   198
    DollarTemplate templ;
jim@4664
   199
    //fscanf(fp,"%lu ",&templ.hash);
jim@4664
   200
    /*
jim@4659
   201
    for(i = 0;i < DOLLARNPOINTS; i++) {		
jim@4659
   202
      int x,y;
jim@4659
   203
      if(fscanf(fp,"%i %i ",&x,&y) != 2) break;
jim@4659
   204
      templ.path[i].x = x;
jim@4659
   205
      templ.path[i].y = y;
jim@4659
   206
    }
jim@4659
   207
    fscanf(fp,"\n");
jim@4664
   208
    */
jim@4664
   209
    if(SDL_RWread(src,templ.path,sizeof(templ.path[0]),DOLLARNPOINTS) < DOLLARNPOINTS) break;
jim@4659
   210
jim@4659
   211
    if(touchId >= 0) {
jim@4664
   212
      printf("Adding loaded gesture to 1 touch\n");
jim@4664
   213
      if(SDL_AddDollarGesture(touch,templ.path)) loaded++;
jim@4659
   214
    }
jim@4659
   215
    else {
jim@4664
   216
      printf("Adding to: %i touches\n",numGestureTouches);
jim@4659
   217
      for(i = 0;i < numGestureTouches; i++) {
jim@4664
   218
	touch = &gestureTouch[i];
jim@4664
   219
	printf("Adding loaded gesture to + touches\n");
jim@4664
   220
	//TODO: What if this fails?
jim@4664
   221
	SDL_AddDollarGesture(touch,templ.path);	
jim@4659
   222
      }
jim@4659
   223
      loaded++;
jim@4659
   224
    }
jim@4659
   225
  }
jim@4659
   226
jim@4664
   227
  return loaded; 
jim@4659
   228
}
jim@4659
   229
jim@4659
   230
jim@4658
   231
float dollarDifference(Point* points,Point* templ,float ang) {
jim@4658
   232
  //  Point p[DOLLARNPOINTS];
jim@4658
   233
  float dist = 0;
jim@4658
   234
  Point p;
jim@4658
   235
  int i;
jim@4658
   236
  for(i = 0; i < DOLLARNPOINTS; i++) {
jim@4658
   237
    p.x = points[i].x * cos(ang) - points[i].y * sin(ang);
jim@4658
   238
    p.y = points[i].x * sin(ang) + points[i].y * cos(ang);
jim@4658
   239
    dist += sqrt((p.x-templ[i].x)*(p.x-templ[i].x)+
jim@4658
   240
		 (p.y-templ[i].y)*(p.y-templ[i].y));
jim@4658
   241
  }
jim@4658
   242
  return dist/DOLLARNPOINTS;
jim@4658
   243
  
jim@4658
   244
}
jim@4658
   245
jim@4658
   246
float bestDollarDifference(Point* points,Point* templ) {
jim@4658
   247
  //------------BEGIN DOLLAR BLACKBOX----------------//
jim@4658
   248
  //-TRANSLATED DIRECTLY FROM PSUDEO-CODE AVAILABLE AT-//
jim@4658
   249
  //-"http://depts.washington.edu/aimgroup/proj/dollar/"-//
jim@4658
   250
  float ta = -M_PI/4;
jim@4658
   251
  float tb = M_PI/4;
jim@4658
   252
  float dt = M_PI/90;
jim@4658
   253
  float x1 = PHI*ta + (1-PHI)*tb;
jim@4658
   254
  float f1 = dollarDifference(points,templ,x1);
jim@4658
   255
  float x2 = (1-PHI)*ta + PHI*tb;
jim@4658
   256
  float f2 = dollarDifference(points,templ,x2);
jim@4658
   257
  while(abs(ta-tb) > dt) {
jim@4658
   258
    if(f1 < f2) {
jim@4658
   259
      tb = x2;
jim@4658
   260
      x2 = x1;
jim@4658
   261
      f2 = f1;
jim@4658
   262
      x1 = PHI*ta + (1-PHI)*tb;
jim@4658
   263
      f1 = dollarDifference(points,templ,x1);
jim@4658
   264
    }
jim@4658
   265
    else {
jim@4658
   266
      ta = x1;
jim@4658
   267
      x1 = x2;
jim@4658
   268
      f1 = f2;
jim@4658
   269
      x2 = (1-PHI)*ta + PHI*tb;
jim@4658
   270
      f2 = dollarDifference(points,templ,x2);
jim@4658
   271
    }
jim@4658
   272
  }
jim@4658
   273
  /*
jim@4658
   274
  if(f1 <= f2)
jim@4658
   275
    printf("Min angle (x1): %f\n",x1);
jim@4658
   276
  else if(f1 >  f2)
jim@4658
   277
    printf("Min angle (x2): %f\n",x2);
jim@4658
   278
  */
jim@4658
   279
  return SDL_min(f1,f2);  
jim@4658
   280
}
jim@4658
   281
jim@4658
   282
//DollarPath contains raw points, plus (possibly) the calculated length
jim@4658
   283
int dollarNormalize(DollarPath path,Point *points) {
jim@4658
   284
  int i;
jim@4658
   285
  //Calculate length if it hasn't already been done
jim@4658
   286
  if(path.length <= 0) {
jim@4658
   287
    for(i=1;i<path.numPoints;i++) {
jim@4658
   288
      float dx = path.p[i  ].x - 
jim@4658
   289
	         path.p[i-1].x;
jim@4658
   290
      float dy = path.p[i  ].y - 
jim@4658
   291
	         path.p[i-1].y;
jim@4658
   292
      path.length += sqrt(dx*dx+dy*dy);
jim@4658
   293
    }
jim@4658
   294
  }
jim@4658
   295
jim@4658
   296
jim@4658
   297
  //Resample
jim@4658
   298
  float interval = path.length/(DOLLARNPOINTS - 1);
jim@4658
   299
  float dist = 0;
jim@4658
   300
jim@4658
   301
  int numPoints = 0;
jim@4658
   302
  Point centroid; centroid.x = 0;centroid.y = 0;
jim@4658
   303
  //printf("(%f,%f)\n",path.p[path.numPoints-1].x,path.p[path.numPoints-1].y);
jim@4658
   304
  for(i = 1;i < path.numPoints;i++) {
jim@4658
   305
    float d = sqrt((path.p[i-1].x-path.p[i].x)*(path.p[i-1].x-path.p[i].x)+
jim@4658
   306
		   (path.p[i-1].y-path.p[i].y)*(path.p[i-1].y-path.p[i].y));
jim@4658
   307
    //printf("d = %f dist = %f/%f\n",d,dist,interval);
jim@4658
   308
    while(dist + d > interval) {
jim@4658
   309
      points[numPoints].x = path.p[i-1].x + 
jim@4658
   310
	((interval-dist)/d)*(path.p[i].x-path.p[i-1].x);
jim@4658
   311
      points[numPoints].y = path.p[i-1].y + 
jim@4658
   312
	((interval-dist)/d)*(path.p[i].y-path.p[i-1].y);
jim@4658
   313
      centroid.x += points[numPoints].x;
jim@4658
   314
      centroid.y += points[numPoints].y;
jim@4658
   315
      numPoints++;
jim@4658
   316
jim@4658
   317
      dist -= interval;
jim@4658
   318
    }
jim@4658
   319
    dist += d;
jim@4658
   320
  }
jim@4658
   321
  if(numPoints < 1) return 0;
jim@4658
   322
  centroid.x /= numPoints;
jim@4658
   323
  centroid.y /= numPoints;
jim@4658
   324
 
jim@4658
   325
  //printf("Centroid (%f,%f)",centroid.x,centroid.y);
jim@4658
   326
  //Rotate Points so point 0 is left of centroid and solve for the bounding box
jim@4658
   327
  float xmin,xmax,ymin,ymax;
jim@4658
   328
  xmin = centroid.x;
jim@4658
   329
  xmax = centroid.x;
jim@4658
   330
  ymin = centroid.y;
jim@4658
   331
  ymax = centroid.y;
jim@4658
   332
  
jim@4658
   333
  float ang = atan2(centroid.y - points[0].y,
jim@4658
   334
		    centroid.x - points[0].x);
jim@4658
   335
jim@4658
   336
  for(i = 0;i<numPoints;i++) {					       
jim@4658
   337
    float px = points[i].x;
jim@4658
   338
    float py = points[i].y;
jim@4658
   339
    points[i].x = (px - centroid.x)*cos(ang) - 
jim@4658
   340
                  (py - centroid.y)*sin(ang) + centroid.x;
jim@4658
   341
    points[i].y = (px - centroid.x)*sin(ang) + 
jim@4658
   342
                  (py - centroid.y)*cos(ang) + centroid.y;
jim@4658
   343
jim@4658
   344
jim@4658
   345
    if(points[i].x < xmin) xmin = points[i].x;
jim@4658
   346
    if(points[i].x > xmax) xmax = points[i].x; 
jim@4658
   347
    if(points[i].y < ymin) ymin = points[i].y;
jim@4658
   348
    if(points[i].y > ymax) ymax = points[i].y;
jim@4658
   349
  }
jim@4658
   350
jim@4658
   351
  //Scale points to DOLLARSIZE, and translate to the origin
jim@4658
   352
  float w = xmax-xmin;
jim@4658
   353
  float h = ymax-ymin;
jim@4658
   354
jim@4658
   355
  for(i=0;i<numPoints;i++) {
jim@4658
   356
    points[i].x = (points[i].x - centroid.x)*DOLLARSIZE/w;
jim@4658
   357
    points[i].y = (points[i].y - centroid.y)*DOLLARSIZE/h;
jim@4658
   358
  }  
jim@4658
   359
  return numPoints;
jim@4658
   360
}
jim@4658
   361
slouken@4663
   362
float dollarRecognize(DollarPath path,int *bestTempl,GestureTouch* touch) {
slouken@4663
   363
	
slouken@4663
   364
	Point points[DOLLARNPOINTS];
slouken@4663
   365
	int numPoints = dollarNormalize(path,points);
slouken@4663
   366
	int i;
slouken@4663
   367
	
slouken@4663
   368
	int bestDiff = 10000;
slouken@4663
   369
	*bestTempl = -1;
slouken@4663
   370
	for(i = 0;i < touch->numDollarTemplates;i++) {
slouken@4663
   371
		int diff = bestDollarDifference(points,touch->dollarTemplate[i].path);
slouken@4663
   372
		if(diff < bestDiff) {bestDiff = diff; *bestTempl = i;}
slouken@4663
   373
	}
slouken@4663
   374
	return bestDiff;
slouken@4663
   375
}
slouken@4663
   376
jim@4657
   377
int SDL_GestureAddTouch(SDL_Touch* touch) { 
jim@4657
   378
  if(numGestureTouches >= MAXTOUCHES) return -1;
jim@4657
   379
  
jim@4657
   380
  gestureTouch[numGestureTouches].res.x = touch->xres;
jim@4657
   381
  gestureTouch[numGestureTouches].res.y = touch->yres;
jim@4657
   382
  gestureTouch[numGestureTouches].numDownFingers = 0;
jim@4657
   383
jim@4657
   384
  gestureTouch[numGestureTouches].res.x = touch->xres;
jim@4657
   385
  gestureTouch[numGestureTouches].id = touch->id;
jim@4657
   386
jim@4658
   387
  gestureTouch[numGestureTouches].numDollarTemplates = 0;
jim@4658
   388
jim@4659
   389
  gestureTouch[numGestureTouches].recording = SDL_FALSE;
jim@4659
   390
jim@4657
   391
  numGestureTouches++;
jim@4657
   392
  return 0;
jim@4657
   393
}
jim@4657
   394
jimtla@4678
   395
GestureTouch * SDL_GetGestureTouch(SDL_TouchID id) {
jim@4657
   396
  int i;
jim@4657
   397
  for(i = 0;i < numGestureTouches; i++) {
jim@4657
   398
    //printf("%i ?= %i\n",gestureTouch[i].id,id);
jim@4657
   399
    if(gestureTouch[i].id == id) return &gestureTouch[i];
jim@4657
   400
  }
jim@4657
   401
  return NULL;
jim@4657
   402
}
jim@4657
   403
jim@4657
   404
int SDL_SendGestureMulti(GestureTouch* touch,float dTheta,float dDist) {
jim@4657
   405
  SDL_Event event;
jim@4657
   406
  event.mgesture.type = SDL_MULTIGESTURE;
jim@4657
   407
  event.mgesture.touchId = touch->id;
jim@4657
   408
  event.mgesture.x = touch->centroid.x;
jim@4657
   409
  event.mgesture.y = touch->centroid.y;
jim@4657
   410
  event.mgesture.dTheta = dTheta;
jim@4657
   411
  event.mgesture.dDist = dDist;  
jim@4683
   412
  event.mgesture.numFingers = touch->numDownFingers;
jim@4657
   413
  return SDL_PushEvent(&event) > 0;
jim@4657
   414
}
jim@4657
   415
jimtla@4678
   416
int SDL_SendGestureDollar(GestureTouch* touch,
jimtla@4678
   417
			  SDL_GestureID gestureId,float error) {
jim@4658
   418
  SDL_Event event;
jim@4658
   419
  event.dgesture.type = SDL_DOLLARGESTURE;
jim@4658
   420
  event.dgesture.touchId = touch->id;
jim@4658
   421
  /*
jim@4658
   422
    //TODO: Add this to give location of gesture?
jim@4658
   423
  event.mgesture.x = touch->centroid.x;
jim@4658
   424
  event.mgesture.y = touch->centroid.y;
jim@4658
   425
  */
jim@4658
   426
  event.dgesture.gestureId = gestureId;
jim@4658
   427
  event.dgesture.error = error;  
jim@4658
   428
  return SDL_PushEvent(&event) > 0;
jim@4658
   429
}
jim@4658
   430
jim@4659
   431
jimtla@4678
   432
int SDL_SendDollarRecord(GestureTouch* touch,SDL_GestureID gestureId) {
jim@4659
   433
  SDL_Event event;
jim@4659
   434
  event.dgesture.type = SDL_DOLLARRECORD;
jim@4659
   435
  event.dgesture.touchId = touch->id;
jim@4659
   436
  event.dgesture.gestureId = gestureId;
jim@4659
   437
jim@4659
   438
  return SDL_PushEvent(&event) > 0;
jim@4659
   439
}
jim@4659
   440
jim@4659
   441
jim@4657
   442
void SDL_GestureProcessEvent(SDL_Event* event)
jim@4657
   443
{
jim@4657
   444
  if(event->type == SDL_FINGERMOTION || 
jim@4657
   445
     event->type == SDL_FINGERDOWN ||
jim@4657
   446
     event->type == SDL_FINGERUP) {
jim@4657
   447
    GestureTouch* inTouch = SDL_GetGestureTouch(event->tfinger.touchId);
jim@4683
   448
    
jim@4657
   449
    //Shouldn't be possible
jim@4657
   450
    if(inTouch == NULL) return;
jim@4657
   451
    
jim@4683
   452
    //printf("@ (%i,%i) with res: (%i,%i)\n",(int)event->tfinger.x,
jim@4683
   453
    //	   (int)event->tfinger.y,
jim@4683
   454
    //   (int)inTouch->res.x,(int)inTouch->res.y);
jim@4683
   455
jim@4657
   456
    
jim@4683
   457
    float x = ((float)event->tfinger.x)/(float)inTouch->res.x;
jim@4683
   458
    float y = ((float)event->tfinger.y)/(float)inTouch->res.y;   
jim@4658
   459
jim@4658
   460
jim@4683
   461
    //Finger Up
jim@4683
   462
    if(event->type == SDL_FINGERUP) {
jim@4683
   463
      inTouch->numDownFingers--;
jim@4683
   464
      
jim@4683
   465
#ifdef ENABLE_DOLLAR
jim@4683
   466
      if(inTouch->recording) {
jim@4683
   467
	inTouch->recording = SDL_FALSE;
jim@4683
   468
	Point path[DOLLARNPOINTS];
jim@4684
   469
	dollarNormalize(inTouch->dollarPath,path);
jim@4683
   470
	int index;
jim@4683
   471
	if(recordAll) {
jim@4683
   472
	  index = SDL_AddDollarGesture(NULL,path);
jim@4683
   473
	  int i;
jim@4683
   474
	  for(i = 0;i < numGestureTouches; i++)
jim@4683
   475
	    gestureTouch[i].recording = SDL_FALSE;
jim@4657
   476
	}
jim@4657
   477
	else {
jim@4683
   478
	  index = SDL_AddDollarGesture(inTouch,path);
jim@4657
   479
	}
jim@4683
   480
	
jim@4683
   481
	if(index >= 0) {
jim@4683
   482
	  SDL_SendDollarRecord(inTouch,inTouch->dollarTemplate[index].hash);
jim@4683
   483
	}
jim@4683
   484
	else {
jim@4683
   485
	  SDL_SendDollarRecord(inTouch,-1);
jim@4683
   486
	}
jim@4683
   487
      }
jim@4683
   488
      else {	
jim@4683
   489
	int bestTempl;
jim@4683
   490
	float error;
jim@4684
   491
	error = dollarRecognize(inTouch->dollarPath,
jim@4683
   492
				&bestTempl,inTouch);
jim@4683
   493
	if(bestTempl >= 0){
jim@4683
   494
	  //Send Event
jim@4683
   495
	  unsigned long gestureId = inTouch->dollarTemplate[bestTempl].hash;
jim@4683
   496
	  SDL_SendGestureDollar(inTouch,gestureId,error);
jim@4684
   497
	  //printf ("%s\n",);("Dollar error: %f\n",error);
jim@4683
   498
	}
jim@4683
   499
      }
jim@4683
   500
#endif 
jim@4683
   501
      //inTouch->gestureLast[j] = inTouch->gestureLast[inTouch->numDownFingers];
jim@4683
   502
      if(inTouch->numDownFingers > 0) {
jim@4683
   503
	inTouch->centroid.x = (inTouch->centroid.x*(inTouch->numDownFingers+1)-
jim@4683
   504
			       x)/inTouch->numDownFingers;
jim@4683
   505
	inTouch->centroid.y = (inTouch->centroid.y*(inTouch->numDownFingers+1)-
jim@4683
   506
			       y)/inTouch->numDownFingers;
jim@4683
   507
      }
jim@4683
   508
    }
jim@4683
   509
    else if(event->type == SDL_FINGERMOTION) {
jim@4683
   510
      float dx = ((float)event->tfinger.dx)/(float)inTouch->res.x;
jim@4683
   511
      float dy = ((float)event->tfinger.dy)/(float)inTouch->res.y;
jim@4683
   512
      //printf("dx,dy: (%f,%f)\n",dx,dy); 
jim@4683
   513
#ifdef ENABLE_DOLLAR
jim@4684
   514
      DollarPath* path = &inTouch->dollarPath;
jim@4683
   515
      if(path->numPoints < MAXPATHSIZE) {
jim@4684
   516
	path->p[path->numPoints].x = inTouch->centroid.x;
jim@4684
   517
	path->p[path->numPoints].y = inTouch->centroid.y;
jim@4683
   518
	path->length += sqrt(dx*dx + dy*dy);
jim@4683
   519
	path->numPoints++;
jim@4683
   520
      }
jim@4683
   521
#endif
jim@4683
   522
      Point lastP;
jim@4683
   523
      lastP.x = x - dx;
jim@4683
   524
      lastP.y = y - dy;
jim@4683
   525
      Point lastCentroid;
jim@4683
   526
      lastCentroid = inTouch->centroid;
jim@4683
   527
      
jim@4683
   528
      inTouch->centroid.x += dx/inTouch->numDownFingers;
jim@4683
   529
      inTouch->centroid.y += dy/inTouch->numDownFingers;    
jim@4683
   530
      if(inTouch->numDownFingers > 1) {
jim@4683
   531
	Point lv; //Vector from centroid to last x,y position
jim@4683
   532
	Point v; //Vector from centroid to current x,y position
jim@4683
   533
	//lv = inTouch->gestureLast[j].cv;
jim@4683
   534
	lv.x = lastP.x - lastCentroid.x;
jim@4683
   535
	lv.y = lastP.y - lastCentroid.y;
jim@4683
   536
	float lDist = sqrt(lv.x*lv.x + lv.y*lv.y);
jim@4683
   537
	//printf("lDist = %f\n",lDist);
jim@4683
   538
	v.x = x - inTouch->centroid.x;
jim@4683
   539
	v.y = y - inTouch->centroid.y;
jim@4683
   540
	//inTouch->gestureLast[j].cv = v;
jim@4683
   541
	float Dist = sqrt(v.x*v.x+v.y*v.y);
jim@4683
   542
	// cos(dTheta) = (v . lv)/(|v| * |lv|)
jim@4683
   543
	
jim@4683
   544
	//Normalize Vectors to simplify angle calculation
jim@4683
   545
	lv.x/=lDist;
jim@4683
   546
	lv.y/=lDist;
jim@4683
   547
	v.x/=Dist;
jim@4683
   548
	v.y/=Dist;
jim@4683
   549
	float dtheta = atan2(lv.x*v.y - lv.y*v.x,lv.x*v.x + lv.y*v.y);
jim@4683
   550
	
jim@4683
   551
	float dDist = (Dist - lDist);
jim@4683
   552
	if(lDist == 0) {dDist = 0;dtheta = 0;} //To avoid impossible values
jim@4683
   553
	
jim@4683
   554
	//inTouch->gestureLast[j].dDist = dDist;
jim@4683
   555
	//inTouch->gestureLast[j].dtheta = dtheta;
jim@4683
   556
	
jim@4683
   557
	//printf("dDist = %f, dTheta = %f\n",dDist,dtheta);
jim@4683
   558
	//gdtheta = gdtheta*.9 + dtheta*.1;
jim@4683
   559
	//gdDist  =  gdDist*.9 +  dDist*.1
jim@4683
   560
	//knob.r += dDist/numDownFingers;
jim@4683
   561
	//knob.ang += dtheta;
jim@4683
   562
	//printf("thetaSum = %f, distSum = %f\n",gdtheta,gdDist);
jim@4683
   563
	//printf("id: %i dTheta = %f, dDist = %f\n",j,dtheta,dDist);
jim@4683
   564
	SDL_SendGestureMulti(inTouch,dtheta,dDist);
jim@4683
   565
      }
jim@4683
   566
      else {
jim@4683
   567
	//inTouch->gestureLast[j].dDist = 0;
jim@4683
   568
	//inTouch->gestureLast[j].dtheta = 0;
jim@4683
   569
	//inTouch->gestureLast[j].cv.x = 0;
jim@4683
   570
	//inTouch->gestureLast[j].cv.y = 0;
jim@4683
   571
      }
jim@4683
   572
      //inTouch->gestureLast[j].f.p.x = x;
jim@4683
   573
      //inTouch->gestureLast[j].f.p.y = y;
jim@4683
   574
      //break;
jim@4683
   575
      //pressure?
jim@4657
   576
    }
jim@4657
   577
    
jim@4683
   578
    if(event->type == SDL_FINGERDOWN) {
jim@4683
   579
jim@4657
   580
      inTouch->numDownFingers++;
jim@4657
   581
      inTouch->centroid.x = (inTouch->centroid.x*(inTouch->numDownFingers - 1)+ 
jim@4657
   582
			     x)/inTouch->numDownFingers;
jim@4657
   583
      inTouch->centroid.y = (inTouch->centroid.y*(inTouch->numDownFingers - 1)+
jim@4657
   584
			     y)/inTouch->numDownFingers;
jim@4683
   585
      printf("Finger Down: (%f,%f). Centroid: (%f,%f\n",x,y,
jim@4683
   586
	     inTouch->centroid.x,inTouch->centroid.y);
jim@4683
   587
      /*
jim@4683
   588
	inTouch->gestureLast[j].f.id = event->tfinger.fingerId;
jim@4683
   589
	inTouch->gestureLast[j].f.p.x  = x;
jim@4683
   590
	inTouch->gestureLast[j].f.p.y  = y;	
jim@4683
   591
	inTouch->gestureLast[j].cv.x = 0;
jim@4683
   592
	inTouch->gestureLast[j].cv.y = 0;
jim@4683
   593
      */
jim@4684
   594
#ifdef ENABLE_DOLLAR
jim@4684
   595
      inTouch->dollarPath.length = 0;
jim@4684
   596
      inTouch->dollarPath.p[0].x = x;
jim@4684
   597
      inTouch->dollarPath.p[0].y = y;
jim@4684
   598
      inTouch->dollarPath.numPoints = 1;
jim@4683
   599
#endif
jim@4657
   600
    }
jim@4657
   601
  }
jim@4683
   602
}
jim@4683
   603
jim@4657
   604
  /* vi: set ts=4 sw=4 expandtab: */
jim@4657
   605