Home » Lab 1

Assignment 1 – Basics of VTK

In this exercise you will work with some fundamental visualization algorithms in VTK. The aim is that you should get an understanding for the VTK pipeline and how data is represented. You should also be able to control how your data is mapped (e.g., color, orientation, size…). You will also learn how to construct a simple keyboard interface for interaction.

Requirements to pass: Present a working solution of, part1 and either a or b of part 2, to one of the lab assistants (Fredrik or Anders) during the lab session or at the next lab session (deadline). You don’t have to hand in the source code.

Obtaining the source code and datasets

Click here to download the source code and datasets required for the assignment. Please note if you have VTK6 you will need to download vetvis_assignment1_vtk6 instead!

Getting started

This little (non-mandatory) exercise will test that your VTK and Python installation works, demonstrate how to set up a basic visualization pipeline in VTK, and make you familiar with the VTK Python wrapper.

Download and extract vetvis_assignment1.zip into some suitable directory. Then, open a command prompt, navigate to the getting_started subdirectory, and type

   python cube.py

to run the included example script cube.py. If everything is set up correctly you should see a window that displays a simple 3D scene (a red cube on a black background). Try to interact (rotate, zoom, translate, etc) with the cube and the camera using the mouse and keyboard. You can find a description of VTK’s standard interaction modes and keyboard shortcuts onhttp://www.vtk.org/doc/nightly/html/classvtkInteractorStyle.html. Please keep the default keyboard shortcuts in mind later when you define your own keyboard interface (so that you do not override the default key bindings).

Also, browse through the source code to see how the visualization pipeline is set up. If you want, you can try to modify the script. For instance, you can try to change the color of the cube or the background. It is not mandatory to write any code here though (the real assignment will start in the next part).

Part 1 – Isosurface extraction

In this part you will start with the actual VTK and Python programming. Your task is to extract and display isosurfaces of the probability density of a hydrogen atom volume dataset. The isosurfaces should be color-mapped according to the isovalue and a colorbar should be available. You should also create a small keyboard interface for changing the isovalue. For this task you have a template Python script available where you can add your code.

  • Python script: isosurface.py
  • The dataset: hydrogen.vtk

The template script contains the most important things such as a data reader, a render window, and an interactor. It also contain a small keyboard interface for saving the render window to a .png file. The only thing visible from start is an outline of the data and you need to complete the pipeline.

Part 1 – Task

Part 2 – Micro and macro

Here you will work on two visualization tasks involving a dataset from a molecular dynamics simulation and a dataset of air currents above North America. The datasets are very different, but we will see that similar visualization techniques can be applied to both.

2a: Molecular dynamics

The data from the simulations (provided by Daniel Spångberg) are stored in three files:coordinates.txt contains 3D coordinates of a number of atoms, radii.txt contains the radii of these atoms, and connections.txt defines how the atoms are connected to each other.

One way to visualize the molecules is to represent each atom with a sphere with a radius corresponding to the radius of the atom. Some of the atoms have quite similar radii, so to make it easier to distinguish between these we can color-map the spheres depending on their radius. Finally, the connections between the atoms can be made with cylinders. The image to the right show an example of what the result can look like (OBS! this is not necessarily the correctsolution).

The files we have available is not in a standard format, so we either have to convert our data into the VTK file format or create our own functions for reading the data. Such functions are provided for you in the Python modulemolecules_io.py. If you want, you can also download the script molecules.py that contains some hints about how to read the data and set up the scene. When you have read the data you have a vtkPolyData with points/vertices/cells and it is time to start working.

Part 2a – Task
  • Display the atoms with some geometric primitives as described above. The size and color of the atoms should be modulated depending on atom radii.
  • Display atom connections with lines or tubes.
  • Add a legend or a colorbar that shows the radius-to-color mapping.
  • Be creative!

Some VTK classes that you might want to look at are:

2b: Air currents

This visualization task is about showing the direction and speed of air currents over North America. The dataset wind.vtk is a vtkStructuredPoints dataset and contains vector data (velocities) and scalar data (speeds). There are several ways of visualizing this, but some standard techniques are streamlines, stream particles, oriented glyphs, slice planes, etc. As a starting point, you have a template script prepared with a data reader and a render window: wind.py.

Part 2b – Task
  • Visualize the air currents by using streams, glyphs, slice planes, …
  • Add a legend or a colorbar that shows the color mapping.
  • Be creative! For instance, you can add a keyboard interface for moving the slice plane(s) or manipulating the streamline seedpoints.

Some VTK classes that you might want to look at are: