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](https://www.vtk.org/doc/nightly/html/classvtk/Examples.html#details)/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 <vtkRandomGraphSource.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.3 FATAL_ERROR)

project(CreateTree)

find_package(VTK COMPONENTS 
  vtkCommonCore
  vtkCommonDataModel
  vtkInfovisCore
  vtkInteractionStyle
  vtkRenderingContextOpenGL2
  vtkRenderingCore
  vtkRenderingFreeType
  vtkRenderingGL2PSOpenGL2
  vtkRenderingOpenGL2
  vtkViewsCore
  vtkViewsInfovis
  QUIET
)

if (NOT VTK_FOUND)
  message("Skipping CreateTree: ${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(CreateTree MACOSX_BUNDLE CreateTree.cxx )
  target_link_libraries(CreateTree PRIVATE ${VTK_LIBRARIES})
else ()
  # include all components
  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}
    )
endif ()

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.