cs482: interactive computer graphics quaternion rotation min...

39
Min H. Kim (KAIST) CS482: Interactive Computer Graphics CS482: Interactive Computer Graphics Quaternion Rotation Min H. Kim KAIST School of Computing

Upload: others

Post on 09-Jul-2020

15 views

Category:

Documents


1 download

TRANSCRIPT

Min H. Kim (KAIST) CS482: Interactive Computer Graphics

CS482: Interactive Computer GraphicsQuaternion Rotation

Min H. KimKAIST School of Computing

Min H. Kim (KAIST) CS482: Interactive Computer Graphics

QUATERNION ROTATIONChapter 7

2

Min H. Kim (KAIST) CS482: Interactive Computer Graphics

Mental model: Quaternion• Why do we need quaternions?• How can we multiply two vectors

?

3

!c

!b

!a

!b = !c!a!c =!b!a−1

suchthat!aand!c areorthogonalandthat

!b and!c areorthogonal.

!a and

!b , resultingin!c

Min H. Kim (KAIST) CS482: Interactive Computer Graphics

Motivation• For animation, we will want to interpolate

between frames in a natural way.• We will study quaternions as alternative to

rotation matrices:

• Later we will add back in the translations

4

R = r 00 1

⎣⎢

⎦⎥

Min H. Kim (KAIST) CS482: Interactive Computer Graphics

RECAP: 3D Rotation • Every rotation fixes an axis of rotation and

rotates by some angle about that axis.• Rotation around the z axis:

5

b1

b2

b3⎡

⎣⎤⎦

xyz

⎢⎢⎢

⎥⎥⎥

⇒ b1

b2

b3⎡

⎣⎤⎦

cosθ −sinθ 0sinθ cosθ 00 0 1

⎢⎢⎢

⎥⎥⎥

xyz

⎢⎢⎢

⎥⎥⎥

Min H. Kim (KAIST) CS482: Interactive Computer Graphics

RECAP: 3D Rotation• Rotation around the x axis

• Rotation around the y axis

6

1 0 00 cosθ −sinθ0 sinθ cosθ

⎢⎢⎢

⎥⎥⎥

cosθ 0 sinθ0 1 0

−sinθ 0 cosθ

⎢⎢⎢

⎥⎥⎥

Min H. Kim (KAIST) CS482: Interactive Computer Graphics

RECAP: xyz-Euler angle rotation• Axis of rotation

• xyz-Euler angle rotation matrix

where7

kx2v + c kxkyv − kzs kxkzv + kys

kykxv + kzs ky2v + c kykzv − kxs

kzkxv − kys kzkyv + kxs kz2v + c

⎢⎢⎢⎢

⎥⎥⎥⎥

k= kx ,ky ,kz⎡⎣ ⎤⎦

t

c := cosθ , s := sinθ , v := 1− c.

Min H. Kim (KAIST) CS482: Interactive Computer Graphics

Euler Rotations Problems• Axis-angle

• Euler rotations: sequentially applying axis-angles along the x, y and z axes.

• Problems: – Changing axes– Gimbal lock problem!– Rotation order (local transformation)

8

Min H. Kim (KAIST) CS482: Interactive Computer Graphics

Invariance of a rotation• Invariance is a property of a class of mathematical

objects that remains unchanged whentransformations of a certain type are applied to the objects.

• An object flying through space with no forces acting on it has its center of mass follow a straight line.

• Its orientation spins along a fixed axis.• This kind of orientation satisfies

both left and right invariance.

9

ω0 c0 o0t = wt R0

Min H. Kim (KAIST) CS482: Interactive Computer Graphics

What if having more than two rotations?

Interpolation of rotations:• Desired object frame rotation

for “time=0” : • Desired object frame rotation

for “time=1” : • We wish to find a sequence of frames

for , that naturally rotates fromto

10

o0t = wt R0

o1t = wt R1

oαt

α ∈[0...1] o0t

o1t

ω0 c0ω1

c1 o0t = wt R0

