Friday, 20 September 2019

mayavi: two surfaces obstruct each other, even though they are non-intersecting

I am trying to plot two surfaces which touch at exactly two points but are otherwise well separated. Depending on the viewing angle, this renders either just fine (figure 1) or it makes some mess with the top surface s2 (plasma, red) obstructing the lower one s1 (figure 2). I suppose that is due to the order in which the surfaces are plotted, so mayavi just puts one in front even though mathematically it should be in the back. How can I solve this issue? Note that I would like to have different colormaps for both surfaces, as they represent different things. Thanks a lot!

figure 1, correct plot figure 1, correct plot figure 2, wrong plot figure 2, wrong plot

Here the code to produce the plot. Viewing angles were chosen in the interactive window, not sure how to get the numerical values.

import numpy as np
import mayavi.mlab

x,y = np.mgrid[-np.pi:np.pi:0.01, -np.pi:np.pi:0.01]

def surface1(x,y):
    return -np.sqrt((np.cos(x) + np.cos(y) - 1)**2 + np.sin(x)**2)

def surface2(x,y):
    return np.sqrt((np.cos(x) + np.cos(y) - 1)**2 + np.sin(x)**2)

s1 = mayavi.mlab.surf(x,y,surface1, colormap='viridis')
s2 = mayavi.mlab.surf(x,y,surface2, colormap='plasma')

mayavi.mlab.show()

EDIT: Finally found the issue: Need to specify the correct backend for rendering. Using ipython3 --gui=qt solves the issue. Thus the issue only appears when using the default backend (whichever that is). I wish this would be documented more clearly somewhere, would have saved me a lot of work.



from mayavi: two surfaces obstruct each other, even though they are non-intersecting

No comments:

Post a Comment