我有一个3d点数组,作为std::vector<Eigen::Vector3d>.
我需要用位置和四元数来转换这些点.
我的问题是:
如何用四元数旋转这些点?有没有比以下更快的方式:
Eigen::Vector3d Trans; // position to move by
Eigen::Quaterniond quats; // quat to rotate by
for (int p = 0; p < objectPoints.size(); p++)
{
Eigen::Vector3d pnt;
//add pose
pnt.x = objectPointsTri[p].x + -Trans.x();
pnt.y = objectPointsTri[p].y + -Trans.y();
pnt.z = objectPointsTri[p].z + -Trans.z();
Eigen::Vector3d pntRot = // rotate pnt by the quaternion
}
Run Code Online (Sandbox Code Playgroud)
@ggael 的答案是完全正确的,我只想提供一些背景知识。
\n\n在这篇维基百科文章中,他们解释了四元数向量乘法 v\xe2\x80\x99 = qvq -1。我们正在使用的Eigen 简写operator*显然也在Unity 库中。
在当前版本的 Eigen 中,您将选择的重载operator*,它调用_transformVector
template<typename RotationDerived,typename OtherVectorType>\nstruct rotation_base_generic_product_selector<RotationDerived,OtherVectorType,true>\n{\n ...\n EIGEN_DEVICE_FUNC static EIGEN_STRONG_INLINE ReturnType run(const RotationDerived& r, const OtherVectorType& v)\n {\n return r._transformVector(v);\n }\n};\nRun Code Online (Sandbox Code Playgroud)\n\n请参阅此处的备注:_transformVector
\n\n\n如果四元数用于旋转多个点 (>1),那么首先将其转换为 3x3 矩阵会更有效。n次变换的运行成本比较:
\n\n\n
\n- 四元数2:30n
\n- 通过矩阵3:24 + 15n
\n
出于这些效率原因,ggael 要求您改变解决问题的方式。
\n操作员*将完成这项工作,您当然可以简化代码:
pnt = objectPointsTri[p] - Trans;
pntRot = quat * pnt;
Run Code Online (Sandbox Code Playgroud)
甚至:
pnt = quat * (objectPointsTri[p] - Trans);
Run Code Online (Sandbox Code Playgroud)
或者,如果您将积分存储在Matrix3Xd:
Matrix3Xd in_pts;
Matrix3Xd out_pts;
Affine3d T = quats * Translation3d(-Trans);
out_pts = T * in_pts;
Run Code Online (Sandbox Code Playgroud)