Getting Started#

Introduction#

Welcome to VTK! We recommend that you start by reading The VTK Book, a comprehensive guide to VTK that covers all aspects of its functionality. Additionally, you may find it helpful to explore the VTK Examples, a collection of useful reference materials that demonstrate how to use VTK’s different modules and features.

Before diving into VTK’s functionality, ensure that your system meets its system requirements. Depending on your programming experience and needs, you can choose different programming languages to work with VTK. We have documentation on how to use VTK with Python, Jupyter, C++ and CMake, Javascript, and WebAssembly.

Lastly, to help address your specific needs, you may also consider exploring existing free and open-source frameworks or applications that already leverage VTK. These frameworks and applications can be extended and customized to work for specific use cases and may provide ready-to-use solutions for your project.

System requirements#

Runtime

  • At least Python 3.x to use scripting capabilities

  • Minimum macOS version 10.10.

  • Minimum OpenGL version is 3.2 but a higher versions may be required for more advanced features.

Build-time

Check the build prerequisites.

Using Python#

VTK is available on PyPI for Windows, macOS and Linux.

pip install vtk

or in a virtual environment if you want to install the package only locally instead of system-wide

python -m venv ./env
source ./env/bin/activate
pip install vtk
python -m venv ./env
source ./env/bin/activate
pip install vtk

Using PowerShell

python -m venv env
.\env\Activate.ps1
pip install vtk

or using cmd.exe

python -m venv env
.\env\activate.bat
pip install vtk

To verify the installation try to import vtk from an interactive python environment:

>>> import vtk
>>> print(vtk.__version__)
9.2.6

That’s it ! You may now try some of the tutorials, how to guides or examples.

If you are looking for a higher-level interface to VTK in Python, you may want to explore using PyVista as it exposes VTK in a “Pythonic” manner.

Using Jupyter#

When it comes to rendering with VTK in Jupyter, there are several options.

To harness the full power of VTK in Jupyter, you may want to leverage PyVista and Trame. PyVista exposes a high-level interface to VTK for plotting and when combined with Trame, empowers users to bring the full power of VTK to a Jupyter notebook. We have a post on the VTK discourse about this. See PyVista’s documentation for more information on using PyVista’s wrappings of VTK in Jupyter.

itkwidgets is one example of a domain-specific Jupyter viewer built on VTK. To try out itkwidgets, check this example.

Using C++ and CMake#

CMake is an open-source platform-independent build system that manages the entire software build process, from source code to executable binary. If you’re new to CMake, you can find more information on the CMake website.

Installing a binary release

Pre-built VTK releases maintained by the community exist for a number of distributions, as shown in the following table:

Operating System/ Package manager

Package Name

Version

Fedora Rawhide

vtk-devel

Fedora rawhide package

Fedora 38

vtk-devel

Fedora 38 package

Fedora 37

vtk-devel

Fedora 37 package

Ubuntu 23.04 (lunar)

libvtk9-dev

Ubuntu lunar package

Ubuntu 22.10 (kinetic)

libvtk9-dev

Ubuntu kinetic package

Ubuntu 22.04 (jammy)

libvtk9-dev

Ubuntu jammy package

Ubuntu 20.04 (focal)

libvtk7-dev

Ubuntu focal package

Debian unstable

libvtk9-devel

Debian unstable package

Debian testing

libvtk9-devel

Debian testing package

Debian stable

libvtk9-devel

Debian stable package

Gentoo

vtk

Gentoo package

homebrew

vtk

homebrew version

vckpg

vtk

Vcpkg

spack

vtk

Spack

Note that these packages may be lacking some optional features such as mpi, qt etc. or, they may not contain the latest VTK features. Check the documentation of each package to verify that the build contains what you need. If what you need is missing you will need to build vtk from scratch.

Building an executable

Once VTK is installed using either of the methods above you can use it in your project utilizing the find_package infrastructure of cmake:

find_package(VTK
  COMPONENTS
  .. list of vtk modules to link to
)

# your executable
add_executable(testExample ...)

