-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathquat.py
434 lines (359 loc) · 14.6 KB
/
quat.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
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
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
# quat.py
# This module defines an entirely new number: the dual quaternion.
# Since a normal quaternion is just a dual quaternion with a zero
# idempotent part, I have chosen to define a single class---quat---and
# that will act as both quaternion and dual quaternion.
# Observe two things, though:
# First, I tried to keep small numbers as "zeros"
# by rounding divisions (see __div__ and norm) to
# five significant digits. So if a number is one
# like 5.2e-6, it will be rounded to 0.
# To allow for experimentation, though, I decided to
# change the roundoff to ROUNDOFF, set in vertex.
# Second, to make sure that division works
# appropriately, I initialized the original quaternion
# with float(etc).
from math import sqrt, acos
import exceptions
#from mm import multimethod
from vertex import *
from matrix import *
class DivideByPureDualException(exceptions.Exception):
def __init__(self):
return
def __str__(self):
print "","Attempt to find Inverse for Pure Dual Quaternion."
class DualDistanceException(exceptions.Exception):
def __init__(self):
return
def __str__(self):
print "","Attempt to find dist() for Pure Dual Quaternion."
class quat(object):
#@multimethod(quat, float, vector, float, vector)
def __init__(self, r=0, I=Zero, er=0, eI=Zero):
# This initialization uses vectors rather than numbers.
# This initializes to 0.
self.r = float(r)
self.I = I
self.er = float(er)
self.eI = eI
#@multimethod(quat, float, float, float, float, float,
# float, float, float)
#def __init__(self, r, i, j, k, er, ei, ej, ek):
def real_quat(self, r, i, j, k, er, ei, ej, ek):
# This is an initialization using numbers.
self.r = float(r)
self.I = vector(i, j, k)
# Alternate to using vector definitions:
#self.i = float(i)
#self.j = float(j)
#self.k = float(k)
self.er = float(er)
self.eI = vector(ei, ej, ek)
# Alternate to using vector definitions:
#self.ei = float(ei)
#self.ej = float(ej)
#self.ek = float(ek)
# Note that this will be the only function that shows this
# alternative, unless I decide to switch to this alternative.
#@multimethod(quat, int, vector)
#def __init__(self, beta, r):
def rotation(self, beta, r):
# This initialization creates a unit quaternion.
self.r = tcos(beta/2)
self.I = r.norm()*tsin(beta/2)
self.er = float(0)
self.eI = Zero
#@multimethod(quat, vector)
#def __init__(self, t):
def vector(self, t):
# This initialization creates a unit dual quaternion.
# As __init__, it is meant to be used for two purposes:
# to multiply vectors as dual quaternions;
# and to initialize traslation dual quaternions.
# For the latter purpose, it's important to remember to
# divide the vector by two before initializing the
# quaternion.
self.r = float(1)
self.I = Zero
self.er = float(0)
self.eI = t
def pureVector(self, t):
"""This sets the quat to be a pure quaternion with a
vector value of t; this is useful if all we want to do with
a given vector is rotate it."""
self.r = float(0)
self.I = t
self.er = float(0)
self.eI = Zero
def extractVector(self):
"""When we translate and/or rotate a vector, we are going
to want to get the new vector information somehow. This
function returns the vector from a unit dual quaternion.
Recall that a vector as a dual quaternion has the form
1 + eI.dot(v) where I = (i, j, k) and e**2==0."""
return self.eI
def extractPureVector(self):
"""When we only rotate a vector, we are going
to want to get the new vector information somehow. This
function returns the vector from a pure vector quaternion.
Recall that a vector as a pure quaternion has the form
I.dot(v) where I = (i, j, k)."""
return self.I
def translation(self, t):
# This initialization creates a unit dual quaternion
# for translations.
self.r = float(1)
self.I = Zero
self.er = float(0)
self.eI = t/2
def purequat(self):
# We'll check to see if each of these items are non-zero.
# If any one of them is non-zero, then this is not a pure
# quaternion.
return not(self.er != 0 or self.eI.x != 0 or
self.eI.y != 0 or self.eI.z != 0)
def puredual(self):
# We'll check to see if each of these items are non-zero
# If any one of them is non-zero, then this is not a purely
# dual quaternion.
return not(self.r != 0 or self.I.x != 0 or
self.I.y != 0 or self.I.z != 0)
def puretrans(self):
# This identifies a dual quaternion that is purely a translation.
return (self.r == 1 and self.I.x == 0 and self.I.y == 0
and self.I.z == 0 and self.er == 0)
def __add__(self, v):
r = self.r+v.r
I = self.I+v.I
er = self.er+v.er
eI = self.eI+v.eI
return quat(r, I, er, eI)
def __sub__(self, v):
r = self.r-v.r
I = self.I-v.I
er = self.er-v.er
eI = self.eI-v.eI
return quat(r, I, er, eI)
def __neg__(self):
return quat(-self.r, -self.I, -self.er, -self.eI)
def __mul__(self, q):
if isinstance(q, quat):
# Do quaternion multiplication
if self.purequat() and q.purequat():
r = round(q.r*self.r - self.I.dot(q.I), ROUNDOFF)
I = q.I*self.r + self.I*q.r + self.I.cross(q.I)
return quat(r, I)
else:
r = round(self.r*q.r - self.I.dot(q.I), ROUNDOFF)
I = q.I*self.r + self.I*q.r + self.I.cross(q.I)
er = round((self.r*q.er - self.I.dot(q.eI)) + \
(self.er*q.r - self.eI.dot(q.I)), ROUNDOFF)
eI = (q.eI*self.r + self.I*q.er+self.I.cross(q.eI) + \
q.I*self.er + self.eI*q.r + self.eI.cross(q.I))
return quat(r, I, er, eI)
else:
# Do scalar multiplication
r = round(self.r*q, ROUNDOFF)
I = self.I*q
er = round(self.er*q, ROUNDOFF)
eI = self.eI*q
return quat(r, I, er, eI)
def conj(self):
# Quaternionic conjugation
return quat(self.r, -self.I, self.er, -self.eI)
def pconj(self):
# Pure Quaternionic conjugation
return quat(self.r, -self.I)
def econj(self):
# Dual quaternionic conjugation
return quat(self.r, self.I, -self.er, -self.eI)
def bconj(self):
# Quaternionic and Dual Quaternionic conjugation together
return quat(self.r, -self.I, -self.er, self.eI)
def dot(self, v):
# I'm not sure if this makes sense for dual quaternions.
return round(self.r*v.r + self.I.dot(v.I) + self.er*v.er \
+ self.eI.dot(v.eI), ROUNDOFF)
def dist(self):
if self.purequat():
return round(sqrt(self.r*self.r + self.I.dot(self.I)), \
ROUNDOFF)
else:
r = round(sqrt(self.r*self.r + self.I.dot(self.I)), \
ROUNDOFF)
if r == 0:
raise DualDistanceException
dot = float(self.I.dot(self.eI))
if dot == 0:
return round(r, ROUNDOFF)
else:
return quat(round(r, ROUNDOFF), Zero, \
round(dot/r, ROUNDOFF), Zero)
def norm(self):
d = self.dist()
if self.purequat():
return quat(self.r/d, self.I/d)
else:
return quat(self.r/d, self.I/d, self.er/d, self.eI/d)
def inverse(self):
if self.purequat():
# Pure quaternion inverse: this is rather easy!
return self.pconj() / sqrt(self.r*self.r + self.I.dot(self.I))
else:
# Dual quaternionic inverse: a little more challenging.
if self.puredual():
# If the non-idempotent part is 0, we can't find
# the inverse.
raise DivideByPureDualException
else:
# the inverse is 1/q_0 - e(q_e/((q_0)^2) --- yikes!
r_sq = self.r*self.r
I_dot = self.I.dot(self.I)
t = 1.0/(r_sq + I_dot)
new_r = r_sq - I_dot
new_I = self.I*(1.0/(new_r*new_r + 4*r_sq*I_dot))
er = -self.er*new_r - self.eI.dot(new_I)
eI = -(new_I*self.er + self.eI*new_r + self.eI.cross(new_I))
return quat(self.r*t, -self.I*t, round(er, ROUNDOFF), eI)
def __div__(self, q):
# Theoretically, I ought to repeat the above, simplified, to
# streamline execution.
# For now, I am NOT going to do that!
if isinstance(q, quat):
return (self*q.inverse())
else:
r = round(self.r/q, ROUNDOFF)
I = self.I/q
er = round(self.er/q, ROUNDOFF)
eI = self.I/q
return quat(r, I, er, eI)
def normalize(self):
self = self.norm()
def matrix(self):
# Here are a few calculations that will be used to
# convert our dual quaternion to a matrix!
# To speed things up, we'll recognize three types of
# matrices.
# Note that, theoretically, we should normalize the
# dual quaternion first.
if self.purequat():
# This is the rotation quaternion...
w = self.r # = tcos(beta/2)
x = self.I.x # = tsin(beta/2)*axis.x
y = self.I.y # = tsin(beta/2)*axis.y
z = self.I.z # = tsin(beta/2)*axis.z
# Now we'll pull out the translation information...
xx2 = 2*x*x; yy2 = 2*y*y; zz2 = 2*z*z
wx2 = 2*w*x; wy2 = 2*w*y; wz2 = 2*w*z
xy2 = 2*x*y; xz2 = 2*x*z; yz2 = 2*y*z
quat_mx = matrix()
quat_mx.mx = [ [1-yy2-zz2, xy2+wz2, xz2-wy2, 0],
[xy2-wz2, 1-xx2-zz2, yz2+wx2, 0],
[xz2+wy2, yz2-wx2, 1-xx2-yy2, 0],
[0, 0, 0, 1] ]
return quat_mx
elif self.puretrans():
# This is a purely translational dual quaternion.
quat_mx = matrix()
quat_mx.mx = [ [1, 0, 0, 2*self.eI.x],
[0, 1, 0, 2*self.eI.y],
[0, 0, 1, 2*self.eI.z],
[0, 0, 0, 1] ]
return quat_mx
else:
# This is the rotation quaternion...
w = self.r # = tcos(beta/2)
x = self.I.x # = tsin(beta/2)*axis.x
y = self.I.y # = tsin(beta/2)*axis.y
z = self.I.z # = tsin(beta/2)*axis.z
# Now we'll pull out the translation information...
ew = self.er
ex = self.eI.x
ey = self.eI.y
ez = self.eI.z
t = 2*(-ew*x + ex*w - ey*z + ez*y)
u = 2*(-ew*y + ex*z + ey*w - ez*x)
v = 2*(-ew*z - ex*y + ey*x + ez*w)
xx2 = 2*x*x; yy2 = 2*y*y; zz2 = 2*z*z
wx2 = 2*w*x; wy2 = 2*w*y; wz2 = 2*w*z
xy2 = 2*x*y; xz2 = 2*x*z; yz2 = 2*y*z
quat_mx = matrix()
quat_mx.mx = [ [1-yy2-zz2, xy2+wz2, xz2-wy2, t],
[xy2-wz2, 1-xx2-zz2, yz2+wx2, u],
[xz2+wy2, yz2-wx2, 1-xx2-yy2, v],
[0, 0, 0, 1] ]
return quat_mx
def __str__(self):
return " %s + I %s + e(%s + I %s)" % (self.r, self.I, \
self.er, self.eI)
def get_angle_axis(self):
# This returns (beta, axis) from a quaternion. Note that
# this assumes that the quaternion is pure; this function
# should probably throw an exception if it isn't pure.
# Note that we convert from radians to bradians.
beta = int(round(2*acos(self.r)*128/pi))
if beta == 0:
# In this case, we have an "identity" rotation;
# thus, we could use any vector we would like.
# Here, we'll default to the Up vector.
v = Up
else:
sin = tsin(beta/2)
if sin == 0:
# If beta is 1 or -1, then it will be a valid rotation;
# in this case, we'll approximate the sine.
sin = tsin(beta)
x = self.I.x/sin
y = self.I.y/sin
z = self.I.z/sin
v = vector(x, y, z)
v = v.norm()
return (beta, v)
def get_translation(self):
"""This returns the translation vector from a translation
quaternion. It assumes that the dual quaternion is purely
translation. If it isn't, I should probably throw an
exception."""
return vector(2*self.eI.x, 2*self.eI.y, 2*self.eI.z)
def get_angle_axis_translation(self):
""" This returns the angle-axis and tranlation information
from a dual quaternion; this does NOT assume that this is
purely a translation or a quaternion.
Every dual quaternion represents a combination of rotation
and translation; in thinking about the relationships of
these two, I have been able to come up with this."""
# First, we get the individual information; I use quaternions
# for both for efficiency reasons. (It takes less adds and
# mults to multiply pure quaternions.
rotation = quat(self.r, self.I) # this is the rotation information
translation = quat(self.er, self.eI) # This is ALMOST
# the translation info
# Now, we'll remove the rotation info from the
# translation portion.
translation = translation*rotation.inverse()
beta, axis = rotation.get_angle_axis()
pos = vector(translation.I.x*2, translation.I.y*2, \
translation.I.z*2)
return beta, axis, pos
# Here are a few quaternion constants that are nice to
# define: in particular, note that [Left, Up, Fwd]
# is a left-hand coord system, while [Right, Up, Fwd]
# represents a right-hand one.
BetaRight = [0, 64, 128, 192]
Identity = quat(1, Zero)
XTrans = quat(); XTrans.translation(vector(100, 0, 0))
YTrans = quat(); YTrans.translation(vector(0, 100, 0))
ZTrans = quat(); ZTrans.translation(vector(0, 0, 100))
XRot0 = quat(); XRot0.rotation(BetaRight[0], vector(1,0, 0))
XRot64 = quat(); XRot64.rotation(BetaRight[1], vector(1,0, 0))
XRot128 = quat(); XRot128.rotation(BetaRight[2], vector(1,0, 0))
XRot192 = quat(); XRot192.rotation(BetaRight[3], vector(1,0, 0))
YRot0 = quat(); YRot0.rotation(BetaRight[0], vector(0,1, 0))
YRot64 = quat(); YRot64.rotation(BetaRight[1], vector(0,1, 0))
YRot128 = quat(); YRot128.rotation(BetaRight[2], vector(0,1, 0))
YRot192 = quat(); YRot192.rotation(BetaRight[3], vector(0,1, 0))
ZRot0 = quat(); ZRot0.rotation(BetaRight[0], vector(0, 0, 1))
ZRot64 = quat(); ZRot64.rotation(BetaRight[1], vector(0, 0, 1))
ZRot128 = quat(); ZRot128.rotation(BetaRight[2], vector(0, 0, 1))
ZRot192 = quat(); ZRot192.rotation(BetaRight[3], vector(0, 0, 1))