By creating a 3D cone or disk and constantly updating the axis of symmetry with matplotlib

I mean, a cone or disk moves or rotates with an axis of symmetry. More precisely, I create this axis, which is constantly changing over time:

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

I need a face of a cone or circle, always perpendicular to this axis. At first I tried it easier by creating a 2D circle, then raise it to the position I want:

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

but this does not make the face of the circle perpendicular to the moving axis. I wonder if there is any function in matplotlib that I can use to rotate this face of the cone / circle?

If I started from a different path, creating a three-dimensional object, like an ellipsoid, the problem remains: how can I allow the object to move with the axis of symmetry, like a solid (stick to its axis), and not a flashlight hanging there (attached only to a fixed point)?

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")
+5
source share
1 answer
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)

This will result in a cone about line directed along the z-axis direction after the rotation through the Euler angles , . (in this case it will have no effect, since the cone is axially symmetric about the z axis). Also see rotation matrix . phitheta psipsi

To draw one circle normal shifted by 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)

It remains as an exercise to get the Euler angles from a given vector.

euler_rotin gist

+2
source

All Articles