Last modified: June 11, 2024

This article is written in: 🇺🇸

Interactivity and User Interface

VTK comes equipped with a range of tools designed to help developers create interactive visualizations and user interfaces. Some of the popular techniques employed for this purpose include:

  1. Picking and Selection
  2. Cutting Planes
  3. 3D Widgets
  4. Event Handling and Callbacks

Picking and Selection

Picking refers to the process of interactively selecting a particular object or point within the visualization. Selection, on the other hand, involves highlighting or extracting a subset of data based on certain defined criteria.

Here are two of the many classes VTK provides for this purpose:

For instance, to pick a cell within a 3D scene, you would use the vtkCellPicker class as follows:

picker = vtk.vtkCellPicker()
picker.Pick(10, 10, 0, renderer)
pickedCell = picker.GetCellId()

picker

Cutting Planes

Cutting planes are used to interactively slice through a 3D object, revealing its internal structure. This technique is particularly useful when you need to inspect the inner structure of complex objects.

The two important classes involved in this process are:

To illustrate, if we wanted to create a cutting plane through a 3D object, we might use:

plane = vtk.vtkPlane()
plane.SetNormal(1, 0, 0)

cutter = vtk.vtkCutter()
cutter.SetCutFunction(plane)
cutter.SetInputData(object)

plane_cutter

3D Widgets

3D widgets are interactive tools used for manipulating data or objects within a 3D scene. They can be used for tasks such as positioning lights or cameras, or adjusting clipping planes.

Some common 3D widget classes include:

For instance, a vtkSliderWidget could be used to control the opacity of an actor like this:

sliderWidget = vtk.vtkSliderWidget()
sliderWidget.SetInteractor(interactor)
sliderWidget.SetRepresentation(sliderRep)
sliderWidget.AddObserver("InteractionEvent", lambda obj, event: actor.GetProperty().SetOpacity(obj.GetRepresentation().GetValue()))

slider

Event Handling and Callbacks

Event handling involves responding to user input or other events within a VTK application, whereas callbacks are user-defined functions that get executed in response to specific events.

Key classes for this purpose include:

Mouse Events

Mouse events can be captured and used to control aspects of the VTK visualization. The vtkRenderWindowInteractor class provides the functionality to handle mouse events.

Here is an example of handling a mouse click event:

def onMouseClick(obj, event):
    x, y = obj.GetEventPosition()
    print(f"Mouse clicked at coordinates {x}, {y}")

interactorStyle = vtk.vtkInteractorStyleTrackballCamera()
interactorStyle.AddObserver("LeftButtonPressEvent", onMouseClick)

interactor = vtk.vtkRenderWindowInteractor()
interactor.SetInteractorStyle(interactorStyle)

Keyboard Input Events

Similarly, keyboard input events can also be captured using the vtkRenderWindowInteractor class. These events can be used to interact with the visualization or control the program flow.

Here is an example of handling a keyboard press event:

def onKeyPress(obj, event):
    key = obj.GetKeySym()
    print(f"Key {key} was pressed")

interactorStyle = vtk.vtkInteractorStyleTrackballCamera()
interactorStyle.AddObserver("KeyPressEvent", onKeyPress)

interactor = vtk.vtkRenderWindowInteractor()
interactor.SetInteractorStyle(interactorStyle)

Table of Contents

  1. Interactivity and User Interface
  2. Picking and Selection
  3. Cutting Planes
  4. 3D Widgets
  5. Event Handling and Callbacks
    1. Mouse Events
    2. Keyboard Input Events