Project

General

Profile

Statistics
| Revision:

root / branches / simulator / projects / simulator / simulator / core / world.c @ 1184

History | View | Annotate | Download (7.2 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 destroy(object_t *obj);
20

    
21
int init_world(int max_objs, bbox_t b)
22
{
23
    world.max_objs = max_objs;
24
    world.cur_objs = 0;
25
    world.objs = calloc(max_objs,  sizeof(object_t));
26
    world.win = b;
27

    
28
        return 0;
29
}
30

    
31
int destroy_world()
32
{
33
    int i;
34
    for (i = 0; i < world.cur_objs; i++)
35
        destroy(&world.objs[i]);
36
    free(world.objs);
37

    
38
        return 0;
39
}
40

    
41
double (*collide_func[NUM_SHAPES])(ray_t *ray, object_t *obj) =
42
    {
43
    collide_poly, collide_circle
44
    };
45
int (*create_func[NUM_SHAPES])(object_t *obj, va_list ap) =
46
{
47
    create_poly, create_circle
48
};
49
int (*destroy_func[NUM_SHAPES])(object_t *obj) = 
50
    {
51
    destroy_poly, destroy_circle
52
    };
53

    
54

    
55
double collide_circle(ray_t *ray, object_t *obj)
56
{
57
    //point_t Or, Oc, Vr;
58
        
59
 
60
        point_t Or = ray->p;
61
        point_t Oc = ((circle_t*)(obj->props))->center;
62
        //point_t Vr = ray->d;
63
        point_t Vr = {ray->p.x+cos(ray->d),ray->p.y+sin(ray->d)};
64
        
65
        
66
        if( ((2*Vr.x*(Or.x - Oc.x))*(2*Vr.x*(Or.x - Oc.x)) - 4*Vr.x*(Or.x - Oc.x)*(Or.x - Oc.x)) < 0) // Discriminant < 0 
67
        {
68
                return -1;
69
        }
70
    
71
        if( ((2*Vr.x*(Or.x - Oc.x))*(2*Vr.x*(Or.x - Oc.x)) - 4*Vr.x*(Or.x - Oc.x)*(Or.x - Oc.x)) == 0) // Discriminant = 0
72
        {
73
                return -((2*Vr.x*(Or.x - Oc.x))*(2*Vr.x*(Or.x - Oc.x)) - (4*Vr.x*(Or.x - Oc.x)*(Or.x - Oc.x)) / (2*Vr.x));
74
        }
75
        
76
        
77
        return -1;
78
}
79

    
80
double collide_seg(ray_t *ray, point_t p3, point_t p4)
81
{   
82
    double denom, nume_a, nume_b, ua, ub, xint, yint, dist;
83
    point_t p1 = ray->p;
84
    point_t p2 = {ray->p.x+cos(ray->d),ray->p.y+sin(ray->d)};
85

    
86
    //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);
87
    denom = (p2.y-p1.y)*(p4.x-p3.x)-(p2.x-p1.x)*(p4.y-p3.y);
88
    if (denom == 0) {
89
        return RAY_MISS;
90
    }
91
    nume_a = (p2.x-p1.x)*(p3.y-p1.y)-(p2.y-p1.y)*(p3.x-p1.x);
92
    nume_b = (p4.x-p3.x)*(p3.y-p1.y)-(p4.y-p3.y)*(p3.x-p1.x);
93

    
94
    ua = nume_a/denom;
95
    if (ua < 0 || ua > 1) {
96
        return RAY_MISS;
97
    }
98

    
99
    ub = nume_b/denom;
100
    if (ub < 0) {
101
        return RAY_MISS;
102
    }
103

    
104
    xint = p3.x+ua*(p4.x-p3.x);
105
    yint = p3.y+ua*(p4.y-p3.y);
106
    dist = sqrt((xint-p1.x)*(xint-p1.x)+(yint-p1.y)*(yint-p1.y));
107
    //printf("Got: %g\n", dist);
108
    return dist;
109

    
110
}
111

    
112
double collide_poly(ray_t *ray, object_t *obj)
113
{
114

    
115
    int i;
116
    double min = RAY_MISS;
117
    double x;
118
    poly_t *p = (poly_t *) obj->props;
119
    
120
    if (obj->id != ID_POLY){
121
        return -1;
122
    }
123

    
124
    for (i = 0; i < p->num_pts - 1; i++) {
125
        if ((x = collide_seg(ray, p->pts[i], p->pts[i+1])) < min){
126
            min = x;
127
        }
128
    }
129
    if (p->type == POLY_CONNECTED) {
130
        if ((x = collide_seg(ray,p->pts[i],p->pts[0])) < min)
131
            min = x;
132
    }
133
    return min;
134
}
135

    
136
double collide(ray_t *ray, object_t *obj) 
137
{
138
    return collide_func[obj->id+ID_OFFSET](ray, obj);
139
}
140

    
141
double collide_world(ray_t *ray)
142
{
143
    double min = RAY_MISS;
144
    double x;
145
    int i;
146
    for (i = 0; i < world.cur_objs; i++) {        
147
        x = collide(ray, &world.objs[i]);
148
        min = (x < min) ? x : min;
149
    }
150
    return min;
151
}
152

    
153

    
154

    
155
object_t *create(int id, ...)
156
{
157
    object_t *obj = &(world.objs[world.cur_objs++]);
158
    va_list args;
159

    
160
    va_start(args, id);
161
    if (create_func[id+ID_OFFSET](obj, args) < 0) {
162
        /* Cleanup ? */
163
    }
164
    va_end(args);
165
    return obj;
166
}
167

    
168
int destroy(object_t *obj) 
169
{
170
    if (obj->id == ID_NULL)
171
        return 0;
172
    destroy_func[obj->id+ID_OFFSET](obj);
173
    obj->id = ID_NULL;
174

    
175
    return 0;
176
}
177

    
178
/**
179
 *
180
 **/
181
int create_poly(object_t *obj, va_list ap) 
182
{
183
    int i;
184
    poly_t *p;
185

    
186
    int argc =  va_arg(ap, int);
187
    int poly_type = va_arg(ap, int);
188

    
189
    p = malloc(sizeof(poly_t));
190

    
191

    
192
    p->num_pts = argc;
193
    p->pts = malloc((argc) * sizeof(point_t));
194
    p->type = poly_type;
195

    
196
                double *pts = va_arg(ap, double*);
197

    
198
    for(i=0;i < argc; i++) {
199
                        p->pts[i].x = pts[2*i];
200
                        p->pts[i].y = pts[2*i+1];
201
    }
202
    obj->id = ID_POLY;
203
    obj->bbox = NULL;
204
    obj->props = p;
205
    return 1;
206
}
207

    
208
/* ap = x, y, radius */
209
int create_circle(object_t *obj, va_list ap)
210
{
211
        circle_t *c;
212

    
213
        c = malloc(sizeof(circle_t));
214
        
215
        c->center.x = va_arg(ap, double);
216
        c->center.y = va_arg(ap, double);
217
        c->radius = va_arg(ap, double);
218

    
219
        obj->id = ID_CIRCLE;
220
        obj->bbox = NULL;
221
        obj->props = c;
222
        return 0;        
223
}
224

    
225
int create_rect(object_t *obj, va_list ap)
226
{
227
    /*int i;
228
    poly_t *p;
229
    int argc = va_arg(ap, int);*/
230

    
231
        return 0;
232
}
233

    
234
int destroy_poly (object_t *obj)
235
{
236
    poly_t *p;
237
    p =  (poly_t *)obj->props;
238
    if (p != NULL) free(p->pts);
239
    free(p);
240
    return 1;
241
}
242

    
243
int destroy_circle (object_t *obj)
244
{
245
        free(obj->props);
246
        free(obj);
247
        return 0;
248
}
249

    
250
void print_world(void)
251
{
252
    int i;
253
    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);
254
    for (i = 0; i < world.cur_objs; i++) {
255
        print_object(&world.objs[i]);
256
    }
257
}
258
void print_object(object_t *obj)
259
{
260
    int i;
261
    poly_t *p;
262
    switch (obj->id) {
263
        case ID_NULL:
264
            printf("\tNULL\n");
265
        case ID_POLY:
266
            p = (poly_t *) obj->props;
267
            printf("\tPOLYGON (%d points, %s) { ", p->num_pts, p->type?"connected" : "disconnected");
268
            for (i = 0; i < p->num_pts; i++){
269
                printf("(%g, %g) ", p->pts[i].x, p->pts[i].y);
270
            }
271
            printf("}\n");
272
            break;
273
        default:
274
            break;
275
                    
276
    }
277
}
278

    
279

    
280
int load_object (const char* line) {
281
  char buf[BUF_SIZE];
282
  int i = 0;
283
  int j;
284
  int id;
285
  if (strstr(line,"POLYGON") == line){
286
    int num;
287
    int type;
288
    double *pts;
289
    id = ID_POLY;
290
    i = strlen("POLYGON") + 1;
291

    
292
    sscanf(line+i,"%s",buf);
293
    num = strtol(buf,NULL,0);
294
    i += strlen(buf) + 1;
295

    
296
    if (strstr(line+i,"CONNECTED") == line+i){
297
      i += strlen("CONNECTED") + 1;
298
      type = POLY_CONNECTED;
299
    }else if (strstr(line+i,"DISCONNECTED") == line+i){
300
      i += strlen("DISCONNECTED") + 1;
301
      type = POLY_DISCONNECTED;
302
    }else if (strstr(line+i,"RECT") == line+i){
303
      i += strlen("RECT") + 1;
304
      type = POLY_RECT;
305
    }else{
306
      // invalid input
307
                        fprintf(stderr,"Invalid input for POLYGON\n");
308
      return -1;
309
    }
310

    
311
    char *c1,*c2;
312
    c1 = (char*)(line+i);
313
    c2 = (char*)(line+i);
314
    pts = (double*) malloc(sizeof(double)*num*2);
315
    for (j = 0;j < num;j++){
316
      pts[2*j] = strtod(c1,&c2);
317
      pts[2*j+1] = strtod(c2,&c1);
318
#if DEBUG
319
      printf("%lf %lf\n",pts[2*j],pts[2*j+1]);
320
#endif
321
    }
322

    
323
    create(id,num,type,pts);
324
    free(pts);
325
  }else if (strstr(line,"CIRCLE") == line){
326
                id = ID_CIRCLE;
327
                i = strlen("CIRCLE") + 1;
328
                double x,y,r;
329
                sscanf(line + i, "%lf %lf %lf", &x, &y, &r);
330
                create(id,x,y,r);
331
        }else if (strstr(line,"RECTANGLE") == line){
332
                id = ID_RECTANGLE;
333
                i = strlen("RECTANGLE") + 1;
334
                //TODO
335
        }else{
336
                //Invalid input
337
#if DEBUG
338
                fprintf(stderr,"Invalid object: %s",line);
339
#endif
340
        }
341
  return 0;
342
}
343

    
344
int load_world (const char* filename,int max_objs) {
345
  FILE* fin;
346
  char buf[BUF_SIZE];
347
  bbox_t bbox;
348
  if ((fin = fopen(filename,"r"))==NULL){
349
    // open file failed
350
                perror("Fail to open file");
351
    return -1;
352
  }
353
  if (fgets(buf,512,fin) != NULL){
354
    sscanf(buf,"WORLD %lf %lf %lf %lf",&bbox.p1.x,&bbox.p1.y,&bbox.p2.x,&bbox.p2.y);
355
    init_world(max_objs, bbox);
356
    while (fgets(buf,512,fin) != NULL){
357
      load_object(buf);
358
    }
359
  }
360
  fclose(fin);
361
  return 0;
362
}