Mayavi: BUG: Opacity of streamlines interacting with visual ordering of visualization modules

Created on 6 Mar 2017  ·  10Comments  ·  Source: enthought/mayavi

Changing the opacity of streamlines to anything other than its default value (1.0) brings the streamlines to the front of the scene even when there are other objects that should obscure them.

For example, I refer you to the magnetic fieldline example given here: http://docs.enthought.com/mayavi/mayavi/auto/example_magnetic_field_lines.html.

Running the code as it is gives the following scene:
test_bug_1

However, if we change the opacity of the fieldlines so that it is different it its default value of 1.0 (either by adding field_lines.actor.property.opacity = 0.99 or by manually adjusting the opacity in the Mayavi pipeline) we get something like the following scene:
test_bug_2
The fieldlines are now at the front of the scene, however the isocontours should still obscure then like in the previous image.

Most helpful comment

With recent VTK versions this works well for me:

from mayavi import mlab
f = mlab.figure()
mlab.points3d(0,0,1,color=(.8,.8,.8),resolution=100,opacity=.99)
mlab.points3d(0,0,0,color=(.8,.8,.8),resolution=100,opacity=.99)
f.scene.renderer.use_depth_peeling = True

Also, there is an f.scene.renderer.maximum_number_of_peels that you could potentially use. It is 4 by default but you could set it to zero for no limit although this will slow down rendering. You could tinker with this.

May I close this issue? I think this does seem to fix many problems I have seen before.

All 10 comments

Is this an issue which will be fixable in the near future? If not, I would appreciate knowing so that I can find another way around my problem. Thank you

This seems to apply to other objects as well. I'm using contour3d with points3d and setting the opacity of the points to anything other than 1.0 causes the surface not to obscure them anymore.

I see what I think is the same issue with two points3d objects. Here are some examples showing how the appearance of 2 spheres rendered depends on rendering order if the opacity is set to .99, but not if opacity is set to 1.

mlab.figure()
mlab.points3d(0,0,1,color=(.8,.8,.8),resolution=100,opacity=.99)
mlab.points3d(0,0,0,color=(.8,.8,.8),resolution=100,opacity=.99)

2

mlab.figure()
mlab.points3d(0,0,0,color=(.8,.8,.8),resolution=100,opacity=.99)
mlab.points3d(0,0,1,color=(.8,.8,.8),resolution=100,opacity=.99)

4

mlab.figure()
mlab.points3d(0,0,1,color=(.8,.8,.8),resolution=100)
mlab.points3d(0,0,0,color=(.8,.8,.8),resolution=100)

1

mlab.figure()
mlab.points3d(0,0,0,color=(.8,.8,.8),resolution=100)
mlab.points3d(0,0,1,color=(.8,.8,.8),resolution=100)

3

The issue does not seem to depend on choice of lights or light_manager ('vtk' or 'raymond').

I suppose one workaround may be first sorting the objects to be rendered by their depth along an axis into or out of the screen.

You probably need to set the front-face culling option on. Try this:

mlab.figure()
g1 = mlab.points3d(0,0,1,color=(.8,.8,.8),resolution=100,opacity=.99)
g2 = mlab.points3d(0,0,0,color=(.8,.8,.8),resolution=100,opacity=.99)
g1.actor.property.frontface_culling = True
g2.actor.property.frontface_culling = True

Thanks Prabhu---I think the frontface_culling improved the visuals, but the rendering order still was a factor only in the semi-transparent case.

