22
CS 450: COMPUTER GRAPHICS QUATERNIONS SPRING 2015 DR. MICHAEL J. REALE

CS 450: COMPUTER GRAPHICS QUATERNIONS SPRING 2015 DR. MICHAEL J. REALE

Embed Size (px)

Citation preview

CS 450: COMPUTER GRAPHICS

QUATERNIONSSPRING 2015

DR. MICHAEL J. REALE

INTRODUCTION

• Quaternions invented by Sir William Rowan Hamilton in 1843

• Developed as extension to complex numbers

• Introduced into computer graphics by Ken Shoemake in 1985

• Very good for rotation transformations better than Euler angles because:

• Straightforward to convert to and from quaternions

• Stable and constant interpolation of orientations

http://commons.wikimedia.org/wiki/File%3AWilliam_Rowan_Hamilton_portrait_oval_combined.png

COMPLEX NUMBERS

• Complex number = 2 part real + imaginary part

• A + iB

• Imaginary number i = sqrt(-1)

SO WHAT IS A QUATERNION?

• Quaternion = has 4 parts

• First 3 parts imaginary vector (each component uses a different imaginary number)

• Last part real part of quaternion

• In practice, stored as a 4D vector of real numbers (but usually put little hat on name)

kjiij

jikki

ikjjk

kji

qqqkqjqiqq

qqqkqjqiqqqq

zyxzyxv

wvwzyxwv

1

),,(

),(ˆ

222

NOTES ABOUT QUATERNIONS

• Imaginary part qv can be treated like an ordinary vector

• So we can do vector addition, dot product, cross product, etc.

• Multiplying the imaginary units (i,j,k) is NONCOMMUTATIVE ORDER MATTERS!

• Note: looks like what happens when you use the cross product (e.g., (y axis) x (z axis) = (x axis))

kjiij

jikki

ikjjk

kji

1222

),,( zyxzyxv qqqkqjqiqq

MULTIPLYING QUATERNIONS

• Remember: parts of each quaternion are added together, BUT can’t actually add them because of the imaginary terms

) ,(

)(

)(

)(

))((ˆˆ

vvwwvwvwvv

zzyyxxww

zwzwxyyx

ywywzxxz

xwxwyzzy

wzyxwzyx

rqrqrqqrrq

rqrqrqrq

rqqrrqrqk

rqqrrqrqj

rqqrrqrqi

rkrjrirqkqjqiqrq

kjiij

jikki

ikjjk

kji

1222

Recall:

RULES OF QUATERNIONS

• Addition:

• Just add imaginary and real parts separately

• Conjugate:

• Negate imaginary parts; leave real part alone

• Norm:

• Imaginary parts cancel out, leaving only the real parts

• Identity:

),(),(),(ˆˆ wwvvwvwv rqrqrrqqrq

),(),(ˆ **wvwv qqqqq

)()( * ibaiba

Conjugate forregular imaginary numbers:

22222** ˆˆˆˆ)ˆ(ˆ wzyxwvv qqqqqqqqqqqqnq

)1,0(ˆ

i

INVERSE OF QUATERNIONS

• The inverse must have the following property:

• We know the following to be true from the definition of the norm:

• Thus, the inverse is given by:

1ˆˆˆˆ 11 qqqq

1)ˆ(

ˆˆˆˆ)ˆ(

2

**2

qn

qqqqqn

*ˆˆ)ˆ(ˆ qqqnq

Norm:

*2

1 ˆ)ˆ(

1ˆ q

qnq

SCALAR MULTIPLICATION

• To multiply a scalar s by a quaternion:

• Also, it is commutative:

),()0,00(),)(,0(ˆ wvvwwvvwv sqsqqsqqsqqqqsqs

),(ˆˆ wv sqsqsqqs

UNIT QUATERNION

• Unit quaternion

• Another way to write this is:

• This works because:

• …only works if:

1)ˆ( such that ),(ˆ qnqqq wv

1such that vector 3D a is where

cos)(sin)cos,)((sinˆ

qq

qq

uu

uuq

1cossincos))((sin)cos,)((sin)ˆ( 2222 qqq uuunqn

21)( qqq uuu

EULER’S FORMULA

• Turns out that sine, cosine, i, and e are all related by Euler’s Formula:

• The equivalent for quaternions is:

• We’ll use this later…

iei sincos

Aside: when φ = π, getEuler’s Identity!

01ie

quq euq cos)(sinˆ

QUATERNION TRANSFORMS

• Unit quaternion can represent ANY 3D rotation!

• Rotates points around axis uq by an angle 2φ

• Advantages:

• Extremely compact and simple

• A lot easier to interpolate between orientations

• Converting to/from quaternions straightforward

cos)(sinˆ quq

USING QUATERNION TRANSFORMS

• Assume we have a point we want to transform as a 4D homogeneous vector p

• Put p into a quaternion

• The following rotates p around axis uq by an angle 2φ:

• Moreover, since we’re dealing with unit quaternions:

• So, the final transformed point is given by:

1ˆˆˆ qpq

**2

1 ˆˆ)ˆ(

1ˆ qq

qnq

*ˆˆˆ qpq

MULTIPLE ROTATIONS

• One important rule of the quaternion conjugate:

• So, to apply two rotations, q and then r:

****** ˆˆˆ)ˆˆ(ˆ)ˆˆ()ˆˆ(ˆ)ˆˆ(ˆ)ˆˆˆ(ˆ cpcqrpqrrqpqrrqpqr

*** ˆˆ)ˆˆ( rqqr

CONVERT TO A MATRIX

• The following gives you the corresponding rotation matrix:

• Where:

• Once you have the quaternion, no trigonometric functions need to be computed!

1000

0)(1)()(

0)()(1)(

0)()()(1

22

22

22

yxxwzyywzw

xwzyzxzwyx

ywzxzwyxzy

qqsqqqqsqqqqs

qqqqsqqsqqqqs

qqqqsqqqqsqqs

M

)ˆ(

2

qns

CONVERT FROM A MATRIX• If qw is not very small:

• Otherwise, have to find largest component using:

wz

wy

wxw

q

mmq

q

mmq

q

mmqMtrq

44

4)(

2

1

01102002

1221

2222

2221100

222

211

200

where

2

2

2

2

zyxw

w

z

y

x

qqqqt

qtmmmu

qtm

qtm

qtm

)(4

4

4

4

2

332211002

332211002

332211002

Mtrq

mmmmq

mmmmq

mmmmq

w

z

y

x

Compute largest using:

zw

yw

xw

qqmm

qqmm

qqmm

4

4

4

0110

2002

1221

Compute the

rest:

SPHERICAL LINEAR INTERPOLATION

• Spherical Linear Interpolation = given two unit quaternions, q and r, and a parameter t ([0,1]), computes an interpolated quaternion

• Useful for animation

• Less useful for camera orientations can tilt camera’s “up” vector

• Mathematically expressed as:qqrtrqs t ˆ)ˆˆ(),ˆ,ˆ(ˆ 1

)cos()sin(

cos)(sinˆ

tut

euq

q

tutq

t q

Power law for quaternions:

SLERP()

• Software implementation, slerp(), often uses this form:

• To get φ:

• There are, however, faster incremental approaches that avoid the trigonometric functions

rt

qt

trqslerptrqs ˆsin

)sin(ˆ

sin

))1(sin(),ˆ,ˆ(),ˆ,ˆ(ˆ

wwzzyyxx rqrqrqrq cos

INTERPOLATED QUATERNIONS

• Interpolation path is unique PROVIDED q and r are not exact opposites

• Shortest arc on 4D unit sphere

• Constant speed, zero acceleration geodesic curve

• NOTE: Interpolation q1 q3 is NOT the same path as q1 q2 q3 (even though the destination is the same)

• Also note: not smooth transition from q1 q2 to q2 q3

• Better to use a spline-based interpolation with quaternions (e.g., squad())

TRANSFORMING FROM ONE VECTOR TO ANOTHER

• You can also directly transform from one direction s to another direction t using the shortest path possible

• Normalize s and t

• Compute normalized rotation axis using cross product:

• Compute dot product:

• Get length of cross product:

• Quaternion we want:

• Using half-angle relations and some simplifications:

tstsu /)(

)2cos( tse

)2sin( ts

cos,)(sinˆ uq

2

)1(2),(

)1(2

ets

eq

TRANSFORMING FROM ONE VECTOR TO ANOTHER• The matrix for this kind of rotation can be efficiently computed without trigonometric functions:

evv

eh

tse

tsv

hvevvhvvvhv

vvhvhvevvhv

vvhvvvhvhve

tsRzxzyyzx

xzyyzyx

yzxzyxx

1

11

)2(sin

)2cos(1

)2cos(

1000

0

0

0

),(

2

2

2

2

TRANSFORMING FROM ONE VECTOR TO ANOTHER

• NOTE: Need to detect when s and t point in:

• Exactly same direction return identity matrix/quaternion

• Exactly opposite directions pick any axis of rotation