Maths - Quaternion to AxisAngle - Forum Discussion

By: Wayne C. Gramlich - wgramlich
file Bug in Quaternion to Angle Axis Conversion?  
2004-07-28 16:11

First, I have been finding the site incredibly useful. Thank
you very much!!!

In reading the Java code for converting a quaternion to
an angle axis, I ran across the following snippet of code:

<Pre>

public void set(Quat4d q1) {
angle = 2 * Math.acos(q1.w);
double s = Math.sqrt(1-q1.w*q1.w);
if (Math.abs(s) < 0.001) s=1;
x = q1.x / s;
y = q1.y / s;
z = q1.z / s;
}

</Pre>

It looks to me like the if statement is trying to
perform the small angle cosine optimization
where theta ~= 0 => cosine(theta) ~= 1.
However, the test is for abs(s) instead of
abs(angle).

My confidence on these topics is quite low,
so please forgive me if I've bungled my
understanding.

For completeness, here is the URL:

https://www.euclideanspace.com/maths/geometry/rotations/conversions/quaternionToAngle/index.htm

Thanks,

-Wayne

By: Martin Baker - martinbaker
file RE: Bug in Quaternion to Angle Axis Conversion?  
2004-07-29 01:25

Hi Wayne,

I think this is just to avoid the divide by zero when:
w = 1
s = 0
angle = 0
although perhaps this is the same thing?
Its an interesting question, what should we do when angle =0? Perhaps it would be better to set:
x=1
y=0
z=0
as this makes sure the axis is normalised even though its value should not matter?

Martin

By: Wayne C. Gramlich - wgramlich
file RE: Bug in Quaternion to Angle Axis Conversio  
2004-07-29 15:51

Martin:

I think you are right. What threw me for a loop is that
the sqrt() function can never return a positive number,
so the abs(s) is unnecessary. I think the code
should probably be something like:

if (s < .001) {
x = 1; y = 0; z = 0;
} else {
the usuual...

I think I get it now. (This is why I don't trust myself
on these issues; I typically get them wrong. ;-)

Thanks,

-Wayne

By: Martin Baker - martinbaker
file RE: Bug in Quaternion to Angle Axis Conversion?  
2004-07-29 23:16

Wayne,

Good point, I have removed the abs and updated the webpage:
https://www.euclideanspace.com/maths/geometry/rotations/conversions/quaternionToAngle/

I made the setting of x=1;y=z=0; an option depending on the importance of the axis being normalised vs. getting the direction as close as possible for very small angles.

Thanks very much,

Martin


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 3D Math Primer - Aimed at complete beginners to vector and matrix algebra.

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

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