# Maths - Conversion Matrix to Axis Angle - Open Forum Discussion

 By: s_ludwig ( Sven Ludwig ) problem in computing Axis and Angle from R   2003-08-31 20:36 Hi, I have a concern about the ambiguities when computing the axis and angle representation from a 3x3 rotation matrix. When the absolute value of the angle of rotation t encoded in the rotation matrix gets bigger than pi radians, the method returns the angle 2pi - t . Therefore, at the moment it seems to me that I cannot determine the correct angle at all (or at least get the complementary angle), because if I have a result I do not know whether the angle was greater than or less than pi. I tried another method presented in the book "Introductory Techniques for 3D Computer Vision". The two eigenvalues that are not equal to 1 of a 3x3 rotation matrix (other than the 3x3 identity matrix which has all three eigenvalues equal to 1) are complex conjugates to each other and contain information about the angle of rotation. The eigenvalues are of the form 1, cos(t) + i * sin(t) and cos(t) - i * sin(t) corresponding to 1, exp(i * t) and exp(-i * t). This should allow one compute the angle (up to the ambiguity between complementary angles in a full arc) directly from the eigenvalues of a rotation matrix. For example, if e2 and e3 are the eigenvalues not equal to 1, the angle can be computed by t = acos(real(e1)) where real() just extracts the real part of a complex number. Note that there is also the ambiguity in the sign of the angle, which means you do not know if you have the correct actual angle that was encoded in the matrix or its complementary angle, which is ok because two rotation about two complementary angles is are same. But the eigenvalue method seems to have the same problems with angles bigger than pi. There is another way of computing t via t = imag(log(e2)) or -t = imag(log(e3)) but this has seems to have the same problem with angles that have abs(t) > pi. Trucco and Verri suggest to compare the resulting rotation matrices with the original to determine the valid angle. I am doing this but it seems to me that always several pairs of rotation matrices equal among the different versions, which should be ok having complementary angles in a full arc, e.g. -0.5pi and 1.5pi, but it seems to be in other cases to when the direction of the axis gets inverted. I'd appreciate any comments on this. Best Regards, Sven
 By: martinbaker ( Martin Baker ) RE: problem in computing Axis and Angle from R   2003-09-01 03:40 Hi Sven, Can I just check if I understand the problem? It seems to me that with axis-angle there are always many ways to represent the rotation, we can go the short way round (which will be an angle between -pi and +pi) or we can go the long way round (which will be an angle between +-pi and +-2pi) or multiples of 2pi of these. I guess in most cases we want to find the shortest rotation? In matrix notation there does not seem to be this distinction, so I guess the conversion from matrix to axis-angle is one to many, but there is a one-to-one relationship between matrix and shortest axis angle? We will get this if we choose in acos implimentation that returns angles angle between -pi and +pi? Martin
 By: s_ludwig ( Sven Ludwig ) RE: problem in computing Axis and Angle from   2003-09-01 16:13 Hi Martin, thank you for your help. The problem I have at the moment is it seems to me that one gets the angle (the smaller of the two complementary) but does not know about the sign then. For example for a rotation matrix R representing a rotation about an axis a = [42.25 -2.5 -75]', which normalized is a_hat =~ [0.4906, -0.0290, -0.8709], and an angle t = 2*pi - 0.3 I get back the result ab =~ [-0.4906, 0.0290, 0.8709] and tb = 0.3 A good result would be tc = -tb and ac = -ab though. I cannot find any bug in my program. I am comparing the rotation matrices resulting conversions for all alternations (changing sign, taking complementary angle) of the result ab/tb with the original R and take the first matching one. However, in this case this tells me 0.3 should be correct. I think it might be because of the complementary direction of the axis. I one inverts the axis, does one also have to invert the angle to represent the same rotation? The method to obtain a rotation matrix from the axis and angle representation tells me yes, one has to. If it would be so, there are even more ways to represent the same rotation matrix by axis and angle. Namely, for an angle t in range [-2*pi,2*pi] (without zero) and an axis a these equivalent combinations would be t, a t - (t / abs(t)) * 2 * pi, a -t, -a -(t - (t / abs(t)) * 2 * pi), -a where t - (t / abs(t)) * 2 * pi gives the complementary angle of t's in the defined range. Do you already have some program ready to convert from axis and angle to rotation matrices? If so, could you backcheck this by just trying these combinations for a non-trivial axis and angle pair and comparing the resulting matrices? Thanks again, Sven
 By: s_ludwig ( Sven Ludwig ) RE: problem in computing Axis and Angle from   2003-09-01 16:54 Hi, I just found a page where the problem with inverting the direction of the axis and the sign of the angle is stated, http://www.ecse.rpi.edu/Homepages/wrf/misc_notes/rotation.html under the headline "Finding the Axis and Angle of a Rotation Matrix" Sven
 By: martinbaker ( Martin Baker ) RE: problem in computing Axis and Angle from R   2003-09-02 10:57 Hi Sven, I think I may have found a problem, in the Matrix to Axis Angle the angle is calculated as follows: angle = 2 * acos(sqrt(1.0 + m00 + m11 + m22) / 2.0) I had a look at the documentation for both the java and C# Math libraries and they both produce results in the following ranges: asin input: -1 < n < 1 output: -pi/2 < n < pi/2 acos input: -1 < n < 1 output: 0 < n < pi atan input: -1 < n < 1 output: -pi/2 < n < pi/2 So unlike the others acos always produces a positive result (I did not realise before that acos is different than the others). Perhaps we can correct this, to give the shortest rotation, by adding an extra line: if (angle > Math.pi) angle -= Math.pi; Other maths libraries may do this differently so this extra line may not be necessary in all cases, but it should not do any harm? The other issue is that, if the angle and the axis are both inverted this will produce the same rotation. (I remember the same issue coming up with quaternions, ie (w,x,y,z) and (-w,-x,-y,-z) both represent the same rotation). The question is: can we say that one of these is right and the other is wrong? are they both equally valid ways to represent the same rotation? It seems to me that rotation has 3 degrees of freedom, but we are expessing it with 4 numbers, so it is always possible that the redundant information can produce more than one way to do the same thing? I am grateful to you in letting me know about this, I'll update the web page to cover these issues. Also, if its alright with you I'll include a link to your messages. I don't know if these would cause what you are seeing? if not, do you have the matrix values for the rotation you are trying to find? Martin
 By: s_ludwig ( Sven Ludwig ) RE: problem in computing Axis and Angle from   2003-09-02 18:02 Hi Martin, thank you very much for the help. I don't mind if you link to the messages. By the way, matlab also has acos in range [0,pi]. I do not see at the moment how you use if (angle > Math.pi) angle -= Math.pi; as a correction. Do you use the equation after retrieving the angle from arccos? Then it should not become greater than pi. Or do you use it before constructing a rotation matrix? Yes, I have some values. The following matrix I constructed from the angle 2.0*pi-0.3 and the axis [42.25, -2.5, -75]^T : R = 0.96608673169969 -0.25800404198456 -0.01050433974302 0.25673182392846 0.95537412871306 0.14611312318926 -0.02766220194012 -0.14385474794174 0.98921211783846 I guess this way some precision is lost. I have another remark, on the hompage of R. Franklin (the link is in my last posting) he also presents another equation to solve for the angle: t = arccos ( (r00 + r11 + r22 - 1)/2) I tried this and so far it seems to be equivalent to the equation you presented, t = 2 * acos(sqrt(1 + r00 + r11 + r22) / 2). The former should be somewhat faster because it does not involve a square root. But I haven't found a proof for the equivalence, yet. I just tried different matrices and it seem to be the same. Best Regards, Sven