我有一个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
}
运营商*
将完成这项工作,您当然可以简化代码:
pnt = objectPointsTri[p] - Trans;
pntRot = quat * pnt;
甚至:
pnt = quat * (objectPointsTri[p] - Trans);
或者,如果您将积分存储在Matrix3Xd
中:
Matrix3Xd in_pts;
Matrix3Xd out_pts;
Affine3d T = quats * Translation3d(-Trans);
out_pts = T * in_pts;
@ggael的答案是完全正确的,我只是想提供一些背景知识。
在this Wikipedia article中,他们解释了四元数 - 向量乘法v'= qvq-1。我们使用的operator*
的Eigen简写也显然在Unity libraries。
在当前版本的Eigen中,你将选择this overload的operator*
,它称为_transformVector
template<typename RotationDerived,typename OtherVectorType>
struct rotation_base_generic_product_selector<RotationDerived,OtherVectorType,true>
{
...
EIGEN_DEVICE_FUNC static EIGEN_STRONG_INLINE ReturnType run(const RotationDerived& r, const OtherVectorType& v)
{
return r._transformVector(v);
}
};
请参阅_transformVector
here上的备注:
如果四元数用于旋转多个点(> 1),那么首先将其转换为3x3矩阵会更有效。 n次转换的运营成本比较:
- 四元数2:30n
- 通过Matrix3:24 + 15n
ggael要求您根据这些效率原因更改解决问题的方法。