ITK  4.13.0
Insight Segmentation and Registration Toolkit
itkFilterWatcher.h
Go to the documentation of this file.
1 /*=========================================================================
2  *
3  * Copyright Insight Software Consortium
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0.txt
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  *
17  *=========================================================================*/
18 #ifndef itkFilterWatcher_h
19 #define itkFilterWatcher_h
20 
21 #include "itkCommand.h"
22 #include "itkProcessObject.h"
23 #include <ctime>
24 // The following class is a convenience to watch the progress of a filter
25 
27 {
28 public:
29  FilterWatcher(itk::ProcessObject* o, const char *comment="")
30  {
31  m_Start = 0; m_End = 0; m_Process = o; m_Steps = 0; m_Comment = comment;
32  m_Iterations = 0;
33  m_TestAbort = false;
34 #if defined(_COMPILER_VERSION) && (_COMPILER_VERSION == 730)
35  m_Quiet = true;
36 #else
37  m_Quiet = false;
38 #endif
44 
45  startFilterCommand = itk::SimpleMemberCommand<FilterWatcher>::New();
47  progressFilterCommand = itk::SimpleMemberCommand<FilterWatcher>::New();
48  iterationFilterCommand = itk::SimpleMemberCommand<FilterWatcher>::New();
49  abortFilterCommand = itk::SimpleMemberCommand<FilterWatcher>::New();
50 
51  startFilterCommand->SetCallbackFunction(this,
53  endFilterCommand->SetCallbackFunction(this,
55  progressFilterCommand->SetCallbackFunction(this,
57  iterationFilterCommand->SetCallbackFunction(this,
59  abortFilterCommand->SetCallbackFunction(this,
61  m_Process->AddObserver(itk::StartEvent(), startFilterCommand);
62  m_Process->AddObserver(itk::EndEvent(), endFilterCommand);
63  m_Process->AddObserver(itk::ProgressEvent(), progressFilterCommand);
64  m_Process->AddObserver(itk::IterationEvent(), iterationFilterCommand);
65  m_Process->AddObserver(itk::AbortEvent(), abortFilterCommand);
66  }
67 
68  virtual ~FilterWatcher() {}
69 
70  virtual void ShowProgress()
71  {
72  m_Steps++;
73  if (!m_Quiet)
74  {
75  std::cout << " | " << m_Process->GetProgress() << std::flush;
76  if ((m_Steps % 10) == 0)
77  {
78  std::cout << std::endl;
79  }
80  }
81  if (m_TestAbort)
82  {
83  if (m_Process->GetProgress() > .03)
84  {
85  m_Process->AbortGenerateDataOn();
86  }
87  }
88  }
89  virtual void ShowAbort()
90  {
91  std::cout << std::endl << " ABORT" << std::endl << std::flush;
92  }
93  virtual void ShowIteration()
94  {
95  std::cout << " # " << std::flush;
96  m_Iterations++;
97  }
98  virtual void StartFilter()
99  {
100  m_Steps = 0;
101  m_Iterations = 0;
102  m_Start = ::clock();
103  std::cout << "-------- Start " << m_Process->GetNameOfClass()
104  << " \"" << m_Comment << "\" "
105  << m_Process
106  << (m_Quiet ? "Progress Quiet " : "Progress ")
107  << std::flush;
108  }
109  const char *GetNameOfClass () {return m_Process->GetNameOfClass();}
110  virtual void EndFilter()
111  {
112  m_End = ::clock();
113  std::cout << std::endl << "Filter took "
114  << static_cast<double>(m_End - m_Start) / CLOCKS_PER_SEC
115  << " seconds.";
116  std::cout << std::endl << std::endl
117  << "-------- End " << m_Process->GetNameOfClass()
118  << " \"" << m_Comment << "\" "
119  << m_Process << std::flush;
120  if (m_Steps < 1)
121  {
122  itkExceptionMacro ("Filter does not have progress.");
123  }
124  }
125 
126  void QuietOn() {m_Quiet = true;}
127  void QuietOff() {m_Quiet = false;}
128  void TestAbortOn() {m_TestAbort = true;}
129  void TestAbortOff() {m_TestAbort = false;}
130 
131 protected:
132  clock_t m_Start;
133  clock_t m_End;
134  int m_Steps;
136  bool m_Quiet;
138 
139  std::string m_Comment;
141 
142 private:
143  FilterWatcher(); // Purposely not implemented
144 };
145 
146 #endif
std::string m_Comment
The base class for all process objects (source, filters, mappers) in the Insight data processing pipe...
FilterWatcher(itk::ProcessObject *o, const char *comment="")
static Pointer New()
virtual void ShowAbort()
virtual void ShowIteration()
virtual void ShowProgress()
virtual void StartFilter()
const char * GetNameOfClass()
virtual ~FilterWatcher()
itk::ProcessObject::Pointer m_Process
virtual void EndFilter()