You are on page 1of 7

Computing Euler angles from a rotation matrix

Gregory G. Slabaugh Abstract


This document discusses a simple technique to nd all possible Euler angles from a rotation matrix. Determination of Euler angles is sometimes a necessary step in computer graphics, vision, robotics, and kinematics. However, the solution may or may not be obvious.

Rotation matrices
We start o with the standard denition of the rotations about the three principle axes. A rotation of radians about the x-axis is dened as 1 0 0 Rx () = 0 cos sin 0 sin cos Similarly, a rotation of radians about cos 0 Ry () = sin the y-axis is dened as 0 sin 1 0 0 cos

Finally, a rotation of radians about the z-axis is dened as cos sin 0 Rz () = sin cos 0 0 0 1 The angles , , and are the Euler angles.

Generalized rotation matrices


A general rotation matrix can will have the form, R11 R12 R13 R = R21 R22 R23 R31 R32 R33

This matrix can be thought of a sequence of three rotations, one about each principle axis. Since matrix multiplication does not commute, the order of the axes which one rotates about will aect the result. For this analysis, we will rotate rst about the x-axis, then the y-axis, and nally the z-axis. Such a sequence of rotations can be represented as the matrix product,

= Rz ()Ry ()Rx () cos cos sin sin cos cos sin cos sin cos + sin sin = cos sin sin sin sin + cos cos cos sin sin sin cos sin sin cos cos cos

Given a rotation matrix R, we can compute the Euler angles, , , and by equating each element in R with the corresponding element in the matrix product Rz ()Ry ()Rx (). This results in nine equations that can be used to nd the Euler angles.

Finding two possible angles for


Starting with R31 , we nd R31 = sin . This equation can be inverted to yield = sin1 (R31 ). (1)

However, one must be careful in interpreting this equation. Since sin( ) = sin(), there are actually two distinct values (for R31 = 1) of that satisfy Equation 1. Therefore, both the values 1 2 = sin1 (R31 ) = 1 = + sin1 (R31 )

are valid solutions. We will handle the special case of R31 = 1 later in this report. So using the R31 element of the rotation matrix, we are able to determine two possible values for .

Finding the corresponding angles of


To nd the values for , we observe that R32 = tan(). R33 We use this equation to solve for , as = atan2(R32 , R33 ), (2)

where atan2(y, x) is arc tangent of the two variables x and y. It is similar to calculating the arc tangent of y/x, except that the signs of both arguments are used to determine the quadrant of the result, which lies in the range [, ]. The function atan2 is available in many programming languages. One must be careful in interpreting Equation 2. If cos() > 0, then = atan2(R32 , R33 ). However, when cos() < 0, = atan2(R32 , R33 ). A simple way to handle this is to use the equation = atan2 R32 R33 , cos cos (3)

to compute . Equation 3 is valid for all cases except when cos = 0. We will deal with this special case later in this report. For each value of , we compute a corresponding value of using Equation 3, yielding 1 2 = = atan2 atan2 R32 R33 , cos 1 cos 1 R33 R32 , cos 2 cos 2 (4) (5)

Finding the corresponding angles of


A similar analysis holds for nding . We observe that R21 = tan . R11 We solve for using the equation = atan2 R21 R11 , cos cos (6)

Again, this equation is valid for all cases except when cos = 0. We will deal with this special case later in this report. For each value of , we compute a corresponding value of using Equation 6, 1 2 = = atan2 atan2 R21 R11 , cos 1 cos 1 R21 R11 , cos 2 cos 2 (7) (8)

Two solutions if cos = 0


For the case of cos = 0, we now have two triplets of Euler angles that reproduce the rotation matrix, namely (1 , 1 , 1 ) (2 , 2 , 2 ) Both of these solutions will be valid. 3

What if cos = 0?
This technique described above does not work if the R31 element of the rotation matrix is 1 or 1, which corresponds to = /2 or = /2, respectively, and to cos = 0. When we try to solve for the possible values of and using the above technique, problems will occur, since the elements R11 , R21 , R32 , and R33 will all be zero, and therefore Equations 3 and 6 will become = atan2 atan2 0 0 , 0 0 0 0 , 0 0