o1t = wt R1

Min H. Kim (KAIST) CS482: Interactive Computer Graphics

Bad ideas 1• Linear interpolation of matrices

and then set • Each basis vector simply moves along a straight

line• In this case, the intermediate are not

rotation matrices

11

Rα := (1−α )R0 + (α )R1 !oαt = !wt Rα

Min H. Kim (KAIST) CS482: Interactive Computer Graphics

Bad idea 2• Factor both and into 3 two-dimensional

rotations, so-called XYZ Euler angles

• These three scalar values could each be linearly interpolated using and used to generate intermediate rotations

12

R0 R1

kx2v + c kxkyv − kzs kxkzv + kys

kykxv + kzs ky2v + c kykzv − kxs

kzkxv − kys kzkyv + kxs kz2v + c

⎢⎢⎢⎢

⎥⎥⎥⎥

α

Min H. Kim (KAIST) CS482: Interactive Computer Graphics

• Not natural,• Not invariant to choice of the world frame.• The rotation frame keeps changing (not global

transformation)• This is called left invariance

• We need an intrinsic geometric operation (describable independent of coordinates)

Bad idea 2

13

Min H. Kim (KAIST) CS482: Interactive Computer Graphics

What we want• Like to first create a single transition matrix• This matrix can, as any rotation matrix, be

thought of as a rotation of some degreesabout some axis

• Suppose we had a power operator:– which gave us a rotation about

by degrees instead.

• Then we could setand set

14

R1R0−1

θ[kx ,ky ,kz ]

t

(R1R0−1)α

[kx ,ky ,kz ]t

αθRα := (R1R0

−1)α R0

oαt = wt Rα

oαt = wt (R1R0

−1)α R0

Min H. Kim (KAIST) CS482: Interactive Computer Graphics

Result• This is a sequence of frames obtained by more and

more rotation about a single axis– Read right to left

• Correct start and finish:

• The transition rotation fixes a unique axis• This axis depends only on and . Not any

choice of world frame.• Up to cycles (which we can uniquify soon). This

gives us a unique interpolation15

wt (R1R0

−1)0R0 =wt R0 =

o0 wt (R1R0

−1)1R0 =wt R1 =

o1

o0

o1

Min H. Kim (KAIST) CS482: Interactive Computer Graphics

Result

16

Min H. Kim (KAIST) CS482: Interactive Computer Graphics

Hard part• Hard part: factor into its axis/angle form• Main quaternion idea: is to keep track of the

axis and angle at all times, but in a way that allows our manipulations.

• This will allow us to do this interpolation• It also could help in general with avoiding

numerical drift away from RBTs.

17

R1R0−1

Min H. Kim (KAIST) CS482: Interactive Computer Graphics

The representation• A quaternion is 4 tuple with operations• Written:

where is a scalar, andis a coordinate vector in 3D.

• A rotation of degree about a unit length axisis presented as .

• Oddity: the division by 2 will be needed to make the operations work out as needed

• Because we are rotating not in 3D, but in 4D,we just rotate only half way through!

18

ωc

⎣⎢

⎦⎥

ωc

θk

ω c

cos θ2

⎛⎝⎜

⎞⎠⎟

sin θ2

⎛⎝⎜

⎞⎠⎟ k

⎢⎢⎢⎢⎢

⎥⎥⎥⎥⎥

θ

Min H. Kim (KAIST) CS482: Interactive Computer Graphics

Antipodes of quaternion (diametrically opposite to it)

• Note that a rotation of degrees about the axis gives us the same quaternion.

• A rotation of degrees about an axis also gives us the same quaternion

• A rotation of degrees about an axis ,which in fact is the same rotation, gives us the negated quaternion

• So antipodes represent the same rotation transformation– But heads up regarding cycles and power

19

−θ−k

θ + 4π k

θ + 2π k

12θ + 2π( )

[−ω ,−c]

[ω ,c]

Min H. Kim (KAIST) CS482: Interactive Computer Graphics

Example code• If you want to make a theta degree-rotation

