root / branches / simulator / projects / simulator / simulator / core / world.c @ 1043
History | View | Annotate | Download (6.64 KB)
1 |
/**
|
---|---|
2 |
* @file world.c
|
3 |
* @author Colony Project
|
4 |
* @brief Simulator world code
|
5 |
*
|
6 |
* This is the world.
|
7 |
**/
|
8 |
|
9 |
#include <stdio.h> |
10 |
#include <stdlib.h> |
11 |
#include <math.h> |
12 |
#include <stdarg.h> |
13 |
#include "world.h" |
14 |
#include "robot.h" |
15 |
#include "robot_shared.h" |
16 |
|
17 |
world_t world; |
18 |
|
19 |
int init_world(int max_objs, bbox_t b) |
20 |
{ |
21 |
world.max_objs = max_objs; |
22 |
world.cur_objs = 0;
|
23 |
world.objs = calloc(max_objs, sizeof(object_t));
|
24 |
world.win = b; |
25 |
//PBBOX(b);
|
26 |
} |
27 |
int destroy_world()
|
28 |
{ |
29 |
int i;
|
30 |
for (i = 0; i < world.cur_objs; i++) { |
31 |
destroy(&world.objs[i]); |
32 |
} |
33 |
free(world.objs); |
34 |
} |
35 |
|
36 |
double (*collide_func[NUM_SHAPES])(ray_t *ray, object_t *obj) =
|
37 |
{ |
38 |
collide_poly |
39 |
}; |
40 |
int (*create_func[NUM_SHAPES])(object_t *obj, va_list ap) =
|
41 |
{ |
42 |
create_poly |
43 |
}; |
44 |
int (*destroy_func[NUM_SHAPES])(object_t *obj) =
|
45 |
{ |
46 |
destroy_poly |
47 |
}; |
48 |
|
49 |
|
50 |
|
51 |
double collide_circle(ray_t *ray, object_t *obj)
|
52 |
{ |
53 |
return -1; |
54 |
} |
55 |
|
56 |
double collide_seg(ray_t *ray, point_t p3, point_t p4)
|
57 |
{ |
58 |
double denom, nume_a, nume_b, ua, ub, xint, yint, dist;
|
59 |
point_t p1 = ray->p; |
60 |
point_t p2 = {ray->p.x+cos(ray->d),ray->p.y+sin(ray->d)}; |
61 |
|
62 |
#if DEBUG
|
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
|
65 |
denom = (p2.y-p1.y)*(p4.x-p3.x)-(p2.x-p1.x)*(p4.y-p3.y); |
66 |
if (denom == 0) { |
67 |
return RAY_MISS;
|
68 |
} |
69 |
nume_a = (p2.x-p1.x)*(p3.y-p1.y)-(p2.y-p1.y)*(p3.x-p1.x); |
70 |
nume_b = (p4.x-p3.x)*(p3.y-p1.y)-(p4.y-p3.y)*(p3.x-p1.x); |
71 |
|
72 |
ua = nume_a/denom; |
73 |
if (ua < 0 || ua > 1) { |
74 |
return RAY_MISS;
|
75 |
} |
76 |
|
77 |
ub = nume_b/denom; |
78 |
if (ub < 0) { |
79 |
return RAY_MISS;
|
80 |
} |
81 |
|
82 |
xint = p3.x+ua*(p4.x-p3.x); |
83 |
yint = p3.y+ua*(p4.y-p3.y); |
84 |
dist = sqrt((xint-p1.x)*(xint-p1.x)+(yint-p1.y)*(yint-p1.y)); |
85 |
#if DEBUG
|
86 |
printf("Got: %g\n", dist);
|
87 |
#endif
|
88 |
return dist;
|
89 |
|
90 |
} |
91 |
|
92 |
double collide_poly(ray_t *ray, object_t *obj)
|
93 |
{ |
94 |
|
95 |
int i;
|
96 |
double min = RAY_MISS;
|
97 |
double x;
|
98 |
poly_t *p = (poly_t *) obj->props; |
99 |
|
100 |
if (obj->id != ID_POLY){
|
101 |
return -1; |
102 |
} |
103 |
|
104 |
for (i = 0; i < p->num_pts - 1; i++) { |
105 |
if ((x = collide_seg(ray, p->pts[i], p->pts[i+1])) < min){ |
106 |
min = x; |
107 |
} |
108 |
} |
109 |
if (p->type == POLY_CONNECTED) {
|
110 |
if ((x = collide_seg(ray,p->pts[i],p->pts[0])) < min) |
111 |
min = x; |
112 |
} |
113 |
return min;
|
114 |
} |
115 |
|
116 |
double collide(ray_t *ray, object_t *obj)
|
117 |
{ |
118 |
return collide_func[obj->id+ID_OFFSET](ray, obj);
|
119 |
} |
120 |
|
121 |
double collide_world(ray_t *ray)
|
122 |
{ |
123 |
double min = RAY_MISS;
|
124 |
int i, x;
|
125 |
for (i = 0; i < world.cur_objs; i++) { |
126 |
x = collide(ray, &world.objs[i]); |
127 |
min = (x < min) ? x : min; |
128 |
} |
129 |
return min;
|
130 |
} |
131 |
|
132 |
|
133 |
|
134 |
object_t *create(int id, ...)
|
135 |
{ |
136 |
object_t *obj = &(world.objs[world.cur_objs++]); |
137 |
va_list args; |
138 |
|
139 |
va_start(args, id); |
140 |
if (create_func[id+ID_OFFSET](obj, args) < 0) { |
141 |
/* Cleanup ? */
|
142 |
} |
143 |
va_end(args); |
144 |
return obj;
|
145 |
} |
146 |
|
147 |
int destroy(object_t *obj)
|
148 |
{ |
149 |
if (obj->id == ID_NULL)
|
150 |
return 0; |
151 |
destroy_func[obj->id+ID_OFFSET](obj); |
152 |
obj->id = ID_NULL; |
153 |
} |
154 |
|
155 |
/**
|
156 |
*
|
157 |
**/
|
158 |
int create_poly(object_t *obj, va_list ap)
|
159 |
{ |
160 |
int i;
|
161 |
poly_t *p; |
162 |
|
163 |
int argc = va_arg(ap, int); |
164 |
int poly_type = va_arg(ap, int); |
165 |
|
166 |
p = malloc(sizeof(poly_t));
|
167 |
|
168 |
|
169 |
p->num_pts = argc; |
170 |
p->pts = malloc((argc) * sizeof(point_t));
|
171 |
p->type = poly_type; |
172 |
|
173 |
double *pts = va_arg(ap, double*); |
174 |
|
175 |
for(i=0;i < argc; i++) { |
176 |
p->pts[i].x = pts[2*i];
|
177 |
p->pts[i].y = pts[2*i+1]; |
178 |
} |
179 |
obj->id = ID_POLY; |
180 |
obj->bbox = NULL;
|
181 |
obj->props = p; |
182 |
return 1; |
183 |
} |
184 |
|
185 |
int create_rect(object_t *obj, va_list ap)
|
186 |
{ |
187 |
int i;
|
188 |
poly_t *p; |
189 |
int argc = va_arg(ap, int); |
190 |
} |
191 |
|
192 |
int destroy_poly (object_t *obj)
|
193 |
{ |
194 |
poly_t *p; |
195 |
p = (poly_t *)obj->props; |
196 |
if (p != NULL) free(p->pts); |
197 |
free(p); |
198 |
return 1; |
199 |
} |
200 |
|
201 |
void print_world(void) |
202 |
{ |
203 |
int i;
|
204 |
printf("WORLD: %d object(s) in (%g,%g)->(%g,%g)\n", world.cur_objs, world.win.p1.x,world.win.p1.y,world.win.p2.x,world.win.p2.y);
|
205 |
for (i = 0; i < world.cur_objs; i++) { |
206 |
print_object(&world.objs[i]); |
207 |
} |
208 |
} |
209 |
void print_object(object_t *obj)
|
210 |
{ |
211 |
int i;
|
212 |
poly_t *p; |
213 |
switch (obj->id) {
|
214 |
case ID_NULL:
|
215 |
printf("\tNULL\n");
|
216 |
case ID_POLY:
|
217 |
p = (poly_t *) obj->props; |
218 |
printf("\tPOLYGON (%d points, %s) { ", p->num_pts, p->type?"connected" : "disconnected"); |
219 |
for (i = 0; i < p->num_pts; i++){ |
220 |
printf("(%g, %g) ", p->pts[i].x, p->pts[i].y);
|
221 |
} |
222 |
printf("}\n");
|
223 |
break;
|
224 |
default:
|
225 |
break;
|
226 |
|
227 |
} |
228 |
} |
229 |
|
230 |
|
231 |
/*
|
232 |
* XXX: This shouldn't be here. Where should it be?
|
233 |
*/
|
234 |
/* These need to be measured... */
|
235 |
double rf_thetas[5] = {0.0, 2*M_PI/5, 4*M_PI/5, 6*M_PI/5, 8*M_PI/5}; |
236 |
void update_rangefinders(Robot *bot)
|
237 |
{ |
238 |
//RangeFinder r = bot->shared->ranges;
|
239 |
//ray_t rf = {bot->pose.x, bot->pose.y};
|
240 |
ray_t rf = {0,0,0}; |
241 |
int ir;
|
242 |
double x;
|
243 |
for (ir = 0; ir < 5; ir++) |
244 |
{ |
245 |
rf.d = rf_thetas[ir]; |
246 |
x = collide_poly(&rf, world.objs); |
247 |
printf("[%d] = %g\n", ir, x);
|
248 |
//bot->shared->ranges.d[ir] = collide_world(&rf);
|
249 |
} |
250 |
} |
251 |
|
252 |
int load_object (const char* line) { |
253 |
char buf[BUF_SIZE];
|
254 |
int i = 0; |
255 |
int j;
|
256 |
int id;
|
257 |
if (strstr(line,"POLYGON") == line){ |
258 |
int num;
|
259 |
int type;
|
260 |
double *pts;
|
261 |
id = ID_POLY; |
262 |
i = strlen("POLYGON") + 1; |
263 |
|
264 |
sscanf(line+i,"%s",buf);
|
265 |
num = strtol(buf,NULL,0); |
266 |
i += strlen(buf) + 1;
|
267 |
|
268 |
if (strstr(line+i,"CONNECTED") == line+i){ |
269 |
i += strlen("CONNECTED") + 1; |
270 |
type = POLY_CONNECTED; |
271 |
}else if (strstr(line+i,"DISCONNECTED") == line+i){ |
272 |
i += strlen("DISCONNECTED") + 1; |
273 |
type = POLY_DISCONNECTED; |
274 |
}else if (strstr(line+i,"RECT") == line+i){ |
275 |
i += strlen("RECT") + 1; |
276 |
type = POLY_RECT; |
277 |
}else{
|
278 |
// invalid input
|
279 |
fprintf(stderr,"Invalid input for POLYGON\n");
|
280 |
return -1; |
281 |
} |
282 |
|
283 |
char *c1,*c2;
|
284 |
c1 = (char*)(line+i);
|
285 |
c2 = (char*)(line+i);
|
286 |
pts = (double*) malloc(sizeof(double)*num*2); |
287 |
for (j = 0;j < num;j++){ |
288 |
pts[2*j] = strtod(c1,&c2);
|
289 |
pts[2*j+1] = strtod(c2,&c1); |
290 |
#if DEBUG
|
291 |
printf("%lf %lf\n",pts[2*j],pts[2*j+1]); |
292 |
#endif
|
293 |
} |
294 |
|
295 |
create(id,num,type,pts); |
296 |
free(pts); |
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 |
} |
311 |
return 0; |
312 |
} |
313 |
|
314 |
int load_world (const char* filename,int max_objs) { |
315 |
FILE* fin; |
316 |
char buf[BUF_SIZE];
|
317 |
bbox_t bbox = {0,0,0,0}; |
318 |
if ((fin = fopen(filename,"r"))==NULL){ |
319 |
// open file failed
|
320 |
perror("Fail to open file");
|
321 |
return -1; |
322 |
} |
323 |
if (fgets(buf,512,fin) != NULL){ |
324 |
sscanf(buf,"WORLD %lf %lf %lf %lf",&bbox.p1.x,&bbox.p1.y,&bbox.p2.x,&bbox.p2.y);
|
325 |
init_world(max_objs, bbox); |
326 |
while (fgets(buf,512,fin) != NULL){ |
327 |
load_object(buf); |
328 |
} |
329 |
} |
330 |
fclose(fin); |
331 |
return 0; |
332 |
} |
333 |
|