/
se3.py
241 lines (207 loc) · 8.44 KB
/
se3.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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
import sympy
import sys
import unittest
import sophus
import functools
class Se3:
""" 3 dimensional group of rigid body transformations """
def __init__(self, so3, t):
""" internally represented by a unit quaternion q and a translation
3-vector """
assert isinstance(so3, sophus.So3)
assert isinstance(t, sympy.Matrix)
assert t.shape == (3, 1), t.shape
self.so3 = so3
self.t = t
@staticmethod
def exp(v):
""" exponential map """
upsilon = v[0:3, :]
omega = sophus.Vector3(v[3], v[4], v[5])
so3 = sophus.So3.exp(omega)
Omega = sophus.So3.hat(omega)
Omega_sq = Omega * Omega
theta = sympy.sqrt(sophus.squared_norm(omega))
V = (sympy.Matrix.eye(3) +
(1 - sympy.cos(theta)) / (theta**2) * Omega +
(theta - sympy.sin(theta)) / (theta**3) * Omega_sq)
return Se3(so3, V * upsilon)
def log(self):
omega = self.so3.log()
theta = sympy.sqrt(sophus.squared_norm(omega))
Omega = sophus.So3.hat(omega)
half_theta = 0.5 * theta
V_inv = sympy.Matrix.eye(3) - 0.5 * Omega + (1 - theta * sympy.cos(
half_theta) / (2 * sympy.sin(half_theta))) / (theta * theta) *\
(Omega * Omega)
upsilon = V_inv * self.t
return upsilon.col_join(omega)
def __repr__(self):
return "Se3: [" + repr(self.so3) + " " + repr(self.t)
@staticmethod
def hat(v):
upsilon = sophus.Vector3(v[0], v[1], v[2])
omega = sophus.Vector3(v[3], v[4], v[5])
return sophus.So3.hat(omega).\
row_join(upsilon).\
col_join(sympy.Matrix.zeros(1, 4))
def matrix(self):
""" returns matrix representation """
R = self.so3.matrix()
return (R.row_join(self.t)).col_join(sympy.Matrix(1, 4, [0, 0, 0, 1]))
def __mul__(self, right):
""" left-multiplication
either rotation concatenation or point-transform """
if isinstance(right, sympy.Matrix):
assert right.shape == (3, 1), right.shape
return self.so3 * right + self.t
elif isinstance(right, Se3):
r = self.so3 * right.so3
t = self.t + self.so3 * right.t
return Se3(r, t)
assert False, "unsupported type: {0}".format(type(right))
def __getitem__(self, key):
""" We use the following convention [q0, q1, q2, q3, t0, t1, t2] """
assert (key >= 0 and key < 7)
if key < 4:
return self.so3[key]
else:
return self.t[key - 4]
@staticmethod
def calc_Dx_exp_x(x):
return sympy.Matrix(7, 6, lambda r, c:
sympy.diff(Se3.exp(x)[r], x[c]))
@staticmethod
def Dx_exp_x_at_0():
return sympy.Matrix([[0.0, 0.0, 0.0, 0.5, 0.0, 0.0],
[0.0, 0.0, 0.0, 0.0, 0.5, 0.0],
[0.0, 0.0, 0.0, 0.0, 0.0, 0.5],
[0.0, 0.0, 0.0, 0.0, 0.0, 0.0],
[1.0, 0.0, 0.0, 0.0, 0.0, 0.0],
[0.0, 1.0, 0.0, 0.0, 0.0, 0.0],
[0.0, 0.0, 1.0, 0.0, 0.0, 0.0]])
def calc_Dx_this_mul_exp_x_at_0(self, x):
v = Se3.exp(x)
return sympy.Matrix(7, 6, lambda r, c:
sympy.diff((self * Se3.exp(x))[r], x[c])). \
subs(x[0], 0).subs(x[1], 0).subs(x[2], 0).\
subs(x[3], 0).subs(x[4], 0).limit(x[5], 0)
@staticmethod
def calc_Dx_exp_x_at_0(x):
return Se3.calc_Dx_exp_x(x).subs(x[0], 0).subs(x[1], 0).subs(x[2], 0).\
subs(x[3], 0).subs(x[4], 0).limit(x[5], 0)
@staticmethod
def Dxi_x_matrix(x, i):
if i < 4:
return sophus.So3.Dxi_x_matrix(x, i).\
row_join(sympy.Matrix.zeros(3, 1)).\
col_join(sympy.Matrix.zeros(1, 4))
M = sympy.Matrix.zeros(4, 4)
M[i - 4, 3] = 1
return M
@staticmethod
def calc_Dxi_x_matrix(x, i):
return sympy.Matrix(4, 4, lambda r, c:
sympy.diff(x.matrix()[r, c], x[i]))
@staticmethod
def Dxi_exp_x_matrix(x, i):
T = Se3.exp(x)
Dx_exp_x = Se3.calc_Dx_exp_x(x)
l = [Dx_exp_x[j, i] * Se3.Dxi_x_matrix(T, j) for j in range(0, 7)]
return functools.reduce((lambda a, b: a + b), l)
@staticmethod
def calc_Dxi_exp_x_matrix(x, i):
return sympy.Matrix(4, 4, lambda r, c:
sympy.diff(Se3.exp(x).matrix()[r, c], x[i]))
@staticmethod
def Dxi_exp_x_matrix_at_0(i):
v = sophus.ZeroVector6()
v[i] = 1
return Se3.hat(v)
@staticmethod
def calc_Dxi_exp_x_matrix_at_0(x, i):
return sympy.Matrix(4, 4, lambda r, c:
sympy.diff(Se3.exp(x).matrix()[r, c], x[i])
).subs(x[0], 0).subs(x[1], 0).subs(x[2], 0).\
subs(x[3], 0).subs(x[4], 0).limit(x[5], 0)
class TestSe3(unittest.TestCase):
def setUp(self):
upsilon0, upsilon1, upsilon2, omega0, omega1, omega2 = sympy.symbols(
'upsilon[0], upsilon[1], upsilon[2], omega[0], omega[1], omega[2]',
real=True)
x, v0, v1, v2 = sympy.symbols('q.w() q.x() q.y() q.z()', real=True)
p0, p1, p2 = sympy.symbols('p0 p1 p2', real=True)
t0, t1, t2 = sympy.symbols('t[0] t[1] t[2]', real=True)
v = sophus.Vector3(v0, v1, v2)
self.upsilon_omega = sophus.Vector6(
upsilon0, upsilon1, upsilon2, omega0, omega1, omega2)
self.t = sophus.Vector3(t0, t1, t2)
self.a = Se3(sophus.So3(sophus.Quaternion(x, v)), self.t)
self.p = sophus.Vector3(p0, p1, p2)
def test_exp_log(self):
for v in [sophus.Vector6(0., 1, 0.5, 2., 1, 0.5),
sophus.Vector6(0.1, 0.1, 0.1, 0., 1, 0.5),
sophus.Vector6(0.01, 0.2, 0.03, 0.01, 0.2, 0.03)]:
w = Se3.exp(v).log()
for i in range(0, 3):
self.assertAlmostEqual(v[i], w[i])
def test_matrix(self):
T_foo_bar = Se3.exp(self.upsilon_omega)
Tmat_foo_bar = T_foo_bar.matrix()
point_bar = self.p
p1_foo = T_foo_bar * point_bar
p2_foo = sophus.proj(Tmat_foo_bar * sophus.unproj(point_bar))
self.assertEqual(sympy.simplify(p1_foo - p2_foo),
sophus.ZeroVector3())
def test_derivatives(self):
self.assertEqual(sympy.simplify(
Se3.calc_Dx_exp_x_at_0(self.upsilon_omega) -
Se3.Dx_exp_x_at_0()),
sympy.Matrix.zeros(7, 6))
for i in range(0, 7):
self.assertEqual(sympy.simplify(Se3.calc_Dxi_x_matrix(self.a, i) -
Se3.Dxi_x_matrix(self.a, i)),
sympy.Matrix.zeros(4, 4))
for i in range(0, 6):
self.assertEqual(sympy.simplify(
Se3.Dxi_exp_x_matrix(self.upsilon_omega, i) -
Se3.calc_Dxi_exp_x_matrix(self.upsilon_omega, i)),
sympy.Matrix.zeros(4, 4))
self.assertEqual(sympy.simplify(
Se3.Dxi_exp_x_matrix_at_0(i) -
Se3.calc_Dxi_exp_x_matrix_at_0(self.upsilon_omega, i)),
sympy.Matrix.zeros(4, 4))
def test_codegen(self):
stream = sophus.cse_codegen(self.a.calc_Dx_exp_x(self.upsilon_omega))
filename = "cpp_gencode/Se3_Dx_exp_x.cpp"
# set to true to generate codegen files
if True:
file = open(filename, "w")
for line in stream:
file.write(line)
file.close()
else:
file = open(filename, "r")
file_lines = file.readlines()
for i, line in enumerate(stream):
self.assertEqual(line, file_lines[i])
file.close()
stream.close
stream = sophus.cse_codegen(self.a.calc_Dx_this_mul_exp_x_at_0(
self.upsilon_omega))
filename = "cpp_gencode/Se3_Dx_this_mul_exp_x_at_0.cpp"
# set to true to generate codegen files
if True:
file = open(filename, "w")
for line in stream:
file.write(line)
file.close()
else:
file = open(filename, "r")
file_lines = file.readlines()
for i, line in enumerate(stream):
self.assertEqual(line, file_lines[i])
file.close()
stream.close
if __name__ == '__main__':
unittest.main()