i
i
i
i
i
i
i
i
4.3. Quaternions 77
To have a numerically stable routine [1177], divisions by small numbers
should be avoided. Therefore, first set t = q
2
w
−q
2
x
−q
2
y
−q
2
z
,fromwhichit
follows that
m
00
= t +2q
2
x
,
m
11
= t +2q
2
y
,
m
22
= t +2q
2
z
,
u = m
00
+ m
11
+ m
22
= t +2q
2
w
,
(4.47)
which in turn implies that the largest of m
00
, m
11
, m
22
,andu determine
which of q
x
, q
y
, q
z
,andq
w
is largest. If q
w
is largest, then Equation 4.46 is
used to derive the quaternion. Otherwise, we note that the following holds:
4q
2
x
=+m
00
− m
11
− m
22
+ m
33
,
4q
2
y
= −m
00
+ m
11
− m
22
+ m
33
,
4q
2
z
= −m
00
− m
11
+ m
22
+ m
33
,
4q
2
w
=tr(M
q
).
(4.48)
The appropriate equation of the ones above is then used to compute the
largest of q
x
, q
y
,andq
z
, after which Equation 4.44 is used to calculate the
remaining components of
ˆ
q. Luckily, there is code for this—see the Further
Reading and Resources at the end of this chapter.
Spherical Linear Interpolation
Spherical linear interpolation is an operation that, given two unit quater-
nions,
ˆ
q and
ˆ
r, and a parameter t ∈ [0, 1], computes an interpolated quater-
nion. This is useful for animating objects, for example. It is not as useful
for interpolating camera orientations, as the camera’s “up” vector can be-
come tilted during the interpolation, usually a disturbing effect [349].
The algebraic form of this operation is expressed by the composite
quaternion,
ˆ
s,below:
ˆ
s(
ˆ
q,
ˆ
r,t)=(
ˆ
r
ˆ
q
−1
)
t
ˆ
q. (4.49)
However, for software implementations, the following form, where slerp
stands for spherical linear interpolation, is much more appropriate:
ˆ
s(
ˆ
q,
ˆ
r,t)=slerp(
ˆ
q,
ˆ
r,t)=
sin(φ(1 − t))
sin φ
ˆ
q +
sin(φt)
sin φ
ˆ
r. (4.50)
To compute φ, which is needed in this equation, the following fact can
be used: cosφ = q
x
r
x
+ q
y
r
y
+ q
z
r
z
+ q
w
r
w
[224]. For t ∈ [0, 1], the
slerp function computes (unique
11
) interpolated quaternions that together
11
If and only if
ˆ
q and
ˆ
r are not opposite.