# link to required VTK libraries
target_link_libraries(testExample
  PRIVATE
   ${VTK_LIBRARIES}
)

vtk_module_autoinit(
  TARGETS testExample
  MODULES ${VTK_LIBRARIES}
)

vtk_module_autoinit() is responsible for triggering static code construction required for some VTK classes. For more details regarding the autoinit system of VTK see here.

The list of required vtk modules depends on the files #included in your code. The module a header file belongs to is determined in most cases by its location in the VTK source tree. For, example vtkXMLPolyDataReader is located under IO/XML so it belongs to the IOXML module, to verify check the accompanying vtk.module file in the same directory.

The above method works in most cases but it does not express the dependencies that some module have. A better (and easier) way to find the required modules is the VTKModulesForCxx script.

For example, running the script on the CylinderExample we get the following suggestion:

find_package(VTK
 COMPONENTS
    CommonColor
    CommonCore
    FiltersSources
    RenderingCore
    #
    # These modules are suggested since they implement an existing module.
    # You may need to uncomment one or more of these.
    # If vtkRenderWindow is used and you want to use OpenGL,
    #   you also need the RenderingOpenGL2 module.
    # If vtkRenderWindowInteractor is used,
    #    uncomment RenderingUI and possibly InteractionStyle.
    # If text rendering is used, uncomment RenderingFreeType
    #
    # InteractionStyle  # implements VTK::RenderingCore
    # RenderingCellGrid # implements VTK::RenderingCore
    # RenderingFreeType # implements VTK::RenderingCore
    # RenderingOpenGL2  # implements VTK::RenderingCore
    # RenderingUI       # implements VTK::RenderingCore
)

Based on the suggestions of the script and the template above the relevant sections of the CMakeLists.txt are:

...
find_package(VTK COMPONENTS
  CommonColor
  CommonCore
  FiltersSources
  InteractionStyle
  RenderingContextOpenGL2
  RenderingCore
  RenderingFreeType
  RenderingGL2PSOpenGL2
  RenderingOpenGL2
)

add_executable(CylinderExample CylinderExample.cxx)
target_link_libraries(CylinderExample PRIVATE ${VTK_LIBRARIES})
# vtk_module_autoinit is needed
vtk_module_autoinit(
  TARGETS CylinderExample
  MODULES ${VTK_LIBRARIES}
)

The full source of the example can be found here.

To build the example:

mkdir build
cd build
ccmake ../ # or cmake-gui if on Windows

Hit C if using ccmake or the configure button if using cmake-gui. If VTK was built from scratch you will need to set VTK_DIR to the installation path. If ccmake/cmake-gui reports no errors quit ccmake/cmake-gui and build the project as follows:

cmake --build .

To run the example

./CylinderExample

For more examples check the tutorials, how to guides or examples sections of the vtk examples website.

Using Javascript#

vtk.js is an implementation of VTK in JavaScript that consists of an ES6 class library which can be integrated into any web application. See here to learn more about the differences between VTK C++ and vtk.js.

Using WebAssembly#

VTK-Wasm is a prototype infrastructure that enables the compilation of VTK C++ code to WebAssembly via Emscripten. This feature is still under active development.

To learn more about VTK-Wasm and its capabilities, please take a look at the following resources:

We welcome your feedback and contributions to this project. Feel free to share your experiences, questions, and ideas in the web/vtk-wasm category of the VTK Discourse forum. Stay tuned for updates and new developments!

Using existing frameworks and applications#

There are many VTK-based, free, open-source applications for scientific, bio-medical and medical image visualization and processing; several of them are extensible frameworks that can be customized for particular use cases. ParaView, Trame, PyVista, and 3D Slicer are examples. Therefore, it is worth evaluating if any of these would allow you to address your challenges. This would save time by avoiding redeveloping everything from scratch and by capitalizing on large communities with thousands of experts.

Generally, the default (complex, but powerful) user interface of these applications allows one to figure out the complete workflow. Once one knows exactly what and how to do it, they can create a small Python scripted module that automates most of the steps and provides a simplified user interface.