ColorVertexLabels
vtk-examples/Cxx/Graphs/ColorVertexLabels
Other languages
See (Python)
Question
If you have a question about this example, please use the VTK Discourse Forum
Code¶
ColorVertexLabels.cxx
#include <vtkCamera.h>
#include <vtkDataSetAttributes.h>
#include <vtkGraphLayoutView.h>
#include <vtkIntArray.h>
#include <vtkMutableDirectedGraph.h>
#include <vtkNamedColors.h>
#include <vtkNew.h>
#include <vtkRenderWindow.h>
#include <vtkRenderWindowInteractor.h>
#include <vtkRenderedGraphRepresentation.h>
#include <vtkRenderer.h>
#include <vtkTextProperty.h>
int main(int, char*[])
{
vtkNew<vtkNamedColors> colors;
// Create a graph.
vtkNew<vtkMutableDirectedGraph> graph;
vtkIdType v1 = graph->AddVertex();
vtkIdType v2 = graph->AddVertex();
graph->AddEdge(v1, v2);
// Create an array for the vertex labels.
vtkNew<vtkIntArray> vertexIDs;
vertexIDs->SetNumberOfComponents(1);
vertexIDs->SetName("VertexIDs");
// Set the vertex labels
vertexIDs->InsertNextValue(0);
vertexIDs->InsertNextValue(1);
// Add the array to the graph.
graph->GetVertexData()->AddArray(vertexIDs);
vtkNew<vtkGraphLayoutView> graphLayoutView;
graphLayoutView->AddRepresentationFromInput(graph);
graphLayoutView->SetVertexLabelVisibility(true);
dynamic_cast<vtkRenderedGraphRepresentation*>(
graphLayoutView->GetRepresentation())
->GetVertexLabelTextProperty()
->SetColor(colors->GetColor3d("Red").GetData());
graphLayoutView->SetLayoutStrategyToSimple2D();
graphLayoutView->SetVertexLabelArrayName("VertexIDs");
graphLayoutView->ResetCamera();
graphLayoutView->GetRenderer()->GetActiveCamera()->Zoom(0.8);
graphLayoutView->GetRenderWindow()->SetWindowName("ColorVertexLabels");
graphLayoutView->Render();
graphLayoutView->GetInteractor()->Start();
return EXIT_SUCCESS;
}
CMakeLists.txt¶
cmake_minimum_required(VERSION 3.12 FATAL_ERROR)
project(ColorVertexLabels)
find_package(VTK COMPONENTS
)
if (NOT VTK_FOUND)
message(FATAL_ERROR "ColorVertexLabels: 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(ColorVertexLabels MACOSX_BUNDLE ColorVertexLabels.cxx )
target_link_libraries(ColorVertexLabels PRIVATE ${VTK_LIBRARIES}
)
# vtk_module_autoinit is needed
vtk_module_autoinit(
TARGETS ColorVertexLabels
MODULES ${VTK_LIBRARIES}
)
Download and Build ColorVertexLabels¶
Click here to download ColorVertexLabels and its CMakeLists.txt file. Once the tarball ColorVertexLabels.tar has been downloaded and extracted,
cd ColorVertexLabels/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:
./ColorVertexLabels
WINDOWS USERS
Be sure to add the VTK bin directory to your path. This will resolve the VTK dll's at run time.