CS 445 / 645 Introduction to Computer Graphics Lecture 21 Representing Rotations Lecture 21...

26
CS 445 / 645 Introduction to Computer Graphics Lecture 21 Lecture 21 Representing Rotations Representing Rotations

Transcript of CS 445 / 645 Introduction to Computer Graphics Lecture 21 Representing Rotations Lecture 21...

CS 445 / 645Introduction to Computer Graphics

Lecture 21Lecture 21

Representing RotationsRepresenting Rotations

Lecture 21Lecture 21

Representing RotationsRepresenting Rotations

Parameterizing Rotations

Straightforward in 2DStraightforward in 2D

• A scalar, A scalar, , represents rotation in plane, represents rotation in plane

More complicated in 3DMore complicated in 3D

• Three scalars are required to define orientationThree scalars are required to define orientation

• Note that three scalars are also required to define positionNote that three scalars are also required to define position

• Objects free to translate and tumble in 3D have 6 degrees of Objects free to translate and tumble in 3D have 6 degrees of freedom (DOF)freedom (DOF)

Straightforward in 2DStraightforward in 2D

• A scalar, A scalar, , represents rotation in plane, represents rotation in plane

More complicated in 3DMore complicated in 3D

• Three scalars are required to define orientationThree scalars are required to define orientation

• Note that three scalars are also required to define positionNote that three scalars are also required to define position

• Objects free to translate and tumble in 3D have 6 degrees of Objects free to translate and tumble in 3D have 6 degrees of freedom (DOF)freedom (DOF)

Representing 3 Rotational DOFs

3x3 Matrix (9 DOFs)3x3 Matrix (9 DOFs)• Rows of matrix define orthogonal axesRows of matrix define orthogonal axes

Euler Angles (3 DOFs)Euler Angles (3 DOFs)• Rot x + Rot y + Rot zRot x + Rot y + Rot z

Axis-angle (4 DOFs)Axis-angle (4 DOFs)• Axis of rotation + Rotation amountAxis of rotation + Rotation amount

Quaternion (4 DOFs)Quaternion (4 DOFs)• 4 dimensional complex numbers4 dimensional complex numbers

3x3 Matrix (9 DOFs)3x3 Matrix (9 DOFs)• Rows of matrix define orthogonal axesRows of matrix define orthogonal axes

Euler Angles (3 DOFs)Euler Angles (3 DOFs)• Rot x + Rot y + Rot zRot x + Rot y + Rot z

Axis-angle (4 DOFs)Axis-angle (4 DOFs)• Axis of rotation + Rotation amountAxis of rotation + Rotation amount

Quaternion (4 DOFs)Quaternion (4 DOFs)• 4 dimensional complex numbers4 dimensional complex numbers

Rotation Matrix

9 DOFs must reduce to 39 DOFs must reduce to 3

Rows must be unit length (-3 DOFs)Rows must be unit length (-3 DOFs)

Rows must be orthogonal (-3 DOFs)Rows must be orthogonal (-3 DOFs)

Drifting matrices is very badDrifting matrices is very bad

• Numerical errors results when trying to gradually rotate matrix by Numerical errors results when trying to gradually rotate matrix by adding derivativesadding derivatives

• Resulting matrix may scale / shearResulting matrix may scale / shear

• Gram-Schmidt algorithm will re-orthogonalize your matrixGram-Schmidt algorithm will re-orthogonalize your matrix

Difficult to interpolate between matricesDifficult to interpolate between matrices

• How would you do it?How would you do it?

9 DOFs must reduce to 39 DOFs must reduce to 3

Rows must be unit length (-3 DOFs)Rows must be unit length (-3 DOFs)

Rows must be orthogonal (-3 DOFs)Rows must be orthogonal (-3 DOFs)

Drifting matrices is very badDrifting matrices is very bad

• Numerical errors results when trying to gradually rotate matrix by Numerical errors results when trying to gradually rotate matrix by adding derivativesadding derivatives

• Resulting matrix may scale / shearResulting matrix may scale / shear

• Gram-Schmidt algorithm will re-orthogonalize your matrixGram-Schmidt algorithm will re-orthogonalize your matrix

Difficult to interpolate between matricesDifficult to interpolate between matrices

• How would you do it?How would you do it?

Euler Angles

((xx, , yy, , zz) = R) = RzzRRyyRRxx

• Rotate Rotate xx degrees about x-axis degrees about x-axis

