Reputation: 25914
I have a three.js quaternion and would like to get the axis and angle representation of it. Is this possible in three.js or do I have to do this:
export function getAxisAndAngelFromQuaternion(q: Quaternion) {
const axis = [0, 0, 0];
const angle = 2 * Math.acos(q.w);
if (1 - q.w * q.w < 0.000001) {
// test to avoid divide by zero, s is always positive due to sqrt
// if s close to zero then direction of axis not important
axis[0] = q.x;
axis[1] = q.y;
axis[2] = q.z;
} else {
// http://www.euclideanspace.com/maths/geometry/rotations/conversions/quaternionToAngle/
const s = Math.sqrt(1 - q.w * q.w);
axis[0] = q.x / s;
axis[1] = q.y / s;
axis[2] = q.z / s;
}
return { axis: new Vector3().fromArray(axis), angle };
}
Upvotes: 0
Views: 2349
Reputation: 1410
Is this in a pre-javascript code -- "export"? "q: Quaternion"?
Anyway, it's a relatively simple algorithm. A simplifying suggestion:
function getAxisAndAngelFromQuaternion(q) {
const angle = 2 * Math.acos(q.w);
var s;
if (1 - q.w * q.w < 0.000001) {
// test to avoid divide by zero, s is always positive due to sqrt
// if s close to zero then direction of axis not important
// http://www.euclideanspace.com/maths/geometry/rotations/conversions/quaternionToAngle/
s = 1;
} else {
s = Math.sqrt(1 - q.w * q.w);
}
return { axis: new Vector3(q.x/s, q.y/s, q.z/s), angle };
}
Upvotes: 4