GraphAlgorithmSource
VTKEx/Cxx/Developers/GraphAlgorithmSource
Description¶
This example produces a vtkGraph as output.
Question
If you have a simple question about this example contact us at VTKExProject If your question is more complex and may require extended discussion, please use the VTK Discourse Forum
Code¶
GraphAlgorithmSource.cxx
#include <vtkSmartPointer.h>
#include <vtkGraph.h>
#include <vtkMutableUndirectedGraph.h>
#include "vtkTestGraphAlgorithmSource.h"
int main (int, char *[])
{
vtkSmartPointer<vtkTestGraphAlgorithmSource> source =
vtkSmartPointer<vtkTestGraphAlgorithmSource>::New();
source->Update();
vtkGraph* outputGraph = source->GetOutput();
std::cout << "Output number of vertices: "
<< outputGraph->GetNumberOfVertices() << std::endl;
return EXIT_SUCCESS;
}
vtkTestGraphAlgorithmSource.h
#ifndef __vtkTestGraphAlgorithmSource_h
#define __vtkTestGraphAlgorithmSource_h
#include <vtkGraphAlgorithm.h>
class vtkTestGraphAlgorithmSource : public vtkGraphAlgorithm
{
public:
static vtkTestGraphAlgorithmSource *New();
vtkTypeMacro(vtkTestGraphAlgorithmSource,vtkGraphAlgorithm);
void PrintSelf(ostream& os, vtkIndent indent) override;
protected:
vtkTestGraphAlgorithmSource();
~vtkTestGraphAlgorithmSource();
int RequestData(vtkInformation *, vtkInformationVector **, vtkInformationVector *) override;
int RequestDataObject(vtkInformation *, vtkInformationVector **, vtkInformationVector *) override;
private:
vtkTestGraphAlgorithmSource(const vtkTestGraphAlgorithmSource&); // Not implemented.
void operator=(const vtkTestGraphAlgorithmSource&); // Not implemented.
};
#endif
vtkTestGraphAlgorithmSource.cxx
#include "vtkTestGraphAlgorithmSource.h"
#include <vtkObjectFactory.h>
#include <vtkStreamingDemandDrivenPipeline.h>
#include <vtkInformationVector.h>
#include <vtkInformation.h>
#include <vtkDataObject.h>
#include <vtkSmartPointer.h>
#include <vtkMutableUndirectedGraph.h>
#include <vtkUndirectedGraph.h>
#include <vtkGraph.h>
vtkStandardNewMacro(vtkTestGraphAlgorithmSource);
vtkTestGraphAlgorithmSource::vtkTestGraphAlgorithmSource()
{
this->SetNumberOfInputPorts(0);
this->SetNumberOfOutputPorts(1);
}
vtkTestGraphAlgorithmSource::~vtkTestGraphAlgorithmSource()
{
}
int vtkTestGraphAlgorithmSource::RequestData(
vtkInformation *vtkNotUsed(request),
vtkInformationVector **vtkNotUsed(inputVector),
vtkInformationVector *outputVector)
{
vtkInformation *outInfo = outputVector->GetInformationObject(0);
vtkGraph *output = dynamic_cast<vtkGraph*>(
outInfo->Get(vtkDataObject::DATA_OBJECT()));
vtkSmartPointer<vtkMutableUndirectedGraph> NewGraph =
vtkSmartPointer<vtkMutableUndirectedGraph>::New();
//add 3 vertices
NewGraph->AddVertex();
NewGraph->AddVertex();
NewGraph->AddVertex();
output->ShallowCopy(NewGraph);
return 1;
}
int vtkTestGraphAlgorithmSource::RequestDataObject(
vtkInformation*,
vtkInformationVector**,
vtkInformationVector* )
{
vtkSmartPointer<vtkUndirectedGraph> output =
vtkSmartPointer<vtkUndirectedGraph>::New();
this->GetExecutive()->SetOutputData(0, output);
return 1;
}
//----------------------------------------------------------------------------
void vtkTestGraphAlgorithmSource::PrintSelf(ostream& os, vtkIndent indent)
{
this->Superclass::PrintSelf(os,indent);
}
CMakeLists.txt¶
cmake_minimum_required(VERSION 3.3 FATAL_ERROR)
project(GraphAlgorithmSource)
find_package(VTK COMPONENTS
vtkvtkCommonCore
vtkvtkCommonDataModel QUIET)
if (NOT VTK_FOUND)
message("Skipping GraphAlgorithmSource: ${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(GraphAlgorithmSource MACOSX_BUNDLE GraphAlgorithmSource.cxx )
target_link_libraries(GraphAlgorithmSource PRIVATE ${VTK_LIBRARIES})
else ()
# include all components
add_executable(GraphAlgorithmSource MACOSX_BUNDLE GraphAlgorithmSource.cxx )
target_link_libraries(GraphAlgorithmSource PRIVATE ${VTK_LIBRARIES})
# vtk_module_autoinit is needed
vtk_module_autoinit(
TARGETS GraphAlgorithmSource
MODULES ${VTK_LIBRARIES}
)
endif ()
Download and Build GraphAlgorithmSource¶
Click here to download GraphAlgorithmSource and its CMakeLists.txt file. Once the tarball GraphAlgorithmSource.tar has been downloaded and extracted,
cd GraphAlgorithmSource/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:
./GraphAlgorithmSource
WINDOWS USERS
Be sure to add the VTK bin directory to your path. This will resolve the VTK dll's at run time.