| 1 |
|
/*========================================================================= |
| 2 |
|
|
| 3 |
|
Program: Insight Segmentation & Registration Toolkit |
| 4 |
|
Module: $RCSfile: itkProgressReporter.h.html,v $ |
| 5 |
|
Language: C++ |
| 6 |
|
Date: $Date: 2006/01/17 19:15:46 $ |
| 7 |
|
Version: $Revision: 1.4 $ |
| 8 |
|
|
| 9 |
|
Copyright (c) Insight Software Consortium. All rights reserved. |
| 10 |
|
See ITKCopyright.txt or http://www.itk.org/HTML/Copyright.htm for details. |
| 11 |
|
|
| 12 |
|
This software is distributed WITHOUT ANY WARRANTY; without even |
| 13 |
|
the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR |
| 14 |
|
PURPOSE. See the above copyright notices for more information. |
| 15 |
|
|
| 16 |
|
=========================================================================*/ |
| 17 |
DEF |
#ifndef _itkProgressReporter_h |
| 18 |
DEF |
#define _itkProgressReporter_h |
| 19 |
|
|
| 20 |
|
#include "itkProcessObject.h" |
| 21 |
|
|
| 22 |
|
namespace itk |
| 23 |
|
{ |
| 24 |
|
|
| 25 |
|
/** \class ProgressReporter |
| 26 |
|
* \brief Implements progress tracking for a filter. |
| 27 |
|
* |
| 28 |
|
* This is a utility class for use by filter implementations in |
| 29 |
|
* GenerateData() and ThreadedGenerateData(). |
| 30 |
|
* |
| 31 |
|
* The class constructor sets the progress to 0, and the destructor |
| 32 |
|
* sets it to 1. In between, there should be one call to |
| 33 |
|
* CompletedPixel() per pixel. The reporter will automatically update |
| 34 |
|
* the filter's progress at an interval resulting in the specified |
| 35 |
|
* number of updates. The default number of updates is 100. |
| 36 |
|
* |
| 37 |
|
* Example usage: |
| 38 |
|
* |
| 39 |
|
* \code |
| 40 |
|
* ProgressReporter progress(this, threadId, |
| 41 |
|
* threadRegion.GetNumberOfPixels(), 100); |
| 42 |
|
* for( each pixel ) |
| 43 |
|
* { |
| 44 |
IND |
** ... |
| 45 |
IND |
** progress.CompletedPixel(); |
| 46 |
IND |
** } |
| 47 |
|
* \endcode |
| 48 |
|
* |
| 49 |
|
* When used in a non-threaded filter, the threadId argument should be 0. |
| 50 |
|
* |
| 51 |
|
* \sa |
| 52 |
|
* This class is a tool for filter implementers to equip a filter to |
| 53 |
|
* report on its progress. For information on how to acquire this |
| 54 |
|
* progress information, see: |
| 55 |
|
* - ProcessObject::ReportProgress() |
| 56 |
|
* - Object::AddObserver() |
| 57 |
|
*/ |
| 58 |
|
class ITKCommon_EXPORT ProgressReporter |
| 59 |
|
{ |
| 60 |
|
public: |
| 61 |
|
/** Constructor sets progress to 0 because the filter is starting. */ |
| 62 |
|
ProgressReporter(ProcessObject* filter, int threadId, |
| 63 |
|
unsigned long numberOfPixels, |
| 64 |
|
unsigned long numberOfUpdates = 100, |
| 65 |
|
float initialProgress = 0.0f, |
| 66 |
|
float progressWeight = 1.0f ); |
| 67 |
|
|
| 68 |
|
/** Destructor sets progress to 1 because the filter has finished. */ |
| 69 |
|
~ProgressReporter(); |
| 70 |
|
|
| 71 |
|
/** Called by a filter once per pixel. */ |
| 72 |
|
void CompletedPixel() |
| 73 |
|
{ |
| 74 |
|
// Inline implementation for efficiency. |
| 75 |
|
if(--m_PixelsBeforeUpdate == 0) |
| 76 |
|
{ |
| 77 |
|
m_PixelsBeforeUpdate = m_PixelsPerUpdate; |
| 78 |
|
m_CurrentPixel += m_PixelsPerUpdate; |
| 79 |
|
// only thread 0 should update the progress of the filter |
| 80 |
|
if (m_ThreadId == 0) |
| 81 |
|
{ |
| 82 |
|
m_Filter->UpdateProgress( |
| 83 |
LEN |
m_CurrentPixel * m_InverseNumberOfPixels * m_ProgressWeight + m_InitialProgress); |
| 84 |
|
} |
| 85 |
|
// all threads needs to check the abort flag |
| 86 |
|
if( m_Filter->GetAbortGenerateData() ) |
| 87 |
|
{ |
| 88 |
|
throw ProcessAborted(); |
| 89 |
|
} |
| 90 |
|
} |
| 91 |
|
} |
| 92 |
|
|
| 93 |
|
protected: |
| 94 |
|
ProcessObject* m_Filter; |
| 95 |
|
int m_ThreadId; |
| 96 |
|
float m_InverseNumberOfPixels; |
| 97 |
|
unsigned long m_CurrentPixel; |
| 98 |
|
unsigned long m_PixelsPerUpdate; |
| 99 |
|
unsigned long m_PixelsBeforeUpdate; |
| 100 |
|
float m_InitialProgress; |
| 101 |
|
float m_ProgressWeight; |
| 102 |
|
|
| 103 |
|
private: |
| 104 |
|
ProgressReporter(); //purposely not implemented |
| 105 |
|
|
| 106 |
|
}; |
| 107 |
|
|
| 108 |
|
} // end namespace itk |
| 109 |
|
|
| 110 |
|
#endif |
| 111 |
|
|