Skip to content

VertexConnectivity

Repository source: VertexConnectivity


Description

The input must be only edges (i.e. vtkExtractEdges must be run).

Other languages

See (PythonicAPI)

Question

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

Code

VertexConnectivity.cxx

#include <vtkActor.h>
#include <vtkDataSetMapper.h>
#include <vtkExtractEdges.h>
#include <vtkExtractSelection.h>
#include <vtkIdList.h>
#include <vtkIdTypeArray.h>
#include <vtkNamedColors.h>
#include <vtkNew.h>
#include <vtkPolyData.h>
#include <vtkProperty.h>
#include <vtkRenderWindow.h>
#include <vtkRenderWindowInteractor.h>
#include <vtkRenderer.h>
#include <vtkSelection.h>
#include <vtkSelectionNode.h>
#include <vtkSmartPointer.h>
#include <vtkSphereSource.h>
#include <vtkTriangleFilter.h>
#include <vtkVertexGlyphFilter.h>

#include <iostream>
#include <string>

// Note that:
// vtkExtractEdges moved from vtkFiltersExtraction to vtkFiltersCore in
// VTK commit d9981b9aeb93b42d1371c6e295d76bfdc18430bd

namespace {

vtkSmartPointer<vtkIdList> GetConnectedVertices(vtkPolyData* mesh, int id);

}

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

  vtkNew<vtkSphereSource> sphereSource;
  sphereSource->Update();

  vtkNew<vtkTriangleFilter> triangleFilter;
  triangleFilter->SetInputConnection(sphereSource->GetOutputPort());
  triangleFilter->Update();

  vtkNew<vtkExtractEdges> extractEdges;
  extractEdges->SetInputConnection(triangleFilter->GetOutputPort());
  extractEdges->Update();

  vtkSmartPointer<vtkPolyData> mesh = extractEdges->GetOutput();

  vtkSmartPointer<vtkIdList> connectedVertices = GetConnectedVertices(mesh, 0);

  vtkNew<vtkIdTypeArray> ids;
  ids->SetNumberOfComponents(1);

  std::cout << "Connected vertices: ";
  for (vtkIdType i = 0; i < connectedVertices->GetNumberOfIds(); i++)
  {
    std::cout << connectedVertices->GetId(i) << " ";
    ids->InsertNextValue(connectedVertices->GetId(i));
  }
  std::cout << std::endl;

  vtkNew<vtkDataSetMapper> connectedVertexMapper;

  {
    vtkNew<vtkSelectionNode> selectionNode;
    selectionNode->SetFieldType(vtkSelectionNode::POINT);
    selectionNode->SetContentType(vtkSelectionNode::INDICES);
    selectionNode->SetSelectionList(ids);

    vtkNew<vtkSelection> selection;
    selection->AddNode(selectionNode);

    vtkNew<vtkExtractSelection> extractSelection;

    extractSelection->SetInputConnection(0, extractEdges->GetOutputPort());
    extractSelection->SetInputData(1, selection);
    extractSelection->Update();

    vtkNew<vtkVertexGlyphFilter> glyphFilter;
    glyphFilter->SetInputConnection(extractSelection->GetOutputPort());
    glyphFilter->Update();

    connectedVertexMapper->SetInputConnection(glyphFilter->GetOutputPort());
  }

  vtkNew<vtkActor> connectedVertexActor;
  connectedVertexActor->SetMapper(connectedVertexMapper);
  connectedVertexActor->GetProperty()->SetColor(
      colors->GetColor3d("Red").GetData());
  connectedVertexActor->GetProperty()->SetPointSize(5);

  vtkNew<vtkDataSetMapper> queryVertexMapper;

  {
    vtkNew<vtkIdTypeArray> ids2;
    ids2->SetNumberOfComponents(1);
    ids2->InsertNextValue(0);

    vtkNew<vtkSelectionNode> selectionNode;
    selectionNode->SetFieldType(vtkSelectionNode::POINT);
    selectionNode->SetContentType(vtkSelectionNode::INDICES);
    selectionNode->SetSelectionList(ids2);

    vtkNew<vtkSelection> selection;
    selection->AddNode(selectionNode);

    vtkNew<vtkExtractSelection> extractSelection;

    extractSelection->SetInputConnection(0, extractEdges->GetOutputPort());
    extractSelection->SetInputData(1, selection);
    extractSelection->Update();

    vtkNew<vtkVertexGlyphFilter> glyphFilter;
    glyphFilter->SetInputConnection(extractSelection->GetOutputPort());
    glyphFilter->Update();

    queryVertexMapper->SetInputConnection(glyphFilter->GetOutputPort());
  }

  vtkNew<vtkActor> queryVertexActor;
  queryVertexActor->SetMapper(queryVertexMapper);
  queryVertexActor->GetProperty()->SetColor(
      colors->GetColor3d("Lime").GetData());
  queryVertexActor->GetProperty()->SetPointSize(5);

  vtkNew<vtkDataSetMapper> sphereMapper;
  sphereMapper->SetInputConnection(extractEdges->GetOutputPort());
  vtkNew<vtkActor> sphereActor;
  sphereActor->SetMapper(sphereMapper);
  sphereActor->GetProperty()->SetColor(colors->GetColor3d("Snow").GetData());

  // Create a renderer, render window, and interactor
  vtkNew<vtkRenderer> renderer;
  vtkNew<vtkRenderWindow> renderWindow;
  renderWindow->AddRenderer(renderer);
  renderWindow->SetWindowName("VertexConnectivity");

  vtkNew<vtkRenderWindowInteractor> renderWindowInteractor;
  renderWindowInteractor->SetRenderWindow(renderWindow);

  // Add the actors to the scene
  renderer->AddActor(sphereActor);
  renderer->AddActor(queryVertexActor);
  renderer->AddActor(connectedVertexActor);
  renderer->SetBackground(colors->GetColor3d("DarkSlateGray").GetData());

  // Render and interact
  renderWindow->Render();
  renderWindowInteractor->Start();

  return EXIT_SUCCESS;
}

