qt - 来自向量 x y z 分量的四元数 - 在 Qt 中

标签 qt 3d rotation quaternions euler-angles

我想知道是否有一种直接的方法来获取四元数,该四元数表示位于其中一个轴(例如 Z 轴)上的向量的旋转,当我有 x、y、z 分量时结果向量。

enter image description here

在上图中,我有 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/

相关文章:

c++ - 在 qml 列表中显示查询结果

c++ - 如何在 Qt 程序中实现 IDv3 标签?

ios - 如何快速在 sceneKit 中连续闪烁 3d 对象?

opengl - 尝试在 openGL 中绘制 3D 线图

c++ - 当仅需要 X-Y 旋转时,3D 对象沿所有三个轴旋转

ios - 强制旋转 UIViewController

c++ - QAbstractItemModel 子类线程安全吗?

QTableView- 覆盖 QAbstractTableModel 中的 CSS

ios - 如何在 Scene Kit 的 3D SCNNode 中添加 2D View

java - 在 Android 中旋转文本(不是图像)