RotationAroundLine

VTKExamples/Cxx/PolyData/RotationAroundLine

Description

This example demonstrates how to rotate an object around an axis. For the demo, we rotate a cone 10 degrees around the y axis.

Other Languages

See (Python)

Code

RotationAroundLine.cxx

#include <vtkConeSource.h>
#include <vtkPolyData.h>
#include <vtkTransformPolyDataFilter.h>
#include <vtkTransform.h>
#include <vtkSmartPointer.h>
#include <vtkXMLPolyDataWriter.h>

int main(int, char *[])
{
  //Create a cone
  vtkSmartPointer<vtkConeSource> coneSource = 
    vtkSmartPointer<vtkConeSource>::New();

  {
  vtkSmartPointer<vtkXMLPolyDataWriter> writer = 
      vtkSmartPointer<vtkXMLPolyDataWriter>::New();
  writer->SetInputConnection(coneSource->GetOutputPort());
  writer->SetFileName("original.vtp");
  writer->Write();
  }

  vtkSmartPointer<vtkTransform> transform = vtkSmartPointer<vtkTransform>::New();
  //transform->RotateWXYZ(double angle, double x, double y, double z);
  transform->RotateWXYZ(10, 0, 1, 0);

  vtkSmartPointer<vtkTransformPolyDataFilter> transformFilter = 
      vtkSmartPointer<vtkTransformPolyDataFilter>::New();

  transformFilter->SetTransform(transform);
  transformFilter->SetInputConnection(coneSource->GetOutputPort());
  transformFilter->Update();

  {
    vtkSmartPointer<vtkXMLPolyDataWriter> writer = 
        vtkSmartPointer<vtkXMLPolyDataWriter>::New();
    writer->SetInputConnection(transformFilter->GetOutputPort());
    writer->SetFileName("transformed.vtp");
    writer->Write();
  }

  return EXIT_SUCCESS;
}

CMakeLists.txt

cmake_minimum_required(VERSION 3.3 FATAL_ERROR)

project(RotationAroundLine)

find_package(VTK COMPONENTS 
  vtkCommonCore
  vtkCommonDataModel
  vtkCommonTransforms
  vtkFiltersGeneral
  vtkFiltersSources
  vtkIOXML QUIET)
if (NOT VTK_FOUND)
  message("Skipping RotationAroundLine: ${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(RotationAroundLine MACOSX_BUNDLE RotationAroundLine.cxx )
  target_link_libraries(RotationAroundLine PRIVATE ${VTK_LIBRARIES})
else ()
  # include all components
  add_executable(RotationAroundLine MACOSX_BUNDLE RotationAroundLine.cxx )
  target_link_libraries(RotationAroundLine PRIVATE ${VTK_LIBRARIES})
  # vtk_module_autoinit is needed
  vtk_module_autoinit(
    TARGETS RotationAroundLine
    MODULES ${VTK_LIBRARIES}
    )
endif () 

Download and Build RotationAroundLine

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

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

./RotationAroundLine

WINDOWS USERS

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