Revision 1043
max_objs is not an argument of load_world. Remove starts_with
branches/simulator/projects/simulator/simulator/core/world.c | ||
---|---|---|
59 | 59 |
point_t p1 = ray->p; |
60 | 60 |
point_t p2 = {ray->p.x+cos(ray->d),ray->p.y+sin(ray->d)}; |
61 | 61 |
|
62 |
#if DEBUG |
|
62 | 63 |
printf("(%g,%g) --> (%g,%g) with (%g,%g) --> (%g,%g)\n",p1.x,p1.y,p2.x,p2.y,p3.x,p3.y,p4.x,p4.y); |
64 |
#endif |
|
63 | 65 |
denom = (p2.y-p1.y)*(p4.x-p3.x)-(p2.x-p1.x)*(p4.y-p3.y); |
64 | 66 |
if (denom == 0) { |
65 | 67 |
return RAY_MISS; |
... | ... | |
80 | 82 |
xint = p3.x+ua*(p4.x-p3.x); |
81 | 83 |
yint = p3.y+ua*(p4.y-p3.y); |
82 | 84 |
dist = sqrt((xint-p1.x)*(xint-p1.x)+(yint-p1.y)*(yint-p1.y)); |
85 |
#if DEBUG |
|
83 | 86 |
printf("Got: %g\n", dist); |
87 |
#endif |
|
84 | 88 |
return dist; |
85 | 89 |
|
86 | 90 |
} |
... | ... | |
166 | 170 |
p->pts = malloc((argc) * sizeof(point_t)); |
167 | 171 |
p->type = poly_type; |
168 | 172 |
|
173 |
double *pts = va_arg(ap, double*); |
|
174 |
|
|
169 | 175 |
for(i=0;i < argc; i++) { |
170 |
p->pts[i].x = va_arg(ap, double);
|
|
171 |
p->pts[i].y = va_arg(ap, double);
|
|
176 |
p->pts[i].x = pts[2*i];
|
|
177 |
p->pts[i].y = pts[2*i+1];
|
|
172 | 178 |
} |
173 | 179 |
obj->id = ID_POLY; |
174 | 180 |
obj->bbox = NULL; |
... | ... | |
243 | 249 |
} |
244 | 250 |
} |
245 | 251 |
|
246 |
|
|
247 |
int starts_with (const char* line, const char* word) { |
|
248 |
do { |
|
249 |
line++; |
|
250 |
word++; |
|
251 |
}while (*line == *word); |
|
252 |
if (*word == '\n' || *word == '\0') return 0; |
|
253 |
return 1; |
|
254 |
} |
|
255 |
|
|
256 | 252 |
int load_object (const char* line) { |
257 | 253 |
char buf[BUF_SIZE]; |
258 | 254 |
int i = 0; |
259 | 255 |
int j; |
260 | 256 |
int id; |
261 |
if (starts_with(line,"POLYGON") == 0){
|
|
257 |
if (strstr(line,"POLYGON") == line){
|
|
262 | 258 |
int num; |
263 | 259 |
int type; |
264 | 260 |
double *pts; |
265 | 261 |
id = ID_POLY; |
266 |
i = strlen("POLYGON")+1;
|
|
262 |
i = strlen("POLYGON") + 1;
|
|
267 | 263 |
|
268 | 264 |
sscanf(line+i,"%s",buf); |
269 | 265 |
num = strtol(buf,NULL,0); |
270 | 266 |
i += strlen(buf) + 1; |
271 | 267 |
|
272 |
if (starts_with(line+i,"CONNECTED") == 0){
|
|
268 |
if (strstr(line+i,"CONNECTED") == line+i){
|
|
273 | 269 |
i += strlen("CONNECTED") + 1; |
274 | 270 |
type = POLY_CONNECTED; |
275 |
}else if (starts_with(line+i,"DISCONNECTED") == 0){
|
|
271 |
}else if (strstr(line+i,"DISCONNECTED") == line+i){
|
|
276 | 272 |
i += strlen("DISCONNECTED") + 1; |
277 | 273 |
type = POLY_DISCONNECTED; |
278 |
}else if (starts_with(line+i,"RECT") == 0){
|
|
274 |
}else if (strstr(line+i,"RECT") == line+i){
|
|
279 | 275 |
i += strlen("RECT") + 1; |
280 | 276 |
type = POLY_RECT; |
281 | 277 |
}else{ |
282 | 278 |
// invalid input |
283 |
fprintf(stderr,"Invalid input for POLYGON\n"); |
|
279 |
fprintf(stderr,"Invalid input for POLYGON\n");
|
|
284 | 280 |
return -1; |
285 | 281 |
} |
286 | 282 |
|
... | ... | |
291 | 287 |
for (j = 0;j < num;j++){ |
292 | 288 |
pts[2*j] = strtod(c1,&c2); |
293 | 289 |
pts[2*j+1] = strtod(c2,&c1); |
290 |
#if DEBUG |
|
294 | 291 |
printf("%lf %lf\n",pts[2*j],pts[2*j+1]); |
292 |
#endif |
|
295 | 293 |
} |
296 | 294 |
|
297 | 295 |
create(id,num,type,pts); |
298 | 296 |
free(pts); |
299 |
} |
|
297 |
}else if (strstr(line,"CIRCLE") == line){ |
|
298 |
id = ID_CIRCLE; |
|
299 |
i = strlen("CIRCLE") + 1; |
|
300 |
//TODO |
|
301 |
}else if (strstr(line,"RECTANGLE") == line){ |
|
302 |
id = ID_RECTANGLE; |
|
303 |
i = strlen("RECTANGLE") + 1; |
|
304 |
//TODO |
|
305 |
}else{ |
|
306 |
//Invalid input |
|
307 |
#if DEBUG |
|
308 |
fprintf(stderr,"Invalid object: %s",line); |
|
309 |
#endif |
|
310 |
} |
|
300 | 311 |
return 0; |
301 | 312 |
} |
302 | 313 |
|
303 |
int load_world (const char* filename) { |
|
314 |
int load_world (const char* filename,int max_objs) {
|
|
304 | 315 |
FILE* fin; |
305 | 316 |
char buf[BUF_SIZE]; |
306 | 317 |
bbox_t bbox = {0,0,0,0}; |
307 | 318 |
if ((fin = fopen(filename,"r"))==NULL){ |
308 | 319 |
// open file failed |
309 |
perror("Fail to open file"); |
|
320 |
perror("Fail to open file");
|
|
310 | 321 |
return -1; |
311 | 322 |
} |
312 | 323 |
if (fgets(buf,512,fin) != NULL){ |
313 | 324 |
sscanf(buf,"WORLD %lf %lf %lf %lf",&bbox.p1.x,&bbox.p1.y,&bbox.p2.x,&bbox.p2.y); |
314 |
init_world(MAX_OBJS, bbox);
|
|
325 |
init_world(max_objs, bbox);
|
|
315 | 326 |
while (fgets(buf,512,fin) != NULL){ |
316 | 327 |
load_object(buf); |
317 | 328 |
} |
branches/simulator/projects/simulator/simulator/core/world.h | ||
---|---|---|
32 | 32 |
(b).p2.y);} |
33 | 33 |
#define PRAY(v) {printf("(%g,%g), %g\n", (v).p.x, (v).p.y,(v).d);} |
34 | 34 |
|
35 |
#define MAX_OBJS 100 |
|
36 | 35 |
#define BUF_SIZE 512 |
37 | 36 |
|
38 | 37 |
typedef struct { |
... | ... | |
96 | 95 |
/* WORLD 0 0 512 512 |
97 | 96 |
* POLYGON 3 CONNECTED 1 2 3 4 5 6 |
98 | 97 |
*/ |
99 |
int load_world (const char* filename); |
|
98 |
int load_world (const char* filename, int max_objs);
|
|
100 | 99 |
int load_object (const char* line); |
101 |
int starts_with (const char* line, const char* word); |
|
102 | 100 |
|
103 | 101 |
#endif |
104 | 102 |
|
Also available in: Unified diff