我想知道是否有一种直接的方法来获取四元数,该四元数表示位于其中一个轴(例如 Z 轴)上的向量的旋转,当我有 x、y、z 分量时结果向量。
在上图中,我有 x、y、z 分量,我想将原点在 0,0,0 且平行于 Z 轴的对象(例如一支笔)旋转成一支笔图中矢量方向相同。
我肯定可以使用组件来获得 3 个欧拉角(tan-1(y/z)、tan-1(y/x)、tan-1(x/z)),然后转换为四元数。 但我想知道是否有更好的方法。 我正在使用 Qt ( http://doc.qt.io/qt-5/qquaternion.html ),但如果有一个简单的公式,我可以用 C++ 实现它。 谢谢
最佳答案
要计算从向量到向量的旋转,QQuaternion
中有一个现成的方法: <强> QQuaternion::rotationTo()
.
我做了一个 MCVE检查并演示:
#include <QQuaternion>
#include <QMatrix3x3>
#include <QVector3D>
int main()
{
QVector3D from(0.0f, 0.0f, 1.0f); // z axis
qDebug() << "from: " << from;
QVector3D to(1.0f, 0.0f, 1.0f); // arbitrary target vector
qDebug() << "to : " << to;
QQuaternion rot = QQuaternion::rotationTo(from, to);
qDebug() << "rot. (Quat.): " << rot;
// Unfortunately, I cannot read quaternions.
// so output as axis/angle:
float x, y, z, angle;
rot.getAxisAndAngle(&x, &y, &z, &angle);
qDebug() << "rot. axis: " << QVector3D(x, y, z);
qDebug() << "rog. ang.: " << angle;
// done
return 0;
}
在 VS2013 中编译和测试:
from: QVector3D(0, 0, 1)
to : QVector3D(1, 0, 1)
rot. (Quat.): QQuaternion(scalar:0.92388, vector:(0, 0.382683, 0))
rot. axis: QVector3D(0, 1, 0)
rog. ang.: 45
对我来说,输出看起来很合理......
关于qt - 来自向量 x y z 分量的四元数 - 在 Qt 中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45165313/