Skip to content

CameraOrientationWidget

vtk-examples/Cxx/Widgets/CameraOrientationWidget

Description

This example demonstrates a 3D camera orientation gizmo.

The widget representation has shafts and little spheres with text on them. The spheres always follow the camera.

The widget representation's orientation is synchronized with the camera of the parent renderer.

To look down on any particular axis, simply click on a handle.

To rotate the camera and get a feel of the camera orientation, either move mouse in the renderer or click on a handle and move it around.

Other languages

See (Python)

Question

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

Code

CameraOrientationWidget.cxx

#include <vtkActor.h>
#include <vtkCameraOrientationWidget.h>
#include <vtkNamedColors.h>
#include <vtkNew.h>
#include <vtkPolyDataMapper.h>
#include <vtkProperty.h>
#include <vtkRenderWindow.h>
#include <vtkRenderWindowInteractor.h>
#include <vtkRenderer.h>
#include <vtkXMLPolyDataReader.h>

int main(int argc, char* argv[])
{
  if (argc < 2)
  {
    std::cout << "Demonstrates a 3D camera orientation widget." << std::endl;
    std::cout << "Usage: " << argv[0] << " fileName" << std::endl;
    std::cout << "where: fileName is cow.vtp." << std::endl;
    return EXIT_FAILURE;
  }
  std::string fileName(argv[1]);

  vtkNew<vtkNamedColors> colors;

  vtkNew<vtkRenderer> renderer;
  vtkNew<vtkRenderWindow> renWin;
  vtkNew<vtkRenderWindowInteractor> iRen;

  vtkNew<vtkXMLPolyDataReader> reader;
  reader->SetFileName(fileName.c_str());

  vtkNew<vtkPolyDataMapper> mapper;
  mapper->SetInputConnection(reader->GetOutputPort());

  vtkNew<vtkActor> actor;
  actor->GetProperty()->SetColor(colors->GetColor3d("Beige").GetData());
  actor->SetMapper(mapper);

  renderer->AddActor(actor);
  renderer->SetBackground(colors->GetColor3d("DimGray").GetData());

  renWin->AddRenderer(renderer);
  renWin->SetSize(600, 600);
  renWin->SetWindowName("CameraOrientationWidget");

  // Important: The interactor must be set prior to enabling the widget.
  iRen->SetRenderWindow(renWin);

  vtkNew<vtkCameraOrientationWidget> camOrientManipulator;
  camOrientManipulator->SetParentRenderer(renderer);
  // Enable the widget.
  camOrientManipulator->On();

  renWin->Render();
  iRen->Initialize();
  iRen->Start();

  return EXIT_SUCCESS;
}

CMakeLists.txt

cmake_minimum_required(VERSION 3.12 FATAL_ERROR)

project(CameraOrientationWidget)

find_package(VTK COMPONENTS 
)

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

Download and Build CameraOrientationWidget

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

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

./CameraOrientationWidget

WINDOWS USERS

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