LabeledDataMapper
Repository source: LabeledDataMapper
Description¶
This example demonstrates how to display the point ID at each point.
Question
If you have a question about this example, please use the VTK Discourse Forum
Code¶
LabeledDataMapper.cxx
#include <vtkActor.h>
#include <vtkActor2D.h>
#include <vtkGlyph3DMapper.h>
#include <vtkLabeledDataMapper.h>
#include <vtkNamedColors.h>
#include <vtkNew.h>
#include <vtkPointSource.h>
#include <vtkPoints.h>
#include <vtkPolyData.h>
#include <vtkPolyDataMapper.h>
#include <vtkProperty.h>
#include <vtkRenderWindow.h>
#include <vtkRenderWindowInteractor.h>
#include <vtkRenderer.h>
#include <vtkSmartPointer.h>
#include <vtkSphereSource.h>
#include <vtkTextProperty.h>
namespace {
/**
* Convert points to glyphs.
*
* @param points - The points to glyph.
* @param scale - The scale, used to determine the size of the glyph.
* representing the point, expressed as a fraction of the largest side of the
* bounding box surrounding the points. e.g. 0.05.
*
* @return The actor.
*/
vtkSmartPointer<vtkActor> PointToGlyph(vtkPoints* points, double const& scale);
} // namespace
int main(int, char*[])
{
vtkNew<vtkNamedColors> colors;
// Create a point set
vtkNew<vtkPointSource> pointSource;
pointSource->SetNumberOfPoints(10);
pointSource->Update();
// Create a mapper and actor.
vtkNew<vtkPolyDataMapper> pointMapper;
pointMapper->SetInputConnection(pointSource->GetOutputPort());
vtkNew<vtkActor> pointActor;
pointActor->SetMapper(pointMapper);
pointActor->GetProperty()->SetPointSize(1);
pointActor->GetProperty()->SetColor(colors->GetColor3d("Black").GetData());
// Map the points to spheres.
auto sphereActor = PointToGlyph(pointSource->GetOutput()->GetPoints(), 0.02);
sphereActor->GetProperty()->SetColor(colors->GetColor3d("Gold").GetData());
vtkNew<vtkLabeledDataMapper> labelMapper;
labelMapper->SetInputConnection(pointSource->GetOutputPort());
labelMapper->GetLabelTextProperty()->SetColor(
colors->GetColor3d("Magenta").GetData());
vtkNew<vtkActor2D> labelActor;
labelActor->SetMapper(labelMapper);
// Create a renderer, render window, and interactor.
vtkNew<vtkRenderer> renderer;
vtkNew<vtkRenderWindow> renderWindow;
renderWindow->AddRenderer(renderer);
renderWindow->SetWindowName("LabeledDataMapper");
vtkNew<vtkRenderWindowInteractor> renderWindowInteractor;
renderWindowInteractor->SetRenderWindow(renderWindow);
// Add the actor to the scene.
// renderer->AddActor(pointActor);
renderer->AddActor(sphereActor);
renderer->AddActor(labelActor);
renderer->SetBackground(colors->GetColor3d("DarkSlateGray").GetData());
// Render and interact
renderWindow->Render();
renderWindowInteractor->Start();
return EXIT_SUCCESS;
}
namespace {
vtkSmartPointer<vtkActor> PointToGlyph(vtkPoints* points, double const& scale)
{
auto bounds = points->GetBounds();
double maxLen = 0;
for (int i = 1; i < 3; ++i)
{
maxLen = std::max(bounds[i + 1] - bounds[i], maxLen);
}
vtkNew<vtkSphereSource> sphereSource;
sphereSource->SetRadius(scale * maxLen);
vtkNew<vtkPolyData> pd;
pd->SetPoints(points);
vtkNew<vtkGlyph3DMapper> mapper;
mapper->SetInputData(pd);
mapper->SetSourceConnection(sphereSource->GetOutputPort());
mapper->ScalarVisibilityOff();
mapper->ScalingOff();
vtkNew<vtkActor> actor;
actor->SetMapper(mapper);
return actor;
}
} // namespace
CMakeLists.txt¶
cmake_minimum_required(VERSION 3.12 FATAL_ERROR)
project(LabeledDataMapper)
find_package(VTK COMPONENTS
CommonColor
CommonCore
CommonDataModel
FiltersSources
InteractionStyle
RenderingContextOpenGL2
RenderingCore
RenderingFreeType
RenderingGL2PSOpenGL2
RenderingLabel
RenderingOpenGL2
)
if (NOT VTK_FOUND)
message(FATAL_ERROR "LabeledDataMapper: 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(LabeledDataMapper MACOSX_BUNDLE LabeledDataMapper.cxx )
target_link_libraries(LabeledDataMapper PRIVATE ${VTK_LIBRARIES}
)
# vtk_module_autoinit is needed
vtk_module_autoinit(
TARGETS LabeledDataMapper
MODULES ${VTK_LIBRARIES}
)
Download and Build LabeledDataMapper¶
Click here to download LabeledDataMapper and its CMakeLists.txt file. Once the tarball LabeledDataMapper.tar has been downloaded and extracted,
cd LabeledDataMapper/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:
./LabeledDataMapper
WINDOWS USERS
Be sure to add the VTK bin directory to your path. This will resolve the VTK dll's at run time.