• Rotate Rotate yy degrees about y-axis degrees about y-axis

• Rotate Rotate zz degrees about z-axis degrees about z-axis

Axis order is not definedAxis order is not defined• (y, z, x), (x, z, y), (z, y, x)…(y, z, x), (x, z, y), (z, y, x)…

are all legal are all legal

• Pick onePick one

((xx, , yy, , zz) = R) = RzzRRyyRRxx

• Rotate Rotate xx degrees about x-axis degrees about x-axis

• Rotate Rotate yy degrees about y-axis degrees about y-axis

• Rotate Rotate zz degrees about z-axis degrees about z-axis

Axis order is not definedAxis order is not defined• (y, z, x), (x, z, y), (z, y, x)…(y, z, x), (x, z, y), (z, y, x)…

are all legal are all legal

• Pick onePick one

Euler Angles

Rotations not uniquely definedRotations not uniquely defined

• ex: (z, x, y) = (90, 45, 45) = (45, 0, -45)ex: (z, x, y) = (90, 45, 45) = (45, 0, -45)takes positive x-axis to (1, 1, 1)takes positive x-axis to (1, 1, 1)

• Cartesian coordinates are independent of one another, but Euler Cartesian coordinates are independent of one another, but Euler angles are notangles are not

• Remember, the axes stay in the same place during rotationsRemember, the axes stay in the same place during rotations

Gimbal LockGimbal Lock

• Term derived from mechanical problem that arises in gimbal Term derived from mechanical problem that arises in gimbal mechanism that supports a compass or a gyromechanism that supports a compass or a gyro

Rotations not uniquely definedRotations not uniquely defined

• ex: (z, x, y) = (90, 45, 45) = (45, 0, -45)ex: (z, x, y) = (90, 45, 45) = (45, 0, -45)takes positive x-axis to (1, 1, 1)takes positive x-axis to (1, 1, 1)

• Cartesian coordinates are independent of one another, but Euler Cartesian coordinates are independent of one another, but Euler angles are notangles are not

• Remember, the axes stay in the same place during rotationsRemember, the axes stay in the same place during rotations

Gimbal LockGimbal Lock

• Term derived from mechanical problem that arises in gimbal Term derived from mechanical problem that arises in gimbal mechanism that supports a compass or a gyromechanism that supports a compass or a gyro

Gimbal Lock

http://www.anticz.com/eularqua.htmhttp://www.anticz.com/eularqua.htmhttp://www.anticz.com/eularqua.htmhttp://www.anticz.com/eularqua.htm

Gimbal Lock

Occurs when two axes are Occurs when two axes are alignedaligned

Second and third rotations Second and third rotations have effect of transforming have effect of transforming earlier rotationsearlier rotations• ex: Rot x, Rot y, Rot zex: Rot x, Rot y, Rot z

– If Rot y = 90 degrees, If Rot y = 90 degrees, Rot z == -Rot xRot z == -Rot x

Occurs when two axes are Occurs when two axes are alignedaligned

Second and third rotations Second and third rotations have effect of transforming have effect of transforming earlier rotationsearlier rotations• ex: Rot x, Rot y, Rot zex: Rot x, Rot y, Rot z

– If Rot y = 90 degrees, If Rot y = 90 degrees, Rot z == -Rot xRot z == -Rot x

A Gimbal

Hardware implementation of Euler angles (used for Hardware implementation of Euler angles (used for mounting gyroscopes and globes)mounting gyroscopes and globes)Hardware implementation of Euler angles (used for Hardware implementation of Euler angles (used for mounting gyroscopes and globes)mounting gyroscopes and globes)

Interpolation

Interpolation between two Euler angles is not Interpolation between two Euler angles is not uniqueunique

ex: (x, y, z) rotationex: (x, y, z) rotation

• (0, 0, 0) to (180, 0, 0) vs. (0, 0, 0) to (0, 180, 180)(0, 0, 0) to (180, 0, 0) vs. (0, 0, 0) to (0, 180, 180)

• Interpolation about different axes are not Interpolation about different axes are not independentindependent

Interpolation between two Euler angles is not Interpolation between two Euler angles is not uniqueunique

ex: (x, y, z) rotationex: (x, y, z) rotation

• (0, 0, 0) to (180, 0, 0) vs. (0, 0, 0) to (0, 180, 180)(0, 0, 0) to (180, 0, 0) vs. (0, 0, 0) to (0, 180, 180)

