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