from mpl_toolkits.mplot3d import Axes3D
def euler_rot(XYZ,phi,theta,psi):
'''Returns the points XYZ rotated by the given euler angles'''
ERot = np.array([[np.cos(theta)*np.cos(psi),
-np.cos(phi)*np.sin(psi) + np.sin(phi)*np.sin(theta)*np.cos(psi),
np.sin(phi)*np.sin(psi) + np.cos(phi)*np.sin(theta)*np.cos(psi)],
[np.cos(theta)*np.sin(psi),
np.cos(phi)*np.cos(psi) + np.sin(phi)*np.sin(theta)*np.sin(psi),
-np.sin(phi)*np.cos(psi) + np.cos(phi)*np.sin(theta)*np.sin(psi)],
[-np.sin(theta),
np.sin(phi)*np.cos(theta),
np.cos(phi)*np.cos(theta)]])
return ERot.dot(XYZ)
u = np.linspace(0,2*np.pi,50)
num_levels = 10
r0 = 1 # maximum radius of cone
h0 = 5 # height of cone
phi = .5 # aka alpha
theta = .25 # aka beta
psi = 0 # aka gamma
norm = np.array([0,0,h0]).reshape(3,1)
normp = euler_rot(norm,phi,theta,psi)
fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')
ax.plot([0,normp[0]],[0,normp[1]],zs= [0,normp[2]])
x = np.hstack([r0*(1-h)*np.cos(u) for h in linspace(0,1,num_levels)])
y = np.hstack([r0*(1-h)*np.sin(u) for h in linspace(0,1,num_levels)])
z = np.hstack([np.ones(len(u))*h*h0 for h in linspace(0,1,num_levels)])
XYZ = np.vstack([x,y,z])
xp,yp,zp = euler_rot(XYZ,phi,theta,psi)
ax.plot_wireframe(xp,yp,zp)
这将在通过欧拉角 phi
, theta
,旋转后围绕指向 z 轴方向的线绘制一个圆锥psi
。(在这种情况下psi
,由于锥体围绕 z 轴轴对称,因此无效)另请参见旋转矩阵。
要绘制一个沿法线移动的圆h0
:
x=r0*np.cos(u)
y=r0*np.sin(u)
z=h0*np.ones(len(x))
XYZ = np.vstack([x,y,z])
xp,yp,zp = euler_rot(XYZ,phi,theta,psi)
ax.plot(xp,yp,zs=zp)
从给定的向量中获取欧拉角作为练习。
euler_rot
在一个要点