Skip to content


# PolyDataAlgorithmReader

VTKEx/Cxx/Developers/PolyDataAlgorithmReader

### Description

This example demonstrates a reader that takes nothing as input and produces a vtkPolyData as output.

Question

If you have a simple question about this example contact us at VTKExProject If your question is more complex and may require extended discussion, please use the VTK Discourse Forum

### Code

PolyDataAlgorithmReader.cxx

    #include <vtkSmartPointer.h>
    #include <vtkPolyData.h>

    #include <vtkTestReader.h>

    int main (int, char *[])
    {
      vtkSmartPointer<vtkTestReader> reader =
        vtkSmartPointer<vtkTestReader>::New();
      reader->Update();

      vtkPolyData* polydata = reader->GetOutput();
      polydata->Print(std::cout);

      return EXIT_SUCCESS;
    }

vtkTestReader.h

    #ifndef __vtkTestReader_h
    #define __vtkTestReader_h

    #include <vtkPolyDataAlgorithm.h>

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

      static vtkTestReader *New();

    protected:
      vtkTestReader();
      ~vtkTestReader();

      int FillOutputPortInformation( int port, vtkInformation* info );
      int RequestData(vtkInformation *, vtkInformationVector **, vtkInformationVector *);

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

      char* FileName;
    };

    #endif

vtkTestReader.cxx

    #include <vtkTestReader.h>

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

    vtkStandardNewMacro(vtkTestReader);

    vtkTestReader::vtkTestReader()
    {
      this->FileName = NULL;
      this->SetNumberOfInputPorts(0);
      this->SetNumberOfOutputPorts(1);
    }

    vtkTestReader::~vtkTestReader()
    {

    }

    // This override is not needed as the FillOutputPortInformation on vtkPolyDataAlgorithm
    // does this.  Override this if you need something different from one output that
    // is a vtkPolyData
    int vtkTestReader::FillOutputPortInformation( int port, vtkInformation* info )
    {
      if ( port == 0 )
      {
        info->Set(vtkDataObject::DATA_TYPE_NAME(), "vtkPolyData" );

        return 1;
      }

      return 0;
    }

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

      // Get the output
      vtkPolyData *output = vtkPolyData::GetData(outputVector,0)

      vtkNew<vtkPolyData> polydata;
      vtkNew<vtkPoints> points;
      points->InsertNextPoint(0.0, 0.0, 0.0);
      polydata->SetPoints(points.GetPointer());

      //output = polydata.GetPointer(); //doesn't work
      output->ShallowCopy(polydata.GetPointer());

      return 1;
    }

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

      os << indent << "File Name: "
          << (this->FileName ? this->FileName : "(none)") << "\n";
    }

### CMakeLists.txt

    cmake_minimum_required(VERSION 3.3 FATAL_ERROR)

    project(PolyDataAlgorithmReader)

    find_package(VTK COMPONENTS
      vtkCommonCore
      vtkCommonDataModel QUIET)
    if (NOT VTK_FOUND)
      message("Skipping PolyDataAlgorithmReader: ${VTK_NOT_FOUND_MESSAGE}")
      return ()
    endif()
    message (STATUS "VTK_VERSION: ${VTK_VERSION}")
    if (VTK_VERSION VERSION_LESS "8.90.0")
      # old system
      include(${VTK_USE_FILE})
      add_executable(PolyDataAlgorithmReader MACOSX_BUNDLE PolyDataAlgorithmReader.cxx )
      target_link_libraries(PolyDataAlgorithmReader PRIVATE ${VTK_LIBRARIES})
    else ()
      # include all components
      add_executable(PolyDataAlgorithmReader MACOSX_BUNDLE PolyDataAlgorithmReader.cxx )
      target_link_libraries(PolyDataAlgorithmReader PRIVATE ${VTK_LIBRARIES})
      # vtk_module_autoinit is needed
      vtk_module_autoinit(
        TARGETS PolyDataAlgorithmReader
        MODULES ${VTK_LIBRARIES}
        )
    endif ()

### Download and Build PolyDataAlgorithmReader

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

    cd PolyDataAlgorithmReader/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:

    ./PolyDataAlgorithmReader

WINDOWS USERS

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