-
Notifications
You must be signed in to change notification settings - Fork 6
/
utils.py
165 lines (135 loc) · 4.3 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
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
import numpy as np
import time
import os
from matplotlib import pyplot as plt
from matplotlib.transforms import Bbox
def tic():
return time.time()
def toc(tic):
return time.time() - tic
def f_arr(xs, th_deg=False):
xs_str = [f'{x:.3f}' for x in xs]
if th_deg:
xs_str[-1] += '°'
return '[' + ','.join(xs_str) + ']'
def radarImgPathToTimestamp(radarImgPath):
"""
eg: radarImgPathToTimestamp('data\\tiny\\radar\\1547131046353776.png') -> 1547131046353776
"""
return int(os.path.basename(radarImgPath)[:-4])
def normalize_angles(th):
"""
Normalize an angle to be between -pi and pi
"""
return (th + np.pi) % (2 * np.pi) - np.pi
def getRotationMatrix(th, degrees=False):
if degrees:
th = np.deg2rad(th)
cth = np.cos(th)
sth = np.sin(th)
R = np.array([[cth, -sth], [sth, cth]])
return R
def convertPoseToTransform(poses):
'''
@param[in] poses np.ndarray of (3,) or (N x 3)
@return pose_transforms np.ndarray of (3 x 3) or (N x 3 x 3)
'''
if type(poses) == list:
poses = np.array(poses)
single = False
if len(poses.shape) == 1:
single = True
poses = np.expand_dims(poses, axis=0)
xs = poses[:, 0]
ys = poses[:, 1]
ths = poses[:, 2]
cths = np.cos(ths)
sths = np.sin(ths)
pose_transform = np.zeros((len(ths), 3, 3))
pose_transform[:, 0, 0] = cths
pose_transform[:, 0, 1] = -sths
pose_transform[:, 1, 0] = sths
pose_transform[:, 1, 1] = cths
pose_transform[:, 0, 2] = xs
pose_transform[:, 1, 2] = ys
pose_transform[:, 2, 2] = 1
if single:
pose_transform = pose_transform[0, :, :]
return pose_transform
def convertTransformToPose(pose_transforms):
'''
@param[in] pose_transforms np.ndarray of (3 x 3) or (N x 3 x 3)
@return poses np.ndarray of (3,) or (N x 3)
'''
if type(pose_transforms) == list:
pose_transforms = np.array(pose_transforms)
single = False
if len(pose_transforms.shape) == 2:
single = True
pose_transforms = np.expand_dims(pose_transforms, axis=0)
ths = np.arctan2(pose_transforms[:, 1, 0], pose_transforms[:, 0, 0])
xs = pose_transforms[:, 0, 2]
ys = pose_transforms[:, 1, 2]
pose_transforms = np.stack([xs, ys, ths], axis=1)
if single:
pose_transforms = pose_transforms[0, :]
return pose_transforms
def flatten(x):
return x.reshape(x.shape[0])
def convertRandHtoDeltas(R, h):
dx = float(*h[0])
dy = float(*h[1])
dth = np.arctan2(R[1, 0], R[0, 0])
return np.array([dx, dy, dth])
def quiver(poses, c='r', label=None):
poses = np.array(poses)
plt.quiver(
poses[:, 0],
poses[:, 1],
np.cos(poses[:, 2]),
np.sin(poses[:, 2]),
color=c,
width=0.02,
scale=10,
alpha=.5,
label=label
)
def plt_full_extent(ax, pad=0.0):
"""
@brief Get the full extent of a plt axes, including axes labels, tick labels, and titles.
"""
# For text objects, we need to draw the figure first, otherwise the extents
# are undefined.
ax.figure.canvas.draw()
items = ax.get_xticklabels() + ax.get_yticklabels()
items += [ax, ax.title, ax.xaxis.label, ax.yaxis.label]
bbox = Bbox.union([item.get_window_extent() for item in items])
return bbox.expanded(1.0 + pad, 1.0 + pad)
def plt_savefig_by_axis(filePath, fig, ax, pad=0.0):
'''
@brief Save a plt figure by extent of its axis (allows us to save subplots)
@param[in] filePath Path to save figure to
@param[in] fig Overall figure containing axis
@param[in] ax Axis to save
'''
extent = plt_full_extent(ax, pad).transformed(fig.dpi_scale_trans.inverted())
# extent = ax.get_tightbbox(fig.canvas.get_renderer())
fig.savefig(filePath, bbox_inches=extent)
def invert_transform(T):
theta = np.arctan2(T[1, 0], T[0, 0])
x = T[0, 2]
y = T[1, 2]
c = np.cos(theta)
s = np.sin(theta)
T_inv = np.array([[ c, s, -s * y - c * x],
[-s, c, -c * y + s * x],
[ 0, 0, 1]])
return T_inv
def homogenize(points):
result = None
if points.shape[1] == 2:
ones = np.ones((points.shape[0], 1))
result = np.concatenate((points, ones) , axis = 1)
else:
result = points.copy()
return result