你如何得到三.js四元数的轴和角度表示?

  • 本文关键字:表示 四元数 何得 js three.js
  • 更新时间 :
  • 英文 :


我有一个三.js四元数,想得到它的轴和角度表示。这在三个方面可能吗.js还是我必须这样做:

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 };
}

这是在 javascript 之前的代码 - "导出"吗? "问:四元数"?
无论如何,这是一个相对简单的算法。 一个简化的建议:

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 };
}

最新更新