comparison src/events/SDL_gesture.c @ 4665:c2493813a2f4

Merged changes
author Jim Grandpre <jim.tla@gmail.com>
date Fri, 16 Jul 2010 20:53:44 -0400
parents 317a151b79ad 56a2d70de945
children f8431f66613d
comparison
equal deleted inserted replaced
4664:317a151b79ad 4665:c2493813a2f4
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, SDL_RWops * src) { 113
114 static int SaveTemplate(DollarTemplate *templ, SDL_RWops * src) {
114 if(src == NULL) return 0; 115 if(src == NULL) return 0;
116
115 int i; 117 int i;
116 118
117 //No Longer storing the Hash, rehash on load 119 //No Longer storing the Hash, rehash on load
118 //fprintf(fp,"%lu ",templ->hash); 120 //fprintf(fp,"%lu ",templ->hash);
119 //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;
121 /* 123 /*
122 for(i = 0;i < DOLLARNPOINTS;i++) { 124 for(i = 0;i < DOLLARNPOINTS;i++) {
123 fprintf(fp,"%i %i ",(int)templ->path[i].x,(int)templ->path[i].y); 125 fprintf(fp,"%i %i ",(int)templ->path[i].x,(int)templ->path[i].y);
124 } 126 }
125 fprintf(fp,"\n"); 127 fprintf(fp,"\n");
128
126 */ 129 */
127 if(SDL_RWwrite(src,templ->path,sizeof(templ->path[0]),DOLLARNPOINTS) != DOLLARNPOINTS) return 0; 130 if(SDL_RWwrite(src,templ->path,sizeof(templ->path[0]),DOLLARNPOINTS) != DOLLARNPOINTS) return 0;
128 return 1; 131 return 1;
129 } 132 }
130 133
148 if(touch->dollarTemplate[i].hash == gestureId) { 151 if(touch->dollarTemplate[i].hash == gestureId) {
149 return SaveTemplate(&touch->dollarTemplate[i],src); 152 return SaveTemplate(&touch->dollarTemplate[i],src);
150 } 153 }
151 } 154 }
152 } 155 }
153 } 156 SDL_SetError("Unknown gestureId");
154 157 return -1;
155 int SDL_LoadDollarTemplates(int touchId, SDL_RWops *src) { 158 }
156 if(src == NULL) return 0;
157 int i,loaded = 0;
158 GestureTouch *touch = NULL;
159 if(touchId >= 0) {
160 for(i = 0;i < numGestureTouches; i++)
161 if(gestureTouch[i].id == touchId)
162 touch = &gestureTouch[i];
163 if(touch == NULL) return -1;
164 }
165
166 while(1) {
167 DollarTemplate templ;
168 //fscanf(fp,"%lu ",&templ.hash);
169 /*
170 for(i = 0;i < DOLLARNPOINTS; i++) {
171 int x,y;
172 if(fscanf(fp,"%i %i ",&x,&y) != 2) break;
173 templ.path[i].x = x;
174 templ.path[i].y = y;
175 }
176 fscanf(fp,"\n");
177 */
178 if(SDL_RWread(src,templ.path,sizeof(templ.path[0]),DOLLARNPOINTS) < DOLLARNPOINTS) break;
179
180 if(touchId >= 0) {
181 printf("Adding loaded gesture to 1 touch\n");
182 if(SDL_AddDollarGesture(touch,templ.path)) loaded++;
183 }
184 else {
185 printf("Adding to: %i touches\n",numGestureTouches);
186 for(i = 0;i < numGestureTouches; i++) {
187 touch = &gestureTouch[i];
188 printf("Adding loaded gesture to + touches\n");
189 //TODO: What if this fails?
190 SDL_AddDollarGesture(touch,templ.path);
191 }
192 loaded++;
193 }
194 }
195
196 return loaded;
197 }
198
199 159
200 //path is an already sampled set of points 160 //path is an already sampled set of points
201 //Returns the index of the gesture on success, or -1 161 //Returns the index of the gesture on success, or -1
202 int SDL_AddDollarGesture(GestureTouch* inTouch,Point* path) { 162 static int SDL_AddDollarGesture(GestureTouch* inTouch,Point* path) {
203 if(inTouch == NULL) { 163 if(inTouch == NULL) {
204 if(numGestureTouches == 0) return -1; 164 if(numGestureTouches == 0) return -1;
205 int i = 0; 165 int i = 0;
206 for(i = 0;i < numGestureTouches; i++) { 166 for(i = 0;i < numGestureTouches; i++) {
207 inTouch = &gestureTouch[i]; 167 inTouch = &gestureTouch[i];
223 return inTouch->numDollarTemplates - 1; 183 return inTouch->numDollarTemplates - 1;
224 } 184 }
225 return -1; 185 return -1;
226 } 186 }
227 187
228 188 int SDL_LoadDollarTemplates(int touchId, SDL_RWops *src) {
189 if(src == NULL) return 0;
190 int i,loaded = 0;
191 GestureTouch *touch = NULL;
192 if(touchId >= 0) {
193 for(i = 0;i < numGestureTouches; i++)
194 if(gestureTouch[i].id == touchId)
195 touch = &gestureTouch[i];
196 if(touch == NULL) return -1;
197 }
198
199 while(1) {
200 DollarTemplate templ;
201 //fscanf(fp,"%lu ",&templ.hash);
202 /*
203 for(i = 0;i < DOLLARNPOINTS; i++) {
204 int x,y;
205 if(fscanf(fp,"%i %i ",&x,&y) != 2) break;
206 templ.path[i].x = x;
207 templ.path[i].y = y;
208 }
209 fscanf(fp,"\n");
210 */
211 if(SDL_RWread(src,templ.path,sizeof(templ.path[0]),DOLLARNPOINTS) < DOLLARNPOINTS) break;
212
213 if(touchId >= 0) {
214 printf("Adding loaded gesture to 1 touch\n");
215 if(SDL_AddDollarGesture(touch,templ.path)) loaded++;
216 }
217 else {
218 printf("Adding to: %i touches\n",numGestureTouches);
219 for(i = 0;i < numGestureTouches; i++) {
220 touch = &gestureTouch[i];
221 printf("Adding loaded gesture to + touches\n");
222 //TODO: What if this fails?
223 SDL_AddDollarGesture(touch,templ.path);
224 }
225 loaded++;
226 }
227 }
228
229 return loaded;
230 }
229 231
230 232
231 float dollarDifference(Point* points,Point* templ,float ang) { 233 float dollarDifference(Point* points,Point* templ,float ang) {
232 // Point p[DOLLARNPOINTS]; 234 // Point p[DOLLARNPOINTS];
233 float dist = 0; 235 float dist = 0;
277 printf("Min angle (x2): %f\n",x2); 279 printf("Min angle (x2): %f\n",x2);
278 */ 280 */
279 return SDL_min(f1,f2); 281 return SDL_min(f1,f2);
280 } 282 }
281 283
282 float dollarRecognize(DollarPath path,int *bestTempl,GestureTouch* touch) {
283
284 Point points[DOLLARNPOINTS];
285 int numPoints = dollarNormalize(path,points);
286 int i;
287
288 int bestDiff = 10000;
289 *bestTempl = -1;
290 for(i = 0;i < touch->numDollarTemplates;i++) {
291 int diff = bestDollarDifference(points,touch->dollarTemplate[i].path);
292 if(diff < bestDiff) {bestDiff = diff; *bestTempl = i;}
293 }
294 return bestDiff;
295 }
296
297 //DollarPath contains raw points, plus (possibly) the calculated length 284 //DollarPath contains raw points, plus (possibly) the calculated length
298 int dollarNormalize(DollarPath path,Point *points) { 285 int dollarNormalize(DollarPath path,Point *points) {
299 int i; 286 int i;
300 //Calculate length if it hasn't already been done 287 //Calculate length if it hasn't already been done
301 if(path.length <= 0) { 288 if(path.length <= 0) {
370 for(i=0;i<numPoints;i++) { 357 for(i=0;i<numPoints;i++) {
371 points[i].x = (points[i].x - centroid.x)*DOLLARSIZE/w; 358 points[i].x = (points[i].x - centroid.x)*DOLLARSIZE/w;
372 points[i].y = (points[i].y - centroid.y)*DOLLARSIZE/h; 359 points[i].y = (points[i].y - centroid.y)*DOLLARSIZE/h;
373 } 360 }
374 return numPoints; 361 return numPoints;
362 }
363
364 float dollarRecognize(DollarPath path,int *bestTempl,GestureTouch* touch) {
365
366 Point points[DOLLARNPOINTS];
367 int numPoints = dollarNormalize(path,points);
368 int i;
369
370 int bestDiff = 10000;
371 *bestTempl = -1;
372 for(i = 0;i < touch->numDollarTemplates;i++) {
373 int diff = bestDollarDifference(points,touch->dollarTemplate[i].path);
374 if(diff < bestDiff) {bestDiff = diff; *bestTempl = i;}
375 }
376 return bestDiff;
375 } 377 }
376 378
377 int SDL_GestureAddTouch(SDL_Touch* touch) { 379 int SDL_GestureAddTouch(SDL_Touch* touch) {
378 if(numGestureTouches >= MAXTOUCHES) return -1; 380 if(numGestureTouches >= MAXTOUCHES) return -1;
379 381