Difference between revisions of "VTK/Examples/Cxx/Broken/Graphs/TableToGraph"

From KitwarePublic
< VTK‎ | Examples‎ | Cxx
Jump to navigationJump to search
 
Line 1: Line 1:
Vertex labels are wrong. Edge labels are not displayed.
+
Vertex labels are wrong (all 2).
  
 
==TableToGraph.cxx==
 
==TableToGraph.cxx==
 
<source lang="cpp">
 
<source lang="cpp">
#include <vtkTable.h>
+
 
#include <vtkTableToGraph.h>
+
#include <vtkAdjacencyMatrixToEdgeTable.h>
#include <vtkVariant.h>
+
#include <vtkArrayData.h>
#include <vtkVariantArray.h>
+
#include <vtkArrayPrint.h>
#include <vtkSmartPointer.h>
+
#include <vtkDenseArray.h>
 
#include <vtkGraphLayoutStrategy.h>
 
#include <vtkGraphLayoutStrategy.h>
 
#include <vtkGraphLayoutView.h>
 
#include <vtkGraphLayoutView.h>
Line 14: Line 14:
 
#include <vtkRenderWindowInteractor.h>
 
#include <vtkRenderWindowInteractor.h>
 
#include <vtkSimple2DLayoutStrategy.h>
 
#include <vtkSimple2DLayoutStrategy.h>
#include <vtkAdjacencyMatrixToEdgeTable.h>
+
#include <vtkSmartPointer.h>
#include <vtkDenseArray.h>
+
#include <vtkTable.h>
#include <vtkArrayData.h>
+
#include <vtkTableToGraph.h>
#include <vtkArrayPrint.h>
+
#include <vtkVariant.h>
 +
#include <vtkVariantArray.h>
  
 
int main(int, char *[])
 
int main(int, char *[])
Line 26: Line 27:
 
   array->Resize(3,3);
 
   array->Resize(3,3);
  
 +
  // Indicate that nodes 0 and 1 are connected, 0 and 2 are connected, and 1 and 2 are connected (form a fully connected set of 3 nodes)
 +
  array->SetValue(0, 1, 10);
 +
  array->SetValue(0, 2, 15);
 +
  array->SetValue(1, 2, 25);
 +
 +
  // Indicate that nothing else is connected (no self connections, etc.)
 
   array->SetValue(0, 0, 0);
 
   array->SetValue(0, 0, 0);
 
   array->SetValue(1, 1, 0);
 
   array->SetValue(1, 1, 0);
 
   array->SetValue(2, 2, 0);
 
   array->SetValue(2, 2, 0);
   array->SetValue(0, 1, 10);
+
   array->SetValue(1, 0, 0);
   array->SetValue(0, 2, 15);
+
   array->SetValue(2, 0, 0);
   array->SetValue(1, 2, 25);
+
   array->SetValue(2, 1, 0);
  
 
   vtkPrintMatrixFormat(std::cout, array.GetPointer());
 
   vtkPrintMatrixFormat(std::cout, array.GetPointer());
  
   array->SetDimensionLabel(0, "rows");
+
  // Thes names must match the names specified to the vtkTableToGraph later.
   array->SetDimensionLabel(1, "columns");
+
   array->SetDimensionLabel(0, "row");
 
+
   array->SetDimensionLabel(1, "column");
 +
 
 
   vtkSmartPointer<vtkArrayData> arrayData =
 
   vtkSmartPointer<vtkArrayData> arrayData =
 
     vtkSmartPointer<vtkArrayData>::New();
 
     vtkSmartPointer<vtkArrayData>::New();
Line 44: Line 52:
 
   vtkSmartPointer<vtkAdjacencyMatrixToEdgeTable> adjacencyMatrixToEdgeTable =
 
   vtkSmartPointer<vtkAdjacencyMatrixToEdgeTable> adjacencyMatrixToEdgeTable =
 
     vtkSmartPointer<vtkAdjacencyMatrixToEdgeTable>::New();
 
     vtkSmartPointer<vtkAdjacencyMatrixToEdgeTable>::New();
   adjacencyMatrixToEdgeTable->SetInputConnection(arrayData->GetProducerPort());
