ColorVertexLabels
VTKExamples/Cxx/Graphs/ColorVertexLabels
Other Languages
See (Python)
Code¶
ColorVertexLabels.cxx
#include <vtkSmartPointer.h> #include <vtkIntArray.h> #include <vtkDataSetAttributes.h> #include <vtkGraphLayoutView.h> #include <vtkMutableDirectedGraph.h> #include <vtkRenderWindowInteractor.h> #include <vtkRenderWindow.h> #include <vtkRenderedGraphRepresentation.h> #include <vtkTextProperty.h> int main(int, char *[]) { // Create a graph vtkSmartPointer<vtkMutableDirectedGraph> graph = vtkSmartPointer<vtkMutableDirectedGraph>::New(); vtkIdType v1 = graph->AddVertex(); vtkIdType v2 = graph->AddVertex(); graph->AddEdge(v1,v2); // Create an array for the vertex labels vtkSmartPointer<vtkIntArray> vertexIDs = vtkSmartPointer<vtkIntArray>::New(); 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); vtkSmartPointer<vtkGraphLayoutView> graphLayoutView = vtkSmartPointer<vtkGraphLayoutView>::New(); graphLayoutView->AddRepresentationFromInput(graph); graphLayoutView->SetVertexLabelVisibility(true); vtkRenderedGraphRepresentation::SafeDownCast(graphLayoutView->GetRepresentation()) ->GetVertexLabelTextProperty()->SetColor(1,0,0); graphLayoutView->SetLayoutStrategyToSimple2D(); graphLayoutView->SetVertexLabelArrayName("VertexIDs"); graphLayoutView->ResetCamera(); graphLayoutView->Render(); graphLayoutView->GetInteractor()->Start(); return EXIT_SUCCESS; }
CMakeLists.txt¶
cmake_minimum_required(VERSION 3.3 FATAL_ERROR) project(ColorVertexLabels) find_package(VTK COMPONENTS vtkCommonCore vtkCommonDataModel vtkInteractionStyle vtkRenderingCore vtkRenderingFreeType vtkRenderingOpenGL2 vtkViewsInfovis QUIET) if (NOT VTK_FOUND) message("Skipping ColorVertexLabels: ${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(ColorVertexLabels MACOSX_BUNDLE ColorVertexLabels.cxx ) target_link_libraries(ColorVertexLabels PRIVATE ${VTK_LIBRARIES}) else () # include all components 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} ) endif ()
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.