VTK: Transformations

A colour pattern transforming in colour.

Sixth article in a series on VTK covers geometric transformations of models.

This is article 6 in a series on VTK. The first in this series can be found here. We discuss how to transform and manipulate the position and geometry of a visualisation’s models.

There are several ways in which VTK can set and transform the shape and position of models in a scene. Actors have their own local origin and coordinate system. The scene itself uses a different coordinate system with its own global origin. When an actor is added to a scene using the .AddActor(actor) method of a vtkRenderer instance the mesh referenced by the actor is added to the scene with the two origins overlapping.

To move a model you can modify the polygon data of the mesh (not recommended) or apply a translation to the actor referencing that mesh. Translating the actor makes it possible to position individual instances of a mesh without deforming any other instance of an actor.

An actor has several methods to define transformations. We are interested in the 3 operations to translate, scale, and, rotate the actor.

Moving an Actor

The .SetPosition(x, y, z) method of an actor will move the actor such that the local origin of the actor is moved to the position specified by x, y, and z in the global coordinates. The current position of an actor is available via the .GetPosition() method.

To move an actor relative to its current position use .AddPosition(x, y, z).

Scaling an Actor

The scale of an actor is set with the .SetScale() method. It can take either 1 or 3 parameters. A single scalar value will scale the actor equally in all directions, e.g., .SetScale(3.0) will triple the size of the model equally in all directions.

It is possible to scale an actor different amounts across different axes. To do so three parameters must be sent to .SetScale(). In the example below the actor is first scaled by a factor of 5 in each direction, then scaled by factors of 4, 2, and 3, in the x, y, and z directions.

factor = 5
actor.SetScale(factor)
factor = [4.0, 2.0, 3.0]
actor.SetScale(factor)
Rotating an Actor

Actors can be rotated using the three methods: .RotateX(theta), .RotateY(theta), and .RotateZ(theta). Here theta is the angle in degrees by which the model is rotated. These angles are relative to the model’s local coordinate system, not the global coordinate system.

A note about the order of rotations

It is important to consider the order in which rotations are applied. Rotations are noncommutative. This means that changing the order of a set of rotations changes the result.

Also note that translation occurs in the global coordinate system, whereas rotations and scaling occur in the local coordinate system of the actor’s mesh.

A consequence of the above is that there are also multiple ways to rotate an object to arrive at a particular orientation. Actors have a .GetOrientation() method which returns the current orientation of the model. These values are specified such that an actor would arrive at its current orientation if rotations are performed in the order of z, x, then y. Similarly, the method .AddOrientation(x, y, z) is equivalent to performing .RotateZ(z), .RotateX(x), and .RotateY(y) in that order.

Continue here for an example that brings all that we have learnt so far together. We create a 3D function plotting application.