src/events/SDL_gesture.c
author Jim Grandpre <jim.tla@gmail.com>
Sun, 11 Jul 2010 01:15:39 -0400
changeset 4659 063b9455bd1a
parent 4658 454385d76845
child 4663 56a2d70de945
child 4664 317a151b79ad
permissions -rw-r--r--
Added some files I had previosuly missed
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;
jim@4657
    49
  int 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 {
jim@4657
    74
  int 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
jim@4659
    90
int SDL_RecordGesture(int 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@4659
   113
int SaveTemplate(DollarTemplate *templ, FILE *fp) {
jim@4659
   114
  int i;
jim@4659
   115
  fprintf(fp,"%lu ",templ->hash);
jim@4659
   116
  for(i = 0;i < DOLLARNPOINTS;i++) {
jim@4659
   117
    fprintf(fp,"%i %i ",(int)templ->path[i].x,(int)templ->path[i].y);
jim@4659
   118
  }
jim@4659
   119
  fprintf(fp,"\n");
jim@4659
   120
}
jim@4659
   121
jim@4659
   122
jim@4659
   123
int SDL_SaveAllDollarTemplates(FILE *fp) {  
jim@4659
   124
  int i,j,rtrn = 0;
jim@4659
   125
  for(i = 0; i < numGestureTouches; i++) {
jim@4659
   126
    GestureTouch* touch = &gestureTouch[i];
jim@4659
   127
    for(j = 0;j < touch->numDollarTemplates; j++) {
jim@4659
   128
	rtrn += SaveTemplate(&touch->dollarTemplate[i],fp);
jim@4659
   129
    }
jim@4659
   130
  }
jim@4659
   131
  return rtrn;  
jim@4659
   132
}
jim@4659
   133
jim@4659
   134
int SDL_SaveDollarTemplate(unsigned long gestureId, FILE *fp) {
jim@4659
   135
  int i,j;
jim@4659
   136
  for(i = 0; i < numGestureTouches; i++) {
jim@4659
   137
    GestureTouch* touch = &gestureTouch[i];
jim@4659
   138
    for(j = 0;j < touch->numDollarTemplates; j++) {
jim@4659
   139
      if(touch->dollarTemplate[i].hash == gestureId) {
jim@4659
   140
	return SaveTemplate(&touch->dollarTemplate[i],fp);
jim@4659
   141
      }
jim@4659
   142
    }
jim@4659
   143
  }
jim@4659
   144
}
jim@4659
   145
jim@4659
   146
int SDL_LoadDollarTemplates(int touchId, FILE *fp) {
jim@4659
   147
  int i,loaded = 0;
jim@4659
   148
  GestureTouch *touch = NULL;
jim@4659
   149
  if(touchId >= 0) {
jim@4659
   150
    for(i = 0;i < numGestureTouches; i++)
jim@4659
   151
      if(gestureTouch[i].id == touchId)
jim@4659
   152
	touch = &gestureTouch[i];
jim@4659
   153
    if(touch == NULL) return -1;
jim@4659
   154
  }
jim@4659
   155
jim@4659
   156
  while(!feof(fp)) {
jim@4659
   157
    DollarTemplate templ;
jim@4659
   158
    fscanf(fp,"%lu ",&templ.hash);
jim@4659
   159
    for(i = 0;i < DOLLARNPOINTS; i++) {		
jim@4659
   160
      int x,y;
jim@4659
   161
      if(fscanf(fp,"%i %i ",&x,&y) != 2) break;
jim@4659
   162
      templ.path[i].x = x;
jim@4659
   163
      templ.path[i].y = y;
jim@4659
   164
    }
jim@4659
   165
    fscanf(fp,"\n");
jim@4659
   166
jim@4659
   167
    if(touchId >= 0) {
jim@4659
   168
      if(SDL_AddDollarGesture(touch,templ)) loaded++;
jim@4659
   169
    }
jim@4659
   170
    else {
jim@4659
   171
      for(i = 0;i < numGestureTouches; i++) {
jim@4659
   172
	if(gestureTouch[i].id == touchId) {
jim@4659
   173
	  touch = &gestureTouch[i];
jim@4659
   174
	  SDL_AddDollarGesture(touch,templ);
jim@4659
   175
	}
jim@4659
   176
      }
jim@4659
   177
      loaded++;
jim@4659
   178
    }
jim@4659
   179
  }
jim@4659
   180
jim@4659
   181
  return 1; 
jim@4659
   182
}
jim@4659
   183
jim@4659
   184
jim@4659
   185
//path is an already sampled set of points
jim@4659
   186
//Returns the index of the gesture on success, or -1
jim@4659
   187
int SDL_AddDollarGesture(GestureTouch* inTouch,Point* path) {
jim@4659
   188
  if(inTouch == NULL) {
jim@4659
   189
    if(numGestureTouches == 0) return -1;
jim@4659
   190
    int i = 0;
jim@4659
   191
    for(i = 0;i < numGestureTouches; i++) {
jim@4659
   192
      inTouch = &gestureTouch[i];
jim@4659
   193
      if(inTouch->numDollarTemplates < MAXTEMPLATES) {
jim@4659
   194
	DollarTemplate *templ = 
jim@4659
   195
	  &inTouch->dollarTemplate[inTouch->numDollarTemplates];
jim@4659
   196
	memcpy(templ->path,path,DOLLARNPOINTS*sizeof(Point));
jim@4659
   197
	templ->hash = SDL_HashDollar(templ->path);
jim@4659
   198
	inTouch->numDollarTemplates++;
jim@4659
   199
      }
jim@4659
   200
    }
jim@4659
   201
    return inTouch->numDollarTemplates - 1;
jim@4659
   202
  }else if(inTouch->numDollarTemplates < MAXTEMPLATES) {
jim@4659
   203
    DollarTemplate *templ = 
jim@4659
   204
      &inTouch->dollarTemplate[inTouch->numDollarTemplates];
jim@4659
   205
    memcpy(templ->path,path,DOLLARNPOINTS*sizeof(Point));
jim@4659
   206
    templ->hash = SDL_HashDollar(templ->path);
jim@4659
   207
    inTouch->numDollarTemplates++;
jim@4659
   208
    return inTouch->numDollarTemplates - 1;
jim@4659
   209
  }
jim@4659
   210
  return -1;
jim@4659
   211
}
jim@4659
   212
jim@4659
   213
jim@4659
   214
jim@4658
   215
jim@4658
   216
float dollarDifference(Point* points,Point* templ,float ang) {
jim@4658
   217
  //  Point p[DOLLARNPOINTS];
jim@4658
   218
  float dist = 0;
jim@4658
   219
  Point p;
jim@4658
   220
  int i;
jim@4658
   221
  for(i = 0; i < DOLLARNPOINTS; i++) {
jim@4658
   222
    p.x = points[i].x * cos(ang) - points[i].y * sin(ang);
jim@4658
   223
    p.y = points[i].x * sin(ang) + points[i].y * cos(ang);
jim@4658
   224
    dist += sqrt((p.x-templ[i].x)*(p.x-templ[i].x)+
jim@4658
   225
		 (p.y-templ[i].y)*(p.y-templ[i].y));
jim@4658
   226
  }
jim@4658
   227
  return dist/DOLLARNPOINTS;
jim@4658
   228
  
jim@4658
   229
}
jim@4658
   230
jim@4658
   231
float bestDollarDifference(Point* points,Point* templ) {
jim@4658
   232
  //------------BEGIN DOLLAR BLACKBOX----------------//
jim@4658
   233
  //-TRANSLATED DIRECTLY FROM PSUDEO-CODE AVAILABLE AT-//
jim@4658
   234
  //-"http://depts.washington.edu/aimgroup/proj/dollar/"-//
jim@4658
   235
  float ta = -M_PI/4;
jim@4658
   236
  float tb = M_PI/4;
jim@4658
   237
  float dt = M_PI/90;
jim@4658
   238
  float x1 = PHI*ta + (1-PHI)*tb;
jim@4658
   239
  float f1 = dollarDifference(points,templ,x1);
jim@4658
   240
  float x2 = (1-PHI)*ta + PHI*tb;
jim@4658
   241
  float f2 = dollarDifference(points,templ,x2);
jim@4658
   242
  while(abs(ta-tb) > dt) {
jim@4658
   243
    if(f1 < f2) {
jim@4658
   244
      tb = x2;
jim@4658
   245
      x2 = x1;
jim@4658
   246
      f2 = f1;
jim@4658
   247
      x1 = PHI*ta + (1-PHI)*tb;
jim@4658
   248
      f1 = dollarDifference(points,templ,x1);
jim@4658
   249
    }
jim@4658
   250
    else {
jim@4658
   251
      ta = x1;
jim@4658
   252
      x1 = x2;
jim@4658
   253
      f1 = f2;
jim@4658
   254
      x2 = (1-PHI)*ta + PHI*tb;
jim@4658
   255
      f2 = dollarDifference(points,templ,x2);
jim@4658
   256
    }
jim@4658
   257
  }
jim@4658
   258
  /*
jim@4658
   259
  if(f1 <= f2)
jim@4658
   260
    printf("Min angle (x1): %f\n",x1);
jim@4658
   261
  else if(f1 >  f2)
jim@4658
   262
    printf("Min angle (x2): %f\n",x2);
jim@4658
   263
  */
jim@4658
   264
  return SDL_min(f1,f2);  
jim@4658
   265
}
jim@4658
   266
jim@4658
   267
float dollarRecognize(DollarPath path,int *bestTempl,GestureTouch* touch) {
jim@4658
   268
jim@4658
   269
  Point points[DOLLARNPOINTS];
jim@4658
   270
  int numPoints = dollarNormalize(path,points);
jim@4658
   271
  int i;
jim@4658
   272
 
jim@4658
   273
  int bestDiff = 10000;
jim@4658
   274
  *bestTempl = -1;
jim@4658
   275
  for(i = 0;i < touch->numDollarTemplates;i++) {
jim@4659
   276
    int diff = bestDollarDifference(points,touch->dollarTemplate[i].path);
jim@4658
   277
    if(diff < bestDiff) {bestDiff = diff; *bestTempl = i;}
jim@4658
   278
  }
jim@4658
   279
  return bestDiff;
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
jim@4657
   362
int SDL_GestureAddTouch(SDL_Touch* touch) { 
jim@4657
   363
  if(numGestureTouches >= MAXTOUCHES) return -1;
jim@4657
   364
  
jim@4657
   365
  gestureTouch[numGestureTouches].res.x = touch->xres;
jim@4657
   366
  gestureTouch[numGestureTouches].res.y = touch->yres;
jim@4657
   367
  gestureTouch[numGestureTouches].numDownFingers = 0;
jim@4657
   368
jim@4657
   369
  gestureTouch[numGestureTouches].res.x = touch->xres;
jim@4657
   370
  gestureTouch[numGestureTouches].id = touch->id;
jim@4657
   371
jim@4658
   372
  gestureTouch[numGestureTouches].numDollarTemplates = 0;
jim@4658
   373
jim@4659
   374
  gestureTouch[numGestureTouches].recording = SDL_FALSE;
jim@4659
   375
jim@4657
   376
  numGestureTouches++;
jim@4657
   377
  return 0;
jim@4657
   378
}
jim@4657
   379
jim@4657
   380
GestureTouch * SDL_GetGestureTouch(int id) {
jim@4657
   381
  int i;
jim@4657
   382
  for(i = 0;i < numGestureTouches; i++) {
jim@4657
   383
    //printf("%i ?= %i\n",gestureTouch[i].id,id);
jim@4657
   384
    if(gestureTouch[i].id == id) return &gestureTouch[i];
jim@4657
   385
  }
jim@4657
   386
  return NULL;
jim@4657
   387
}
jim@4657
   388
jim@4657
   389
int SDL_SendGestureMulti(GestureTouch* touch,float dTheta,float dDist) {
jim@4657
   390
  SDL_Event event;
jim@4657
   391
  event.mgesture.type = SDL_MULTIGESTURE;
jim@4657
   392
  event.mgesture.touchId = touch->id;
jim@4657
   393
  event.mgesture.x = touch->centroid.x;
jim@4657
   394
  event.mgesture.y = touch->centroid.y;
jim@4657
   395
  event.mgesture.dTheta = dTheta;
jim@4657
   396
  event.mgesture.dDist = dDist;  
jim@4657
   397
  return SDL_PushEvent(&event) > 0;
jim@4657
   398
}
jim@4657
   399
jim@4658
   400
int SDL_SendGestureDollar(GestureTouch* touch,int gestureId,float error) {
jim@4658
   401
  SDL_Event event;
jim@4658
   402
  event.dgesture.type = SDL_DOLLARGESTURE;
jim@4658
   403
  event.dgesture.touchId = touch->id;
jim@4658
   404
  /*
jim@4658
   405
    //TODO: Add this to give location of gesture?
jim@4658
   406
  event.mgesture.x = touch->centroid.x;
jim@4658
   407
  event.mgesture.y = touch->centroid.y;
jim@4658
   408
  */
jim@4658
   409
  event.dgesture.gestureId = gestureId;
jim@4658
   410
  event.dgesture.error = error;  
jim@4658
   411
  return SDL_PushEvent(&event) > 0;
jim@4658
   412
}
jim@4658
   413
jim@4659
   414
jim@4659
   415
int SDL_SendDollarRecord(GestureTouch* touch,int gestureId) {
jim@4659
   416
  SDL_Event event;
jim@4659
   417
  event.dgesture.type = SDL_DOLLARRECORD;
jim@4659
   418
  event.dgesture.touchId = touch->id;
jim@4659
   419
  event.dgesture.gestureId = gestureId;
jim@4659
   420
jim@4659
   421
  return SDL_PushEvent(&event) > 0;
jim@4659
   422
}
jim@4659
   423
jim@4659
   424
jim@4657
   425
void SDL_GestureProcessEvent(SDL_Event* event)
jim@4657
   426
{
jim@4657
   427
  if(event->type == SDL_FINGERMOTION || 
jim@4657
   428
     event->type == SDL_FINGERDOWN ||
jim@4657
   429
     event->type == SDL_FINGERUP) {
jim@4657
   430
    GestureTouch* inTouch = SDL_GetGestureTouch(event->tfinger.touchId);
jim@4657
   431
jim@4657
   432
    //Shouldn't be possible
jim@4657
   433
    if(inTouch == NULL) return;
jim@4657
   434
    
jim@4657
   435
    
jim@4657
   436
    float x = ((float)event->tfinger.x)/inTouch->res.x;
jim@4657
   437
    float y = ((float)event->tfinger.y)/inTouch->res.y;
jim@4657
   438
    int j,empty = -1;
jim@4657
   439
    
jim@4657
   440
    for(j = 0;j<inTouch->numDownFingers;j++) {
jim@4657
   441
      if(inTouch->gestureLast[j].f.id != event->tfinger.fingerId) continue;
jim@4659
   442
      //Finger Up
jim@4657
   443
      if(event->type == SDL_FINGERUP) {
jim@4657
   444
	inTouch->numDownFingers--;
jim@4658
   445
jim@4659
   446
	if(inTouch->recording) {
jim@4659
   447
	  inTouch->recording = SDL_FALSE;
jim@4659
   448
	  Point path[DOLLARNPOINTS];
jim@4659
   449
	  dollarNormalize(inTouch->gestureLast[j].dollarPath,path);
jim@4659
   450
	  int index;
jim@4659
   451
	  if(recordAll) {
jim@4659
   452
	    index = SDL_AddDollarGesture(NULL,path);
jim@4659
   453
	    int i;
jim@4659
   454
	    for(i = 0;i < numGestureTouches; i++)
jim@4659
   455
	      gestureTouch[i].recording = SDL_FALSE;
jim@4659
   456
	  }
jim@4659
   457
	  else {
jim@4659
   458
	    index = SDL_AddDollarGesture(inTouch,path);
jim@4659
   459
	  }
jim@4659
   460
	  
jim@4659
   461
	  if(index >= 0) {
jim@4659
   462
	    SDL_SendDollarRecord(inTouch,inTouch->dollarTemplate[index].hash);
jim@4659
   463
	  }
jim@4659
   464
	  else {
jim@4659
   465
	    SDL_SendDollarRecord(inTouch,-1);
jim@4659
   466
	  }
jim@4658
   467
	}
jim@4659
   468
	else {	
jim@4659
   469
	  int bestTempl;
jim@4659
   470
	  float error;
jim@4659
   471
	  error = dollarRecognize(inTouch->gestureLast[j].dollarPath,
jim@4659
   472
				  &bestTempl,inTouch);
jim@4659
   473
	  if(bestTempl >= 0){
jim@4659
   474
	    //Send Event
jim@4659
   475
	    int gestureId = inTouch->dollarTemplate[bestTempl].hash;
jim@4659
   476
	    SDL_SendGestureDollar(inTouch,gestureId,error);
jim@4659
   477
	    printf("Dollar error: %f\n",error);
jim@4659
   478
	  }
jim@4659
   479
	} 
jim@4657
   480
	inTouch->gestureLast[j] = inTouch->gestureLast[inTouch->numDownFingers];
jim@4657
   481
	break;
jim@4657
   482
      }
jim@4657
   483
      else {
jim@4657
   484
	float dx = x - inTouch->gestureLast[j].f.p.x;
jim@4657
   485
	float dy = y - inTouch->gestureLast[j].f.p.y;
jim@4658
   486
	DollarPath* path = &inTouch->gestureLast[j].dollarPath;
jim@4658
   487
	if(path->numPoints < MAXPATHSIZE) {
jim@4658
   488
	  path->p[path->numPoints].x = x;
jim@4658
   489
	  path->p[path->numPoints].y = y;
jim@4658
   490
	  path->length += sqrt(dx*dx + dy*dy);
jim@4658
   491
	  path->numPoints++;
jim@4658
   492
	}
jim@4658
   493
jim@4658
   494
jim@4657
   495
	inTouch->centroid.x += dx/inTouch->numDownFingers;
jim@4657
   496
	inTouch->centroid.y += dy/inTouch->numDownFingers;    
jim@4657
   497
	if(inTouch->numDownFingers > 1) {
jim@4657
   498
	  Point lv; //Vector from centroid to last x,y position
jim@4657
   499
	  Point v; //Vector from centroid to current x,y position
jim@4657
   500
	  lv = inTouch->gestureLast[j].cv;
jim@4657
   501
	  float lDist = sqrt(lv.x*lv.x + lv.y*lv.y);
jim@4657
   502
	  //printf("lDist = %f\n",lDist);
jim@4657
   503
	  v.x = x - inTouch->centroid.x;
jim@4657
   504
	  v.y = y - inTouch->centroid.y;
jim@4657
   505
	  inTouch->gestureLast[j].cv = v;
jim@4657
   506
	  float Dist = sqrt(v.x*v.x+v.y*v.y);
jim@4657
   507
	  // cos(dTheta) = (v . lv)/(|v| * |lv|)
jim@4657
   508
	  
jim@4657
   509
	  //Normalize Vectors to simplify angle calculation
jim@4657
   510
	  lv.x/=lDist;
jim@4657
   511
	  lv.y/=lDist;
jim@4657
   512
	  v.x/=Dist;
jim@4657
   513
	  v.y/=Dist;
jim@4657
   514
	  float dtheta = atan2(lv.x*v.y - lv.y*v.x,lv.x*v.x + lv.y*v.y);
jim@4657
   515
	  
jim@4657
   516
	  float dDist = (Dist - lDist);
jim@4657
   517
	  if(lDist == 0) {dDist = 0;dtheta = 0;} //To avoid impossible values
jim@4657
   518
	  inTouch->gestureLast[j].dDist = dDist;
jim@4657
   519
	  inTouch->gestureLast[j].dtheta = dtheta;
jim@4657
   520
	  
jim@4657
   521
	  //printf("dDist = %f, dTheta = %f\n",dDist,dtheta);
jim@4657
   522
	  //gdtheta = gdtheta*.9 + dtheta*.1;
jim@4657
   523
	  //gdDist  =  gdDist*.9 +  dDist*.1
jim@4657
   524
	  //knob.r += dDist/numDownFingers;
jim@4657
   525
	  //knob.ang += dtheta;
jim@4657
   526
	  //printf("thetaSum = %f, distSum = %f\n",gdtheta,gdDist);
jim@4657
   527
	  //printf("id: %i dTheta = %f, dDist = %f\n",j,dtheta,dDist);
jim@4657
   528
	  SDL_SendGestureMulti(inTouch,dtheta,dDist);
jim@4657
   529
	}
jim@4657
   530
	else {
jim@4657
   531
	  inTouch->gestureLast[j].dDist = 0;
jim@4657
   532
	  inTouch->gestureLast[j].dtheta = 0;
jim@4657
   533
	  inTouch->gestureLast[j].cv.x = 0;
jim@4657
   534
	  inTouch->gestureLast[j].cv.y = 0;
jim@4657
   535
	}
jim@4657
   536
	inTouch->gestureLast[j].f.p.x = x;
jim@4657
   537
	inTouch->gestureLast[j].f.p.y = y;
jim@4657
   538
	break;
jim@4657
   539
	//pressure?
jim@4657
   540
      }      
jim@4657
   541
    }
jim@4657
   542
    
jim@4657
   543
    if(j == inTouch->numDownFingers) {
jim@4657
   544
      //printf("Finger Down!!!\n");
jim@4657
   545
      inTouch->numDownFingers++;
jim@4657
   546
      inTouch->centroid.x = (inTouch->centroid.x*(inTouch->numDownFingers - 1)+ 
jim@4657
   547
			     x)/inTouch->numDownFingers;
jim@4657
   548
      inTouch->centroid.y = (inTouch->centroid.y*(inTouch->numDownFingers - 1)+
jim@4657
   549
			     y)/inTouch->numDownFingers;
jim@4657
   550
      
jim@4657
   551
      inTouch->gestureLast[j].f.id = event->tfinger.fingerId;
jim@4657
   552
      inTouch->gestureLast[j].f.p.x  = x;
jim@4657
   553
      inTouch->gestureLast[j].f.p.y  = y;	
jim@4657
   554
      inTouch->gestureLast[j].cv.x = 0;
jim@4657
   555
      inTouch->gestureLast[j].cv.y = 0;
jim@4658
   556
jim@4658
   557
      inTouch->gestureLast[j].dollarPath.length = 0;
jim@4658
   558
      inTouch->gestureLast[j].dollarPath.p[0].x = x;
jim@4658
   559
      inTouch->gestureLast[j].dollarPath.p[0].y = y;
jim@4658
   560
      inTouch->gestureLast[j].dollarPath.numPoints = 1;
jim@4657
   561
    }
jim@4657
   562
  }
jim@4657
   563
}  
jim@4657
   564
  
jim@4657
   565
  /* vi: set ts=4 sw=4 expandtab: */
jim@4657
   566