VTK/Examples/Cxx/Broken/Graphs/ExtractSelectedGraph

From KitwarePublic
< VTK‎ | Examples‎ | Cxx
Jump to navigationJump to search

Fix: Need to add dual GraphView when it is available.

ExtractSelectedGraph.cxx

#include <vtkSmartPointer.h>
#include <vtkDataSetAttributes.h>
#include <vtkDoubleArray.h>
#include <vtkExtractSelectedGraph.h>
#include <vtkIdTypeArray.h>
#include <vtkMutableUndirectedGraph.h>
#include <vtkSelection.h>
#include <vtkSelectionNode.h>
#include <vtkDataSetAttributes.h>
#include <vtkDoubleArray.h>
#include <vtkGraphLayoutView.h>
#include <vtkIntArray.h>
#include <vtkMutableUndirectedGraph.h>
#include <vtkRenderWindowInteractor.h>
#include <vtkStringArray.h>

int main(int, char *[])
{
  vtkSmartPointer<vtkMutableUndirectedGraph> g = 
    vtkSmartPointer<vtkMutableUndirectedGraph>::New();
 
  //create 3 vertices
  vtkIdType v1 = g->AddVertex();
  vtkIdType v2 = g->AddVertex();
  vtkIdType v3 = g->AddVertex();
 
  //add edges
  g->AddEdge(v1, v2);
  g->AddEdge(v2, v3);
  g->AddEdge(v1, v3);
   
  //create the vertex label array
  vtkSmartPointer<vtkStringArray> vertexLabels =
    vtkSmartPointer<vtkStringArray>::New();
  vertexLabels->SetNumberOfComponents(1);
  vertexLabels->SetName("VertexLabels");
  vertexLabels->InsertNextValue("Label0");
  vertexLabels->InsertNextValue("Label1");
  vertexLabels->InsertNextValue("Label2");
  g->GetVertexData()->AddArray(vertexLabels);
  
  std::cout << "Number of vertices: "
            << g->GetNumberOfVertices() << std::endl;
  std::cout << "Number of edges: "
            << g->GetNumberOfEdges() << std::endl;
 
  vtkSmartPointer<vtkIdTypeArray> ids = 
    vtkSmartPointer<vtkIdTypeArray>::New();
  ids->InsertNextValue(0);
  ids->InsertNextValue(1);
 
  vtkSmartPointer<vtkSelection> selection =
    vtkSmartPointer<vtkSelection>::New();
 
  vtkSmartPointer<vtkSelectionNode> node =
    vtkSmartPointer<vtkSelectionNode>::New();
  selection->AddNode(node);
  node->SetSelectionList(ids);
  node->SetContentType(vtkSelectionNode::INDICES);
  node->SetFieldType(vtkSelectionNode::VERTEX);
 
  vtkSmartPointer<vtkExtractSelectedGraph> extractSelectedGraph = 
    vtkSmartPointer<vtkExtractSelectedGraph>::New();
  extractSelectedGraph->SetInput(0, g);
  extractSelectedGraph->SetInput(1, selection);
  extractSelectedGraph->Update();
 
  std::cout << "Number of vertices: "
            << extractSelectedGraph->GetOutput()->GetNumberOfVertices()
            << std::endl;
  std::cout << "Number of edges: "
            << extractSelectedGraph->GetOutput()->GetNumberOfEdges()
            << std::endl;
 
  vtkSmartPointer<vtkGraphLayoutView> graphLayoutView = 
    vtkSmartPointer<vtkGraphLayoutView>::New();
  graphLayoutView->AddRepresentationFromInput(g);
 
  graphLayoutView->SetVertexLabelVisibility(true);
  graphLayoutView->SetEdgeLabelVisibility(true);
  graphLayoutView->SetVertexLabelArrayName("VertexLabels");
  graphLayoutView->ResetCamera();
  graphLayoutView->Render();
  graphLayoutView->GetInteractor()->Start();
  
  graphLayoutView->AddRepresentationFromInput(extractSelectedGraph->GetOutput());
  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(ExtractSelectedGraph)

find_package(VTK REQUIRED)
include(${VTK_USE_FILE})

add_executable(ExtractSelectedGraph MACOSX_BUNDLE ExtractSelectedGraph.cxx)

if(VTK_LIBRARIES)
  target_link_libraries(ExtractSelectedGraph ${VTK_LIBRARIES})
else()
  target_link_libraries(ExtractSelectedGraph vtkHybrid vtkWidgets)
endif()

Download and Build ExtractSelectedGraph

Click here to download ExtractSelectedGraph. and its CMakeLists.txt file.

Once the tarball ExtractSelectedGraph.tar has been downloaded and extracted,

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

./ExtractSelectedGraph

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.