ITK  4.2.0
Insight Segmentation and Registration Toolkit
itkSimpleFilterWatcher.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 __itkSimpleFilterWatcher_h
19 #define __itkSimpleFilterWatcher_h
20 
21 #include "itkCommand.h"
22 #include "itkProcessObject.h"
23 #include "itkTimeProbe.h"
24 
25 namespace itk
26 {
67 class ITKCommon_EXPORT SimpleFilterWatcher
68 {
69 public:
72  SimpleFilterWatcher(itk::ProcessObject *o, const char *comment = "");
73 
76 
80 
82  void operator=(const SimpleFilterWatcher &);
83 
85  virtual ~SimpleFilterWatcher();
86 
89  const char * GetNameOfClass()
90  {
91  return ( m_Process.GetPointer() ? m_Process->GetNameOfClass() : "None" );
92  }
93 
96  void QuietOn() { m_Quiet = true; }
97  void QuietOff() { m_Quiet = false; }
99 
103  void TestAbortOn() { m_TestAbort = true; }
104  void TestAbortOff() { m_TestAbort = false; }
106 
110  ProcessObject * GetProcess() { return m_Process.GetPointer(); }
111 
113  void SetSteps(int val) { m_Steps = val; }
114  int GetSteps() { return m_Steps; }
116 
118  void SetIterations(int val) { m_Iterations = val; }
119  int GetIterations() { return m_Iterations; }
121 
124  void SetQuiet(bool val) { m_Quiet = val; }
125  bool GetQuiet() { return m_Quiet; }
127 
129  std::string GetComment() { return m_Comment; }
130 
132  TimeProbe & GetTimeProbe() { return m_TimeProbe; }
133 protected:
134 
136  virtual void ShowProgress()
137  {
138  if ( m_Process )
139  {
140  m_Steps++;
141  if ( !m_Quiet )
142  {
143  std::cout << " | " << m_Process->GetProgress() << std::flush;
144  if ( ( m_Steps % 10 ) == 0 )
145  {
146  std::cout << std::endl;
147  }
148  }
149  if ( m_TestAbort )
150  {
151  if ( m_Process->GetProgress() > .03 )
152  {
153  m_Process->AbortGenerateDataOn();
154  }
155  }
156  }
157  }
159 
161  virtual void ShowAbort()
162  {
163  std::cout << std::endl << "-------Aborted" << std::endl << std::flush;
164  }
165 
167  virtual void ShowIteration()
168  {
169  std::cout << " #" << std::flush;
170  m_Iterations++;
171  }
172 
174  virtual void StartFilter()
175  {
176  m_Steps = 0;
177  m_Iterations = 0;
178  m_TimeProbe.Start();
179  std::cout << "-------- Start "
180  << ( m_Process.GetPointer() ? m_Process->GetNameOfClass() : "None" )
181  << " \"" << m_Comment << "\" ";
182  if ( !m_Quiet )
183  {
184  if ( m_Process )
185  {
186  std::cout << m_Process;
187  }
188  else
189  {
190  std::cout << "Null";
191  }
192  }
193  std::cout << ( m_Quiet ? "Progress Quiet " : "Progress " )
194  << std::flush;
195  }
197 
199  virtual void EndFilter()
200  {
201  m_TimeProbe.Stop();
202  std::cout << std::endl << "Filter took "
203  << m_TimeProbe.GetMean()
204  << " seconds.";
205  std::cout << std::endl
206  << "-------- End "
207  << ( m_Process.GetPointer() ? m_Process->GetNameOfClass() : "None" )
208  << " \"" << m_Comment << "\" " << std::endl;
209  if ( !m_Quiet )
210  {
211  if ( m_Process )
212  {
213  std::cout << m_Process;
214  }
215  else
216  {
217  std::cout << "None";
218  }
219  std::cout << std::flush;
220  }
221  if ( m_Steps < 1 )
222  {
223  itkExceptionMacro ("Filter does not have progress.");
224  }
225  }
227 
228 private:
230  int m_Steps;
232  bool m_Quiet;
234  std::string m_Comment;
236 
243 
244  unsigned long m_StartTag;
245  unsigned long m_EndTag;
246  unsigned long m_ProgressTag;
247  unsigned long m_IterationTag;
248  unsigned long m_AbortTag;
249 };
250 } // end namespace itk
251 
252 #endif
253