VTK/Examples/Developers/MultipleInputConnections

From KitwarePublic

Jump to: navigation, search

Contents

FilterExample.cxx

#include <vtkSmartPointer.h>
#include "vtkTestFilter.h"
 
int main(int, char*[])
{
  //setup the first input
  vtkSmartPointer<vtkPoints> points1 =
    vtkSmartPointer<vtkPoints>::New();
  points1->InsertNextPoint(1.0, 2.0, 3.0);
  vtkSmartPointer<vtkPolyData> inputPolydata1 =
    vtkSmartPointer<vtkPolyData>::New();
  inputPolydata1->SetPoints(points1);
 
  //setup the second input
  vtkSmartPointer<vtkPoints> points2 =
    vtkSmartPointer<vtkPoints>::New();
  points2->InsertNextPoint(4.0, 5.0, 6.0);
  points2->InsertNextPoint(4.0, 5.0, 6.0);
  vtkSmartPointer<vtkPolyData> inputPolydata2 =
    vtkSmartPointer<vtkPolyData>::New();
  inputPolydata2->SetPoints(points2);
 
  vtkSmartPointer<vtkTestFilter> filter =
    vtkSmartPointer<vtkTestFilter>::New();
  filter->AddInputConnection(inputPolydata1->GetProducerPort());
  filter->AddInputConnection(inputPolydata2->GetProducerPort());
  filter->Update();
 
  vtkPolyData* outputPolydata = filter->GetOutput();
 
  std::cout << "Output points: " << outputPolydata->GetNumberOfPoints() << std::endl;
 
  return EXIT_SUCCESS;
}

vtkTestFilter.h

// .NAME vtkTestFilter
// .SECTION Description
// vtkTestFilter
 
#ifndef __vtkTestFilter_h
#define __vtkTestFilter_h
 
#include "vtkPolyDataAlgorithm.h"
 
class vtkTestFilter : public vtkPolyDataAlgorithm
{
public:
  vtkTypeMacro(vtkTestFilter,vtkPolyDataAlgorithm);
  void PrintSelf(ostream& os, vtkIndent indent);
 
  static vtkTestFilter *New();
 
protected:
  vtkTestFilter(){}
  ~vtkTestFilter(){}
 
  int FillInputPortInformation( int port, vtkInformation* info );
  int RequestData(vtkInformation *, vtkInformationVector **, vtkInformationVector *);
 
private:
  vtkTestFilter(const vtkTestFilter&);  // Not implemented.
  void operator=(const vtkTestFilter&);  // Not implemented.
 
};
 
#endif

vtkTestFilter.cxx

#include "vtkTestFilter.h"
 
#include "vtkObjectFactory.h"
#include "vtkStreamingDemandDrivenPipeline.h"
#include "vtkInformationVector.h"
#include "vtkInformation.h"
#include "vtkDataObject.h"
#include "vtkSmartPointer.h"
 
vtkStandardNewMacro(vtkTestFilter);
 
int vtkTestFilter::FillInputPortInformation( int port, vtkInformation* info )
{
  if(port == 0)
  {
    info->Set(vtkDataObject::DATA_TYPE_NAME(), "vtkPolyData" );
    info->Set(vtkAlgorithm::INPUT_IS_REPEATABLE(), 1);
    return 1;
  }
 
  vtkErrorMacro("This filter does not have more than 1 input port!");
  return 0;
}
 
int vtkTestFilter::RequestData(vtkInformation *vtkNotUsed(request),
                                             vtkInformationVector **inputVector,
                                             vtkInformationVector *outputVector)
{
 
  // get the info objects
  vtkInformation *inInfo0 = inputVector[0]->GetInformationObject(0);
  vtkInformation *inInfo1 = inputVector[0]->GetInformationObject(1);
  vtkInformation *outInfo = outputVector->GetInformationObject(0);
 
  // get the output
  vtkPolyData *output = vtkPolyData::SafeDownCast(
      outInfo->Get(vtkDataObject::DATA_OBJECT()));
 
  // get the input
  vtkPolyData *input0 = vtkPolyData::SafeDownCast(
      inInfo0->Get(vtkDataObject::DATA_OBJECT()));
 
  vtkPolyData *input1 = vtkPolyData::SafeDownCast(
    inInfo1->Get(vtkDataObject::DATA_OBJECT()));
 
  std::cout << "Input 0 has " << input0->GetNumberOfPoints() << " points." << std::endl;
  std::cout << "Input 1 has " << input1->GetNumberOfPoints() << " points." << std::endl;
 
  output->ShallowCopy(input0);
 
  return 1;
}
 
 
//----------------------------------------------------------------------------
void vtkTestFilter::PrintSelf(ostream& os, vtkIndent indent)
{
  this->Superclass::PrintSelf(os,indent);
}

CMakeLists.txt

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