[vtkusers] trying to implement a poly-to-image vtkAlgorithm

Hal Canary hal at cs.unc.edu
Wed Aug 22 09:42:04 EDT 2012


Hello all,

I'm trying to implement an algorithm that takes in a vtkPolyData and 
outputs a vtkImageData.  Here's the minimal code that is failing to work.

Specifically, the RequestData function is never called.  I'm not sure 
why not.  Any help would be appreciated.

Thanks,

Hal Canary

-------------------------------------

/* --CMakeLists.txt--
cmake_minimum_required(VERSION 2.6)
project(Test)
set(VTK_DIR "/home/hal/build/VTK")
find_package(VTK REQUIRED)
include(${VTK_USE_FILE})
set(CMAKE_CXX_FLAGS "-g -Wall")
add_executable(Test Test.cxx)
target_link_libraries(Test ${VTK_LIBRARIES})
*/
/* --Test.cxx-- */
#include <iostream>
#include "vtkAlgorithm.h"
#include "vtkSmartPointer.h"
#include "vtkCubeSource.h"
#include "vtkXMLImageDataWriter.h"
#include "vtkInformationVector.h"
#include "vtkInformation.h"
#include "vtkStreamingDemandDrivenPipeline.h"
#include "vtkImageData.h"
#include "vtkPointData.h"
#include "vtkFloatArray.h"

class  vtkExamplePolyToImageAlgorithm : public vtkAlgorithm
{
public:
   static vtkExamplePolyToImageAlgorithm *New();
   vtkTypeMacro(vtkExamplePolyToImageAlgorithm,vtkAlgorithm);
   void PrintSelf(ostream& os, vtkIndent indent);
protected:
   virtual int FillInputPortInformation(int port, vtkInformation * info);
   virtual int FillOutputPortInformation(int port, vtkInformation * info);
   vtkExamplePolyToImageAlgorithm();
   ~vtkExamplePolyToImageAlgorithm() { }
   virtual int RequestInformation(
     vtkInformation *request,
     vtkInformationVector **inputVector,
     vtkInformationVector *outputVector);
   virtual int RequestData(
     vtkInformation * request,
     vtkInformationVector ** inputVector,
     vtkInformationVector * outputVector);
  private:
   // Not implemented.
   vtkExamplePolyToImageAlgorithm(const vtkExamplePolyToImageAlgorithm&);
   // Not implemented.
   void operator=(const vtkExamplePolyToImageAlgorithm&);
};

vtkStandardNewMacro(vtkExamplePolyToImageAlgorithm);
vtkExamplePolyToImageAlgorithm::vtkExamplePolyToImageAlgorithm()
{
   std::cerr
     << 
"vtkExamplePolyToImageAlgorithm::vtkExamplePolyToImageAlgorithm()\n";
   this->SetNumberOfInputPorts(1);
   this->SetNumberOfOutputPorts(1);
}

int vtkExamplePolyToImageAlgorithm::FillInputPortInformation(
   int vtkNotUsed(port), vtkInformation * info)
{
   std::cerr
     << "vtkExamplePolyToImageAlgorithm::FillInputPortInformation()\n";
   info->Set(vtkAlgorithm::INPUT_REQUIRED_DATA_TYPE(), "vtkDataSet");
   return 1;
}

int vtkExamplePolyToImageAlgorithm::FillOutputPortInformation(
   int vtkNotUsed(port), vtkInformation * info)
{
   std::cerr
     << "vtkExamplePolyToImageAlgorithm::FillOutputPortInformation()\n";
   info->Set(vtkDataObject::DATA_TYPE_NAME(), "vtkImageData");
   return 1;
}


int vtkExamplePolyToImageAlgorithm::RequestInformation(
    vtkInformation *vtkNotUsed(request),
    vtkInformationVector **vtkNotUsed(inputVector),
    vtkInformationVector *outputVector)
{
   std::cerr << "vtkExamplePolyToImageAlgorithm::RequestInformation()\n";
   vtkInformation *outInfo = outputVector->GetInformationObject(0);
   int extent[6] = { 0, 1023, 0, 1023, 0, 0 };
   outInfo->Set(
     vtkStreamingDemandDrivenPipeline::WHOLE_EXTENT(),
     extent, 6);
   outInfo->Set(vtkDataObject::ORIGIN(),  0.0, 0.0, 0.0);
   outInfo->Set(vtkDataObject::SPACING(), 1.0, 1.0, 1.0);
   vtkDataObject::SetPointDataActiveScalarInfo(outInfo, VTK_FLOAT, 1);
   return 1;
}

int vtkExamplePolyToImageAlgorithm::RequestData(
   vtkInformation * request,
   vtkInformationVector ** inputVector,
   vtkInformationVector * outputVector)
{
   std::cerr << "vtkExamplePolyToImageAlgorithm::RequestData()\n";
   //This function never seems to be called.
   vtkInformation *inInfo = inputVector[0]->GetInformationObject(0);
   vtkInformation *outInfo = outputVector->GetInformationObject(0);
   vtkPolyData *input = vtkPolyData::SafeDownCast(
     inInfo->Get(vtkDataObject::DATA_OBJECT()));
   vtkImageData *output = vtkImageData::SafeDownCast(
     outInfo->Get(vtkDataObject::DATA_OBJECT()));

   vtkSmartPointer< vtkImageData > image =
     vtkSmartPointer< vtkImageData >::New();
   image->SetDimensions(1024,1024,1);
   vtkSmartPointer< vtkFloatArray > array =
     vtkSmartPointer< vtkFloatArray >::New();
   array->SetNumberOfTuples(1024*1024);
   array->SetName("DATA_ARRAY");
   float f = 0.0;
   for (vtkIdType  i = 0; i < (1024*1024); ++i)
     { //EXAMPLE ARRAY---in reality it would be filled
     // with values calculated from the vtkPolyData *input
     array->SetTupleValue (i, &f);
     f += 0.001;
     }
   image->GetPointData()->AddArray(array);
   output->ShallowCopy(image);
   return 1;
}

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

int main(int argc, char ** argv)
{
   std::cerr << "main()\n";
   vtkSmartPointer< vtkPolyDataAlgorithm > cube =
     vtkSmartPointer< vtkCubeSource >::New();
   vtkSmartPointer< vtkExamplePolyToImageAlgorithm >
     examplePolyToImageAlgorithm =
     vtkSmartPointer< vtkExamplePolyToImageAlgorithm >::New();
   vtkSmartPointer< vtkXMLImageDataWriter > writer=
     vtkSmartPointer< vtkXMLImageDataWriter >::New();
   cube->Update();
   examplePolyToImageAlgorithm->SetInputConnection(
     cube->GetOutputPort());
   examplePolyToImageAlgorithm->Update();
   writer->SetInputConnection(
     examplePolyToImageAlgorithm->GetOutputPort());
   writer->SetFileName("cube.vti");
   writer->Write();
   return 0;
}




More information about the vtkusers mailing list