comparison src/events/SDL_gesture.c @ 4663:56a2d70de945

Started trying to build gesture code for iPhone
author Sam Lantinga <slouken@libsdl.org>
date Thu, 15 Jul 2010 06:51:16 -0700
parents 063b9455bd1a
children c2493813a2f4
comparison
equal deleted inserted replaced
4662:3c4e0130c9b1 4663:56a2d70de945
108 hash = ((hash<<5) + hash) + points[i].y; 108 hash = ((hash<<5) + hash) + points[i].y;
109 } 109 }
110 return hash; 110 return hash;
111 } 111 }
112 112
113 int SaveTemplate(DollarTemplate *templ, FILE *fp) { 113 static int SaveTemplate(DollarTemplate *templ, FILE *fp) {
114 int i; 114 int i;
115 fprintf(fp,"%lu ",templ->hash); 115 fprintf(fp,"%lu ",templ->hash);
116 for(i = 0;i < DOLLARNPOINTS;i++) { 116 for(i = 0;i < DOLLARNPOINTS;i++) {
117 fprintf(fp,"%i %i ",(int)templ->path[i].x,(int)templ->path[i].y); 117 fprintf(fp,"%i %i ",(int)templ->path[i].x,(int)templ->path[i].y);
118 } 118 }
119 fprintf(fp,"\n"); 119 fprintf(fp,"\n");
120 return 0;
120 } 121 }
121 122
122 123
123 int SDL_SaveAllDollarTemplates(FILE *fp) { 124 int SDL_SaveAllDollarTemplates(FILE *fp) {
124 int i,j,rtrn = 0; 125 int i,j,rtrn = 0;
139 if(touch->dollarTemplate[i].hash == gestureId) { 140 if(touch->dollarTemplate[i].hash == gestureId) {
140 return SaveTemplate(&touch->dollarTemplate[i],fp); 141 return SaveTemplate(&touch->dollarTemplate[i],fp);
141 } 142 }
142 } 143 }
143 } 144 }
144 } 145 SDL_SetError("Unknown gestureId");
145 146 return -1;
146 int SDL_LoadDollarTemplates(int touchId, FILE *fp) { 147 }
147 int i,loaded = 0;
148 GestureTouch *touch = NULL;
149 if(touchId >= 0) {
150 for(i = 0;i < numGestureTouches; i++)
151 if(gestureTouch[i].id == touchId)
152 touch = &gestureTouch[i];
153 if(touch == NULL) return -1;
154 }
155
156 while(!feof(fp)) {
157 DollarTemplate templ;
158 fscanf(fp,"%lu ",&templ.hash);
159 for(i = 0;i < DOLLARNPOINTS; i++) {
160 int x,y;
161 if(fscanf(fp,"%i %i ",&x,&y) != 2) break;
162 templ.path[i].x = x;
163 templ.path[i].y = y;
164 }
165 fscanf(fp,"\n");
166
167 if(touchId >= 0) {
168 if(SDL_AddDollarGesture(touch,templ)) loaded++;
169 }
170 else {
171 for(i = 0;i < numGestureTouches; i++) {
172 if(gestureTouch[i].id == touchId) {
173 touch = &gestureTouch[i];
174 SDL_AddDollarGesture(touch,templ);
175 }
176 }
177 loaded++;
178 }
179 }
180
181 return 1;
182 }
183
184 148
185 //path is an already sampled set of points 149 //path is an already sampled set of points
186 //Returns the index of the gesture on success, or -1 150 //Returns the index of the gesture on success, or -1
187 int SDL_AddDollarGesture(GestureTouch* inTouch,Point* path) { 151 static int SDL_AddDollarGesture(GestureTouch* inTouch,Point* path) {
188 if(inTouch == NULL) { 152 if(inTouch == NULL) {
189 if(numGestureTouches == 0) return -1; 153 if(numGestureTouches == 0) return -1;
190 int i = 0; 154 int i = 0;
191 for(i = 0;i < numGestureTouches; i++) { 155 for(i = 0;i < numGestureTouches; i++) {
192 inTouch = &gestureTouch[i]; 156 inTouch = &gestureTouch[i];
208 return inTouch->numDollarTemplates - 1; 172 return inTouch->numDollarTemplates - 1;
209 } 173 }
210 return -1; 174 return -1;
211 } 175 }
212 176
213 177 int SDL_LoadDollarTemplates(int touchId, FILE *fp) {
178 int i,loaded = 0;
179 GestureTouch *touch = NULL;
180 if(touchId >= 0) {
181 for(i = 0;i < numGestureTouches; i++)
182 if(gestureTouch[i].id == touchId)
183 touch = &gestureTouch[i];
184 if(touch == NULL) return -1;
185 }
186
187 while(!feof(fp)) {
188 DollarTemplate templ;
189 fscanf(fp,"%lu ",&templ.hash);
190 for(i = 0;i < DOLLARNPOINTS; i++) {
191 int x,y;
192 if(fscanf(fp,"%i %i ",&x,&y) != 2) break;
193 templ.path[i].x = x;
194 templ.path[i].y = y;
195 }
196 fscanf(fp,"\n");
197
198 if(touchId >= 0) {
199 if(SDL_AddDollarGesture(touch,templ)) loaded++;
200 }
201 else {
202 for(i = 0;i < numGestureTouches; i++) {
203 if(gestureTouch[i].id == touchId) {
204 touch = &gestureTouch[i];
205 SDL_AddDollarGesture(touch,templ);
206 }
207 }
208 loaded++;
209 }
210 }
211
212 return 1;
213 }
214 214
215 215
216 float dollarDifference(Point* points,Point* templ,float ang) { 216 float dollarDifference(Point* points,Point* templ,float ang) {
217 // Point p[DOLLARNPOINTS]; 217 // Point p[DOLLARNPOINTS];
218 float dist = 0; 218 float dist = 0;
262 printf("Min angle (x2): %f\n",x2); 262 printf("Min angle (x2): %f\n",x2);
263 */ 263 */
264 return SDL_min(f1,f2); 264 return SDL_min(f1,f2);
265 } 265 }
266 266
267 float dollarRecognize(DollarPath path,int *bestTempl,GestureTouch* touch) {
268
269 Point points[DOLLARNPOINTS];
270 int numPoints = dollarNormalize(path,points);
271 int i;
272
273 int bestDiff = 10000;
274 *bestTempl = -1;
275 for(i = 0;i < touch->numDollarTemplates;i++) {
276 int diff = bestDollarDifference(points,touch->dollarTemplate[i].path);
277 if(diff < bestDiff) {bestDiff = diff; *bestTempl = i;}
278 }
279 return bestDiff;
280 }
281
282 //DollarPath contains raw points, plus (possibly) the calculated length 267 //DollarPath contains raw points, plus (possibly) the calculated length
283 int dollarNormalize(DollarPath path,Point *points) { 268 int dollarNormalize(DollarPath path,Point *points) {
284 int i; 269 int i;
285 //Calculate length if it hasn't already been done 270 //Calculate length if it hasn't already been done
286 if(path.length <= 0) { 271 if(path.length <= 0) {
355 for(i=0;i<numPoints;i++) { 340 for(i=0;i<numPoints;i++) {
356 points[i].x = (points[i].x - centroid.x)*DOLLARSIZE/w; 341 points[i].x = (points[i].x - centroid.x)*DOLLARSIZE/w;
357 points[i].y = (points[i].y - centroid.y)*DOLLARSIZE/h; 342 points[i].y = (points[i].y - centroid.y)*DOLLARSIZE/h;
358 } 343 }
359 return numPoints; 344 return numPoints;
345 }
346
347 float dollarRecognize(DollarPath path,int *bestTempl,GestureTouch* touch) {
348
349 Point points[DOLLARNPOINTS];
350 int numPoints = dollarNormalize(path,points);
351 int i;
352
353 int bestDiff = 10000;
354 *bestTempl = -1;
355 for(i = 0;i < touch->numDollarTemplates;i++) {
356 int diff = bestDollarDifference(points,touch->dollarTemplate[i].path);
357 if(diff < bestDiff) {bestDiff = diff; *bestTempl = i;}
358 }
359 return bestDiff;
360 } 360 }
361 361
362 int SDL_GestureAddTouch(SDL_Touch* touch) { 362 int SDL_GestureAddTouch(SDL_Touch* touch) {
363 if(numGestureTouches >= MAXTOUCHES) return -1; 363 if(numGestureTouches >= MAXTOUCHES) return -1;
364 364