Skip to content

AnnotatedCubeActor

vtk-examples/Cxx/Visualization/AnnotatedCubeActor


Other languages

See (Java)

Question

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

Code

AnnotatedCubeActor.cxx

#include <vtkAnnotatedCubeActor.h>
#include <vtkCamera.h>
#include <vtkNamedColors.h>
#include <vtkNew.h>
#include <vtkProperty.h>
#include <vtkRenderWindow.h>
#include <vtkRenderWindowInteractor.h>
#include <vtkRenderer.h>
#include <vtkSmartPointer.h>

int main(int, char*[])
{
  vtkNew<vtkAnnotatedCubeActor> cube;
  cube->SetFaceTextScale(0.666667);

  // set up the renderer, window, and interactor
  //
  vtkNew<vtkNamedColors> colors;

  vtkNew<vtkRenderer> renderer;
  renderer->SetBackground(colors->GetColor3d("Wheat").GetData());

  vtkNew<vtkRenderWindow> renderWindow;
  renderWindow->AddRenderer(renderer);
  renderWindow->SetSize(640, 480);
  renderWindow->SetWindowName("AnnotatedCubeActor");

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

  // anatomic labelling
  //
  cube->SetXPlusFaceText("A");
  cube->SetXMinusFaceText("P");
  cube->SetYPlusFaceText("L");
  cube->SetYMinusFaceText("R");
  cube->SetZPlusFaceText("S");
  cube->SetZMinusFaceText("I");

  // change the vector text colors
  //
  cube->GetTextEdgesProperty()->SetColor(colors->GetColor3d("Black").GetData());
  cube->GetTextEdgesProperty()->SetLineWidth(4);

  vtkProperty* property;
  property = cube->GetXPlusFaceProperty();
  property->SetColor(colors->GetColor3d("Turquoise").GetData());
  property = cube->GetXMinusFaceProperty();
  property->SetColor(colors->GetColor3d("Turquoise").GetData());
  property = cube->GetYPlusFaceProperty();
  property->SetColor(colors->GetColor3d("Mint").GetData());
  property = cube->GetYMinusFaceProperty();
  property->SetColor(colors->GetColor3d("Mint").GetData());
  property = cube->GetZPlusFaceProperty();
  property->SetColor(colors->GetColor3d("Tomato").GetData());
  property = cube->GetZMinusFaceProperty();
  property->SetColor(colors->GetColor3d("Tomato").GetData());

  renderer->AddActor(cube);

  // set up an interesting view
  //
  vtkCamera* camera = renderer->GetActiveCamera();
  camera->SetViewUp(0, 0, 1);
  camera->SetFocalPoint(0, 0, 0);
  camera->SetPosition(4.5, 4.5, 2.5);
  renderer->ResetCamera();
  camera->Dolly(1.3);
  renderer->ResetCameraClippingRange();

  renderWindow->Render();
  interactor->Start();

  return EXIT_SUCCESS;
}

CMakeLists.txt

cmake_minimum_required(VERSION 3.3 FATAL_ERROR)

project(AnnotatedCubeActor)

find_package(VTK COMPONENTS 
  vtkCommonColor
  vtkCommonCore
  vtkInteractionStyle
  vtkRenderingAnnotation
  vtkRenderingContextOpenGL2
  vtkRenderingCore
  vtkRenderingFreeType
  vtkRenderingGL2PSOpenGL2
  vtkRenderingOpenGL2
  QUIET
)

if (NOT VTK_FOUND)
  message("Skipping AnnotatedCubeActor: ${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(AnnotatedCubeActor MACOSX_BUNDLE AnnotatedCubeActor.cxx )
  target_link_libraries(AnnotatedCubeActor PRIVATE ${VTK_LIBRARIES})
else ()
  # include all components
  add_executable(AnnotatedCubeActor MACOSX_BUNDLE AnnotatedCubeActor.cxx )
  target_link_libraries(AnnotatedCubeActor PRIVATE ${VTK_LIBRARIES})
  # vtk_module_autoinit is needed
  vtk_module_autoinit(
    TARGETS AnnotatedCubeActor
    MODULES ${VTK_LIBRARIES}
    )
endif ()

Download and Build AnnotatedCubeActor

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

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

./AnnotatedCubeActor

WINDOWS USERS

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