ITK  5.2.0
Insight Toolkit
itkSimpleFilterWatcher.h
Go to the documentation of this file.
1 /*=========================================================================
2  *
3  * Copyright NumFOCUS
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 
83  operator=(const SimpleFilterWatcher &);
84 
86  virtual ~SimpleFilterWatcher();
87 
90  const char *
92  {
93  return (m_Process ? m_Process->GetNameOfClass() : "None");
94  }
95 
98  void
100  {
101  m_Quiet = true;
102  }
103  void
105  {
106  m_Quiet = false;
107  }
109 
113  void
115  {
116  m_TestAbort = true;
117  }
118  void
120  {
121  m_TestAbort = false;
122  }
124 
128  ProcessObject *
130  {
131  return m_Process.GetPointer();
132  }
133 
135  void
136  SetSteps(int val)
137  {
138  m_Steps = val;
139  }
140  int
141  GetSteps() const
142  {
143  return m_Steps;
144  }
146 
148  void
149  SetIterations(int val)
150  {
151  m_Iterations = val;
152  }
153  int
155  {
156  return m_Iterations;
157  }
159 
162  void
163  SetQuiet(bool val)
164  {
165  m_Quiet = val;
166  }
167  bool
168  GetQuiet() const
169  {
170  return m_Quiet;
171  }
173 
175  std::string
177  {
178  return m_Comment;
179  }
180 
182  TimeProbe &
184  {
185  return m_TimeProbe;
186  }
187 
188 protected:
190  virtual void
192  {
193  if (m_Process)
194  {
195  m_Steps++;
196  if (!m_Quiet)
197  {
198  std::cout << " | " << m_Process->GetProgress() << std::flush;
199  if ((m_Steps % 10) == 0)
200  {
201  std::cout << std::endl;
202  }
203  }
204  if (m_TestAbort)
205  {
206  if (m_Process->GetProgress() > .03)
207  {
208  m_Process->AbortGenerateDataOn();
209  }
210  }
211  }
212  }
214 
216  void
217  CreateCommands();
218 
220  void
221  RemoveObservers();
222 
224  void
225  DeepCopy(const SimpleFilterWatcher & watch);
226 
228  virtual void
230  {
231  std::cout << std::endl << "-------Aborted" << std::endl << std::flush;
232  }
233 
235  virtual void
237  {
238  std::cout << " #" << std::flush;
239  m_Iterations++;
240  }
241 
243  virtual void
245  {
246  m_Steps = 0;
247  m_Iterations = 0;
248  m_TimeProbe.Start();
249  std::cout << "-------- Start " << (m_Process.GetPointer() ? m_Process->GetNameOfClass() : "None") << " \""
250  << m_Comment << "\" ";
251  if (!m_Quiet)
252  {
253  if (m_Process)
254  {
255  std::cout << m_Process;
256  }
257  else
258  {
259  std::cout << "Null";
260  }
261  }
262  std::cout << (m_Quiet ? "Progress Quiet " : "Progress ") << std::flush;
263  }
265 
267  virtual void
269  {
270  m_TimeProbe.Stop();
271  std::cout << std::endl << "Filter took " << m_TimeProbe.GetMean() << " seconds.";
272  std::cout << std::endl
273  << "-------- End " << (m_Process.GetPointer() ? m_Process->GetNameOfClass() : "None") << " \""
274  << m_Comment << "\" " << std::endl;
275  if (!m_Quiet)
276  {
277  if (m_Process)
278  {
279  std::cout << m_Process;
280  }
281  else
282  {
283  std::cout << "None";
284  }
285  std::cout << std::flush;
286  }
287  if (m_Steps < 1)
288  {
289  itkExceptionMacro("Filter does not have progress.");
290  }
291  }
293 
294 private:
296  int m_Steps{ 0 };
297  int m_Iterations{ 0 };
298  bool m_Quiet{ false };
299  bool m_TestAbort{ false };
300  std::string m_Comment;
302 
309 
310  unsigned long m_StartTag{ 0 };
311  unsigned long m_EndTag{ 0 };
312  unsigned long m_ProgressTag{ 0 };
313  unsigned long m_IterationTag{ 0 };
314  unsigned long m_AbortTag{ 0 };
315 };
316 } // end namespace itk
317 
318 #endif
itk::SimpleFilterWatcher::m_AbortFilterCommand
CommandType::Pointer m_AbortFilterCommand
Definition: itkSimpleFilterWatcher.h:308
itk::SimpleFilterWatcher::m_TimeProbe
TimeProbe m_TimeProbe
Definition: itkSimpleFilterWatcher.h:295
itkTimeProbe.h
itk::SimpleFilterWatcher::GetQuiet
bool GetQuiet() const
Definition: itkSimpleFilterWatcher.h:168
itk::SimpleFilterWatcher::ShowIteration
virtual void ShowIteration()
Definition: itkSimpleFilterWatcher.h:236
itk::SimpleFilterWatcher::m_Process
itk::ProcessObject::Pointer m_Process
Definition: itkSimpleFilterWatcher.h:301
itk::SimpleFilterWatcher::GetProcess
ProcessObject * GetProcess()
Definition: itkSimpleFilterWatcher.h:129
itk::SimpleMemberCommand
A Command subclass that calls a pointer to a member function.
Definition: itkCommand.h:238
itk::SimpleFilterWatcher::m_StartFilterCommand
CommandType::Pointer m_StartFilterCommand
Definition: itkSimpleFilterWatcher.h:304
itk::SmartPointer< Self >
itk::SimpleFilterWatcher::QuietOn
void QuietOn()
Definition: itkSimpleFilterWatcher.h:99
itk::SimpleFilterWatcher
Simple mechanism for monitoring the pipeline events of a filter and reporting these events to std::co...
Definition: itkSimpleFilterWatcher.h:67
itk::SimpleFilterWatcher::ShowAbort
virtual void ShowAbort()
Definition: itkSimpleFilterWatcher.h:229
itkProcessObject.h
itk::SimpleFilterWatcher::SetSteps
void SetSteps(int val)
Definition: itkSimpleFilterWatcher.h:136
itk::SimpleFilterWatcher::TestAbortOn
void TestAbortOn()
Definition: itkSimpleFilterWatcher.h:114
itk::SimpleFilterWatcher::GetSteps
int GetSteps() const
Definition: itkSimpleFilterWatcher.h:141
itk::SimpleFilterWatcher::m_Comment
std::string m_Comment
Definition: itkSimpleFilterWatcher.h:300
itk::SimpleFilterWatcher::GetNameOfClass
const char * GetNameOfClass()
Definition: itkSimpleFilterWatcher.h:91
itk::TimeProbe
Computes the time passed between two points in code.
Definition: itkTimeProbe.h:44
itk::SimpleFilterWatcher::GetIterations
int GetIterations() const
Definition: itkSimpleFilterWatcher.h:154
itk::SimpleFilterWatcher::GetComment
std::string GetComment()
Definition: itkSimpleFilterWatcher.h:176
itk::SimpleFilterWatcher::TestAbortOff
void TestAbortOff()
Definition: itkSimpleFilterWatcher.h:119
itk::SimpleFilterWatcher::StartFilter
virtual void StartFilter()
Definition: itkSimpleFilterWatcher.h:244
itk
The "itk" namespace contains all Insight Segmentation and Registration Toolkit (ITK) classes....
Definition: itkAnnulusOperator.h:24
itk::SimpleFilterWatcher::m_EndFilterCommand
CommandType::Pointer m_EndFilterCommand
Definition: itkSimpleFilterWatcher.h:305
itk::SimpleFilterWatcher::GetTimeProbe
TimeProbe & GetTimeProbe()
Definition: itkSimpleFilterWatcher.h:183
itk::ProcessObject
The base class for all process objects (source, filters, mappers) in the Insight data processing pipe...
Definition: itkProcessObject.h:138
itk::SimpleFilterWatcher::ShowProgress
virtual void ShowProgress()
Definition: itkSimpleFilterWatcher.h:191
itk::SimpleFilterWatcher::SetIterations
void SetIterations(int val)
Definition: itkSimpleFilterWatcher.h:149
itk::SimpleFilterWatcher::m_ProgressFilterCommand
CommandType::Pointer m_ProgressFilterCommand
Definition: itkSimpleFilterWatcher.h:306
itk::SimpleFilterWatcher::QuietOff
void QuietOff()
Definition: itkSimpleFilterWatcher.h:104
itk::SimpleFilterWatcher::SetQuiet
void SetQuiet(bool val)
Definition: itkSimpleFilterWatcher.h:163
itk::SimpleFilterWatcher::m_IterationFilterCommand
CommandType::Pointer m_IterationFilterCommand
Definition: itkSimpleFilterWatcher.h:307
itkCommand.h
itk::SimpleFilterWatcher::EndFilter
virtual void EndFilter()
Definition: itkSimpleFilterWatcher.h:268