Skip to content

TransformOrderDemo

vtk-examples/Cxx/PolyData/TransformOrderDemo


Description

This example creates an image. In the left viewport, it translates the image, then translates and rotates the image. In the right viewport, it rotates the image, then rotates and translates the image. One can see that the result is clearly not the same!

Question

If you have a question about this example, please use the VTK Discourse Forum

Code

TransformOrderDemo.cxx

#include <vtkAxesActor.h>
#include <vtkCamera.h>
#include <vtkImageActor.h>
#include <vtkImageCast.h>
#include <vtkImageMandelbrotSource.h>
#include <vtkImageMapper3D.h>
#include <vtkNamedColors.h>
#include <vtkNew.h>
#include <vtkProperty.h>
#include <vtkRenderWindow.h>
#include <vtkRenderWindowInteractor.h>
#include <vtkRenderer.h>
#include <vtkTransform.h>

int main(int, char*[])
{
  vtkNew<vtkNamedColors> colors;

  // Create an image
  vtkNew<vtkImageMandelbrotSource> source;
  source->Update();

  vtkNew<vtkImageCast> castFilter;
  castFilter->SetInputConnection(source->GetOutputPort());
  castFilter->SetOutputScalarTypeToUnsignedChar();
  castFilter->Update();

  // Create an actor
  vtkNew<vtkImageActor> actor1a;
  actor1a->GetMapper()->SetInputConnection(castFilter->GetOutputPort());
  vtkNew<vtkImageActor> actor1b;
  actor1b->GetMapper()->SetInputConnection(castFilter->GetOutputPort());

  vtkNew<vtkImageActor> actor2a;
  actor2a->GetMapper()->SetInputConnection(castFilter->GetOutputPort());

  vtkNew<vtkImageActor> actor2b;
  actor2b->GetMapper()->SetInputConnection(castFilter->GetOutputPort());

  // Create the transformation
  vtkNew<vtkTransform> transform1a;
  transform1a->PostMultiply();
  transform1a->Translate(10.0, 0.0, 0.0);
  actor1a->SetUserTransform(transform1a);

  vtkNew<vtkTransform> transform1b;
  transform1b->PostMultiply();
  transform1b->Translate(10.0, 0.0, 0.0);
  transform1b->RotateZ(40.0);
  actor1b->SetUserTransform(transform1b);

  vtkNew<vtkTransform> transform2a;
  transform2a->PostMultiply();
  transform2a->RotateZ(40.0);
  actor2a->SetUserTransform(transform2a);

  vtkNew<vtkTransform> transform2b;
  transform2b->PostMultiply();
  transform2b->RotateZ(40.0);
  transform2b->Translate(10.0, 0.0, 0.0);
  actor2b->SetUserTransform(transform2b);

  // Define viewport ranges
  // (xmin, ymin, xmax, ymax)
  double leftViewport[4] = {0.0, 0.0, 0.5, 1.0};
  double rightViewport[4] = {0.5, 0.0, 1.0, 1.0};

  // Cetup render window
  vtkNew<vtkRenderWindow> renderWindow;
  renderWindow->SetSize(640, 640);
  renderWindow->SetWindowName("TransformOrderDemo");

  // Setup both renderers
  vtkNew<vtkRenderer> leftRenderer;
  renderWindow->AddRenderer(leftRenderer);
  leftRenderer->SetViewport(leftViewport);
  leftRenderer->SetBackground(colors->GetColor3d("BurlyWood").GetData());

  vtkNew<vtkRenderer> rightRenderer;
  renderWindow->AddRenderer(rightRenderer);
  rightRenderer->SetViewport(rightViewport);
  rightRenderer->SetBackground(colors->GetColor3d("SteelBlue").GetData());

  // an interactor
  vtkNew<vtkRenderWindowInteractor> interactor;

  interactor->SetRenderWindow(renderWindow);

  vtkNew<vtkAxesActor> axes;
  axes->AxisLabelsOff();
  axes->SetTotalLength(5, 5, 5);

  vtkCamera* camera;
  camera = leftRenderer->GetActiveCamera();
  leftRenderer->AddActor(axes);
  leftRenderer->AddActor(actor1a);
  leftRenderer->AddActor(actor1b);

  rightRenderer->AddActor(axes);
  rightRenderer->AddActor(actor2a);
  rightRenderer->AddActor(actor2b);
  rightRenderer->SetActiveCamera(camera);

  leftRenderer->ResetCamera();
  // rightRenderer->ResetCamera();
  camera->Zoom(1.3);

  renderWindow->Render();

  interactor->Start();

  return EXIT_SUCCESS;
}

CMakeLists.txt

cmake_minimum_required(VERSION 3.3 FATAL_ERROR)

project(TransformOrderDemo)

find_package(VTK COMPONENTS 
  vtkCommonColor
  vtkCommonCore
  vtkCommonTransforms
  vtkImagingCore
  vtkImagingSources
  vtkInteractionStyle
  vtkRenderingAnnotation
  vtkRenderingContextOpenGL2
  vtkRenderingCore
  vtkRenderingFreeType
  vtkRenderingGL2PSOpenGL2
  vtkRenderingOpenGL2
  QUIET
)

if (NOT VTK_FOUND)
  message("Skipping TransformOrderDemo: ${VTK_NOT_FOUND_MESSAGE}")
  return ()
endif()
message (STATUS "VTK_VERSION: ${VTK_VERSION}")
if (VTK_VERSION VERSION_LESS "8.90.0")
  # old system
  include(${VTK_USE_FILE})
  add_executable(TransformOrderDemo MACOSX_BUNDLE TransformOrderDemo.cxx )
  target_link_libraries(TransformOrderDemo PRIVATE ${VTK_LIBRARIES})
else ()
  # include all components
  add_executable(TransformOrderDemo MACOSX_BUNDLE TransformOrderDemo.cxx )
  target_link_libraries(TransformOrderDemo PRIVATE ${VTK_LIBRARIES})
  # vtk_module_autoinit is needed
  vtk_module_autoinit(
    TARGETS TransformOrderDemo
    MODULES ${VTK_LIBRARIES}
    )
endif ()

Download and Build TransformOrderDemo

Click here to download TransformOrderDemo and its CMakeLists.txt file. Once the tarball TransformOrderDemo.tar has been downloaded and extracted,

cd TransformOrderDemo/build

If VTK is installed:

cmake ..

If VTK is not installed but compiled on your system, you will need to specify the path to your VTK build:

cmake -DVTK_DIR:PATH=/home/me/vtk_build ..

Build the project:

make

and run it:

./TransformOrderDemo

WINDOWS USERS

Be sure to add the VTK bin directory to your path. This will resolve the VTK dll's at run time.