Rotations and quaternions Jiří Chmelík, Marek Trtík PA199 ► Rotation matrix ► Euler angles ► Tait-Bryan angles ► Axis-angle representation ► Quaternions ► Rotations via quaternions ► Quaternion derivative Outline  Well know topic from computer graphics courses. => We only discuss relation between basis vectors and rotation matrix.  Let 𝒊′, 𝒋′, 𝒌′ be orthonormal basis vectors of a coordinate system inside the world coordinate system.  Then, the orientation of the coordinate system is represented by the rotation matrix: 𝑅 = 𝒊 𝑥 ′ 𝒋 𝑥 ′ 𝒌 𝑥 ′ 𝒊 𝑦 ′ 𝒋 𝑦 ′ 𝒌 𝑦 ′ 𝒊 𝑧 ′ 𝒋 𝑧 ′ 𝒌 𝑧 ′ ► 𝑅 transforms vectors “to world space”. ► 𝑅−1 = 𝑅⊤ transforms vectors “from world space”. Rotation matrix 3 𝑥 𝑦 𝑧 𝑥′ 𝑦′ 𝑧′ 𝒊′ 𝒋′ 𝒌′ 𝑁 = 𝑧 × 𝑍 𝛾 ► Three rotations are always sufficient to transform a source frame 𝑥𝑦𝑧 to a target one 𝑋𝑌𝑍: ► 𝛼 ∈ ‫ۦ‬0,2𝜋) ► 𝛽 ∈ 0, 𝜋 ► 𝛾 ∈ ‫ۦ‬0,2𝜋) ► Actual rotations: Euler angles 𝑥 𝑦 𝑧 𝑋 𝑌 𝑍 𝛼 𝛽 𝑥𝑦-plane 𝑋𝑌-plane When planes 𝑥𝑦 and 𝑋𝑌 are equal, then the statement is clearly true. 4 “Line of nodes” 𝑁 = 𝑧 × 𝑍 𝛾 ► Three rotations are always sufficient to transform a source frame 𝑥𝑦𝑧 to a target one 𝑋𝑌𝑍: ► 𝛼 ∈ ‫ۦ‬0,2𝜋) ► 𝛽 ∈ 0, 𝜋 ► 𝛾 ∈ ‫ۦ‬0,2𝜋) ► Actual rotations: ► Start with frame 𝑋0 𝑌0 𝑍0 = 𝑥𝑦𝑧. ► Rotate 𝑋0 𝑌0 𝑍0 about 𝑍0 by 𝛼. Euler angles 𝑥 𝑦 𝑧 𝑋 𝑌 𝑍 𝛼 𝛽 𝑥𝑦-plane 𝑋𝑌-plane 5 When planes 𝑥𝑦 and 𝑋𝑌 are equal, then the statement is clearly true. 𝑋0 = = 𝑌0 𝑍0 = “Line of nodes” 𝑁 = 𝑧 × 𝑍 𝛾 ► Three rotations are always sufficient to transform a source frame 𝑥𝑦𝑧 to a target one 𝑋𝑌𝑍: ► 𝛼 ∈ ‫ۦ‬0,2𝜋) ► 𝛽 ∈ 0, 𝜋 ► 𝛾 ∈ ‫ۦ‬0,2𝜋) ► Actual rotations: ► Start with frame 𝑋0 𝑌0 𝑍0 = 𝑥𝑦𝑧. ► Rotate 𝑋0 𝑌0 𝑍0 about 𝑍0 by 𝛼. ► Rotate 𝑋1 𝑌1 𝑍1 about 𝑋1 by 𝛽. Euler angles 𝑥 𝑦 𝑧 𝑋 𝑌 𝑍 𝛼 𝛽 𝑥𝑦-plane 𝑋𝑌-plane 6 When planes 𝑥𝑦 and 𝑋𝑌 are equal, then the statement is clearly true. 𝑋1 = 𝑌1 𝑍1 = “Line of nodes” 𝑁 = 𝑧 × 𝑍 𝛾 ► Three rotations are always sufficient to transform a source frame 𝑥𝑦𝑧 to a target one 𝑋𝑌𝑍: ► 𝛼 ∈ ‫ۦ‬0,2𝜋) ► 𝛽 ∈ 0, 𝜋 ► 𝛾 ∈ ‫ۦ‬0,2𝜋) ► Actual rotations: ► Start with frame 𝑋0 𝑌0 𝑍0 = 𝑥𝑦𝑧. ► Rotate 𝑋0 𝑌0 𝑍0 about 𝑍0 by 𝛼. ► Rotate 𝑋1 𝑌1 𝑍1 about 𝑋1 by 𝛽. ► Rotate 𝑋2 𝑌2 𝑍2 about 𝑍2 by 𝛾. Euler angles 𝑥 𝑦 𝑧 𝑋 𝑌 𝑍 𝛼 𝛽 𝑥𝑦-plane 𝑋𝑌-plane 7 When planes 𝑥𝑦 and 𝑋𝑌 are equal, then the statement is clearly true. 𝑌2 𝑍2 = 𝑋2 = “Line of nodes” 𝑁 = 𝑧 × 𝑍 𝛾 ► Three rotations are always sufficient to transform a source frame 𝑥𝑦𝑧 to a target one 𝑋𝑌𝑍: ► 𝛼 ∈ ‫ۦ‬0,2𝜋) ► 𝛽 ∈ 0, 𝜋 ► 𝛾 ∈ ‫ۦ‬0,2𝜋) ► Actual rotations: ► Start with frame 𝑋0 𝑌0 𝑍0 = 𝑥𝑦𝑧. ► Rotate 𝑋0 𝑌0 𝑍0 about 𝑍0 by 𝛼. ► Rotate 𝑋1 𝑌1 𝑍1 about 𝑋1 by 𝛽. ► Rotate 𝑋2 𝑌2 𝑍2 about 𝑍2 by 𝛾. Euler angles 𝑥 𝑦 𝑧 𝑋 𝑌 𝑍 𝛼 𝛽 𝑥𝑦-plane 𝑋𝑌-plane 8 When planes 𝑥𝑦 and 𝑋𝑌 are equal, then the statement is clearly true. = 𝑌3 𝑍3 = = 𝑋3 “Line of nodes” ► Let 𝑅 𝜑, 𝑎 denotes a rotation matrix about an axis 𝑎 by an angle 𝜑. ► So, our rotations can be expressed by matrices: ► 𝑅 𝛼, 𝑍0 ► 𝑅 𝛽, 𝑋1 ► 𝑅 𝛾, 𝑍2 ► We compose them by the matrix multiplication: 𝑅 𝛾, 𝑍2 𝑅 𝛽, 𝑋1 𝑅 𝛼, 𝑍0 ► Here we work with 𝑍-𝑋-𝑍 convention. But there are 5 more: ► 𝑋-𝑌-𝑋, 𝑋-𝑍-𝑋, 𝑌-𝑋-𝑌, 𝑌-𝑍-𝑌, and 𝑍-𝑌-𝑍. ► We can choose any of the conventions we want. ► Observation: 1st and 3rd rotation axes are the same. Euler angles 9 ► The rotations 𝑅 𝛼, 𝑍0 , 𝑅 𝛽, 𝑋1 , 𝑅 𝛾, 𝑍2 about the axes of the rotated (target) frame 𝑋𝑌𝑍 are called intrinsic. ► A practical disadvantage of intrinsic rotations is that some of rotations are about arbitrary oriented axis. ► In CG courses we only learned how to build rotation matrices for fixed axes 𝑥, 𝑦, 𝑧. ► But axes 𝑋1, 𝑍2 may be arbitrary (the axis 𝑍0 is OK, since 𝑍0 = 𝑧). ► Fortunately, we can also transform a source frame 𝑥𝑦𝑧 to a target one 𝑋𝑌𝑍 using extrinsic rotations 𝑅 𝛼, 𝑧 , 𝑅 𝛽, 𝑥 , 𝑅 𝛾, 𝑧 . ► Let us figure out how to do that… Euler angles 10 ► Start with the 𝑋𝑌𝑍 aligned with 𝑥𝑦𝑧. Euler angles 𝑥 𝑦 𝑧 𝑋 𝑌 𝑍 11 𝑋 = = 𝑌 𝑍 = ► Start with the 𝑋𝑌𝑍 aligned with 𝑥𝑦𝑧. ► Apply 𝑅 𝛼, 𝑥 to rotate the tip of 𝑍 to the plane 𝜌. ► 𝜌 is parallel with 𝑥𝑦 plane and contains the tip of 𝑍. Euler angles 𝑥 𝑦 𝑧 𝑋 𝑌 𝑍 𝛼 12 𝑌 𝑍 𝜌 𝑋 = ► Start with the 𝑋𝑌𝑍 aligned with 𝑥𝑦𝑧. ► Apply 𝑅 𝛼, 𝑥 to rotate the tip of 𝑍 to the plane 𝜌. ► 𝜌 is parallel with 𝑥𝑦 plane and contains the tip of 𝑍. ► Apply 𝑅 𝛽, 𝑧 to rotate the tip of 𝑍 to the tip of 𝑍. Euler angles 𝑥 𝑦 𝑧 𝑋 𝑌 𝑍 𝛼 13 𝑌 𝜌 𝑋 𝑍 = 𝛽 ► Start with the 𝑋𝑌𝑍 aligned with 𝑥𝑦𝑧. ► Apply 𝑅 𝛼, 𝑥 to rotate the tip of 𝑍 to the plane 𝜌. ► 𝜌 is parallel with 𝑥𝑦 plane and contains the tip of 𝑍. ► Apply 𝑅 𝛽, 𝑧 to rotate the tip of 𝑍 to the tip of 𝑍. ► Apply the “twist” rotation 𝑅 𝛾, 𝑍 to align 𝑋 with 𝑋 and 𝑌 with 𝑌. ► But, this is not extrinsic rotation! ► We can fix it by applying the twist 𝑅 𝛾, 𝑧 as the first rotation. ► The value of 𝛾 will be different since the other two rotations affect the twist too. Euler angles 𝑥 𝑦 𝑧 𝑋 𝑌 𝑍 𝛼 14 𝜌 𝑍 = 𝛽 = 𝑋 = 𝑌 𝛾 More intuition is in video: [5] ► Planes 𝑥𝑦 and 𝑋𝑌 are parallel => 1 degree of freedom is lost = gimbal lock. ► The special value of 𝛽 “locks” the other two rotations into the same plane (although they can rotate freely in that plane). Euler angles: gimbal lock 15 𝑥 𝑦 𝑍 = 𝑧 𝑋 𝑌 Case: 𝛽 = 0 𝛼, 𝛾: ambiguous 𝛼 + 𝛾: unique 𝑥 𝑦 𝑧 𝑋 𝑌 𝑍 Case: 𝛽 = 𝜋 𝛼, 𝛾: ambiguous 𝛼 − 𝛾: unique ► We can use 3 angles to express any orientation of an object in 3D space: public class Orientation { float alpha; float beta; float gamma; }; ►Pros: ►Low memory footprint. ►Easy to understand. ►Cons: ►Suffers from the gimbal lock. ►Slow conversion to matrix representation (sin and cos for each angle). Euler angles representation 16 ► Same as Euler angles, except that all three axes are different. ► There are 6 possible conventions: ► 𝑋-𝑌-𝑍, 𝑋-𝑍-𝑌, 𝑌-𝑋-𝑍, 𝑌-𝑍-𝑋, 𝑍-𝑋-𝑌, and 𝑍-𝑌-𝑋. ► The line of nodes is different: It is an intersection of the 𝑥𝑦-plane and the plane orthogonal to the 3rd rotation axis of the convention. ► The angles 𝛼, 𝛽, 𝛾 are often called yaw, pitch, roll, respectively. Tait-Bryan angles 17 ► Euler’s rotation theorem (one of the versions): Any reconfiguration of an object in 3D space with one of its points fixed is equivalent to its single rotation about an axis passing through the fixed point. ► Proof: ► We look for a rotation axis passing though 𝑆. ► We “paint” a great circle (green) on the sphere in the initial position. ►We rotate the sphere => We get the rotated green circle, which is depicted as red circle. ►If the circles coincide, then the axis clearly exists. Otherwise, the circles intersect - two points 𝐴, 𝑍. ► 𝐴 is on red circle => its pre-image 𝐵 is on green one. 𝐴 is on green circle => its post-image 𝐶 is on red one. Axis-angle rotation 18 𝑆 𝐶 𝐵𝐴 𝑍 ►Construct a great circle (blue) passing through 𝐴, 𝑍 and bisecting the angle 𝐵𝐴𝐶. ►Find a point 𝑂 on the blue circle s.t. the length of arcs 𝐴𝑂 and 𝐵𝑂 is the same. ►The length of the arc 𝐴𝑂 must be equal to the length of the arc 𝐶𝑂, because lengths of arcs 𝐴𝐵 and 𝐵𝐶 are the same and the blue circle in the bisector of the angle 𝐶𝐴𝐵. Triangles 𝐶𝐴𝑂 and 𝐴𝐵𝑂 on the sphere must be the same. Actually, 𝐴𝐵𝑂 becomes 𝐶𝐴𝑂 after the rotation. The point 𝑂 lies on the reached rotation axis, because it does not move when rotating the triangles.  𝑆𝑂 is the rotation axis and the arc length 𝐴𝐵 is the angle. Axis-angle rotation 19 𝑂 𝑆 𝐴 𝐵 𝐶 𝑍 ►Rodrigues' rotation formula: A vector 𝑣 ∈ 𝑅3 rotated about a unit axis 𝑎 ∈ 𝑅3 by an angle 𝜃 ∈ ‫ۦ‬0,2𝜋) is the vector: ҧ𝑣 = cos 𝜃 𝑣 + 1 − cos 𝜃 𝑎 ⋅ 𝑣 𝑎 + sin 𝜃 𝑎 × 𝑣. ►Proof: 𝑣 𝑎 = 𝑎 ⋅ 𝑣 𝑎, 𝑣⊥ = 𝑣 − 𝑣 𝑎 = 𝑣 − 𝑎 ⋅ 𝑣 𝑎, 𝑣× = 𝑎 × 𝑣⊥ = 𝑎 × 𝑣 − 𝑣 𝑎 = 𝑎 × 𝑣. Note: |𝑣×| = |𝑣⊥|. ҧ𝑣⊥ = cos 𝜃 𝑣⊥ + sin 𝜃 𝑣×. ҧ𝑣 = 𝑣 𝑎 + ҧ𝑣⊥ = 𝑣 𝑎 + cos 𝜃 𝑣⊥ + sin 𝜃 𝑣× = 𝑎 ⋅ 𝑣 𝑎 + cos 𝜃 (𝑣 − 𝑎 ⋅ 𝑣 𝑎) + sin 𝜃 𝑎 × 𝑣 = cos 𝜃 𝑣 + 1 − cos 𝜃 𝑎 ⋅ 𝑣 𝑎 + sin 𝜃 𝑎 × 𝑣. Axis-angle rotation 𝜃 𝑎 𝑣 ҧ𝑣 𝑣 𝑎 𝑣⊥ 𝑣× ҧ𝑣⊥ 20 ►Vector triple product: 𝑢 × 𝑣 × 𝑤 = 𝑢 ⋅ 𝑤 𝑣 − 𝑢 ⋅ 𝑣 𝑤 ►If 𝑎 = 1, then 𝑎 × 𝑎 × 𝑣 = 𝑎 ⋅ 𝑣 𝑎 − 𝑎 ⋅ 𝑎 𝑣 = 𝑎 ⋅ 𝑣 𝑎 − 𝑣 ► Matrix representation of the cross product: 𝑢 × 𝑣 = 0 −𝑢 𝑧 𝑢 𝑦 𝑢 𝑧 0 −𝑢 𝑥 −𝑢 𝑦 𝑢 𝑥 0 𝑣 = 𝑢 𝑣. ►Matrix representation of the axis-angle: ҧ𝑣 = cos 𝜃 𝑣 + 1 − cos 𝜃 𝑎 ⋅ 𝑣 𝑎 + sin 𝜃 𝑎 × 𝑣 = cos 𝜃 𝑣 + 1 − cos 𝜃 (𝑎 × 𝑎 × 𝑣 + 𝑣) + sin 𝜃 𝑎 × 𝑣 = 𝑣 + 1 − cos 𝜃 𝑎 × 𝑎 × 𝑣 + sin 𝜃 𝑎 × 𝑣 = 𝑣 + 1 − cos 𝜃 𝑎 2 𝑣 + sin 𝜃 𝑎 𝑣 = 𝐼 + 1 − cos 𝜃 𝑎 2 + sin 𝜃 𝑎 𝑣 = 𝑅 𝜃, 𝑎 𝑣 Axis-Angle to rotation Matrix 21 ►Given two axis-angle rotations 𝑅 𝜑, 𝑎 and 𝑅 𝜓, 𝑏 , the linearly interpolated rotation is then 𝑅 1 − 𝑡 𝜑 + 𝑡𝜓, (1−𝑡)𝑎+𝑡𝑏 |(1−𝑡)𝑎+𝑡𝑏| , 𝑡 ∈ 0,1 . ►Technical issues related to |(1 − 𝑡)𝑎 + 𝑡𝑏|: ►Slow – we must compute the square root. ►The result is not defined when =0. ►Problem: The velocity is not constant (increases and decreases) . Visible visual artefact – we prefer uniform blending between rotations. ►Can we do better? ►Yes, use spherical linear interpolation. Linear interpolation (lerp) 22 ►Given two linearly independent unit vectors 𝑢, 𝑣 and a parameter 𝑡 ∈ 0,1 , find a unit vector 𝑤 = 𝛼𝑢 + 𝛽𝑣 s.t. 𝛼, 𝛽 > 0 and angle between 𝑢, 𝑤 is 𝑡𝜃, where 𝜃 is the angle between 𝑢, 𝑣. ► 𝑣⊥ ∗ = 𝑣−cos 𝜃𝑢 (𝑣−cos 𝜃𝑢)(𝑣−cos 𝜃𝑢) = 𝑣−cos 𝜃𝑢 1−cos2 𝜃 = 𝑣−cos 𝜃𝑢 sin 𝜃 . ► 𝑤 = cos 𝑡𝜃 𝑢 + sin 𝑡𝜃 𝑣−cos 𝜃𝑢 sin 𝜃 = cos 𝑡𝜃 − sin 𝑡𝜃 cos 𝜃 sin 𝜃 𝑢 + sin 𝑡𝜃 sin 𝜃 𝑣 = cos 𝑡𝜃 sin 𝜃−sin 𝑡𝜃 cos 𝜃 sin 𝜃 𝑢 + sin 𝑡𝜃 sin 𝜃 𝑣 = sin(1−𝑡)𝜃 sin 𝜃 𝑢 + sin 𝑡𝜃 sin 𝜃 𝑣. ►Given two axis-angle rotations 𝑅 𝜑, 𝑎 and 𝑅 𝜓, 𝑏 , the interpolated rotation is then 𝑅 1 − 𝑡 𝜑 + 𝑡𝜓, sin(1−𝑡)𝜃 sin 𝜃 𝑎 + sin 𝑡𝜃 sin 𝜃 𝑏 . Spherical linear interpolation (slerp) 23 𝑢 𝑣 𝑤 𝜃 𝑡𝜃 𝑣⊥ 𝑣⊥ = 𝑣 − cos 𝜃 𝑢 𝑣⊥ ∗ = 𝑣⊥/|𝑣⊥| 𝑣⊥ ∗ 𝑤 = cos 𝑡𝜃 𝑢 + sin 𝑡𝜃 𝑣⊥ ∗ ►We can use axis-angle to express any orientation of object in 3D space. public class Orientation { float angle; Vector3 unitAxis; }; ►Pros: ►Fast conversion to matrix representation (sine and cosine for one angle). ►We can use lerp and slerp. ►Easy to understand. ►Low memory footprint. ►Cons: ►Complicated composition of rotations (often solved via other rep.). Axis-angle representation 24 ► Let 𝑎, 𝑏 are real numbers and 𝒊 = −1 be an imaginary unit. Then 𝑎 + 𝑏𝒊 is a complex number (constructed by the pairing process). ► Let 𝑎 + 𝑏𝒊, 𝑐 + 𝑑𝒊 are complex numbers and 𝒋 = −1 be an imaginary unit, 𝒊 ≠ 𝒋. Then 𝑎 + 𝑏𝒊 + 𝑐 + 𝑑𝒊 𝒋 = 𝑎 + 𝑏𝒊 + 𝑐𝒋 + 𝑑𝒊𝒋 = 𝑎 + 𝑏𝒊 + 𝑐𝒋 + 𝑑𝒌 where 𝒌 = 𝒊𝒋, is a quaternion. ► 𝒌 = −1 is another unique imaginary unit, i.e., 𝒌 ≠ 𝒊, 𝒌 ≠ 𝒋. ►Relations between imaginary units: 𝒊𝒋 = 𝒌, 𝒋𝒌 = 𝒊 , 𝒌𝒊 = 𝒋, 𝒋𝒊 = −𝒌, 𝒌𝒋 = −𝒊 , 𝒊𝒌 = −𝒋. Quaternions 25 How to remember these? Think of the cross product of basis vectors 𝒊, 𝒋, 𝒌, e.g., 𝒊 × 𝒋 = 𝒌. ► A quaternion 𝑞 = 𝑠 + 𝑢 𝑥 𝒊 + 𝑢 𝑦 𝒋 + 𝑢 𝑧 𝒌 can be written in a scalar-vector notation as a pair 𝑞 = (𝑠, 𝑢), where the vector 𝑢 = 𝑢 𝑥, 𝑢 𝑦, 𝑢 𝑧 ⊤ . ► Let 𝑞 = 𝑠, 𝑢 , 𝑝 = (𝑡, 𝑣) be quaternions and 𝑐 a real number. Then ► 𝑞 + 𝑝 = 𝑠, 𝑢 + 𝑡, 𝑣 = 𝑠 + 𝑢 𝑥 𝒊 + 𝑢 𝑦 𝒋 + 𝑢 𝑧 𝒌 + 𝑡 + 𝑣 𝑥 𝒊 + 𝑣 𝑦 𝒋 + 𝑣𝑧 𝒌 = (𝑠 + 𝑡, 𝑢 + 𝑣). ► 𝑐𝑞 = (𝑐𝑠, 𝑐𝑢). −1 𝑞 = −𝑞 = (−𝑠, −𝑢). ► 𝑞𝑝 = ⋯ (use distributive law) ⋯ = (𝑠𝑡 − 𝑢 ⋅ 𝑣, 𝑠𝑣 + 𝑡𝑢 + 𝑢 × 𝑣). 𝑐 𝑞𝑝 = 𝑐𝑞 𝑝. ► Conjugation 𝑞∗ = 𝑠 − 𝑢 𝑥 𝒊 − 𝑢 𝑦 𝒋 − 𝑢 𝑧 𝒌 = (𝑠, −𝑢). 𝑐𝑞∗ = 𝑐𝑞 ∗. ► Length 𝑞 = 𝑞𝑞∗ = 𝑠2 + 𝑢 ⋅ 𝑢. If 𝑞 = 𝑝 = 1, then 𝑞𝑝 = 1. ► Additive unit quaternion (0,0), multiplicative unit quaternion (1,0). ► 𝑞−1 = 1 𝑞 2 𝑞∗ . If 𝑞 = 1, then 𝑞−1 = 𝑞∗ . ► 𝑞 + 𝑝 ∗ = 𝑞∗ + 𝑝∗. ► 𝑞𝑝 ∗ = 𝑝∗ 𝑞∗. ► Dot product: 𝑞 ⋅ 𝑝 = 𝑠𝑡 + 𝑢 ⋅ 𝑣. ► Addition and multiplication are associative. Only addition is commutative. Quaternions 26 ►Let 𝑞 = 𝑠, 𝑢 be a quaternion s.t. 𝑞 = 1. Then there exists an angle 𝛼 ∈ ‫ۦ‬0,2𝜋) s.t. 𝑞 = cos 𝛼 , sin 𝛼 𝑣 , where 𝑣 = 0 if |𝑠| = 1, else 𝑣 = 𝑢/ sin 𝛼. Proof: If 𝑠 = 1 ⇒ 𝛼 = 0. Otherwise, 𝑞 = 1 ⇒ 𝑠 < 1 ⇒ 𝛼 = cos−1 𝑠 (choose 𝛼 s.t. sin 𝛼 > 0), 12 = 𝑞 2 = 𝑠2 + 𝑢 ⋅ 𝑢 = cos2 𝛼 + 𝑢 2 ⇒ 𝑢 2 = 1 − cos2 𝛼 = sin2 𝛼 ⇒ 𝑢 = | sin 𝛼 |. ► Let 𝑞 = 𝑠, 𝑢 , 𝑝 = (0, 𝑣) be quaternions s.t. 𝑞 = 1. Then we define 𝑅 𝑞 𝑝 = 𝑞𝑝𝑞−1 = 𝑞𝑝𝑞∗ = ⋯ = (0, 𝑠2 − 𝑢 ⋅ 𝑢 𝑣 + 2 𝑢 ⋅ 𝑣 𝑢 + 2𝑠(𝑢 × 𝑣)). ►Let us compare the Rodrigues' rotation formula with 𝑅 𝑞 𝑝 : cos 𝜃 𝑣 + 1 − cos 𝜃 𝑎 ⋅ 𝑣 𝑎 + sin 𝜃 𝑎 × 𝑣.  rotation formula 𝑠2 − 𝑢 ⋅ 𝑢 𝑣 + 2 𝑢 ⋅ 𝑣 𝑢 + 2𝑠 𝑢 × 𝑣  vector of 𝑅 𝑞 𝑝 ►Question: Is the vector part of 𝑅 𝑞 𝑝 equal to Rodrigues’ formula? Rotation via quaternion 27 ► 𝑞 = 𝑠, 𝑢 can be expressed as 𝑞 = cos 𝛼 , sin 𝛼 𝑎 , 𝑠 = cos 𝛼 , 𝑎 = 𝑢/ sin 𝛼. ►Therefore, the vector part of 𝑅 𝑞 𝑝 is: 𝑠2 − 𝑢 ⋅ 𝑢 𝑣 + 2 𝑢 ⋅ 𝑣 𝑢 + 2𝑠 𝑢 × 𝑣 = cos2 𝛼 − sin2 𝛼 𝑣 + 2 sin2 𝛼 𝑎 ⋅ 𝑣 𝑎 + 2 cos 𝛼 sin 𝛼 𝑎 × 𝑣. So, the angle 𝛼 must satisfy these three equalities (relevant trigonometry identities are on the right): cos 𝜃 = cos2 𝛼 − sin2 𝛼 // cos 2𝛼 = cos2 𝛼 − sin2 𝛼 1 − cos 𝜃 = 2 sin2 𝛼 // cos 2𝛼 = 1 − 2 sin2 𝛼 sin 𝜃 = 2 cos 𝛼 sin 𝛼 // sin 2𝛼 = 2 cos 𝛼 sin 𝛼 A solution exists: 𝜃 = 2𝛼. So, 𝑅 𝑞 𝑝 rotates 𝑝 about axis 𝑎 by 2𝛼. ►Observations, for quaternions 𝑞, 𝑞′, 𝑝 s.t. 𝑞 = 𝑞′ = 1: ► 𝑞∗ 𝑅 𝑞 𝑝 𝑞 = 𝑞∗ 𝑞𝑝𝑞∗ 𝑞 = 𝑝. => 𝑞∗ is the inverse rotation to 𝑞. ► 𝑅−𝑞 𝑝 = −𝑞 𝑝 −𝑞 ∗ = 𝑞𝑝𝑞∗ = 𝑅 𝑞 𝑝 . => −𝑞 is the same rotation as 𝑞. ► 𝑅 𝑞′ 𝑅 𝑞 𝑝 = 𝑅 𝑞′ 𝑞𝑝𝑞∗ = 𝑞′ 𝑞𝑝𝑞∗ 𝑞′∗ = 𝑞′ 𝑞 𝑝 𝑞′ 𝑞 ∗ = 𝑅 𝑞′𝑞 𝑝 . Rotation via quaternion 28 ►Conversion axis-angle to quaternion: A vector 𝑣 ∈ 𝑅3 rotated about a unit axis 𝑎 ∈ 𝑅3 by an angle 𝜃 ∈ ‫ۦ‬0,2𝜋) can be computed using quaternions as 𝑅 𝑞 𝑝 = 𝑞𝑝𝑞∗, where 𝑝 = 0, 𝑣 , 𝑞 = (cos 𝜃/2 , sin 𝜃/2 𝑎). ►Conversion quaternion to axis-angle: Let 𝑞 be a quaternion s.t. 𝑞 = 1 expressed in the form 𝑞 = cos 𝛼 , sin 𝛼 𝑎 , 𝑎 = 1. Then 𝑞 represents rotation about the axis vector 𝑎 by the angle 2𝛼. ►Conversion quaternion to rotation matrix: Let 𝑞 = (𝑠, 𝑢) be a quaternion s.t. 𝑞 = 1. Then the vector part of 𝑅 𝑞 (0, 𝑣) is: 𝑠2 − 𝑢 ⋅ 𝑢 𝑣 + 2 𝑢 ⋅ 𝑣 𝑢 + 2𝑠 𝑢 × 𝑣 = 𝑠2 − (1 − 𝑠2) 𝑣 + 2 𝑢 × 𝑢 × 𝑣 + (1 − 𝑠2)𝑣 + 2𝑠 𝑢 × 𝑣 = 𝑣 + 2 𝑢 2 𝑣 + 2𝑠 𝑢 𝑣 = 𝐼 + 2 𝑢 2 + 2𝑠 𝑢 𝑣 Quaternions and other representations 29 𝑞 2 = 12 = 𝑠2 + 𝑢 ⋅ 𝑢 𝑢 × 𝑢 × 𝑣 = 𝑢 ⋅ 𝑣 𝑢 − 𝑢 ⋅ 𝑢 𝑣 = 𝑢 ⋅ 𝑣 𝑢 − (1 − 𝑠2 )𝑣 ►Given quaternions 𝑞, 𝑝 s.t. 𝑞 = 𝑝 = 1 we can linearly interpolate between them by 𝑡 ∈ 0,1 : 𝑄 𝑡 = 1 − 𝑡 𝑞 + 𝑡𝑝 | 1 − 𝑡 𝑞 + 𝑡𝑝| . ►Technical issues related to | 1 − 𝑡 𝑞 + 𝑡𝑝|: ►Slow – we must compute the square root. ►The result is not defined when =0. ►Problem: The velocity is not constant (increases and decreases) . Visible visual artefact – we prefer uniform blending between rotations. ►Can we do better? ►Yes, use spherical linear interpolation. Linear interpolation (lerp) 30 ►Let us first compute a quaternion Δ𝑞 representing a rotation from a quaternion 𝑞0 = 𝑠, 𝑢 to 𝑞1 = ℎ, 𝑣 . We assume Δ𝑞 = 𝑞0 = 𝑞1 = 1.  Δ𝑞𝑞0 = 𝑞1 Δ𝑞 = 𝑞1 𝑞0 −1 = 𝑞1 𝑞0 ∗ = 𝑠ℎ + 𝑢 ⋅ 𝑣, 𝑠𝑣 − ℎ𝑢 + 𝑢 × 𝑣 = cos 𝛼 , sin 𝛼 𝑎 . where, 𝛼 = cos−1 (𝑠ℎ + 𝑢 ⋅ 𝑣), 𝑎 = 𝑠𝑣 − ℎ𝑢 + 𝑢 × 𝑣 / sin 𝛼 (𝑎 = 0 for 𝛼 = 0, 𝜋). ►For 𝑡 ∈ 0,1 we define Δ𝑞 𝑡 = cos 𝑡𝛼 , sin 𝑡𝛼 𝑎 . So, we get: slerp 𝑞0, 𝑞1, 𝑡 = Δ𝑞 𝑡 𝑞0 = cos 𝑡𝛼 , sin 𝑡𝛼 𝑎 𝑞0. Spherical linear interpolation (slerp) 31 ► Let 𝑞(𝑡) = 𝑠 𝑡 + 𝑢 𝑥 𝑡 𝒊 + 𝑢 𝑦 𝑡 𝒋 + 𝑢 𝑧 𝑡 𝒌 = (𝑠 𝑡 , 𝑢(𝑡)), be a quaternion where 𝑠 𝑡 , 𝑢 𝑥 𝑡 , 𝑢 𝑦 𝑡 , 𝑢 𝑧 𝑡 are functions of 𝑡 ∈ 𝑅. Then we define d𝑞(𝑡) d𝑡 = ሶ𝑞 𝑡 = lim Δ𝑡→0 𝑞 𝑡+Δ𝑡 −𝑞(𝑡) Δ𝑡 = = lim Δ𝑡→0 𝑠 𝑡+Δ𝑡 −𝑠(𝑡) Δ𝑡 + 𝒊 lim Δ𝑡→0 𝑢 𝑥 𝑡+Δ𝑡 −𝑢 𝑥(𝑡) Δ𝑡 + 𝒋 lim Δ𝑡→0 𝑢 𝑦 𝑡+Δ𝑡 −𝑢 𝑦(𝑡) Δ𝑡 + 𝒌 lim Δ𝑡→0 𝑢 𝑧 𝑡+Δ𝑡 −𝑢 𝑧(𝑡) Δ𝑡 = = d𝑠(𝑡) d𝑡 + d𝑢 𝑥(𝑡) d𝑡 𝒊 + d𝑢 𝑦(𝑡) d𝑡 𝒋 + d𝑢 𝑧(𝑡) d𝑡 𝒌 = = d𝑠 𝑡 d𝑡 , d𝑢 𝑡 d𝑡 . ►Note: ∫ 𝑞 𝑡 𝑑𝑡 = ∫ 𝑠 𝑡 𝑑𝑡 + 𝒊∫ 𝑢 𝑥 𝑡 𝑑𝑡 + 𝒋∫ 𝑢 𝑦 𝑡 𝑑𝑡 + 𝒌∫ 𝑢 𝑧 𝑡 𝑑𝑡. Quaternion derivative 32 ► Example: Let’s compute a derivative of the orientation of a frame of reference (orange) rotating a constant angular speed |𝜔| about the unit axis vector ෝ𝜔 = 𝜔/|𝜔|. ►Solution: ►Let 𝑞(𝑡) be an orientation (rotation) of the orange frame in the green one (world). ►We express the rotation about ෝ𝜔: Δ𝑞 Δ𝑡 = cos |𝜔|Δ𝑡 2 , sin |𝜔|Δ𝑡 2 ෝ𝜔 . ► ሶ𝑞 𝑡 = lim Δt→0 𝑞 𝑡+Δ𝑡 − 𝑞(𝑡) Δt = lim Δt→0 Δ𝑞 Δ𝑡 𝑞 𝑡 − 𝑞(𝑡) Δt = lim Δt→0 Δ𝑞 Δ𝑡 − (1, 0) Δt 𝑞(𝑡) Quaternion derivative 33 𝑥 𝑦 𝑧 𝑋 𝑌 𝑍 𝜔 = lim Δ𝑡→0 cos |𝜔|Δ𝑡 2 − 1 Δt , lim Δ𝑡→0 sin |𝜔|Δ𝑡 2 Δ𝑡 ෝ𝜔 𝑞(𝑡) // cos 2𝜑 = 1 − 2sin2 𝜑 = lim Δ𝑡→0 −2sin2 |𝜔|Δ𝑡 4 Δt , lim Δ𝑡→0 sin |𝜔|Δ𝑡 2 Δ𝑡 ෝ𝜔 𝑞(𝑡) = d d𝑡 (−2sin2 |𝜔|𝑡 4 )(0), d d𝑡 (sin |𝜔|𝑡 2 )(0)ෝ𝜔 𝑞(𝑡) = (−2 |𝜔| 4 cos |𝜔|𝑡 4 2sin |𝜔|𝑡 4 )(0), ( |𝜔| 2 cos |𝜔|𝑡 2 )(0)ෝ𝜔 𝑞(𝑡) = 0, |𝜔| 2 ෝ𝜔 𝑞(𝑡) // Observe: 0, |𝜔|ෝ𝜔 represents 𝜔. = 1 2 𝜔𝑞(𝑡). Quaternion derivative 34 ►We can use quaternions to express any orientation of object in 3D space. public class Orientation { // Equals to a quaternion q=(s,u), |q|=1. float s; // the scalar part Vector3 u; // the vector part }; ►Pros: ►Low memory footprint. ►Fast conversion to rotation matrix (no need to compute cosine & sine). ►Fast composition of rotations (just multiply the quaternions). ►We can use lerp and slerp. ►Cons: ►Less human readable. Quaternion representation 35 ► There is no single winner – each representation has pros and cons. ► Examples: ► When specifying a rotation along a coordinate axis (e.g., world Z), then Euler angles are a good choice. ► When specifying a rotation along non-coordinate axis, then axis-angle representation is a good choice. ► When composing rotations of some joint of a skeleton, then quaternions can do it quickly. ► When rotations must be composed with other transformations, then use matrix representation. ► Game engine should provide all representations and conversions between them. What representation to use? 36 ► [1] D.M.Mount; Lecture notes CMSC 425 Game Programming; University of Maryland, 2016. ► [2] E.B.Dam,M.Koch,M.Lillholm; Quaternions, Interpolation and Animation; TechnicalReport DIKU-TR-98/5, University of Copenhagen, 1998. ► [3] M.C.Nechyba; Lecture notes EEL6667: Kinematics, Dynamics and Control of Robot Manipulators - Introduction to quaternions; 2003, https://mil.ufl.edu/nechyba/www/__eel6667.f2003/course_materials. html. ► [4] Y.-B. Jia; Lecture notes (Com S 477/577 Notes) Quaternions; 2018. ► [5] Euler angles: https://www.youtube.com/watch?v=A6lf8t9WXn8 References 37