Skip to content

AxisActor

vtk-examples/Cxx/Visualization/AxisActor


Description

This example illustrates the use of vtkAxisActor. This is a fairly complicated object that allows extensive control over a single axis. The parameters may be tricky to setup. vtkAxisActor is usually used onsode other classes,e.g. vtkCubeAxisActor.

Question

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

Code

AxisActor.cxx

#include <vtkActor.h>
#include <vtkAxisActor.h>
#include <vtkCamera.h>
#include <vtkNamedColors.h>
#include <vtkNew.h>
#include <vtkPolyDataMapper.h>
#include <vtkProperty.h>
#include <vtkRenderWindow.h>
#include <vtkRenderWindowInteractor.h>
#include <vtkRenderer.h>
#include <vtkSphereSource.h>
#include <vtkStringArray.h>
#include <vtkTextProperty.h>

#include <iostream>
#include <string>

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

  vtkNew<vtkSphereSource> source;
  source->SetPhiResolution(31);
  source->SetThetaResolution(31);
  source->Update();
  double bounds[6];
  source->GetOutput()->GetBounds(bounds);
  std::cout << "Bounds: " << bounds[0] << ", " << bounds[2] << ", " << bounds[3]
            << ", " << bounds[1] << ", " << bounds[3] << ", " << bounds[5]
            << std::endl;
  double center[3];
  source->GetOutput()->GetCenter(center);
  std::cout << "Center: " << center[0] << ", " << center[1] << ", " << center[2]
            << std::endl;

  // Create the axis actor.
  vtkNew<vtkAxisActor> axis;
  axis->SetPoint1(-1.1, 0.0, 0.0);
  axis->SetPoint2(1.1, 0.0, 0.0);
  axis->SetTickLocationToBoth();
  axis->SetAxisTypeToX();
  axis->SetTitle("A Sphere");
  axis->SetTitleScale(0.2);
  axis->TitleVisibilityOn();

  axis->SetMajorTickSize(0.05);
  axis->SetMinorTickSize(1);
  axis->DrawGridlinesOff();
  axis->GetTitleTextProperty()->SetColor(
      colors->GetColor3d("banana").GetData());
  axis->GetLabelTextProperty()->SetColor(
      colors->GetColor3d("orange").GetData());

  vtkNew<vtkStringArray> labels;
  labels->SetNumberOfTuples(1);
  labels->SetValue(0, "x Axis");

  axis->SetLabels(labels);
  axis->SetLabelScale(.1);
  axis->MinorTicksVisibleOn();
  axis->SetDeltaMajor(0, .1);
  axis->SetCalculateTitleOffset(0);
  axis->SetCalculateLabelOffset(0);

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

  vtkNew<vtkActor> actor;
  actor->SetMapper(mapper);
  actor->GetProperty()->SetDiffuseColor(colors->GetColor4d("Tomato").GetData());

  // Create the RenderWindow, Renderer and both Actors.
  vtkNew<vtkRenderer> renderer;
  vtkNew<vtkRenderWindow> renderWindow;
  renderWindow->AddRenderer(renderer);
  renderWindow->SetWindowName("AxisActor");

  vtkNew<vtkRenderWindowInteractor> interactor;
  interactor->SetRenderWindow(renderWindow);

  axis->SetCamera(renderer->GetActiveCamera());

  renderer->AddActor(actor);
  renderer->AddActor(axis);

  renderer->SetBackground(colors->GetColor4d("SlateGray").GetData());
  renderWindow->SetSize(640, 480);
  renderer->ResetCamera();
  renderer->ResetCameraClippingRange();

  // Render the image.
  renderWindow->Render();

  interactor->Initialize();
  interactor->Start();

  return EXIT_SUCCESS;
}

CMakeLists.txt

cmake_minimum_required(VERSION 3.12 FATAL_ERROR)

project(AxisActor)

find_package(VTK COMPONENTS 
)

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

Download and Build AxisActor

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

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

./AxisActor

WINDOWS USERS

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