被欧拉角折磨了几年了… 这次还是记一下吧
一般表示旋转有以下几种方式
# 用 torch 实现的. 在ogl 中 z = cam_at
def ogl_cam_R(z):
y_t = z.new(3).zero_()
#y_t.fill_(0)
y_t[1]=1
x = y_t.cross(z)
y = z.cross(x)
xyz = [ (e/e.norm()).view(-1, 1) for e in [x, y, z] ]
R = torch.cat(xyz, 1)
return R
前面说过 R 的形式 是由 你的欧拉角确定的, 要从 R 恢复欧拉角, 首先要知道你要确定你的欧拉角是怎样的定义.
接下来先做一个非常有必要的规定
(其实我自己之所以感觉绕就是因为各种博客和paper 没有明确给出其内在的顺序, 或者是自己没看到)
可以有不同的定义方式如果你没有搞清楚内在的定义顺序你将很糊涂 比如
示例1: http://pages.mtu.edu/~dcclark/dice/Background/eulerangles2.pdf
示例2: https://www1.udel.edu/biology/rosewc/kaap686/notes/matrices_rotations_2.pdf
示例3: https://en.wikipedia.org/wiki/Euler_angles
示例3定义的顺序我的定义相同
至于怎么从R转欧拉角, 当你定义好顺序之后, 看这些矩阵的表达式你将很容易恢复出那些角度来. 比如示例3中我写为 R z x y R_{zxy} Rzxy, 其要恢复x, y 如下
def get_yaw_pitch_from_Rzxy(self, R):
view = R[:, 2]
yaw = math.atan2(view[0], view[2])
pitch = math.asin(-view[1])
return yaw, pitch
先看完上一个小节, 然后本标题的 … 为 R z x y R_{zxy} Rzxy
为什么?
这个主要是为了记录一下自己总结的plt的使用, 可以忽略
import matplotlib.pyplot as plt
# 2018-3-4
# o p 可以快捷操作图片
# 终于找到退出程序的方法了
def on_release(event):
if event.button == 3: plt.close()
def on_press(event):
if event.dblclick == True: exit(0)
def create_fig():
fig = plt.figure()
fig.canvas.mpl_connect("button_press_event", on_press)
fig.canvas.mpl_connect("button_release_event", on_release)
return fig
# 给定Rw2c
cam_direct = Rw2c[:, 2]
R = ogl_cam_R(cam_direct)
fig = create_fig()
ax = plt.subplot(111, projection='3d')
bg = torch.zeros(1, 3)
# 前面的长一些, 方便能看到
for i in range(3):
pts = torch.cat((bg, Rw2c[:, i].view(1, -1)*1.1), 0).numpy()
ax.plot(pts[:, 0], pts[:, 1], pts[:, 2], c='g')
for i in range(3):
pts = torch.cat((bg, R[:, i].view(1, -1)), 0).numpy()
ax.plot(pts[:, 0], pts[:, 1], pts[:, 2], c='r')
ax.set_zlabel('Z') # 坐标轴
ax.set_ylabel('Y')
ax.set_xlabel('X')
plt.show()
# 可以防止阻塞
#plt.draw()
#plt.pause(.001)
有帮助请点赞…