-
Notifications
You must be signed in to change notification settings - Fork 0
/
visual_utils.py
69 lines (65 loc) · 2.27 KB
/
visual_utils.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
def draw_3d_bounding_box(array, vertices_pos2d):
# Shows which verticies that are connected so that we can draw lines between them
# The key of the dictionary is the index in the bbox array, and the corresponding value is a list of indices
# referring to the same array.
vertex_graph = {0: [1, 2, 4],
1: [0, 3, 5],
2: [0, 3, 6],
3: [1, 2, 7],
4: [0, 5, 6],
5: [1, 4, 7],
6: [2, 4, 7]}
# Note that this can be sped up by not drawing duplicate lines
for vertex_idx in vertex_graph:
neighbour_idxs = vertex_graph[vertex_idx]
from_pos2d = vertices_pos2d[vertex_idx]
for neighbour_idx in neighbour_idxs:
to_pos2d = vertices_pos2d[neighbour_idx]
if from_pos2d is None or to_pos2d is None:
continue
y1, x1 = from_pos2d[0], from_pos2d[1]
y2, x2 = to_pos2d[0], to_pos2d[1]
# Only stop drawing lines if both are outside
if not point_in_canvas((y1, x1)) and not point_in_canvas((y2, x2)):
continue
for x, y in get_line(x1, y1, x2, y2):
if point_in_canvas((y, x)):
array[int(y), int(x)] = (255, 0, 0)
def get_line(x1, y1, x2, y2):
x1, y1, x2, y2 = int(x1), int(y1), int(x2), int(y2)
# print("Calculating line from {},{} to {},{}".format(x1,y1,x2,y2))
points = []
issteep = abs(y2 - y1) > abs(x2 - x1)
if issteep:
x1, y1 = y1, x1
x2, y2 = y2, x2
rev = False
if x1 > x2:
x1, x2 = x2, x1
y1, y2 = y2, y1
rev = True
deltax = x2 - x1
deltay = abs(y2 - y1)
error = int(deltax / 2)
y = y1
if y1 < y2:
ystep = 1
else:
ystep = -1
for x in range(x1, x2 + 1):
if issteep:
points.append((y, x))
else:
points.append((x, y))
error -= deltay
if error < 0:
y += ystep
error += deltax
# Reverse the list if the coordinates were reversed
if rev:
points.reverse()
return points
def point_in_canvas(pos):
if (pos[0] >= 0) and (pos[0] < 360) and (pos[1] >= 0) and (pos[1] < 720):
return True
return False