Skip to content

PolyDataFilter

Repository source: PolyDataFilter

Description

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

You will need the following in your CMakeLists.txt file:

find_package(VTK
 COMPONENTS
    CommonCore
    CommonDataModel
    CommonExecutionModel
    FiltersSources
    InfovisCore
)

Question

If you have a question about this example, please use the VTK Discourse Forum

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();
  filter->SetInputData(inputPolydata);
  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 <vtkDataObject.h>
#include <vtkInformation.h>
#include <vtkInformationVector.h>
#include <vtkObjectFactory.h>
#include <vtkSmartPointer.h>
#include <vtkStreamingDemandDrivenPipeline.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 3.12 FATAL_ERROR)

project(PolyDataFilter)

find_package(VTK COMPONENTS 
  CommonCore
)

if (NOT VTK_FOUND)
  message(FATAL_ERROR "PolyDataFilter: Unable to find the VTK build folder.")
endif()

# Prevent a "command line is too long" failure in Windows.
set(CMAKE_NINJA_FORCE_RESPONSE_FILE "ON" CACHE BOOL "Force Ninja to use response files.")
add_executable(PolyDataFilter MACOSX_BUNDLE PolyDataFilter.cxx )
  target_link_libraries(PolyDataFilter PRIVATE ${VTK_LIBRARIES}
)
# vtk_module_autoinit is needed
vtk_module_autoinit(
  TARGETS PolyDataFilter
  MODULES ${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.