根据两向量求旋转矩阵(C++)


Windows 10(64bits) + VMware 16 Pro + Ubuntu 20.04 + VS Code 2022
PCL 1.10 + Eigen3


一、库及VS code中的配置

1.1需要的库

PCL库
Eigen库

1.2 VS Code配置

直接使用文本编辑器写代码的时候,库是由CMakeLists.txt进行链接的,但是用VS Code打开是,代码头文件中PCL库和Eigen库下面会有波浪线,像这样:
在这里插入图片描述
配置过程:
ctrl+shift+P——>C/C++:Edit Configuration中增加库的文件路径,库文件通常在/usr/include/中,或者对应上自己库的位置就可以。
这里要注意的是:
#include <pcl/io/pcd_io.h>需要Eigen库的依赖,如果只添加了PCL库,这一条头文件还会有波浪线,可以自己试一下看看。

{
    "configurations": [
        {
            "name": "Linux",
            "includePath": [
                "${workspaceFolder}/**",
                "/usr/include/pcl-1.10", 
                "/usr/include/eigen3"
            ],
            "defines": [],
            "compilerPath": "/usr/bin/gcc",
            "cStandard": "gnu17",
            "cppStandard": "gnu++14",
            "intelliSenseMode": "linux-gcc-x64"
        }
    ],
    "version": 4
}

二、求两向量旋转矩阵,并进行点云变换

旋转矩阵是33的,变换矩阵是44的,变换矩阵最后一列是平移向量。求两向量的旋转矩阵由Eigen完成,省去很多敲公式的时间。

//创建两个向量
Eigen::Vector3d VectorBefore(a, b, c);
Eigen::Vector3d VectorAfter(x, y, z);

//创建旋转矩阵、变换矩阵、位移向量
Eigen::Matrix3d rotationMatrix;
Eigen::Matrix4d transformMatrix;
transformMatrix.setIdentity();
Eigen::Vector3d t(0, 0, 0);

//求两向量旋转矩阵
rotationMatrix = Eigen::Quaterniond::FromTwoVectors(VectorBefore, VectorAfter).toRotationMatrix();

//旋转矩阵和平移向量凑成变换矩阵
transformMatrix.block<3, 3>(0, 0) = rotationMatrix;
transformMatrix.topRightCorner(3, 1) = t;

//点云变换
pcl::transformPointCloud(*cloud_in, *cloud_out, transformMatrix);

Eigen库使用教程: C++矩阵库 Eigen 快速入门

  • 2
    点赞
  • 15
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
旋转矩阵的雅可比矩阵可以通过取对应的旋转向量旋转矩阵的导数来得到。假设旋转向量为 $\boldsymbol{\theta} = (\theta_1, \theta_2, \theta_3)$,则旋转矩阵为: $$ \mathbf{R}(\boldsymbol{\theta}) = \begin{pmatrix} c_2 c_3 & -c_2 s_3 & s_2 \\ c_1 s_3 + c_3 s_1 s_2 & c_1 c_3 - s_1 s_2 s_3 & -c_2 s_1 \\ s_1 s_3 - c_1 c_3 s_2 & c_3 s_1 + c_1 s_2 s_3 & c_1 c_2 \end{pmatrix} $$ 其中 $c_i = \cos(\theta_i)$,$s_i = \sin(\theta_i)$。 对于旋转向量的雅可比矩阵,可以通过以下公式计算: $$ \frac{\partial \mathbf{R}}{\partial \boldsymbol{\theta}} = \begin{pmatrix} \frac{\partial \mathbf{r}_1}{\partial \boldsymbol{\theta}} \\ \frac{\partial \mathbf{r}_2}{\partial \boldsymbol{\theta}} \\ \frac{\partial \mathbf{r}_3}{\partial \boldsymbol{\theta}} \\ \end{pmatrix} $$ 其中 $\mathbf{r}_i$ 表示旋转矩阵的第 $i$ 行,$\frac{\partial \mathbf{r}_i}{\partial \boldsymbol{\theta}}$ 可以通过以下公式计算: $$ \frac{\partial \mathbf{r}_i}{\partial \boldsymbol{\theta}} = \begin{pmatrix} -\sin \theta_i \cdot r_{i2} - \cos \theta_i \cdot r_{i3} \\ \cos \theta_i \cdot r_{i2} - \sin \theta_i \cdot r_{i3} \\ r_{i1} \end{pmatrix} $$ 具体的 C++ 代码实现如下: ```cpp #include <Eigen/Dense> using namespace Eigen; // 计算旋转矩阵的雅可比矩阵 Matrix<double, 9, 3> rotation_jacobian(const Vector3d& theta) { double c1 = cos(theta(0)), c2 = cos(theta(1)), c3 = cos(theta(2)); double s1 = sin(theta(0)), s2 = sin(theta(1)), s3 = sin(theta(2)); double r11 = c2 * c3, r12 = -c2 * s3, r13 = s2; double r21 = c1 * s3 + c3 * s1 * s2, r22 = c1 * c3 - s1 * s2 * s3, r23 = -c2 * s1; double r31 = s1 * s3 - c1 * c3 * s2, r32 = c3 * s1 + c1 * s2 * s3, r33 = c1 * c2; Matrix<double, 9, 3> J; // 计算第一行的雅可比矩阵 J(0, 0) = -s1 * r21 - c1 * r31; J(1, 0) = -s1 * r22 - c1 * r32; J(2, 0) = -s1 * r23 - c1 * r33; // 计算第二行的雅可比矩阵 J(3, 1) = -s2 * r11 - c2 * r31; J(4, 1) = -s2 * r12 - c2 * r32; J(5, 1) = -s2 * r13 - c2 * r33; // 计算第三行的雅可比矩阵 J(6, 2) = -s3 * r11 - c3 * r21; J(7, 2) = -s3 * r12 - c3 * r22; J(8, 2) = -s3 * r13 - c3 * r23; return J; } ```

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

可见一班

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值