我有两个四元数组成我需要的旋转。但是,其中一个包含 Yaw 旋转,另一个包含 Pitch 和 Roll。
我需要做的是从四元数 A 中提取 Yaw,从四元数 B 中提取 Pitch 和 Roll,然后将它们组合成四元数 C。
我有:
#include <Eigen/Dense>
#define M_PI 3.14159265358979323846264338327950288
int main()
{
Eigen::Quaternionf quatA(0.775, 0.321, -0.208, 0.503);
Eigen::Quaternionf quatB(0.364, 0.606, 0.364, 0.606);
//A
auto eulerA = quatA.toRotationMatrix().eulerAngles(0, 1, 2);
float rollA = eulerA.x();
float yawA = eulerA.y();
float pitchA = eulerA.z();
std::cout << "rotation A" << std::endl;
std::cout << eulerA.x() * 180 / M_PI << " " << eulerA.y() * 180 / M_PI << " " << eulerA.z() * 180 / M_PI << std::endl;
//B
auto eulerB = quatB.toRotationMatrix().eulerAngles(0, 1, 2);
float rollB = eulerB.x();
float yawB = eulerB.y();
float pitchB = eulerB.z();
std::cout << "rotation B" << std::endl;
std::cout << eulerB.x() * 180 / M_PI << " " << eulerB.y() * 180 / M_PI << " " << eulerB.z() * 180 / M_PI << std::endl;
//create a new quat from just the Yaw.
Eigen::Quaternionf QuatYaw = Eigen::AngleAxisf(0, Eigen::Vector3f::UnitX())
* Eigen::AngleAxisf(yawB, Eigen::Vector3f::UnitY())
* Eigen::AngleAxisf(0, Eigen::Vector3f::UnitZ());
//Test the yaw rotation value
auto testYaw = QuatYaw.toRotationMatrix().eulerAngles(0, 1, 2);
std::cout << "Yaw value: " << testYaw.x() << " " << testYaw.y() * 180 / M_PI << " " << testYaw.z() << std::endl;
//A back to quat
Eigen::Quaternionf quatA_back = Eigen::AngleAxisf(rollA, Eigen::Vector3f::UnitX())
* Eigen::AngleAxisf(yawA, Eigen::Vector3f::UnitY())
* Eigen::AngleAxisf(pitchA, Eigen::Vector3f::UnitZ());
Eigen::Quaternionf resultQuat = quatA_back * QuatYaw;
//result
auto eulerResult = resultQuat.toRotationMatrix().eulerAngles(0, 1, 2);
std::cout << "result" << std::endl;
std::cout << eulerResult.x() * 180 / M_PI << " " << eulerResult.y() * 180 / M_PI << " " << eulerResult.z() * 180 / M_PI << std::endl;
}
这给了我:
rotation A
44.976 0.0301393 65.955
rotation B
-0 89.9693 117.99
Yaw value: -0 89.9693 0
result
134.996 155.955 -89.9807
当我期望得到以下结果时:
44.976 89.9693 65.955
我怎样才能以这种方式合并四元数轴?我哪里错了?