+
   adjacencyMatrixToEdgeTable->SetInputData(arrayData);
 
   adjacencyMatrixToEdgeTable->Update();
 
   adjacencyMatrixToEdgeTable->Update();
  
 
   adjacencyMatrixToEdgeTable->GetOutput()->Dump();
 
   adjacencyMatrixToEdgeTable->GetOutput()->Dump();
 
  
 
   vtkSmartPointer<vtkTableToGraph> tableToGraph =
 
   vtkSmartPointer<vtkTableToGraph> tableToGraph =
 
     vtkSmartPointer<vtkTableToGraph>::New();
 
     vtkSmartPointer<vtkTableToGraph>::New();
 
   tableToGraph->SetInputConnection(adjacencyMatrixToEdgeTable->GetOutputPort());
 
   tableToGraph->SetInputConnection(adjacencyMatrixToEdgeTable->GetOutputPort());
   tableToGraph->AddLinkVertex("rows");
+
   tableToGraph->AddLinkVertex("row");
   tableToGraph->AddLinkVertex("columns");
+
   tableToGraph->AddLinkVertex("column");
 
+
 
   tableToGraph->AddLinkEdge("rows", "columns");
+
   tableToGraph->AddLinkEdge("row", "column"); // Add the edges to the graph
 
   tableToGraph->Update();
 
   tableToGraph->Update();
  
Line 64: Line 71:
 
   graphLayoutView->SetVertexLabelVisibility(true);
 
   graphLayoutView->SetVertexLabelVisibility(true);
 
   graphLayoutView->SetEdgeLabelVisibility(true);
 
   graphLayoutView->SetEdgeLabelVisibility(true);
   graphLayoutView->SetEdgeLabelArrayName("values");
+
   graphLayoutView->SetEdgeLabelArrayName("value");
 
   graphLayoutView->ResetCamera();
 
   graphLayoutView->ResetCamera();
 
   graphLayoutView->Render();
 
   graphLayoutView->Render();
Line 72: Line 79:
 
   return EXIT_SUCCESS;
 
   return EXIT_SUCCESS;
 
}
 
}
 +
 +
 
</source>
 
</source>
  
 
{{VTKCMakeLists|{{SUBPAGENAME}}}}
 
{{VTKCMakeLists|{{SUBPAGENAME}}}}

Latest revision as of 15:11, 12 July 2013

Vertex labels are wrong (all 2).

TableToGraph.cxx

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

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

  array->Resize(3,3);

  // Indicate that nodes 0 and 1 are connected, 0 and 2 are connected, and 1 and 2 are connected (form a fully connected set of 3 nodes)
  array->SetValue(0, 1, 10);
  array->SetValue(0, 2, 15);
  array->SetValue(1, 2, 25);

  // Indicate that nothing else is connected (no self connections, etc.)
  array->SetValue(0, 0, 0);
  array->SetValue(1, 1, 0);
  array->SetValue(2, 2, 0);
  array->SetValue(1, 0, 0);
  array->SetValue(2, 0, 0);
  array->SetValue(2, 1, 0);

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

  // Thes names must match the names specified to the vtkTableToGraph later.
  array->SetDimensionLabel(0, "row");
  array->SetDimensionLabel(1, "column");

  vtkSmartPointer<vtkArrayData> arrayData =
    vtkSmartPointer<vtkArrayData>::New();
  arrayData->AddArray(array);

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

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

  vtkSmartPointer<vtkTableToGraph> tableToGraph =
    vtkSmartPointer<vtkTableToGraph>::New();
  tableToGraph->SetInputConnection(adjacencyMatrixToEdgeTable->GetOutputPort());
  tableToGraph->AddLinkVertex("row");
  tableToGraph->AddLinkVertex("column");

  tableToGraph->AddLinkEdge("row", "column"); // Add the edges to the graph
  tableToGraph->Update();

  vtkSmartPointer<vtkGraphLayoutView> graphLayoutView =
    vtkSmartPointer<vtkGraphLayoutView>::New();
  graphLayoutView->AddRepresentationFromInput(tableToGraph->GetOutput());
  graphLayoutView->SetVertexLabelVisibility(true);
  graphLayoutView->SetEdgeLabelVisibility(true);
  graphLayoutView->SetEdgeLabelArrayName("value");
  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.