Scientific Visualisations with Python

Installing and Setting Up VTK

Install VTK using pip command:

pip install vtk

Verify installation:

import vtk
print(vtk.VTK_VERSION)

Hello World program

Here’s the minimalist "Hello, World!" example for VTK in Python. This script creates a simple 3D window with a sphere rendered in it.

import vtk
# Create a sphere source
sphere = vtk.vtkSphereSource()
#
# Create a mapper and actor
mapper = vtk.vtkPolyDataMapper()
mapper.SetInputConnection(sphere.GetOutputPort())
actor = vtk.vtkActor()
actor.SetMapper(mapper)
#
# Create a renderer, render window, and interactor
renderer = vtk.vtkRenderer()
window = vtk.vtkRenderWindow()
window.AddRenderer(renderer)
interactor = vtk.vtkRenderWindowInteractor()
interactor.SetRenderWindow(window)
#
# Add the actor to the scene
renderer.AddActor(actor)
renderer.SetBackground(0, 0, 0)  # Black background
#
# Render and start interaction
window.Render()
interactor.Start()

The Graphics Model

In the Visualization Toolkit (VTK), there are seven primary objects commonly used to render a scene. While many additional objects work behind the scenes, these seven form the core of most visualization tasks. Below is a summary of these objects, along with an illustrative diagram.

vtkRenderWindow manages a window on the display device. One or more renderers can draw into an instance of vtkRenderWindow.

vtkRenderer coordinates the rendering process, including the arrangement of lights, cameras, and actors.

vtkLight represents a light source to illuminate the scene.

vtkCamera Defines the viewpoint, focal point, and other viewing parameters of the scene.

vtkActor represents an object in the scene, specifying its properties and position in world coordinates.

vtkProperty specifies the visual appearance of an actor, including color, transparency, and lighting properties (e.g., specular and diffuse).Also defines representational properties such as wireframe or solid surfaces.

vtkMapper provides the geometric representation for an actor. Multiple actors can share the same mapper.

VTK's dataflow architecture

VTK’s dataflow architecture is a modular and flexible framework designed to process and visualize data. It organizes operations into a directed graph of interconnected objects, enabling efficient handling of data transformations and rendering. This architecture is foundational to VTK and facilitates a systematic approach to managing complex visualization pipelines.

Source

A source generates or loads data into VTK. This could involve creating synthetic geometry (e.g., a sphere, cube) or loading datasets from external files. Sources are the starting points in the data pipeline.

Example Classes:

vtkSphereSource - Creates a sphere geometry.
vtkCubeSource - Creates a cube geometry.
vtkXMLPolyDataReader - Reads a .vtp file.
vtkSTLReader - Reads a .stl file.
vtkPNGReader - Loads png image data from disk.

Filter

A filter processes or transforms the data coming from a source. Filters can perform operations such as smoothing, clipping, contouring, or calculating normals.

Example Classes:

vtkPolyDataNormals - Computes normals for geometry.
vtkContourFilter - Generates isosurfaces from scalar fields.
tkDecimatePro - Reduces the number of polygons in a mesh (decimation).

Exaggerated topographic relief on the globe created by warping the surface along computed normals using elevation data.

Mapper

A mapper converts the processed data into a format suitable for rendering, essentially mapping raw geometry or volume data to visual primitives.

Example Classes:
vtkPolyDataMapper - Maps polygonal data (e.g., meshes) for rendering.
vtkDataSetMapper - Maps general dataset structures.
vtkVolumeMapper - Maps volumetric data (e.g., medical imaging).

The mapper you will use most in the labs is vtkPolyDataMapper

The example below present application of diffrent mapers to render a sphere.

Actor

An actor represents an object in the 3D scene. It serves as a container for the mapper and manages its position, orientation, and appearance.

Example Classes:

vtkActor - Represents a 3D object in the scene.
vtkVolume - Represents volumetric data in the scene.

Renderer

A renderer manages the rendering process, including arranging actors, setting up the camera, and applying lighting. It controls how objects appear on the screen.

Example Classes:

vtkRenderer - Handles rendering for a scene.
vtkRenderWindow - Provides a window to display the rendered scene.
vtkRenderWindowInteractor - Enables user interaction with the rendered scene.