forked from decentr-vslam/decentr-vslam
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathreadDecentrStateFromOptG2ofiles.py
101 lines (78 loc) · 2.42 KB
/
readDecentrStateFromOptG2ofiles.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
import re
import numpy as np
import IPython
def gtsamFrameIdToIndices(identity):
assert (type(identity) is int)
frame_i = np.mod(identity, 2**56)
robot_i = (identity - frame_i + 1) / 2**56 - 97 + 1
return robot_i, frame_i
def vec(v):
return v.reshape(-1,1)
def fixR(R):
if (np.abs(np.linalg.det(R)) - 1) > 1e-5:
assert(False)
[U,S,Vt] = np.linalg.svd(R)
V=Vt.T
S=np.diag(S)
corr = np.zeros((3,3))
for i in range(3):
corr += np.dot(vec(V[:,i]),vec(V[:,i]).T) / S[i,i]
return np.dot(R,corr)
def quat2rot(q):
"""QUAT2ROT - Transform quaternion into rotation matrix
Usage: R = quat2rot(q)
Input:
q - 4-by-1 quaternion, with form [w x y z], where w is the scalar term.
Output:
R - 3-by-3 Rotation matrix
"""
q = q / np.linalg.norm(q)
w = q[0]
x = q[1]
y = q[2]
z = q[3]
x2 = x * x
y2 = y * y
z2 = z * z
w2 = w * w
xy = 2 * x * y
xz = 2 * x * z
yz = 2 * y * z
wx = 2 * w * x
wy = 2 * w * y
wz = 2 * w * z
R = np.array([[w2 + x2 - y2 - z2, xy - wz, xz + wy],
[xy + wz, w2 - x2 + y2 - z2, yz - wx],
[xz - wy, yz + wx, w2 - x2 - y2 + z2]])
return R
def readDecentrStateFromOptG2oFiles(g2o_dir, decentr_state, suffix):
nr_robots = len(decentr_state)
for robot_i in range(nr_robots):
file_id = open(g2o_dir+'/' + str(robot_i) + suffix + '.g2o', 'r')
while True:
line = file_id.readline()[:-1]
if line.split(' ')!='VERTEX_SE3:QUAT':
break
head,frame_id,x,y,z,qx,qy,qz,qw = line.split(' ')
if head[:]!=head[:]:
break
frame_id = int(frame_id)
x=float(x)
y=float(y)
z=float(z)
qx=float(qx)
qy=float(qy)
qz=float(qz)
qw=float(qw)
[robot_i_val, frame_i] = gtsamFrameIdToIndices(frame_id)
assert (robot_i_val == robot_i)
Sim_W_C = np.eye(4)
Sim_W_C[:3,3] = np.array([x,y,z])
q = np.array([qw,qx,qy,qz])
if (abs(np.linalg.norm(q)-1)>1e-3):
print('Error: Quaternion has not unit norm')
else:
q = q/np.linalg.norm(q)
Sim_W_C[:3,:3] = fixR(quat2rot(q))
decentr_state[robot_i]["Sim_O_C"][frame_i] = Sim_W_C
return decentr_state