quaternion, we need to make theta/2 quaternion.

• (later, we will multiply it twice in a forward/backward manner)

20

static Quat makeXRotation(const double ang) {Quat r;const double h = 0.5 * ang * PI/180;r.q_[1] = std::sin(h);r.q_[0] = std::cos(h);return r;

}

Min H. Kim (KAIST) CS482: Interactive Computer Graphics

Cycles

21

R1R0−1

θ + n2π n

nθ + n2π

• matrix can, be thought of as a rotation of some degrees for any integer .

• Not relevant for linear transformation on vectors, but is relevant for interpolation

• The natural choice is to choose such that is minimal.

• This might result in a negative rotational angle (the other way).

Min H. Kim (KAIST) CS482: Interactive Computer Graphics

Unit norm quats. == rotations• Squared norm is sum of 4 squares.• Any quaternion of the form

has a unit form• Conversely, any such unit norm quaternion can

be interpreted (along with its negation) as a unique rotation matrix.

22

ωc

⎣⎢

⎦⎥

1= ω2+ x2+ y2+z2 , c =[x , y ,z]t

cos θ2⎛

⎝⎜⎞

⎠⎟

sin θ2⎛

⎝⎜⎞

⎠⎟k

⎢⎢⎢⎢⎢

⎥⎥⎥⎥⎥

=

cos θ2⎛

⎝⎜⎞

⎠⎟

sin θ2⎛

⎝⎜⎞

⎠⎟k x

sin θ2⎛

⎝⎜⎞

⎠⎟k y

sin θ2⎛

⎝⎜⎞

⎠⎟kz

⎢⎢⎢⎢⎢⎢⎢⎢⎢⎢⎢⎢

⎥⎥⎥⎥⎥⎥⎥⎥⎥⎥⎥⎥ k =1

Min H. Kim (KAIST) CS482: Interactive Computer Graphics

Operations• Quat * quat multiply

• Where and are the dot and cross product on 3 dimensional coordinate vectors.

• Correctly models (rotation matrix) * (rotation matrix) multiplications

• Example:

23

ω1

c1

⎣⎢⎢

⎦⎥⎥

ω 2

c2

⎣⎢⎢

⎦⎥⎥=

(ω1ω 2 − c1 ⋅ c2 )(ω1c2 +ω 2c1 + c1 × c2 )

⎣⎢⎢

⎦⎥⎥

⋅ ×

M = Matrix4::makeXRotation(-dy) * Matrix4::makeYRotation(dx);

Min H. Kim (KAIST) CS482: Interactive Computer Graphics

Unit norm quats• Identity rotation example

• Flip rotation example

• Unit quaternion multiplication

24

10

⎣⎢⎢

⎦⎥⎥, −1

0

⎣⎢⎢

⎦⎥⎥

0k

⎣⎢⎢

⎦⎥⎥, 0

− k

⎣⎢⎢

⎦⎥⎥

0c1

⎣⎢⎢

⎦⎥⎥

0c2

⎣⎢⎢

⎦⎥⎥=

−c1 ⋅ c2c1 × c2

⎣⎢⎢

⎦⎥⎥

k1 ⋅ k2k1 × k2

⎢⎢

⎥⎥=

0k2

⎣⎢⎢

⎦⎥⎥

0−k1

⎣⎢⎢

⎦⎥⎥

Min H. Kim (KAIST) CS482: Interactive Computer Graphics

Operations• scalar * quat multiply

• Quaternions q and –q are the same rotation!!!• Unit quaternion multiplicative inverse

(conjugate)

25

α ω

c

⎣⎢

⎦⎥= αω

αc

⎣⎢

⎦⎥, example)-1 ω

c

⎣⎢

⎦⎥= −ω

−c

⎣⎢

⎦⎥

cos θ2

⎛⎝⎜

⎞⎠⎟

sin θ2

⎛⎝⎜

⎞⎠⎟k

⎢⎢⎢⎢⎢

⎥⎥⎥⎥⎥

