Skip to content

CubeAxesActor2D

vtk-examples/Cxx/Visualization/CubeAxesActor2D


Description

For more information on how to configure this actor, see this email.

Seealso

BoundingBox and Outline.

Other languages

See (Java)

Question

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

Code

CubeAxesActor2D.cxx

#include <vtkActor.h>
#include <vtkCamera.h>
#include <vtkCubeAxesActor2D.h>
#include <vtkLODActor.h>
// #include <vtkLight.h>
#include <vtkNamedColors.h>
#include <vtkNew.h>
#include <vtkOutlineFilter.h>
#include <vtkPlatonicSolidSource.h>
#include <vtkPolyDataMapper.h>
#include <vtkPolyDataNormals.h>
#include <vtkProperty.h>
#include <vtkRenderWindow.h>
#include <vtkRenderWindowInteractor.h>
#include <vtkRenderer.h>
#include <vtkTextProperty.h>

// The vtkCubeAxesActor2D draws axes on the bounding box of the data set and
// labels the axes with x-y-z coordinates.

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

  vtkNew<vtkPlatonicSolidSource> icosahedron;
  icosahedron->SetSolidTypeToIcosahedron();

  // Create a vtkPolyDataNormals filter to calculate the normals of the data
  // set.
  vtkNew<vtkPolyDataNormals> normals;
  normals->SetInputConnection(icosahedron->GetOutputPort());

  // Set up the associated mapper and actor.
  vtkNew<vtkPolyDataMapper> icosahedron_mapper;
  icosahedron_mapper->SetInputConnection(normals->GetOutputPort());
  icosahedron_mapper->ScalarVisibilityOff();

  vtkNew<vtkLODActor> icosahedron_actor;
  icosahedron_actor->SetMapper(icosahedron_mapper.GetPointer());
  icosahedron_actor->GetProperty()->SetColor(
      colors->GetColor3d("Plum").GetData());

  // Create a vtkOutlineFilter to draw the bounding box of the data set.
  // Also create the associated mapper and actor.
  vtkNew<vtkOutlineFilter> outline;
  outline->SetInputConnection(normals->GetOutputPort());

  vtkNew<vtkPolyDataMapper> map_outline;
  map_outline->SetInputConnection(outline->GetOutputPort());

  vtkNew<vtkActor> outline_actor;
  outline_actor->SetMapper(map_outline.GetPointer());
  outline_actor->GetProperty()->SetColor(colors->GetColor3d("Black").GetData());

  // Create the Renderers.  Assign them the appropriate viewport
  // coordinates, active camera, and light.
  vtkNew<vtkRenderer> ren;
  ren->SetViewport(0, 0, 0.5, 1.0);
  // It is also possible to enhance the display with cameras.
  // ren->SetActiveCamera(camera);
  // ren->AddLight(light);

  vtkNew<vtkRenderer> ren2;
  ren2->SetViewport(0.5, 0, 1.0, 1.0);
  ren2->SetActiveCamera(ren->GetActiveCamera());
  // ren2->AddLight(light);

  // Create the RenderWindow and RenderWindowInteractor.
  vtkNew<vtkRenderWindow> renWin;
  renWin->AddRenderer(ren.GetPointer());
  renWin->AddRenderer(ren2.GetPointer());
  renWin->SetWindowName("CubeAxesActor2D");
  renWin->SetSize(1200, 600);

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

  // Add the actors to the renderer, and set the background.
  ren->AddViewProp(icosahedron_actor.GetPointer());
  ren->AddViewProp(outline_actor.GetPointer());
  ren2->AddViewProp(icosahedron_actor.GetPointer());
  ren2->AddViewProp(outline_actor.GetPointer());

  ren->SetBackground(colors->GetColor3d("MidnightBlue").GetData());
  ren2->SetBackground(colors->GetColor3d("MidnightBlue").GetData());

  // Create a text property for both cube axes.
  vtkNew<vtkTextProperty> tprop;
  tprop->SetColor(colors->GetColor3d("Yellow").GetData());
  tprop->ShadowOn();
  tprop->SetFontSize(20);

  // Create a vtkCubeAxesActor2D. Use the outer edges of the bounding box to
  // draw the axes. Add the actor to the renderer.
  vtkNew<vtkCubeAxesActor2D> axes;
  axes->SetInputConnection(normals->GetOutputPort());
  axes->SetCamera(ren->GetActiveCamera());
  axes->SetLabelFormat("%6.4g");
  axes->SetFlyModeToOuterEdges();
  axes->SetAxisTitleTextProperty(tprop.GetPointer());
  axes->SetAxisLabelTextProperty(tprop.GetPointer());
  ren->AddViewProp(axes.GetPointer());

  // Create a vtkCubeAxesActor2D. Use the closest vertex to the camera to
  // determine where to draw the axes. Add the actor to the renderer.
  vtkNew<vtkCubeAxesActor2D> axes2;
  axes2->SetViewProp(icosahedron_actor.GetPointer());
  axes2->SetCamera(ren2->GetActiveCamera());
  axes2->SetLabelFormat("%6.4g");
  axes2->SetFlyModeToClosestTriad();
  axes2->ScalingOff();
  axes2->SetAxisTitleTextProperty(tprop.GetPointer());
  axes2->SetAxisLabelTextProperty(tprop.GetPointer());
  ren2->AddViewProp(axes2.GetPointer());

  ren->ResetCamera();
  iren->Initialize();
  renWin->Render();
  iren->Start();

  return EXIT_SUCCESS;
}

CMakeLists.txt

cmake_minimum_required(VERSION 3.12 FATAL_ERROR)

project(CubeAxesActor2D)

find_package(VTK COMPONENTS 
  CommonColor
  CommonCore
  FiltersCore
  FiltersModeling
  FiltersSources
  InteractionStyle
  RenderingAnnotation
  RenderingContextOpenGL2
  RenderingCore
  RenderingFreeType
  RenderingGL2PSOpenGL2
  RenderingLOD
  RenderingOpenGL2
)

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

Download and Build CubeAxesActor2D

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

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

./CubeAxesActor2D

WINDOWS USERS

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