Skip to content

DirectedGraphToMutableDirectedGraph

Repository source: DirectedGraphToMutableDirectedGraph

Other languages

See (PythonicAPI)

Question

If you have a question about this example, please use the VTK Discourse Forum

Code

DirectedGraphToMutableDirectedGraph.cxx

#include <vtkDirectedGraph.h>
#include <vtkMutableDirectedGraph.h>
#include <vtkNew.h>
#include <vtkPoints.h>

int main(int, char*[])
{
  // vtkDirectedGraph is a collection of vertices along with a collection of
  // directed edges (edges that have a source and target). ShallowCopy() and
  // DeepCopy() (and CheckedShallowCopy(), CheckedDeepCopy()) accept instances
  // of vtkTree and vtkMutableDirectedGraph. vtkDirectedGraph is read-only.

  // Create a graph.
  vtkNew<vtkMutableDirectedGraph> mdg;

  // Add 4 vertices to the graph.
  vtkIdType v1 = mdg->AddVertex();
  vtkIdType v2 = mdg->AddVertex();
  vtkIdType v3 = mdg->AddVertex();
  vtkIdType v4 = mdg->AddVertex();

  // Add 3 edges to the graph.
  mdg->AddEdge(v1, v2);
  mdg->AddEdge(v1, v3);
  mdg->AddEdge(v1, v4);

  // Create 4 points - one for each vertex.
  vtkNew<vtkPoints> points;
  points->InsertNextPoint(0.0, 0.0, 0.0);
  points->InsertNextPoint(1.0, 0.0, 0.0);
  points->InsertNextPoint(0.0, 1.0, 0.0);
  points->InsertNextPoint(0.0, 0.0, 2.0);

  // Add the coordinates of the points to the graph.
  mdg->SetPoints(points);

  std::cout << "MDG: " << std::endl;
  std::cout << " Type: " << mdg->GetClassName() << std::endl;
  std::cout << " Vertices: " << mdg->GetNumberOfVertices() << std::endl;
  std::cout << " Edges: " << mdg->GetNumberOfEdges() << std::endl;

  vtkNew<vtkDirectedGraph> dg;
  if (!dg->CheckedShallowCopy(mdg))
  {
    std::cerr << "Could not convert mutable directed graph to directed graph!"
              << std::endl;
    return EXIT_FAILURE;
  }

  std::cout << "DG: " << std::endl;
  std::cout << " Type: " << dg->GetClassName() << std::endl;
  std::cout << " Vertices: " << dg->GetNumberOfVertices() << std::endl;
  std::cout << " Edges: " << dg->GetNumberOfEdges() << std::endl;

  vtkNew<vtkMutableDirectedGraph> mdg2;

  if (!mdg2->CheckedShallowCopy(dg))
  {
    std::cerr << "Could not convert directed graph to mutable directed graph!"
              << std::endl;
    return EXIT_FAILURE;
  }

  std::cout << "MDG2: " << std::endl;
  std::cout << " Type: " << mdg2->GetClassName() << std::endl;
  std::cout << " Vertices: " << mdg2->GetNumberOfVertices() << std::endl;
  std::cout << " Edges: " << mdg2->GetNumberOfEdges() << std::endl;

  return EXIT_SUCCESS;
}

CMakeLists.txt

cmake_minimum_required(VERSION 3.12 FATAL_ERROR)

project(DirectedGraphToMutableDirectedGraph)

find_package(VTK COMPONENTS 
  CommonCore
  CommonDataModel
)

if (NOT VTK_FOUND)
  message(FATAL_ERROR "DirectedGraphToMutableDirectedGraph: 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(DirectedGraphToMutableDirectedGraph MACOSX_BUNDLE DirectedGraphToMutableDirectedGraph.cxx )
  target_link_libraries(DirectedGraphToMutableDirectedGraph PRIVATE ${VTK_LIBRARIES}
)
# vtk_module_autoinit is needed
vtk_module_autoinit(
  TARGETS DirectedGraphToMutableDirectedGraph
  MODULES ${VTK_LIBRARIES}
)

Download and Build DirectedGraphToMutableDirectedGraph

Click here to download DirectedGraphToMutableDirectedGraph and its CMakeLists.txt file. Once the tarball DirectedGraphToMutableDirectedGraph.tar has been downloaded and extracted,

cd DirectedGraphToMutableDirectedGraph/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:

./DirectedGraphToMutableDirectedGraph

WINDOWS USERS

Be sure to add the VTK bin directory to your path. This will resolve the VTK dll's at run time.