−1

=cos θ

2⎛⎝⎜

⎞⎠⎟

−sin θ2

⎛⎝⎜

⎞⎠⎟k

⎢⎢⎢⎢⎢

⎥⎥⎥⎥⎥

, 10

⎣⎢

⎦⎥ =

cos θ2

⎛⎝⎜

⎞⎠⎟

sin θ2

⎛⎝⎜

⎞⎠⎟k

⎢⎢⎢⎢⎢

⎥⎥⎥⎥⎥

cos θ2

⎛⎝⎜

⎞⎠⎟

sin θ2

⎛⎝⎜

⎞⎠⎟k

⎢⎢⎢⎢⎢

⎥⎥⎥⎥⎥

−1

Min H. Kim (KAIST) CS482: Interactive Computer Graphics

RECAP: Affine rotation transformation

• Rotate a vector by an affine transformation for rotation:

• Start with 4-coordinate vector

• Left multiply it by a 4 by 4 rotation matrixto get:

• With result of from

26

c = c 1⎡⎣ ⎤⎦t

R′c = Rc

′c = ˆ′c 1⎡⎣ ⎤⎦t

Min H. Kim (KAIST) CS482: Interactive Computer Graphics

Rotate a vector by a unit quaternion• Let q be represented with the unit norm

quaternion:

• Conjugate of quaternion q

• Use a vector Cvec3 to create the non-unit norm quaternion

27

q=cos θ2

⎝⎜⎞

⎠⎟

sin θ2⎛

⎝⎜⎞

⎠⎟k

⎢⎢⎢⎢⎢

⎥⎥⎥⎥⎥

v = 0

c

⎣⎢

⎦⎥

c

q−1 =cos θ2

⎝⎜⎞

⎠⎟

sin θ2⎛

⎝⎜⎞

⎠⎟k

⎢⎢⎢⎢⎢

⎥⎥⎥⎥⎥

−1

=

cos θ2⎛

⎝⎜⎞

⎠⎟

−sin θ2⎛

⎝⎜⎞

⎠⎟k

⎢⎢⎢⎢⎢

⎥⎥⎥⎥⎥

Min H. Kim (KAIST) CS482: Interactive Computer Graphics

Rotate a vector by a unit quaternion• Perform the following triple quaternion multiplication:

• We need inv(q) additionally, such that q and v are not orthogonal!• If q and v are orthogonal, v’=qv.• If we just multiply q with v, qv cannot become a vector (the first

element will be non-zero). The additional right multiplication of inverse allows us to get a rotated vector.

• But we will write this in code as: cvec = quat * cvec28

0ˆ ʹc

⎣⎢

⎦⎥=

cos θ2⎛

⎝⎜⎞

⎠⎟

sin θ2⎛

⎝⎜⎞

⎠⎟k

⎢⎢⎢⎢⎢

⎥⎥⎥⎥⎥

0c

⎣⎢

⎦⎥

cos θ2⎛

⎝⎜⎞

⎠⎟

sin θ2⎛

⎝⎜⎞

⎠⎟k

⎢⎢⎢⎢⎢

⎥⎥⎥⎥⎥

−1

v '=qvq−1v

q v '

ω1

c1

⎢⎢

⎥⎥

ω2

c2

⎢⎢

⎥⎥=

(ω1ω2− c1 ⋅ c2)(ω1c2+ω2c1+ c1× c2)

⎢⎢

⎥⎥

In quaternion space

Min H. Kim (KAIST) CS482: Interactive Computer Graphics

Example code

29

Cvec4 operator * (const Cvec4& a) const {const Quat r = *this * (Quat(0, a[0], a[1], a[2]) * inv(*this));return Cvec4(r[1], r[2], r[3], a[3]);

}

Min H. Kim (KAIST) CS482: Interactive Computer Graphics

Quaternions summary• Expression:• Multiplication rules:• A conjugate (inverse) of a quaternion:

• unit quaternion:

• A quaternion v, a vecter in 3D when w=0:

