jim@4657
|
1 |
/*
|
jim@4657
|
2 |
SDL - Simple DirectMedia Layer
|
jim@4657
|
3 |
Copyright (C) 1997-2010 Sam Lantinga
|
jim@4657
|
4 |
|
jim@4657
|
5 |
This library is free software; you can redistribute it and/or
|
jim@4657
|
6 |
modify it under the terms of the GNU Lesser General Public
|
jim@4657
|
7 |
License as published by the Free Software Foundation; either
|
jim@4657
|
8 |
version 2.1 of the License, or (at your option) any later version.
|
jim@4657
|
9 |
|
jim@4657
|
10 |
This library is distributed in the hope that it will be useful,
|
jim@4657
|
11 |
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
jim@4657
|
12 |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
jim@4657
|
13 |
Lesser General Public License for more details.
|
jim@4657
|
14 |
|
jim@4657
|
15 |
You should have received a copy of the GNU Lesser General Public
|
jim@4659
|
16 |
License along with this library; if not, write to the Free Software Founation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
|
jim@4657
|
17 |
|
jim@4657
|
18 |
Sam Lantinga
|
jim@4657
|
19 |
slouken@libsdl.org
|
jim@4657
|
20 |
*/
|
jim@4657
|
21 |
#include "SDL_config.h"
|
jim@4657
|
22 |
|
jim@4657
|
23 |
/* General mouse handling code for SDL */
|
jim@4657
|
24 |
|
jim@4657
|
25 |
#include "SDL_events.h"
|
jim@4657
|
26 |
#include "SDL_events_c.h"
|
jim@4657
|
27 |
#include "SDL_gesture_c.h"
|
jim@4657
|
28 |
|
jim@4657
|
29 |
//TODO: Replace with malloc
|
jim@4657
|
30 |
#define MAXFINGERS 3
|
jim@4657
|
31 |
#define MAXTOUCHES 2
|
jim@4658
|
32 |
#define MAXTEMPLATES 4
|
jim@4658
|
33 |
#define MAXPATHSIZE 1024
|
jim@4658
|
34 |
|
jim@4658
|
35 |
#define DOLLARNPOINTS 64
|
jim@4658
|
36 |
#define DOLLARSIZE 256
|
jim@4658
|
37 |
|
jim@4658
|
38 |
//PHI = ((sqrt(5)-1)/2)
|
jim@4658
|
39 |
#define PHI 0.618033989
|
jim@4657
|
40 |
|
jim@4657
|
41 |
typedef struct {
|
jim@4657
|
42 |
float x,y;
|
jim@4657
|
43 |
} Point;
|
jim@4657
|
44 |
|
jim@4657
|
45 |
|
jim@4657
|
46 |
typedef struct {
|
jim@4657
|
47 |
Point p;
|
jim@4657
|
48 |
float pressure;
|
jim@4657
|
49 |
int id;
|
jim@4657
|
50 |
} Finger;
|
jim@4657
|
51 |
|
jim@4658
|
52 |
|
jim@4658
|
53 |
typedef struct {
|
jim@4658
|
54 |
float length;
|
jim@4658
|
55 |
|
jim@4658
|
56 |
int numPoints;
|
jim@4658
|
57 |
Point p[MAXPATHSIZE];
|
jim@4658
|
58 |
} DollarPath;
|
jim@4658
|
59 |
|
jim@4658
|
60 |
|
jim@4657
|
61 |
typedef struct {
|
jim@4657
|
62 |
Finger f;
|
jim@4657
|
63 |
Point cv;
|
jim@4657
|
64 |
float dtheta,dDist;
|
jim@4658
|
65 |
DollarPath dollarPath;
|
jim@4657
|
66 |
} TouchPoint;
|
jim@4657
|
67 |
|
jim@4659
|
68 |
typedef struct {
|
jim@4659
|
69 |
Point path[DOLLARNPOINTS];
|
jim@4659
|
70 |
unsigned long hash;
|
jim@4659
|
71 |
} DollarTemplate;
|
jim@4657
|
72 |
|
jim@4657
|
73 |
typedef struct {
|
jim@4657
|
74 |
int id;
|
jim@4657
|
75 |
Point res;
|
jim@4657
|
76 |
Point centroid;
|
jim@4657
|
77 |
TouchPoint gestureLast[MAXFINGERS];
|
jim@4657
|
78 |
int numDownFingers;
|
jim@4658
|
79 |
|
jim@4658
|
80 |
int numDollarTemplates;
|
jim@4659
|
81 |
DollarTemplate dollarTemplate[MAXTEMPLATES];
|
jim@4659
|
82 |
|
jim@4659
|
83 |
SDL_bool recording;
|
jim@4657
|
84 |
} GestureTouch;
|
jim@4657
|
85 |
|
jim@4657
|
86 |
GestureTouch gestureTouch[MAXTOUCHES];
|
jim@4657
|
87 |
int numGestureTouches = 0;
|
jim@4659
|
88 |
SDL_bool recordAll;
|
jim@4659
|
89 |
|
jim@4659
|
90 |
int SDL_RecordGesture(int touchId) {
|
jim@4659
|
91 |
int i;
|
jim@4659
|
92 |
if(touchId < 0) recordAll = SDL_TRUE;
|
jim@4659
|
93 |
for(i = 0;i < numGestureTouches; i++) {
|
jim@4659
|
94 |
if((touchId < 0) || (gestureTouch[i].id == touchId)) {
|
jim@4659
|
95 |
gestureTouch[i].recording = SDL_TRUE;
|
jim@4659
|
96 |
if(touchId >= 0)
|
jim@4659
|
97 |
return 1;
|
jim@4659
|
98 |
}
|
jim@4659
|
99 |
}
|
jim@4659
|
100 |
return (touchId < 0);
|
jim@4659
|
101 |
}
|
jim@4659
|
102 |
|
jim@4659
|
103 |
unsigned long SDL_HashDollar(Point* points) {
|
jim@4659
|
104 |
unsigned long hash = 5381;
|
jim@4659
|
105 |
int i;
|
jim@4659
|
106 |
for(i = 0;i < DOLLARNPOINTS; i++) {
|
jim@4659
|
107 |
hash = ((hash<<5) + hash) + points[i].x;
|
jim@4659
|
108 |
hash = ((hash<<5) + hash) + points[i].y;
|
jim@4659
|
109 |
}
|
jim@4659
|
110 |
return hash;
|
jim@4659
|
111 |
}
|
jim@4659
|
112 |
|
jim@4664
|
113 |
int SaveTemplate(DollarTemplate *templ, SDL_RWops * src) {
|
jim@4664
|
114 |
if(src == NULL) return 0;
|
jim@4659
|
115 |
int i;
|
jim@4664
|
116 |
|
jim@4664
|
117 |
//No Longer storing the Hash, rehash on load
|
jim@4664
|
118 |
//fprintf(fp,"%lu ",templ->hash);
|
jim@4664
|
119 |
//if(SDL_RWops.write(src,&(templ->hash),sizeof(templ->hash),1) != 1) return 0;
|
jim@4664
|
120 |
|
jim@4664
|
121 |
/*
|
jim@4659
|
122 |
for(i = 0;i < DOLLARNPOINTS;i++) {
|
jim@4659
|
123 |
fprintf(fp,"%i %i ",(int)templ->path[i].x,(int)templ->path[i].y);
|
jim@4659
|
124 |
}
|
jim@4659
|
125 |
fprintf(fp,"\n");
|
jim@4664
|
126 |
*/
|
jim@4664
|
127 |
if(SDL_RWwrite(src,templ->path,sizeof(templ->path[0]),DOLLARNPOINTS) != DOLLARNPOINTS) return 0;
|
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@4659
|
134 |
for(i = 0; i < numGestureTouches; i++) {
|
jim@4659
|
135 |
GestureTouch* touch = &gestureTouch[i];
|
jim@4659
|
136 |
for(j = 0;j < touch->numDollarTemplates; j++) {
|
jim@4664
|
137 |
rtrn += SaveTemplate(&touch->dollarTemplate[i],src);
|
jim@4659
|
138 |
}
|
jim@4659
|
139 |
}
|
jim@4659
|
140 |
return rtrn;
|
jim@4659
|
141 |
}
|
jim@4659
|
142 |
|
jim@4664
|
143 |
int SDL_SaveDollarTemplate(unsigned long gestureId, SDL_RWops *src) {
|
jim@4659
|
144 |
int i,j;
|
jim@4659
|
145 |
for(i = 0; i < numGestureTouches; i++) {
|
jim@4659
|
146 |
GestureTouch* touch = &gestureTouch[i];
|
jim@4659
|
147 |
for(j = 0;j < touch->numDollarTemplates; j++) {
|
jim@4659
|
148 |
if(touch->dollarTemplate[i].hash == gestureId) {
|
jim@4664
|
149 |
return SaveTemplate(&touch->dollarTemplate[i],src);
|
jim@4659
|
150 |
}
|
jim@4659
|
151 |
}
|
jim@4659
|
152 |
}
|
jim@4659
|
153 |
}
|
jim@4659
|
154 |
|
jim@4664
|
155 |
int SDL_LoadDollarTemplates(int touchId, SDL_RWops *src) {
|
jim@4664
|
156 |
if(src == NULL) return 0;
|
jim@4659
|
157 |
int i,loaded = 0;
|
jim@4659
|
158 |
GestureTouch *touch = NULL;
|
jim@4659
|
159 |
if(touchId >= 0) {
|
jim@4659
|
160 |
for(i = 0;i < numGestureTouches; i++)
|
jim@4659
|
161 |
if(gestureTouch[i].id == touchId)
|
jim@4659
|
162 |
touch = &gestureTouch[i];
|
jim@4659
|
163 |
if(touch == NULL) return -1;
|
jim@4659
|
164 |
}
|
jim@4659
|
165 |
|
jim@4664
|
166 |
while(1) {
|
jim@4659
|
167 |
DollarTemplate templ;
|
jim@4664
|
168 |
//fscanf(fp,"%lu ",&templ.hash);
|
jim@4664
|
169 |
/*
|
jim@4659
|
170 |
for(i = 0;i < DOLLARNPOINTS; i++) {
|
jim@4659
|
171 |
int x,y;
|
jim@4659
|
172 |
if(fscanf(fp,"%i %i ",&x,&y) != 2) break;
|
jim@4659
|
173 |
templ.path[i].x = x;
|
jim@4659
|
174 |
templ.path[i].y = y;
|
jim@4659
|
175 |
}
|
jim@4659
|
176 |
fscanf(fp,"\n");
|
jim@4664
|
177 |
*/
|
jim@4664
|
178 |
if(SDL_RWread(src,templ.path,sizeof(templ.path[0]),DOLLARNPOINTS) < DOLLARNPOINTS) break;
|
jim@4659
|
179 |
|
jim@4659
|
180 |
if(touchId >= 0) {
|
jim@4664
|
181 |
printf("Adding loaded gesture to 1 touch\n");
|
jim@4664
|
182 |
if(SDL_AddDollarGesture(touch,templ.path)) loaded++;
|
jim@4659
|
183 |
}
|
jim@4659
|
184 |
else {
|
jim@4664
|
185 |
printf("Adding to: %i touches\n",numGestureTouches);
|
jim@4659
|
186 |
for(i = 0;i < numGestureTouches; i++) {
|
jim@4664
|
187 |
touch = &gestureTouch[i];
|
jim@4664
|
188 |
printf("Adding loaded gesture to + touches\n");
|
jim@4664
|
189 |
//TODO: What if this fails?
|
jim@4664
|
190 |
SDL_AddDollarGesture(touch,templ.path);
|
jim@4659
|
191 |
}
|
jim@4659
|
192 |
loaded++;
|
jim@4659
|
193 |
}
|
jim@4659
|
194 |
}
|
jim@4659
|
195 |
|
jim@4664
|
196 |
return loaded;
|
jim@4659
|
197 |
}
|
jim@4659
|
198 |
|
jim@4659
|
199 |
|
jim@4659
|
200 |
//path is an already sampled set of points
|
jim@4659
|
201 |
//Returns the index of the gesture on success, or -1
|
jim@4659
|
202 |
int SDL_AddDollarGesture(GestureTouch* inTouch,Point* path) {
|
jim@4659
|
203 |
if(inTouch == NULL) {
|
jim@4659
|
204 |
if(numGestureTouches == 0) return -1;
|
jim@4659
|
205 |
int i = 0;
|
jim@4659
|
206 |
for(i = 0;i < numGestureTouches; i++) {
|
jim@4659
|
207 |
inTouch = &gestureTouch[i];
|
jim@4659
|
208 |
if(inTouch->numDollarTemplates < MAXTEMPLATES) {
|
jim@4659
|
209 |
DollarTemplate *templ =
|
jim@4659
|
210 |
&inTouch->dollarTemplate[inTouch->numDollarTemplates];
|
jim@4659
|
211 |
memcpy(templ->path,path,DOLLARNPOINTS*sizeof(Point));
|
jim@4659
|
212 |
templ->hash = SDL_HashDollar(templ->path);
|
jim@4659
|
213 |
inTouch->numDollarTemplates++;
|
jim@4659
|
214 |
}
|
jim@4659
|
215 |
}
|
jim@4659
|
216 |
return inTouch->numDollarTemplates - 1;
|
jim@4659
|
217 |
}else if(inTouch->numDollarTemplates < MAXTEMPLATES) {
|
jim@4659
|
218 |
DollarTemplate *templ =
|
jim@4659
|
219 |
&inTouch->dollarTemplate[inTouch->numDollarTemplates];
|
jim@4659
|
220 |
memcpy(templ->path,path,DOLLARNPOINTS*sizeof(Point));
|
jim@4659
|
221 |
templ->hash = SDL_HashDollar(templ->path);
|
jim@4659
|
222 |
inTouch->numDollarTemplates++;
|
jim@4659
|
223 |
return inTouch->numDollarTemplates - 1;
|
jim@4659
|
224 |
}
|
jim@4659
|
225 |
return -1;
|
jim@4659
|
226 |
}
|
jim@4659
|
227 |
|
jim@4659
|
228 |
|
jim@4659
|
229 |
|
jim@4658
|
230 |
|
jim@4658
|
231 |
float dollarDifference(Point* points,Point* templ,float ang) {
|
jim@4658
|
232 |
// Point p[DOLLARNPOINTS];
|
jim@4658
|
233 |
float dist = 0;
|
jim@4658
|
234 |
Point p;
|
jim@4658
|
235 |
int i;
|
jim@4658
|
236 |
for(i = 0; i < DOLLARNPOINTS; i++) {
|
jim@4658
|
237 |
p.x = points[i].x * cos(ang) - points[i].y * sin(ang);
|
jim@4658
|
238 |
p.y = points[i].x * sin(ang) + points[i].y * cos(ang);
|
jim@4658
|
239 |
dist += sqrt((p.x-templ[i].x)*(p.x-templ[i].x)+
|
jim@4658
|
240 |
(p.y-templ[i].y)*(p.y-templ[i].y));
|
jim@4658
|
241 |
}
|
jim@4658
|
242 |
return dist/DOLLARNPOINTS;
|
jim@4658
|
243 |
|
jim@4658
|
244 |
}
|
jim@4658
|
245 |
|
jim@4658
|
246 |
float bestDollarDifference(Point* points,Point* templ) {
|
jim@4658
|
247 |
//------------BEGIN DOLLAR BLACKBOX----------------//
|
jim@4658
|
248 |
//-TRANSLATED DIRECTLY FROM PSUDEO-CODE AVAILABLE AT-//
|
jim@4658
|
249 |
//-"http://depts.washington.edu/aimgroup/proj/dollar/"-//
|
jim@4658
|
250 |
float ta = -M_PI/4;
|
jim@4658
|
251 |
float tb = M_PI/4;
|
jim@4658
|
252 |
float dt = M_PI/90;
|
jim@4658
|
253 |
float x1 = PHI*ta + (1-PHI)*tb;
|
jim@4658
|
254 |
float f1 = dollarDifference(points,templ,x1);
|
jim@4658
|
255 |
float x2 = (1-PHI)*ta + PHI*tb;
|
jim@4658
|
256 |
float f2 = dollarDifference(points,templ,x2);
|
jim@4658
|
257 |
while(abs(ta-tb) > dt) {
|
jim@4658
|
258 |
if(f1 < f2) {
|
jim@4658
|
259 |
tb = x2;
|
jim@4658
|
260 |
x2 = x1;
|
jim@4658
|
261 |
f2 = f1;
|
jim@4658
|
262 |
x1 = PHI*ta + (1-PHI)*tb;
|
jim@4658
|
263 |
f1 = dollarDifference(points,templ,x1);
|
jim@4658
|
264 |
}
|
jim@4658
|
265 |
else {
|
jim@4658
|
266 |
ta = x1;
|
jim@4658
|
267 |
x1 = x2;
|
jim@4658
|
268 |
f1 = f2;
|
jim@4658
|
269 |
x2 = (1-PHI)*ta + PHI*tb;
|
jim@4658
|
270 |
f2 = dollarDifference(points,templ,x2);
|
jim@4658
|
271 |
}
|
jim@4658
|
272 |
}
|
jim@4658
|
273 |
/*
|
jim@4658
|
274 |
if(f1 <= f2)
|
jim@4658
|
275 |
printf("Min angle (x1): %f\n",x1);
|
jim@4658
|
276 |
else if(f1 > f2)
|
jim@4658
|
277 |
printf("Min angle (x2): %f\n",x2);
|
jim@4658
|
278 |
*/
|
jim@4658
|
279 |
return SDL_min(f1,f2);
|
jim@4658
|
280 |
}
|
jim@4658
|
281 |
|
jim@4658
|
282 |
float dollarRecognize(DollarPath path,int *bestTempl,GestureTouch* touch) {
|
jim@4658
|
283 |
|
jim@4658
|
284 |
Point points[DOLLARNPOINTS];
|
jim@4658
|
285 |
int numPoints = dollarNormalize(path,points);
|
jim@4658
|
286 |
int i;
|
jim@4658
|
287 |
|
jim@4658
|
288 |
int bestDiff = 10000;
|
jim@4658
|
289 |
*bestTempl = -1;
|
jim@4658
|
290 |
for(i = 0;i < touch->numDollarTemplates;i++) {
|
jim@4659
|
291 |
int diff = bestDollarDifference(points,touch->dollarTemplate[i].path);
|
jim@4658
|
292 |
if(diff < bestDiff) {bestDiff = diff; *bestTempl = i;}
|
jim@4658
|
293 |
}
|
jim@4658
|
294 |
return bestDiff;
|
jim@4658
|
295 |
}
|
jim@4658
|
296 |
|
jim@4658
|
297 |
//DollarPath contains raw points, plus (possibly) the calculated length
|
jim@4658
|
298 |
int dollarNormalize(DollarPath path,Point *points) {
|
jim@4658
|
299 |
int i;
|
jim@4658
|
300 |
//Calculate length if it hasn't already been done
|
jim@4658
|
301 |
if(path.length <= 0) {
|
jim@4658
|
302 |
for(i=1;i<path.numPoints;i++) {
|
jim@4658
|
303 |
float dx = path.p[i ].x -
|
jim@4658
|
304 |
path.p[i-1].x;
|
jim@4658
|
305 |
float dy = path.p[i ].y -
|
jim@4658
|
306 |
path.p[i-1].y;
|
jim@4658
|
307 |
path.length += sqrt(dx*dx+dy*dy);
|
jim@4658
|
308 |
}
|
jim@4658
|
309 |
}
|
jim@4658
|
310 |
|
jim@4658
|
311 |
|
jim@4658
|
312 |
//Resample
|
jim@4658
|
313 |
float interval = path.length/(DOLLARNPOINTS - 1);
|
jim@4658
|
314 |
float dist = 0;
|
jim@4658
|
315 |
|
jim@4658
|
316 |
int numPoints = 0;
|
jim@4658
|
317 |
Point centroid; centroid.x = 0;centroid.y = 0;
|
jim@4658
|
318 |
//printf("(%f,%f)\n",path.p[path.numPoints-1].x,path.p[path.numPoints-1].y);
|
jim@4658
|
319 |
for(i = 1;i < path.numPoints;i++) {
|
jim@4658
|
320 |
float d = sqrt((path.p[i-1].x-path.p[i].x)*(path.p[i-1].x-path.p[i].x)+
|
jim@4658
|
321 |
(path.p[i-1].y-path.p[i].y)*(path.p[i-1].y-path.p[i].y));
|
jim@4658
|
322 |
//printf("d = %f dist = %f/%f\n",d,dist,interval);
|
jim@4658
|
323 |
while(dist + d > interval) {
|
jim@4658
|
324 |
points[numPoints].x = path.p[i-1].x +
|
jim@4658
|
325 |
((interval-dist)/d)*(path.p[i].x-path.p[i-1].x);
|
jim@4658
|
326 |
points[numPoints].y = path.p[i-1].y +
|
jim@4658
|
327 |
((interval-dist)/d)*(path.p[i].y-path.p[i-1].y);
|
jim@4658
|
328 |
centroid.x += points[numPoints].x;
|
jim@4658
|
329 |
centroid.y += points[numPoints].y;
|
jim@4658
|
330 |
numPoints++;
|
jim@4658
|
331 |
|
jim@4658
|
332 |
dist -= interval;
|
jim@4658
|
333 |
}
|
jim@4658
|
334 |
dist += d;
|
jim@4658
|
335 |
}
|
jim@4658
|
336 |
if(numPoints < 1) return 0;
|
jim@4658
|
337 |
centroid.x /= numPoints;
|
jim@4658
|
338 |
centroid.y /= numPoints;
|
jim@4658
|
339 |
|
jim@4658
|
340 |
//printf("Centroid (%f,%f)",centroid.x,centroid.y);
|
jim@4658
|
341 |
//Rotate Points so point 0 is left of centroid and solve for the bounding box
|
jim@4658
|
342 |
float xmin,xmax,ymin,ymax;
|
jim@4658
|
343 |
xmin = centroid.x;
|
jim@4658
|
344 |
xmax = centroid.x;
|
jim@4658
|
345 |
ymin = centroid.y;
|
jim@4658
|
346 |
ymax = centroid.y;
|
jim@4658
|
347 |
|
jim@4658
|
348 |
float ang = atan2(centroid.y - points[0].y,
|
jim@4658
|
349 |
centroid.x - points[0].x);
|
jim@4658
|
350 |
|
jim@4658
|
351 |
for(i = 0;i<numPoints;i++) {
|
jim@4658
|
352 |
float px = points[i].x;
|
jim@4658
|
353 |
float py = points[i].y;
|
jim@4658
|
354 |
points[i].x = (px - centroid.x)*cos(ang) -
|
jim@4658
|
355 |
(py - centroid.y)*sin(ang) + centroid.x;
|
jim@4658
|
356 |
points[i].y = (px - centroid.x)*sin(ang) +
|
jim@4658
|
357 |
(py - centroid.y)*cos(ang) + centroid.y;
|
jim@4658
|
358 |
|
jim@4658
|
359 |
|
jim@4658
|
360 |
if(points[i].x < xmin) xmin = points[i].x;
|
jim@4658
|
361 |
if(points[i].x > xmax) xmax = points[i].x;
|
jim@4658
|
362 |
if(points[i].y < ymin) ymin = points[i].y;
|
jim@4658
|
363 |
if(points[i].y > ymax) ymax = points[i].y;
|
jim@4658
|
364 |
}
|
jim@4658
|
365 |
|
jim@4658
|
366 |
//Scale points to DOLLARSIZE, and translate to the origin
|
jim@4658
|
367 |
float w = xmax-xmin;
|
jim@4658
|
368 |
float h = ymax-ymin;
|
jim@4658
|
369 |
|
jim@4658
|
370 |
for(i=0;i<numPoints;i++) {
|
jim@4658
|
371 |
points[i].x = (points[i].x - centroid.x)*DOLLARSIZE/w;
|
jim@4658
|
372 |
points[i].y = (points[i].y - centroid.y)*DOLLARSIZE/h;
|
jim@4658
|
373 |
}
|
jim@4658
|
374 |
return numPoints;
|
jim@4658
|
375 |
}
|
jim@4658
|
376 |
|
jim@4657
|
377 |
int SDL_GestureAddTouch(SDL_Touch* touch) {
|
jim@4657
|
378 |
if(numGestureTouches >= MAXTOUCHES) return -1;
|
jim@4657
|
379 |
|
jim@4657
|
380 |
gestureTouch[numGestureTouches].res.x = touch->xres;
|
jim@4657
|
381 |
gestureTouch[numGestureTouches].res.y = touch->yres;
|
jim@4657
|
382 |
gestureTouch[numGestureTouches].numDownFingers = 0;
|
jim@4657
|
383 |
|
jim@4657
|
384 |
gestureTouch[numGestureTouches].res.x = touch->xres;
|
jim@4657
|
385 |
gestureTouch[numGestureTouches].id = touch->id;
|
jim@4657
|
386 |
|
jim@4658
|
387 |
gestureTouch[numGestureTouches].numDollarTemplates = 0;
|
jim@4658
|
388 |
|
jim@4659
|
389 |
gestureTouch[numGestureTouches].recording = SDL_FALSE;
|
jim@4659
|
390 |
|
jim@4657
|
391 |
numGestureTouches++;
|
jim@4657
|
392 |
return 0;
|
jim@4657
|
393 |
}
|
jim@4657
|
394 |
|
jim@4657
|
395 |
GestureTouch * SDL_GetGestureTouch(int id) {
|
jim@4657
|
396 |
int i;
|
jim@4657
|
397 |
for(i = 0;i < numGestureTouches; i++) {
|
jim@4657
|
398 |
//printf("%i ?= %i\n",gestureTouch[i].id,id);
|
jim@4657
|
399 |
if(gestureTouch[i].id == id) return &gestureTouch[i];
|
jim@4657
|
400 |
}
|
jim@4657
|
401 |
return NULL;
|
jim@4657
|
402 |
}
|
jim@4657
|
403 |
|
jim@4657
|
404 |
int SDL_SendGestureMulti(GestureTouch* touch,float dTheta,float dDist) {
|
jim@4657
|
405 |
SDL_Event event;
|
jim@4657
|
406 |
event.mgesture.type = SDL_MULTIGESTURE;
|
jim@4657
|
407 |
event.mgesture.touchId = touch->id;
|
jim@4657
|
408 |
event.mgesture.x = touch->centroid.x;
|
jim@4657
|
409 |
event.mgesture.y = touch->centroid.y;
|
jim@4657
|
410 |
event.mgesture.dTheta = dTheta;
|
jim@4657
|
411 |
event.mgesture.dDist = dDist;
|
jim@4657
|
412 |
return SDL_PushEvent(&event) > 0;
|
jim@4657
|
413 |
}
|
jim@4657
|
414 |
|
jim@4658
|
415 |
int SDL_SendGestureDollar(GestureTouch* touch,int gestureId,float error) {
|
jim@4658
|
416 |
SDL_Event event;
|
jim@4658
|
417 |
event.dgesture.type = SDL_DOLLARGESTURE;
|
jim@4658
|
418 |
event.dgesture.touchId = touch->id;
|
jim@4658
|
419 |
/*
|
jim@4658
|
420 |
//TODO: Add this to give location of gesture?
|
jim@4658
|
421 |
event.mgesture.x = touch->centroid.x;
|
jim@4658
|
422 |
event.mgesture.y = touch->centroid.y;
|
jim@4658
|
423 |
*/
|
jim@4658
|
424 |
event.dgesture.gestureId = gestureId;
|
jim@4658
|
425 |
event.dgesture.error = error;
|
jim@4658
|
426 |
return SDL_PushEvent(&event) > 0;
|
jim@4658
|
427 |
}
|
jim@4658
|
428 |
|
jim@4659
|
429 |
|
jim@4659
|
430 |
int SDL_SendDollarRecord(GestureTouch* touch,int gestureId) {
|
jim@4659
|
431 |
SDL_Event event;
|
jim@4659
|
432 |
event.dgesture.type = SDL_DOLLARRECORD;
|
jim@4659
|
433 |
event.dgesture.touchId = touch->id;
|
jim@4659
|
434 |
event.dgesture.gestureId = gestureId;
|
jim@4659
|
435 |
|
jim@4659
|
436 |
return SDL_PushEvent(&event) > 0;
|
jim@4659
|
437 |
}
|
jim@4659
|
438 |
|
jim@4659
|
439 |
|
jim@4657
|
440 |
void SDL_GestureProcessEvent(SDL_Event* event)
|
jim@4657
|
441 |
{
|
jim@4657
|
442 |
if(event->type == SDL_FINGERMOTION ||
|
jim@4657
|
443 |
event->type == SDL_FINGERDOWN ||
|
jim@4657
|
444 |
event->type == SDL_FINGERUP) {
|
jim@4657
|
445 |
GestureTouch* inTouch = SDL_GetGestureTouch(event->tfinger.touchId);
|
jim@4657
|
446 |
|
jim@4657
|
447 |
//Shouldn't be possible
|
jim@4657
|
448 |
if(inTouch == NULL) return;
|
jim@4657
|
449 |
|
jim@4657
|
450 |
|
jim@4657
|
451 |
float x = ((float)event->tfinger.x)/inTouch->res.x;
|
jim@4657
|
452 |
float y = ((float)event->tfinger.y)/inTouch->res.y;
|
jim@4657
|
453 |
int j,empty = -1;
|
jim@4657
|
454 |
|
jim@4657
|
455 |
for(j = 0;j<inTouch->numDownFingers;j++) {
|
jim@4657
|
456 |
if(inTouch->gestureLast[j].f.id != event->tfinger.fingerId) continue;
|
jim@4659
|
457 |
//Finger Up
|
jim@4657
|
458 |
if(event->type == SDL_FINGERUP) {
|
jim@4657
|
459 |
inTouch->numDownFingers--;
|
jim@4658
|
460 |
|
jim@4659
|
461 |
if(inTouch->recording) {
|
jim@4659
|
462 |
inTouch->recording = SDL_FALSE;
|
jim@4659
|
463 |
Point path[DOLLARNPOINTS];
|
jim@4659
|
464 |
dollarNormalize(inTouch->gestureLast[j].dollarPath,path);
|
jim@4659
|
465 |
int index;
|
jim@4659
|
466 |
if(recordAll) {
|
jim@4659
|
467 |
index = SDL_AddDollarGesture(NULL,path);
|
jim@4659
|
468 |
int i;
|
jim@4659
|
469 |
for(i = 0;i < numGestureTouches; i++)
|
jim@4659
|
470 |
gestureTouch[i].recording = SDL_FALSE;
|
jim@4659
|
471 |
}
|
jim@4659
|
472 |
else {
|
jim@4659
|
473 |
index = SDL_AddDollarGesture(inTouch,path);
|
jim@4659
|
474 |
}
|
jim@4659
|
475 |
|
jim@4659
|
476 |
if(index >= 0) {
|
jim@4659
|
477 |
SDL_SendDollarRecord(inTouch,inTouch->dollarTemplate[index].hash);
|
jim@4659
|
478 |
}
|
jim@4659
|
479 |
else {
|
jim@4659
|
480 |
SDL_SendDollarRecord(inTouch,-1);
|
jim@4659
|
481 |
}
|
jim@4658
|
482 |
}
|
jim@4659
|
483 |
else {
|
jim@4659
|
484 |
int bestTempl;
|
jim@4659
|
485 |
float error;
|
jim@4659
|
486 |
error = dollarRecognize(inTouch->gestureLast[j].dollarPath,
|
jim@4659
|
487 |
&bestTempl,inTouch);
|
jim@4659
|
488 |
if(bestTempl >= 0){
|
jim@4659
|
489 |
//Send Event
|
jim@4659
|
490 |
int gestureId = inTouch->dollarTemplate[bestTempl].hash;
|
jim@4659
|
491 |
SDL_SendGestureDollar(inTouch,gestureId,error);
|
jim@4659
|
492 |
printf("Dollar error: %f\n",error);
|
jim@4659
|
493 |
}
|
jim@4659
|
494 |
}
|
jim@4657
|
495 |
inTouch->gestureLast[j] = inTouch->gestureLast[inTouch->numDownFingers];
|
jim@4657
|
496 |
break;
|
jim@4657
|
497 |
}
|
jim@4657
|
498 |
else {
|
jim@4657
|
499 |
float dx = x - inTouch->gestureLast[j].f.p.x;
|
jim@4657
|
500 |
float dy = y - inTouch->gestureLast[j].f.p.y;
|
jim@4658
|
501 |
DollarPath* path = &inTouch->gestureLast[j].dollarPath;
|
jim@4658
|
502 |
if(path->numPoints < MAXPATHSIZE) {
|
jim@4658
|
503 |
path->p[path->numPoints].x = x;
|
jim@4658
|
504 |
path->p[path->numPoints].y = y;
|
jim@4658
|
505 |
path->length += sqrt(dx*dx + dy*dy);
|
jim@4658
|
506 |
path->numPoints++;
|
jim@4658
|
507 |
}
|
jim@4658
|
508 |
|
jim@4658
|
509 |
|
jim@4657
|
510 |
inTouch->centroid.x += dx/inTouch->numDownFingers;
|
jim@4657
|
511 |
inTouch->centroid.y += dy/inTouch->numDownFingers;
|
jim@4657
|
512 |
if(inTouch->numDownFingers > 1) {
|
jim@4657
|
513 |
Point lv; //Vector from centroid to last x,y position
|
jim@4657
|
514 |
Point v; //Vector from centroid to current x,y position
|
jim@4657
|
515 |
lv = inTouch->gestureLast[j].cv;
|
jim@4657
|
516 |
float lDist = sqrt(lv.x*lv.x + lv.y*lv.y);
|
jim@4657
|
517 |
//printf("lDist = %f\n",lDist);
|
jim@4657
|
518 |
v.x = x - inTouch->centroid.x;
|
jim@4657
|
519 |
v.y = y - inTouch->centroid.y;
|
jim@4657
|
520 |
inTouch->gestureLast[j].cv = v;
|
jim@4657
|
521 |
float Dist = sqrt(v.x*v.x+v.y*v.y);
|
jim@4657
|
522 |
// cos(dTheta) = (v . lv)/(|v| * |lv|)
|
jim@4657
|
523 |
|
jim@4657
|
524 |
//Normalize Vectors to simplify angle calculation
|
jim@4657
|
525 |
lv.x/=lDist;
|
jim@4657
|
526 |
lv.y/=lDist;
|
jim@4657
|
527 |
v.x/=Dist;
|
jim@4657
|
528 |
v.y/=Dist;
|
jim@4657
|
529 |
float dtheta = atan2(lv.x*v.y - lv.y*v.x,lv.x*v.x + lv.y*v.y);
|
jim@4657
|
530 |
|
jim@4657
|
531 |
float dDist = (Dist - lDist);
|
jim@4657
|
532 |
if(lDist == 0) {dDist = 0;dtheta = 0;} //To avoid impossible values
|
jim@4657
|
533 |
inTouch->gestureLast[j].dDist = dDist;
|
jim@4657
|
534 |
inTouch->gestureLast[j].dtheta = dtheta;
|
jim@4657
|
535 |
|
jim@4657
|
536 |
//printf("dDist = %f, dTheta = %f\n",dDist,dtheta);
|
jim@4657
|
537 |
//gdtheta = gdtheta*.9 + dtheta*.1;
|
jim@4657
|
538 |
//gdDist = gdDist*.9 + dDist*.1
|
jim@4657
|
539 |
//knob.r += dDist/numDownFingers;
|
jim@4657
|
540 |
//knob.ang += dtheta;
|
jim@4657
|
541 |
//printf("thetaSum = %f, distSum = %f\n",gdtheta,gdDist);
|
jim@4657
|
542 |
//printf("id: %i dTheta = %f, dDist = %f\n",j,dtheta,dDist);
|
jim@4657
|
543 |
SDL_SendGestureMulti(inTouch,dtheta,dDist);
|
jim@4657
|
544 |
}
|
jim@4657
|
545 |
else {
|
jim@4657
|
546 |
inTouch->gestureLast[j].dDist = 0;
|
jim@4657
|
547 |
inTouch->gestureLast[j].dtheta = 0;
|
jim@4657
|
548 |
inTouch->gestureLast[j].cv.x = 0;
|
jim@4657
|
549 |
inTouch->gestureLast[j].cv.y = 0;
|
jim@4657
|
550 |
}
|
jim@4657
|
551 |
inTouch->gestureLast[j].f.p.x = x;
|
jim@4657
|
552 |
inTouch->gestureLast[j].f.p.y = y;
|
jim@4657
|
553 |
break;
|
jim@4657
|
554 |
//pressure?
|
jim@4657
|
555 |
}
|
jim@4657
|
556 |
}
|
jim@4657
|
557 |
|
jim@4657
|
558 |
if(j == inTouch->numDownFingers) {
|
jim@4657
|
559 |
//printf("Finger Down!!!\n");
|
jim@4657
|
560 |
inTouch->numDownFingers++;
|
jim@4657
|
561 |
inTouch->centroid.x = (inTouch->centroid.x*(inTouch->numDownFingers - 1)+
|
jim@4657
|
562 |
x)/inTouch->numDownFingers;
|
jim@4657
|
563 |
inTouch->centroid.y = (inTouch->centroid.y*(inTouch->numDownFingers - 1)+
|
jim@4657
|
564 |
y)/inTouch->numDownFingers;
|
jim@4657
|
565 |
|
jim@4657
|
566 |
inTouch->gestureLast[j].f.id = event->tfinger.fingerId;
|
jim@4657
|
567 |
inTouch->gestureLast[j].f.p.x = x;
|
jim@4657
|
568 |
inTouch->gestureLast[j].f.p.y = y;
|
jim@4657
|
569 |
inTouch->gestureLast[j].cv.x = 0;
|
jim@4657
|
570 |
inTouch->gestureLast[j].cv.y = 0;
|
jim@4658
|
571 |
|
jim@4658
|
572 |
inTouch->gestureLast[j].dollarPath.length = 0;
|
jim@4658
|
573 |
inTouch->gestureLast[j].dollarPath.p[0].x = x;
|
jim@4658
|
574 |
inTouch->gestureLast[j].dollarPath.p[0].y = y;
|
jim@4658
|
575 |
inTouch->gestureLast[j].dollarPath.numPoints = 1;
|
jim@4657
|
576 |
}
|
jim@4657
|
577 |
}
|
jim@4657
|
578 |
}
|
jim@4657
|
579 |
|
jim@4657
|
580 |
/* vi: set ts=4 sw=4 expandtab: */
|
jim@4657
|
581 |
|