VisualizeDirectedGraph
vtk-examples/Cxx/Graphs/VisualizeDirectedGraph
Other languages
See (Python)
Question
If you have a question about this example, please use the VTK Discourse Forum
Code¶
VisualizeDirectedGraph.cxx
#include <vtkActor.h>
#include <vtkGlyph3D.h>
#include <vtkGlyphSource2D.h>
#include <vtkGraphLayout.h>
#include <vtkGraphLayoutView.h>
#include <vtkGraphToPolyData.h>
#include <vtkMutableDirectedGraph.h>
#include <vtkNamedColors.h>
#include <vtkNew.h>
#include <vtkPolyDataMapper.h>
#include <vtkRenderWindow.h>
#include <vtkRenderWindowInteractor.h>
#include <vtkRenderer.h>
#include <vtkSimple2DLayoutStrategy.h>
int main(int, char*[])
{
vtkNew<vtkNamedColors> colors;
vtkNew<vtkMutableDirectedGraph> g;
vtkIdType v1 = g->AddVertex();
vtkIdType v2 = g->AddVertex();
vtkIdType v3 = g->AddVertex();
g->AddEdge(v1, v2);
g->AddEdge(v2, v3);
g->AddEdge(v3, v1);
// Do layout manually before handing graph to the view.
// This allows us to know the positions of edge arrows.
vtkNew<vtkGraphLayoutView> graphLayoutView;
vtkNew<vtkGraphLayout> layout;
vtkNew<vtkSimple2DLayoutStrategy> strategy;
layout->SetInputData(g);
layout->SetLayoutStrategy(strategy);
// Tell the view to use the vertex layout we provide
graphLayoutView->SetLayoutStrategyToPassThrough();
// The arrows will be positioned on a straight line between two
// vertices so tell the view not to draw arcs for parallel edges
graphLayoutView->SetEdgeLayoutStrategyToPassThrough();
// Add the graph to the view. This will render vertices and edges,
// but not edge arrows.
graphLayoutView->AddRepresentationFromInputConnection(
layout->GetOutputPort());
// Manually create an actor containing the glyphed arrows.
vtkNew<vtkGraphToPolyData> graphToPoly;
graphToPoly->SetInputConnection(layout->GetOutputPort());
graphToPoly->EdgeGlyphOutputOn();
// Set the position (0: edge start, 1: edge end) where
// the edge arrows should go.
graphToPoly->SetEdgeGlyphPosition(0.98);
// Make a simple edge arrow for glyphing.
vtkNew<vtkGlyphSource2D> arrowSource;
arrowSource->SetGlyphTypeToEdgeArrow();
arrowSource->SetScale(0.1);
arrowSource->Update();
// Use Glyph3D to repeat the glyph on all edges.
vtkNew<vtkGlyph3D> arrowGlyph;
arrowGlyph->SetInputConnection(0, graphToPoly->GetOutputPort(1));
arrowGlyph->SetInputConnection(1, arrowSource->GetOutputPort());
// Add the edge arrow actor to the view.
vtkNew<vtkPolyDataMapper> arrowMapper;
arrowMapper->SetInputConnection(arrowGlyph->GetOutputPort());
vtkNew<vtkActor> arrowActor;
arrowActor->SetMapper(arrowMapper);
graphLayoutView->GetRenderer()->AddActor(arrowActor);
graphLayoutView->GetRenderer()->SetBackground(
colors->GetColor3d("SaddleBrown").GetData());
graphLayoutView->GetRenderer()->SetBackground2(
colors->GetColor3d("Wheat").GetData());
graphLayoutView->GetRenderWindow()->SetWindowName("VisualizeDirectedGraph");
graphLayoutView->ResetCamera();
graphLayoutView->Render();
graphLayoutView->GetInteractor()->Start();
return EXIT_SUCCESS;
}
CMakeLists.txt¶
cmake_minimum_required(VERSION 3.12 FATAL_ERROR)
project(VisualizeDirectedGraph)
find_package(VTK COMPONENTS
)
if (NOT VTK_FOUND)
message(FATAL_ERROR "VisualizeDirectedGraph: 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(VisualizeDirectedGraph MACOSX_BUNDLE VisualizeDirectedGraph.cxx )
target_link_libraries(VisualizeDirectedGraph PRIVATE ${VTK_LIBRARIES}
)
# vtk_module_autoinit is needed
vtk_module_autoinit(
TARGETS VisualizeDirectedGraph
MODULES ${VTK_LIBRARIES}
)
Download and Build VisualizeDirectedGraph¶
Click here to download VisualizeDirectedGraph and its CMakeLists.txt file. Once the tarball VisualizeDirectedGraph.tar has been downloaded and extracted,
cd VisualizeDirectedGraph/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:
./VisualizeDirectedGraph
WINDOWS USERS
Be sure to add the VTK bin directory to your path. This will resolve the VTK dll's at run time.