Skip to content

CreateTree

vtk-examples/Cxx/Graphs/CreateTree


Description

We create the tree, and label the vertices and edges.

Info

This is an update of the original example found in vtk/Examples/Infovis/Cxx/CreateTree.cxx.

Other languages

See (Python)

Question

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

Code

CreateTree.cxx

/*=========================================================================

  Program:   Visualization Toolkit
  Module:    CreateTree.cxx

  Copyright (c) Ken Martin, Will Schroeder, Bill Lorensen
  All rights reserved.
  See Copyright.txt or http://www.kitware.com/Copyright.htm for details.

     This software is distributed WITHOUT ANY WARRANTY; without even
     the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
     PURPOSE.  See the above copyright notice for more information.

=========================================================================*/
//
// This example creates a tree and labels the vertices and edges.
//

#include <vtkDataSetAttributes.h>
#include <vtkGraphLayoutView.h>
#include <vtkMutableDirectedGraph.h>
#include <vtkNew.h>
#include <vtkRenderWindow.h>
#include <vtkRenderWindowInteractor.h>
#include <vtkRenderer.h>
#include <vtkStringArray.h>
#include <vtkTree.h>
#include <vtkViewTheme.h>

int main(int, char*[])
{
  vtkNew<vtkMutableDirectedGraph> graph;
  vtkIdType a = graph->AddVertex();
  vtkIdType b = graph->AddChild(a);
  vtkIdType c = graph->AddChild(a);
  vtkIdType d = graph->AddChild(b);
  vtkIdType e = graph->AddChild(c);
  vtkIdType f = graph->AddChild(c);

  vtkNew<vtkStringArray> vertexLabels;
  vertexLabels->SetName("VertexLabel");
  vertexLabels->InsertValue(a, "a");
  vertexLabels->InsertValue(b, "b");
  vertexLabels->InsertValue(c, "c");
  vertexLabels->InsertValue(d, "d");
  vertexLabels->InsertValue(e, "e");
  vertexLabels->InsertValue(f, "f");
  graph->GetVertexData()->AddArray(vertexLabels);
  vtkNew<vtkStringArray> edgeLabels;
  edgeLabels->SetName("EdgeLabel");
  edgeLabels->InsertValue(graph->GetEdgeId(a, b), "a -> b");
  edgeLabels->InsertValue(graph->GetEdgeId(a, c), "a -> c");
  edgeLabels->InsertValue(graph->GetEdgeId(b, d), "b -> d");
  edgeLabels->InsertValue(graph->GetEdgeId(c, e), "c -> e");
  edgeLabels->InsertValue(graph->GetEdgeId(c, f), "c -> f");
  graph->GetEdgeData()->AddArray(edgeLabels);

  vtkNew<vtkTree> tree;
  bool validTree = tree->CheckedShallowCopy(graph);
  if (!validTree)
  {
    std::cout << "Invalid tree" << std::endl;
    return EXIT_FAILURE;
  }

  vtkNew<vtkGraphLayoutView> view;
  view->SetRepresentationFromInput(tree);
  // Apply a theme to the views
  vtkNew<vtkViewTheme> theme;
  view->ApplyViewTheme(theme->CreateMellowTheme());
  view->SetVertexColorArrayName("VertexDegree");
  view->SetColorVertices(true);
  view->SetVertexLabelArrayName("VertexLabel");
  view->SetVertexLabelVisibility(true);
  view->SetEdgeLabelArrayName("EdgeLabel");
  view->SetEdgeLabelVisibility(true);
  view->SetLayoutStrategyToTree();

  view->ResetCamera();
  view->GetRenderWindow()->SetSize(600, 600);
  view->GetRenderWindow()->SetWindowName("CreateTree");
  view->GetRenderWindow()->Render();
  view->GetInteractor()->Initialize();
  view->GetInteractor()->Start();

  return EXIT_SUCCESS;
}

CMakeLists.txt

cmake_minimum_required(VERSION 3.12 FATAL_ERROR)

project(CreateTree)

find_package(VTK COMPONENTS 
  CommonCore
  CommonDataModel
  InteractionStyle
  RenderingContextOpenGL2
  RenderingCore
  RenderingFreeType
  RenderingGL2PSOpenGL2
  RenderingOpenGL2
  ViewsCore
  ViewsInfovis
)

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

Download and Build CreateTree

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

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

./CreateTree

WINDOWS USERS

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