1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141
|
/*=========================================================================
Program: Insight Segmentation & Registration Toolkit
Module: itkFilterWatcher.h
Language: C++
Date: $Date$
Version: $Revision$
Copyright (c) Insight Software Consortium. All rights reserved.
See ITKCopyright.txt or http://www.itk.org/HTML/Copyright.htm for details.
This software is distributed WITHOUT ANY WARRANTY; without even
the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
PURPOSE. See the above copyright notices for more information.
=========================================================================*/
#ifndef _itkFilterWatcher_h
#define _itkFilterWatcher_h
#include "itkCommand.h"
#include "itkProcessObject.h"
#include <time.h>
// The following class is a convenience to watch the progress of a filter
class FilterWatcher
{
public:
FilterWatcher(itk::ProcessObject* o, const char *comment="")
{
m_Start = 0; m_End = 0; m_Process = o; m_Steps = 0; m_Comment = comment;
m_TestAbort = false;
#if defined(_COMPILER_VERSION) && (_COMPILER_VERSION == 730)
m_Quiet = true;
#else
m_Quiet = false;
#endif
itk::SimpleMemberCommand<FilterWatcher>::Pointer startFilterCommand;
itk::SimpleMemberCommand<FilterWatcher>::Pointer endFilterCommand;
itk::SimpleMemberCommand<FilterWatcher>::Pointer progressFilterCommand;
itk::SimpleMemberCommand<FilterWatcher>::Pointer iterationFilterCommand;
itk::SimpleMemberCommand<FilterWatcher>::Pointer abortFilterCommand;
startFilterCommand = itk::SimpleMemberCommand<FilterWatcher>::New();
endFilterCommand = itk::SimpleMemberCommand<FilterWatcher>::New();
progressFilterCommand = itk::SimpleMemberCommand<FilterWatcher>::New();
iterationFilterCommand = itk::SimpleMemberCommand<FilterWatcher>::New();
abortFilterCommand = itk::SimpleMemberCommand<FilterWatcher>::New();
startFilterCommand->SetCallbackFunction(this,
&FilterWatcher::StartFilter);
endFilterCommand->SetCallbackFunction(this,
&FilterWatcher::EndFilter);
progressFilterCommand->SetCallbackFunction(this,
&FilterWatcher::ShowProgress);
iterationFilterCommand->SetCallbackFunction(this,
&FilterWatcher::ShowIteration);
abortFilterCommand->SetCallbackFunction(this,
&FilterWatcher::ShowAbort);
m_Process->AddObserver(itk::StartEvent(), startFilterCommand);
m_Process->AddObserver(itk::EndEvent(), endFilterCommand);
m_Process->AddObserver(itk::ProgressEvent(), progressFilterCommand);
m_Process->AddObserver(itk::IterationEvent(), iterationFilterCommand);
m_Process->AddObserver(itk::AbortEvent(), abortFilterCommand);
}
virtual ~FilterWatcher() {}
virtual void ShowProgress()
{
m_Steps++;
if (!m_Quiet)
{
std::cout << " | " << m_Process->GetProgress() << std::flush;
if ((m_Steps % 10) == 0)
{
std::cout << std::endl;
}
}
if (m_TestAbort)
{
if (m_Process->GetProgress() > .03)
{
m_Process->AbortGenerateDataOn();
}
}
}
virtual void ShowAbort()
{
std::cout << std::endl << " ABORT" << std::endl << std::flush;
}
virtual void ShowIteration()
{
std::cout << " # " << std::flush;
m_Iterations++;
}
virtual void StartFilter()
{
m_Steps = 0;
m_Iterations = 0;
m_Start = ::clock();
std::cout << "-------- Start " << m_Process->GetNameOfClass()
<< " \"" << m_Comment << "\" "
<< m_Process
<< (m_Quiet ? "Progress Quiet " : "Progress ")
<< std::flush;
}
const char *GetNameOfClass () {return m_Process->GetNameOfClass();}
virtual void EndFilter()
{
m_End = ::clock();
std::cout << std::endl << "Filter took "
<< static_cast<double>(m_End - m_Start) / CLOCKS_PER_SEC
<< " seconds.";
std::cout << std::endl << std::endl
<< "-------- End " << m_Process->GetNameOfClass()
<< " \"" << m_Comment << "\" "
<< m_Process << std::flush;
if (m_Steps < 1)
{
itkExceptionMacro ("Filter does not have progress.");
}
}
void QuietOn() {m_Quiet = true;};
void QuietOff() {m_Quiet = false;};
void TestAbortOn() {m_TestAbort = true;};
void TestAbortOff() {m_TestAbort = false;};
protected:
clock_t m_Start;
clock_t m_End;
int m_Steps;
int m_Iterations;
bool m_Quiet;
bool m_TestAbort;
std::string m_Comment;
itk::ProcessObject::Pointer m_Process;
private:
FilterWatcher(); // Purposely not implemented
};
#endif
|