本文主要是介绍使用python进行欧拉角和旋转矩阵变换,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
一. 配置PIP镜像源
pip config set global.index-url https://pypi.tuna.tsinghua.edu.cn/simple
二.下载numpy等库
pip install scipy
三.欧拉角转旋转矩阵(右手坐标系, ZXY顺序)
# coding=UTF-8
from scipy.spatial.transform import Rotation as R# 定义欧拉角 XYZ顺序
euler_angles = [90, 3, 3]if (90 - abs(euler_angles[0])) < 0.5:euler_angles[0] = 89.5
euler_angles_zxy = [0, 0, 0]
euler_angles_zxy[0] = euler_angles[2]
euler_angles_zxy[1] = euler_angles[0]
euler_angles_zxy[2] = euler_angles[1]
euler_angles_zxy = [-x for x in euler_angles_zxy]# 创建Rotation对象
r = R.from_euler('zxy', euler_angles_zxy, degrees=True)# 获取旋转矩阵
rotation_matrix = r.as_dcm()
print('[')
for row in rotation_matrix:msg = ", ".join(map(str, row))msg = '[' + msg + '],'print(msg)
print(']')
四.旋转矩阵转欧拉角(右手坐标系, ZXY顺序)
# coding=UTF-8
import numpy as np
from scipy.spatial.transform import Rotation as R# 假设我们有一个旋转矩阵
#80 -7 45
rotation_matrix = np.array([
[0.9945219996629926, 0.10452647320585942, -0.00045671157999537665],
[-0.00045671157999537665, 0.008714576084760173, 0.9999619230641713],
[0.10452647320585942, -0.9944839227271639, 0.008714576084760173]
])
# 使用SciPy的Rotation对象转换欧拉角
rotation = R.from_dcm(rotation_matrix)
euler_angles_zxy = rotation.as_euler('zxy', degrees=True) # sco存储使用'zxy'顺序,如果需要度为单位,设置degrees=True#左右手参考坐标系正负值替换
euler_angles_zxy = [-x for x in euler_angles_zxy]
euler_angles = [0, 0, 0]
euler_angles[0] = euler_angles_zxy[1]
euler_angles[1] = euler_angles_zxy[2]
euler_angles[2] = euler_angles_zxy[0]
print(euler_angles)
这篇关于使用python进行欧拉角和旋转矩阵变换的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!