Skip to content

XGMLReader

web-test/Cxx/InfoVis/XGMLReader



Description

This example reads and displays the graph from a gml file.

Other languages

See (CSharp)

Question

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

Code

XGMLReader.cxx

#include <vtkGraphLayoutStrategy.h>
#include <vtkGraphLayoutView.h>
#include <vtkNamedColors.h>
#include <vtkNew.h>
#include <vtkRenderWindow.h>
#include <vtkRenderWindowInteractor.h>
#include <vtkRenderer.h>
#include <vtkSimple2DLayoutStrategy.h>
#include <vtkUndirectedGraph.h>
#include <vtkViewTheme.h>
#include <vtkXGMLReader.h>

int main(int argc, char* argv[])
{
  vtkNew<vtkNamedColors> colors;

  if (argc != 2)
  {
    std::cout << "Required parameters: Filename e.g. fsm.gml" << std::endl;
    return EXIT_FAILURE;
  }

  std::string inputFilename = argv[1];

  vtkNew<vtkXGMLReader> reader;
  reader->SetFileName(inputFilename.c_str());
  reader->Update();

  vtkUndirectedGraph* g = reader->GetOutput();

  vtkNew<vtkViewTheme> theme;
  theme->SetLineWidth(1);
  theme->SetPointSize(5);
  theme->SetCellOpacity(0.99);
  theme->SetOutlineColor(colors->GetColor3d("Gray").GetData());
  // Vertices
  theme->SetPointColor(colors->GetColor3d("Chartreuse").GetData());
  theme->SetSelectedPointColor(colors->GetColor3d("Magenta").GetData());
  theme->SetPointHueRange(1.0, 1.0);
  theme->SetPointSaturationRange(1.0, 1.0);
  theme->SetPointValueRange(0.0, 1.0);
  // theme->SetPointAlphaRange(0.2, 0.8);
  // Edges
  theme->SetCellColor(colors->GetColor3d("Honeydew").GetData());
  theme->SetSelectedCellColor(colors->GetColor3d("Cyan").GetData());
  theme->SetCellHueRange(0.1, 0.1);
  theme->SetCellSaturationRange(0.2, 1.0);
  theme->SetCellValueRange(0.5, 1.0);
  // theme->SetCellAlphaRange(0.2, 0.8);

  vtkNew<vtkSimple2DLayoutStrategy> simple2D;

  vtkNew<vtkGraphLayoutView> graphLayoutView;
  graphLayoutView->AddRepresentationFromInput(g);
  graphLayoutView->ApplyViewTheme(theme);
  // If we create a layout object directly, just set the pointer to this method.
  // graphLayoutView->SetLayoutStrategy(simple2D);
  graphLayoutView->SetLayoutStrategyToSimple2D();

  graphLayoutView->ResetCamera();

  graphLayoutView->GetRenderer()->GradientBackgroundOn();
  graphLayoutView->GetRenderer()->SetBackground2(
      colors->GetColor3d("DarkSlateGray").GetData());
  graphLayoutView->GetRenderer()->SetBackground(
      colors->GetColor3d("Black").GetData());

  graphLayoutView->GetRenderWindow()->SetSize(600, 600);
  graphLayoutView->GetRenderWindow()->SetWindowName("XGMLReader");

  graphLayoutView->Render();

  graphLayoutView->GetInteractor()->Start();

  return EXIT_SUCCESS;
}

CMakeLists.txt

cmake_minimum_required(VERSION 3.12 FATAL_ERROR)

project(XGMLReader)

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

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

Download and Build XGMLReader

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

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

./XGMLReader

WINDOWS USERS

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