ROS的tf转为RT矩阵

cpp 复制代码
void // Get the TF as an Eigen matrix
transformAsMatrix (const tf::Transform& bt, Eigen::Matrix4f &out_mat)
{
  double mv[12];
  bt.getBasis ().getOpenGLSubMatrix (mv);

  tf::Vector3 origin = bt.getOrigin ();

  out_mat (0, 0) = mv[0]; out_mat (0, 1) = mv[4]; out_mat (0, 2) = mv[8];
  out_mat (1, 0) = mv[1]; out_mat (1, 1) = mv[5]; out_mat (1, 2) = mv[9];
  out_mat (2, 0) = mv[2]; out_mat (2, 1) = mv[6]; out_mat (2, 2) = mv[10];

  out_mat (3, 0) = out_mat (3, 1) = out_mat (3, 2) = 0; out_mat (3, 3) = 1;
  out_mat (0, 3) = origin.x ();
  out_mat (1, 3) = origin.y ();
  out_mat (2, 3) = origin.z ();
}

这段代码定义了一个函数 transformAsMatrix,它将一个 tf::Transform 对象转换为一个 4x4 的 Eigen 矩阵 (Eigen::Matrix4f)。tf::Transform 是一个表示 3D 空间中的变换的对象,通常包括旋转和平移。下面是这段代码的逐步解释:

函数签名

cpp 复制代码
transformAsMatrix (const tf::Transform& bt, Eigen::Matrix4f &out_mat)
  • const tf::Transform& btbt 是一个 tf::Transform 类型的常量引用,表示输入的变换。
  • Eigen::Matrix4f &out_matout_mat 是一个 Eigen::Matrix4f 类型的引用,表示输出的 4x4 矩阵。

函数内部

  1. 获取旋转矩阵(基础部分)

    cpp 复制代码
    double mv[12];
    bt.getBasis ().getOpenGLSubMatrix (mv);
    • bt.getBasis().getOpenGLSubMatrix(mv):从 tf::Transform 对象 bt 中提取旋转矩阵部分,并以 OpenGL 的行优先顺序存储在 mv 数组中。mv 是一个长度为 12 的数组,包含了旋转矩阵的前 3 行的内容(不包括平移部分)。
  2. 获取平移向量

    cpp 复制代码
    tf::Vector3 origin = bt.getOrigin ();
    • bt.getOrigin():从 tf::Transform 对象中获取平移向量(原点)。这个向量存储了变换的平移部分。
  3. 填充输出矩阵的旋转部分

    cpp 复制代码
    out_mat (0, 0) = mv[0]; out_mat (0, 1) = mv[4]; out_mat (0, 2) = mv[8];
    out_mat (1, 0) = mv[1]; out_mat (1, 1) = mv[5]; out_mat (1, 2) = mv[9];
    out_mat (2, 0) = mv[2]; out_mat (2, 1) = mv[6]; out_mat (2, 2) = mv[10];
    • 这几行代码将 mv 中的元素填入 out_mat 的旋转部分。注意 mv 中的元素是按照行优先顺序存储的。
  4. 填充输出矩阵的平移部分

    cpp 复制代码
    out_mat (0, 3) = origin.x ();
    out_mat (1, 3) = origin.y ();
    out_mat (2, 3) = origin.z ();
    • 这几行代码将平移向量 origin 的 x、y 和 z 分量填入 out_mat 的最后一列。
  5. 填充输出矩阵的齐次坐标部分

    cpp 复制代码
    out_mat (3, 0) = out_mat (3, 1) = out_mat (3, 2) = 0; out_mat (3, 3) = 1;
    • 最后一行 [0, 0, 0, 1] 是为了齐次坐标表示法,确保矩阵是一个有效的 4x4 变换矩阵。前三个元素设置为 0,最后一个元素设置为 1。

总结

  • 该函数将 tf::Transform 对象 bt 中的旋转和平移信息提取出来,并将其填充到 Eigen::Matrix4f 类型的 4x4 矩阵 out_mat 中。这种矩阵可以用来进行3D空间中的坐标变换。
相关推荐
kobesdu1 天前
【ROS2实战笔记-6】RobotPerf:机器人计算系统的基准测试方法论
笔记·机器人·ros
MIXLLRED2 天前
随笔——ROS Ubuntu版本变化详解
linux·ubuntu·机器人·ros
sanzk2 天前
astra pro稠密建图
ubuntu·ros·3d相机
kobesdu3 天前
【ROS2实战笔记-4】Gazebo:从通信桥接到性能瓶颈相关技术梳理
笔记·机器人·ros·gazebo
LN花开富贵4 天前
【ROS】鱼香ROS2学习笔记一
linux·笔记·python·学习·嵌入式·ros·agv
kobesdu4 天前
【ROS2实战笔记-3】RViz2图形底层与调试暗坑
笔记·机器人·ros·rviz
sanzk4 天前
ASTRA PRO相机
ubuntu·ros·3d相机
Robot_Nav4 天前
CMake、Ament 与 Catkin:ROS 构建系统的前世今生
ros·cmake
zhanglianzhao5 天前
Gazebo仿真机器人和相机时Gazebo ROS Control 插件偶发性加载失败bug分析
机器人·bug·ros·gazebo·ros_control
kobesdu6 天前
「ROS2实战-2」集成大语言模型:ollama_ros_chat 本地智能对话功能包部署和使用解析
人工智能·语言模型·自然语言处理·机器人·ros