Deriving from vtkAlgorithm
This example demonstrates how to derive from vtkAlgorithm to set a custom output type for a filter. vtkTest is a class that simply stores a double named Value. vtkTestReader is a class whose GetOutput() function returns a vtkTest.
This example is currently broken - vtkDataObject has a function DATA_TYPE_NAME () which is used when calling info->Set(vtkDataObject::DATA_TYPE_NAME(), "vtkTest" ); However, since test is derived from vtkObject, DATA_TYPE_NAME() is not available. How do you call info->Set() with a vtkObject?
Example.cxx
<source lang="cpp">
- include <vtkSmartPointer.h>
- include "vtkTestReader.h"
- include "vtkTest.h"
int main (int argc, char *argv[]) {
if(argc != 2) { vtkstd::cout << "Required arguments: Filename" << vtkstd::endl; exit(-1); } vtkstd::string InputFilename = argv[1]; vtkSmartPointer<vtkTestReader> reader = vtkSmartPointer<vtkTestReader>::New(); //reader->SetFileName(InputFilename.c_str()); reader->Update(); //vtkTest* test = reader->GetOutput(); vtkTest* test = reader->GetOutputPort(); vtkstd::cout << "Value: " << test->GetValue() << vtkstd::endl; return 0;
}
</source>
vtkTest.h
<source lang="cpp">
- ifndef __vtkTest_h
- define __vtkTest_h
- include "vtkObject.h"
class vtkInformation; class vtkInformationVector;
class vtkTest : public vtkObject { public:
vtkTypeRevisionMacro(vtkTest,vtkObject); void PrintSelf(ostream& os, vtkIndent indent);
static vtkTest *New(); vtkSetMacro(Value,double); vtkGetMacro(Value,double);
protected:
vtkTest(); ~vtkTest(); int RequestData(vtkInformation *, vtkInformationVector **, vtkInformationVector *);
private:
vtkTest(const vtkTest&); // Not implemented. void operator=(const vtkTest&); // Not implemented. double Value;
};
- endif
</source>
vtkTestReader.h
<source lang="cpp">
- ifndef __vtkTestReader_h
- define __vtkTestReader_h
- include "vtkAlgorithm.h"
class vtkTestReader : public vtkAlgorithm { public:
vtkTypeRevisionMacro(vtkTestReader,vtkAlgorithm); void PrintSelf(ostream& os, vtkIndent indent);
static vtkTestReader *New(); // Description: // Specify file name of the ptx file. vtkSetStringMacro(FileName); // Description: // Get the file name of the ptx file. vtkGetStringMacro(FileName);
protected:
vtkTestReader(); ~vtkTestReader(); int FillOutputPortInformation( int port, vtkInformation* info ); int RequestData(vtkInformation *, vtkInformationVector **, vtkInformationVector *);
private:
vtkTestReader(const vtkTestReader&); // Not implemented. void operator=(const vtkTestReader&); // Not implemented. bool ReadFile(); char* FileName;
};
- endif
</source>
vtkTest.cxx
<source lang="cpp">
- include "vtkTest.h"
- include "vtkObjectFactory.h"
- include "vtkStreamingDemandDrivenPipeline.h"
- include "vtkInformationVector.h"
- include "vtkInformation.h"
vtkCxxRevisionMacro(vtkTest, "$Revision: 1.70 $"); vtkStandardNewMacro(vtkTest);
vtkTest::vtkTest() {
}
vtkTest::~vtkTest() {
}
int vtkTest::RequestData(
vtkInformation *vtkNotUsed(request), vtkInformationVector **vtkNotUsed(inputVector), vtkInformationVector *outputVector)
{
// get the info object vtkInformation *outInfo = outputVector->GetInformationObject(0); // get the ouptut
// vtkPolyData *output = vtkPolyData::SafeDownCast( // outInfo->Get(vtkDataObject::DATA_OBJECT()));
//output->ShallowCopy(polydata); return 1;
}
//----------------------------------------------------------------------------
void vtkTest::PrintSelf(ostream& os, vtkIndent indent)
{
this->Superclass::PrintSelf(os,indent);
os << indent << "Value: " << this->Value << "\n";
}
</source>
vtkTestReader.cxx
<source lang="cpp">
- include "vtkTestReader.h"
- include "vtkTest.h"
- include "vtkObjectFactory.h"
- include "vtkStreamingDemandDrivenPipeline.h"
- include "vtkInformationVector.h"
- include "vtkInformation.h"
- include "vtkDataObject.h"
- include "vtkSmartPointer.h"
vtkCxxRevisionMacro(vtkTestReader, "$Revision: 1.70 $"); vtkStandardNewMacro(vtkTestReader);
vtkTestReader::vtkTestReader() {
this->FileName = NULL; this->SetNumberOfInputPorts(0); this->SetNumberOfOutputPorts(1);
}
vtkTestReader::~vtkTestReader() {
}
int vtkTestReader::FillOutputPortInformation( int port, vtkInformation* info ) {
if ( port == 0 ) { //info->Set(vtkDataObject::DATA_TYPE_NAME(), "vtkTest" ); info->Set(vtkObject::DATA_TYPE_NAME(), "vtkTest" ); return 1; }
return 0;
}
int vtkTestReader::RequestData(
vtkInformation *vtkNotUsed(request), vtkInformationVector **vtkNotUsed(inputVector), vtkInformationVector *outputVector)
{
// get the info object vtkInformation *outInfo = outputVector->GetInformationObject(0); // get the ouptut vtkTest *output = vtkTest::SafeDownCast( outInfo->Get(vtkObject::DATA_OBJECT()));
//outInfo->Get(vtkDataObject::DATA_OBJECT()));
vtkSmartPointer<vtkTest> test = vtkSmartPointer<vtkTest>::New(); test->SetValue(5.1) ; output = test; return 1;
}
//----------------------------------------------------------------------------
void vtkTestReader::PrintSelf(ostream& os, vtkIndent indent)
{
this->Superclass::PrintSelf(os,indent);
os << indent << "File Name: " << (this->FileName ? this->FileName : "(none)") << "\n";
}
</source>
CMakeLists.txt
<source lang="text"> cmake_minimum_required(VERSION 2.6) PROJECT(vtkAlgorithmDemo)
FIND_PACKAGE(VTK REQUIRED) INCLUDE(${VTK_USE_FILE})
ADD_EXECUTABLE(vtkAlgorithmDemo Example.cxx vtkTest.cxx vtkTestReader.cxx) TARGET_LINK_LIBRARIES(vtkAlgorithmDemo vtkHybrid)
</source>