• Interpolation about different axes are not Interpolation about different axes are not independentindependent

Interpolation

Axis-angle Notation

Define an axis of rotation (x, y, z) and a rotation Define an axis of rotation (x, y, z) and a rotation about that axis, about that axis, R(R(, , nn))

4 degrees of freedom specify 3 rotational degrees 4 degrees of freedom specify 3 rotational degrees of freedom because axis of rotation is of freedom because axis of rotation is constrained to be a unit vectorconstrained to be a unit vector

Define an axis of rotation (x, y, z) and a rotation Define an axis of rotation (x, y, z) and a rotation about that axis, about that axis, R(R(, , nn))

4 degrees of freedom specify 3 rotational degrees 4 degrees of freedom specify 3 rotational degrees of freedom because axis of rotation is of freedom because axis of rotation is constrained to be a unit vectorconstrained to be a unit vector

Axis-angle Notation

r

Rr

n

rpar = (n.r) n

rperp = r – (n.r) n

V = n x (r – (n.r) n) = n x r

Rr = Rrpar + Rrperp

= Rrpar + (cos ) rperp + (sin ) V=(n.r) n + cos (r – (n.r)n) + (sin ) n x r= (cos )r + (1 – cos ) n (n.r) + (sin ) n x r

Axis-angle Rotation

rr’

n

Given r – Vector in space to rotate n – Unit-length axis in space about which to rotate – The amount about n to rotate

Solve r’ – The rotated vector

Axis-angle Rotation

Step 1Step 1

• Compute rCompute rkk an extended version of the rotation axis, n an extended version of the rotation axis, n

• rrkk = (n = (n ¢¢ r) n r) n

Step 1Step 1

• Compute rCompute rkk an extended version of the rotation axis, n an extended version of the rotation axis, n

• rrkk = (n = (n ¢¢ r) n r) n

rr’

rk

Axis-angle Rotation

Compute rCompute r? ?

rr?? = r – (n = r – (n ¢¢ r) n r) n

Compute rCompute r? ?

rr?? = r – (n = r – (n ¢¢ r) n r) n

rr’r?

Axis-angle Rotation

Compute v, a vector perpendicular to rCompute v, a vector perpendicular to rkk and r and r??

v = rv = rkk ££ r r??

Use v and rUse v and r?? and and to compute r’ to compute r’

Compute v, a vector perpendicular to rCompute v, a vector perpendicular to rkk and r and r??

v = rv = rkk ££ r r??

Use v and rUse v and r?? and and to compute r’ to compute r’

v

cos() r? + sin() v

r?

Axis-angle Notation

No easy way to determine how to concatenate No easy way to determine how to concatenate many axis-angle rotations that result in final many axis-angle rotations that result in final desired axis-angle rotationdesired axis-angle rotation

No simple way to interpolate rotationsNo simple way to interpolate rotations

No easy way to determine how to concatenate No easy way to determine how to concatenate many axis-angle rotations that result in final many axis-angle rotations that result in final desired axis-angle rotationdesired axis-angle rotation

No simple way to interpolate rotationsNo simple way to interpolate rotations

Quaternion

Remember complex numbers: a + ibRemember complex numbers: a + ib• Where iWhere i22 = -1 = -1

Invented by Sir William Hamilton (1843)Invented by Sir William Hamilton (1843)• Remember Hamiltonian path from Discrete II?Remember Hamiltonian path from Discrete II?

Quaternion:Quaternion:• Q = a + bi + cj + dkQ = a + bi + cj + dk

– Where iWhere i22 = j = j22 = k = k22 = -1 and ij = k and ji = -k = -1 and ij = k and ji = -k

• Represented as: q = (s, Represented as: q = (s, vv) = s + v) = s + vxxi + vi + vyyj + vj + vzzkk

Remember complex numbers: a + ibRemember complex numbers: a + ib• Where iWhere i22 = -1 = -1

Invented by Sir William Hamilton (1843)Invented by Sir William Hamilton (1843)• Remember Hamiltonian path from Discrete II?Remember Hamiltonian path from Discrete II?

Quaternion:Quaternion:• Q = a + bi + cj + dkQ = a + bi + cj + dk

– Where iWhere i22 = j = j22 = k = k22 = -1 and ij = k and ji = -k = -1 and ij = k and ji = -k

• Represented as: q = (s, Represented as: q = (s, vv) = s + v) = s + vxxi + vi + vyyj + vj + vzzkk