• Product of the vector with the unit quaternion:30

q =ω + xi+ yj+ zki2 = j2 = k2 = ijk = −1

q*=ω − xi− yj− zk, q−1 = q*q 2

q 2= qq*= q*q =1, 1=ω 2 + x2 + y2 + z2

v = v1i+ v2 j+ v3k

v 'q = qv ⇒ v ' = qvq−1

Min H. Kim (KAIST) CS482: Interactive Computer Graphics

Quaternions interpolation• To interpolate between two frames related to world

frame by and • And suppose that these two matrices corresponds

to the two quaternions:

• Linear interpolation (LERP):– Simple, efficient approximation

• Spherical linear interpolation (SLERP):– More accurate way

31

R0 R1

cos θ02

⎛⎝⎜

⎞⎠⎟

sin θ02

⎛⎝⎜

⎞⎠⎟ k0

⎢⎢⎢⎢⎢

⎥⎥⎥⎥⎥

,cos θ1

2⎛⎝⎜

⎞⎠⎟

sin θ12

⎛⎝⎜

⎞⎠⎟ k1

⎢⎢⎢⎢⎢

⎥⎥⎥⎥⎥

Min H. Kim (KAIST) CS482: Interactive Computer Graphics

LERP (Linear Interpolation)• An even easier hack is to do 4D Linear intERPolation

(LERP) and renormalization in quaternion space

32

p = a+α(b−a)p = a+α!vp = (1−α)a+αb

a bp

α (1-α) !v

(1−α )cos θ0

2⎛⎝⎜

⎞⎠⎟

sin θ02

⎛⎝⎜

⎞⎠⎟ k0

⎢⎢⎢⎢⎢

⎥⎥⎥⎥⎥

+αcos θ1

2⎛⎝⎜

⎞⎠⎟

sin θ12

⎛⎝⎜

⎞⎠⎟ k1

⎢⎢⎢⎢⎢

⎥⎥⎥⎥⎥

Min H. Kim (KAIST) CS482: Interactive Computer Graphics

LERPing• Both left and right invariant.• More efficient approximation• Useful for blending n different

rotations.

• NB this interpolant is no longer a unit norm quaternion, it shouldbe normalized after calculation.

33

(1−α )cos θ0

2⎛⎝⎜

⎞⎠⎟

sin θ02

⎛⎝⎜

⎞⎠⎟ k0

⎢⎢⎢⎢⎢

⎥⎥⎥⎥⎥

+αcos θ1

2⎛⎝⎜

⎞⎠⎟

sin θ12

⎛⎝⎜

⎞⎠⎟ k1

⎢⎢⎢⎢⎢

⎥⎥⎥⎥⎥

Min H. Kim (KAIST) CS482: Interactive Computer Graphics

SLERP (Spherical Linear Interpolation)

• This is called Spherical Linear intERPolation(SLERP) or just slerping since it happens to match moving on a great circle in quaternion space

• Power-based SLERP

34

Rα :=(R1R0−1)αR0

Min H. Kim (KAIST) CS482: Interactive Computer Graphics

Power-based SLERP• Linear

interpolation

• Spherical Linear Interpolation in quaternion space

35

p = !vq = rq−1q

!v =cos θ

2⎛

⎝⎜⎞

⎠⎟

sin θ2⎛

⎝⎜⎞

⎠⎟k

⎢⎢⎢⎢⎢

⎥⎥⎥⎥⎥

α!v =α θ

2⎛

⎝⎜⎞

⎠⎟k =

αθ2

⎝⎜

⎠⎟k

p = a +α (b − a)p = a +α !vp = (1−α )a +αb

a bp

α (1-α) !v

!v = rq−1q r

!v

(1-α)

p = !vαq = rq−1( )α q

!vα =cos αθ

2⎛

⎝⎜

⎠⎟

sin αθ2

⎝⎜

⎠⎟k

⎢⎢⎢⎢⎢

⎥⎥⎥⎥⎥

Min H. Kim (KAIST) CS482: Interactive Computer Graphics

