import numpy as np
from scipy.spatial.transform import Rotation
def euler_xyz_to_rt_matrix(pose, degrees=False):
"""
将 [x, y, z, rx, ry, rz] 转换为 4x4 RT 矩阵
其中 [rx, ry, rz] 是 XYZ 固定轴顺序的欧拉角
Args:
pose: [x, y, z, rx, ry, rz]
degrees: 如果为True,输入角度为度数;如果为False,输入为弧度
Returns:
4x4 RT 矩阵
"""
# 提取平移部分
translation = np.array(pose[:3])
# 提取欧拉角 (XYZ 顺序)
euler_angles = pose[3:]
# 创建旋转对象 (使用 'XYZ' 固定轴顺序)
# 'xyz'(小写字母):内在旋转
# 旋转围绕 物体自身的坐标系轴进行,且每次旋转后,坐标系会跟随物体一起转动。
rotation = Rotation.from_euler('xyz', euler_angles, degrees=degrees)
# 旋转围绕 固定不动的世界坐标系轴进行,轴的方向始终不变
# rotation = Rotation.from_euler('XYZ', euler_angles, degrees=degrees)
rotation_matrix = rotation.as_matrix()
# 构建 4x4 RT 矩阵
rt_matrix = np.eye(4)
rt_matrix[:3, :3] = rotation_matrix
rt_matrix[:3, 3] = translation
return rt_matrix
# 示例使用 (弧度)
pose_rad = [1.0, 2.0, 3.0, 0.1, 0.2, 0.3] # [x, y, z, rx, ry, rz] 弧度
rt_matrix_rad = euler_xyz_to_rt_matrix(pose_rad, degrees=False)
# 示例使用 (角度)
pose_deg = [1.0, 2.0, 3.0, 30.0, 45.0, 60.0] # [x, y, z, rx, ry, rz] 角度
rt_matrix_deg = euler_xyz_to_rt_matrix(pose_deg, degrees=True)
print("RT 矩阵 (弧度 - XYZ欧拉角):")
print(rt_matrix_rad)
print("\nRT 矩阵 (角度 - XYZ欧拉角):")
print(rt_matrix_deg)
RT 矩阵 (弧度 - XYZ欧拉角):
[[ 0.93629336 -0.28962948 0.19866933 1. ]
[ 0.31299183 0.94470249 -0.0978434 2. ]
[-0.15934508 0.153792 0.97517033 3. ]
[ 0. 0. 0. 1. ]]
RT 矩阵 (角度 - XYZ欧拉角):
[[ 0.35355339 -0.61237244 0.70710678 1. ]
[ 0.9267767 0.12682648 -0.35355339 2. ]
[ 0.12682648 0.78033009 0.61237244 3. ]
[ 0. 0. 0. 1. ]]