CaptionWidget
Repository source: CaptionWidget
Other languages
See (PythonicAPI), (Java)
Question
If you have a question about this example, please use the VTK Discourse Forum
Code¶
CaptionWidget.cxx
#include <vtkActor.h>
#include <vtkCamera.h>
#include <vtkCaptionActor2D.h>
#include <vtkCaptionRepresentation.h>
#include <vtkCaptionWidget.h>
#include <vtkNamedColors.h>
#include <vtkNew.h>
#include <vtkPolyDataMapper.h>
#include <vtkProperty.h>
#include <vtkRenderWindow.h>
#include <vtkRenderWindowInteractor.h>
#include <vtkRenderer.h>
#include <vtkSphereSource.h>
#include <vtkTextActor.h>
#include <vtkTextProperty.h>
int main(int, char*[])
{
vtkNew<vtkNamedColors> colors;
// Sphere
vtkNew<vtkSphereSource> sphereSource;
sphereSource->Update();
vtkNew<vtkPolyDataMapper> mapper;
mapper->SetInputConnection(sphereSource->GetOutputPort());
vtkNew<vtkActor> actor;
actor->SetMapper(mapper);
actor->GetProperty()->SetColor(
colors->GetColor3d("DarkOliveGreen").GetData());
// A renderer and render window.
vtkNew<vtkRenderer> renderer;
vtkNew<vtkRenderWindow> renderWindow;
renderWindow->AddRenderer(renderer);
renderWindow->SetWindowName("CaptionWidget");
// An interactor
vtkNew<vtkRenderWindowInteractor> renderWindowInteractor;
renderWindowInteractor->SetRenderWindow(renderWindow);
// Create the widget and its representation.
vtkNew<vtkCaptionRepresentation> captionRepresentation;
captionRepresentation->GetCaptionActor2D()->SetCaption("Test caption");
captionRepresentation->GetCaptionActor2D()
->GetTextActor()
->GetTextProperty()
->SetFontSize(100);
double pos[3] = {.5, 0, 0};
captionRepresentation->SetAnchorPosition(pos);
vtkNew<vtkCaptionWidget> captionWidget;
captionWidget->SetInteractor(renderWindowInteractor);
captionWidget->SetRepresentation(captionRepresentation);
// Add the actors to the scene.
renderer->AddActor(actor);
renderer->SetBackground(colors->GetColor3d("Blue").GetData());
renderWindow->Render();
// Rotate the camera to bring the point the caption is pointing to into view.
renderer->GetActiveCamera()->Azimuth(90);
captionWidget->On();
// Begin mouse interaction.
renderWindowInteractor->Start();
return EXIT_SUCCESS;
}
CMakeLists.txt¶
cmake_minimum_required(VERSION 3.12 FATAL_ERROR)
project(CaptionWidget)
find_package(VTK COMPONENTS
CommonColor
CommonCore
CommonDataModel
FiltersSources
InteractionStyle
InteractionWidgets
RenderingAnnotation
RenderingContextOpenGL2
RenderingCore
RenderingFreeType
RenderingGL2PSOpenGL2
RenderingOpenGL2
)
if (NOT VTK_FOUND)
message(FATAL_ERROR "CaptionWidget: 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(CaptionWidget MACOSX_BUNDLE CaptionWidget.cxx )
target_link_libraries(CaptionWidget PRIVATE ${VTK_LIBRARIES}
)
# vtk_module_autoinit is needed
vtk_module_autoinit(
TARGETS CaptionWidget
MODULES ${VTK_LIBRARIES}
)
Download and Build CaptionWidget¶
Click here to download CaptionWidget and its CMakeLists.txt file. Once the tarball CaptionWidget.tar has been downloaded and extracted,
cd CaptionWidget/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:
./CaptionWidget
WINDOWS USERS
Be sure to add the VTK bin directory to your path. This will resolve the VTK dll's at run time.