-
Notifications
You must be signed in to change notification settings - Fork 0
/
reachability_map.py
103 lines (72 loc) · 2.9 KB
/
reachability_map.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
#!/usr/bin/env python
import sys
from Queue import Queue
import numpy as np
import matplotlib.pyplot as plt
from slam_map import SlamMap
class ReachabilityMap:
""" Reachability map
given a slam map finds all the places that are reachable from a set position inside the map
"""
def __init__(self, slam_map):
# constants
self.start_x = 45
self.start_y = 30
self.kernel_size = 2 # size from edge to center of kernel
self.impassability_cutoff = 70
# variables
self.map = None
# build the map
self._build_map(slam_map)
def _build_map(self, slam_map):
""" build map
inputs:
slam_map - completed slam map
"""
# set up
kernel_size = self.kernel_size
s_map = slam_map.map
reachability_map = np.zeros_like(s_map, dtype=int)
queue = Queue()
queue.put((self.start_x, self.start_y))
while queue.empty() is False:
# get next element to check
current_point = queue.get()
invalid = False
# check if this element is valid
for i in range(current_point[0] - kernel_size, current_point[0] + kernel_size + 1):
for j in range(current_point[1] - kernel_size, current_point[1] + kernel_size + 1):
# check if this index is valid
if i > 0 and i < s_map.shape[0] and j > 0 and j < s_map.shape[1]:
# check if this index violates the cutoff for being impassable
if s_map[i, j] > self.impassability_cutoff:
invalid = True
else:
invalid = True
if invalid is True:
continue
# element is valid
# update new values
for i in range(current_point[0] - kernel_size, current_point[0] + kernel_size + 1):
for j in range(current_point[1] - kernel_size, current_point[1] + kernel_size + 1):
# check if this index is valid
if i > 0 and i < reachability_map.shape[0] and j > 0 and j < reachability_map.shape[1]:
# add to queue if not already
if reachability_map[i, j] == 0:
queue.put((i, j))
# update value
reachability_map[i, j] = 1
self.map = reachability_map
def display_as_heatmap(self):
plt.imshow(np.transpose(self.map), cmap='hot', interpolation='nearest')
# flip y axis
axis = plt.gca()
axis.set_ylim(axis.get_ylim()[::-1])
plt.show()
def main():
slam_map_filepath = sys.argv[1]
slam_map = SlamMap(slam_map_filepath)
reachability_map = ReachabilityMap(slam_map)
reachability_map.display_as_heatmap()
if __name__ == "__main__":
main()