Power-based SLERP• First, extract the unit axis by normalizing the

three last entries of the quaternion.• Define

• So we get a unique value and thus a unique

36

k

θ / 2∈ −π ...π[ ]θ ∈ −2π ...2π[ ]

cos θ2

⎛⎝⎜

⎞⎠⎟

sin θ2

⎛⎝⎜

⎞⎠⎟ k

⎢⎢⎢⎢⎢

⎥⎥⎥⎥⎥

α

=cos αθ

2⎛⎝⎜

⎞⎠⎟

sin αθ2

⎛⎝⎜

⎞⎠⎟ k

⎢⎢⎢⎢⎢

⎥⎥⎥⎥⎥

As goes from 0 to 1, we get a series of rotations with angles going between 0 and

α

θ

Min H. Kim (KAIST) CS482: Interactive Computer Graphics

Power-based SLERP• But what if the transition quaternion

presents a of more than 180( ) degrees :

• In particular, if then – So would go more than 180 degrees which we

don’t want during interpolation

• In this case, suppose we had swapped to the antipode before calling power

• Then , we get – And thus

37

cos θ2

⎛⎝⎜

⎞⎠⎟

sin θ2

⎛⎝⎜

⎞⎠⎟ k

⎢⎢⎢⎢⎢

⎥⎥⎥⎥⎥

θ

cos θ2

⎛⎝⎜

⎞⎠⎟ < 0 θ ∈ π ...2π[ ]

αθ

cos θ2

⎛⎝⎜

⎞⎠⎟ > 0 θ / 2∈ −π / 2...π / 2[ ]θ ∈ −π ...π[ ]

π

ω c

Min H. Kim (KAIST) CS482: Interactive Computer Graphics

Power-based SLERP• In order to select “the short interpolation” of less

than 180 degrees, • When we interpolate, before calling the power

operator, we first check the sign of the first coordinate, and conditionally negate the quaternion.

• We call this the conditional negation operator• Quaternions q and –q are the same rotation!!!• Finally, we output:

38

cn(): −1 ω

c

⎣⎢

⎦⎥= −ω

−c

⎣⎢

⎦⎥

cncos θ1

2⎛⎝⎜

⎞⎠⎟

sin θ12

⎛⎝⎜

⎞⎠⎟ k1

⎢⎢⎢⎢⎢

⎥⎥⎥⎥⎥

cos θ02

⎛⎝⎜

⎞⎠⎟

sin θ02

⎛⎝⎜

⎞⎠⎟ k0

⎢⎢⎢⎢⎢

⎥⎥⎥⎥⎥

−1⎛

⎜⎜⎜⎜⎜

⎟⎟⎟⎟⎟

⎜⎜⎜⎜⎜

⎟⎟⎟⎟⎟

α

cos θ02

⎛⎝⎜

⎞⎠⎟

sin θ02

⎛⎝⎜

⎞⎠⎟ k0

⎢⎢⎢⎢⎢

⎥⎥⎥⎥⎥

[−ω ,−c]

[ω ,c]

Min H. Kim (KAIST) CS482: Interactive Computer Graphics

Sphere-based SLERPing• In any dimension n, a trigonometric argument

can be used to show that spherical linear interpolation between any two unit vectors in , can be calculated as:

39

sin[(1−α )Ω]sin(Ω)

!v0 +sin[αΩ]sin(Ω)

!v1

whereΩ= cos−1(!v0 ⋅!v1)

sin[(1−α )Ω]sin(Ω)

cos θ02

⎛⎝⎜

⎞⎠⎟

sin θ02

⎛⎝⎜

⎞⎠⎟ k0

⎢⎢⎢⎢⎢

⎥⎥⎥⎥⎥

+ sin[αΩ]sin(Ω)

cos θ12

⎛⎝⎜

⎞⎠⎟

sin θ12

⎛⎝⎜

⎞⎠⎟ k1

⎢⎢⎢⎢⎢

⎥⎥⎥⎥⎥

!n