Now quaternion-vector multiplication is as simple as converting a vector into a quaternion (by setting w = 0 and leaving x, y, and z the same) and then multiplying q * v * q_conjugate(q):def qv_mult(q1, v1): q2 = (0.0,) + v1 return q_mult(q_mult(q1, q2), q_conjugate(q1))[1:]最后,您需要知道如何将轴角旋转转换为四元数.还容易!通过调用normalize在此处清理"输入和输出很有意义.Finally, you need to know how to convert from axis-angle rotations to quaternions. Also easy! It makes sense to "sanitize" input and output here by calling normalize.def axisangle_to_q(v, theta): v = normalize(v) x, y, z = v theta /= 2 w = cos(theta) x = x * sin(theta) y = y * sin(theta) z = z * sin(theta) return w, x, y, z然后返回:def q_to_axisangle(q): w, v = q[0], q[1:] theta = acos(w) * 2.0 return normalize(v), theta这是一个快速的用法示例.围绕x,y和z轴旋转90度的序列会将y轴上的矢量返回到其原始位置.此代码执行这些轮换:Here's a quick usage example. A sequence of 90-degree rotations about the x, y, and z axes will return a vector on the y axis to its original position. This code performs those rotations:x_axis_unit = (1, 0, 0)y_axis_unit = (0, 1, 0)z_axis_unit = (0, 0, 1)r1 = axisangle_to_q(x_axis_unit, numpy.pi / 2)r2 = axisangle_to_q(y_axis_unit, numpy.pi / 2)r3 = axisangle_to_q(z_axis_unit, numpy.pi / 2)v = qv_mult(r1, y_axis_unit)v = qv_mult(r2, v)v = qv_mult(r3, v)print v# output: (0.0, 1.0, 2.220446049250313e-16)请记住,这种旋转顺序不会将所有向量返回到相同位置;例如,对于x轴上的矢量,它将对应于围绕y轴旋转90度. (此处请牢记右手法则;围绕y轴的正向旋转会将x轴上的矢量推入负 z区域.)Keep in mind that this sequence of rotations won't return all vectors to the same position; for example, for a vector on the x axis, it will correspond to a 90 degree rotation about the y axis. (Keep the right-hand-rule in mind here; a positive rotation about the y axis pushes a vector on the x axis into the negative z region.)v = qv_mult(r1, x_axis_unit)v = qv_mult(r2, v)v = qv_mult(r3, v)print v# output: (4.930380657631324e-32, 2.220446049250313e-16, -1.0)一如既往,如果您在此处发现任何问题,请告诉我.As always, please let me know if you find any problems here. 这篇关于通过四元数旋转坐标系的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云! 08-29 21:58