-
Notifications
You must be signed in to change notification settings - Fork 24
/
Visualization.py
257 lines (225 loc) · 10.8 KB
/
Visualization.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
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
import sys
import pygame
from pygame.locals import KEYDOWN, K_q
import numpy as np
import time
from sklearn.preprocessing import MinMaxScaler
# CONSTANTS:
BLACK = (0, 0, 0)
GREY = (160, 160, 160)
class visualize_paths():
def __init__(self, AllRealPaths, subCellsAssignment, DroneNo, color):
self.AllRealPaths = AllRealPaths
self.subCellsAssignment = subCellsAssignment
min_max_scaler = MinMaxScaler(feature_range=(0, 800))
self.dimensions = min_max_scaler.fit_transform(np.array([self.subCellsAssignment.shape[0], self.subCellsAssignment.shape[1], 0]).reshape(-1, 1)).ravel()
self.DroneNo = DroneNo
self._VARS = {'surf': False,
'gridWH': (self.dimensions[0], self.dimensions[1]),
'gridOrigin': (0, 0),
'gridCellsX': self.subCellsAssignment.shape[0],
'gridCellsY': self.subCellsAssignment.shape[1],
'lineWidth': 2}
self.color = color
def visualize_paths(self, mode):
pygame.init()
self._VARS['surf'] = pygame.display.set_mode((self.dimensions[1], self.dimensions[0]))
pygame.display.set_caption('Mode: ' + str(mode))
while True:
keep_going = self.checkEvents()
if not keep_going:
break
self._VARS['surf'].fill(GREY)
self.drawSquareGrid(self._VARS['gridOrigin'],
self._VARS['gridWH'],
self._VARS['gridCellsX'],
self._VARS['gridCellsY'])
self.placeCells()
pygame.display.update()
def placeCells(self):
cellBorder = 0
celldimX = (self._VARS['gridWH'][0]/self._VARS['gridCellsX'])
celldimY = (self._VARS['gridWH'][1]/self._VARS['gridCellsY'])
for r in range(self.DroneNo):
for point in self.AllRealPaths[r]:
color = pygame.Color(255, 0, 0)
pygame.draw.line(self._VARS['surf'],
self.color[r],
(self._VARS['gridOrigin'][0] + (celldimX*point[1] + celldimX/2),
self._VARS['gridOrigin'][1] + (celldimY*point[0]) + celldimY/2),
(self._VARS['gridOrigin'][0] + (celldimX*point[3]) + celldimX/2,
self._VARS['gridOrigin'][1] + (celldimY*point[2]) + celldimY/2), width=4)
cellBorder = 0
for row in range(self.subCellsAssignment.shape[0]):
for column in range(self.subCellsAssignment.shape[1]):
if (self.subCellsAssignment[row][column] == self.DroneNo):
self.drawSquareCell(
self._VARS['gridOrigin'][0] + (celldimX*column)
+ self._VARS['lineWidth']/2,
self._VARS['gridOrigin'][1] + (celldimY*row)
+ self._VARS['lineWidth']/2,
celldimX, celldimY, BLACK)
# Draw filled rectangle at coordinates
def drawSquareCell(self, x, y, dimX, dimY, color):
pygame.draw.rect(
self._VARS['surf'], color,
(x, y, dimX, dimY)
)
def drawSquareGrid(self, origin, gridWH, cellsX, cellsY):
CONTAINER_WIDTH_HEIGHT = gridWH
cont_x, cont_y = (0, 0)
# DRAW Grid Border:
# TOP lEFT TO RIGHT
pygame.draw.line(
self._VARS['surf'], BLACK,
(cont_x, cont_y),
(CONTAINER_WIDTH_HEIGHT[1] + cont_x, cont_y), self._VARS['lineWidth'])
# # BOTTOM lEFT TO RIGHT
pygame.draw.line(
self._VARS['surf'], BLACK,
(cont_x, CONTAINER_WIDTH_HEIGHT[0] + cont_y),
(CONTAINER_WIDTH_HEIGHT[1] + cont_x,
CONTAINER_WIDTH_HEIGHT[0] + cont_y), self._VARS['lineWidth'])
# # LEFT TOP TO BOTTOM
pygame.draw.line(
self._VARS['surf'], BLACK,
(cont_x, cont_y),
(cont_x, cont_y + CONTAINER_WIDTH_HEIGHT[0]), self._VARS['lineWidth'])
# # RIGHT TOP TO BOTTOM
pygame.draw.line(
self._VARS['surf'], BLACK,
(CONTAINER_WIDTH_HEIGHT[1] + cont_x, cont_y),
(CONTAINER_WIDTH_HEIGHT[1] + cont_x,
CONTAINER_WIDTH_HEIGHT[0] + cont_y), self._VARS['lineWidth'])
# Get cell size, just one since its a square grid.
cellSizeX = CONTAINER_WIDTH_HEIGHT[0]/cellsX
cellSizeY = CONTAINER_WIDTH_HEIGHT[1]/cellsY
for x in range(cellsY):
pygame.draw.line(
self._VARS['surf'], BLACK,
(cont_x + (cellSizeX * x), cont_y),
(cont_x + (cellSizeX * x), CONTAINER_WIDTH_HEIGHT[0] + cont_y), 2)
for y in range(cellsX):
# # HORIZONTAl DIVISIONS
pygame.draw.line(
self._VARS['surf'], BLACK,
(cont_x, cont_y + (cellSizeY*y)),
(cont_x + CONTAINER_WIDTH_HEIGHT[1], cont_y + (cellSizeY*y)), 2)
def checkEvents(self):
for event in pygame.event.get():
if event.type == pygame.QUIT or (event.type == KEYDOWN and event.key == K_q):
pygame.quit()
return False
return True
class darp_area_visualization(object):
def __init__(self, Assignment_matrix, DroneNo, color, init_robot_pos):
self.Assignment_matrix = Assignment_matrix
min_max_scaler = MinMaxScaler(feature_range=(0, 800))
dimensions = min_max_scaler.fit_transform(np.array([self.Assignment_matrix.shape[0], self.Assignment_matrix.shape[1], 0]).reshape(-1, 1)).ravel()
self.DroneNo = DroneNo
self._VARS = {'surf': False,
'gridWH': (dimensions[0], dimensions[1]),
'gridOrigin': (0, 0),
'gridCellsX': self.Assignment_matrix.shape[0],
'gridCellsY': self.Assignment_matrix.shape[1],
'lineWidth': 2}
self.color = color
self.init_robot_pos_colors = [np.clip((r[0] - 20, r[1] + 20, r[2] - 20), 0, 255).tolist() for r in self.color]
self.init_robot_pos = init_robot_pos
pygame.init()
self._VARS['surf'] = pygame.display.set_mode((dimensions[1], dimensions[0]))
self.checkEvents()
self._VARS['surf'].fill(GREY)
self.drawSquareGrid(self._VARS['gridOrigin'], self._VARS['gridWH'],
self._VARS['gridCellsX'], self._VARS['gridCellsY'])
self.placeCells(self.Assignment_matrix)
pygame.display.set_caption('Assignment Matrix')
pygame.display.update()
# time.sleep(5)
def checkEvents(self):
for event in pygame.event.get():
if event.type == pygame.QUIT:
sys.exit()
elif event.type == KEYDOWN and event.key == K_q:
pygame.quit()
sys.exit()
def drawSquareGrid(self, origin, gridWH, cellsX, cellsY):
CONTAINER_WIDTH_HEIGHT = gridWH
cont_x, cont_y = (0, 0)
# DRAW Grid Border:
# TOP lEFT TO RIGHT
pygame.draw.line(
self._VARS['surf'], BLACK,
(cont_x, cont_y),
(CONTAINER_WIDTH_HEIGHT[1] + cont_x, cont_y), self._VARS['lineWidth'])
# # BOTTOM lEFT TO RIGHT
pygame.draw.line(
self._VARS['surf'], BLACK,
(cont_x, CONTAINER_WIDTH_HEIGHT[0] + cont_y),
(CONTAINER_WIDTH_HEIGHT[1] + cont_x,
CONTAINER_WIDTH_HEIGHT[0] + cont_y), self._VARS['lineWidth'])
# # LEFT TOP TO BOTTOM
pygame.draw.line(
self._VARS['surf'], BLACK,
(cont_x, cont_y),
(cont_x, cont_y + CONTAINER_WIDTH_HEIGHT[0]), self._VARS['lineWidth'])
# # RIGHT TOP TO BOTTOM
pygame.draw.line(
self._VARS['surf'], BLACK,
(CONTAINER_WIDTH_HEIGHT[1] + cont_x, cont_y),
(CONTAINER_WIDTH_HEIGHT[1] + cont_x,
CONTAINER_WIDTH_HEIGHT[0] + cont_y), self._VARS['lineWidth'])
# Get cell size, just one since its a square grid.
cellSizeX = CONTAINER_WIDTH_HEIGHT[0]/cellsX
cellSizeY = CONTAINER_WIDTH_HEIGHT[1]/cellsY
for x in range(cellsY):
pygame.draw.line(
self._VARS['surf'], BLACK,
(cont_x + (cellSizeX * x), cont_y),
(cont_x + (cellSizeX * x), CONTAINER_WIDTH_HEIGHT[0] + cont_y), 2)
for y in range(cellsX):
# # HORIZONTAl DIVISIONS
pygame.draw.line(
self._VARS['surf'], BLACK,
(cont_x, cont_y + (cellSizeY*y)),
(cont_x + CONTAINER_WIDTH_HEIGHT[1], cont_y + (cellSizeY*y)), 2)
pygame.display.update()
def placeCells(self, Assignment_matrix, iteration_number=0):
celldimX = (self._VARS['gridWH'][0]/self._VARS['gridCellsX'])
celldimY = (self._VARS['gridWH'][1]/self._VARS['gridCellsY'])
for row in range(self.Assignment_matrix.shape[0]):
for column in range(self.Assignment_matrix.shape[1]):
if (self.Assignment_matrix[row][column] == self.DroneNo):
self.drawSquareCell(
self._VARS['gridOrigin'][0] + (celldimX*column)
+ self._VARS['lineWidth']/2,
self._VARS['gridOrigin'][1] + (celldimY*row)
+ self._VARS['lineWidth']/2,
celldimX, celldimY, BLACK)
continue
for r in range(self.DroneNo):
if self.init_robot_pos[r] == (row, column):
self.drawSquareCell(
self._VARS['gridOrigin'][0] + (celldimX * column)
+ self._VARS['lineWidth'] / 2,
self._VARS['gridOrigin'][1] + (celldimY * row)
+ self._VARS['lineWidth'] / 2,
celldimX, celldimY, self.init_robot_pos_colors[r])
continue
else:
if self.Assignment_matrix[row][column] == r:
self.drawSquareCell(
self._VARS['gridOrigin'][0] + (celldimX*column)
+ self._VARS['lineWidth']/2,
self._VARS['gridOrigin'][1] + (celldimY*row)
+ self._VARS['lineWidth']/2,
celldimX, celldimY, self.color[r])
self.drawSquareGrid(self._VARS['gridOrigin'], self._VARS['gridWH'],
self._VARS['gridCellsX'], self._VARS['gridCellsY'])
pygame.display.set_caption('Assignment Matrix [Iteration: ' + str(iteration_number) + ']')
pygame.display.update()
def drawSquareCell(self, x, y, dimX, dimY, color):
pygame.draw.rect(
self._VARS['surf'], color,
(x, y, dimX, dimY)
)