-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
374 lines (326 loc) · 10.2 KB
/
Copy pathmain.cpp
File metadata and controls
374 lines (326 loc) · 10.2 KB
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
#ifdef __APPLE__
#include <GLUT/glut.h>
#else
#include <gl/glut.h>
#endif
#include <unistd.h>
#include <math.h>
#include <list>
#include <iterator>
#include <vector>
#include "uav.h"
#include "main.h"
#include "simulator.h"
int mouseX, mouseY;
int zoom = 0;
bool visualize = true;
Simulator sim;
double angleToRad(double angle) {return 0.01745329252*angle;}
/* Translates a simulated coordinate to one that fits properly in the window
Axis should be 0 for x and 1 for y */
double coorToUnit (double coor, bool axis)
{
double base;
double dist;
if (axis)
{
base = yFloor;
dist = yCeil - yFloor;
}
else
{
base = xFloor;
dist = xCeil - xFloor;
}
coor -= base;
coor /= dist/2;
coor -= 1;
return coor;
}
// Reads and runs a course file
int readCourse (char* filename, Simulator* sim)
{
sim->uavs.empty();
std::vector<int> courseID;
/* The vector courseID[x] stores the id defined by the course file of the xth plane in the simulation
For many course files, courseID[x] == x */
FILE *file = fopen(filename, "r");
if (file != NULL)
{
char buffer[256];
int id;
double x;
double y;
double dir;
int found = 0;
bool latLng = true;
double baseLat = -1;
double baseLng = -1;
while (fgets(buffer,sizeof(buffer), file))
{
if (buffer[0] >= 48 && buffer[0] < 58)
{
sscanf(buffer, "%d %lf %lf %lf", &id, &x, &y, &dir);
/* If the course file is in latitude and longitude, convert it to meters
It is most accurate for course files that have latitiudes around 32-33 */
if (latLng)
{
if (baseLat == -1)
{
baseLat = y;
baseLng = x;
}
y-=baseLat;
y*=93976.73;
x-=baseLng;
x*=110895.58;
}
// Match the course id to the simulation id
found = -1;
for (int i=0; i<courseID.size(); i++)
{
if (courseID[i] == id)
found = i;
}
// The the uav is not found in the simulation, create it, otherwise, add the waypoint
if (found == -1)
{
sim->uavs.push_back(UAV(x, y, dir));
courseID.push_back(id);
}
else
{
sim->uavs[found].waypoints.push_back(Coordinate(x, y));
}
}
else if (buffer[0] == '-')
{
if (buffer[1] == 'm')
latLng = false;
else if (buffer[1] == 'l')
latLng = true;
}
else if (buffer[0] == '=' && buffer[1] == '=')
{
break;
}
}
fclose(file);
return 1;
}
return -1;
}
// Draws a grid
void drawGrid()
{
double dist = xCeil-xFloor;
double intensity = 0;
glBegin(GL_LINES);
if (dist < 800)
{
intensity = std::min(1.0, 1.0*(4-dist/200));
glColor3f(intensity*0.25, intensity*0.1, intensity*0.25);
for (int temp=ceil(xFloor/40)*40; temp<xCeil; temp+=40)
{
glVertex2f(coorToUnit(temp,0), coorToUnit(yFloor,1));
glVertex2f(coorToUnit(temp,0), coorToUnit(yCeil,1));
}
for (int temp=ceil(yFloor/40)*40; temp<yCeil; temp+=40)
{
glVertex2f(coorToUnit(xFloor,0), coorToUnit(temp,1));
glVertex2f(coorToUnit(xCeil,0), coorToUnit(temp,1));
}
}
if (dist < 4000)
{
intensity = std::min(1.0, 1.0*(4-dist/1000));
glColor3f(intensity*0.45, intensity*0.45, intensity*0.45);
for (int temp=ceil(xFloor/200)*200; temp<xCeil; temp+=200)
{
glVertex2f(coorToUnit(temp,0), coorToUnit(yFloor,1));
glVertex2f(coorToUnit(temp,0), coorToUnit(yCeil,1));
}
for (int temp=ceil(yFloor/200)*200; temp<yCeil; temp+=200)
{
glVertex2f(coorToUnit(xFloor,0), coorToUnit(temp,1));
glVertex2f(coorToUnit(xCeil,0), coorToUnit(temp,1));
}
}
if (dist < 40000)
{
intensity = std::min(1.0, 1.0*(4-dist/5000));
glColor3f(intensity*0.3, intensity*0.9, intensity*0.3);
for (int temp=ceil(xFloor/1000)*1000; temp<xCeil; temp+=1000)
{
glVertex2f(coorToUnit(temp,0), coorToUnit(yFloor,1));
glVertex2f(coorToUnit(temp,0), coorToUnit(yCeil,1));
}
for (int temp=ceil(yFloor/1000)*1000; temp<yCeil; temp+=1000)
{
glVertex2f(coorToUnit(xFloor,0), coorToUnit(temp,1));
glVertex2f(coorToUnit(xCeil,0), coorToUnit(temp,1));
}
}
glEnd();
}
// Draws an uav
void drawUAV(UAV u)
{
double d1 = angleToRad(u.direction);
double d2 = angleToRad(u.direction+120);
double d3 = angleToRad(u.direction-120);
double scaleUAV = uavDrawScale/200.0;
double scaleWP = waypointDrawScale*0.004;
// The tail
glBegin(GL_LINE_STRIP);
double i=TAIL_SIZE-u.oldCoor.size();
for (Coordinate c : u.oldCoor)
{
i++;
glColor4f(u.color[0], u.color[1], u.color[2], i/TAIL_SIZE);
glVertex2f(coorToUnit(c.x, 0), (coorToUnit(c.y, 1)));
}
glVertex2f(coorToUnit(u.coor.x, 0), coorToUnit(u.coor.y, 1));
glEnd();
// Waypoints
glColor3f(u.color[0], u.color[1], u.color[2]);
glBegin(GL_QUADS);
glVertex2f(coorToUnit(u.curWaypoint.x,0)+scaleWP, coorToUnit(u.curWaypoint.y,1));
glVertex2f(coorToUnit(u.curWaypoint.x,0), coorToUnit(u.curWaypoint.y,1)+scaleWP);
glVertex2f(coorToUnit(u.curWaypoint.x,0)-scaleWP, coorToUnit(u.curWaypoint.y,1));
glVertex2f(coorToUnit(u.curWaypoint.x,0), coorToUnit(u.curWaypoint.y,1)-scaleWP);
glEnd();
if (u.waypoints.size()!=0)
{
glBegin(GL_LINE_LOOP);
glVertex2f(coorToUnit(u.waypoints.begin()->x,0)+scaleWP, coorToUnit(u.waypoints.begin()->y,1));
glVertex2f(coorToUnit(u.waypoints.begin()->x,0), coorToUnit(u.waypoints.begin()->y,1)+scaleWP);
glVertex2f(coorToUnit(u.waypoints.begin()->x,0)-scaleWP, coorToUnit(u.waypoints.begin()->y,1));
glVertex2f(coorToUnit(u.waypoints.begin()->x,0), coorToUnit(u.waypoints.begin()->y,1)-scaleWP);
glEnd();
}
// The UAV
glColor3f(u.color[0], u.color[1], u.color[2]);
glBegin(GL_TRIANGLES);
glVertex2f(coorToUnit(u.coor.x, 0) + cos(d1)*scaleUAV, coorToUnit(u.coor.y, 1) + sin(d1)*scaleUAV);
glVertex2f(coorToUnit(u.coor.x, 0) + cos(d2)*scaleUAV/2, coorToUnit(u.coor.y, 1) + sin(d2)*scaleUAV/2);
glVertex2f(coorToUnit(u.coor.x, 0) + cos(d3)*scaleUAV/2, coorToUnit(u.coor.y, 1) + sin(d3)*scaleUAV/2);
glEnd();
}
// Draws an event
void drawEvent(event e)
{
switch (e.type)
{
case EVENT_REACHED_WAYPOINT:
{
double scaleWP = waypointDrawScale*0.004;
double drawTime = (double)e.time/20.0;
glColor4f(1, 1, 1, drawTime);
glBegin(GL_QUADS);
glVertex2f(coorToUnit(e.coor.x,0)+scaleWP*(5-drawTime*4), coorToUnit(e.coor.y,1));
glVertex2f(coorToUnit(e.coor.x,0), coorToUnit(e.coor.y,1)+scaleWP*(5-drawTime*4));
glVertex2f(coorToUnit(e.coor.x,0)-scaleWP*(5-drawTime*4), coorToUnit(e.coor.y,1));
glVertex2f(coorToUnit(e.coor.x,0), coorToUnit(e.coor.y,1)-scaleWP*(5-drawTime*4));
glEnd();
break;
}
case EVENT_NEAR_MISS:
{
glBegin(GL_LINES);
glColor3f(1,1,1);
glVertex2f(coorToUnit(e.coor.x,0)+0.03, coorToUnit(e.coor.y,1)+0.03);
glVertex2f(coorToUnit(e.coor.x,0)-0.03, coorToUnit(e.coor.y,1)-0.03);
glVertex2f(coorToUnit(e.coor.x,0)-0.03, coorToUnit(e.coor.y,1)+0.03);
glVertex2f(coorToUnit(e.coor.x,0)+0.03, coorToUnit(e.coor.y,1)-0.03);
glEnd();
break;
}
}
}
void display() {
glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
glClear(GL_COLOR_BUFFER_BIT);
sim.step();
drawGrid();
for (UAV u:sim.uavs)
{
if (u.active)
drawUAV(u);
}
for (event e:sim.events)
{
drawEvent(e);
}
glFlush();
usleep(sleepRate);
if (zoom != 0)
{
double scale = zoom*8*(xCeil-xFloor)/glutGet(GLUT_WINDOW_WIDTH);
xCeil -=scale;
xFloor+=scale;
yCeil -=scale;
yFloor+=scale;
}
glutPostRedisplay();
}
void dragFunc(int x, int y)
{
double xScale = (xCeil-xFloor)/glutGet(GLUT_WINDOW_WIDTH);
double yScale = (xCeil-xFloor)/glutGet(GLUT_WINDOW_HEIGHT);
xCeil += (mouseX-(double)x)*xScale;
xFloor+= (mouseX-(double)x)*xScale;
yCeil -= (mouseY-(double)y)*yScale;
yFloor-= (mouseY-(double)y)*yScale;
mouseX = x;
mouseY = y;
}
void mouseFunc(int btn, int state, int x, int y)
{
if (btn == GLUT_LEFT_BUTTON && state == GLUT_DOWN)
{
mouseX = x;
mouseY = y;
}
}
void keyFunc(unsigned char key, int x, int y)
{
if (key=='=') // Zoom in
{
zoom = 1;
}
else if (key=='-') // Zoom out
{
zoom = -1;
}
}
void keyUpFunc(unsigned char key, int x, int y)
{
if (key=='=' || key=='-') // Zoom in/out
{
zoom = 0;
}
}
int main(int argc, char** argv) {
readCourse((char*)"test.txt", &sim);
sim.init(visualize);
if (visualize)
{
glutInit(&argc, argv);
glutInitWindowSize(800, 800);
glutCreateWindow("UAV");
glutDisplayFunc(display);
glutMotionFunc(dragFunc);
glutMouseFunc(mouseFunc);
glutKeyboardFunc(keyFunc);
glutKeyboardUpFunc(keyUpFunc);
glEnable(GL_BLEND);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
glutMainLoop();
}
else
{
while (sim.step()) {}
}
return 0;
}