VTK/Examples/Cxx/Broken/Graphs/TableToGraph
From KitwarePublic
Jump to navigationJump to searchVertex labels are wrong (all 2).
Contents
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.