GraphAlgorithmFilter
VTKExamples/Cxx/Developers/GraphAlgorithmFilter
Description¶
This example demonstrates how to create a filter that takes a vtkGraph as input and produces a vtkGraph as output.
Question
If you have a simple question about this example contact us at VTKExamplesProject If your question is more complex and may require extended discussion, please use the VTK Discourse Forum
Code¶
GraphAlgorithmFilter.cxx
#include <vtkSmartPointer.h> #include <vtkGraph.h> #include <vtkMutableUndirectedGraph.h> #include <vtkMutableDirectedGraph.h> #include "vtkTestGraphAlgorithmFilter.h" void TestDirected(); void TestUndirected(); int main(int, char *[]) { TestDirected(); TestUndirected(); return EXIT_SUCCESS; } void TestDirected() { vtkSmartPointer<vtkMutableDirectedGraph> g = vtkSmartPointer<vtkMutableDirectedGraph>::New(); vtkIdType v1 = g->AddVertex(); vtkIdType v2 = g->AddVertex(); g->AddEdge(v1, v2); std::cout << "Input type: " << g->GetClassName() << std::endl; vtkSmartPointer<vtkTestGraphAlgorithmFilter> filter = vtkSmartPointer<vtkTestGraphAlgorithmFilter>::New(); filter->SetInputData(g); filter->Update(); std::cout << "Output type: " << filter->GetOutput()->GetClassName() << std::endl; std::cout << "Output has " << filter->GetOutput()->GetNumberOfVertices() << " vertices." << std::endl; std::cout << std::endl; } void TestUndirected() { std::cout << "TestUndirected" << std::endl; vtkSmartPointer<vtkMutableUndirectedGraph> g = vtkSmartPointer<vtkMutableUndirectedGraph>::New(); vtkIdType v1 = g->AddVertex(); vtkIdType v2 = g->AddVertex(); g->AddEdge(v1, v2); std::cout << "Input type: " << g->GetClassName() << std::endl; vtkSmartPointer<vtkTestGraphAlgorithmFilter> filter = vtkSmartPointer<vtkTestGraphAlgorithmFilter>::New(); filter->SetInputData(g); filter->Update(); std::cout << "Output type: " << filter->GetOutput()->GetClassName() << std::endl; std::cout << "Output has " << filter->GetOutput()->GetNumberOfVertices() << " vertices." << std::endl; }
vtkTestGraphAlgorithmFilter.h
#ifndef __vtkTestGraphAlgorithmFilter_h #define __vtkTestGraphAlgorithmFilter_h #include <vtkGraphAlgorithm.h> class vtkTestGraphAlgorithmFilter : public vtkGraphAlgorithm { public: vtkTypeMacro(vtkTestGraphAlgorithmFilter,vtkGraphAlgorithm); static vtkTestGraphAlgorithmFilter *New(); protected: vtkTestGraphAlgorithmFilter(){} ~vtkTestGraphAlgorithmFilter(){} int RequestData(vtkInformation *, vtkInformationVector **, vtkInformationVector *) override; private: vtkTestGraphAlgorithmFilter(const vtkTestGraphAlgorithmFilter&); // Not implemented. void operator=(const vtkTestGraphAlgorithmFilter&); // Not implemented. }; #endif
vtkTestGraphAlgorithmFilter.cxx
#include "vtkTestGraphAlgorithmFilter.h" #include <vtkObjectFactory.h> #include <vtkStreamingDemandDrivenPipeline.h> #include <vtkInformationVector.h> #include <vtkInformation.h> #include <vtkDataObject.h> #include <vtkSmartPointer.h> #include <vtkMutableUndirectedGraph.h> #include <vtkMutableDirectedGraph.h> #include <vtkMutableGraphHelper.h> vtkStandardNewMacro(vtkTestGraphAlgorithmFilter); int vtkTestGraphAlgorithmFilter::RequestData(vtkInformation *vtkNotUsed(request), vtkInformationVector **inputVector, vtkInformationVector *outputVector) { // get the info objects vtkInformation *inInfo = inputVector[0]->GetInformationObject(0); vtkInformation *outInfo = outputVector->GetInformationObject(0); // get the input and ouptut vtkGraph *input = dynamic_cast<vtkGraph*>( inInfo->Get(vtkDataObject::DATA_OBJECT())); vtkGraph *output = dynamic_cast<vtkGraph*>( outInfo->Get(vtkDataObject::DATA_OBJECT())); vtkSmartPointer<vtkMutableDirectedGraph> mdg = vtkSmartPointer<vtkMutableDirectedGraph>::New(); vtkSmartPointer<vtkMutableUndirectedGraph> mug = vtkSmartPointer<vtkMutableUndirectedGraph>::New(); if(input->IsA("vtkMutableUndirectedGraph")) { vtkSmartPointer<vtkMutableUndirectedGraph> ug = vtkSmartPointer<vtkMutableUndirectedGraph>::New(); ug->AddVertex(); output->ShallowCopy(ug); } else if(input->IsA("vtkMutableDirectedGraph")) { vtkSmartPointer<vtkMutableDirectedGraph> dg = vtkSmartPointer<vtkMutableDirectedGraph>::New(); dg->AddVertex(); output->ShallowCopy(dg); } std::cout << "Output is type: " << output->GetClassName() << std::endl; return 1; }
CMakeLists.txt¶
cmake_minimum_required(VERSION 3.3 FATAL_ERROR) project(GraphAlgorithmFilter) find_package(VTK COMPONENTS vtkCommonCore vtkCommonDataModel QUIET) if (NOT VTK_FOUND) message("Skipping GraphAlgorithmFilter: ${VTK_NOT_FOUND_MESSAGE}") return () endif() message (STATUS "VTK_VERSION: ${VTK_VERSION}") if (VTK_VERSION VERSION_LESS "8.90.0") # old system include(${VTK_USE_FILE}) add_executable(GraphAlgorithmFilter MACOSX_BUNDLE GraphAlgorithmFilter.cxx ) target_link_libraries(GraphAlgorithmFilter PRIVATE ${VTK_LIBRARIES}) else () # include all components add_executable(GraphAlgorithmFilter MACOSX_BUNDLE GraphAlgorithmFilter.cxx ) target_link_libraries(GraphAlgorithmFilter PRIVATE ${VTK_LIBRARIES}) # vtk_module_autoinit is needed vtk_module_autoinit( TARGETS GraphAlgorithmFilter MODULES ${VTK_LIBRARIES} ) endif ()
Download and Build GraphAlgorithmFilter¶
Click here to download GraphAlgorithmFilter and its CMakeLists.txt file. Once the tarball GraphAlgorithmFilter.tar has been downloaded and extracted,
cd GraphAlgorithmFilter/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:
./GraphAlgorithmFilter
WINDOWS USERS
Be sure to add the VTK bin directory to your path. This will resolve the VTK dll's at run time.