Maths - Conversion Matrix to Axis Angle

Matrix to Axis Angle Fix By: Dominik Ries (hzdgopher) - 2008-09-23 11:11
Well first of all, thanks for that great site :)

I've been using the matrix to axis angle code snippet for creating vrml exports of a scenegraph.
It worked really well until a few days ago when i apparently encoutered a case which is not handled correctly in the code.

My matrix looks like this:
-0.141 0.111 0.983
0.111 -0.985 0.127
0.983 0.127 0.126

The result I get with the code from this site is:
axis(0.655, 0.084, 0.750), angle(PI)

but should rather be:
axis(-0.655, 0.084, 0.750), angle(PI)

So the conversion somehow didnt flip the sign of the axis x component.
Since the matrix is symmetrical and is no identity matrix, its the code branch for the 180deg rotation which seems to produce that error.

To determine the sign of the axis components there is a permutation table given from which the code was derieved, but the comment in the code snippet confuses me a bit:
if (xZero && !yZero && !zZero) { // implements last 6 rows of above table

If I look at the permutation table and go through my matrix values by hand I come to the case in the table row 7 (- - + Use -x)
which is not in the last 6 rows of the table but needs to be considered as well (as well as the cases in row 5 and 6) imho.

I added this in my code and it works fine. My sign adjustment code in the 180deg rotation case reads now (sorry for vb code) :

Dim xy As Double
Dim xz As Double
Dim yz As Double
If (Math.Abs(x) < epsilon1) Then x = 0
If (Math.Abs(y) < epsilon1) Then y = 0
If (Math.Abs(z) < epsilon1) Then z = 0
xy = m(3)
xz = m(6)
yz = m(7)

' table rows 5-7
If ((x <> 0) And (y <> 0) And (z <> 0)) Then
If ((xy > 0) And (xz < 0) And (yz < 0)) Then: z = -z
If ((xy < 0) And (xz > 0) And (yz < 0)) Then: y = -y
If ((xy < 0) And (xz < 0) And (yz > 0)) Then: x = -x

' table row 12
ElseIf ((xy = 0) And (xz = 0) And (yz = 0)) Then
x = 0
y = 1
z = 0

' table rows 13 and 16
ElseIf ((x = 0) And (y <> 0) And (z <> 0)) Then
If (yz < 0) Then: y = -y

' table rows 14 and 17
ElseIf ((x <> 0) And (y = 0) And (z <> 0)) Then
If (xz < 0) Then: z = -z

' table rows 15 and 18
ElseIf ((x <> 0) And (y <> 0) And (z = 0)) Then
If (xy < 0) Then: x = -x

End If

Maybe someone can confirm this. I'm not too deep into maths, but at least it seems to work now for my matrix.

Dominik Ries

RE: Matrix to Axis Angle Fix (New) By: Martin Baker (martinbakerProject Admin) - 2008-10-01 10:24
I have just updated the web page, sorry it took so long, thank you very much for the the correction and the comments.

As I think this thread may be useful to others I have linked to it from the webpage assuming that's OK with you.




metadata block
see also:


Correspondence about this page

Book Shop - Further reading.

Where I can, I have put links to Amazon for books that are relevant to the subject, click on the appropriate country flag to get more details of the book or to buy it from them.

cover Introductory Techniques for 3-D Computer Vision by Emanuele Trucco, Alessandro Verri


This site may have errors. Don't use for critical systems.

Copyright (c) 1998-2017 Martin John Baker - All rights reserved - privacy policy.