AxisActor

VTKExamples/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 simple question about this example contact us at VTKExamplesProject If your question is more complex and may require extended discussion, please use the VTK Discourse Forum

Code

AxisActor.cxx

#include "vtkSmartPointer.h"
#include "vtkAxisActor.h"

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

//----------------------------------------------------------------------------
int main( int, char *[] )
{
  vtkSmartPointer<vtkNamedColors> colors =
    vtkSmartPointer<vtkNamedColors>::New();

  vtkSmartPointer<vtkSphereSource> source =
    vtkSmartPointer<vtkSphereSource>::New();
  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()->GetBounds(center);
  std::cout << "center: "
            << center[0] << ", " << center[1] << ", " << center[2] << std::endl;

  // Create the axis actor
  vtkSmartPointer<vtkAxisActor> axis =
    vtkSmartPointer<vtkAxisActor>::New();
  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());

  vtkSmartPointer<vtkStringArray> labels =
    vtkSmartPointer<vtkStringArray>::New();
  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);

  vtkSmartPointer<vtkPolyDataMapper> mapper =
    vtkSmartPointer<vtkPolyDataMapper>::New();
  mapper->SetInputConnection(source->GetOutputPort());

 vtkSmartPointer<vtkActor> actor =
    vtkSmartPointer<vtkActor>::New();
 actor->SetMapper(mapper);
 actor->GetProperty()->SetDiffuseColor(colors->GetColor4d("tomato").GetData());

  // Create the RenderWindow, Renderer and both Actors
  vtkSmartPointer<vtkRenderer> renderer =
    vtkSmartPointer<vtkRenderer>::New();
  vtkSmartPointer<vtkRenderWindow> renderWindow =
    vtkSmartPointer<vtkRenderWindow>::New();
  renderWindow->AddRenderer(renderer);
  vtkSmartPointer<vtkRenderWindowInteractor> interactor =
    vtkSmartPointer<vtkRenderWindowInteractor>::New();
  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->Start();

  return EXIT_SUCCESS;
}

CMakeLists.txt

cmake_minimum_required(VERSION 3.3 FATAL_ERROR)

project(AxisActor)

find_package(VTK COMPONENTS 
  vtkCommonColor
  vtkCommonCore
  vtkFiltersSources
  vtkInteractionStyle
  vtkRenderingAnnotation
  vtkRenderingCore
  vtkRenderingFreeType
  vtkRenderingOpenGL2 QUIET)
if (NOT VTK_FOUND)
  message("Skipping AxisActor: ${VTK_NOT_FOUND_MESSAGE}")
  return ()
endif()
message (STATUS "VTK_VERSION: ${VTK_VERSION}")
if (VTK_VERSION VERSION_LESS "8.90.0")
  # old system
  include(${VTK_USE_FILE})
  add_executable(AxisActor MACOSX_BUNDLE AxisActor.cxx )
  target_link_libraries(AxisActor PRIVATE ${VTK_LIBRARIES})
else ()
  # include all components
  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}
    )
endif () 

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.