Skip to content

OrientationMarkerWidget

vtk-examples/Cxx/Widgets/OrientationMarkerWidget


Other languages

See (Python), (Java)

Question

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

Code

OrientationMarkerWidget.cxx

#include <vtkActor.h>
#include <vtkAnnotatedCubeActor.h>
#include <vtkCamera.h>
#include <vtkCubeSource.h>
#include <vtkNamedColors.h>
#include <vtkNew.h>
#include <vtkOrientationMarkerWidget.h>
#include <vtkPolyDataMapper.h>
#include <vtkProperty.h>
#include <vtkRenderWindow.h>
#include <vtkRenderWindowInteractor.h>
#include <vtkRenderer.h>

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

  // create a rendering window and renderer;
  vtkNew<vtkRenderer> ren;
  vtkNew<vtkRenderWindow> renWin;
  renWin->AddRenderer(ren);
  renWin->SetWindowName("OrientationMarkerWidget");

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

  vtkNew<vtkCubeSource> cube;
  cube->SetXLength(200);
  cube->SetYLength(200);
  cube->SetZLength(200);
  cube->Update();
  vtkNew<vtkPolyDataMapper> cm;
  cm->SetInputConnection(cube->GetOutputPort());
  vtkNew<vtkActor> ca;
  ca->SetMapper(cm);
  ca->GetProperty()->SetColor(colors->GetColor3d("BurlyWood").GetData());
  ca->GetProperty()->EdgeVisibilityOn();
  ca->GetProperty()->SetEdgeColor(colors->GetColor3d("Red").GetData());

  // assign actor to the renderer;
  ren->AddActor(ca);
  ren->SetBackground(colors->GetColor3d("CornflowerBlue").GetData());

  vtkNew<vtkAnnotatedCubeActor> axesActor;
  axesActor->SetXPlusFaceText("L");
  axesActor->SetXMinusFaceText("R");
  axesActor->SetYMinusFaceText("I");
  axesActor->SetYPlusFaceText("S");
  axesActor->SetZMinusFaceText("P");
  axesActor->SetZPlusFaceText("A");
  axesActor->GetTextEdgesProperty()->SetColor(
      colors->GetColor3d("Yellow").GetData());
  axesActor->GetTextEdgesProperty()->SetLineWidth(2);
  axesActor->GetCubeProperty()->SetColor(colors->GetColor3d("Blue").GetData());
  vtkNew<vtkOrientationMarkerWidget> axes;
  axes->SetOrientationMarker(axesActor);
  axes->SetInteractor(iren);
  axes->EnabledOn();
  axes->InteractiveOn();
  ren->ResetCamera();

  // enable user interface interactor;
  iren->Initialize();
  renWin->Render();
  ren->GetActiveCamera()->Azimuth(45);
  ren->GetActiveCamera()->Elevation(30);
  renWin->Render();
  iren->Start();

  return EXIT_SUCCESS;
}

CMakeLists.txt

cmake_minimum_required(VERSION 3.12 FATAL_ERROR)

project(OrientationMarkerWidget)

find_package(VTK COMPONENTS 
  CommonColor
  CommonCore
  FiltersSources
  InteractionStyle
  InteractionWidgets
  RenderingAnnotation
  RenderingContextOpenGL2
  RenderingCore
  RenderingFreeType
  RenderingGL2PSOpenGL2
  RenderingOpenGL2
)

if (NOT VTK_FOUND)
  message(FATAL_ERROR "OrientationMarkerWidget: 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(OrientationMarkerWidget MACOSX_BUNDLE OrientationMarkerWidget.cxx )
  target_link_libraries(OrientationMarkerWidget PRIVATE ${VTK_LIBRARIES}
)
# vtk_module_autoinit is needed
vtk_module_autoinit(
  TARGETS OrientationMarkerWidget
  MODULES ${VTK_LIBRARIES}
)

Download and Build OrientationMarkerWidget

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

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

./OrientationMarkerWidget

WINDOWS USERS

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