-
Notifications
You must be signed in to change notification settings - Fork 3
/
draw.h
273 lines (214 loc) · 6.1 KB
/
draw.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
/*
* anytimeRRT.cpp
*
* Created on: Nov 8, 2014
* Author: Luo Yuanfu
* This Project is based on the paper:
Ferguson, Dave, and Anthony Stentz. "Anytime rrts."
In Intelligent Robots and Systems, 2006 IEEE/RSJ
International Conference on, pp. 5369-5375. IEEE, 2006.
*/
#ifndef DRAW_H_
#define DRAW_H_
#include <graphics.h>
#include <X11/Xlib.h>
class vect//vector
{public:
double x;
double y;
void setValue(double x,double y)
{
this->x=x;
this->y=y;
}
};
class obstacles
{public:
double x0;
double y0;
double x1;
double y1;
void setValue(double x0,double y0,double x1,double y1)
{
this->x0=x0;
this->y0=y0;
this->x1=x1;
this->y1=y1;
}
};
class robot_pos
{public:
double x1;
double y1;
double x2;
double y2;
double x3;
double y3;
void setValue(double x1,double y1,double x2,double y2,double x3, double y3)
{
this->x1=x1;
this->y1=y1;
this->x2=x2;
this->y2=y2;
this->x3=x3;
this->y3=y3;
}
};
class CostArea
{public:
double x0;
double y0;
double x1;
double y1;
void setValue(double x0,double y0,double x1,double y1)
{
this->x0=x0;
this->y0=y0;
this->x1=x1;
this->y1=y1;
}
};
std::vector<CostArea> cost_area_vec;
std::vector<obstacles> obsts;
std::vector<robot_pos> path;
void drawPath(std::vector<robot_pos>path,double env_width,double env_height,int agent_num)
{
int graph_width=getmaxx();
int graph_height=getmaxy();
setcolor(YELLOW);
circle(int(path.at(0).x1/env_width*graph_width),int(path.at(0).y1/env_height*graph_height),3);
for(int i=1;i<path.size();i++)
{
line(int(path.at(i).x1/env_width*graph_width),int(path.at(i).y1/env_height*graph_height),int(path.at(i-1).x1/env_width*graph_width),int(path.at(i-1).y1/env_height*graph_height));
circle(int(path.at(i).x1/env_width*graph_width),int(path.at(i).y1/env_height*graph_height),3);
}
if(agent_num!=3) return;
setcolor(GREEN);
circle(int(path.at(0).x2/env_width*graph_width),int(path.at(0).y2/env_height*graph_height),3);
for(int i=1;i<path.size();i++)
{
line(int(path.at(i).x2/env_width*graph_width),int(path.at(i).y2/env_height*graph_height),int(path.at(i-1).x2/env_width*graph_width),int(path.at(i-1).y2/env_height*graph_height));
circle(int(path.at(i).x2/env_width*graph_width),int(path.at(i).y2/env_height*graph_height),3);
}
setcolor(RED);
circle(int(path.at(0).x3/env_width*graph_width),int(path.at(0).y3/env_height*graph_height),3);
for(int i=1;i<path.size();i++)
{
line(int(path.at(i).x3/env_width*graph_width),int(path.at(i).y3/env_height*graph_height),int(path.at(i-1).x3/env_width*graph_width),int(path.at(i-1).y3/env_height*graph_height));
circle(int(path.at(i).x3/env_width*graph_width),int(path.at(i).y3/env_height*graph_height),3);
}
}
void drawObsts(double env_width,double env_height)
{
int graph_width=getmaxx();
int graph_height=getmaxy();
setcolor(WHITE);
// setfillstyle(BROWN,5,NULL);
for(int i=0;i<obsts.size();i++)
{
//fillpoly(2,int(obsts.at(i).x0*500),int(obsts.at(i).y0*500),int(obsts.at(i).x1*500),int(obsts.at(i).y1*500));
rectangle(int(obsts.at(i).x0/env_width*graph_width),int(obsts.at(i).y0/env_height*graph_height),int(obsts.at(i).x1/env_width*graph_width),int(obsts.at(i).y1/env_height*graph_height));
//bar(int(obsts.at(i).x0*graph_width),int(obsts.at(i).y0*graph_height),int(obsts.at(i).x1*graph_width),int(obsts.at(i).y1*graph_height));
}
}
void drawCostArea(double env_width,double env_height)
{
int graph_width=getmaxx();
int graph_height=getmaxy();
setcolor(BROWN);
// setfillstyle(BROWN,5,NULL);
for(int i=0;i<cost_area_vec.size();i++)
{
//fillpoly(2,int(obsts.at(i).x0*500),int(obsts.at(i).y0*500),int(obsts.at(i).x1*500),int(obsts.at(i).y1*500));
rectangle(int(cost_area_vec.at(i).x0/env_width*graph_width),int(cost_area_vec.at(i).y0/env_height*graph_height),int(cost_area_vec.at(i).x1/env_width*graph_width),int(cost_area_vec.at(i).y1/env_height*graph_height));
//bar(int(obsts.at(i).x0*graph_width),int(obsts.at(i).y0*graph_height),int(obsts.at(i).x1*graph_width),int(obsts.at(i).y1*graph_height));
}
}
void loadData(int agent_num)
{
FILE *p;
char temp[30];
char c='a';
obstacles obst;
robot_pos robot;
CostArea cost_area;
if((p=fopen("obstacles.mp","r"))==NULL)
{
//printf("cannot open obstacle files. using the environment without obstacles now\n");
//return;
goto out2;
}
while(c!=EOF)
{
fgets(temp,30,p);
fscanf(p,"%lf %lf",&obst.x0,&obst.y0);
fgetc(p);
fscanf(p,"%lf %lf",&obst.x0,&obst.y0);
fgetc(p);
fscanf(p,"%lf %lf",&obst.x1,&obst.y1);
fgetc(p);
fscanf(p,"%lf %lf",&obst.x1,&obst.y1);
fgetc(p);
fgets(temp,30,p);
c=fgetc(p);
/*
if(obst.x0>obst.x1)
{
double temp_swap=obst.x1;
obst.x1=obst.x0;
obst.x0=temp_swap;
}
if(obst.y0>obst.y1)
{
double temp_swap=obst.y1;
obst.y1=obst.y0;
obst.y0=temp_swap;
}
*/
obsts.push_back(obst);
}
out2: fclose(p);
if((p=fopen("path.dat","r"))==NULL)
{
printf("no solution file\n");
exit(0);
}
char c1='b';
while(c1!=EOF)
{
if(agent_num==3)fscanf(p,"%lf %lf %lf %lf %lf %lf",&robot.x1,&robot.y1,&robot.x2,&robot.y2,&robot.x3,&robot.y3);
else fscanf(p,"%lf %lf",&robot.x1,&robot.y1);
// fgetc(p);
c1=fgetc(p);
path.push_back(robot);
}
fclose(p);
if((p=fopen("cost_area.mp","r"))==NULL)
{
printf("cannot open cost_area files. using the uniform-cost environment now\n");
return;
//exit(0);
}
char c2='a';
while(c2!=EOF)
{
fscanf(p,"%lf %lf %lf %lf",&cost_area.x0,&cost_area.y0,&cost_area.x1,&cost_area.y1);
c2=fgetc(p);
cost_area_vec.push_back(cost_area);
}
fclose(p);
}
void drawMap(double env_width,double env_height,int agent_num)
{
XInitThreads();
int gd=DETECT,gm;
initgraph(&gd,&gm,NULL);
// initgraph(,500);
loadData(agent_num);
drawObsts(env_width,env_height);
drawPath(path,env_width,env_height,agent_num);
drawCostArea(env_width,env_height);
getchar();
closegraph();
}
#endif /* DRAW_H_ */