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

From KitwarePublic
< VTK‎ | Examples‎ | Cxx
Jump to navigationJump to search
Line 7: Line 7:
 
#include <vtkMutableUndirectedGraph.h>
 
#include <vtkMutableUndirectedGraph.h>
  
#include "vtkTestSource.h"
+
#include "vtkTest GraphAlgorithmSource.h"
  
 
int main (int, char *[])
 
int main (int, char *[])
 
{
 
{
   vtkSmartPointer<vtkTestSource> source =
+
   vtkSmartPointer<vtkTest GraphAlgorithmSource> source =
     vtkSmartPointer<vtkTestSource>::New();
+
     vtkSmartPointer<vtkTest GraphAlgorithmSource>::New();
 
   source->Update();
 
   source->Update();
 
    
 
    
Line 24: Line 24:
 
</source>
 
</source>
  
==vtkTestSource.h==
+
==vtkTestGraphAlgorithmSource.h==
 
<source lang="cpp">
 
<source lang="cpp">
  
#ifndef __vtkTestSource_h
+
#ifndef __vtkTestGraphAlgorithmSource_h
#define __vtkTestSource_h
+
#define __vtkTestGraphAlgorithmSource_h
  
 
#include "vtkGraphAlgorithm.h"
 
#include "vtkGraphAlgorithm.h"
  
class vtkTestSource : public vtkGraphAlgorithm  
+
class vtkTestGraphAlgorithmSource : public vtkGraphAlgorithm  
 
{
 
{
 
public:
 
public:
   vtkTypeRevisionMacro(vtkTestSource,vtkGraphAlgorithm);
+
   vtkTypeRevisionMacro(vtkTestGraphAlgorithmSource,vtkGraphAlgorithm);
 
   void PrintSelf(ostream& os, vtkIndent indent);
 
   void PrintSelf(ostream& os, vtkIndent indent);
  
   static vtkTestSource *New();
+
   static vtkTestGraphAlgorithmSource *New();
 
 
 
protected:
 
protected:
   vtkTestSource();
+
   vtkTestGraphAlgorithmSource();
   ~vtkTestSource();
+
   ~vtkTestGraphAlgorithmSource();
 
    
 
    
 
   int RequestData(vtkInformation *, vtkInformationVector **, vtkInformationVector *);
 
   int RequestData(vtkInformation *, vtkInformationVector **, vtkInformationVector *);
Line 49: Line 49:
  
 
private:
 
private:
   vtkTestSource(const vtkTestSource&);  // Not implemented.
+
   vtkTestGraphAlgorithmSource(const vtkTestGraphAlgorithmSource&);  // Not implemented.
   void operator=(const vtkTestSource&);  // Not implemented.
+
   void operator=(const vtkTestGraphAlgorithmSource&);  // Not implemented.
  
 
};
 
};
Line 58: Line 58:
 
</source>
 
</source>
  
==vtkTestSource.cxx==
+
==vtkTestGraphAlgorithmSource.cxx==
 
<source lang="cpp">
 
<source lang="cpp">
#include "vtkTestSource.h"
+
#include "vtkTestGraphAlgorithmSource.h"
  
 
#include "vtkObjectFactory.h"
 
#include "vtkObjectFactory.h"
Line 72: Line 72:
 
#include "vtkGraph.h"
 
#include "vtkGraph.h"
  
vtkCxxRevisionMacro(vtkTestSource, "$Revision: 1.70 $");
+
vtkCxxRevisionMacro(vtkTestGraphAlgorithmSource, "$Revision: 1.70 $");
vtkStandardNewMacro(vtkTestSource);
+
vtkStandardNewMacro(vtkTestGraphAlgorithmSource);
  
vtkTestSource::vtkTestSource()
+
vtkTestGraphAlgorithmSource::vtkTestGraphAlgorithmSource()
 
{
 
{
 
   this->SetNumberOfInputPorts(0);
 
   this->SetNumberOfInputPorts(0);
Line 81: Line 81:
 
}
 
}
  
vtkTestSource::~vtkTestSource()
+
vtkTestGraphAlgorithmSource::~vtkTestGraphAlgorithmSource()
 
{
 
{
  
 
}
 
}
  
int vtkTestSource::RequestData(
+
int vtkTestGraphAlgorithmSource::RequestData(
 
   vtkInformation *vtkNotUsed(request),
 
   vtkInformation *vtkNotUsed(request),
 
   vtkInformationVector **vtkNotUsed(inputVector),
 
   vtkInformationVector **vtkNotUsed(inputVector),
Line 110: Line 110:
 
}
 
}
  
int vtkTestSource::RequestDataObject(
+
int vtkTestGraphAlgorithmSource::RequestDataObject(
 
   vtkInformation*,  
 
   vtkInformation*,  
 
   vtkInformationVector**,  
 
   vtkInformationVector**,  
Line 127: Line 127:
  
 
//----------------------------------------------------------------------------
 
//----------------------------------------------------------------------------
void vtkTestSource::PrintSelf(ostream& os, vtkIndent indent)
+
void vtkTestGraphAlgorithmSource::PrintSelf(ostream& os, vtkIndent indent)
 
{
 
{
 
   this->Superclass::PrintSelf(os,indent);
 
   this->Superclass::PrintSelf(os,indent);
Line 133: Line 133:
 
</source>
 
</source>
  
==CMakeLists.txt==
+
{{VTKCMakeListsMultiple|{{SUBPAGENAME}}|vtkTestGraphAlgorithmSource}}
<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>
 

Revision as of 22:02, 3 February 2017

This example produces a vtkGraph as output.

GraphAlgorithmSource.cxx

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

#include "vtkTest GraphAlgorithmSource.h"

int main (int, char *[])
{
  vtkSmartPointer<vtkTest GraphAlgorithmSource> source =
    vtkSmartPointer<vtkTest GraphAlgorithmSource>::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:
  vtkTypeRevisionMacro(vtkTestGraphAlgorithmSource,vtkGraphAlgorithm);
  void PrintSelf(ostream& os, vtkIndent indent);

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

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"

vtkCxxRevisionMacro(vtkTestGraphAlgorithmSource, "$Revision: 1.70 $");
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 = 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 vtkTestGraphAlgorithmSource::RequestDataObject(
  vtkInformation*, 
  vtkInformationVector**, 
  vtkInformationVector* )
{

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

  return 1;
}

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

CMakeLists.txt

cmake_minimum_required(VERSION 2.8)

PROJECT(GraphAlgorithmSource)

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

add_executable(GraphAlgorithmSource MACOSX_BUNDLE GraphAlgorithmSource vtkTestGraphAlgorithmSource)

if(VTK_LIBRARIES)
  target_link_libraries(GraphAlgorithmSource ${VTK_LIBRARIES})
else()
  target_link_libraries(GraphAlgorithmSource vtkHybrid vtkWidgets)
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 PLEASE NOTE: Be sure to add the VTK bin directory to your path. This will resolve the VTK dll's at run time.