advertisement

Quaternions Design and Creation of Virtual Environments CISE 6930/4930 Section 6589/3146 Benjamin Lok Fall 2003 Euler Angles Orientation of an object in computer graphics is often described using Euler Angles. Z RxRyRz Any axis order will work and could be used. Yaw, Pitch & Roll Y X Problems with Euler Angles Rotations not uniquely defined Ex: (z, x, y) [roll, yaw, pitch] = (90, 45, 45) = (45, 0, -45) takes positive x-axis to (1, 1, 1) Cartesian coordinates are independent of one another, but Euler angles are not Remember, the axes stay in the same place during rotations Gimbal Lock Term derived from mechanical problem that arises in gimbal mechanism that supports a compass or a gyro Second and third rotations have effect of transforming earlier rotations, we lose a degree of freedom ex: Rot x, Rot y, Rot z If Rot y = 90 degrees, Rot z is equivalent to -Rot x A Gimble is a hardware implementation of Euler angles (used for mounting gyroscopes and globes) Another Problem with Euler Angles What if we want to produce a smooth animation of a rotation from point P1 to Point P2 around some axis. The entire rotation is defined as RxRyRz but how do we get an intermediate point? Rotations are defined as rotations about the x, y and z axes. Rotations about any other vector in space have to be broken down into equivalent rotations about the major axes. Smooth Rotation y z With Euler Angles, knowing the transformations for the entire rotation does not help us with the intermediate rotations. Each is a unique set of three rotations about the x,y and z axes x Quaternions Invented by Sir William Hamilton (1843) Do not suffer from Gimbal Lock Provide a natural way to interpolate intermediate steps in a rotation about an arbitrary axis Are used in many position tracking systems and VR software support systems Quaternions Definitions & Basic Operations Remember Complex Numbers? z = a + i b where i = (-1) Can think of a complex number as having a real and an imaginary part or as a vector in two-dimensional space z = [a, b] z1+z2 = (a1 + i b1) + (a2 + i b2) = (a1+a2) + i (b1+b2) z1*z2 = (a1+i b1)*(a2+i b2) = (a1a2–b1b2) + i (a2b1+a1b2) Quaternions You can think of quaternions as an extension of complex numbers where there are three different square roots of -1. q = w + i x + j y + k z where i = (-1), j = (-1), k = (-1) i*j = k, j*k = i, k*i = j j*i = -k, k*j = -i, i*k = -j You could also think of q as a value in fourdimensional space, q = [w, x, y, z] Sometimes written as q = [w, v] where w is a scalar and v is a vector in 3-space Addition of Quaternions q1 + q 2 = (w1+i x1+j y1+k z1) + (w2+i x2+j y2+k z2) = w1+w2 + i (x1+x2) + j (y1+y2) +k (z1+z2) q1 + q2 = [w1, v1] + [w2, v2] = [w1+w2, v1+v2] Multiplication of Quaternions q1 * q2 = (w1+i x1+j y1+k z1) * (w2+i x2+j y2+k z2) = w1w2 + i w1x2 + j w1y2 + k w1z2 + -x1x2 + i w2x1 – j x1z2 + k x1y2 + -y1y2 + i y1z2 + j w2y1 - k x2y1 + -z1z2 - i y2z1 + j x2z1 + k w2z1 q1 * q2 = [w1, v1] * [w2, v2] = Multiplication of Quaternions q1*q2 = (w1+ix1+jy1+kz1)*(w2+ix2+jy2+kz2) = w1w2 + i w1x2 + j w1y2 + k w1z2 + -x1x2 + i w2x1 – j x1z2 + k x1y2 + -y1y2 + i y1z2 + j w2y1 - k x2y1 + -z1z2 - i y2z1 + j x2z1 + k w2z1 q1*q2 = [w1, v1] * [w2, v2] = [w1w2 – v1°v2, w1v2 + w2v1+ v1v2] Practice q1 = [1.57 0 0 1], q2= [.78 0 1 0] q1 = [.25 3 4 4], q2= [.20 2 9 9] q1 + q2? q1 + q2 = w1+w2 + i (x1+x2) + j (y1+y2) +k (z1+z2) q1 + q2 = [w1+w2, v1+v2] q1 * q2? q1 * q 2 = (w1+i x1+j y1+k z1) * (w2+i x2+j y2+k z2) = w1w2 + i w1x2 + j w1y2 + k w1z2 + -x1x2 + i w2x1 – j x1z2 + k x1y2 + -y1y2 + i y1z2 + j w2y1 - k x2y1 + -z1z2 - i y2z1 + j x2z1 + k w2z1 [w1w2 – v1°v2, w1v2 + w2v1+ v1v2] Other operations ||q|| = (w2 + x2 + y2 + z2) ||q|| = 1 is a unit quaternion The inverse of a quaternion, q-1 =[w, -v]/ ||q||2 q*q-1 = [1,0,0,0] Practice: Find ||q|| and q-1 of q1 = [1.57 0 0 1], q2 = [.25 3 4 4] Quaternion multiplication is associate but not commutative Find the inverse of q = [0,6,8,0] q-1 =[w, -v]/ ||q||2 q-1 =[0,-6,-8,-0]/(0+36+64+0) = [0,-.06,.08,0] Find the inverse of q = [0,6,8,0] q-1 =[w, -v]/ ||q||2 q-1 =[0,-6,-8,-0]/(0+36+64+0) = [0,-.06,-.08,0] To check, q*q-1 should give us the identity quaternion. q*q-1 = [0-(6*-.06 + 8*-.08 + 0), [0,0,0]+ [0,0,0] +[0,0,0]] = [1,0, 0, 0] [w1w2 – v1°v2, w1v2 + w2v1+ v1v2] Quaternions Rotating with Quaternions Rotation about an arbitrary axis Let q be a unit quaternion, i.e., ||q|| = 1 q = [w,x,y,z] describes a rotation through an angle where w = cos(/2) and [x,y,z] is the axis of rotation. (x,y,z) Procedure To create a unit quaternion that represents a rotation of degrees about an arbitrary axis v = [x,y,z]. q = [cos(/2), sin(/2)*v/||v||] Find q, s/t q describes a rotation of 60 degrees about v = [3,4,0]. Remember, we need a unit quaternion. To get this: Compute cos (60/2) = cos30 = 1/2 Compute sin (60/2) = sin30 = 3/2 Compute v’ = v/||v|| = [3,4,0]/5 = [3/5,4/5,0] q = [1/2, 3/2*[3/5,4/5, 0]] = [1/2, 33/10, 43/10, 0] ||q|| = ¼ + 27/100 + 48/100 = (25+27+48)/100 = 1 To rotate a point, P = (px,py,pz) Protated = q[0,P]q-1 Remember this is quaternion multiplication not matrix multiplication! Example: P = (0,1,1) rotated 90 degrees about the vertical y-axis. y x z Rotated point should be (1,1,0) Example: P = (0,1,1) rotated 90 degrees about the vertical y-axis. First, compute q Next compute q-1 q = [cos(90/2), sin(90/2)[0,1,0]] q = [cos45, 0, sin45, 0] = [2/2, 0, 2/2, 0] q-1 = [2/2, 0, -2/2, 0] Why? Compute q[0,P]q-1 =[2/2, 0, 2/2, 0][0,0,1,1][2/2, 0, 2/2, 0] Compute q[0,P]q-1 =[2/2, 0, 2/2, 0][0,0,1,1][2/2, 0, -2/2, 0] Remember that q1*q2 = [w1w2 – v1°v2, w1v2 + w2v1+ v1v2] so q[0, P] = [2/2, 0, 2/2, 0][0,0,1,1] = [2/2*0 – [0,2/2,0]°[0,1,1], 2/2[0,1,1] + 0*v1 + v1 x v2 ] = [-2/2, 2/2, 2/2, 2/2] q[0, P] q-1 = [-2/2, 2/2, 2/2, 2/2][2/2, 0, -2/2, 0] = [-2/2* 2/2 – [2/2, 2/2, 2/2]°[0, -2/2, 0], (-2/2)[0, -2/2, 0] +(2/2)*[2/2, 2/2, 2/2] + v1 x v2 = [0, [1/2,1,1/2] + [1/2 ,0,-1/2] = [0, 1, 1, 0] Transformed point is [1,1,0]. See notes page for ALL the details Why use quaternions? If you want to apply multiple rotations, say q1 and q2. When? Recall: q[0,P]q-1 Thus: q2 (q1 [0,P] q1-1 ) q2-1 (q2 q1) [0,P] (q1-1 q2-1 ) q 2 * q1 Why is a quaternion multiplication better than a matrix multiply? What about keeping rotations? Smooth Interpolation SLERP – spherical linear interpolation slerp(t, a, b) = f(t) = (b.a-1)t a What happens when t=0? t=1? In actuality, we need to conserve camera velocity (Spherical – Cubic Interpolation) Why? Where have you seen this before? D A Bezier/Hermite curves slerp (t,a,c,d,b) a, b – start points, c,d – define curve The curve touchs cd when t=0.5 B C