-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathutils.py
71 lines (51 loc) · 1.47 KB
/
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
70
71
import math
import png
import numpy
import matplotlib.pyplot as plt
def dist2d(point1, point2):
"""
Euclidean distance between two points
:param point1:
:param point2:
:return:
"""
x1, y1 = point1[0:2]
x2, y2 = point2[0:2]
dist2 = (x1 - x2)**2 + (y1 - y2)**2
return math.sqrt(dist2)
def png_to_ogm(filename, normalized=False, origin='lower'):
"""
Convert a png image to occupancy data.
:param filename: the image filename
:param normalized: whether the data should be normalised, i.e. to be in value range [0, 1]
:param origin:
:return:
"""
r = png.Reader(filename)
img = r.read()
img_data = list(img[2])
out_img = []
bitdepth = img[3]['bitdepth']
for i in range(len(img_data)):
out_img_row = []
for j in range(len(img_data[0])):
if j % img[3]['planes'] == 0:
if normalized:
out_img_row.append(img_data[i][j]*1.0/(2**bitdepth))
else:
out_img_row.append(img_data[i][j])
out_img.append(out_img_row)
if origin == 'lower':
out_img.reverse()
return out_img
def plot_path(path):
start_x, start_y = path[0]
goal_x, goal_y = path[-1]
# plot path
path_arr = numpy.array(path)
plt.plot(path_arr[:, 0], path_arr[:, 1], 'y')
# plot start point
plt.plot(start_x, start_y, 'ro')
# plot goal point
plt.plot(goal_x, goal_y, 'go')
plt.show()