Difference between revisions of "VTK/Examples/Cxx/Developers/GraphAlgorithmSource"

From KitwarePublic
< VTK‎ | Examples‎ | Cxx
Jump to navigationJump to search
Line 133: Line 133:
 
</source>
 
</source>
  
{{VTKCMakeLists|{{SUBPAGENAME}}}}
+
==CMakeLists.txt==
 +
<source lang="cmake">
 +
cmake_minimum_required(VERSION 2.6)
 +
project(GraphAlgorithmSource)
 +
 
 +
find_package(VTK REQUIRED)
 +
include(${VTK_USE_FILE})
 +
 
 +
add_executable(GraphAlgorithmSource GraphAlgorithmSource.cxx vtkTestSource.cxx)
 +
 
 +
if(VTK_LIBRARIES)
 +
  target_link_libraries(GraphAlgorithmSource ${VTK_LIBRARIES})
 +
else()
 +
  target_link_libraries(GraphAlgorithmSource vtkHybrid)
 +
endif()
  
 
</source>
 
</source>

Revision as of 16:40, 28 April 2014

This example produces a vtkGraph as output.

GraphAlgorithmSource.cxx

#include <vtkSmartPointer.h>
#include <vtkGraph.h>
#include <vtkMutableUndirectedGraph.h>

#include "vtkTestSource.h"

int main (int, char *[])
{
  vtkSmartPointer<vtkTestSource> source =
    vtkSmartPointer<vtkTestSource>::New();
  source->Update();
  
  vtkGraph* outputGraph = source->GetOutput();
  
  std::cout << "Output number of vertices: "
            << outputGraph->GetNumberOfVertices() << std::endl;
  
  return EXIT_SUCCESS;
}

vtkTestSource.h

#ifndef __vtkTestSource_h
#define __vtkTestSource_h

#include "vtkGraphAlgorithm.h"

class vtkTestSource : public vtkGraphAlgorithm 
{
public:
  vtkTypeRevisionMacro(vtkTestSource,vtkGraphAlgorithm);
  void PrintSelf(ostream& os, vtkIndent indent);

  static vtkTestSource *New();
	
protected:
  vtkTestSource();
  ~vtkTestSource();
  
  int RequestData(vtkInformation *, vtkInformationVector **, vtkInformationVector *);
  
  int RequestDataObject(vtkInformation *, vtkInformationVector **, vtkInformationVector *);

private:
  vtkTestSource(const vtkTestSource&);  // Not implemented.
  void operator=(const vtkTestSource&);  // Not implemented.

};

#endif

vtkTestSource.cxx

#include "vtkTestSource.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"

vtkCxxRevisionMacro(vtkTestSource, "$Revision: 1.70 $");
vtkStandardNewMacro(vtkTestSource);

vtkTestSource::vtkTestSource()
{
  this->SetNumberOfInputPorts(0);
  this->SetNumberOfOutputPorts(1);
}

vtkTestSource::~vtkTestSource()
{

}

int vtkTestSource::RequestData(
  vtkInformation *vtkNotUsed(request),
  vtkInformationVector **vtkNotUsed(inputVector),
  vtkInformationVector *outputVector)
{
  
  vtkInformation *outInfo = outputVector->GetInformationObject(0);
  
  vtkGraph *output = vtkGraph::SafeDownCast(
    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 vtkTestSource::RequestDataObject(
  vtkInformation*, 
  vtkInformationVector**, 
  vtkInformationVector* )
{

  vtkUndirectedGraph *output = 0;
  output = vtkUndirectedGraph::New();
  
  
  this->GetExecutive()->SetOutputData(0, output);
  output->Delete();

  return 1;
}

//----------------------------------------------------------------------------
void vtkTestSource::PrintSelf(ostream& os, vtkIndent indent)
{
  this->Superclass::PrintSelf(os,indent);
}

CMakeLists.txt

cmake_minimum_required(VERSION 2.6)
project(GraphAlgorithmSource)

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

add_executable(GraphAlgorithmSource GraphAlgorithmSource.cxx vtkTestSource.cxx)

if(VTK_LIBRARIES)
  target_link_libraries(GraphAlgorithmSource ${VTK_LIBRARIES})
else()
  target_link_libraries(GraphAlgorithmSource vtkHybrid)
endif()