ITK  4.1.0
Insight Segmentation and Registration Toolkit
itkFilterWatcher.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 __itkFilterWatcher_h
00019 #define __itkFilterWatcher_h
00020 
00021 #include "itkCommand.h"
00022 #include "itkProcessObject.h"
00023 #include <time.h>
00024 // The following class is a convenience  to watch the progress of a filter
00025 
00026 class FilterWatcher
00027 {
00028 public:
00029   FilterWatcher(itk::ProcessObject* o, const char *comment="")
00030   {
00031     m_Start = 0; m_End = 0; m_Process = o; m_Steps = 0; m_Comment = comment;
00032     m_TestAbort = false;
00033 #if defined(_COMPILER_VERSION) && (_COMPILER_VERSION == 730)
00034     m_Quiet = true;
00035 #else
00036     m_Quiet = false;
00037 #endif
00038     itk::SimpleMemberCommand<FilterWatcher>::Pointer startFilterCommand;
00039     itk::SimpleMemberCommand<FilterWatcher>::Pointer endFilterCommand;
00040     itk::SimpleMemberCommand<FilterWatcher>::Pointer progressFilterCommand;
00041     itk::SimpleMemberCommand<FilterWatcher>::Pointer iterationFilterCommand;
00042     itk::SimpleMemberCommand<FilterWatcher>::Pointer abortFilterCommand;
00043 
00044     startFilterCommand =    itk::SimpleMemberCommand<FilterWatcher>::New();
00045     endFilterCommand =      itk::SimpleMemberCommand<FilterWatcher>::New();
00046     progressFilterCommand = itk::SimpleMemberCommand<FilterWatcher>::New();
00047     iterationFilterCommand = itk::SimpleMemberCommand<FilterWatcher>::New();
00048     abortFilterCommand = itk::SimpleMemberCommand<FilterWatcher>::New();
00049 
00050     startFilterCommand->SetCallbackFunction(this,
00051                                             &FilterWatcher::StartFilter);
00052     endFilterCommand->SetCallbackFunction(this,
00053                                           &FilterWatcher::EndFilter);
00054     progressFilterCommand->SetCallbackFunction(this,
00055                                                &FilterWatcher::ShowProgress);
00056     iterationFilterCommand->SetCallbackFunction(this,
00057                                                &FilterWatcher::ShowIteration);
00058     abortFilterCommand->SetCallbackFunction(this,
00059                                                &FilterWatcher::ShowAbort);
00060     m_Process->AddObserver(itk::StartEvent(), startFilterCommand);
00061     m_Process->AddObserver(itk::EndEvent(), endFilterCommand);
00062     m_Process->AddObserver(itk::ProgressEvent(), progressFilterCommand);
00063     m_Process->AddObserver(itk::IterationEvent(), iterationFilterCommand);
00064     m_Process->AddObserver(itk::AbortEvent(), abortFilterCommand);
00065   }
00066 
00067   virtual ~FilterWatcher() {}
00068 
00069   virtual void ShowProgress()
00070   {
00071     m_Steps++;
00072     if (!m_Quiet)
00073       {
00074       std::cout << " | " << m_Process->GetProgress() << std::flush;
00075       if ((m_Steps % 10) == 0)
00076         {
00077         std::cout << std::endl;
00078         }
00079       }
00080     if (m_TestAbort)
00081       {
00082       if (m_Process->GetProgress() > .03)
00083         {
00084         m_Process->AbortGenerateDataOn();
00085         }
00086       }
00087   }
00088   virtual void ShowAbort()
00089   {
00090     std::cout << std::endl << "      ABORT" << std::endl << std::flush;
00091   }
00092   virtual void ShowIteration()
00093   {
00094     std::cout << " # " << std::flush;
00095     m_Iterations++;
00096   }
00097   virtual void StartFilter()
00098   {
00099     m_Steps = 0;
00100     m_Iterations = 0;
00101     m_Start = ::clock();
00102     std::cout << "-------- Start " << m_Process->GetNameOfClass()
00103               << " \"" << m_Comment << "\" "
00104               << m_Process
00105               << (m_Quiet ? "Progress Quiet " : "Progress ")
00106               << std::flush;
00107     }
00108   const char *GetNameOfClass () {return m_Process->GetNameOfClass();}
00109   virtual void EndFilter()
00110   {
00111     m_End = ::clock();
00112     std::cout << std::endl << "Filter took "
00113               << static_cast<double>(m_End - m_Start) / CLOCKS_PER_SEC
00114               << " seconds.";
00115     std::cout << std::endl << std::endl
00116               << "-------- End " << m_Process->GetNameOfClass()
00117               << " \"" << m_Comment << "\" "
00118               << m_Process << std::flush;
00119     if (m_Steps < 1)
00120       {
00121       itkExceptionMacro ("Filter does not have progress.");
00122       }
00123     }
00124 
00125   void QuietOn() {m_Quiet = true;}
00126   void QuietOff() {m_Quiet = false;}
00127   void TestAbortOn() {m_TestAbort = true;}
00128   void TestAbortOff() {m_TestAbort = false;}
00129 protected:
00130   clock_t m_Start;
00131   clock_t m_End;
00132   int     m_Steps;
00133   int     m_Iterations;
00134   bool    m_Quiet;
00135   bool    m_TestAbort;
00136 
00137   std::string                 m_Comment;
00138   itk::ProcessObject::Pointer m_Process;
00139 private:
00140   FilterWatcher(); // Purposely not implemented
00141 };
00142 
00143 #endif
00144