VTK/Examples/Cxx/Developers/ProgressReport

From KitwarePublic

Jump to: navigation, search

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.

Contents

Progress.cxx

#include <vtkSmartPointer.h>
#include <vtkPoints.h>
#include <vtkPolyData.h>
#include <vtkSphereSource.h>
#include <vtkCallbackCommand.h>
#include <vtkCommand.h>
 
#include "vtkTestFilter.h"
 
void ProgressFunction(vtkObject* caller, long unsigned int eventId, void* clientData, void* callData);
 
int main(int, char *[])
{ 
  vtkSmartPointer<vtkSphereSource> sphereSource =
    vtkSmartPointer<vtkSphereSource>::New();
  sphereSource->Update();
 
  vtkSmartPointer<vtkCallbackCommand> progressCallback = 
    vtkSmartPointer<vtkCallbackCommand>::New();
  progressCallback->SetCallback(ProgressFunction);
 
  vtkSmartPointer<vtkTestFilter> testFilter = 
    vtkSmartPointer<vtkTestFilter>::New();
  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) )
{
  vtkTestFilter* testFilter = static_cast<vtkTestFilter*>(caller);
  std::cout << "Progress: " << testFilter->GetProgress() << std::endl;
}

vtkTestFilter.h

#ifndef __vtkTestFilter_h
#define __vtkTestFilter_h
 
#include "vtkPolyDataAlgorithm.h"
 
class vtkTestFilter : public vtkPolyDataAlgorithm 
{
public:
  vtkTypeRevisionMacro(vtkTestFilter,vtkAlgorithm);
 
  static vtkTestFilter *New();
 
protected:
  vtkTestFilter(){}
  ~vtkTestFilter(){}
 
  int RequestData(vtkInformation *, vtkInformationVector **, vtkInformationVector *);
 
private:
  vtkTestFilter(const vtkTestFilter&);  // Not implemented.
  void operator=(const vtkTestFilter&);  // Not implemented.
 
};
 
#endif

vtkTestFilter.cxx

#include "vtkTestFilter.h"
 
#include "vtkObjectFactory.h"
#include "vtkStreamingDemandDrivenPipeline.h"
#include "vtkInformationVector.h"
#include "vtkInformation.h"
#include "vtkDataObject.h"
#include "vtkSmartPointer.h"
 
vtkCxxRevisionMacro(vtkTestFilter, "$Revision: 1.70 $");
vtkStandardNewMacro(vtkTestFilter);
 
int vtkTestFilter::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 = vtkPolyData::SafeDownCast(
      inInfo->Get(vtkDataObject::DATA_OBJECT()));
 
  vtkPolyData *output = vtkPolyData::SafeDownCast(
      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 2.6)
 
PROJECT(Progress)
 
FIND_PACKAGE(VTK REQUIRED)
INCLUDE(${VTK_USE_FILE})
 
ADD_EXECUTABLE(Progress Progress.cxx)
TARGET_LINK_LIBRARIES(Progress vtkHybrid)
Personal tools