Mercurial > sdl-ios-xcode
comparison src/events/SDL_gesture.c @ 4880:27c458e4ae31
Update VS2010 project to add new files; update new files so code builds on Win32/Win64
author | Andreas Schiffler <aschiffler@ferzkopp.net> |
---|---|
date | Mon, 23 Aug 2010 23:44:28 -0700 |
parents | f9ab8df6d45a |
children |
comparison
equal
deleted
inserted
replaced
4864:ebcb4988b16f | 4880:27c458e4ae31 |
---|---|
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 | 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 |
17 | 17 |
18 Sam Lantinga | 18 Sam Lantinga |
19 slouken@libsdl.org | 19 slouken@libsdl.org |
20 */ | 20 */ |
21 | |
21 #include "SDL_config.h" | 22 #include "SDL_config.h" |
22 | 23 |
23 /* General mouse handling code for SDL */ | 24 /* General mouse handling code for SDL */ |
24 | 25 |
25 #include "SDL_events.h" | 26 #include "SDL_events.h" |
26 #include "SDL_events_c.h" | 27 #include "SDL_events_c.h" |
27 #include "SDL_gesture_c.h" | 28 #include "SDL_gesture_c.h" |
29 | |
30 #include <memory.h> | |
31 #include <string.h> | |
32 #include <stdio.h> | |
33 #include <math.h> | |
28 | 34 |
29 //TODO: Replace with malloc | 35 //TODO: Replace with malloc |
30 | 36 |
31 #define MAXPATHSIZE 1024 | 37 #define MAXPATHSIZE 1024 |
32 | 38 |
98 | 104 |
99 unsigned long SDL_HashDollar(SDL_FloatPoint* points) { | 105 unsigned long SDL_HashDollar(SDL_FloatPoint* points) { |
100 unsigned long hash = 5381; | 106 unsigned long hash = 5381; |
101 int i; | 107 int i; |
102 for(i = 0;i < DOLLARNPOINTS; i++) { | 108 for(i = 0;i < DOLLARNPOINTS; i++) { |
103 hash = ((hash<<5) + hash) + points[i].x; | 109 hash = ((hash<<5) + hash) + (unsigned long)points[i].x; |
104 hash = ((hash<<5) + hash) + points[i].y; | 110 hash = ((hash<<5) + hash) + (unsigned long)points[i].y; |
105 } | 111 } |
106 return hash; | 112 return hash; |
107 } | 113 } |
108 | 114 |
109 | 115 |
110 static int SaveTemplate(SDL_DollarTemplate *templ, SDL_RWops * src) { | 116 static int SaveTemplate(SDL_DollarTemplate *templ, SDL_RWops * src) { |
111 if(src == NULL) return 0; | 117 if(src == NULL) return 0; |
112 | 118 |
113 int i; | |
114 | 119 |
115 //No Longer storing the Hash, rehash on load | 120 //No Longer storing the Hash, rehash on load |
116 //if(SDL_RWops.write(src,&(templ->hash),sizeof(templ->hash),1) != 1) return 0; | 121 //if(SDL_RWops.write(src,&(templ->hash),sizeof(templ->hash),1) != 1) return 0; |
117 | 122 |
118 if(SDL_RWwrite(src,templ->path, | 123 if(SDL_RWwrite(src,templ->path, |
149 } | 154 } |
150 | 155 |
151 //path is an already sampled set of points | 156 //path is an already sampled set of points |
152 //Returns the index of the gesture on success, or -1 | 157 //Returns the index of the gesture on success, or -1 |
153 static int SDL_AddDollarGesture(SDL_GestureTouch* inTouch,SDL_FloatPoint* path) { | 158 static int SDL_AddDollarGesture(SDL_GestureTouch* inTouch,SDL_FloatPoint* path) { |
159 SDL_DollarTemplate* dollarTemplate; | |
160 SDL_DollarTemplate *templ; | |
161 int i = 0; | |
154 if(inTouch == NULL) { | 162 if(inTouch == NULL) { |
155 if(SDL_numGestureTouches == 0) return -1; | 163 if(SDL_numGestureTouches == 0) return -1; |
156 int i = 0; | |
157 for(i = 0;i < SDL_numGestureTouches; i++) { | 164 for(i = 0;i < SDL_numGestureTouches; i++) { |
158 inTouch = &SDL_gestureTouch[i]; | 165 inTouch = &SDL_gestureTouch[i]; |
159 | 166 |
160 SDL_DollarTemplate* dollarTemplate = | 167 dollarTemplate = |
161 SDL_realloc(inTouch->dollarTemplate, | 168 (SDL_DollarTemplate *)SDL_realloc(inTouch->dollarTemplate, |
162 (inTouch->numDollarTemplates + 1) * | 169 (inTouch->numDollarTemplates + 1) * |
163 sizeof(SDL_DollarTemplate)); | 170 sizeof(SDL_DollarTemplate)); |
164 if(!dollarTemplate) { | 171 if(!dollarTemplate) { |
165 SDL_OutOfMemory(); | 172 SDL_OutOfMemory(); |
166 return -1; | 173 return -1; |
167 } | 174 } |
168 | 175 |
169 inTouch->dollarTemplate = dollarTemplate; | 176 inTouch->dollarTemplate = dollarTemplate; |
170 | 177 |
171 SDL_DollarTemplate *templ = | 178 templ = |
172 &inTouch->dollarTemplate[inTouch->numDollarTemplates]; | 179 &inTouch->dollarTemplate[inTouch->numDollarTemplates]; |
173 memcpy(templ->path,path,DOLLARNPOINTS*sizeof(SDL_FloatPoint)); | 180 memcpy(templ->path,path,DOLLARNPOINTS*sizeof(SDL_FloatPoint)); |
174 templ->hash = SDL_HashDollar(templ->path); | 181 templ->hash = SDL_HashDollar(templ->path); |
175 inTouch->numDollarTemplates++; | 182 inTouch->numDollarTemplates++; |
176 } | 183 } |
177 return inTouch->numDollarTemplates - 1; | 184 return inTouch->numDollarTemplates - 1; |
178 } else { | 185 } else { |
179 SDL_DollarTemplate* dollarTemplate = | 186 SDL_DollarTemplate* dollarTemplate = |
180 SDL_realloc(inTouch->dollarTemplate, | 187 ( SDL_DollarTemplate *)SDL_realloc(inTouch->dollarTemplate, |
181 (inTouch->numDollarTemplates + 1) * | 188 (inTouch->numDollarTemplates + 1) * |
182 sizeof(SDL_DollarTemplate)); | 189 sizeof(SDL_DollarTemplate)); |
183 if(!dollarTemplate) { | 190 if(!dollarTemplate) { |
184 SDL_OutOfMemory(); | 191 SDL_OutOfMemory(); |
185 return -1; | 192 return -1; |
186 } | 193 } |
187 | 194 |
188 inTouch->dollarTemplate = dollarTemplate; | 195 inTouch->dollarTemplate = dollarTemplate; |
189 | 196 |
190 SDL_DollarTemplate *templ = | 197 templ = |
191 &inTouch->dollarTemplate[inTouch->numDollarTemplates]; | 198 &inTouch->dollarTemplate[inTouch->numDollarTemplates]; |
192 memcpy(templ->path,path,DOLLARNPOINTS*sizeof(SDL_FloatPoint)); | 199 memcpy(templ->path,path,DOLLARNPOINTS*sizeof(SDL_FloatPoint)); |
193 templ->hash = SDL_HashDollar(templ->path); | 200 templ->hash = SDL_HashDollar(templ->path); |
194 inTouch->numDollarTemplates++; | 201 inTouch->numDollarTemplates++; |
195 return inTouch->numDollarTemplates - 1; | 202 return inTouch->numDollarTemplates - 1; |
196 } | 203 } |
197 return -1; | 204 return -1; |
198 } | 205 } |
199 | 206 |
200 int SDL_LoadDollarTemplates(SDL_TouchID touchId, SDL_RWops *src) { | 207 int SDL_LoadDollarTemplates(SDL_TouchID touchId, SDL_RWops *src) { |
201 if(src == NULL) return 0; | |
202 int i,loaded = 0; | 208 int i,loaded = 0; |
203 SDL_GestureTouch *touch = NULL; | 209 SDL_GestureTouch *touch = NULL; |
210 if(src == NULL) return 0; | |
204 if(touchId >= 0) { | 211 if(touchId >= 0) { |
205 for(i = 0;i < SDL_numGestureTouches; i++) | 212 for(i = 0;i < SDL_numGestureTouches; i++) |
206 if(SDL_gestureTouch[i].id == touchId) | 213 if(SDL_gestureTouch[i].id == touchId) |
207 touch = &SDL_gestureTouch[i]; | 214 touch = &SDL_gestureTouch[i]; |
208 if(touch == NULL) return -1; | 215 if(touch == NULL) return -1; |
238 // SDL_FloatPoint p[DOLLARNPOINTS]; | 245 // SDL_FloatPoint p[DOLLARNPOINTS]; |
239 float dist = 0; | 246 float dist = 0; |
240 SDL_FloatPoint p; | 247 SDL_FloatPoint p; |
241 int i; | 248 int i; |
242 for(i = 0; i < DOLLARNPOINTS; i++) { | 249 for(i = 0; i < DOLLARNPOINTS; i++) { |
243 p.x = points[i].x * cos(ang) - points[i].y * sin(ang); | 250 p.x = (float)(points[i].x * cos(ang) - points[i].y * sin(ang)); |
244 p.y = points[i].x * sin(ang) + points[i].y * cos(ang); | 251 p.y = (float)(points[i].x * sin(ang) + points[i].y * cos(ang)); |
245 dist += sqrt((p.x-templ[i].x)*(p.x-templ[i].x)+ | 252 dist += (float)(sqrt((p.x-templ[i].x)*(p.x-templ[i].x)+ |
246 (p.y-templ[i].y)*(p.y-templ[i].y)); | 253 (p.y-templ[i].y)*(p.y-templ[i].y))); |
247 } | 254 } |
248 return dist/DOLLARNPOINTS; | 255 return dist/DOLLARNPOINTS; |
249 | 256 |
250 } | 257 } |
251 | 258 |
252 float bestDollarDifference(SDL_FloatPoint* points,SDL_FloatPoint* templ) { | 259 float bestDollarDifference(SDL_FloatPoint* points,SDL_FloatPoint* templ) { |
253 //------------BEGIN DOLLAR BLACKBOX----------------// | 260 //------------BEGIN DOLLAR BLACKBOX----------------// |
254 //-TRANSLATED DIRECTLY FROM PSUDEO-CODE AVAILABLE AT-// | 261 //-TRANSLATED DIRECTLY FROM PSUDEO-CODE AVAILABLE AT-// |
255 //-"http://depts.washington.edu/aimgroup/proj/dollar/"-// | 262 //-"http://depts.washington.edu/aimgroup/proj/dollar/"-// |
256 float ta = -M_PI/4; | 263 double ta = -M_PI/4; |
257 float tb = M_PI/4; | 264 double tb = M_PI/4; |
258 float dt = M_PI/90; | 265 double dt = M_PI/90; |
259 float x1 = PHI*ta + (1-PHI)*tb; | 266 float x1 = (float)(PHI*ta + (1-PHI)*tb); |
260 float f1 = dollarDifference(points,templ,x1); | 267 float f1 = dollarDifference(points,templ,x1); |
261 float x2 = (1-PHI)*ta + PHI*tb; | 268 float x2 = (float)((1-PHI)*ta + PHI*tb); |
262 float f2 = dollarDifference(points,templ,x2); | 269 float f2 = dollarDifference(points,templ,x2); |
263 while(abs(ta-tb) > dt) { | 270 while(fabs(ta-tb) > dt) { |
264 if(f1 < f2) { | 271 if(f1 < f2) { |
265 tb = x2; | 272 tb = x2; |
266 x2 = x1; | 273 x2 = x1; |
267 f2 = f1; | 274 f2 = f1; |
268 x1 = PHI*ta + (1-PHI)*tb; | 275 x1 = (float)(PHI*ta + (1-PHI)*tb); |
269 f1 = dollarDifference(points,templ,x1); | 276 f1 = dollarDifference(points,templ,x1); |
270 } | 277 } |
271 else { | 278 else { |
272 ta = x1; | 279 ta = x1; |
273 x1 = x2; | 280 x1 = x2; |
274 f1 = f2; | 281 f1 = f2; |
275 x2 = (1-PHI)*ta + PHI*tb; | 282 x2 = (float)((1-PHI)*ta + PHI*tb); |
276 f2 = dollarDifference(points,templ,x2); | 283 f2 = dollarDifference(points,templ,x2); |
277 } | 284 } |
278 } | 285 } |
279 /* | 286 /* |
280 if(f1 <= f2) | 287 if(f1 <= f2) |
286 } | 293 } |
287 | 294 |
288 //DollarPath contains raw points, plus (possibly) the calculated length | 295 //DollarPath contains raw points, plus (possibly) the calculated length |
289 int dollarNormalize(SDL_DollarPath path,SDL_FloatPoint *points) { | 296 int dollarNormalize(SDL_DollarPath path,SDL_FloatPoint *points) { |
290 int i; | 297 int i; |
298 float interval; | |
299 float dist; | |
300 int numPoints = 0; | |
301 SDL_FloatPoint centroid; | |
302 float xmin,xmax,ymin,ymax; | |
303 float ang; | |
304 float w,h; | |
305 | |
291 //Calculate length if it hasn't already been done | 306 //Calculate length if it hasn't already been done |
292 if(path.length <= 0) { | 307 if(path.length <= 0) { |
293 for(i=1;i<path.numPoints;i++) { | 308 for(i=1;i<path.numPoints;i++) { |
294 float dx = path.p[i ].x - | 309 float dx = path.p[i ].x - |
295 path.p[i-1].x; | 310 path.p[i-1].x; |
296 float dy = path.p[i ].y - | 311 float dy = path.p[i ].y - |
297 path.p[i-1].y; | 312 path.p[i-1].y; |
298 path.length += sqrt(dx*dx+dy*dy); | 313 path.length += (float)(sqrt(dx*dx+dy*dy)); |
299 } | 314 } |
300 } | 315 } |
301 | 316 |
302 //Resample | 317 //Resample |
303 float interval = path.length/(DOLLARNPOINTS - 1); | 318 interval = path.length/(DOLLARNPOINTS - 1); |
304 float dist = interval; | 319 dist = interval; |
305 | 320 |
306 int numPoints = 0; | |
307 SDL_FloatPoint centroid; | |
308 centroid.x = 0;centroid.y = 0; | 321 centroid.x = 0;centroid.y = 0; |
309 | 322 |
310 //printf("(%f,%f)\n",path.p[path.numPoints-1].x,path.p[path.numPoints-1].y); | 323 //printf("(%f,%f)\n",path.p[path.numPoints-1].x,path.p[path.numPoints-1].y); |
311 for(i = 1;i < path.numPoints;i++) { | 324 for(i = 1;i < path.numPoints;i++) { |
312 float d = sqrt((path.p[i-1].x-path.p[i].x)*(path.p[i-1].x-path.p[i].x)+ | 325 float d = (float)(sqrt((path.p[i-1].x-path.p[i].x)*(path.p[i-1].x-path.p[i].x)+ |
313 (path.p[i-1].y-path.p[i].y)*(path.p[i-1].y-path.p[i].y)); | 326 (path.p[i-1].y-path.p[i].y)*(path.p[i-1].y-path.p[i].y))); |
314 //printf("d = %f dist = %f/%f\n",d,dist,interval); | 327 //printf("d = %f dist = %f/%f\n",d,dist,interval); |
315 while(dist + d > interval) { | 328 while(dist + d > interval) { |
316 points[numPoints].x = path.p[i-1].x + | 329 points[numPoints].x = path.p[i-1].x + |
317 ((interval-dist)/d)*(path.p[i].x-path.p[i-1].x); | 330 ((interval-dist)/d)*(path.p[i].x-path.p[i-1].x); |
318 points[numPoints].y = path.p[i-1].y + | 331 points[numPoints].y = path.p[i-1].y + |
336 centroid.x /= numPoints; | 349 centroid.x /= numPoints; |
337 centroid.y /= numPoints; | 350 centroid.y /= numPoints; |
338 | 351 |
339 //printf("Centroid (%f,%f)",centroid.x,centroid.y); | 352 //printf("Centroid (%f,%f)",centroid.x,centroid.y); |
340 //Rotate Points so point 0 is left of centroid and solve for the bounding box | 353 //Rotate Points so point 0 is left of centroid and solve for the bounding box |
341 float xmin,xmax,ymin,ymax; | |
342 xmin = centroid.x; | 354 xmin = centroid.x; |
343 xmax = centroid.x; | 355 xmax = centroid.x; |
344 ymin = centroid.y; | 356 ymin = centroid.y; |
345 ymax = centroid.y; | 357 ymax = centroid.y; |
346 | 358 |
347 float ang = atan2(centroid.y - points[0].y, | 359 ang = (float)(atan2(centroid.y - points[0].y, |
348 centroid.x - points[0].x); | 360 centroid.x - points[0].x)); |
349 | 361 |
350 for(i = 0;i<numPoints;i++) { | 362 for(i = 0;i<numPoints;i++) { |
351 float px = points[i].x; | 363 float px = points[i].x; |
352 float py = points[i].y; | 364 float py = points[i].y; |
353 points[i].x = (px - centroid.x)*cos(ang) - | 365 points[i].x = (float)((px - centroid.x)*cos(ang) - |
354 (py - centroid.y)*sin(ang) + centroid.x; | 366 (py - centroid.y)*sin(ang) + centroid.x); |
355 points[i].y = (px - centroid.x)*sin(ang) + | 367 points[i].y = (float)((px - centroid.x)*sin(ang) + |
356 (py - centroid.y)*cos(ang) + centroid.y; | 368 (py - centroid.y)*cos(ang) + centroid.y); |
357 | 369 |
358 | 370 |
359 if(points[i].x < xmin) xmin = points[i].x; | 371 if(points[i].x < xmin) xmin = points[i].x; |
360 if(points[i].x > xmax) xmax = points[i].x; | 372 if(points[i].x > xmax) xmax = points[i].x; |
361 if(points[i].y < ymin) ymin = points[i].y; | 373 if(points[i].y < ymin) ymin = points[i].y; |
362 if(points[i].y > ymax) ymax = points[i].y; | 374 if(points[i].y > ymax) ymax = points[i].y; |
363 } | 375 } |
364 | 376 |
365 //Scale points to DOLLARSIZE, and translate to the origin | 377 //Scale points to DOLLARSIZE, and translate to the origin |
366 float w = xmax-xmin; | 378 w = xmax-xmin; |
367 float h = ymax-ymin; | 379 h = ymax-ymin; |
368 | 380 |
369 for(i=0;i<numPoints;i++) { | 381 for(i=0;i<numPoints;i++) { |
370 points[i].x = (points[i].x - centroid.x)*DOLLARSIZE/w; | 382 points[i].x = (points[i].x - centroid.x)*DOLLARSIZE/w; |
371 points[i].y = (points[i].y - centroid.y)*DOLLARSIZE/h; | 383 points[i].y = (points[i].y - centroid.y)*DOLLARSIZE/h; |
372 } | 384 } |
378 SDL_FloatPoint points[DOLLARNPOINTS]; | 390 SDL_FloatPoint points[DOLLARNPOINTS]; |
379 int numPoints = dollarNormalize(path,points); | 391 int numPoints = dollarNormalize(path,points); |
380 //SDL_PrintPath(points); | 392 //SDL_PrintPath(points); |
381 int i; | 393 int i; |
382 | 394 |
383 int bestDiff = 10000; | 395 float bestDiff = 10000; |
384 *bestTempl = -1; | 396 *bestTempl = -1; |
385 for(i = 0;i < touch->numDollarTemplates;i++) { | 397 for(i = 0;i < touch->numDollarTemplates;i++) { |
386 int diff = bestDollarDifference(points,touch->dollarTemplate[i].path); | 398 float diff = bestDollarDifference(points,touch->dollarTemplate[i].path); |
387 if(diff < bestDiff) {bestDiff = diff; *bestTempl = i;} | 399 if(diff < bestDiff) {bestDiff = diff; *bestTempl = i;} |
388 } | 400 } |
389 return bestDiff; | 401 return bestDiff; |
390 } | 402 } |
391 | 403 |
392 int SDL_GestureAddTouch(SDL_Touch* touch) { | 404 int SDL_GestureAddTouch(SDL_Touch* touch) { |
393 SDL_GestureTouch *gestureTouch = SDL_realloc(SDL_gestureTouch, | 405 SDL_GestureTouch *gestureTouch = (SDL_GestureTouch *)SDL_realloc(SDL_gestureTouch, |
394 (SDL_numGestureTouches + 1) * | 406 (SDL_numGestureTouches + 1) * |
395 sizeof(SDL_GestureTouch)); | 407 sizeof(SDL_GestureTouch)); |
396 | 408 |
397 if(!gestureTouch) { | 409 if(!gestureTouch) { |
398 SDL_OutOfMemory(); | 410 SDL_OutOfMemory(); |
477 } | 489 } |
478 | 490 |
479 | 491 |
480 void SDL_GestureProcessEvent(SDL_Event* event) | 492 void SDL_GestureProcessEvent(SDL_Event* event) |
481 { | 493 { |
494 float x,y; | |
495 SDL_FloatPoint path[DOLLARNPOINTS]; | |
496 int index; | |
497 int i; | |
498 float pathDx, pathDy; | |
499 SDL_FloatPoint lastP; | |
500 SDL_FloatPoint lastCentroid; | |
501 float lDist; | |
502 float Dist; | |
503 float dtheta; | |
504 float dDist; | |
505 | |
482 if(event->type == SDL_FINGERMOTION || | 506 if(event->type == SDL_FINGERMOTION || |
483 event->type == SDL_FINGERDOWN || | 507 event->type == SDL_FINGERDOWN || |
484 event->type == SDL_FINGERUP) { | 508 event->type == SDL_FINGERUP) { |
485 SDL_GestureTouch* inTouch = SDL_GetGestureTouch(event->tfinger.touchId); | 509 SDL_GestureTouch* inTouch = SDL_GetGestureTouch(event->tfinger.touchId); |
486 | 510 |
490 //printf("@ (%i,%i) with res: (%i,%i)\n",(int)event->tfinger.x, | 514 //printf("@ (%i,%i) with res: (%i,%i)\n",(int)event->tfinger.x, |
491 // (int)event->tfinger.y, | 515 // (int)event->tfinger.y, |
492 // (int)inTouch->res.x,(int)inTouch->res.y); | 516 // (int)inTouch->res.x,(int)inTouch->res.y); |
493 | 517 |
494 | 518 |
495 float x = ((float)event->tfinger.x)/(float)inTouch->res.x; | 519 x = ((float)event->tfinger.x)/(float)inTouch->res.x; |
496 float y = ((float)event->tfinger.y)/(float)inTouch->res.y; | 520 y = ((float)event->tfinger.y)/(float)inTouch->res.y; |
497 | 521 |
498 | 522 |
499 //Finger Up | 523 //Finger Up |
500 if(event->type == SDL_FINGERUP) { | 524 if(event->type == SDL_FINGERUP) { |
501 inTouch->numDownFingers--; | 525 inTouch->numDownFingers--; |
502 | 526 |
503 #ifdef ENABLE_DOLLAR | 527 #ifdef ENABLE_DOLLAR |
504 if(inTouch->recording) { | 528 if(inTouch->recording) { |
505 inTouch->recording = SDL_FALSE; | 529 inTouch->recording = SDL_FALSE; |
506 SDL_FloatPoint path[DOLLARNPOINTS]; | |
507 dollarNormalize(inTouch->dollarPath,path); | 530 dollarNormalize(inTouch->dollarPath,path); |
508 //SDL_PrintPath(path); | 531 //SDL_PrintPath(path); |
509 int index; | |
510 if(recordAll) { | 532 if(recordAll) { |
511 index = SDL_AddDollarGesture(NULL,path); | 533 index = SDL_AddDollarGesture(NULL,path); |
512 int i; | |
513 for(i = 0;i < SDL_numGestureTouches; i++) | 534 for(i = 0;i < SDL_numGestureTouches; i++) |
514 SDL_gestureTouch[i].recording = SDL_FALSE; | 535 SDL_gestureTouch[i].recording = SDL_FALSE; |
515 } | 536 } |
516 else { | 537 else { |
517 index = SDL_AddDollarGesture(inTouch,path); | 538 index = SDL_AddDollarGesture(inTouch,path); |
552 #ifdef ENABLE_DOLLAR | 573 #ifdef ENABLE_DOLLAR |
553 SDL_DollarPath* path = &inTouch->dollarPath; | 574 SDL_DollarPath* path = &inTouch->dollarPath; |
554 if(path->numPoints < MAXPATHSIZE) { | 575 if(path->numPoints < MAXPATHSIZE) { |
555 path->p[path->numPoints].x = inTouch->centroid.x; | 576 path->p[path->numPoints].x = inTouch->centroid.x; |
556 path->p[path->numPoints].y = inTouch->centroid.y; | 577 path->p[path->numPoints].y = inTouch->centroid.y; |
557 float pathDx = | 578 pathDx = |
558 (path->p[path->numPoints].x-path->p[path->numPoints-1].x); | 579 (path->p[path->numPoints].x-path->p[path->numPoints-1].x); |
559 float pathDy = | 580 pathDy = |
560 (path->p[path->numPoints].y-path->p[path->numPoints-1].y); | 581 (path->p[path->numPoints].y-path->p[path->numPoints-1].y); |
561 path->length += sqrt(pathDx*pathDx + pathDy*pathDy); | 582 path->length += (float)sqrt(pathDx*pathDx + pathDy*pathDy); |
562 path->numPoints++; | 583 path->numPoints++; |
563 } | 584 } |
564 #endif | 585 #endif |
565 SDL_FloatPoint lastP; | |
566 lastP.x = x - dx; | 586 lastP.x = x - dx; |
567 lastP.y = y - dy; | 587 lastP.y = y - dy; |
568 SDL_FloatPoint lastCentroid; | |
569 lastCentroid = inTouch->centroid; | 588 lastCentroid = inTouch->centroid; |
570 | 589 |
571 inTouch->centroid.x += dx/inTouch->numDownFingers; | 590 inTouch->centroid.x += dx/inTouch->numDownFingers; |
572 inTouch->centroid.y += dy/inTouch->numDownFingers; | 591 inTouch->centroid.y += dy/inTouch->numDownFingers; |
573 //printf("Centrid : (%f,%f)\n",inTouch->centroid.x,inTouch->centroid.y); | 592 //printf("Centrid : (%f,%f)\n",inTouch->centroid.x,inTouch->centroid.y); |
575 SDL_FloatPoint lv; //Vector from centroid to last x,y position | 594 SDL_FloatPoint lv; //Vector from centroid to last x,y position |
576 SDL_FloatPoint v; //Vector from centroid to current x,y position | 595 SDL_FloatPoint v; //Vector from centroid to current x,y position |
577 //lv = inTouch->gestureLast[j].cv; | 596 //lv = inTouch->gestureLast[j].cv; |
578 lv.x = lastP.x - lastCentroid.x; | 597 lv.x = lastP.x - lastCentroid.x; |
579 lv.y = lastP.y - lastCentroid.y; | 598 lv.y = lastP.y - lastCentroid.y; |
580 float lDist = sqrt(lv.x*lv.x + lv.y*lv.y); | 599 lDist = (float)sqrt(lv.x*lv.x + lv.y*lv.y); |
581 //printf("lDist = %f\n",lDist); | 600 //printf("lDist = %f\n",lDist); |
582 v.x = x - inTouch->centroid.x; | 601 v.x = x - inTouch->centroid.x; |
583 v.y = y - inTouch->centroid.y; | 602 v.y = y - inTouch->centroid.y; |
584 //inTouch->gestureLast[j].cv = v; | 603 //inTouch->gestureLast[j].cv = v; |
585 float Dist = sqrt(v.x*v.x+v.y*v.y); | 604 Dist = (float)sqrt(v.x*v.x+v.y*v.y); |
586 // cos(dTheta) = (v . lv)/(|v| * |lv|) | 605 // cos(dTheta) = (v . lv)/(|v| * |lv|) |
587 | 606 |
588 //Normalize Vectors to simplify angle calculation | 607 //Normalize Vectors to simplify angle calculation |
589 lv.x/=lDist; | 608 lv.x/=lDist; |
590 lv.y/=lDist; | 609 lv.y/=lDist; |
591 v.x/=Dist; | 610 v.x/=Dist; |
592 v.y/=Dist; | 611 v.y/=Dist; |
593 float dtheta = atan2(lv.x*v.y - lv.y*v.x,lv.x*v.x + lv.y*v.y); | 612 dtheta = (float)atan2(lv.x*v.y - lv.y*v.x,lv.x*v.x + lv.y*v.y); |
594 | 613 |
595 float dDist = (Dist - lDist); | 614 dDist = (Dist - lDist); |
596 if(lDist == 0) {dDist = 0;dtheta = 0;} //To avoid impossible values | 615 if(lDist == 0) {dDist = 0;dtheta = 0;} //To avoid impossible values |
597 | 616 |
598 //inTouch->gestureLast[j].dDist = dDist; | 617 //inTouch->gestureLast[j].dDist = dDist; |
599 //inTouch->gestureLast[j].dtheta = dtheta; | 618 //inTouch->gestureLast[j].dtheta = dtheta; |
600 | 619 |