I mean the cone or disk is moving or rotating with its axis of symmetry. To be exact, I am creating this axis, which is constantly changing with time:
line = ax.plot([x,0],[y,0],[z,z- n_o],color='#000066', marker= 'o')
I need the face of the cone or circle always perpendicular to that axis. I tried simpler one first by creating a 2D circle then lift it up 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 that won't make the face of the circle perpendicular to the moving axis. I wonder is there any function in matplotlib I can use to rotate that face of the cone/circle?
If, I started from another way by creating a 3D object, like an ellipsoid, the problem remains: how do I let the object moving with its axis of symmetry like a rigid body(stick with its axis) rather than a lantern hanging there(attached to a fixed point only)?
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")
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 draw a cone around the line pointing along the direction of the z-axis after rotation through the Euler angles phi
,theta
,psi
. (in this case psi
will have no effect as the cone is axi-symmetric around the z-axis) Also see rotation matrix.
To draw a single circle shifted along the normal 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 is left as an exercise to get the euler angles from a given vector.
euler_rot
in a gist
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With