ITK  4.2.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 <time.h>
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_TestAbort = false;
33 #if defined(_COMPILER_VERSION) && (_COMPILER_VERSION == 730)
34  m_Quiet = true;
35 #else
36  m_Quiet = false;
37 #endif
43 
44  startFilterCommand = itk::SimpleMemberCommand<FilterWatcher>::New();
46  progressFilterCommand = itk::SimpleMemberCommand<FilterWatcher>::New();
47  iterationFilterCommand = itk::SimpleMemberCommand<FilterWatcher>::New();
48  abortFilterCommand = itk::SimpleMemberCommand<FilterWatcher>::New();
49 
50  startFilterCommand->SetCallbackFunction(this,
52  endFilterCommand->SetCallbackFunction(this,
54  progressFilterCommand->SetCallbackFunction(this,
56  iterationFilterCommand->SetCallbackFunction(this,
58  abortFilterCommand->SetCallbackFunction(this,
60  m_Process->AddObserver(itk::StartEvent(), startFilterCommand);
61  m_Process->AddObserver(itk::EndEvent(), endFilterCommand);
62  m_Process->AddObserver(itk::ProgressEvent(), progressFilterCommand);
63  m_Process->AddObserver(itk::IterationEvent(), iterationFilterCommand);
64  m_Process->AddObserver(itk::AbortEvent(), abortFilterCommand);
65  }
66 
67  virtual ~FilterWatcher() {}
68 
69  virtual void ShowProgress()
70  {
71  m_Steps++;
72  if (!m_Quiet)
73  {
74  std::cout << " | " << m_Process->GetProgress() << std::flush;
75  if ((m_Steps % 10) == 0)
76  {
77  std::cout << std::endl;
78  }
79  }
80  if (m_TestAbort)
81  {
82  if (m_Process->GetProgress() > .03)
83  {
84  m_Process->AbortGenerateDataOn();
85  }
86  }
87  }
88  virtual void ShowAbort()
89  {
90  std::cout << std::endl << " ABORT" << std::endl << std::flush;
91  }
92  virtual void ShowIteration()
93  {
94  std::cout << " # " << std::flush;
95  m_Iterations++;
96  }
97  virtual void StartFilter()
98  {
99  m_Steps = 0;
100  m_Iterations = 0;
101  m_Start = ::clock();
102  std::cout << "-------- Start " << m_Process->GetNameOfClass()
103  << " \"" << m_Comment << "\" "
104  << m_Process
105  << (m_Quiet ? "Progress Quiet " : "Progress ")
106  << std::flush;
107  }
108  const char *GetNameOfClass () {return m_Process->GetNameOfClass();}
109  virtual void EndFilter()
110  {
111  m_End = ::clock();
112  std::cout << std::endl << "Filter took "
113  << static_cast<double>(m_End - m_Start) / CLOCKS_PER_SEC
114  << " seconds.";
115  std::cout << std::endl << std::endl
116  << "-------- End " << m_Process->GetNameOfClass()
117  << " \"" << m_Comment << "\" "
118  << m_Process << std::flush;
119  if (m_Steps < 1)
120  {
121  itkExceptionMacro ("Filter does not have progress.");
122  }
123  }
124 
125  void QuietOn() {m_Quiet = true;}
126  void QuietOff() {m_Quiet = false;}
127  void TestAbortOn() {m_TestAbort = true;}
128  void TestAbortOff() {m_TestAbort = false;}
129 protected:
130  clock_t m_Start;
131  clock_t m_End;
132  int m_Steps;
134  bool m_Quiet;
136 
137  std::string m_Comment;
139 private:
140  FilterWatcher(); // Purposely not implemented
141 };
142 
143 #endif
144