Computing Euler angles from a rotation matrix - truesculpt

Computing Euler angles from a rotation matrix - truesculpt Computing Euler angles from a rotation matrix - truesculpt

soi.city.ac.uk
from soi.city.ac.uk More from this publisher
12.07.2015 Views

Computing Euler angles from a rotation matrixGregory G. SlabaughAbstractThis document discusses a simple technique to find all possible Euler angles froma rotation matrix. Determination of Euler angles is sometimes a necessary stepin computer graphics, vision, robotics, and kinematics. However, the solutionmay or may not be obvious.Rotation matricesWe start off with the standard definition of the rotations about the three principleaxes.A rotation of ψ radians about the x-axis is defined asR x (ψ) =⎡⎣ 1 0 00 cos ψ − sin ψ0 sin ψ cos ψSimilarly, a rotation of θ radians about the y-axis is defined as⎡cos θ 0 sin θ⎤R y (θ) = ⎣ 0 1 0 ⎦− sin θ 0 cos θFinally, a rotation of φ radians about the z-axis is defined as⎡cos φ − sin φ 0⎤R z (φ) = ⎣ sin φ cos φ 0 ⎦0 0 1The angles ψ, θ, and φ are the Euler angles.Generalized rotation matricesA general rotation matrix can will have the form,⎡R = ⎣ R ⎤11 R 12 R 13R 21 R 22 R 23⎦R 31 R 32 R 33⎤⎦1

<strong>Computing</strong> <strong>Euler</strong> <strong>angles</strong> <strong>from</strong> a <strong>rotation</strong> <strong>matrix</strong>Gregory G. SlabaughAbstractThis document discusses a simple technique to find all possible <strong>Euler</strong> <strong>angles</strong> <strong>from</strong>a <strong>rotation</strong> <strong>matrix</strong>. Determination of <strong>Euler</strong> <strong>angles</strong> is sometimes a necessary stepin computer graphics, vision, robotics, and kinematics. However, the solutionmay or may not be obvious.Rotation matricesWe start off with the standard definition of the <strong>rotation</strong>s about the three principleaxes.A <strong>rotation</strong> of ψ radians about the x-axis is defined asR x (ψ) =⎡⎣ 1 0 00 cos ψ − sin ψ0 sin ψ cos ψSimilarly, a <strong>rotation</strong> of θ radians about the y-axis is defined as⎡cos θ 0 sin θ⎤R y (θ) = ⎣ 0 1 0 ⎦− sin θ 0 cos θFinally, a <strong>rotation</strong> of φ radians about the z-axis is defined as⎡cos φ − sin φ 0⎤R z (φ) = ⎣ sin φ cos φ 0 ⎦0 0 1The <strong>angles</strong> ψ, θ, and φ are the <strong>Euler</strong> <strong>angles</strong>.Generalized <strong>rotation</strong> matricesA general <strong>rotation</strong> <strong>matrix</strong> can will have the form,⎡R = ⎣ R ⎤11 R 12 R 13R 21 R 22 R 23⎦R 31 R 32 R 33⎤⎦1


This <strong>matrix</strong> can be thought of a sequence of three <strong>rotation</strong>s, one about eachprinciple axis. Since <strong>matrix</strong> multiplication does not commute, the order of theaxes which one rotates about will affect the result. For this analysis, we willrotate first about the x-axis, then the y-axis, and finally the z-axis. Such asequence of <strong>rotation</strong>s can be represented as the <strong>matrix</strong> product,R = R z (φ)R y (θ)R x (ψ)⎡⎤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 <strong>rotation</strong> <strong>matrix</strong> R, we can compute the <strong>Euler</strong> <strong>angles</strong>, ψ, θ, and φby equating each element in R with the corresponding element in the <strong>matrix</strong>product R z (φ)R y (θ)R x (ψ). This results in nine equations that can be used tofind the <strong>Euler</strong> <strong>angles</strong>.Finding two possible <strong>angles</strong> for θStarting with R 31 , we findThis equation can be inverted to yieldR 31 = − sin θ.θ = − sin −1 (R 31 ). (1)However, one must be careful in interpreting this equation. Since sin(π − θ) =sin(θ), there are actually two distinct values (for R 31 ≠ ±1) of θ that satisfyEquation 1. Therefore, both the valuesθ 1 = − sin −1 (R 31 )θ 2 = π − θ 1 = π + sin −1 (R 31 )are valid solutions. We will handle the special case of R 31 = ±1 later in thisreport. So using the R 31 element of the <strong>rotation</strong> <strong>matrix</strong>, we are able to determinetwo possible values for θ.Finding the corresponding <strong>angles</strong> of ψTo find the values for ψ, we observe thatWe use this equation to solve for ψ, asR 32R 33= tan(ψ).ψ = atan2(R 32 , R 33 ), (2)2


