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