Skip to content

AdjacencyMatrixToEdgeTable

vtk-examples/Cxx/Graphs/AdjacencyMatrixToEdgeTable

Question

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

Code

AdjacencyMatrixToEdgeTable.cxx

/* The output is:

10 20 30
40 50 60
70 80 90

+-----------------+------------------+
|                 | value            |
+-----------------+------------------+
| 2               | 30               |
| 1               | 20               |
| 0               | 10               |
| 2               | 60               |
| 1               | 50               |
| 0               | 40               |
| 2               | 90               |
| 1               | 80               |
| 0               | 70               |
+-----------------+------------------+

The first column is the column index of the item in the 'value' column.
The row index is given by the number of times we've previously seen the column
index. For some reason, zeros in the matrix are not reported in the table.

For example, the first row says that the value '30' is in column 2 of the matrix
(0-based indexing). Since we have not previously seen an item in column 2, it is
in row 0 of the matrix.

The fourth row says that the value '60' is also in column 2. We infer that '60'
is row 1 of the matrix because we have already seen one item (the '30') in
column 2.
*/

#include <vtkAdjacencyMatrixToEdgeTable.h>
#include <vtkArrayData.h>
#include <vtkArrayPrint.h>
#include <vtkDenseArray.h>
#include <vtkNew.h>
#include <vtkTable.h>

int main(int, char*[])
{
  vtkNew<vtkDenseArray<double>> array;

  array->Resize(3, 3);

  unsigned int counter{1};
  unsigned int scale{10};
  for (vtkIdType i = 0; i < array->GetExtents()[0].GetEnd(); i++)
  {
    for (vtkIdType j = 0; j < array->GetExtents()[1].GetEnd(); j++)
    {
      array->SetValue(i, j, counter * scale);
      counter++;
    }
  }

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

  vtkNew<vtkArrayData> arrayData;
  arrayData->AddArray(array);

  vtkNew<vtkAdjacencyMatrixToEdgeTable> adjacencyMatrixToEdgeTable;
  adjacencyMatrixToEdgeTable->SetInputData(arrayData);
  adjacencyMatrixToEdgeTable->Update();

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

  return EXIT_SUCCESS;
}

CMakeLists.txt

cmake_minimum_required(VERSION 3.12 FATAL_ERROR)

project(AdjacencyMatrixToEdgeTable)

find_package(VTK COMPONENTS 
  CommonCore
  CommonDataModel
  InfovisCore
)

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

Download and Build AdjacencyMatrixToEdgeTable

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

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

./AdjacencyMatrixToEdgeTable

WINDOWS USERS

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