5

我的意思是圆锥或圆盘以其对称轴移动或旋转。确切地说,我正在创建这个轴,它会随着时间不断变化:

line = ax.plot([x,0],[y,0],[z,z- n_o],color='#000066', marker= 'o')

我需要始终垂直于该轴的圆锥面或圆面。我首先尝试了一个更简单的方法,方法是创建一个 2D 圆圈,然后将其提升到我想要的位置:

circle = Circle((0, 0), .3, color='r')
ax.add_patch(circle)
art3d.pathpatch_2d_to_3d(circle, z=1)

但这不会使圆的面垂直于移动轴。我想知道 matplotlib 中是否有任何函数可以用来旋转圆锥/圆的那个面?

如果,我从另一种方式开始创建一个 3D 对象,比如一个椭圆体,问题仍然存在:我如何让对象像刚体一样以其对称轴移动(坚持它的轴)而不是挂在那里的灯笼(仅连接到一个固定点)?

u, v = np.mgrid[0:2*np.pi:20j, 0:np.pi:10j]
x=np.cos(u)*np.sin(v)
y=np.sin(u)*np.sin(v)
z=.3*np.cos(v)
ax.plot_wireframe(x, y, z, color="r")
4

1 回答 1

2
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在一个要点

于 2012-10-29T01:27:47.837 回答