-
Notifications
You must be signed in to change notification settings - Fork 14
/
universe.cc
429 lines (341 loc) · 10.2 KB
/
universe.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
/****
universe.cc
version 3
Richard Vaughan
****/
#include <assert.h>
#include <unistd.h>
#include <sys/time.h>
#include "universe.h"
using namespace Uni;
const char* PROGNAME = "universe";
#if GRAPHICS
#include <GLUT/glut.h> // OS X users need <glut/glut.h> instead
#endif
namespace Uni {
bool need_redraw( true );
double worldsize(1.0);
std::vector<Robot> population( 100 );
uint64_t updates(0);
uint64_t updates_max( 0.0 );
bool paused( false );
int winsize( 600 );
int displaylist(0);
bool show_data( true );
unsigned int sleep_msec( 50 );
double lastseconds;
// Robot static members
unsigned int Robot::pixel_count( 8);
double Robot::range( 0.1 );
double Robot::fov( dtor(270.0) );
}
char usage[] = "Universe understands these command line arguments:\n"
" -? : Prints this helpful message.\n"
" -c <int> : sets the number of pixels in the robots' sensor.\n"
" -d Disables drawing the sensor field of view. Speeds things up a bit.\n"
" -f <float> : sets the sensor field of view angle in degrees.\n"
" -p <int> : set the size of the robot population.\n"
" -q : disables chatty status output (quiet mode).\n"
" -r <float> : sets the sensor field of view range.\n"
" -s <float> : sets the side length of the (square) world.\n"
" -u <int> : sets the number of updates to run before quitting.\n"
" -w <int> : sets the initial size of the window, in pixels.\n"
" -z <int> : sets the number of milliseconds to sleep between updates.\n";
#if GRAPHICS
// GLUT callback functions ---------------------------------------------------
// update the world - this is called whenever GLUT runs out of events
// to process
static void idle_func( void )
{
Uni::UpdateAll();
// possibly snooze to save CPU and slow things down
if( Uni::sleep_msec > 0 )
usleep( Uni::sleep_msec * 1e3 );
}
static void timer_func( int dummy )
{
glutPostRedisplay(); // force redraw
}
// draw the world - this is called whenever the window needs redrawn
static void display_func( void )
{
if( Uni::need_redraw )
{
Uni::need_redraw = false;
glClear( GL_COLOR_BUFFER_BIT );
FOR_EACH( r, population )
r->Draw();
glutSwapBuffers();
glFlush();
}
// cause this run again in about 50 msec
glutTimerFunc( 50, timer_func, 0 );
}
static void mouse_func(int button, int state, int x, int y)
{
if( (button == GLUT_LEFT_BUTTON) && (state == GLUT_DOWN ) )
{
Uni::paused = !Uni::paused;
}
}
#endif // GRAPHICS
Robot::Robot()
: pose(),
speed(),
color(),
pixels( pixel_count ),
callback(NULL),
callback_data(NULL)
{
// until C++ supports array literals in the initialization list, we're forced to do this
memset( pose, 0, sizeof(pose));
memset( speed, 0, sizeof(speed));
color[0] = 128;
color[1] = 0;
color[2] = 0;
}
void Uni::Init( int argc, char** argv )
{
// seed the random number generator with the current time
// srand48(time(NULL));
// seed the random number generator with a constant for repeatability
srand48(0);
bool quiet = false; // controls output verbosity
int population_size = 100;
// parse arguments to configure Robot static members
// opterr = 0; // supress errors about bad options
int c;
while( ( c = getopt( argc, argv, ":?dqp:s:f:r:c:u:z:w:")) != -1 )
switch( c )
{
case 'p':
population_size = atoi( optarg );
if( ! quiet ) fprintf( stderr, "[Uni] population_size: %d\n", population_size );
population.resize( population_size );
break;
case 's':
worldsize = atof( optarg );
if( ! quiet ) fprintf( stderr, "[Uni] worldsize: %.2f\n", worldsize );
break;
case 'f':
Robot::fov = dtor(atof( optarg )); // degrees to radians
if( ! quiet ) fprintf( stderr, "[Uni] fov: %.2f\n", Robot::fov );
break;
case 'r':
Robot::range = atof( optarg );
if( ! quiet ) fprintf( stderr, "[Uni] range: %.2f\n", Robot::range );
break;
case 'c':
Robot::pixel_count = atoi( optarg );
if( ! quiet ) fprintf( stderr, "[Uni] pixel_count: %d\n", Robot::pixel_count );
break;
case 'u':
updates_max = atol( optarg );
if( ! quiet ) fprintf( stderr, "[Uni] updates_max: %lu\n", (long unsigned)updates_max );
break;
case 'z':
sleep_msec = atoi( optarg );
if( ! quiet ) fprintf( stderr, "[Uni] sleep_msec: %d\n", sleep_msec );
break;
#if GRAPHICS
case 'w': winsize = atoi( optarg );
if( ! quiet ) fprintf( stderr, "[Uni] winsize: %d\n", winsize );
break;
case 'd': show_data= false;
if( ! quiet ) fprintf( stderr, "[Uni] hide data\n" );
break;
case 'q': quiet = true;
break;
#endif
case '?':
puts( usage );
exit(0); // ok
break;
default:
fprintf( stderr, "[Uni] Option parse error.\n" );
puts( usage );
exit(-1); // error
}
#if GRAPHICS
// initialize opengl graphics
glutInit( &argc, argv );
glutInitWindowSize( winsize, winsize );
glutInitDisplayMode( GLUT_DOUBLE | GLUT_RGBA );
glutCreateWindow( PROGNAME );
glClearColor( 0.8,0.8,1.0,1.0 );
glutDisplayFunc( display_func );
glutTimerFunc( 50, timer_func, 0 );
glutMouseFunc( mouse_func );
glutIdleFunc( idle_func );
glBlendFunc( GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA );
glEnable( GL_BLEND );
glMatrixMode( GL_PROJECTION );
glLoadIdentity();
gluOrtho2D( 0,1,0,1 );
glMatrixMode( GL_MODELVIEW );
glLoadIdentity();
glScalef( 1.0/worldsize, 1.0/worldsize, 1 );
// define a display list for a robot body
double h = 0.01;
double w = 0.01;
glPointSize( 4.0 );
displaylist = glGenLists(1);
glNewList( displaylist, GL_COMPILE );
glPolygonMode( GL_FRONT_AND_BACK, GL_LINE );
glBegin( GL_POLYGON );
glVertex2f( h/2.0, 0 );
glVertex2f( -h/2.0, w/2.0 );
glVertex2f( -h/2.0, -w/2.0 );
glEnd();
glEndList();
#endif // GRAPHICS
struct timeval start;
gettimeofday( &start, NULL );
lastseconds = start.tv_sec + start.tv_usec/1e6;
}
void Robot::UpdateSensor()
{
double radians_per_pixel = fov / (double)pixel_count;
double halfworld = worldsize * 0.5;
// initialize pixels vector
FOR_EACH( it, pixels )
{
it->range = Robot::range; // maximum range
it->robot = NULL; // nothing detected
}
// check every robot in the world to see if it is detected
FOR_EACH( it, population )
{
Robot* other = &(*it);
// discard if it's the same robot
if( other == this )
continue;
// discard if it's out of range. We put off computing the
// hypotenuse as long as we can, as it's relatively expensive.
double dx = other->pose[0] - pose[0];
// wrap around torus
if( dx > halfworld )
dx -= worldsize;
else if( dx < -halfworld )
dx += worldsize;
if( fabs(dx) > Robot::range )
continue; // out of range
double dy = other->pose[1] - pose[1];
// wrap around torus
if( dy > halfworld )
dy -= worldsize;
else if( dy < -halfworld )
dy += worldsize;
if( fabs(dy) > Robot::range )
continue; // out of range
double range = hypot( dx, dy );
if( range > Robot::range )
continue;
// discard if it's out of field of view
double absolute_heading = atan2( dy, dx );
double relative_heading = AngleNormalize((absolute_heading - pose[2]) );
if( fabs(relative_heading) > fov/2.0 )
continue;
// find which pixel it falls in
int pixel = floor( relative_heading / radians_per_pixel );
pixel += pixel_count / 2;
pixel %= pixel_count;
assert( pixel >= 0 );
assert( pixel < (int)pixel_count );
// discard if we've seen something closer in this pixel already.
if( pixels[pixel].range < range)
continue;
// if we made it here, we see this other robot in this pixel.
pixels[pixel].range = range;
pixels[pixel].robot = other;
}
}
void Robot::UpdatePose()
{
// move according to the current speed
double dx = speed[0] * cos(pose[2]);
double dy = speed[0] * sin(pose[2]);;
double da = speed[1];
pose[0] = DistanceNormalize( pose[0] + dx );
pose[1] = DistanceNormalize( pose[1] + dy );
pose[2] = AngleNormalize( pose[2] + da );
}
void Uni::UpdateAll()
{
// if we've done enough updates, exit the program
if( updates_max > 0 && updates > updates_max )
{
FOR_EACH( it, population )
printf( "%.2f,%.2f,%.2f\n", it->pose[0], it->pose[1], it->pose[2] );
exit(1);
}
if( ! paused )
{
FOR_EACH( r, population )
r->UpdatePose();
FOR_EACH( r, population )
r->UpdateSensor();
FOR_EACH( r, population )
{
Robot &b = *r;
r->callback( b, r->callback_data);
}
need_redraw = true;
}
const int period = 10;
if( updates % period == 0 )
{
struct timeval now;
gettimeofday( &now, NULL );
double seconds = now.tv_sec + now.tv_usec/1e6;
double interval = seconds - lastseconds;
fprintf( stderr, "[%d] FPS %.3f\r",(int)updates, period/interval );
fflush(stdout);
lastseconds = seconds;
}
++updates;
}
// draw a robot
void Robot::Draw() const
{
#if GRAPHICS
glPushMatrix();
glTranslatef( pose[0], pose[1], 0 );
glRotatef( rtod(pose[2]), 0,0,1 );
glColor3ub( color[0], color[1], color[2] );
// draw the pre-compiled triangle for a body
glCallList(displaylist);
if( show_data )
{
// render the sensors
double rads_per_pixel = fov / (double)pixel_count;
glPolygonMode( GL_FRONT_AND_BACK, GL_FILL );
for( unsigned int p=0; p<pixel_count; p++ )
{
double angle = -fov/2.0 + (p+0.5) * rads_per_pixel;
double dx1 = pixels[p].range * cos(angle+rads_per_pixel/2.0);
double dy1 = pixels[p].range * sin(angle+rads_per_pixel/2.0);
double dx2 = pixels[p].range * cos(angle-rads_per_pixel/2.0);
double dy2 = pixels[p].range * sin(angle-rads_per_pixel/2.0);
glColor4f( 1,0,0, pixels[p].robot ? 0.2 : 0.05 );
glBegin( GL_POLYGON );
glVertex2f( 0,0 );
glVertex2f( dx1, dy1 );
glVertex2f( dx2, dy2 );
glEnd();
}
}
glPopMatrix();
#endif // GRAPHICS
}
void Uni::Run()
{
#if GRAPHICS
glutMainLoop();
#else
while( 1 )
{
FOR_EACH( r, population )
r->Update();
#endif
}