Added some files I had previosuly missed
authorJim Grandpre <jim.tla@gmail.com>
Sun, 11 Jul 2010 01:15:39 -0400
changeset 4659063b9455bd1a
parent 4658 454385d76845
child 4660 b15e7017409b
Added some files I had previosuly missed
Makefile.in
include/SDL_events.h
include/SDL_gesture.h
include/SDL_touch.h
src/events/SDL_gesture.c
src/events/SDL_gesture_c.h
touchTest/gestureSDLTest.c
touchTest/touchPong
touchTest/touchSimp
     1.1 --- a/Makefile.in	Fri Jul 09 00:50:40 2010 -0700
     1.2 +++ b/Makefile.in	Sun Jul 11 01:15:39 2010 -0400
     1.3 @@ -44,7 +44,7 @@
     1.4  
     1.5  DIST = acinclude autogen.sh Borland.html Borland.zip BUGS build-scripts configure configure.in COPYING CREDITS include INSTALL Makefile.minimal Makefile.in README* sdl-config.in sdl.m4 sdl.pc.in SDL.spec SDL.spec.in src test TODO VisualC.html VisualC VisualCE Watcom-Win32.zip WhatsNew Xcode Xcode-iPhoneOS
     1.6  
     1.7 -HDRS = SDL.h SDL_assert.h SDL_atomic.h SDL_audio.h SDL_compat.h SDL_cpuinfo.h SDL_endian.h SDL_error.h SDL_events.h SDL_haptic.h SDL_joystick.h SDL_keyboard.h SDL_keysym.h SDL_loadso.h SDL_main.h SDL_mouse.h SDL_mutex.h SDL_name.h SDL_opengl.h SDL_opengles.h SDL_pixels.h SDL_platform.h SDL_power.h SDL_quit.h SDL_rect.h SDL_revision.h SDL_rwops.h SDL_scancode.h SDL_stdinc.h SDL_surface.h SDL_syswm.h SDL_thread.h SDL_timer.h SDL_types.h SDL_version.h SDL_video.h begin_code.h close_code.h SDL_touch.h
     1.8 +HDRS = SDL.h SDL_assert.h SDL_atomic.h SDL_audio.h SDL_compat.h SDL_cpuinfo.h SDL_endian.h SDL_error.h SDL_events.h SDL_haptic.h SDL_joystick.h SDL_keyboard.h SDL_keysym.h SDL_loadso.h SDL_main.h SDL_mouse.h SDL_mutex.h SDL_name.h SDL_opengl.h SDL_opengles.h SDL_pixels.h SDL_platform.h SDL_power.h SDL_quit.h SDL_rect.h SDL_revision.h SDL_rwops.h SDL_scancode.h SDL_stdinc.h SDL_surface.h SDL_syswm.h SDL_thread.h SDL_timer.h SDL_types.h SDL_version.h SDL_video.h begin_code.h close_code.h SDL_touch.h SDL_gesture.h
     1.9  
    1.10  LT_AGE      = @LT_AGE@
    1.11  LT_CURRENT  = @LT_CURRENT@
     2.1 --- a/include/SDL_events.h	Fri Jul 09 00:50:40 2010 -0700
     2.2 +++ b/include/SDL_events.h	Sun Jul 11 01:15:39 2010 -0400
     2.3 @@ -36,6 +36,8 @@
     2.4  #include "SDL_mouse.h"
     2.5  #include "SDL_joystick.h"
     2.6  #include "SDL_quit.h"
     2.7 +#include "SDL_gesture.h"
     2.8 +#include "SDL_touch.h"
     2.9  
    2.10  #include "begin_code.h"
    2.11  /* Set up for C function definitions, even when using C++ */
    2.12 @@ -95,6 +97,7 @@
    2.13  
    2.14      /*Gesture events*/
    2.15      SDL_DOLLARGESTURE     = 0x800,
    2.16 +    SDL_DOLLARRECORD,
    2.17      SDL_MULTIGESTURE,
    2.18  
    2.19      /* Obsolete events */
    2.20 @@ -359,9 +362,10 @@
    2.21      Uint32 type;        /**< ::SDL_DOLLARGESTURE */
    2.22      Uint32 windowID;    /**< The window with mouse focus, if any */
    2.23      Uint8 touchId;        /**< The touch device index */
    2.24 -    Uint8 gestureId;
    2.25 +    Uint8 padding1;
    2.26      Uint8 padding2;
    2.27      Uint8 padding3;
    2.28 +    unsigned long gestureId;
    2.29      float error;
    2.30    /*
    2.31      //TODO: Enable to give location?
     3.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     3.2 +++ b/include/SDL_gesture.h	Sun Jul 11 01:15:39 2010 -0400
     3.3 @@ -0,0 +1,90 @@
     3.4 +/*
     3.5 +    SDL - Simple DirectMedia Layer
     3.6 +    Copyright (C) 1997-2010 Sam Lantinga
     3.7 +
     3.8 +    This library is free software; you can redistribute it and/or
     3.9 +    modify it under the terms of the GNU Lesser General Public
    3.10 +    License as published by the Free Software Foundation; either
    3.11 +    version 2.1 of the License, or (at your option) any later version.
    3.12 +
    3.13 +    This library is distributed in the hope that it will be useful,
    3.14 +    but WITHOUT ANY WARRANTY; without even the implied warranty of
    3.15 +    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
    3.16 +    Lesser General Public License for more details.
    3.17 +
    3.18 +    You should have received a copy of the GNU Lesser General Public
    3.19 +    License along with this library; if not, write to the Free Software
    3.20 +    Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
    3.21 +
    3.22 +    Sam Lantinga
    3.23 +    slouken@libsdl.org
    3.24 +*/
    3.25 +
    3.26 +/**
    3.27 + *  \file SDL_gesture.h
    3.28 + *  
    3.29 + *  Include file for SDL gesture event handling.
    3.30 + */
    3.31 +
    3.32 +#ifndef _SDL_gesture_h
    3.33 +#define _SDL_gesture_h
    3.34 +
    3.35 +#include "SDL_stdinc.h"
    3.36 +#include "SDL_error.h"
    3.37 +#include "SDL_video.h"
    3.38 +
    3.39 +#include "begin_code.h"
    3.40 +/* Set up for C function definitions, even when using C++ */
    3.41 +#ifdef __cplusplus
    3.42 +/* *INDENT-OFF* */
    3.43 +extern "C" {
    3.44 +/* *INDENT-ON* */
    3.45 +#endif
    3.46 +
    3.47 +
    3.48 +/* Function prototypes */
    3.49 +
    3.50 +/**
    3.51 + *  \brief Begin Recording a gesture on the specified touch, or all touches (-1)
    3.52 + *
    3.53 + *
    3.54 + */
    3.55 +  extern DECLSPEC int SDLCALL SDL_RecordGesture(int touchId);
    3.56 +
    3.57 +
    3.58 +/**
    3.59 + *  \brief Save all currently loaded Dollar Gesture templates
    3.60 + *
    3.61 + *
    3.62 + */
    3.63 +  extern DECLSPEC int SDLCALL SDL_SaveAllDollarTemplates(FILE *fp);
    3.64 +
    3.65 +/**
    3.66 + *  \brief Save a currently loaded Dollar Gesture template
    3.67 + *
    3.68 + *
    3.69 + */
    3.70 +  extern DECLSPEC int 
    3.71 +  SDLCALL SDL_SaveDollarTemplate(unsigned long gestureId,FILE *fp);
    3.72 +
    3.73 +
    3.74 +/**
    3.75 + *  \brief Load Dollar Gesture templates from a file
    3.76 + *
    3.77 + *
    3.78 + */
    3.79 +  extern DECLSPEC int SDLCALL SDL_LoadDollarTemplates(int touchId, FILE *fp);
    3.80 +
    3.81 +
    3.82 +
    3.83 +/* Ends C function definitions when using C++ */
    3.84 +#ifdef __cplusplus
    3.85 +/* *INDENT-OFF* */
    3.86 +}
    3.87 +/* *INDENT-ON* */
    3.88 +#endif
    3.89 +#include "close_code.h"
    3.90 +
    3.91 +#endif /* _SDL_gesture_h */
    3.92 +
    3.93 +/* vi: set ts=4 sw=4 expandtab: */
     4.1 --- a/include/SDL_touch.h	Fri Jul 09 00:50:40 2010 -0700
     4.2 +++ b/include/SDL_touch.h	Sun Jul 11 01:15:39 2010 -0400
     4.3 @@ -23,7 +23,7 @@
     4.4  /**
     4.5   *  \file SDL_touch.h
     4.6   *  
     4.7 - *  Include file for SDL mouse event handling.
     4.8 + *  Include file for SDL touch event handling.
     4.9   */
    4.10  
    4.11  #ifndef _SDL_touch_h
     5.1 --- a/src/events/SDL_gesture.c	Fri Jul 09 00:50:40 2010 -0700
     5.2 +++ b/src/events/SDL_gesture.c	Sun Jul 11 01:15:39 2010 -0400
     5.3 @@ -13,8 +13,7 @@
     5.4      Lesser General Public License for more details.
     5.5  
     5.6      You should have received a copy of the GNU Lesser General Public
     5.7 -    License along with this library; if not, write to the Free Software
     5.8 -    Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
     5.9 +    License along with this library; if not, write to the Free Software    Founation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
    5.10  
    5.11      Sam Lantinga
    5.12      slouken@libsdl.org
    5.13 @@ -66,6 +65,10 @@
    5.14    DollarPath dollarPath;
    5.15  } TouchPoint;
    5.16  
    5.17 +typedef struct {
    5.18 +  Point path[DOLLARNPOINTS];
    5.19 +  unsigned long hash;
    5.20 +} DollarTemplate;
    5.21  
    5.22  typedef struct {
    5.23    int id;
    5.24 @@ -75,11 +78,140 @@
    5.25    int numDownFingers;
    5.26  
    5.27    int numDollarTemplates;
    5.28 -  Point dollarTemplate[MAXTEMPLATES][DOLLARNPOINTS];
    5.29 +  DollarTemplate dollarTemplate[MAXTEMPLATES];
    5.30 +
    5.31 +  SDL_bool recording;
    5.32  } GestureTouch;
    5.33  
    5.34  GestureTouch gestureTouch[MAXTOUCHES];
    5.35  int numGestureTouches = 0;
    5.36 +SDL_bool recordAll;
    5.37 +
    5.38 +int SDL_RecordGesture(int touchId) {
    5.39 +  int i;
    5.40 +  if(touchId < 0) recordAll = SDL_TRUE;
    5.41 +  for(i = 0;i < numGestureTouches; i++) {
    5.42 +    if((touchId < 0) || (gestureTouch[i].id == touchId)) {
    5.43 +      gestureTouch[i].recording = SDL_TRUE;
    5.44 +      if(touchId >= 0)
    5.45 +	return 1;
    5.46 +    }      
    5.47 +  }
    5.48 +  return (touchId < 0);
    5.49 +}
    5.50 +
    5.51 +unsigned long SDL_HashDollar(Point* points) {
    5.52 +  unsigned long hash = 5381;
    5.53 +  int i;
    5.54 +  for(i = 0;i < DOLLARNPOINTS; i++) { 
    5.55 +    hash = ((hash<<5) + hash) + points[i].x;
    5.56 +    hash = ((hash<<5) + hash) + points[i].y;
    5.57 +  }
    5.58 +  return hash;
    5.59 +}
    5.60 +
    5.61 +int SaveTemplate(DollarTemplate *templ, FILE *fp) {
    5.62 +  int i;
    5.63 +  fprintf(fp,"%lu ",templ->hash);
    5.64 +  for(i = 0;i < DOLLARNPOINTS;i++) {
    5.65 +    fprintf(fp,"%i %i ",(int)templ->path[i].x,(int)templ->path[i].y);
    5.66 +  }
    5.67 +  fprintf(fp,"\n");
    5.68 +}
    5.69 +
    5.70 +
    5.71 +int SDL_SaveAllDollarTemplates(FILE *fp) {  
    5.72 +  int i,j,rtrn = 0;
    5.73 +  for(i = 0; i < numGestureTouches; i++) {
    5.74 +    GestureTouch* touch = &gestureTouch[i];
    5.75 +    for(j = 0;j < touch->numDollarTemplates; j++) {
    5.76 +	rtrn += SaveTemplate(&touch->dollarTemplate[i],fp);
    5.77 +    }
    5.78 +  }
    5.79 +  return rtrn;  
    5.80 +}
    5.81 +
    5.82 +int SDL_SaveDollarTemplate(unsigned long gestureId, FILE *fp) {
    5.83 +  int i,j;
    5.84 +  for(i = 0; i < numGestureTouches; i++) {
    5.85 +    GestureTouch* touch = &gestureTouch[i];
    5.86 +    for(j = 0;j < touch->numDollarTemplates; j++) {
    5.87 +      if(touch->dollarTemplate[i].hash == gestureId) {
    5.88 +	return SaveTemplate(&touch->dollarTemplate[i],fp);
    5.89 +      }
    5.90 +    }
    5.91 +  }
    5.92 +}
    5.93 +
    5.94 +int SDL_LoadDollarTemplates(int touchId, FILE *fp) {
    5.95 +  int i,loaded = 0;
    5.96 +  GestureTouch *touch = NULL;
    5.97 +  if(touchId >= 0) {
    5.98 +    for(i = 0;i < numGestureTouches; i++)
    5.99 +      if(gestureTouch[i].id == touchId)
   5.100 +	touch = &gestureTouch[i];
   5.101 +    if(touch == NULL) return -1;
   5.102 +  }
   5.103 +
   5.104 +  while(!feof(fp)) {
   5.105 +    DollarTemplate templ;
   5.106 +    fscanf(fp,"%lu ",&templ.hash);
   5.107 +    for(i = 0;i < DOLLARNPOINTS; i++) {		
   5.108 +      int x,y;
   5.109 +      if(fscanf(fp,"%i %i ",&x,&y) != 2) break;
   5.110 +      templ.path[i].x = x;
   5.111 +      templ.path[i].y = y;
   5.112 +    }
   5.113 +    fscanf(fp,"\n");
   5.114 +
   5.115 +    if(touchId >= 0) {
   5.116 +      if(SDL_AddDollarGesture(touch,templ)) loaded++;
   5.117 +    }
   5.118 +    else {
   5.119 +      for(i = 0;i < numGestureTouches; i++) {
   5.120 +	if(gestureTouch[i].id == touchId) {
   5.121 +	  touch = &gestureTouch[i];
   5.122 +	  SDL_AddDollarGesture(touch,templ);
   5.123 +	}
   5.124 +      }
   5.125 +      loaded++;
   5.126 +    }
   5.127 +  }
   5.128 +
   5.129 +  return 1; 
   5.130 +}
   5.131 +
   5.132 +
   5.133 +//path is an already sampled set of points
   5.134 +//Returns the index of the gesture on success, or -1
   5.135 +int SDL_AddDollarGesture(GestureTouch* inTouch,Point* path) {
   5.136 +  if(inTouch == NULL) {
   5.137 +    if(numGestureTouches == 0) return -1;
   5.138 +    int i = 0;
   5.139 +    for(i = 0;i < numGestureTouches; i++) {
   5.140 +      inTouch = &gestureTouch[i];
   5.141 +      if(inTouch->numDollarTemplates < MAXTEMPLATES) {
   5.142 +	DollarTemplate *templ = 
   5.143 +	  &inTouch->dollarTemplate[inTouch->numDollarTemplates];
   5.144 +	memcpy(templ->path,path,DOLLARNPOINTS*sizeof(Point));
   5.145 +	templ->hash = SDL_HashDollar(templ->path);
   5.146 +	inTouch->numDollarTemplates++;
   5.147 +      }
   5.148 +    }
   5.149 +    return inTouch->numDollarTemplates - 1;
   5.150 +  }else if(inTouch->numDollarTemplates < MAXTEMPLATES) {
   5.151 +    DollarTemplate *templ = 
   5.152 +      &inTouch->dollarTemplate[inTouch->numDollarTemplates];
   5.153 +    memcpy(templ->path,path,DOLLARNPOINTS*sizeof(Point));
   5.154 +    templ->hash = SDL_HashDollar(templ->path);
   5.155 +    inTouch->numDollarTemplates++;
   5.156 +    return inTouch->numDollarTemplates - 1;
   5.157 +  }
   5.158 +  return -1;
   5.159 +}
   5.160 +
   5.161 +
   5.162 +
   5.163  
   5.164  float dollarDifference(Point* points,Point* templ,float ang) {
   5.165    //  Point p[DOLLARNPOINTS];
   5.166 @@ -141,7 +273,7 @@
   5.167    int bestDiff = 10000;
   5.168    *bestTempl = -1;
   5.169    for(i = 0;i < touch->numDollarTemplates;i++) {
   5.170 -    int diff = bestDollarDifference(points,touch->dollarTemplate[i]);
   5.171 +    int diff = bestDollarDifference(points,touch->dollarTemplate[i].path);
   5.172      if(diff < bestDiff) {bestDiff = diff; *bestTempl = i;}
   5.173    }
   5.174    return bestDiff;
   5.175 @@ -239,6 +371,8 @@
   5.176  
   5.177    gestureTouch[numGestureTouches].numDollarTemplates = 0;
   5.178  
   5.179 +  gestureTouch[numGestureTouches].recording = SDL_FALSE;
   5.180 +
   5.181    numGestureTouches++;
   5.182    return 0;
   5.183  }
   5.184 @@ -277,6 +411,17 @@
   5.185    return SDL_PushEvent(&event) > 0;
   5.186  }
   5.187  
   5.188 +
   5.189 +int SDL_SendDollarRecord(GestureTouch* touch,int gestureId) {
   5.190 +  SDL_Event event;
   5.191 +  event.dgesture.type = SDL_DOLLARRECORD;
   5.192 +  event.dgesture.touchId = touch->id;
   5.193 +  event.dgesture.gestureId = gestureId;
   5.194 +
   5.195 +  return SDL_PushEvent(&event) > 0;
   5.196 +}
   5.197 +
   5.198 +
   5.199  void SDL_GestureProcessEvent(SDL_Event* event)
   5.200  {
   5.201    if(event->type == SDL_FINGERMOTION || 
   5.202 @@ -294,23 +439,44 @@
   5.203      
   5.204      for(j = 0;j<inTouch->numDownFingers;j++) {
   5.205        if(inTouch->gestureLast[j].f.id != event->tfinger.fingerId) continue;
   5.206 -
   5.207 +      //Finger Up
   5.208        if(event->type == SDL_FINGERUP) {
   5.209  	inTouch->numDownFingers--;
   5.210  
   5.211 -	int bestTempl;
   5.212 -	float error;
   5.213 -	error = dollarRecognize(inTouch->gestureLast[j].dollarPath,
   5.214 -				&bestTempl,inTouch);
   5.215 -	if(bestTempl >= 0){
   5.216 -	  //Send Event
   5.217 -	  int gestureId = 0; //?
   5.218 -	  SDL_SendGestureDollar(inTouch->id,gestureId,error);
   5.219 -
   5.220 -
   5.221 -	  printf("Dollar error: %f\n",error);
   5.222 +	if(inTouch->recording) {
   5.223 +	  inTouch->recording = SDL_FALSE;
   5.224 +	  Point path[DOLLARNPOINTS];
   5.225 +	  dollarNormalize(inTouch->gestureLast[j].dollarPath,path);
   5.226 +	  int index;
   5.227 +	  if(recordAll) {
   5.228 +	    index = SDL_AddDollarGesture(NULL,path);
   5.229 +	    int i;
   5.230 +	    for(i = 0;i < numGestureTouches; i++)
   5.231 +	      gestureTouch[i].recording = SDL_FALSE;
   5.232 +	  }
   5.233 +	  else {
   5.234 +	    index = SDL_AddDollarGesture(inTouch,path);
   5.235 +	  }
   5.236 +	  
   5.237 +	  if(index >= 0) {
   5.238 +	    SDL_SendDollarRecord(inTouch,inTouch->dollarTemplate[index].hash);
   5.239 +	  }
   5.240 +	  else {
   5.241 +	    SDL_SendDollarRecord(inTouch,-1);
   5.242 +	  }
   5.243  	}
   5.244 -
   5.245 +	else {	
   5.246 +	  int bestTempl;
   5.247 +	  float error;
   5.248 +	  error = dollarRecognize(inTouch->gestureLast[j].dollarPath,
   5.249 +				  &bestTempl,inTouch);
   5.250 +	  if(bestTempl >= 0){
   5.251 +	    //Send Event
   5.252 +	    int gestureId = inTouch->dollarTemplate[bestTempl].hash;
   5.253 +	    SDL_SendGestureDollar(inTouch,gestureId,error);
   5.254 +	    printf("Dollar error: %f\n",error);
   5.255 +	  }
   5.256 +	} 
   5.257  	inTouch->gestureLast[j] = inTouch->gestureLast[inTouch->numDownFingers];
   5.258  	break;
   5.259        }
     6.1 --- a/src/events/SDL_gesture_c.h	Fri Jul 09 00:50:40 2010 -0700
     6.2 +++ b/src/events/SDL_gesture_c.h	Sun Jul 11 01:15:39 2010 -0400
     6.3 @@ -26,6 +26,7 @@
     6.4  
     6.5  extern void SDL_GestureProcessEvent(SDL_Event* event);
     6.6  
     6.7 +extern int SDL_RecordGesture(int touchId);
     6.8  
     6.9  #endif /* _SDL_gesture_c_h */
    6.10  
     7.1 --- a/touchTest/gestureSDLTest.c	Fri Jul 09 00:50:40 2010 -0700
     7.2 +++ b/touchTest/gestureSDLTest.c	Sun Jul 11 01:15:39 2010 -0400
     7.3 @@ -2,6 +2,7 @@
     7.4  #include <SDL.h>
     7.5  #include <math.h>
     7.6  #include <SDL_touch.h>
     7.7 +#include <SDL_gesture.h>
     7.8  
     7.9  #define PI 3.1415926535897
    7.10  #define PHI ((sqrt(5)-1)/2)
    7.11 @@ -159,171 +160,6 @@
    7.12    
    7.13  }
    7.14  
    7.15 -void drawDollarPath(SDL_Surface* screen,Point* points,int numPoints,
    7.16 -		    int rad,unsigned int col){
    7.17 -  int i;
    7.18 -  for(i=0;i<numPoints;i++) {
    7.19 -    drawCircle(screen,points[i].x+screen->w/2,
    7.20 -	       points[i].y+screen->h/2,
    7.21 -	       rad,col);
    7.22 -  }
    7.23 -}
    7.24 -
    7.25 -float dollarDifference(Point* points,Point* templ,float ang) {
    7.26 -  //  Point p[DOLLARNPOINTS];
    7.27 -  float dist = 0;
    7.28 -  Point p;
    7.29 -  int i;
    7.30 -  for(i = 0; i < DOLLARNPOINTS; i++) {
    7.31 -    p.x = points[i].x * cos(ang) - points[i].y * sin(ang);
    7.32 -    p.y = points[i].x * sin(ang) + points[i].y * cos(ang);
    7.33 -    dist += sqrt((p.x-templ[i].x)*(p.x-templ[i].x)+
    7.34 -		 (p.y-templ[i].y)*(p.y-templ[i].y));
    7.35 -  }
    7.36 -  return dist/DOLLARNPOINTS;
    7.37 -  
    7.38 -}
    7.39 -
    7.40 -float bestDollarDifference(Point* points,Point* templ) {
    7.41 -  //------------BEGIN DOLLAR BLACKBOX----------------//
    7.42 -  //-TRANSLATED DIRECTLY FROM PSUDEO-CODE AVAILABLE AT-//
    7.43 -  //-"http://depts.washington.edu/aimgroup/proj/dollar/"-//
    7.44 -  float ta = -PI/4;
    7.45 -  float tb = PI/4;
    7.46 -  float dt = PI/90;
    7.47 -  float x1 = PHI*ta + (1-PHI)*tb;
    7.48 -  float f1 = dollarDifference(points,templ,x1);
    7.49 -  float x2 = (1-PHI)*ta + PHI*tb;
    7.50 -  float f2 = dollarDifference(points,templ,x2);
    7.51 -  while(abs(ta-tb) > dt) {
    7.52 -    if(f1 < f2) {
    7.53 -      tb = x2;
    7.54 -      x2 = x1;
    7.55 -      f2 = f1;
    7.56 -      x1 = PHI*ta + (1-PHI)*tb;
    7.57 -      f1 = dollarDifference(points,templ,x1);
    7.58 -    }
    7.59 -    else {
    7.60 -      ta = x1;
    7.61 -      x1 = x2;
    7.62 -      f1 = f2;
    7.63 -      x2 = (1-PHI)*ta + PHI*tb;
    7.64 -      f2 = dollarDifference(points,templ,x2);
    7.65 -    }
    7.66 -  }
    7.67 -  /*
    7.68 -  if(f1 <= f2)
    7.69 -    printf("Min angle (x1): %f\n",x1);
    7.70 -  else if(f1 >  f2)
    7.71 -    printf("Min angle (x2): %f\n",x2);
    7.72 -  */
    7.73 -  return SDL_min(f1,f2);  
    7.74 -}
    7.75 -
    7.76 -float dollarRecognize(SDL_Surface* screen, DollarPath path,int *bestTempl) {
    7.77 -
    7.78 -  Point points[DOLLARNPOINTS];
    7.79 -  int numPoints = dollarNormalize(path,points);
    7.80 -  int i;
    7.81 -  
    7.82 -  int k;
    7.83 -  /*
    7.84 -  for(k = 0;k<DOLLARNPOINTS;k++) {
    7.85 -    printf("(%f,%f)\n",points[k].x,
    7.86 -	   points[k].y);
    7.87 -  }
    7.88 -  */
    7.89 -  drawDollarPath(screen,points,numPoints,-15,0xFF6600);
    7.90 -
    7.91 -  int bestDiff = 10000;
    7.92 -  *bestTempl = -1;
    7.93 -  for(i = 0;i < numDollarTemplates;i++) {
    7.94 -    int diff = bestDollarDifference(points,dollarTemplate[i]);
    7.95 -    if(diff < bestDiff) {bestDiff = diff; *bestTempl = i;}
    7.96 -  }
    7.97 -  return bestDiff;
    7.98 -}
    7.99 -
   7.100 -//DollarPath contains raw points, plus (possibly) the calculated length
   7.101 -int dollarNormalize(DollarPath path,Point *points) {
   7.102 -  int i;
   7.103 -  //Calculate length if it hasn't already been done
   7.104 -  if(path.length <= 0) {
   7.105 -    for(i=1;i<path.numPoints;i++) {
   7.106 -      float dx = path.p[i  ].x - 
   7.107 -	         path.p[i-1].x;
   7.108 -      float dy = path.p[i  ].y - 
   7.109 -	         path.p[i-1].y;
   7.110 -      path.length += sqrt(dx*dx+dy*dy);
   7.111 -    }
   7.112 -  }
   7.113 -
   7.114 -
   7.115 -  //Resample
   7.116 -  float interval = path.length/(DOLLARNPOINTS - 1);
   7.117 -  float dist = 0;
   7.118 -
   7.119 -  int numPoints = 0;
   7.120 -  Point centroid; centroid.x = 0;centroid.y = 0;
   7.121 -  //printf("(%f,%f)\n",path.p[path.numPoints-1].x,path.p[path.numPoints-1].y);
   7.122 -  for(i = 1;i < path.numPoints;i++) {
   7.123 -    float d = sqrt((path.p[i-1].x-path.p[i].x)*(path.p[i-1].x-path.p[i].x)+
   7.124 -		   (path.p[i-1].y-path.p[i].y)*(path.p[i-1].y-path.p[i].y));
   7.125 -    //printf("d = %f dist = %f/%f\n",d,dist,interval);
   7.126 -    while(dist + d > interval) {
   7.127 -      points[numPoints].x = path.p[i-1].x + 
   7.128 -	((interval-dist)/d)*(path.p[i].x-path.p[i-1].x);
   7.129 -      points[numPoints].y = path.p[i-1].y + 
   7.130 -	((interval-dist)/d)*(path.p[i].y-path.p[i-1].y);
   7.131 -      centroid.x += points[numPoints].x;
   7.132 -      centroid.y += points[numPoints].y;
   7.133 -      numPoints++;
   7.134 -
   7.135 -      dist -= interval;
   7.136 -    }
   7.137 -    dist += d;
   7.138 -  }
   7.139 -  if(numPoints < 1) return 0;
   7.140 -  centroid.x /= numPoints;
   7.141 -  centroid.y /= numPoints;
   7.142 - 
   7.143 -  //printf("Centroid (%f,%f)",centroid.x,centroid.y);
   7.144 -  //Rotate Points so point 0 is left of centroid and solve for the bounding box
   7.145 -  float xmin,xmax,ymin,ymax;
   7.146 -  xmin = centroid.x;
   7.147 -  xmax = centroid.x;
   7.148 -  ymin = centroid.y;
   7.149 -  ymax = centroid.y;
   7.150 -  
   7.151 -  float ang = atan2(centroid.y - points[0].y,
   7.152 -		    centroid.x - points[0].x);
   7.153 -
   7.154 -  for(i = 0;i<numPoints;i++) {					       
   7.155 -    float px = points[i].x;
   7.156 -    float py = points[i].y;
   7.157 -    points[i].x = (px - centroid.x)*cos(ang) - 
   7.158 -                  (py - centroid.y)*sin(ang) + centroid.x;
   7.159 -    points[i].y = (px - centroid.x)*sin(ang) + 
   7.160 -                  (py - centroid.y)*cos(ang) + centroid.y;
   7.161 -
   7.162 -
   7.163 -    if(points[i].x < xmin) xmin = points[i].x;
   7.164 -    if(points[i].x > xmax) xmax = points[i].x; 
   7.165 -    if(points[i].y < ymin) ymin = points[i].y;
   7.166 -    if(points[i].y > ymax) ymax = points[i].y;
   7.167 -  }
   7.168 -
   7.169 -  //Scale points to DOLLARSIZE, and translate to the origin
   7.170 -  float w = xmax-xmin;
   7.171 -  float h = ymax-ymin;
   7.172 -
   7.173 -  for(i=0;i<numPoints;i++) {
   7.174 -    points[i].x = (points[i].x - centroid.x)*DOLLARSIZE/w;
   7.175 -    points[i].y = (points[i].y - centroid.y)*DOLLARSIZE/h;
   7.176 -  }  
   7.177 -  return numPoints;
   7.178 -}
   7.179 -
   7.180  void DrawScreen(SDL_Surface* screen, int h)
   7.181  {
   7.182    int x, y, xm,ym,c;
   7.183 @@ -369,138 +205,20 @@
   7.184        //SDL_Finger* inFinger = SDL_GetFinger(inTouch,event.tfinger.fingerId);
   7.185  	    
   7.186        float x = ((float)event.tfinger.x)/inTouch->xres;
   7.187 -      float y = ((float)event.tfinger.y)/inTouch->yres;
   7.188 -      int j,empty = -1;
   7.189 -      
   7.190 -      for(j = 0;j<MAXFINGERS;j++) {
   7.191 -	if(gestureLast[j].f.id == event.tfinger.fingerId) {
   7.192 -	  if(event.type == SDL_FINGERUP) {
   7.193 -	    numDownFingers--;
   7.194 -	    if(numDownFingers <= 1) {
   7.195 -	      gdtheta = 0;
   7.196 -	      gdDist = 0;
   7.197 -	    }
   7.198 -	    if(!keystat[32]){ //spacebar
   7.199 -	      int bestTempl;
   7.200 -	      float error = dollarRecognize(screen,dollarPath[j],&bestTempl);
   7.201 -	      if(bestTempl >= 0){
   7.202 -		drawDollarPath(screen,dollarTemplate[bestTempl]
   7.203 -			       ,DOLLARNPOINTS,-15,0x0066FF);		
   7.204 -		printf("Dollar error: %f\n",error);
   7.205 -	      }
   7.206 -	      
   7.207 -	    }
   7.208 -	    else if(numDollarTemplates < MAXTEMPLATES) {
   7.209 -	      
   7.210 -	      dollarNormalize(dollarPath[j],
   7.211 -			      dollarTemplate[numDollarTemplates]);
   7.212 -	      /*
   7.213 -	      int k;	      
   7.214 -	      for(k = 0;k<DOLLARNPOINTS;k++) {
   7.215 -		printf("(%f,%f)\n",dollarTemplate[numDollarTemplates][i].x,
   7.216 -		       dollarTemplate[numDollarTemplates][i].y);
   7.217 -		       }*/
   7.218 -	      numDollarTemplates++;	      
   7.219 -	    }
   7.220 -
   7.221 -	    gestureLast[j].f.id = -1;
   7.222 -	    break;
   7.223 -	  }
   7.224 -	  else {
   7.225 -	    dollarPath[j].p[dollarPath[j].numPoints].x = x;
   7.226 -	    dollarPath[j].p[dollarPath[j].numPoints].y = y;
   7.227 -	    float dx = (dollarPath[j].p[dollarPath[j].numPoints  ].x-
   7.228 -			dollarPath[j].p[dollarPath[j].numPoints-1].x);
   7.229 -	    float dy = (dollarPath[j].p[dollarPath[j].numPoints  ].y-
   7.230 -			dollarPath[j].p[dollarPath[j].numPoints-1].y);
   7.231 -	    dollarPath[j].length += sqrt(dx*dx + dy*dy);
   7.232 -
   7.233 -	    dollarPath[j].numPoints++;
   7.234 -
   7.235 -	    centroid.x = centroid.x + dx/numDownFingers;
   7.236 -	    centroid.y = centroid.y + dy/numDownFingers;    
   7.237 -	    if(numDownFingers > 1) {
   7.238 -	      Point lv; //Vector from centroid to last x,y position
   7.239 -	      Point v; //Vector from centroid to current x,y position
   7.240 -	      lv.x = gestureLast[j].cv.x;
   7.241 -	      lv.y = gestureLast[j].cv.y;
   7.242 -	      float lDist = sqrt(lv.x*lv.x + lv.y*lv.y);
   7.243 -	      
   7.244 -	      v.x = x - centroid.x;
   7.245 -	      v.y = y - centroid.y;
   7.246 -	      gestureLast[j].cv = v;
   7.247 -	      float Dist = sqrt(v.x*v.x+v.y*v.y);
   7.248 -	      // cos(dTheta) = (v . lv)/(|v| * |lv|)
   7.249 -	      
   7.250 -	      lv.x/=lDist;
   7.251 -	      lv.y/=lDist;
   7.252 -	      v.x/=Dist;
   7.253 -	      v.y/=Dist;
   7.254 -	      float dtheta = atan2(lv.x*v.y - lv.y*v.x,lv.x*v.x + lv.y*v.y);
   7.255 -	      
   7.256 -	      float dDist = (lDist - Dist);	      
   7.257 -	      
   7.258 -	      gestureLast[j].dDist = dDist;
   7.259 -	      gestureLast[j].dtheta = dtheta;
   7.260 -
   7.261 -	      //gdtheta = gdtheta*.9 + dtheta*.1;
   7.262 -	      //gdDist  =  gdDist*.9 +  dDist*.1
   7.263 -	      gdtheta += dtheta;
   7.264 -	      gdDist += dDist;
   7.265 -
   7.266 -	      //printf("thetaSum = %f, distSum = %f\n",gdtheta,gdDist);
   7.267 -	      //printf("id: %i dTheta = %f, dDist = %f\n",j,dtheta,dDist);
   7.268 -	    }
   7.269 -	    else {
   7.270 -	      gestureLast[j].dDist = 0;
   7.271 -	      gestureLast[j].dtheta = 0;
   7.272 -	      gestureLast[j].cv.x = 0;
   7.273 -	      gestureLast[j].cv.y = 0;
   7.274 -	    }
   7.275 -	    gestureLast[j].f.p.x = x;
   7.276 -	    gestureLast[j].f.p.y = y;
   7.277 -	    break;
   7.278 -	    //pressure?
   7.279 -	  }	  
   7.280 -	}
   7.281 -	else if(gestureLast[j].f.id == -1 && empty == -1) {
   7.282 -	  empty = j;
   7.283 -	}
   7.284 -      }
   7.285 -      
   7.286 -      if(j >= MAXFINGERS && empty >= 0) {
   7.287 -	//	printf("Finger Down!!!\n");
   7.288 -	numDownFingers++;
   7.289 -	centroid.x = (centroid.x*(numDownFingers - 1) + x)/numDownFingers;
   7.290 -	centroid.y = (centroid.y*(numDownFingers - 1) + y)/numDownFingers;
   7.291 -	
   7.292 -	j = empty;
   7.293 -	gestureLast[j].f.id = event.tfinger.fingerId;
   7.294 -	gestureLast[j].f.p.x  = x;
   7.295 -	gestureLast[j].f.p.y  = y;
   7.296 -	
   7.297 -	
   7.298 -	dollarPath[j].length = 0;
   7.299 -	dollarPath[j].p[0].x = x;
   7.300 -	dollarPath[j].p[0].y = y;
   7.301 -	dollarPath[j].numPoints = 1;
   7.302 -      }
   7.303 +      float y = ((float)event.tfinger.y)/inTouch->yres;      
   7.304        
   7.305        //draw the touch:
   7.306        
   7.307 -      if(gestureLast[j].f.id < 0) continue; //Finger up. Or some error...
   7.308 -      
   7.309 -      unsigned int c = colors[gestureLast[j].f.id%7]; 
   7.310 +      unsigned int c = colors[event.tfinger.touchId%7]; 
   7.311        unsigned int col = 
   7.312  	((unsigned int)(c*(.1+.85))) |
   7.313  	((unsigned int)((0xFF*(1-((float)age)/EVENT_BUF_SIZE))) & 0xFF)<<24;
   7.314 -      x = gestureLast[j].f.p.x;
   7.315 -      y = gestureLast[j].f.p.y;
   7.316 +
   7.317        if(event.type == SDL_FINGERMOTION)
   7.318  	drawCircle(screen,x*screen->w,y*screen->h,5,col);
   7.319        else if(event.type == SDL_FINGERDOWN)
   7.320  	drawCircle(screen,x*screen->w,y*screen->h,-10,col);     
   7.321 -      
   7.322 +      /*      
   7.323        //if there is a centroid, draw it
   7.324        if(numDownFingers > 1) {
   7.325  	unsigned int col = 
   7.326 @@ -508,6 +226,7 @@
   7.327  	  ((unsigned int)((0xFF*(1-((float)age)/EVENT_BUF_SIZE))) & 0xFF)<<24;
   7.328  	drawCircle(screen,centroid.x*screen->w,centroid.y*screen->h,5,col);
   7.329        }
   7.330 +      */
   7.331      }
   7.332    }
   7.333    
   7.334 @@ -582,6 +301,22 @@
   7.335  	  case SDL_KEYDOWN:
   7.336  	    //printf("%i\n",event.key.keysym.sym);
   7.337  	    keystat[event.key.keysym.sym] = 1;
   7.338 +	    if(event.key.keysym.sym == 32) {
   7.339 +	      SDL_RecordGesture(-1);
   7.340 +	    }
   7.341 +	    else if(event.key.keysym.sym == 115) {
   7.342 +	      FILE *fp;
   7.343 +	      fp = fopen("gestureSave","w");
   7.344 +	      SDL_SaveAllDollarTemplates(fp);
   7.345 +	      fclose(fp);
   7.346 +	    }
   7.347 +	    else if(event.key.keysym.sym == 108) {
   7.348 +	      FILE *fp;
   7.349 +	      fp = fopen("gestureSave","r");
   7.350 +	      printf("Loaded: %i\n",SDL_LoadDollarTemplates(-1,fp));
   7.351 +	      fclose(fp);
   7.352 +	    }
   7.353 +	    
   7.354  	    //keypress = 1;
   7.355  	    break;
   7.356  	  case SDL_KEYUP:
   7.357 @@ -665,20 +400,21 @@
   7.358  	    knob.p.y = event.mgesture.y;
   7.359  	    knob.ang += event.mgesture.dTheta;
   7.360  	    knob.r += event.mgesture.dDist;
   7.361 +	    break;
   7.362 +	  case SDL_DOLLARGESTURE:
   7.363 +	    printf("Gesture %lu performed, error: %f\n",
   7.364 +		   event.dgesture.gestureId,
   7.365 +		   event.dgesture.error);
   7.366 +	    break;
   7.367 +	  case SDL_DOLLARRECORD:
   7.368 +	    printf("Recorded gesture: %lu\n",event.dgesture.gestureId);
   7.369 +	    break;
   7.370  	  }
   7.371 -      
   7.372 -    
   7.373 -	
   7.374 -	//And draw
   7.375 -	
   7.376        }
   7.377 -    DrawScreen(screen,h);
   7.378 -    //printf("c: (%f,%f)\n",centroid.x,centroid.y);
   7.379 -    //printf("numDownFingers: %i\n",numDownFingers);
   7.380 -    //for(i=0;i<512;i++) 
   7.381 -    // if(keystat[i]) printf("%i\n",i);
   7.382 -      
   7.383 -    
   7.384 +    DrawScreen(screen,h);    
   7.385 +    for(i = 0; i < 256; i++) 
   7.386 +      if(keystat[i]) 
   7.387 +	printf("Key %i down\n",i);
   7.388    }  
   7.389    SDL_Quit();
   7.390    
     8.1 Binary file touchTest/touchPong has changed
     9.1 Binary file touchTest/touchSimp has changed