Search code examples
python-3.xmayavimayavi.mlab

ScalarCutPlane mayavi is not showing the cross-section(outline) of Mayavi iso-surface plot


I want to get the image of the cross-section similar to this stackoverflow question. But when I am trying to do the same I am not able to get the outline of the plot but I am getting mayavi iso-surface plot and scalar_cut_plane or plot opacity=0.0.

But what I want a cross-section image like stackoverflow ex and stackoverflow ex image2

Reproducible Code:

import numpy as np
from numpy import cos
from mayavi.mlab import contour3d
from mayavi import mlab

fig = mlab.figure(size=(800, 600), fgcolor=(0, 0, 0),bgcolor=(0.5,0.5,0.5))
x, y, z = np.ogrid[-3:3:60j, -3:3:60j, -3:3:60j]
t = 0

H1   =   0.45+((x*cos(t))*(x*cos(t)) + (y*cos(t))*(y*cos(t))-(z*cos(t))*(z*cos(t)))
obj1 = contour3d(H1, contours=[0], transparent=False, opacity=1.0)

H1_handle=mlab.gcf()
mlab.outline(obj1,figure=fig)

plane=mlab.pipeline.scalar_cut_plane(obj1.module_manager.source, plane_orientation='z_axes', figure=fig, )
plane.implicit_plane.widget.enabled = True
mlab.show()

The above referenced question is also not answered. After getting the crossestion image, I want to process it further so any help in this regard also will be highly appreciated.

Thanks in advance!!


Solution

  • Finally found out the solution using this answer and this question.

    The trick was to convert mayavi mlab.contour3d data to vtkPolyData. And for the rest I used same approach as the above question.

    Code:

    from mayavi import mlab
    import numpy as np
    from numpy import cos
    from tvtk.api import tvtk
    from mayavi.mlab import contour3d
    
    fig = mlab.figure(size=(800, 600), bgcolor=(1, 1, 1), fgcolor=(0, 0, 0))
    ap = tvtk.AppendPolyData()
    
    x, y, z = np.ogrid[-3:3:60j, -3:3:60j, -3:3:60j]
    t = 0
    
    H1   =   0.45+((x*cos(t))*(x*cos(t)) + (y*cos(t))*(y*cos(t))-(z*cos(t))*(z*cos(t)))
    src = contour3d(H1, contours=[0], transparent=False,opacity=0.0)
    
    
    fig.remove_child(fig.children[0])
    data_out = src.module_manager.source.get_output_dataset()
    actor = src.actor.actors[0]
    polydata = tvtk.to_vtk(actor.mapper.input)
    ap.add_input_data(polydata)
    ap.update()
    
    surf = mlab.pipeline.surface(ap.output, figure=fig)
    surf.visible = False
    vcp = mlab.pipeline.scalar_cut_plane(ap.output, plane_orientation='z_axes', figure=fig)
    outline = mlab.outline(surf, figure=fig)
    
    mlab.show()