PolyDataFilter

VTKExamples/Cxx/Developers/PolyDataFilter

Description

This example demonstrates a filter named vtkTestPolyDataFilter that takes a vtkPolyData as input and produces a vtkPolyData as output.

Code

PolyDataFilter.cxx

#include <vtkSmartPointer.h>

#include <vtkTestPolyDataFilter.h>

int main(int, char *[])
{
  vtkSmartPointer<vtkPoints> points = 
    vtkSmartPointer<vtkPoints>::New();
  points->InsertNextPoint(0.0, 0.0, 0.0);

  vtkSmartPointer<vtkPolyData> inputPolydata =   
    vtkSmartPointer<vtkPolyData>::New();
  inputPolydata->SetPoints(points);

  std::cout << "Input points: " << inputPolydata->GetNumberOfPoints() 
            << std::endl;

  vtkSmartPointer<vtkTestPolyDataFilter> filter = 
    vtkSmartPointer<vtkTestPolyDataFilter>::New();
#if VTK_MAJOR_VERSION <= 5
  filter->SetInput(inputPolydata);
#else
  filter->SetInputData(inputPolydata);
#endif
  filter->Update();

  vtkPolyData* outputPolydata = filter->GetOutput();

  std::cout << "Output points: " << outputPolydata->GetNumberOfPoints() 
            << std::endl;

  return EXIT_SUCCESS;
}

vtkTestPolyDataFilter.h

#ifndef __vtkTestPolyDataFilter_h
#define __vtkTestPolyDataFilter_h

#include <vtkPolyDataAlgorithm.h>

class vtkTestPolyDataFilter : public vtkPolyDataAlgorithm 
{
public:
  vtkTypeMacro(vtkTestPolyDataFilter,vtkPolyDataAlgorithm);
  void PrintSelf(ostream& os, vtkIndent indent);

  static vtkTestPolyDataFilter *New();

protected:
  vtkTestPolyDataFilter();
  ~vtkTestPolyDataFilter();

  int RequestData(vtkInformation *, vtkInformationVector **, vtkInformationVector *);

private:
  vtkTestPolyDataFilter(const vtkTestPolyDataFilter&);  // Not implemented.
  void operator=(const vtkTestPolyDataFilter&);  // Not implemented.

};

#endif

vtkTestPolyDataFilter.cxx

#include <vtkTestPolyDataFilter.h>

#include <vtkObjectFactory.h>
#include <vtkStreamingDemandDrivenPipeline.h>
#include <vtkInformationVector.h>
#include <vtkInformation.h>
#include <vtkDataObject.h>
#include <vtkSmartPointer.h>

vtkStandardNewMacro(vtkTestPolyDataFilter);

vtkTestPolyDataFilter::vtkTestPolyDataFilter()
{
  this->SetNumberOfInputPorts(1);
  this->SetNumberOfOutputPorts(1);
}

vtkTestPolyDataFilter::~vtkTestPolyDataFilter()
{
}

int vtkTestPolyDataFilter::RequestData(vtkInformation *vtkNotUsed(request),
                                             vtkInformationVector **inputVector,
                                             vtkInformationVector *outputVector)
{

  // get the input and output
  vtkPolyData* input = vtkPolyData::GetData(inputVector[0],0);
  vtkPolyData* output = vtkPolyData::GetData(outputVector,0);

  input->GetPoints()->InsertNextPoint(1.0, 1.0, 1.0);

  output->ShallowCopy(input);

  return 1;
}

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

CMakeLists.txt

cmake_minimum_required(VERSION 2.8)

PROJECT(PolyDataFilter)

find_package(VTK REQUIRED)
include(${VTK_USE_FILE})

add_executable(PolyDataFilter MACOSX_BUNDLE PolyDataFilter.cxx  vtkTestPolyDataFilter.cxx)

target_link_libraries(PolyDataFilter ${VTK_LIBRARIES})

Download and Build PolyDataFilter

Click here to download PolyDataFilter and its CMakeLists.txt file. Once the tarball PolyDataFilter.tar has been downloaded and extracted,

cd PolyDataFilter/build 

If VTK is installed:

cmake ..

If VTK is not installed but compiled on your system, you will need to specify the path to your VTK build:

cmake -DVTK_DIR:PATH=/home/me/vtk_build ..

Build the project:

make

and run it:

./PolyDataFilter

WINDOWS USERS

Be sure to add the VTK bin directory to your path. This will resolve the VTK dll's at run time.