-
Notifications
You must be signed in to change notification settings - Fork 14
/
pose_utils.py
75 lines (46 loc) · 1.57 KB
/
pose_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
import torch
import numpy as np
def quat_to_euler(q, is_degree=False):
w, x, y, z = q[0], q[1], q[2], q[3]
t0 = +2.0 * (w * x + y * z)
t1 = +1.0 - 2.0 * (x * x + y * y)
roll = np.arctan2(t0, t1)
t2 = +2.0 * (w * y - z * x)
t2 = +1.0 if t2 > +1.0 else t2
t2 = -1.0 if t2 < -1.0 else t2
pitch = np.arcsin(t2)
t3 = +2.0 * (w * z + x * y)
t4 = +1.0 - 2.0 * (y * y + z * z)
yaw = np.arctan2(t3, t4)
if is_degree:
roll = np.rad2deg(roll)
pitch = np.rad2deg(pitch)
yaw = np.rad2deg(yaw)
return np.array([roll, pitch, yaw])
def array_dist(pred, target):
return np.linalg.norm(pred - target, 2)
def position_dist(pred, target):
return np.linalg.norm(pred-target, 2)
def rotation_dist(pred, target):
pred = quat_to_euler(pred)
target = quat_to_euler(target)
return np.linalg.norm(pred-target, 2)
def fit_gaussian(pose_quat):
# pose_quat = pose_quat.detach().cpu().numpy()
num_data, _ = pose_quat.shape
# Convert quat to euler
pose_euler = []
for i in range(0, num_data):
pose = pose_quat[i, :3]
quat = pose_quat[i, 3:]
euler = quat_to_euler(quat)
pose_euler.append(np.concatenate((pose, euler)))
# Calculate mean and variance
pose_mean = np.mean(pose_euler, axis=0)
mat_var = np.zeros((6, 6))
for i in range(0, num_data):
pose_diff = pose_euler[i] - pose_mean
mat_var += pose_diff * np.transpose(pose_diff)
mat_var = mat_var / num_data
pose_var = mat_var.diagonal()
return pose_mean, pose_var