VTK/Examples/Cxx/Broken/Graphs/TableToGraph

From KitwarePublic
< VTK‎ | Examples‎ | Cxx
Revision as of 00:24, 10 December 2012 by Lorensen (talk | contribs)
Jump to navigationJump to search

Vertex labels are wrong. Edge labels are not displayed.

TableToGraph.cxx

#include <vtkTable.h>
#include <vtkTableToGraph.h>
#include <vtkVariant.h>
#include <vtkVariantArray.h>
#include <vtkSmartPointer.h>
#include <vtkGraphLayoutStrategy.h>
#include <vtkGraphLayoutView.h>
#include <vtkGraphWriter.h>
#include <vtkMutableUndirectedGraph.h>
#include <vtkRenderWindowInteractor.h>
#include <vtkSimple2DLayoutStrategy.h>
#include <vtkAdjacencyMatrixToEdgeTable.h>
#include <vtkDenseArray.h>
#include <vtkArrayData.h>
#include <vtkArrayPrint.h>

int main(int, char *[])
{
   vtkSmartPointer<vtkDenseArray<double> > array =
    vtkSmartPointer<vtkDenseArray<double> >::New();

  array->Resize(3,3);

  array->SetValue(0, 0, 0);
  array->SetValue(1, 1, 0);
  array->SetValue(2, 2, 0);
  array->SetValue(0, 1, 10);
  array->SetValue(0, 2, 15);
  array->SetValue(1, 2, 25);

  vtkPrintMatrixFormat(std::cout, array.GetPointer());

  array->SetDimensionLabel(0, "rows");
  array->SetDimensionLabel(1, "columns");
  
  vtkSmartPointer<vtkArrayData> arrayData =
    vtkSmartPointer<vtkArrayData>::New();
  arrayData->AddArray(array);

  vtkSmartPointer<vtkAdjacencyMatrixToEdgeTable> adjacencyMatrixToEdgeTable =
    vtkSmartPointer<vtkAdjacencyMatrixToEdgeTable>::New();
  adjacencyMatrixToEdgeTable->SetInputConnection(arrayData->GetProducerPort());
  adjacencyMatrixToEdgeTable->Update();

  adjacencyMatrixToEdgeTable->GetOutput()->Dump();


  vtkSmartPointer<vtkTableToGraph> tableToGraph =
    vtkSmartPointer<vtkTableToGraph>::New();
  tableToGraph->SetInputConnection(adjacencyMatrixToEdgeTable->GetOutputPort());
  tableToGraph->AddLinkVertex("rows");
  tableToGraph->AddLinkVertex("columns");
  
  tableToGraph->AddLinkEdge("rows", "columns");
  tableToGraph->Update();

  vtkSmartPointer<vtkGraphLayoutView> graphLayoutView =
    vtkSmartPointer<vtkGraphLayoutView>::New();
  graphLayoutView->AddRepresentationFromInput(tableToGraph->GetOutput());
  graphLayoutView->SetVertexLabelVisibility(true);
  graphLayoutView->SetEdgeLabelVisibility(true);
  graphLayoutView->SetEdgeLabelArrayName("values");
  graphLayoutView->ResetCamera();
  graphLayoutView->Render();

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

  return EXIT_SUCCESS;
}

Please try the new VTKExamples website.

CMakeLists.txt

cmake_minimum_required(VERSION 2.8)

PROJECT(TableToGraph)

find_package(VTK REQUIRED)
include(${VTK_USE_FILE})

add_executable(TableToGraph MACOSX_BUNDLE TableToGraph.cxx)

if(VTK_LIBRARIES)
  target_link_libraries(TableToGraph ${VTK_LIBRARIES})
else()
  target_link_libraries(TableToGraph vtkHybrid vtkWidgets)
endif()

Download and Build TableToGraph

Click here to download TableToGraph. and its CMakeLists.txt file.

Once the tarball TableToGraph.tar has been downloaded and extracted,

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

./TableToGraph

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