-
Notifications
You must be signed in to change notification settings - Fork 0
/
preview.py
144 lines (117 loc) · 2.99 KB
/
preview.py
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
import sys
import matplotlib.pyplot as plt
def load_obstacles(filename):
with open(filename, 'r') as myfile:
data = myfile.read().replace('\n', ' ')
s = data.split()
obs_num = int(s[0])
res = []
j = 1
for i in range(obs_num):
p_num = int(s[j])
j += 1
pol = []
for k in range(p_num):
pol.append([float(s[j]), float(s[j + 1])])
j += 2
pol.append(pol[0]) # repeat the first point to create a 'closed loop'
res.append(pol)
return res
def draw_obstacles(obstacles):
for pol in obstacles:
xs, ys = zip(*pol) # create lists of x and y values
plt.plot(xs, ys, 'red')
def draw_robot(start, robot):
xs, ys = zip(*robot) # create lists of x and y values
xs = list(xs)
ys = list(ys)
for i in range(len(xs)):
xs[i] = xs[i] - robot[0][0] + start[0]
for i in range(len(ys)):
ys[i] = ys[i] - robot[0][1] + start[1]
plt.plot(xs, ys, 'black')
def load_paths(filename):
with open(filename, 'r') as myfile:
data = myfile.read().replace('\n', ' ')
s = data.split()
j = 0
path = []
n = int(s[j])
j += 1
for i in range(n):
path.append([float(s[j]), float(s[j + 1])])
j += 2
return path
def draw_path(path):
for point in path:
xs, ys = zip(*path) # create lists of x and y values
xs = list(xs)
ys = list(ys)
plt.plot(xs, ys, 'green')
def old_draw_path(path, robot):
for point in robot:
xs, ys = zip(*path) # create lists of x and y values
xs = list(xs)
ys = list(ys)
for i in range(len(xs)):
xs[i] = xs[i] - robot[0][0] + point[0]
for i in range(len(ys)):
ys[i] = ys[i] - robot[0][1] + point[1]
plt.plot(xs, ys, 'green')
for point in path:
xs, ys = zip(*robot) # create lists of x and y values
xs = list(xs)
ys = list(ys)
for i in range(len(xs)):
xs[i] = xs[i] - robot[0][0] + point[0]
for i in range(len(ys)):
ys[i] = ys[i] - robot[0][1] + point[1]
plt.plot(xs, ys, 'green')
def load_sites(filename):
with open(filename, 'r') as myfile:
data = myfile.read().replace('\n', ' ')
s = data.split()
j = 0
sites = []
n = int(s[j])
j += 1
for i in range(n):
sites.append([float(s[j]), float(s[j + 1])])
j += 2
return sites
def draw_sites(sites):
for point in sites:
xs, ys = zip(*sites) # create lists of x and y values
xs = list(xs)
ys = list(ys)
plt.plot(xs, ys, 'ro')
def load_robot(filename):
with open(filename, 'r') as myfile:
data = myfile.read().replace('\n', ' ')
s = data.split()
start = [float(s[0]), float(s[1])]
n = int(s[2])
res = []
j = 3
for i in range(n):
res.append([float(s[j]), float(s[j + 1])])
j += 2
res.append(res[0])
return [start, res]
if len(sys.argv) < 4:
print("[USAGE1] RobotFile SitesFile ObstaclesFile")
print("[USAGE2] RobotFile SitesFile ObstaclesFile OutputFile")
exit()
cmds = sys.argv[1:]
plt.figure()
sites = load_sites(cmds[1])
draw_sites(sites)
obstacles = load_obstacles(cmds[2])
draw_obstacles(obstacles)
start, robot = load_robot(cmds[0])
if len(cmds) == 4:
path = load_paths(cmds[3])
draw_path(path)
#old_draw_path(path, robot)
draw_robot(start, robot)
plt.show()