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.

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

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 <vtkDataObject.h>
#include <vtkInformation.h>
#include <vtkInformationVector.h>
#include <vtkObjectFactory.h>
#include <vtkSmartPointer.h>
#include <vtkStreamingDemandDrivenPipeline.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.12 FATAL_ERROR)

project(ProgressReport)

find_package(VTK COMPONENTS 
  CommonCore
  CommonDataModel
  FiltersSources
)

if (NOT VTK_FOUND)
  message(FATAL_ERROR "ProgressReport: 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(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}
)

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.