Skip to content

RotationAroundLine

vtk-examples/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), (Java)

Question

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

Code

RotationAroundLine.cxx

#include <vtkActor.h>
#include <vtkArrowSource.h>
#include <vtkConeSource.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 <vtkTransform.h>
#include <vtkTransformPolyDataFilter.h>
#include <vtkTransformPolyDataFilter.h>

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

  // Create a rendering window and renderer
  vtkNew<vtkRenderer> ren;
  vtkNew<vtkRenderWindow> renWin;
  renWin->AddRenderer(ren);
  renWin->SetSize(640, 480);
  renWin->SetWindowName("RotationAroundLine");

  // Create a renderwindowinteractor
  vtkNew<vtkRenderWindowInteractor> iren;
  iren->SetRenderWindow(renWin);

  // Create arrow
  vtkNew<vtkArrowSource> source;

  // Create a transform that rotates the arrow 45° around the z-axis
  vtkNew<vtkTransform> transform;
  transform->RotateWXYZ(45, 0, 0, 1);
  vtkNew<vtkTransformPolyDataFilter> transformFilter;
  transformFilter->SetTransform(transform);
  transformFilter->SetInputConnection(source->GetOutputPort());
  transformFilter->Update();

  // Mapper for the original arrow
  vtkNew<vtkPolyDataMapper> coneMapper1;
  coneMapper1->SetInputConnection(source->GetOutputPort());

  // Another mapper for the rotated arrow
  vtkNew<vtkPolyDataMapper> coneMapper2;
  coneMapper2->SetInputConnection(transformFilter->GetOutputPort());

  // Actor for original arrow
  vtkNew<vtkActor> actor1;
  actor1->SetMapper(coneMapper1);

  // Actor for rotated arrow
  vtkNew<vtkActor> actor2;
  actor2->SetMapper(coneMapper2);

  // Color the original arrow
  actor1->GetProperty()->SetColor(colors->GetColor3d("LightCoral").GetData());
  // Color rotated arrow
  actor2->GetProperty()->SetColor(
      colors->GetColor3d("PaleTurquoise").GetData());

  // Assign actor to the renderer
  ren->AddActor(actor1);
  ren->AddActor(actor2);
  ren->SetBackground(colors->GetColor3d("SlateGray").GetData());

  // Enable the user interface interactor
  iren->Initialize();
  renWin->Render();
  iren->Start();

  return EXIT_SUCCESS;
}

CMakeLists.txt

cmake_minimum_required(VERSION 3.3 FATAL_ERROR)

project(RotationAroundLine)

find_package(VTK COMPONENTS 
  vtkCommonColor
  vtkCommonCore
  vtkCommonDataModel
  vtkCommonTransforms
  vtkFiltersGeneral
  vtkFiltersSources
  vtkInteractionStyle
  vtkRenderingContextOpenGL2
  vtkRenderingCore
  vtkRenderingFreeType
  vtkRenderingGL2PSOpenGL2
  vtkRenderingOpenGL2
  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.