-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathantix.h
372 lines (286 loc) · 8.7 KB
/
antix.h
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
/****
antix.h
Clone this package from git://github.com/rtv/Antix.git
version 2
Richard Vaughan
****/
#include <vector>
#include <set>
#include <list>
#include <math.h>
#include <stdio.h>
#include <stdlib.h>
#include <assert.h>
#define GRAPHICS 1
#define DEBUGVIS 0
// handy STL iterator macro pair. Use FOR_EACH(I,C){ } to get an iterator I to
// each item in a collection C.
#define VAR(V,init) __typeof(init) V=(init)
#define FOR_EACH(I,C) for(VAR(I,(C).begin());I!=(C).end();I++)
namespace Antix
{
/** Convert radians to degrees. */
inline double rtod( double r ){ return( r * 180.0 / M_PI ); }
/** Convert degrees to radians */
inline double dtor( double d){ return( d * M_PI / 180.0 ); }
// bounds type - specifies a range of values
typedef struct
{
double min, max;
} bounds_t;
// bounding box type - specifies a 2d range of values
typedef struct
{
bounds_t x, y;
} bbox_t;
class Home;
class Puck
{
public:
bool held; // true iff carried by a robot
Home* home;
unsigned int index; // the matrix cell that currently contains this puck
uint64_t delivery_time;
double x,y; // location
/** constructor places a puck at specified pose */
Puck( double x, double y );
~Puck();
void Replace();
//Puck()
//: held(false), index(0), lifetime(10), x(0.0), y(0.0)//, count(1)
// { /* do nothing */ }
void Pickup();
void Drop();
};
class Home
{
public:
unsigned int id;
class Color
{
public:
double r, g, b;
Color( double r, double g, double b ) : r(r), g(g), b(b) {}
// get a random color
static Color Random()
{
return Color( drand48(), drand48(), drand48() );
}
} color;
std::list<Puck*> pucks;
unsigned int score;
double x, y, r;
Home( unsigned int id, const Color& color, double x, double y, double r );
void UpdatePucks();
};
class Robot
{
public:
/** initialization: call this before using any other calls. */
static void Init( int argc, char** argv );
static void UpdateGui();
/** update all robots */
static void UpdateAll();
/** Normalize a length to within 0 to worldsize. */
static double DistanceNormalize( double d );
/** Normalize an angle to within +/_ M_PI. */
static double AngleNormalize( double a );
/** Wrap distances around the torus */
static double WrapDistance( double d );
/** Start running the simulation. Does not return. */
static void Run();
static bool paused; // runs only when this is false
static bool show_data; // controls visualization of pixel data
static double fov; // sensor detects objects within this angular field-of-view about the current heading
static double pickup_range;
static double radius; // radius of all robot's bodies
static double range; // sensor detects objects up tp this maximum distance
static double worldsize; // side length of the toroidal world
static std::vector<Home*> homes;
static std::vector<Robot*> population;
static uint64_t updates; // number of simulation steps so far
static uint64_t updates_max; // number of simulation steps to run before quitting (0 means infinity)
static unsigned int home_count; // number of home zones
static unsigned int home_population; // number of robots
static unsigned int puck_count; // number of pucks that exist in the world
static unsigned int sleep_msec; // number of milliseconds to sleep at each update
static unsigned int gui_interval; // number of milliseconds between window redraws
static Robot* first;
class MatrixCell
{
public:
std::vector<Robot*> robots;
std::vector<Puck*> pucks;
};
static std::vector<Robot::MatrixCell> matrix;
static unsigned int matrixwidth;
void TestPucksInCell( const MatrixCell& cell );
void TestRobotsInCell( const MatrixCell& cell );
unsigned int index; // the matrix cell that currently holds this robot
#if GRAPHICS
static int winsize; // initial size of the window in pixels
/** initialization: call this before using any other calls. */
static void InitGraphics( int argc, char* argv[] );
/** render all robots in OpenGL */
static void DrawAll();
// render the robot in OpenGL
void Draw();
#endif
bbox_t sensor_bbox;
void FovBBox( bbox_t& box );
// deliver pucks to this location
Home* home;
class Pose
{
public:
double x,y,a; // 2d position and orientation
Pose( double x, double y, double a ) : x(x), y(y), a(a) {}
Pose() : x(0.0), y(0.0), a(0.0) {}
//Pose( const Pose &p ) : x(p.x), y(p.y), a(p.a) {}
// get a random pose
static Pose Random()
{
return Pose( drand48() * Robot::worldsize,
drand48() * Robot::worldsize,
Robot::AngleNormalize( drand48() * (M_PI*2.0)));
}
} pose; // instance: robot is located at this pose
class Speed
{
public:
double v; // forward speed
double w; // turn speed
// constructor sets speeds to zero
Speed() : v(0.0), w(0.0) {}
} speed; // instance: robot is moving this fast
class SeeRobot
{
public:
const Home* home;
Pose pose;
Speed speed;
double range;
double bearing;
bool haspuck;
SeeRobot( const Home* home, const Pose& p, const Speed& s, const double range, const double bearing, const bool haspuck )
: home(home), pose(p), speed(s), range(range), bearing(bearing), haspuck(haspuck)
{ /* empty */}
};
/** A sense vector containing information about all the robots
detected in my field of view */
std::vector<SeeRobot> see_robots;
static inline unsigned int Cell( double x )
{
const double d = Robot::worldsize / (double)Robot::matrixwidth;
while( x > worldsize ) // wraparound
x -= worldsize;
while( x < 0 ) // wraparound
x += worldsize;
return floor( x / d );
}
static inline int CellNoWrap( double x )
{
const double d = Robot::worldsize / (double)Robot::matrixwidth;
return floor( x / d );
}
static inline unsigned int CellWrap( int x )
{
while( x >= (int)matrixwidth ) // wraparound
x -= matrixwidth;
while( x < 0 ) // wraparound
x += matrixwidth;
return x;
}
static inline unsigned int Cell( double x, double y )
{
return (Cell(x) + (Cell(y) * Robot::matrixwidth) );
}
static std::vector<Puck> pucks;
class SeePuck
{
public:
Puck* puck;
bool held;
double bearing;
double range;
SeePuck( Puck* puck, const double range, const double bearing, const bool held )
: puck(puck), held(held), bearing(bearing), range(range)
{ /* empty */}
};
/** A sense vector containing information about all the pucks
detected in my field of view */
std::vector<SeePuck> see_pucks;
#if DEBUGVIS
std::vector<Robot*> neighbors;
std::vector<Puck*> neighbor_pucks;
std::set<unsigned int> neighbor_cells;
#endif
// constructor
Robot( Home* home, const Pose& pose );
// destructor
virtual ~Robot() {}
/** Attempt to pick up a puck. Returns true if one was picked up,
else false. */
bool Pickup();
/** Attempt to drop a puck. Returns true if one was dropped, else
false. */
bool Drop();
/** Returns true if we are currently holding a puck. */
bool Holding() const;
/** pure virtual - subclasses must implement this method */
virtual void Controller() = 0;
private:
Puck* puck_held;
// move the robot
void UpdatePose();
// update
//void UpdateSensors();
public:
void UpdateRobotSensor();
void UpdatePuckSensor();
};
// fast approximation to atan2
inline double fast_atan2( double y, double x )
{
const double piD2( M_PI/2.0 );
double atan;
double z = y/x;
if ( x == 0.0 ){
if ( y > 0.0 ) return piD2;
if ( y == 0.0 ) return 0.0;
return -piD2;
}
if ( fabs( z ) < 1.0 ){
atan = z/(1.0 + 0.28*z*z);
if ( x < 0.0 ){
if ( y < 0.0 ) return atan - M_PI;
return atan + M_PI;
}
}
else{
atan = piD2 - z/(z*z + 0.28f);
if ( y < 0.0f ) return atan - M_PI;
}
return atan;
}
inline double fast_sin(double x)
{
const double B = 4/M_PI;
const double C = -4/(M_PI*M_PI);
const double P = 0.225;
const double y = B * x + C * x * fabs(x);
return( P * (y * fabs(y) - y) + y );
}
inline double fast_cos(double x)
{
const double B = 4/M_PI;
const double C = -4/(M_PI*M_PI);
const double P = 0.225;
x = x + M_PI/2;
if(x > M_PI){ // Original x > M_PI/2
x -= 2 * M_PI; // Wrap: cos(x) = cos(x - 2 M_PI)
}
double y = B * x + C * x * fabs(x); //fast, inprecise
return( P * (y * fabs(y) - y) + y );
}
}; // namespace Antix