In this case R11 , R21 , R32 , and R33 do not constrain the values of and . Therefore, we must use dierent elements of the rotation matrix to compute the values of and .

= /2 case: Consider the case when = /2. Then, R12 R13 R22 R23 = sin cos cos sin = sin( ) = cos cos + sin sin = cos( ) = sin sin + cos cos = cos( ) = R13 = cos sin sin cos = sin( ) = R12

Any and that satisfy these equations will be a valid solution. Using the equations for R12 and R13 , we nd that ( ) = atan2(R12 , R13 ) = + atan2(R12 , R13 )

= /2 case: Not surprisingly, a similar result holds for the case when = /2, for which R12 R13 R22 R23 = = = = sin cos cos sin = sin( + ) cos cos + sin sin = cos( + ) sin sin + cos cos = cos( + ) = R13 cos sin sin cos = sin( + ) = R12

Again, using the equations for R12 and R13 , we nd that ( + ) = atan2(R12 , R13 ) = + atan2(R12 , R13 )

if (R31 = 1) 1 = asin(R31 ) 2 = 1 R R 1 = atan2 cos321 , cos331 2 = atan2 1 = atan2


R32 cos 2 , R21 cos 1 , R21 cos 2 , R33 cos 2 R11 cos 1 R11 cos 2

2 = atan2 else = anything; can set to 0 if (R31 = 1) = /2 = + atan2(R12 , R13 ) else = /2 = + atan2(R12 , R13 ) end if end if Figure 1: Pseudo-code for computing Euler angles from a rotation matrix. See text for details. Either case: In both the = /2 and = /2 cases, we have found that and are linked. This phenomenon is called Gimbal lock. Although in this case, there are an innite number of solutions to the problem, in practice, one is often interested in nding one solution. For this task, it is convenient to set = 0 and compute as described above.

Pseudo-code
We now summarize the method by providing a pseudo-code implementation in Figure 1. The code is very simple.

Example
An example that demonstrates the computation of , , and from a rotation matrix is provided below. Suppose we are asked to nd the Euler angles that produce the matrix .5 .1464 .8536 .5 .8536 .1464 R= .7071 .5 .5 First, we nd the possible values for to be 1 = sin(.7071) = 5 4

= 1 =

3 4

Then, we nd the corresponding values of to be 1 2 And we nd to be 1 2 = = atan2 atan2 .5 .5 , = cos(/4) cos(/4) 4 .5 .5 3 , = cos(3/4) cos(3/4) 4 = = atan2 atan2 .5 .5 , = cos(/4) cos(/4) 4 .5 .5 3 , = cos(3/4) cos(3/4) 4

Therefore, the solutions are , , 4 4 4 3 3 3 , , 4 4 4

More than one solution?


It is interesting to note that there is always more than one sequence of rotations about the three principle axes that results in the same orientation of an object. As we have shown in this report, in the non-degenerate case of cos = 0, there are two solutions. For the degenerate case of cos = 0, an innite number of solutions exist. As an example, consider a book laying on a table face up in front of you. Dene the x-axis as to the right, the y-axis as away from you, and the z-axis up. A rotation of radians about the y-axis will turn the book so that the back cover is now facing up. Another way to achieve the same orientation would be to rotate the book radians about the x-axis, and then radians about the z-axis. Thus, there is more than one way to achieve a desired rotation.

Acknowledgement
I wish to thank Sinisa Segvic, from the University of Zagreb, for providing some insights into how to reduce the number of possible solutions to two for the non-degenerate case. Also, many thanks to Tobias Ziegler from the Fraunhofer Institute for pointing out the need for negative signs on R12 and R13 in the = /2 case.

References
[1] Ken Shoemake, Animating Rotation with Quaternion Curves Proc. SIGGRAPH 1985, pp. 245 - 254.

You might also like