Skip to content

SelectedVerticesAndEdgesObserver

Repository source: SelectedVerticesAndEdgesObserver


Description

  • Thanks to Eric Monson

Other languages

See (PythonicAPI)

Question

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

Code

SelectedVerticesAndEdgesObserver.cxx

#include <vtkAnnotationLink.h>
#include <vtkCallbackCommand.h>
#include <vtkGraphLayoutView.h>
#include <vtkIdTypeArray.h>
#include <vtkNamedColors.h>
#include <vtkNew.h>
#include <vtkRandomGraphSource.h>
#include <vtkRenderWindow.h>
#include <vtkRenderWindowInteractor.h>
#include <vtkRenderedGraphRepresentation.h>
#include <vtkRenderer.h>
#include <vtkSelection.h>
#include <vtkSelectionNode.h>

namespace {

void SelectionCallbackFunction(vtkObject* caller, long unsigned int eventId,
                               void* clientData, void* callData);
}

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

  vtkNew<vtkRandomGraphSource> source;

  vtkNew<vtkGraphLayoutView> view;
  view->AddRepresentationFromInputConnection(source->GetOutputPort());

  view->GetRenderWindow()->SetSize(600, 600);
  view->GetRenderWindow()->SetWindowName("SelectedVerticesAndEdges");
  view->GetRenderer()->SetBackground(
      colors->GetColor3d("MidnightBlue").GetData());
  view->GetRenderer()->SetBackground2(
      colors->GetColor3d("RoyalBlue").GetData());

  vtkNew<vtkCallbackCommand> selectionCallback;
  selectionCallback->SetCallback(SelectionCallbackFunction);
  // The vtkRenderedGraphRepresentation should already have a vtkAnnotationLink,
  // so we just want to grab it and add an observer with our callback function
  // attached.
  view->GetRepresentation()->GetAnnotationLink()->AddObserver(
      "AnnotationChangedEvent", selectionCallback);

  view->ResetCamera();
  view->Render();

  view->GetInteractor()->Start();

  return EXIT_SUCCESS;
}

namespace {

void SelectionCallbackFunction(vtkObject* caller, long unsigned int eventId,
                               void* clientData, void* callData)
{

  vtkAnnotationLink* annotationLink = static_cast<vtkAnnotationLink*>(caller);

  vtkSelection* selection = annotationLink->GetCurrentSelection();
  vtkSelectionNode* vertices = nullptr;
  vtkSelectionNode* edges = nullptr;
  if (selection->GetNode(0)->GetFieldType() == vtkSelectionNode::VERTEX)
  {
    vertices = selection->GetNode(0);
  }
  else if (selection->GetNode(0)->GetFieldType() == vtkSelectionNode::EDGE)
  {
    edges = selection->GetNode(0);
  }

  if (selection->GetNode(1)->GetFieldType() == vtkSelectionNode::VERTEX)
  {
    vertices = selection->GetNode(1);
  }
  else if (selection->GetNode(1)->GetFieldType() == vtkSelectionNode::EDGE)
  {
    edges = selection->GetNode(1);
  }

  vtkIdTypeArray* vertexList =
      dynamic_cast<vtkIdTypeArray*>(vertices->GetSelectionList());
  std::cout << "There are " << vertexList->GetNumberOfTuples()
            << " vertices selected." << std::endl;
  auto hasVertices = vertexList->GetNumberOfTuples() > 0;
  if (hasVertices)
  {
    std::cout << "Vertex Ids: ";
    for (vtkIdType i = 0; i < vertexList->GetNumberOfTuples(); i++)
    {
      if (i < vertexList->GetNumberOfTuples() - 1)
      {
        std::cout << vertexList->GetValue(i) << ", ";
      }
      else
      {
        std::cout << vertexList->GetValue(i) << std::endl;
      }
    }
  }

  vtkIdTypeArray* edgeList =
      dynamic_cast<vtkIdTypeArray*>(edges->GetSelectionList());
  std::cout << "There are " << edgeList->GetNumberOfTuples()
            << " edges selected." << std::endl;
  auto hasEdges = edgeList->GetNumberOfTuples() > 0;
  if (hasEdges)
  {
    std::cout << "Edge Ids: ";

    for (vtkIdType i = 0; i < edgeList->GetNumberOfTuples(); i++)
    {
      if (i < edgeList->GetNumberOfTuples() - 1)
      {
        std::cout << edgeList->GetValue(i) << ", ";
      }
      else
      {
        std::cout << edgeList->GetValue(i) << std::endl;
      }
    }
  }

  if (hasVertices || hasEdges)
  {
    std::cout << "- - -" << std::endl;
  }
  else
  {
    std::cout << std::endl;
  }
}

} // namespace

CMakeLists.txt

cmake_minimum_required(VERSION 3.12 FATAL_ERROR)

project(SelectedVerticesAndEdgesObserver)

find_package(VTK COMPONENTS 
  CommonColor
  CommonCore
  CommonDataModel
  FiltersGeneral
  InteractionStyle
  RenderingContextOpenGL2
  RenderingCore
  RenderingFreeType
  RenderingGL2PSOpenGL2
  RenderingOpenGL2
  ViewsInfovis
)

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

Download and Build SelectedVerticesAndEdgesObserver

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

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

./SelectedVerticesAndEdgesObserver

WINDOWS USERS

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