Difference between revisions of "VTK/Examples/Cxx/Filtering/TransformPolyData"

From KitwarePublic
< VTK‎ | Examples‎ | Cxx
Jump to navigationJump to search
(link to classes)
(→‎TransformPolyData.cxx: reorder code and document sections to make the program's structure more evident. add highlight to key lines)
Line 3: Line 3:
  
 
==TransformPolyData.cxx==
 
==TransformPolyData.cxx==
<source lang="cpp">
+
<source lang="cpp" highlight="5, 32-42, 48, 52, 60">
 
#include <vtkSmartPointer.h>
 
#include <vtkSmartPointer.h>
#include <vtkTransformPolyDataFilter.h>
 
 
#include <vtkSphereSource.h>
 
#include <vtkSphereSource.h>
 
#include <vtkPolyData.h>
 
#include <vtkPolyData.h>
 
#include <vtkTransform.h>
 
#include <vtkTransform.h>
 +
#include <vtkTransformPolyDataFilter.h>
 
#include <vtkPolyDataMapper.h>
 
#include <vtkPolyDataMapper.h>
 
#include <vtkActor.h>
 
#include <vtkActor.h>
 +
#include <vtkProperty.h>
 
#include <vtkRenderWindow.h>
 
#include <vtkRenderWindow.h>
 
#include <vtkRenderer.h>
 
#include <vtkRenderer.h>
 
#include <vtkRenderWindowInteractor.h>
 
#include <vtkRenderWindowInteractor.h>
#include <vtkProperty.h>
 
  
 
int main(int, char *[])
 
int main(int, char *[])
 
{
 
{
 +
  // Create the polydata geometry
 +
 
   vtkSmartPointer<vtkSphereSource> sphereSource =
 
   vtkSmartPointer<vtkSphereSource> sphereSource =
 
     vtkSmartPointer<vtkSphereSource>::New();
 
     vtkSmartPointer<vtkSphereSource>::New();
 
   sphereSource->Update();
 
   sphereSource->Update();
 +
 +
  // Set up the actor to display the untransformed polydata
 +
 +
  vtkSmartPointer<vtkPolyDataMapper> originalMapper =
 +
    vtkSmartPointer<vtkPolyDataMapper>::New();
 +
  originalMapper->SetInputConnection(sphereSource->GetOutputPort());
 +
 +
  vtkSmartPointer<vtkActor> originalActor =
 +
    vtkSmartPointer<vtkActor>::New();
 +
  originalActor->SetMapper(originalMapper);
 +
  originalActor->GetProperty()->SetColor(1,0,0);
 +
 +
  // Set up the transform filter
  
 
   vtkSmartPointer<vtkTransform> translation =
 
   vtkSmartPointer<vtkTransform> translation =
Line 32: Line 47:
 
   transformFilter->Update();
 
   transformFilter->Update();
  
   // Create a mapper and actor
+
   // Set up the actor to display the transformed polydata
  vtkSmartPointer<vtkPolyDataMapper> originalMapper =
 
    vtkSmartPointer<vtkPolyDataMapper>::New();
 
  originalMapper->SetInputConnection(sphereSource->GetOutputPort());
 
 
 
  vtkSmartPointer<vtkActor> originalActor =
 
    vtkSmartPointer<vtkActor>::New();
 
  originalActor->SetMapper(originalMapper);
 
  originalActor->GetProperty()->SetColor(1,0,0);
 
  
  // Create a mapper and actor
 
 
   vtkSmartPointer<vtkPolyDataMapper> transformedMapper =
 
   vtkSmartPointer<vtkPolyDataMapper> transformedMapper =
 
     vtkSmartPointer<vtkPolyDataMapper>::New();
 
     vtkSmartPointer<vtkPolyDataMapper>::New();
Line 52: Line 58:
 
   transformedActor->GetProperty()->SetColor(0,1,0);
 
   transformedActor->GetProperty()->SetColor(0,1,0);
  
   // Create a renderer, render window, and interactor
+
   // Set up the rest of the visualization pipeline
 +
 
 
   vtkSmartPointer<vtkRenderer> renderer =
 
   vtkSmartPointer<vtkRenderer> renderer =
 
     vtkSmartPointer<vtkRenderer>::New();
 
     vtkSmartPointer<vtkRenderer>::New();
 +
  renderer->AddActor(originalActor);
 +
  renderer->AddActor(transformedActor);
 +
  renderer->SetBackground(.3, .6, .3); // Set renderer's background color to green
 +
 
   vtkSmartPointer<vtkRenderWindow> renderWindow =
 
   vtkSmartPointer<vtkRenderWindow> renderWindow =
 
     vtkSmartPointer<vtkRenderWindow>::New();
 
     vtkSmartPointer<vtkRenderWindow>::New();
 
   renderWindow->AddRenderer(renderer);
 
   renderWindow->AddRenderer(renderer);
 +
 
   vtkSmartPointer<vtkRenderWindowInteractor> renderWindowInteractor =
 
   vtkSmartPointer<vtkRenderWindowInteractor> renderWindowInteractor =
 
     vtkSmartPointer<vtkRenderWindowInteractor>::New();
 
     vtkSmartPointer<vtkRenderWindowInteractor>::New();
 
   renderWindowInteractor->SetRenderWindow(renderWindow);
 
   renderWindowInteractor->SetRenderWindow(renderWindow);
  
  // Add the actor to the scene
 
  renderer->AddActor(originalActor);
 
  renderer->AddActor(transformedActor);
 
  renderer->SetBackground(.3, .6, .3); // Background color green
 
 
  // Render and interact
 
  renderWindow->Render();
 
 
   renderWindowInteractor->Start();
 
   renderWindowInteractor->Start();
 
    
 
    

Revision as of 12:54, 29 June 2016

VTK Examples Baseline Filters TestTransformPolyData.png

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).

TransformPolyData.cxx

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

int main(int, char *[])
{
  // Create the polydata geometry

  vtkSmartPointer<vtkSphereSource> sphereSource =
    vtkSmartPointer<vtkSphereSource>::New();
  sphereSource->Update();

  // Set up the actor to display the untransformed polydata
 
  vtkSmartPointer<vtkPolyDataMapper> originalMapper =
    vtkSmartPointer<vtkPolyDataMapper>::New();
  originalMapper->SetInputConnection(sphereSource->GetOutputPort());

  vtkSmartPointer<vtkActor> originalActor =
    vtkSmartPointer<vtkActor>::New();
  originalActor->SetMapper(originalMapper);
  originalActor->GetProperty()->SetColor(1,0,0);

  // Set up the transform filter

  vtkSmartPointer<vtkTransform> translation =
    vtkSmartPointer<vtkTransform>::New();
  translation->Translate(1.0, 2.0, 3.0);

  vtkSmartPointer<vtkTransformPolyDataFilter> transformFilter =
    vtkSmartPointer<vtkTransformPolyDataFilter>::New();
  transformFilter->SetInputConnection(sphereSource->GetOutputPort());
  transformFilter->SetTransform(translation);
  transformFilter->Update();

  // Set up the actor to display the transformed polydata

  vtkSmartPointer<vtkPolyDataMapper> transformedMapper =
    vtkSmartPointer<vtkPolyDataMapper>::New();
  transformedMapper->SetInputConnection(transformFilter->GetOutputPort());

  vtkSmartPointer<vtkActor> transformedActor =
    vtkSmartPointer<vtkActor>::New();
  transformedActor->SetMapper(transformedMapper);
  transformedActor->GetProperty()->SetColor(0,1,0);

  // Set up the rest of the visualization pipeline

  vtkSmartPointer<vtkRenderer> renderer =
    vtkSmartPointer<vtkRenderer>::New();
  renderer->AddActor(originalActor);
  renderer->AddActor(transformedActor);
  renderer->SetBackground(.3, .6, .3); // Set renderer's background color to green

  vtkSmartPointer<vtkRenderWindow> renderWindow =
    vtkSmartPointer<vtkRenderWindow>::New();
  renderWindow->AddRenderer(renderer);

  vtkSmartPointer<vtkRenderWindowInteractor> renderWindowInteractor =
    vtkSmartPointer<vtkRenderWindowInteractor>::New();
  renderWindowInteractor->SetRenderWindow(renderWindow);

  renderWindowInteractor->Start();
  
  return EXIT_SUCCESS;
}

Please try the new VTKExamples website.

CMakeLists.txt

cmake_minimum_required(VERSION 2.8)

PROJECT(TransformPolyData)

find_package(VTK REQUIRED)
include(${VTK_USE_FILE})

add_executable(TransformPolyData MACOSX_BUNDLE TransformPolyData.cxx)

if(VTK_LIBRARIES)
  target_link_libraries(TransformPolyData ${VTK_LIBRARIES})
else()
  target_link_libraries(TransformPolyData vtkHybrid vtkWidgets)
endif()

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 PLEASE NOTE: Be sure to add the VTK bin directory to your path. This will resolve the VTK dll's at run time.