I reproduced that observation in vtk alone (it's a lower level issue).

After more reading, I think there is a solution or two built into vtk, but another solution may be to sort the objects before rendering.

Here are a couple links about vtk that seemed relevant:
https://www.vtk.org/Wiki/VTK/Examples/Cxx/Visualization/CorrectlyRenderingTranslucentGeometry
https://www.vtk.org/Wiki/VTK/Depth_Peeling

And a couple links I used for reproducing the sphere case:
https://lorensen.github.io/VTKExamples/site/Python/GeometricObjects/Sphere/
https://www.vtk.org/Wiki/VTK/Examples/Cxx/Visualization/Opacity

Thanks for adding to the discussion and for the helpful links. I am not very knowledgeable about VTK or depth peeling, but a simple solution that I found that worked for my original example and the project I am working on was to include this:

scene = mlab.gcf().scene
scene.renderer.set(use_depth_peeling=True)

which I adapted from this question about a similar issue..

Thanks for the help Matt and Prabhu.

I tried depth_peeling, which for my actual use case is more helpful than it is for this 2 sphere example. But compared to reordering the spheres, the edges seem less...anti-aliased I think might be the term?

f=mlab.figure()
f.scene.renderer.use_depth_peeling=1
g0=mlab.points3d(0,0,1,color=(.8,.8,.8),resolution=100,opacity=.99)
g1=mlab.points3d(0,0,0,color=(.8,.8,.8),resolution=100,opacity=.99)  
g0.actor.property.frontface_culling = True
g1.actor.property.frontface_culling = True

image

For my 3d plotting, I have a list of points and a list of colors. So instead of depth peeling I used this function to reorder those lists together before plotting:

def orderTransparentRender(cl,c):
    '''Opacity < 1 rendering results in appearance of spheres incorrectly in front of or behind other spheres
    One solution is setting f.scene.renderer.use_depth_peeling=1, however this seemed to result in less anti-aliased edges?
    This function sorts the cluster list cl and color list c together.
    '''
    cz=zip(cl,c)
    cz=sorted(cz,key=lambda x:-1*x[0][2])
    cz=zip(*cz)
    return cz[0],cz[1]

Otherwise the sorted call could be all that's required. I learned that from the python docs: https://wiki.python.org/moin/HowTo/Sorting. I learned about python's zip here: https://stackoverflow.com/questions/1663807/how-can-i-iterate-through-two-lists-in-parallel

With recent VTK versions this works well for me:

from mayavi import mlab
f = mlab.figure()
mlab.points3d(0,0,1,color=(.8,.8,.8),resolution=100,opacity=.99)
mlab.points3d(0,0,0,color=(.8,.8,.8),resolution=100,opacity=.99)
f.scene.renderer.use_depth_peeling = True

Also, there is an f.scene.renderer.maximum_number_of_peels that you could potentially use. It is 4 by default but you could set it to zero for no limit although this will slow down rendering. You could tinker with this.

May I close this issue? I think this does seem to fix many problems I have seen before.

Thank you. I will be able to work with this. You can close this issue.

Matthew Allcock | PhD researcher
University of Sheffield | School of Mathematics and Statistics
www.matthewallcock.co.uk

On Sun, 26 Aug 2018, 21:55 Prabhu Ramachandran, notifications@github.com
wrote:

With recent VTK versions this works well for me:

from mayavi import mlab
f = mlab.figure()
mlab.points3d(0,0,1,color=(.8,.8,.8),resolution=100,opacity=.99)
mlab.points3d(0,0,0,color=(.8,.8,.8),resolution=100,opacity=.99)
f.scene.renderer.use_depth_peeling = True

Also, there is an f.scene.renderer.maximum_number_of_peels that you could
potentially use. It is 4 by default but you could set it to zero for no
limit although this will slow down rendering. You could tinker with this.

May I close this issue? I think this does seem to fix many problems I have
seen before.


You are receiving this because you authored the thread.
Reply to this email directly, view it on GitHub
https://github.com/enthought/mayavi/issues/491#issuecomment-416071420,
or mute the thread
https://github.com/notifications/unsubscribe-auth/AP4RUC8AYAnDAV6-wE88hHw5cPP-x01gks5uUwtGgaJpZM4MT7Rp
.

Great, thanks.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

jan-janssen picture jan-janssen  ·  21Comments

Make42 picture Make42  ·  7Comments

aestrivex picture aestrivex  ·  9Comments

stefanoborini picture stefanoborini  ·  11Comments

indranilsinharoy picture indranilsinharoy  ·  9Comments