Skip to content

TransformPolyData

vtk-examples/Cxx/Filtering/TransformPolyData


Description

This example demonstrates how to apply a transform to a data set. It uses vtkTransformPolyDataFilter, but it can be replaced with vtkTransformFilter for different types of data sets, including vtkUnstructuredGrid and vtkStructuredGrid. vtkTransformFilter will work with vtkPolyData, too).

Other languages

See (Python), (Java)

Question

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

Code

TransformPolyData.cxx

#include <vtkActor.h>
#include <vtkNamedColors.h>
#include <vtkNew.h>
#include <vtkPolyData.h>
#include <vtkPolyDataMapper.h>
#include <vtkProperty.h>
#include <vtkRenderWindow.h>
#include <vtkRenderWindowInteractor.h>
#include <vtkRenderer.h>
#include <vtkSphereSource.h>
#include <vtkTransform.h>
#include <vtkTransformPolyDataFilter.h>

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

  // Create the polydata geometry

  vtkNew<vtkSphereSource> sphereSource;
  sphereSource->Update();

  // Set up the actor to display the untransformed polydata

  vtkNew<vtkPolyDataMapper> originalMapper;
  originalMapper->SetInputConnection(sphereSource->GetOutputPort());

  vtkNew<vtkActor> originalActor;
  originalActor->SetMapper(originalMapper);
  originalActor->GetProperty()->SetColor(colors->GetColor3d("Blue").GetData());

  // Set up the transform filter

  vtkNew<vtkTransform> translation;
  translation->Translate(1.0, 2.0, 3.0);

  vtkNew<vtkTransformPolyDataFilter> transformFilter;
  transformFilter->SetInputConnection(sphereSource->GetOutputPort());
  transformFilter->SetTransform(translation);
  transformFilter->Update();

  // Set up the actor to display the transformed polydata

  vtkNew<vtkPolyDataMapper> transformedMapper;
  transformedMapper->SetInputConnection(transformFilter->GetOutputPort());

  vtkNew<vtkActor> transformedActor;
  transformedActor->SetMapper(transformedMapper);
  transformedActor->GetProperty()->SetColor(
      colors->GetColor3d("Red").GetData());

  // Set up the rest of the visualization pipeline

  vtkNew<vtkRenderer> renderer;
  renderer->AddActor(originalActor);
  renderer->AddActor(transformedActor);
  renderer->SetBackground(colors->GetColor3d("Green").GetData());

  vtkNew<vtkRenderWindow> renderWindow;
  renderWindow->AddRenderer(renderer);

  vtkNew<vtkRenderWindowInteractor> renderWindowInteractor;
  renderWindowInteractor->SetRenderWindow(renderWindow);

  renderWindow->SetWindowName("TransformPolyData");
  renderWindow->Render();
  renderWindowInteractor->Start();

  return EXIT_SUCCESS;
}

CMakeLists.txt

cmake_minimum_required(VERSION 3.12 FATAL_ERROR)

project(TransformPolyData)

find_package(VTK COMPONENTS 
  CommonColor
  CommonCore
  CommonDataModel
  CommonTransforms
  FiltersGeneral
  FiltersSources
  InteractionStyle
  RenderingContextOpenGL2
  RenderingCore
  RenderingFreeType
  RenderingGL2PSOpenGL2
  RenderingOpenGL2
)

if (NOT VTK_FOUND)
  message(FATAL_ERROR "TransformPolyData: Unable to find the VTK build folder.")
endif()

# Prevent a "command line is too long" failure in Windows.
set(CMAKE_NINJA_FORCE_RESPONSE_FILE "ON" CACHE BOOL "Force Ninja to use response files.")
add_executable(TransformPolyData MACOSX_BUNDLE TransformPolyData.cxx )
  target_link_libraries(TransformPolyData PRIVATE ${VTK_LIBRARIES}
)
# vtk_module_autoinit is needed
vtk_module_autoinit(
  TARGETS TransformPolyData
  MODULES ${VTK_LIBRARIES}
)

Download and Build TransformPolyData

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

cd TransformPolyData/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:

./TransformPolyData

WINDOWS USERS

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