ITK  4.1.0
Insight Segmentation and Registration Toolkit
itkProgressReporter.h
Go to the documentation of this file.
00001 /*=========================================================================
00002  *
00003  *  Copyright Insight Software Consortium
00004  *
00005  *  Licensed under the Apache License, Version 2.0 (the "License");
00006  *  you may not use this file except in compliance with the License.
00007  *  You may obtain a copy of the License at
00008  *
00009  *         http://www.apache.org/licenses/LICENSE-2.0.txt
00010  *
00011  *  Unless required by applicable law or agreed to in writing, software
00012  *  distributed under the License is distributed on an "AS IS" BASIS,
00013  *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
00014  *  See the License for the specific language governing permissions and
00015  *  limitations under the License.
00016  *
00017  *=========================================================================*/
00018 #ifndef __itkProgressReporter_h
00019 #define __itkProgressReporter_h
00020 
00021 #include "itkIntTypes.h"
00022 #include "itkProcessObject.h"
00023 
00024 namespace itk
00025 {
00060 class ITKCommon_EXPORT ProgressReporter
00061 {
00062 public:
00064   ProgressReporter(ProcessObject *filter, ThreadIdType threadId,
00065                    SizeValueType numberOfPixels,
00066                    SizeValueType numberOfUpdates = 100,
00067                    float initialProgress = 0.0f,
00068                    float progressWeight  = 1.0f);
00069 
00071   ~ProgressReporter();
00072 
00074   void CompletedPixel()
00075   {
00076     // Inline implementation for efficiency.
00077     if ( --m_PixelsBeforeUpdate == 0 )
00078       {
00079       m_PixelsBeforeUpdate = m_PixelsPerUpdate;
00080       m_CurrentPixel += m_PixelsPerUpdate;
00081       // only thread 0 should update the progress of the filter
00082       if ( m_ThreadId == 0 )
00083         {
00084         m_Filter->UpdateProgress(
00085           m_CurrentPixel * m_InverseNumberOfPixels * m_ProgressWeight + m_InitialProgress);
00086         }
00087       // all threads needs to check the abort flag
00088       if ( m_Filter->GetAbortGenerateData() )
00089         {
00090         std::string    msg;
00091         ProcessAborted e(__FILE__, __LINE__);
00092         msg += "Object " + std::string( m_Filter->GetNameOfClass() ) + ": AbortGenerateDataOn";
00093         e.SetDescription(msg);
00094         throw e;
00095         }
00096       }
00097   }
00099 
00100 protected:
00101   ProcessObject *m_Filter;
00102   ThreadIdType   m_ThreadId;
00103   float          m_InverseNumberOfPixels;
00104   SizeValueType  m_CurrentPixel;
00105   SizeValueType  m_PixelsPerUpdate;
00106   SizeValueType  m_PixelsBeforeUpdate;
00107   float          m_InitialProgress;
00108   float          m_ProgressWeight;
00109 private:
00110   ProgressReporter(); //purposely not implemented
00111 };
00112 } // end namespace itk
00113 
00114 #endif
00115