Categories
Development

Rotation of an object in Mayavi (python)

A couple of questions:
I put together multiple parts of the available online scripts, mainly matplotlib and mayavi websites, to create the below scripts.

How can I rotate one of the shperes relative to the other by using Euler angles or the quaternion of each spheres? (I do not mean rotation of the viewpoint, just rotation of the whole object with its colormap)

or in general,
How can I rotate one of the shperes relative to the other by an arbitrary angle?

If I set the opacity to 1.0, then the mesh points on the surface will be visible, how can I create opaque objects without loosing the surface quality? On the other hand, if I set opacity to 0.9, I have a very smooth surface, but I can see through each sphere.
I highly appreciate your help and feedback.
opacity = 1.0

from mayavi import mlab
import numpy as np
from scipy.special import sph_harm

[theta,phi] = np.mgrid[0:2*np.pi:400j,0:np.pi:400j]
x = np.sin(phi)*np.cos(theta)
y = np.sin(phi)*np.sin(theta)
z = np.cos(phi)

mlab.figure(1, bgcolor=(1, 1, 1), fgcolor=(0, 0, 0), size=(400, 300))
mlab.clf()

def plot_sphere(p):
    m=0
    n=2
    s = sph_harm(m, n, theta , phi).real
    r,a,b,c = p
    return mlab.mesh(r*x+a, r*y+b, r*z+c, scalars=s, colormap='jet', resolution=50, opacity=0.9, transparent=False)  

#c = np.loadtxt("../data/d1.dat") 
c = [[10.0, 45.89, -10.99, -38.79], [10.0, 15.17, -22.34, -41.31]]
#plot_sphere(c[0])
for k in range(2):
    plot_sphere(c[k])


mlab.show()

Update 1:
I found here Mayavi: rotate around y axis a method to rotate around one axis. How can I extend it to rotate the object (spheres) in 3D? either using Euler angles or quaternions. The successive use of this rotation style, to create something like rotation matrix, R(x)R(y)R(z), does not work or at least I am not well familiar with its details to do that.

By the way, where can I find more information about this syntax “actor.actor”? Is it vtk style?

Here is the code:

from mayavi import mlab
import numpy as np
from scipy.special import sph_harm

[theta,phi] = np.mgrid[0:2*np.pi:400j,0:np.pi:400j]
x = np.sin(phi)*np.cos(theta)
y = np.sin(phi)*np.sin(theta)
z = np.cos(phi)

mlab.figure(1, bgcolor=(1, 1, 1), fgcolor=(0, 0, 0), size=(400, 300))
mlab.clf()

def plot_sphere(p,ang_x):
    m=0
    n=2
    s = sph_harm(m, n, theta , phi).real
    r,a,b,c = p
    m = mlab.mesh(r*x+a, r*y+b, r*z+c, scalars=s, colormap='jet', resolution=50, opacity=0.9, transparent=False)  
    mx = m.actor.actor.rotate_x(ang_x)
    return 

#c = np.loadtxt("../data/d1.dat") 
c = [[10.0, 45.89, -10.99, -38.79], [10.0, 15.17, -22.34, -41.31]]
plot_sphere(c[0],0)
plot_sphere(c[1],90)
#for k in range(2):
#    plot_sphere(c[k])


mlab.show()

Leave a Reply

Your email address will not be published. Required fields are marked *