Actor2D
vtk-examples/Cxx/Images/Actor2D
Question
If you have a question about this example, please use the VTK Discourse Forum
Code¶
Actor2D.cxx
#include <vtkActor2D.h>
#include <vtkNamedColors.h>
#include <vtkNew.h>
#include <vtkPoints.h>
#include <vtkPolyData.h>
#include <vtkPolyDataMapper2D.h>
#include <vtkProperty2D.h>
#include <vtkRenderWindow.h>
#include <vtkRenderWindowInteractor.h>
#include <vtkRenderer.h>
#include <vtkVertexGlyphFilter.h>
int main(int, char*[])
{
  vtkNew<vtkNamedColors> colors;
  vtkNew<vtkPoints> points;
  points->InsertNextPoint(10, 10, 0);
  points->InsertNextPoint(100, 100, 0);
  points->InsertNextPoint(200, 200, 0);
  vtkNew<vtkPolyData> polydata;
  polydata->SetPoints(points);
  vtkNew<vtkVertexGlyphFilter> glyphFilter;
  glyphFilter->SetInputData(polydata);
  glyphFilter->Update();
  vtkNew<vtkPolyDataMapper2D> mapper;
  mapper->SetInputConnection(glyphFilter->GetOutputPort());
  mapper->Update();
  vtkNew<vtkActor2D> actor;
  actor->SetMapper(mapper);
  actor->GetProperty()->SetColor(colors->GetColor3d("Gold").GetData());
  actor->GetProperty()->SetPointSize(8);
  // Create a renderer, render window, and interactor.
  vtkNew<vtkRenderer> renderer;
  vtkNew<vtkRenderWindow> renderWindow;
  renderWindow->AddRenderer(renderer);
  vtkNew<vtkRenderWindowInteractor> renderWindowInteractor;
  renderWindowInteractor->SetRenderWindow(renderWindow);
  // Add the actor to the scene
  renderer->AddActor(actor);
  renderer->SetBackground(colors->GetColor3d("DarkSlateGray").GetData());
  renderWindow->SetSize(300, 300);
  renderWindow->SetWindowName("Actor2D");
  // Render and interact
  renderWindow->Render();
  renderWindowInteractor->Start();
  return EXIT_SUCCESS;
}
CMakeLists.txt¶
cmake_minimum_required(VERSION 3.12 FATAL_ERROR)
project(Actor2D)
find_package(VTK COMPONENTS 
)
if (NOT VTK_FOUND)
  message(FATAL_ERROR "Actor2D: 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(Actor2D MACOSX_BUNDLE Actor2D.cxx )
  target_link_libraries(Actor2D PRIVATE ${VTK_LIBRARIES}
)
# vtk_module_autoinit is needed
vtk_module_autoinit(
  TARGETS Actor2D
  MODULES ${VTK_LIBRARIES}
)
Download and Build Actor2D¶
Click here to download Actor2D and its CMakeLists.txt file. Once the tarball Actor2D.tar has been downloaded and extracted,
cd Actor2D/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:
./Actor2D
WINDOWS USERS
Be sure to add the VTK bin directory to your path. This will resolve the VTK dll's at run time.
