[Insight-developers] New class: ProgressReporter

Brad King brad . king at kitware . com
Fri, 30 Aug 2002 14:51:27 -0400 (EDT)


Hello, all:

I recently noticed that many of ITK's image filters have duplicate code
for calculating the current progress based on the fraction of output
pixels that have been completed.  Many filters seem to have slightly
differing versions of this code because of fixes that were only applied to
one filter.

I've added a new class called "ProgressReporter" to solve this problem.
It is a helper class designed for use by GenerateData() and
ThreadedGenerateData() methods.  It is used like this:

#include "itkProgressReporter.h"

GenerateData()
{
  // ... setup filter ...
  ProgressReporter progress(this, 0, outputRegion.GetNumberOfPixels());
  for( each pixel in outputRegion )
    {
    // ... process pixel ...
    progress.CompletedPixel();
    }
}

// OR:

ThreadedGenerateData(threadId, threadRegion)
{
  // ... setup filter ...
  ProgressReporter progress(this, threadId,
                            threadRegion.GetNumberOfPixels());
  for( each pixel in threadRegion )
    {
    // ... process pixel ...
    progress.CompletedPixel();
    }
}

The reporter's constructor automatically sets progress to 0, and the
destructor sets it to 1.  The CompletedPixel() method is an inline method
that efficiently updates the progress at an interval determined by the
number of updates.  The default number of updates is 100, but a fourth
argument can be given to the constructor to alter this setting.

When there are multiple threads, only thread 0 will report progress.
This assumes that all threads have approximately the same amount of work
to do, or at least that thread 0 has the most work.  When there are no
threads, the thread id argument should be 0.  I considered having the
thread id be optional, but realized that this could result in having every
thread report progress if the argument is accidentally left off.

-Brad