Quaternion

A quaternion is a 4-D unit vector q = [x y z w]A quaternion is a 4-D unit vector q = [x y z w]

• It lies on the unit hypersphere xIt lies on the unit hypersphere x22 + y + y22 + z + z22 + w + w22 = 1 = 1

For rotation about (unit) axis v by angle For rotation about (unit) axis v by angle • vector part = (sin vector part = (sin /2) /2) vv = = [x y z][x y z]

• scalar part = (cos scalar part = (cos /2) = /2) = ww

• (sin((sin(/2) n/2) nxx, sin(, sin(/2) n/2) nyy, sin(, sin(/2) n/2) nz, z, cos (cos (/2))/2))

Only a unit quaternion encodes a rotation - normalizeOnly a unit quaternion encodes a rotation - normalize

A quaternion is a 4-D unit vector q = [x y z w]A quaternion is a 4-D unit vector q = [x y z w]

• It lies on the unit hypersphere xIt lies on the unit hypersphere x22 + y + y22 + z + z22 + w + w22 = 1 = 1

For rotation about (unit) axis v by angle For rotation about (unit) axis v by angle • vector part = (sin vector part = (sin /2) /2) vv = = [x y z][x y z]

• scalar part = (cos scalar part = (cos /2) = /2) = ww

• (sin((sin(/2) n/2) nxx, sin(, sin(/2) n/2) nyy, sin(, sin(/2) n/2) nz, z, cos (cos (/2))/2))

Only a unit quaternion encodes a rotation - normalizeOnly a unit quaternion encodes a rotation - normalize

Quaternion

Rotation matrix corresponding to a quaternion:Rotation matrix corresponding to a quaternion:• [x y z w] = [x y z w] =

Quaternion MultiplicationQuaternion Multiplication• qq11 * q * q2 2 = [= [vv11, w, w11] * [] * [vv22, w, w22] = [(w] = [(w11vv22+w+w22vv11+ (v+ (v11 x v x v22)), w)), w11ww22-v-v11.v.v22]]

• quaternion * quaternion = quaternionquaternion * quaternion = quaternion

• this satisfies requirements for mathematical this satisfies requirements for mathematical groupgroup

• Rotating object twice according to two different quaternions is equivalent Rotating object twice according to two different quaternions is equivalent to one rotation according to product of two quaternionsto one rotation according to product of two quaternions

Rotation matrix corresponding to a quaternion:Rotation matrix corresponding to a quaternion:• [x y z w] = [x y z w] =

Quaternion MultiplicationQuaternion Multiplication• qq11 * q * q2 2 = [= [vv11, w, w11] * [] * [vv22, w, w22] = [(w] = [(w11vv22+w+w22vv11+ (v+ (v11 x v x v22)), w)), w11ww22-v-v11.v.v22]]

• quaternion * quaternion = quaternionquaternion * quaternion = quaternion

• this satisfies requirements for mathematical this satisfies requirements for mathematical groupgroup

• Rotating object twice according to two different quaternions is equivalent Rotating object twice according to two different quaternions is equivalent to one rotation according to product of two quaternionsto one rotation according to product of two quaternions

22

22

22

2212222

2222122

2222221

yxwxyzwyxz

wxyzzxwzxy

wyxzwzxyzy

Quaternion Example

X-roll of X-roll of • (cos ((cos (/2), sin (/2), sin (/2) (1, 0, 0)) = (0, (1, 0, 0))/2) (1, 0, 0)) = (0, (1, 0, 0))

Y-roll 0f Y-roll 0f • (0, (0, 1, 0))(0, (0, 1, 0))

Z-roll of Z-roll of • (0, (0, 0, 1))(0, (0, 0, 1))

