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