Skip to content

CompassWidget

web-test/Cxx/Widgets/CompassWidget



Description

This example creates a compass widget in the top right corner of the window. The widget can be used to modify the camera position via its distance and tilt sliders and its heading compass wheel.

Note for this example to work correctly VTK with version >= 9.2.20220831 is required.

Other languages

See (Python)

Question

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

Code

CompassWidget.cxx

#include <vtkAnnotatedCubeActor.h>
#include <vtkCamera.h>
#include <vtkCommand.h>
#include <vtkCompassRepresentation.h>
#include <vtkCompassWidget.h>
#include <vtkNamedColors.h>
#include <vtkNew.h>
#include <vtkProperty.h>
#include <vtkRenderWindow.h>
#include <vtkRenderWindowInteractor.h>
#include <vtkRenderer.h>

#if VTK_VERSION_NUMBER >= 90220220831ULL
#define VTK_HAS_IMPROVED_COMPASSWIDGETREPRESENTATION 1
#endif

class vtkICWValueChangedCallback : public vtkCommand
{
public:
  static vtkICWValueChangedCallback* New()
  {
    return new vtkICWValueChangedCallback();
  }
  virtual void Execute(vtkObject* caller, unsigned long vtkNotUsed(eventId),
                       void* vtkNotUsed(callData))
  {
    vtkCompassWidget* widget = vtkCompassWidget::SafeDownCast(caller);
    vtkCamera* camera = widget->GetCurrentRenderer()->GetActiveCamera();

    // calculate new camera position from compass widget parameters
    double distance = widget->GetDistance();
    double tilt = widget->GetTilt();
    double heading = widget->GetHeading();

    double pos[3] = {0, 0, 0};
    pos[0] = distance * cos(vtkMath::RadiansFromDegrees(heading)) *
        cos(vtkMath::RadiansFromDegrees(tilt));
    pos[1] = distance * sin(vtkMath::RadiansFromDegrees(heading)) *
        cos(vtkMath::RadiansFromDegrees(tilt));
    pos[2] = distance * sin(vtkMath::RadiansFromDegrees(tilt));

    camera->SetPosition(pos);
    camera->SetFocalPoint(0, 0, 0);
    camera->SetViewUp(0, 0, 1);
    camera->SetClippingRange(0.1, distance + 1);

    widget->GetCurrentRenderer()->Render();
  }
  vtkICWValueChangedCallback()
  {
  }
};

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

  // a cube with text on its faces
  vtkNew<vtkAnnotatedCubeActor> actor;
  actor->GetTextEdgesProperty()->SetColor(
      colors->GetColor3d("Black").GetData());
  actor->GetCubeProperty()->SetColor(colors->GetColor3d("PeachPuff").GetData());

  // a renderer and render window
  vtkNew<vtkRenderer> renderer;
  vtkNew<vtkRenderWindow> renderWindow;
  renderWindow->AddRenderer(renderer);

  // an interactor
  vtkNew<vtkRenderWindowInteractor> renderWindowInteractor;
  renderWindowInteractor->SetRenderWindow(renderWindow);

  // create the widget and its representation
  vtkNew<vtkCompassRepresentation> compassRepresentation;
#ifdef VTK_HAS_IMPROVED_COMPASSWIDGETREPRESENTATION
  compassRepresentation->SetMinimumDistance(2);
  compassRepresentation->SetMaximumDistance(10);
#endif

  vtkNew<vtkCompassWidget> compassWidget;
  compassWidget->SetInteractor(renderWindowInteractor);
  compassWidget->SetRepresentation(compassRepresentation);
  compassWidget->SetDistance(5.0);

  // create the callback
  vtkNew<vtkICWValueChangedCallback> valueChangedCallback;
  compassWidget->AddObserver(vtkCommand::WidgetValueChangedEvent,
                             valueChangedCallback);

  // add the actors to the scene
  renderer->AddActor(actor);
  renderer->SetBackground(colors->GetColor3d("MidnightBlue").GetData());

  renderWindow->SetSize(640, 480);
  renderWindow->SetWindowName("CompassWidget");

  renderWindow->Render();
  compassWidget->EnabledOn();

  // no interactor style - camera is moved by widget callback
  renderWindowInteractor->SetInteractorStyle(nullptr);

  // set camera to initial position
  compassWidget->InvokeEvent(vtkCommand::WidgetValueChangedEvent);

  // begin interaction
  renderWindowInteractor->Start();

  return EXIT_SUCCESS;
}

CMakeLists.txt

cmake_minimum_required(VERSION 3.12 FATAL_ERROR)

project(CompassWidget)

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

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

Download and Build CompassWidget

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

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

./CompassWidget

WINDOWS USERS

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