Skip to content

OrientationMarkerWidget1

Repository source: OrientationMarkerWidget1


Description

This example uses a polydata as an orientation icon. You can get the bunny data src/Testing/Data/Bunny.vtp.

Other languages

See (Python)

Question

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

Code

OrientationMarkerWidget1.cxx

#include <vtkActor.h>
#include <vtkDataSetMapper.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>
#include <vtkSuperquadricSource.h>
#include <vtkXMLPolyDataReader.h>

int main(int argc, char* argv[])
{
  // Parse command line arguments
  if (argc != 2)
  {
    std::cerr << "Usage: " << argv[0] << " Filename(.vtp) e.g. Bunny.vtp"
              << std::endl;
    return EXIT_FAILURE;
  }

  vtkNew<vtkNamedColors> colors;

  // Read the polydata for the icon
  vtkNew<vtkXMLPolyDataReader> reader;
  reader->SetFileName(argv[1]);

  vtkNew<vtkDataSetMapper> iconMapper;
  iconMapper->SetInputConnection(reader->GetOutputPort());

  vtkNew<vtkActor> iconActor;
  iconActor->SetMapper(iconMapper);
  iconActor->GetProperty()->SetColor(colors->GetColor3d("Silver").GetData());

  // Set up the renderer, window, and interactor
  vtkNew<vtkRenderer> renderer;
  renderer->SetBackground(colors->GetColor3d("SlateGray").GetData());

  vtkNew<vtkRenderWindow> renWin;
  renWin->AddRenderer(renderer);
  renWin->SetSize(400, 400);
  renWin->SetWindowName("OrientationMarkerWidget1");

  vtkNew<vtkRenderWindowInteractor> iren;
  iren->SetRenderWindow(renWin);

  double r;
  double g;
  double b;
  colors->GetColor("Wheat", r, g, b);
  // Set up the widget
  vtkNew<vtkOrientationMarkerWidget> widget;
  widget->SetOrientationMarker(iconActor);
  widget->SetInteractor(iren);
  widget->SetViewport(0.0, 0.0, 0.2, 0.2);
  widget->SetOutlineColor(r, g, b);
  widget->SetEnabled(1);
  widget->InteractiveOn();

  // Create a superquadric
  vtkNew<vtkSuperquadricSource> superquadricSource;
  superquadricSource->SetPhiRoundness(0.2);
  superquadricSource->SetThetaRoundness(0.8);

  // Create a mapper and actor
  vtkNew<vtkPolyDataMapper> superquadricMapper;
  superquadricMapper->SetInputConnection(superquadricSource->GetOutputPort());

  vtkNew<vtkActor> superquadricActor;
  superquadricActor->SetMapper(superquadricMapper);
  superquadricActor->GetProperty()->SetInterpolationToFlat();
  superquadricActor->GetProperty()->SetDiffuseColor(
      colors->GetColor3d("Carrot").GetData());
  superquadricActor->GetProperty()->SetSpecularColor(
      colors->GetColor3d("White").GetData());
  superquadricActor->GetProperty()->SetDiffuse(.6);
  superquadricActor->GetProperty()->SetSpecular(.5);
  superquadricActor->GetProperty()->SetSpecularPower(5.0);

  renderer->AddActor(superquadricActor);
  renderer->ResetCamera();

  renWin->Render();

  iren->Initialize();

  iren->Start();

  return EXIT_SUCCESS;
}

CMakeLists.txt

cmake_minimum_required(VERSION 3.12 FATAL_ERROR)

project(OrientationMarkerWidget1)

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

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

Download and Build OrientationMarkerWidget1

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

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

./OrientationMarkerWidget1

WINDOWS USERS

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