【发布时间】:2020-06-05 02:33:12
【问题描述】:
在 Python 中,使用 PyTorch,我尝试旋转 3 个立方体 x、y 和 z,首先围绕 x 轴,然后围绕 z 轴。然而,围绕 z 轴的旋转似乎以一种我没有预料到的方式表现。
我正在创建 3 个立方体以在下方旋转,然后将它们围绕 x 轴旋转 90 度,然后围绕 z 轴旋转 90 度。
import torch
from numpy import pi
import matplotlib.pyplot as plt
def rotation(x,y,z,theta,phi):
### A function for rotating about the x and then z axes ###
xx = (x*torch.cos(theta)) - (((y*torch.cos(phi)) - (z*torch.sin(phi)))*torch.sin(theta))
yy = (x*torch.sin(theta)) + (((y*torch.cos(phi)) - (z*torch.sin(phi)))*torch.cos(theta))
zz = (y*torch.sin(phi)) + (z*torch.cos(phi))
return xx,yy,zz
### Creating the 3 cubes: x, y, z ###
l = torch.arange(-2,3,1)
x,y,z=torch.meshgrid(l,l,l)
### Scaling the cubes so they can be differentiated from one another ###
x = x.clone().T
y = y.clone().T*2
z = z.clone().T*3
### Defining the amount of rotation about the x and z axes
phi = torch.tensor([pi/2]).to(torch.float) # about the x axis
theta = torch.tensor([pi/2]).to(torch.float) # about the z axis
### Performing the rotation
x_r,y_r,z_r = rotation(x, y, z, theta, phi)
通过可视化每个立方体的第一个切片,我可以看到旋转没有成功,因为乍一看,立方体实际上是围绕 x 轴旋转,然后是 y 轴。
Python 是否有一种特定的方式来处理我所缺少的这种旋转,例如轴随旋转而变化,这意味着初始的 rotation matrix operation 不再适用?
额外信息
如果将theta 替换为0 而不是pi/2,则通过查看每个旋转立方体的第一个切片可以看出第一次旋转的行为符合预期:
可视化代码:
plt.figure()
plt.subplot(231)
x_before = plt.imshow(x[0,:,:])
plt.xlabel('x-before'); plt.colorbar(x_before,fraction=0.046, pad=0.04)
plt.subplot(232)
y_before = plt.imshow(y[0,:,:])
plt.xlabel('y-before'); plt.colorbar(y_before,fraction=0.046, pad=0.04)
plt.subplot(233)
z_before = plt.imshow(z[0,:,:])
plt.xlabel('z-before'); plt.colorbar(z_before,fraction=0.046, pad=0.04)
plt.subplot(234)
x_after = plt.imshow(x_r[0,:,:])
plt.xlabel('x-after'); plt.colorbar(x_after,fraction=0.046, pad=0.04)
plt.subplot(235)
y_after = plt.imshow(y_r[0,:,:])
plt.xlabel('y-after'); plt.colorbar(y_after,fraction=0.046, pad=0.04)
plt.subplot(236)
z_after = plt.imshow(z_r[0,:,:])
plt.xlabel('z-after'); plt.colorbar(z_after,fraction=0.046, pad=0.04)
plt.tight_layout()
【问题讨论】:
标签: python matrix multidimensional-array rotation pytorch