RRyy ( () followed by R) followed by Rzz ( ())• (0, (0, 1, 0) times (0, (0, 0, 1)) = (0, (0, 1, 0) x (0, 0, 1)(0, (0, 1, 0) times (0, (0, 0, 1)) = (0, (0, 1, 0) x (0, 0, 1)

= (0, (1, 0, 0)) = (0, (1, 0, 0))

X-roll of X-roll of • (cos ((cos (/2), sin (/2), sin (/2) (1, 0, 0)) = (0, (1, 0, 0))/2) (1, 0, 0)) = (0, (1, 0, 0))

Y-roll 0f Y-roll 0f • (0, (0, 1, 0))(0, (0, 1, 0))

Z-roll of Z-roll of • (0, (0, 0, 1))(0, (0, 0, 1))

RRyy ( () followed by R) followed by Rzz ( ())• (0, (0, 1, 0) times (0, (0, 0, 1)) = (0, (0, 1, 0) x (0, 0, 1)(0, (0, 1, 0) times (0, (0, 0, 1)) = (0, (0, 1, 0) x (0, 0, 1)

= (0, (1, 0, 0)) = (0, (1, 0, 0))

Quaternion Interpolation

Biggest advantage of quaternionsBiggest advantage of quaternions• InterpolationInterpolation

• Cannot linearly interpolate between two quaternions Cannot linearly interpolate between two quaternions because it would speed up in middlebecause it would speed up in middle

• Instead, Spherical Linear Interpolation, slerp()Instead, Spherical Linear Interpolation, slerp()

• Used by modern video games for third-person Used by modern video games for third-person perspectiveperspective

• Why?Why?

Biggest advantage of quaternionsBiggest advantage of quaternions• InterpolationInterpolation

• Cannot linearly interpolate between two quaternions Cannot linearly interpolate between two quaternions because it would speed up in middlebecause it would speed up in middle

• Instead, Spherical Linear Interpolation, slerp()Instead, Spherical Linear Interpolation, slerp()

• Used by modern video games for third-person Used by modern video games for third-person perspectiveperspective

• Why?Why?

SLERP

Quaternion is a point on the 4-D unit sphereQuaternion is a point on the 4-D unit sphere• interpolating rotations requires a unit quaternion at each stepinterpolating rotations requires a unit quaternion at each step

– another point on the 4-D unit sphereanother point on the 4-D unit sphere

• move with constant angular velocity along the great circle between two move with constant angular velocity along the great circle between two pointspoints

Any rotation is defined by 2 quaternions, so pick the Any rotation is defined by 2 quaternions, so pick the shortest SLERPshortest SLERP

To interpolate more than two points, solve a non-linear To interpolate more than two points, solve a non-linear variational constrained optimizationvariational constrained optimization• Ken Shoemake in SIGGRAPH ’85 (www.acm.org/dl)Ken Shoemake in SIGGRAPH ’85 (www.acm.org/dl)

Quaternion is a point on the 4-D unit sphereQuaternion is a point on the 4-D unit sphere• interpolating rotations requires a unit quaternion at each stepinterpolating rotations requires a unit quaternion at each step

– another point on the 4-D unit sphereanother point on the 4-D unit sphere

• move with constant angular velocity along the great circle between two move with constant angular velocity along the great circle between two pointspoints

Any rotation is defined by 2 quaternions, so pick the Any rotation is defined by 2 quaternions, so pick the shortest SLERPshortest SLERP

To interpolate more than two points, solve a non-linear To interpolate more than two points, solve a non-linear variational constrained optimizationvariational constrained optimization• Ken Shoemake in SIGGRAPH ’85 (www.acm.org/dl)Ken Shoemake in SIGGRAPH ’85 (www.acm.org/dl)

Quaternion Interpolation

Quaternion (white) vs. Quaternion (white) vs. Euler (black) Euler (black) interpolationinterpolation

Left images are linear Left images are linear interpolationinterpolation

Right images are cubic Right images are cubic interpolationinterpolation

Quaternion (white) vs. Quaternion (white) vs. Euler (black) Euler (black) interpolationinterpolation

Left images are linear Left images are linear interpolationinterpolation

Right images are cubic Right images are cubic interpolationinterpolation

Quaternion Code

http://www.gamasutra.com/features/http://www.gamasutra.com/features/programming/19980703/quaternions_01.htmprogramming/19980703/quaternions_01.htm• Registration requiredRegistration required

Camera control codeCamera control code• http://www.xmission.com/~nate/smooth.htmlhttp://www.xmission.com/~nate/smooth.html

– File, gltb.cFile, gltb.c

– gltbMatrix and gltbMotiongltbMatrix and gltbMotion

http://www.gamasutra.com/features/http://www.gamasutra.com/features/programming/19980703/quaternions_01.htmprogramming/19980703/quaternions_01.htm• Registration requiredRegistration required

Camera control codeCamera control code• http://www.xmission.com/~nate/smooth.htmlhttp://www.xmission.com/~nate/smooth.html

– File, gltb.cFile, gltb.c

– gltbMatrix and gltbMotiongltbMatrix and gltbMotion