Skip to article frontmatterSkip to article content
Site not loading correctly?

This may be due to an incorrect BASE_URL configuration. See the MyST Documentation for reference.

VTK Tutorial 10: Rotation

Western University, Canada

In this (10th10^{th}) tutorial, we are going to visualize rotation of an object using VTK.

More examples and tutorials can be found here

To begin with, we need to import all the necessary Python functions and external libraries:

Rotation

We previously discussed how to transform an object by rotation. Here, we are going visualize this process.

Python Setup

We will focus on using

from vtkmodules.vtkCommonTransforms import vtkTransform
import math
import numpy as np

# noinspection PyUnresolvedReferences
import vtkmodules.vtkInteractionStyle
# noinspection PyUnresolvedReferences
import vtkmodules.vtkRenderingOpenGL2
from vtkmodules.vtkCommonColor import vtkNamedColors
from vtkmodules.vtkCommonTransforms import vtkTransform
from vtkmodules.vtkFiltersSources import vtkSphereSource
from vtkmodules.vtkRenderingAnnotation import vtkAxesActor
from vtkmodules.vtkRenderingCore import (
    vtkActor,
    vtkPolyDataMapper,
    vtkRenderWindow,
    vtkRenderWindowInteractor,
    vtkRenderer
)

Rendering

# Visualization Pipeline

# a renderer and render window
ren = vtkRenderer()
renWin = vtkRenderWindow()
renWin.SetWindowName('AISE 4025: rotation')
renWin.SetSize( 640, 480 )
renWin.AddRenderer( ren )

# an interactor
iren = vtkRenderWindowInteractor()
iren.SetRenderWindow( renWin )

Geometry

Instead of visualizing rotation of a point, we will rotate an orthonormal axes instead.

colors = vtkNamedColors()

# add an 3D Axes
originalAxes = vtkAxesActor()
originalAxes.SetTotalLength(5, 5, 5)
originalAxes.AxisLabelsOff()

# properties of the axes labels can be set as follows
# this sets the x axis label to red
originalAxes.GetXAxisCaptionActor2D().GetCaptionTextProperty().SetColor(colors.GetColor3d('Red'));
originalAxes.GetYAxisCaptionActor2D().GetCaptionTextProperty().SetColor(colors.GetColor3d('Green'));
originalAxes.GetZAxisCaptionActor2D().GetCaptionTextProperty().SetColor(colors.GetColor3d('Blue'));

Specify the geometry of the translational vector

alpha = 60 # 60 degree
beta = 45
transform = vtkTransform()
transform.PostMultiply()
transform.Identity()
transform.RotateX( alpha )
transform.RotateY( beta )

originalAxes.SetUserTransform( transform )

We can look inside of the transform, one see that the transformation is represented as a 4×44 \times 4 matrix, and the translational vector is the ttht^{th} column.

print(transform)

We can add a 3D axes to help us orient

# add an 3D Axes
axes = vtkAxesActor()
axes.SetTotalLength(10,10,10)
axes.AxisLabelsOff()

# properties of the axes labels can be set as follows
# this sets the x axis label to red
axes.GetXAxisCaptionActor2D().GetCaptionTextProperty().SetColor(colors.GetColor3d('Red'));
axes.GetYAxisCaptionActor2D().GetCaptionTextProperty().SetColor(colors.GetColor3d('Green'));
axes.GetZAxisCaptionActor2D().GetCaptionTextProperty().SetColor(colors.GetColor3d('Blue'));

Add all actors to the renderer so the renderer knows what to draw.

# Add the actors to the scene
ren.AddActor( originalAxes )
ren.AddActor( axes )
ren.SetBackground( colors.GetColor3d( 'MidnightBlue') ) # the color can be specified as a name
ren.SetBackground( .1, .2, .4 )                         # or as RGB
ren.SetBackground( 1, 1, 1 )                            # this is white
ren.ResetCamera()

Draw onto the render window and start the user interaction.

renWin.Render()
iren.Start()

A window will pop up:

rotation transform

Figure 1:Orthogonal axes (shorter one) being rotated.

Use the mouse to change the viewing angle!

Press ‘q’ to exit