namespace {

vtkSmartPointer<vtkIdList> GetConnectedVertices(vtkPolyData* mesh, int id)
{
  vtkNew<vtkIdList> connectedVertices;

  // Get all cells that vertex 'id' is a part of.
  vtkNew<vtkIdList> cellIdList;
  mesh->GetPointCells(id, cellIdList);

  /*
  cout << "Vertex 0 is used in cells ";
  for(vtkIdType i = 0; i < cellIdList->GetNumberOfIds(); i++)
    {
    cout << cellIdList->GetId(i) << ", ";
    }
  cout << endl;
  */

  for (vtkIdType i = 0; i < cellIdList->GetNumberOfIds(); i++)
  {
    // cout << "id " << i << " : " << cellIdList->GetId(i) << endl;

    vtkNew<vtkIdList> pointIdList;
    mesh->GetCellPoints(cellIdList->GetId(i), pointIdList);

    // cout << "End points are " << pointIdList->GetId(0) << " and " <<
    // pointIdList->GetId(1) << endl;

    if (pointIdList->GetId(0) != id)
    {
      // cout << "Connected to " << pointIdList->GetId(0) << endl;
      connectedVertices->InsertNextId(pointIdList->GetId(0));
    }
    else
    {
      // cout << "Connected to " << pointIdList->GetId(1) << endl;
      connectedVertices->InsertNextId(pointIdList->GetId(1));
    }
  }

  return connectedVertices;
}

} // namespace

CMakeLists.txt

cmake_minimum_required(VERSION 3.12 FATAL_ERROR)

project(VertexConnectivity)

find_package(VTK COMPONENTS 
  CommonColor
  CommonCore
  CommonDataModel
  FiltersCore
  FiltersExtraction
  FiltersGeneral
  FiltersSources
  InteractionStyle
  RenderingContextOpenGL2
  RenderingCore
  RenderingFreeType
  RenderingGL2PSOpenGL2
  RenderingOpenGL2
)

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

Download and Build VertexConnectivity

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

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

./VertexConnectivity

WINDOWS USERS

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