3D Rotation Introduction There are many ways to represent the rotation of an object in space. This note is not exhaustive but just aims at giving an overview of the methods I have used personally at SLAC. Using Axis and Angle This is the very natural geometric way: just specify a unit vector n and an angle of rotation θ about that vector. The general convention is that, when looking in the direction of n, the rotation is positive when counterclockwise. Using Rotation Matrices A rotation matrix is an orthogonal matrix whose determinant is 1. By definition an orthogonal matrix verifies: AAT = I which means that it is always invertible and: A-1 = AT det A = +1 or -1 The condition det A = +1 makes an orthogonal matrix being a rotation matrix. As the result of the definition, the coefficients of a rotation matrix verify the following properties: 1. The dot product of any pair of rows or any pair of columns is 0. 2. The sum of the squares of the elements in any row or column is 1. 3. The rows of the matrix represent the coordinates in the original space of unit vectors along the coordinate axes of the rotated space. 4. The columns of the matrix represent the coordinates in the rotated space of unit vectors along the axes of the original space. 3D Rotation Catherine LeCocq, SLAC – June 2005 – Page 1 Using Euler Angles This means using 3 angles (r1, r2, r3) to represent rotations around 3 coordinate axes. Of course there are different conventions depending on the choice of the coordinate axes but it is a very efficient way of representing a 3D rotation as it uses 3 variables for 3 degrees of freedom. In the accelerator, the usual convention to go from the global system defined by the physics deck to the beam following system is to first translate to the point of interest and then perform the following series of rotations: first yaw (rotation around the third axis), then pitch (rotation around the newly formed second axis) and finally roll (rotation around the first axis or beam direction). There is one major problem with Euler angles known as “Gimbal lock”. This happens when some rotations of π/2 are performed. Suddenly the rotation does not occur due to the alignment of the axes. Let’s look at the case described in the coordinate transformation note, the Euler angles are ω around first axis, Φ around new second axis, κ around new third axis leading to the following rotation matrix: m11 m12 m21 m22 m31 m32 m13 m23 m33 where m11 cos cos m12 sin sin cos cos sin m13 cos sin cos sin sin m21 cos sin m22 sin sin sin cos cos m23 cos sin sin sin cos m31 sin m32 sin cos m33 cos cos when Φ = π/2, the coefficients become: m11 m21 m31 m32 m33 0 m12 sin cos cos sin sin ( ) m13 cos cos sin sin cos ( ) m22 sin sin cos cos cos ( ) m23 cos sin sin cos sin ( ) The coefficients depend only on ω + κ: there is a loss of one degree of freedom. 3D Rotation Catherine LeCocq, SLAC – June 2005 – Page 2 Using Quaternions The quaternions have become popular with the development of new fields like computer vision and robotics. They were developed in the eighteenth century by the Irish mathematician William Rowan Hamilton as a four-dimensional extension of the complex numbers. It can be proven that they can be used to represent rotations in 3D. A common notation for writing a quaternion is: q ( s, v ) where s is a scalar and v a vector. From this it is easy to see that a real number x can be written as the following quaternion q ( x, 0) . Similarly a vector v is also the quaternion q ( 0, v) . Operations can be defined on quaternions: q q ( s s, v v ) - addition q q ( ss v v, sv sv v v ) - multiplication q ( s, v) - conjugate 2 q q q q s2 v - norm q - inverse q 1 2 q 2 q It can be proven that unit quaternions represent 3D rotation. If the rotation is represented by the quaternion q, then a point p (0, p) is rotated into a point p’ given by: p q p q q p q 1 . To convert from an axis and angle representation to quaternion, use the following formula where u is a unit vector describing the axis and θ is the rotation angle around u: q (cos( / 2), sin( / 2) u) To convert from Euler angles to quaternion, just apply the above formula and respect the order of multiplications. For example with the convention ω, Φ and κ: q q q q q ( cos( / 2), (sin( / 2),0,0) ) q ( cos( / 2), (0, sin( / 2),0) ) q ( cos( / 2), (0,0, sin( / 2)) ) To convert from a rotation matrix M to quaternion q can be worked on using these formulas: q ( w, ( x, y, z ) ) 1 2 y 2 2 z 2 2 xy 2 wz 2 xz 2wy 2 2 2 xy 2 wz 1 2 z 2 x 2 yz 2 wx 2 xz 2 wy 2 yz 2 wx 1 2 x 2 2 y 2 Checking of course that the norm of the quaternion is 1, i.e. w2 x 2 y 2 z 2 1 3D Rotation Catherine LeCocq, SLAC – June 2005 – Page 3