旋转的表示转换与实现
旋转的表示
- 轴角 Angle/Axis
- 四元数 Quaternion
- 旋转矩阵
- 欧拉角
轴角 Angle/Axis
四元数 Quaternion
在实际使用中,不同库有不同的顺序
在Eigen 中
// Eigen::quaternion 有两种初始化方式,
// 方式1
Eigen::Vector4d q_(x,y,z,w);
Eigen::quaterniond q(q_) ;
// 方式2
Eigen::quaterniond q(w,x,y,z);
在Scipy库中 顺序是 (x,y,z,w),以单位四元数为例
r = R.from_quat([[0, 0, 0, 1]])
在isaacsim 中 顺序是 (w,x,y,z)
旋转矩阵
欧拉角
欧拉角有12组旋转顺序,内旋 外旋各六组,我们常用外旋的Z-Y-X 顺序
形成的旋转矩阵表示为
https://en.wikipedia.org/wiki/Euler_angles
转换与实现
Axis/Angle -> quaternion
对于以下Axis/Angle
到四元数的转换为
rotate_matrix -> quaternion
# 将旋转矩阵转化为四元数
qw = np.sqrt(1.0+R[0, 0]+R[1, 1]+R[2, 2])/2.0
if(return_order == "wxyz"):
q = np.array([qw,
(R[2, 1]-R[1, 2])/(4*qw),
(R[0, 2]-R[2, 0])/(4*qw),
(R[1, 0]-R[0, 1])/(4*qw)])
else: # xyzw
q = np.array([(R[2, 1]-R[1, 2])/(4*qw),
(R[0, 2]-R[2, 0])/(4*qw),
(R[1, 0]-R[0, 1])/(4*qw),
qw])
return q
rotate_matrix -> euler
euler -> rotate_matrix
对于欧拉角(顺序 rpy/xyz)
其旋转矩阵为
R_x = lambda roll : np.array([ [1 ,0 , 0 ],
[0 ,np.cos(roll) ,-np.sin(roll)],
[0 ,np.sin(roll) , np.cos(roll)]])
R_y = lambda pitch : np.array([[np.cos(pitch) , 0, np.sin(pitch)],
[0,1,0],
[-np.sin(pitch),0,np.cos(pitch)]])
R_z = lambda yaw : np.array([[np.cos(yaw) ,-np.sin(yaw), 0],
[np.sin(yaw),np.cos(yaw),0],
[0,0,1]])
if(order == "xyz" or order == "rpy"):
R = np.dot(np.dot(R_x,R_y),R_z)
R = np.dot(np.dot(R_x,R_y),R_z)
表明了顺序
euler -> rotate_matrix
quaternion -> rotation_matrix
对于 四元数
旋转矩阵为
r00 = 1 - 2 * (y * y + z * z)
r01 = 2 * (x * y - z * w)
r02 = 2 * (x * z + y * w)
r10 = 2 * (x * y + z * w)
r11 = 1 - 2 * (x * x + z * z)
r12 = 2 * (y * z - x * w)
r20 = 2 * (x * z - y * w)
r21 = 2 * (y * z + x * w)
r22 = 1 - 2 * (x * x + y * y)
# 3x3 rotation matrix
rot_matrix = np.array([[r00, r01, r02],
[r10, r11, r12],
[r20, r21, r22]])
quaternion -> euler
对于 四元数
欧拉角为
运算
四元数乘法
先执行
def quaternion_multiply(quaternion0, quaternion1,quat_order="xyzw"):
if(quat_order == "wxyz"):
w0, x0, y0, z0 = quaternion0
w1, x1, y1, z1 = quaternion1
else: # xyzw
x0, y0, z0,w0 = quaternion0
x1, y1, z1,w1 = quaternion1
return np.array([-x1 * x0 - y1 * y0 - z1 * z0 + w1 * w0,
x1 * w0 + y1 * z0 - z1 * y0 + w1 * x0,
-x1 * z0 + y1 * w0 + z1 * x0 + w1 * y0,
x1 * y0 - y1 * x0 + z1 * w0 + w1 * z0], dtype=np.float64)# wxyz
四元数等价与旋转矩阵,像矩阵乘法一样,四元数乘法满足结合律
四元数求逆
旋转矩阵求逆
旋转矩阵是一个 正交矩阵(orthogonal matrix),因此
参考
- https://zhuanlan.zhihu.com/p/45404840
- https://en.wikipedia.org/wiki/Rotation_matrix
- https://paroj.github.io/gltut/Positioning/Tut08 Quaternions.html#:~:text=If the two quaternions being,b*a ).
- https://danceswithcode.net/engineeringnotes/quaternions/quaternions.html#:~:text=Inverting or conjugating a rotation,it to its original location.
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· SQL Server 2025 AI相关能力初探
· AI编程工具终极对决:字节Trae VS Cursor,谁才是开发者新宠?
· 开源Multi-agent AI智能体框架aevatar.ai,欢迎大家贡献代码
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南