Skip to content

BillboardTextActor3D

vtk-examples/Cxx/Visualization/BillboardTextActor3D


Question

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

Code

BillboardTextActor3D.cxx

#include <vtkActor.h>
#include <vtkBillboardTextActor3D.h>
#include <vtkCallbackCommand.h>
#include <vtkMinimalStandardRandomSequence.h>
#include <vtkNamedColors.h>
#include <vtkNew.h>
#include <vtkPolyData.h>
#include <vtkPolyDataMapper.h>
#include <vtkProperty.h>
#include <vtkRenderWindow.h>
#include <vtkRenderWindowInteractor.h>
#include <vtkRenderer.h>
#include <vtkSphereSource.h>
#include <vtkTextProperty.h>

#include <sstream>

namespace {
void ActorCallback(vtkObject* caller, long unsigned int vtkNotUsed(eventId),
                   void* clientData, void* vtkNotUsed(callData))
{
  auto textActor = static_cast<vtkBillboardTextActor3D*>(clientData);
  auto actor = static_cast<vtkActor*>(caller);
  std::ostringstream label;
  label << std::setprecision(3) << actor->GetPosition()[0] << ", "
        << actor->GetPosition()[1] << ", " << actor->GetPosition()[2]
        << std::endl;
  textActor->SetPosition(actor->GetPosition());
  textActor->SetInput(label.str().c_str());
}

void RandomPosition(double p[3], double const& min_r, double const& max_r,
                    vtkMinimalStandardRandomSequence* rng);

} // namespace

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

  // For testing
  vtkNew<vtkMinimalStandardRandomSequence> rng;
  // rng->SetSeed(8775070);
  rng->SetSeed(5127);

  // Create a sphere
  vtkNew<vtkSphereSource> sphereSource;
  sphereSource->SetCenter(0.0, 0.0, 0.0);
  sphereSource->SetRadius(1.0);

  // Create an actor
  vtkNew<vtkPolyDataMapper> mapper2;
  mapper2->SetInputConnection(sphereSource->GetOutputPort());
  vtkNew<vtkActor> actor2;
  actor2->SetMapper(mapper2);
  actor2->SetPosition(0, 0, 0);
  actor2->GetProperty()->SetColor(colors->GetColor3d("Peacock").GetData());

  // Create a renderer
  vtkNew<vtkRenderer> renderer;
  renderer->SetBackground(colors->GetColor3d("DarkSlateGray").GetData());
  renderer->AddActor(actor2);

  // Create a render window
  vtkNew<vtkRenderWindow> renderWindow;
  renderWindow->AddRenderer(renderer);
  renderWindow->SetWindowName("BillboardTextActor3D");

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

  auto min_r = -10.0;
  auto max_r = 10.0;

  for (int i = 0; i < 10; ++i)
  {
    // Create a mapper
    vtkNew<vtkPolyDataMapper> mapper;
    mapper->SetInputConnection(sphereSource->GetOutputPort());

    // Create an actor
    vtkNew<vtkActor> actor;
    actor->SetMapper(mapper);
    actor->SetPosition(0, 0, 0);
    actor->GetProperty()->SetColor(colors->GetColor3d("MistyRose").GetData());

    // Setup the text and add it to the renderer
    vtkNew<vtkBillboardTextActor3D> textActor;
    textActor->SetInput("");
    textActor->SetPosition(actor->GetPosition());
    textActor->GetTextProperty()->SetFontSize(12);
    textActor->GetTextProperty()->SetColor(
        colors->GetColor3d("Gold").GetData());
    textActor->GetTextProperty()->SetJustificationToCentered();

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

    vtkNew<vtkCallbackCommand> actorCallback;
    actorCallback->SetCallback(ActorCallback);
    actorCallback->SetClientData(textActor);
    actor->AddObserver(vtkCommand::ModifiedEvent, actorCallback);
    double position[3];
    RandomPosition(position, min_r, max_r, rng);
    actor->SetPosition(position);
  }
  renderWindow->Render();
  renderWindowInteractor->Start();

  return EXIT_SUCCESS;
}

namespace {

void RandomPosition(double p[3], double const& min_r, double const& max_r,
                    vtkMinimalStandardRandomSequence* rng)
{
  for (auto i = 0; i < 3; ++i)
  {
    p[i] = rng->GetRangeValue(min_r, max_r);
    rng->Next();
  }
}

} // namespace

CMakeLists.txt

cmake_minimum_required(VERSION 3.3 FATAL_ERROR)

project(BillboardTextActor3D)

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

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

Download and Build BillboardTextActor3D

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

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

./BillboardTextActor3D

WINDOWS USERS

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