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