Skip to content


# MultipleInputPorts

VTKEx/Cxx/Developers/MultipleInputPorts

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

MultipleInputPorts.cxx

    #include <vtkSmartPointer.h>
    #include <vtkSphereSource.h>

    #include "vtkTestMultipleInputPortsFilter.h"

    int main (int /* argc */, char * /* argv */ [])
    {
      vtkSmartPointer<vtkSphereSource> sphereSource1 =
          vtkSmartPointer<vtkSphereSource>::New();
      sphereSource1->SetThetaResolution(5);
      sphereSource1->Update();

      vtkSmartPointer<vtkSphereSource> sphereSource2 =
          vtkSmartPointer<vtkSphereSource>::New();
      sphereSource2->SetThetaResolution(10);
      sphereSource2->Update();

      vtkSmartPointer<vtkTestMultipleInputPortsFilter> filter =
          vtkSmartPointer<vtkTestMultipleInputPortsFilter>::New();
      filter->SetInputConnection(0, sphereSource1->GetOutputPort());
      filter->SetInputConnection(1, sphereSource2->GetOutputPort());
      filter->Update();

      return EXIT_SUCCESS;
    }

vtkTestMultipleInputPortsFilter.h

    // .NAME vtkTestMultipleInputPortsFilter
    // .SECTION Description
    // vtkTestMultipleInputPortsFilter

    #ifndef __vtkTestMultipleInputPortsFilter_h
    #define __vtkTestMultipleInputPortsFilter_h

    #include <vtkPolyDataAlgorithm.h>

    class vtkTestMultipleInputPortsFilter : public vtkPolyDataAlgorithm
    {
    public:
      static vtkTestMultipleInputPortsFilter *New();
      vtkTypeMacro(vtkTestMultipleInputPortsFilter,vtkPolyDataAlgorithm);

    protected:
      vtkTestMultipleInputPortsFilter();
      ~vtkTestMultipleInputPortsFilter() {}

      int FillInputPortInformation( int port, vtkInformation* info ) override;
      int RequestData(vtkInformation *, vtkInformationVector **, vtkInformationVector *) override;

    private:
      vtkTestMultipleInputPortsFilter(const vtkTestMultipleInputPortsFilter&) = delete;
      void operator=(const vtkTestMultipleInputPortsFilter&) = delete;

    };

    #endif

vtkTestMultipleInputPortsFilter.cxx

    #include "vtkTestMultipleInputPortsFilter.h"

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

    vtkStandardNewMacro(vtkTestMultipleInputPortsFilter);

    vtkTestMultipleInputPortsFilter::vtkTestMultipleInputPortsFilter()
    {
      this->SetNumberOfInputPorts(2);
    }

    int vtkTestMultipleInputPortsFilter::FillInputPortInformation( int port, vtkInformation* info )
    {
      if ( port == 0 )
      {
        info->Set(vtkDataObject::DATA_TYPE_NAME(), "vtkPolyData" );
        return 1;
      }
      else if(port == 1)
      {
        info->Set(vtkDataObject::DATA_TYPE_NAME(), "vtkPolyData" );
        return 1;
      }

      return 0;
    }

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

      // get the info objects
      vtkInformation *inInfo0 = inputVector[0]->GetInformationObject(0);
      vtkInformation *inInfo1 = inputVector[1]->GetInformationObject(0);

      vtkInformation *outInfo = outputVector->GetInformationObject(0);

      // get the input and ouptut
      vtkPolyData *input0 = dynamic_cast<vtkPolyData*>(
          inInfo0->Get(vtkDataObject::DATA_OBJECT()));

      std::cout << "input0 has " << input0->GetNumberOfPoints() << " points." << std::endl;

      vtkPolyData *input1 = dynamic_cast<vtkPolyData*>(
          inInfo1->Get(vtkDataObject::DATA_OBJECT()));

      std::cout << "input1 has " << input1->GetNumberOfPoints() << " points." << std::endl;

      vtkPolyData *output = dynamic_cast<vtkPolyData*>(
          outInfo->Get(vtkDataObject::DATA_OBJECT()));

      output->ShallowCopy(input0);

      return 1;
    }

### CMakeLists.txt

    cmake_minimum_required(VERSION 3.3 FATAL_ERROR)

    project(MultipleInputPorts)

    find_package(VTK COMPONENTS
      vtkCommonCore
      vtkFiltersSources QUIET)
    if (NOT VTK_FOUND)
      message("Skipping MultipleInputPorts: ${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(MultipleInputPorts MACOSX_BUNDLE MultipleInputPorts.cxx )
      target_link_libraries(MultipleInputPorts PRIVATE ${VTK_LIBRARIES})
    else ()
      # include all components
      add_executable(MultipleInputPorts MACOSX_BUNDLE MultipleInputPorts.cxx )
      target_link_libraries(MultipleInputPorts PRIVATE ${VTK_LIBRARIES})
      # vtk_module_autoinit is needed
      vtk_module_autoinit(
        TARGETS MultipleInputPorts
        MODULES ${VTK_LIBRARIES}
        )
    endif ()

### Download and Build MultipleInputPorts

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

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

    ./MultipleInputPorts

WINDOWS USERS

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