ITK  4.1.0
Insight Segmentation and Registration Toolkit
itkSimpleFilterWatcher.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 __itkSimpleFilterWatcher_h
00019 #define __itkSimpleFilterWatcher_h
00020 
00021 #include "itkCommand.h"
00022 #include "itkProcessObject.h"
00023 #include "itkTimeProbe.h"
00024 
00025 namespace itk
00026 {
00067 class ITKCommon_EXPORT SimpleFilterWatcher
00068 {
00069 public:
00072   SimpleFilterWatcher(itk::ProcessObject *o, const char *comment = "");
00073 
00075   SimpleFilterWatcher(const SimpleFilterWatcher &);
00076 
00079   SimpleFilterWatcher();
00080 
00082   void operator=(const SimpleFilterWatcher &);
00083 
00085   virtual ~SimpleFilterWatcher();
00086 
00089   const char * GetNameOfClass()
00090   {
00091     return ( m_Process.GetPointer() ? m_Process->GetNameOfClass() : "None" );
00092   }
00093 
00096   void QuietOn() { m_Quiet = true; }
00097   void QuietOff() { m_Quiet = false; }
00099 
00103   void TestAbortOn() { m_TestAbort = true; }
00104   void TestAbortOff() { m_TestAbort = false; }
00106 
00110   ProcessObject * GetProcess() { return m_Process.GetPointer(); }
00111 
00113   void SetSteps(int val) { m_Steps = val; }
00114   int GetSteps() { return m_Steps; }
00116 
00118   void SetIterations(int val) { m_Iterations = val; }
00119   int GetIterations() { return m_Iterations; }
00121 
00124   void SetQuiet(bool val) { m_Quiet = val; }
00125   bool GetQuiet() { return m_Quiet; }
00127 
00129   std::string GetComment() { return m_Comment; }
00130 
00132   TimeProbe & GetTimeProbe() { return m_TimeProbe; }
00133 protected:
00134 
00136   virtual void ShowProgress()
00137   {
00138     if ( m_Process )
00139       {
00140       m_Steps++;
00141       if ( !m_Quiet )
00142         {
00143         std::cout << " | " << m_Process->GetProgress() << std::flush;
00144         if ( ( m_Steps % 10 ) == 0 )
00145           {
00146           std::cout << std::endl;
00147           }
00148         }
00149       if ( m_TestAbort )
00150         {
00151         if ( m_Process->GetProgress() > .03 )
00152           {
00153           m_Process->AbortGenerateDataOn();
00154           }
00155         }
00156       }
00157   }
00159 
00161   virtual void ShowAbort()
00162   {
00163     std::cout << std::endl << "-------Aborted" << std::endl << std::flush;
00164   }
00165 
00167   virtual void ShowIteration()
00168   {
00169     std::cout << " #" << std::flush;
00170     m_Iterations++;
00171   }
00172 
00174   virtual void StartFilter()
00175   {
00176     m_Steps = 0;
00177     m_Iterations = 0;
00178     m_TimeProbe.Start();
00179     std::cout << "-------- Start "
00180               << ( m_Process.GetPointer() ? m_Process->GetNameOfClass() : "None" )
00181               << " \"" << m_Comment << "\" ";
00182     if ( !m_Quiet )
00183       {
00184       if ( m_Process )
00185         {
00186         std::cout << m_Process;
00187         }
00188       else
00189         {
00190         std::cout << "Null";
00191         }
00192       }
00193     std::cout << ( m_Quiet ? "Progress Quiet " : "Progress " )
00194               << std::flush;
00195   }
00197 
00199   virtual void EndFilter()
00200   {
00201     m_TimeProbe.Stop();
00202     std::cout << std::endl << "Filter took "
00203               << m_TimeProbe.GetMeanTime()
00204               << " seconds.";
00205     std::cout << std::endl
00206               << "-------- End "
00207               << ( m_Process.GetPointer() ? m_Process->GetNameOfClass() : "None" )
00208               << " \"" << m_Comment << "\" " << std::endl;
00209     if ( !m_Quiet )
00210       {
00211       if ( m_Process )
00212         {
00213         std::cout << m_Process;
00214         }
00215       else
00216         {
00217         std::cout << "None";
00218         }
00219       std::cout << std::flush;
00220       }
00221     if ( m_Steps < 1 )
00222       {
00223       itkExceptionMacro ("Filter does not have progress.");
00224       }
00225   }
00227 
00228 private:
00229   TimeProbe                   m_TimeProbe;
00230   int                         m_Steps;
00231   int                         m_Iterations;
00232   bool                        m_Quiet;
00233   bool                        m_TestAbort;
00234   std::string                 m_Comment;
00235   itk::ProcessObject::Pointer m_Process;
00236 
00237   typedef SimpleMemberCommand< SimpleFilterWatcher > CommandType;
00238   CommandType::Pointer m_StartFilterCommand;
00239   CommandType::Pointer m_EndFilterCommand;
00240   CommandType::Pointer m_ProgressFilterCommand;
00241   CommandType::Pointer m_IterationFilterCommand;
00242   CommandType::Pointer m_AbortFilterCommand;
00243 
00244   unsigned long m_StartTag;
00245   unsigned long m_EndTag;
00246   unsigned long m_ProgressTag;
00247   unsigned long m_IterationTag;
00248   unsigned long m_AbortTag;
00249 };
00250 } // end namespace itk
00251 
00252 #endif
00253