VTK/Examples/Developers/vtkImageAlgorithm Source

From KitwarePublic

Jump to: navigation, search

Contents

vtkImageAlgorithmSourceExample.cxx

#include "vtkImageAlgorithmSource.h"
 
#include <vtkSmartPointer.h>
#include <vtkImageData.h>
#include <vtkXMLImageDataWriter.h>
 
void PrintImage(vtkImageData* image);
 
int main (int argc, char *argv[])
{
  vtkSmartPointer<vtkImageAlgorithmSource> source =
    vtkSmartPointer<vtkImageAlgorithmSource>::New();
  source->Update();
 
  vtkImageData* output = source->GetOutput();
 
  std::cout << "Output image: " << std::endl;
 
  PrintImage(output);
 
  vtkSmartPointer<vtkXMLImageDataWriter> writer =
    vtkSmartPointer<vtkXMLImageDataWriter>::New();
  writer->SetFileName("test.vti");
  writer->SetInputConnection(source->GetOutputPort());
  writer->Write();
 
  return EXIT_SUCCESS;
}
 
void PrintImage(vtkImageData* image)
{
  int* dims = image->GetDimensions();
 
  for (int y=0; y<dims[1]; y++)
    {
    for (int x=0; x<dims[0]; x++)
      {
      double v = image->GetScalarComponentAsDouble(x,y,0,0);
      std::cout << v << " ";
    }
    std::cout << std::endl;
  }
 
}

vtkImageAlgorithmSource.cxx

#include "vtkImageAlgorithmSource.h"
 
#include "vtkObjectFactory.h"
#include "vtkStreamingDemandDrivenPipeline.h"
#include "vtkInformationVector.h"
#include "vtkInformation.h"
#include "vtkDataObject.h"
 
#include "vtkSmartPointer.h"
#include "vtkImageData.h"
 
vtkStandardNewMacro(vtkImageAlgorithmSource);
 
vtkImageAlgorithmSource::vtkImageAlgorithmSource()
{
  this->SetNumberOfOutputPorts(1);
  this->SetNumberOfInputPorts(0);
}
 
int vtkImageAlgorithmSource::RequestData(vtkInformation *vtkNotUsed(request),
                                             vtkInformationVector **inputVector,
                                             vtkInformationVector *outputVector)
{
  vtkInformation *outInfo = outputVector->GetInformationObject(0);
 
  vtkImageData *output = vtkImageData::SafeDownCast(
      outInfo->Get(vtkDataObject::DATA_OBJECT()));
 
  vtkSmartPointer<vtkImageData> image =
    vtkSmartPointer<vtkImageData>::New();
  //image->SetDimensions(2,3,1);
  image->SetExtent(0, 2, 0, 3, 0, 0);
  image->SetScalarComponentFromDouble(0,0,0,0, 5.0);
 
  output->ShallowCopy(image);
 
  // Without these lines, the output will appear real but will not work as the input to any other filters
  output->SetExtent(image->GetExtent());
  output->SetUpdateExtent(output->GetExtent());
  output->SetWholeExtent(output->GetExtent());
 
  return 1;
}

vtkImageAlgorithmSource.h

#ifndef __vtkImageAlgorithmSource_h
#define __vtkImageAlgorithmSource_h
 
#include "vtkImageAlgorithm.h"
 
class vtkImageAlgorithmSource : public vtkImageAlgorithm 
{
public:
  vtkTypeMacro(vtkImageAlgorithmSource,vtkImageAlgorithm);
 
  static vtkImageAlgorithmSource *New();
 
protected:
  vtkImageAlgorithmSource();
 
  int RequestData(vtkInformation *, vtkInformationVector **, vtkInformationVector *);
 
private:
  vtkImageAlgorithmSource(const vtkImageAlgorithmSource&);  // Not implemented.
  void operator=(const vtkImageAlgorithmSource&);  // Not implemented.
 
};
 
#endif

CMakeLists.txt

cmake_minimum_required(VERSION 2.6)
PROJECT(vtkImageAlgorithmSourceExample)
 
FIND_PACKAGE(VTK REQUIRED)
INCLUDE(${VTK_USE_FILE})
 
ADD_EXECUTABLE(vtkImageAlgorithmSourceExample vtkImageAlgorithmSourceExample.cxx vtkImageAlgorithmSource.cxx)
TARGET_LINK_LIBRARIES(vtkImageAlgorithmSourceExample vtkHybrid)
Personal tools