where atan2(y, x) is arc tangent of the two variables x and y. It is similar tocalculating the arc tangent of y/x, except that the signs of both arguments areused 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(R 32 , R 33 ). However, when cos(θ) < 0, ψ = atan2(−R 32 , −R 33 ). A simpleway to handle this is to use the equation(R32ψ = atan2cos θ , R )33(3)cos θto compute ψ.Equation 3 is valid for all cases except when cos θ = 0. We will deal with thisspecial case later in this report. For each value of θ, we compute a correspondingvalue of ψ using Equation 3, yielding( )R32 R 33ψ 1 = atan2 ,(4)cos θ 1 cos θ 1ψ 2 = atan2(R32cos θ 2,Finding the corresponding <strong>angles</strong> of φ)R 33cos θ 2A similar analysis holds for finding φ. We observe thatWe solve for φ using the equationφ = atan2R 21R 11= tan φ.(R21cos θ , R )11cos θAgain, this equation is valid for all cases except when cos θ = 0. We willdeal with this special case later in this report. For each value of θ, we computea corresponding value of φ using Equation 6,( )R21 R 11φ 1 = atan2 ,(7)cos θ 1 cos θ 1φ 2 = atan2Two solutions if cos θ ≠ 0(R21cos θ 2,)R 11cos θ 2For the case of cos θ ≠ 0, we now have two triplets of <strong>Euler</strong> <strong>angles</strong> that reproducethe <strong>rotation</strong> <strong>matrix</strong>, namelyBoth of these solutions will be valid.(ψ 1 , θ 1 , φ 1 )(ψ 2 , θ 2 , φ 2 )(5)(6)(8)3


What if cos θ = 0?This technique described above does not work if the R 31 element of the <strong>rotation</strong><strong>matrix</strong> is 1 or −1, which corresponds to θ = −π/2 or θ = π/2, respectively, andto cos θ = 0. When we try to solve for the possible values of ψ and φ usingthe above technique, problems will occur, since the elements R 11 , R 21 , R 32 , andR 33 will all be zero, and therefore Equations 3 and 6 will become( 0ψ = atan20 , 0 0)( 0φ = atan20 , 0 .0)In this case R 11 , R 21 , R 32 , and R 33 do not constrain the values of ψ and φ.Therefore, we must use different elements of the <strong>rotation</strong> <strong>matrix</strong> to compute thevalues of ψ and φ.θ = π/2 case: Consider the case when θ = π/2. Then,R 12 = sin ψ cos φ − cos ψ sin φ = sin(ψ − φ)R 13 = cos ψ cos φ + sin ψ sin φ = cos(ψ − φ)R 22 = sin ψ sin φ + cos ψ cos φ = cos(ψ − φ) = R 13R 23 = cos ψ sin φ − sin ψ cos φ = − sin(ψ − φ) = −R 12Any ψ and φ that satisfy these equations will be a valid solution. Usingthe equations for R 12 and R 13 , we find that(ψ − φ) = atan2(R 12 , R 13 )ψ = φ + atan2(R 12 , R 13 )θ = −π/2 case: Not surprisingly, a similar result holds for the case when θ =−π/2, for whichR 12 = − sin ψ cos φ − cos ψ sin φ = − sin(ψ + φ)R 13 = − cos ψ cos φ + sin ψ sin φ = − cos(ψ + φ)R 22 = − sin ψ sin φ + cos ψ cos φ = cos(ψ + φ) = −R 13R 23 = − cos ψ sin φ − sin ψ cos φ = − sin(ψ + φ) = R 12Again, using the equations for R 12 and R 13 , we find that(ψ + φ) = atan2(−R 12 , −R 13 )ψ = −φ + atan2(−R 12 , −R 13 )4


