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