Deriving from vtkAlgorithm

From KitwarePublic
Revision as of 19:17, 26 November 2009 by Daviddoria (talk | contribs)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to navigationJump to search

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">

  1. include <vtkSmartPointer.h>
  1. include "vtkTestReader.h"
  2. 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->Update();
 
 vtkTest* test = reader->GetOutput();
 vtkstd::cout << "Value: " << test->GetValue() << vtkstd::endl;
 
 return 0;

}

</source>

vtkTest.h

<source lang="cpp">

  1. ifndef __vtkTest_h
  2. define __vtkTest_h
  1. 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;

};

  1. endif

</source>

vtkTestReader.h

<source lang="cpp">

  1. ifndef __vtkTestReader_h
  2. define __vtkTestReader_h
  1. include "vtkAlgorithm.h"

class vtkTestReader : public vtkAlgorithm { public:

 vtkTypeRevisionMacro(vtkTestReader,vtkAlgorithm);
 void PrintSelf(ostream& os, vtkIndent indent);
 static vtkTestReader *New();

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.

};

  1. endif

</source>

vtkTest.cxx

<source lang="cpp">

  1. include "vtkTest.h"
  1. include "vtkObjectFactory.h"
  2. include "vtkStreamingDemandDrivenPipeline.h"
  3. include "vtkInformationVector.h"
  4. 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">

  1. include "vtkTestReader.h"
  2. include "vtkTest.h"
  1. include "vtkObjectFactory.h"
  2. include "vtkStreamingDemandDrivenPipeline.h"
  3. include "vtkInformationVector.h"
  4. include "vtkInformation.h"
  5. include "vtkDataObject.h"
  6. 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>