In [1]:
from tvtk.api import tvtk
def vtk_show(renderer, w=100, h=100):
"""
Takes vtkRenderer instance and returns an IPython Image with the rendering.
"""
renderWindow = tvtk.RenderWindow(off_screen_rendering=1, size=(w, h))
renderWindow.add_renderer(renderer)
renderWindow.render()
windowToImageFilter = tvtk.WindowToImageFilter(input=renderWindow)
windowToImageFilter.update()
writer = tvtk.PNGWriter(write_to_memory=1, input_connection=windowToImageFilter.output_port)
writer.write()
data = writer.result.to_array().tostring()
from IPython.display import Image
return Image(data)
def get_plane(r, n1, n2, n3):
plane = tvtk.Plane(origin=r.output.center, normal=(n1, n2, n3))
c = tvtk.Cutter(input_connection=r.output_port, cut_function=plane)
m = tvtk.PolyDataMapper(input_connection=c.output_port, scalar_range=r.output.scalar_range)
a = tvtk.Actor(mapper=m)
return a
def get_outline(r):
s = tvtk.OutlineSource(bounds=r.output.bounds)
m = tvtk.DataSetMapper(input_connection=s.output_port)
a = tvtk.Actor(mapper=m)
a.property.color = (0, 0, 0)
return a
def get_renderer(l):
renderer = tvtk.Renderer(background=(1, 1, 1))
for a in l:
renderer.add_actor(a)
return renderer
In [2]:
r = tvtk.RectilinearGridReader()
r.file_name = "data/Venq.vtk"
r.update()
renderer = get_renderer([
get_plane(r, 1, 0, 0),
get_plane(r, 0, 1, 0),
get_plane(r, 0, 0, 1),
get_outline(r),
#get_contour(r),
])
camera = renderer.active_camera
renderer.reset_camera()
camera.elevation(30)
camera.azimuth(30)
vtk_show(renderer, 500, 500)
Out[2]:
The exception is most probably a bug in tvtk and I reported it here: https://github.com/enthought/mayavi/issues/77
In [ ]: