Skip to content

ProgressReport

vtk-examples/Cxx/Developers/ProgressReport

Description

This example demonstrates how to get the progress of a filter. This requires that the filter is updating its progress in a sensible way. A sample filter is provided which loops through the input points and updates its progress along the way.

Question

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

Code

ProgressReport.cxx

#include <vtkCallbackCommand.h>
#include <vtkCommand.h>
#include <vtkNew.h>
#include <vtkPoints.h>
#include <vtkPolyData.h>
#include <vtkSphereSource.h>

#include "vtkTestProgressReportFilter.h"

void ProgressFunction(vtkObject* caller, long unsigned int eventId,
                      void* clientData, void* callData);

int main(int, char*[])
{
  vtkNew<vtkSphereSource> sphereSource;
  sphereSource->Update();

  vtkNew<vtkCallbackCommand> progressCallback;
  progressCallback->SetCallback(ProgressFunction);

  vtkNew<vtkTestProgressReportFilter> testFilter;
  testFilter->SetInputConnection(sphereSource->GetOutputPort());
  testFilter->AddObserver(vtkCommand::ProgressEvent, progressCallback);
  testFilter->Update();

  return EXIT_SUCCESS;
}

void ProgressFunction(vtkObject* caller, long unsigned int vtkNotUsed(eventId),
                      void* vtkNotUsed(clientData), void* vtkNotUsed(callData))
{
  vtkTestProgressReportFilter* testFilter =
      static_cast<vtkTestProgressReportFilter*>(caller);
  std::cout << "Progress: " << testFilter->GetProgress() << std::endl;
}

vtkTestProgressReportFilter.h

#ifndef __vtkTestProgressReportFilter_h
#define __vtkTestProgressReportFilter_h

#include <vtkPolyDataAlgorithm.h>

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

protected:
  vtkTestProgressReportFilter(){}
  ~vtkTestProgressReportFilter() {}

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

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

};

#endif

vtkTestProgressReportFilter.cxx

#include "vtkTestProgressReportFilter.h"

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

vtkStandardNewMacro(vtkTestProgressReportFilter);

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

  // Get the info objects
  vtkInformation *inInfo = inputVector[0]->GetInformationObject(0);
  vtkInformation *outInfo = outputVector->GetInformationObject(0);


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

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

  for(vtkIdType i = 0; i < input->GetNumberOfPoints(); i++)
  {
    this->UpdateProgress(static_cast<double>(i)/input->GetNumberOfPoints());
  }

  output->ShallowCopy(input);

  return 1;
}

CMakeLists.txt

cmake_minimum_required(VERSION 3.3 FATAL_ERROR)

project(ProgressReport)

find_package(VTK COMPONENTS 
  vtkCommonCore
  vtkCommonDataModel
  vtkFiltersSources
  QUIET
)

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

Download and Build ProgressReport

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

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

./ProgressReport

WINDOWS USERS

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