forked from rai-opensource/spatialmath-python
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDualQuaternion.py
More file actions
352 lines (260 loc) · 10.4 KB
/
Copy pathDualQuaternion.py
File metadata and controls
352 lines (260 loc) · 10.4 KB
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
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
import numpy as np
from spatialmath import Quaternion, UnitQuaternion, SE3
from spatialmath import base
# TODO scalar multiplication
class DualQuaternion:
r"""
A dual number is an ordered pair :math:`\hat{a} = (a, b)` or written as
:math:`a + \epsilon b` where :math:`\epsilon^2 = 0`.
A dual quaternion can be considered as either:
- a quaternion with dual numbers as coefficients
- a dual of quaternions, written as an ordered pair of quaternions
The latter form is used here.
:References:
- http://web.cs.iastate.edu/~cs577/handouts/dual-quaternion.pdf
- https://en.wikipedia.org/wiki/Dual_quaternion
.. warning:: Unlike the other spatial math classes, this class does not
(yet) support multiple values per object.
:seealso: :func:`UnitDualQuaternion`
"""
def __init__(self, real=None, dual=None):
"""
Construct a new dual quaternion
:param real: real quaternion
:type real: Quaternion or UnitQuaternion
:param dual: dual quaternion
:type dual: Quaternion or UnitQuaternion
:raises ValueError: incorrect parameters
Example:
.. runblock:: pycon
>>> from spatialmath import DualQuaternion, Quaternion
>>> d = DualQuaternion(Quaternion([1,2,3,4]), Quaternion([5,6,7,8]))
>>> print(d)
>>> d = DualQuaternion([1, 2, 3, 4, 5, 6, 7, 8])
>>> print(d)
The dual number is stored internally as two quaternion, respectively
called ``real`` and ``dual``.
"""
if real is None and dual is None:
self.real = None
self.dual = None
return
elif dual is None and base.isvector(real, 8):
self.real = Quaternion(real[0:4])
self.dual = Quaternion(real[4:8])
elif real is not None and dual is not None:
if not isinstance(real, Quaternion):
raise ValueError('real part must be a Quaternion subclass')
if not isinstance(dual, Quaternion):
raise ValueError('real part must be a Quaternion subclass')
self.real = real # quaternion, real part
self.dual = dual # quaternion, dual part
else:
raise ValueError('expecting zero or two parameters')
@classmethod
def Pure(cls, x):
x = base.getvector(x, 3)
return cls(UnitQuaternion(), Quaternion.Pure(x))
def __repr__(self):
return str(self)
def __str__(self):
"""
String representation of dual quaternion
:return: compact string representation
:rtype: str
Example:
.. runblock:: pycon
>>> from spatialmath import DualQuaternion, Quaternion
>>> d = DualQuaternion(Quaternion([1,2,3,4]), Quaternion([5,6,7,8]))
>>> str(d)
"""
return str(self.real) + " + ε " + str(self.dual)
def norm(self):
"""
Norm of a dual quaternion
:return: Norm as a dual number
:rtype: 2-tuple
The norm of a ``UnitDualQuaternion`` is unity, represented by the dual
number (1,0).
Example:
.. runblock:: pycon
>>> from spatialmath import DualQuaternion, Quaternion
>>> d = DualQuaternion(Quaternion([1,2,3,4]), Quaternion([5,6,7,8]))
>>> d.norm() # norm is a dual number
"""
a = self.real * self.real.conj()
b = self.real * self.dual.conj() + self.dual * self.real.conj()
return (base.sqrt(a.s), base.sqrt(b.s))
def conj(self):
r"""
Conjugate of dual quaternion
:return: Conjugate
:rtype: DualQuaternion
There are several conjugates defined for a dual quaternion. This one
mirrors conjugation for a regular quaternion. For the dual quaternion
:math:`(p, q)` it returns :math:`(p^*, q^*)`.
Example:
.. runblock:: pycon
>>> from spatialmath import DualQuaternion, Quaternion
>>> d = DualQuaternion(Quaternion([1,2,3,4]), Quaternion([5,6,7,8]))
>>> d.conj()
"""
return DualQuaternion(self.real.conj(), self.dual.conj())
def __add__(left, right): # lgtm[py/not-named-self] pylint: disable=no-self-argument
"""
Sum of two dual quaternions
:return: Product
:rtype: DualQuaternion
Example:
.. runblock:: pycon
>>> from spatialmath import DualQuaternion, Quaternion
>>> d = DualQuaternion(Quaternion([1,2,3,4]), Quaternion([5,6,7,8]))
>>> d + d
"""
return DualQuaternion(left.real + right.real, left.dual + right.dual)
def __sub__(left, right): # lgtm[py/not-named-self] pylint: disable=no-self-argument
"""
Difference of two dual quaternions
:return: Product
:rtype: DualQuaternion
Example:
.. runblock:: pycon
>>> from spatialmath import DualQuaternion, Quaternion
>>> d = DualQuaternion(Quaternion([1,2,3,4]), Quaternion([5,6,7,8]))
>>> d - d
"""
return DualQuaternion(left.real - right.real, left.dual - right.dual)
def __mul__(left, right): # lgtm[py/not-named-self] pylint: disable=no-self-argument
"""
Product of dual quaternion
- ``dq1 * dq2`` is a dual quaternion representing the product of
``dq1`` and ``dq2``. If both are unit dual quaternions, the product
will be a unit dual quaternion.
- ``dq * p`` transforms the point ``p`` (3) by the unit dual quaternion
``dq``.
Example:
.. runblock:: pycon
>>> from spatialmath import DualQuaternion, Quaternion
>>> d = DualQuaternion(Quaternion([1,2,3,4]), Quaternion([5,6,7,8]))
>>> d * d
"""
if isinstance(right, DualQuaternion):
real = left.real * right.real
dual = left.real * right.dual + left.dual * right.real
if isinstance(left, UnitDualQuaternion) and isinstance(left, UnitDualQuaternion):
return UnitDualQuaternion(real, dual)
else:
return DualQuaternion(real, dual)
elif isinstance(left, UnitDualQuaternion) and base.isvector(right, 3):
v = base.getvector(right, 3)
vp = left * DualQuaternion.Pure(v) * left.conj()
return vp.dual.v
def matrix(self):
"""
Dual quaternion as a matrix
:return: Matrix represensation
:rtype: ndarray(8,8)
Dual quaternion multiplication can also be written as a matrix-vector
product.
Example:
.. runblock:: pycon
>>> from spatialmath import DualQuaternion, Quaternion
>>> d = DualQuaternion(Quaternion([1,2,3,4]), Quaternion([5,6,7,8]))
>>> d.matrix()
>>> d.matrix() @ d.vec
>>> d * d
"""
return np.block([
[self.real.matrix, np.zeros((4,4))],
[self.dual.matrix, self.real.matrix]
])
@property
def vec(self):
"""
Dual quaternion as a vector
:return: Vector represensation
:rtype: ndarray(8)
Example:
.. runblock:: pycon
>>> from spatialmath import DualQuaternion, Quaternion
>>> d = DualQuaternion(Quaternion([1,2,3,4]), Quaternion([5,6,7,8]))
>>> d.vec
"""
return np.r_[self.real.vec, self.dual.vec]
# def log(self):
# pass
class UnitDualQuaternion(DualQuaternion):
"""[summary]
:param DualQuaternion: [description]
:type DualQuaternion: [type]
.. warning:: Unlike the other spatial math classes, this class does not
(yet) support multiple values per object.
:seealso: :func:`UnitDualQuaternion`
"""
def __init__(self, real=None, dual=None):
r"""
Create new unit dual quaternion
:param real: real quaternion or SE(3) matrix
:type real: Quaternion, UnitQuaternion or SE3
:param dual: dual quaternion
:type dual: Quaternion or UnitQuaternion
- ``UnitDualQuaternion(real, dual)`` is a new unit dual quaternion with
real and dual parts as specified.
- ``UnitDualQuaternion(T)`` is a new unit dual quaternion equivalent to
the rigid-body motion described by the SE3 value ``T``.
Example:
.. runblock:: pycon
>>> from spatialmath import UnitDualQuaternion, SE3
>>> T = SE3.Rand()
>>> print(T)
>>> d = UnitDualQuaternion(T)
>>> print(d)
>>> type(d)
>>> print(d.norm()) # norm is (1, 0)
The dual number is stored internally as two quaternion, respectively
called ``real`` and ``dual``. For a unit dual quaternion they are
respectively:
.. math::
\q_r &\sim \mat{R}
q_d &= \frac{1}{2} q_t \q_r
where :math:`\mat{R}` is the rotational part of the rigid-body motion
and :math:`q_t` is a pure quaternion formed from the translational part
:math:`t`.
"""
if real is None and dual is None:
self.real = None
self.dual = None
return
elif real is not None and dual is not None:
self.real = real # quaternion, real part
self.dual = dual # quaternion, dual part
elif dual is None and isinstance(real, SE3):
T = real
S = UnitQuaternion(T.R)
D = Quaternion.Pure(T.t)
self.real = S
self.dual = 0.5 * D * S
def SE3(self):
"""
Convert unit dual quaternion to SE(3) matrix
:return: SE(3) matrix
:rtype: SE3
Example:
.. runblock:: pycon
>>> from spatialmath import DualQuaternion, SE3
>>> T = SE3.Rand()
>>> print(T)
>>> d = UnitDualQuaternion(T)
>>> print(d)
>>> print(d.T)
"""
R = base.q2r(self.real.A)
t = 2 * self.dual * self.real.conj()
return SE3(base.rt2tr(R, t.v))
# def exp(self):
# w = self.real.v
# v = self.dual.v
# theta = base.norm(w)
if __name__ == "__main__": # pragma: no cover
import pathlib
exec(open(pathlib.Path(__file__).parent.parent.absolute() / "tests" / "test_dualquaternion.py").read()) # pylint: disable=exec-used