Eigen 中的 Quaternion

參考網址:
http://eigen.tuxfamily.org/dox-devel/classEigen_1_1Quaternion.html

http://www.cc.gatech.edu/classes/AY2015/cs4496_spring/Eigen.html

Eigen中quaternion的構造函數爲
Quaternion (const Scalar &w, const Scalar &x, const Scalar &y, const Scalar &z),注意w在前。然而在內部存儲時eigen將四元數的w放在最後
例如通過Eigen::Vector4d q = q_AB.coeffs();訪問時,q中的最後一個元素纔是w。

Eigen中用四元數表示向量的旋轉
Quaternion:

Eigen::Quaterniond q(2, 0, 1, -3);
std::cout << “This quaternion consists of a scalar ” << q.w() << ” and a vector ” << std::endl << q.vec() << std::endl;
q.normalize();
std::cout << “To represent rotation, we need to normalize it such that its length is ” << q.norm() << std::endl;
Eigen::Vector3d v(1, 2, -1);
Eigen::Quaterniond p;
p.w() = 0;
p.vec() = v;
Eigen::Quaterniond rotatedP = q * p * q.inverse();
Eigen::Vector3d rotatedV = rotatedP.vec();
std::cout << “We can now use it to rotate a vector ” << std::endl << v << ” to ” << std::endl << rotatedV << std::endl;

Eigen::Matrix3d R = q.toRotationMatrix(); // convert a quaternion to a 3x3 rotation matrix
std::cout << “Compare with the result using an rotation matrix ” << std::endl << R * v << std::endl;

Eigen::Quaterniond a = Eigen::Quterniond::Identity();
Eigen::Quaterniond b = Eigen::Quterniond::Identity();
Eigen::Quaterniond c; // Adding two quaternion as two 4x1 vectors is not supported by the EIgen API. That is, c = a + b is not allowed. We have to do this in a hard way
c.w() = a.w() + b.w();
c.x() = a.x() + b.x();
c.y() = a.y() + b.y();
c.z() = a.z() + b.z();

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章