if (R 31 ≠ ±1)θ 1 = −asin(R 31 )θ 2 = π − θ 1ψ 1 = atan2(R32cos θ 1,ψ 2 = atan2(R32cos θ 2,φ 1 = atan2(R21cos θ 1,)R 33cos θ 1)R 33cos θ 2)R 11cos θ 1)R 11cos θ 2(φ 2 = atan2R21cos θ 2,elseφ = anything; can set to 0if (R 31 = −1)θ = π/2ψ = φ + atan2(R 12 , R 13 )elseθ = −π/2ψ = −φ + atan2(−R 12 , −R 13 )end ifend ifFigure 1: Pseudo-code for computing <strong>Euler</strong> <strong>angles</strong> <strong>from</strong> a <strong>rotation</strong> <strong>matrix</strong>. Seetext for details.Either case: In both the θ = π/2 and θ = −π/2 cases, we have found thatψ and φ are linked. This phenomenon is called Gimbal lock. Althoughin this case, there are an infinite number of solutions to the problem, inpractice, one is often interested in finding one solution. For this task, it isconvenient to set φ = 0 and compute ψ as described above.Pseudo-codeWe now summarize the method by providing a pseudo-code implementation inFigure 1. The code is very simple.ExampleAn example that demonstrates the computation of ψ, θ, and φ <strong>from</strong> a <strong>rotation</strong><strong>matrix</strong> is provided below.Suppose we are asked to find the <strong>Euler</strong> <strong>angles</strong> that produce the <strong>matrix</strong>⎡R = ⎣.5 −.1464 .8536.5 .8536 −.1464−.7071 .5 .5First, we find the possible values for θ to be⎤⎦θ 1 = − sin(−.7071) = π 45


θ 2 = π − θ 1 = 3π 4Then, we find the corresponding values of ψ to be().5ψ 1 = atan2cos(π/4) , .5= π cos(π/4) 4().5ψ 2 = atan2cos(3π/4) , .5= − 3π cos(3π/4) 4And we find φ to be().5φ 1 = atan2cos(π/4) , .5= π cos(π/4) 4().5φ 2 = atan2cos(3π/4) , .5= − 3π cos(3π/4) 4Therefore, the solutions are( π4 , π 4 , π 4)(− 3π 4 , 3π )4 , −3π 4More than one solution?It is interesting to note that there is always more than one sequence of <strong>rotation</strong>sabout 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, thereare two solutions. For the degenerate case of cos θ = 0, an infinite number ofsolutions exist.As an example, consider a book laying on a table face up in front of you.Define the x-axis as to the right, the y-axis as away <strong>from</strong> you, and the z-axisup. A <strong>rotation</strong> of π radians about the y-axis will turn the book so that the backcover is now facing up. Another way to achieve the same orientation would beto rotate the book π radians about the x-axis, and then π radians about thez-axis. Thus, there is more than one way to achieve a desired <strong>rotation</strong>.AcknowledgementI wish to thank Sinisa Segvic, <strong>from</strong> the University of Zagreb, for providing someinsights into how to reduce the number of possible solutions to two for thenon-degenerate case. Also, many thanks to Tobias Ziegler <strong>from</strong> the FraunhoferInstitute for pointing out the need for negative signs on R 12 and R 13 in theθ = −π/2 case.6


References[1] Ken Shoemake, Animating Rotation with Quaternion Curves Proc. SIG-GRAPH 1985, pp. 245 - 254.7

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!