本文作者:StubbornHuang
版权声明:本文为站长原创文章,如果转载请注明原文链接!
原文标题:计算几何 – 求解两个三维向量之间的三维旋转矩阵
原文链接:https://www.stubbornhuang.com/2204/
发布于:2022年07月12日 8:46:40
修改于:2022年07月12日 8:46:40
1 求解两个三维向量之间的3D旋转矩阵
1.1 方法1
先求解两个三维向量之间的夹角作为旋转角度,然后通过求解两个三维向量之间的叉乘向量作为旋转轴,最后通过旋转角度和旋转轴获取两个向量之间的旋转矩阵。
#include <iostream>
#include <Eigen/Dense>
int main()
{
Eigen::Matrix3d rotation_matrix;
Eigen::Vector3d vector_before{ 0, -1, 0 };
Eigen::Vector3d vector_after{ 0.707, -0.707, 0 };
// 求解两个向量之间的夹角
double tem = vector_before.dot(vector_after);
double tep = sqrt(vector_before.dot(vector_before) * vector_after.dot(vector_after));
double angle = acos(tem / tep);
if (isnan(angle))//acos取值范围[-1,1],若超出范围则越界,输出-1.#IND00
{
angle = acos(tep / tem);
}
// 求解两个向量之间的旋转轴
Eigen::Vector3d axis = vector_before.cross(vector_after);
// 求解旋转矩阵
Eigen::Affine3d transform = Eigen::Affine3d::Identity();
transform.translation() << 0, 0, 0;
transform.rotate(Eigen::AngleAxisd(angle, axis.normalized()));
std::cout << "Rotation Matrix: " << std::endl << transform.matrix() << std::endl;
return 0;
}
输出:
Rotation Matrix:
0.707107 -0.707107 0 0
0.707107 0.707107 0 0
0 0 1 0
0 0 0 1
1.2 方法2
#include <iostream>
#include <Eigen/Dense>
Eigen::Matrix3d GetRotationMatrixBetweenTwo3DVector1(Eigen::Vector3d vector_before, Eigen::Vector3d vector_after)
{
Eigen::Matrix3d rotation_matrix;
rotation_matrix = Eigen::Quaterniond::FromTwoVectors(vector_before, vector_after).toRotationMatrix();
return rotation_matrix;
}
int main()
{
Eigen::Vector3d vector_before{ 0, -1, 0 };
Eigen::Vector3d vector_after{ 0.707, -0.707, 0 };
Eigen::Matrix3d rotation_matrix;
rotation_matrix = Eigen::Quaterniond::FromTwoVectors(vector_before, vector_after).toRotationMatrix();
std::cout << "Rotation Matrix: " << std::endl << rotation_matrix << std::endl;
std::cout << "Result Point" << std::endl << rotation_matrix * vector_before << std::endl;
return 0;
}
输出
Rotation Matrix:
0.707107 -0.707107 0
0.707107 0.707107 0
0 0 1
Result Point
0.707107
-0.707107
0
当前分类随机文章推荐
- 计算几何 - 二维笛卡尔坐标系中,计算二维点绕任意中心点旋转任意角度的结果 阅读1226次,点赞0次
- 计算几何 - C++计算两个二维向量的夹角 阅读4342次,点赞3次
- 计算几何 - 求解两个三维向量之间的三维旋转矩阵 阅读1403次,点赞0次
全站随机文章推荐
- OpenCV - 新建一个图片,并在图片上画由一点到另一点的直线,采用反走样形式 阅读3012次,点赞0次
- 资源分享 - 统计学习方法(李航著) 高清PDF下载 阅读4975次,点赞3次
- WebGL/ThreeJS - 相关文档、教程网站 阅读421次,点赞0次
- WordPress - robots.txt 阅读2625次,点赞0次
- 姿态估计之COCO数据集骨骼关节keypoint标注对应 阅读10010次,点赞5次
- Modern OpenGL从零开始 - 多个帧缓存Framebuffer绘制到同一个铺满屏幕四边形Quad上 阅读2744次,点赞1次
- WordPress - 增加百度统计代码 阅读3132次,点赞0次
- 资源分享 - Game Physics (Second Edition) 英文高清PDF下载 阅读2147次,点赞1次
- 资源分享 - Vulkan Programming Guide - The Official Guide to Learning Vulkan 英文高清PDF下载 阅读2819次,点赞0次
- OpenCV - 使用findContours()查找图片轮廓线,并将轮廓线坐标点输出 阅读4909次,点赞0次
评论
169