ITK  5.0.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  SimpleFilterWatcher & operator=(const SimpleFilterWatcher &);
83 
85  virtual ~SimpleFilterWatcher();
86 
89  const char * GetNameOfClass()
90  {
91  return ( m_Process ? 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 
134 protected:
135 
137  virtual void ShowProgress()
138  {
139  if ( m_Process )
140  {
141  m_Steps++;
142  if ( !m_Quiet )
143  {
144  std::cout << " | " << m_Process->GetProgress() << std::flush;
145  if ( ( m_Steps % 10 ) == 0 )
146  {
147  std::cout << std::endl;
148  }
149  }
150  if ( m_TestAbort )
151  {
152  if ( m_Process->GetProgress() > .03 )
153  {
154  m_Process->AbortGenerateDataOn();
155  }
156  }
157  }
158  }
160 
162  void CreateCommands();
163 
165  void RemoveObservers();
166 
168  void DeepCopy(const SimpleFilterWatcher & watch);
169 
171  virtual void ShowAbort()
172  {
173  std::cout << std::endl << "-------Aborted" << std::endl << std::flush;
174  }
175 
177  virtual void ShowIteration()
178  {
179  std::cout << " #" << std::flush;
180  m_Iterations++;
181  }
182 
184  virtual void StartFilter()
185  {
186  m_Steps = 0;
187  m_Iterations = 0;
188  m_TimeProbe.Start();
189  std::cout << "-------- Start "
190  << ( m_Process.GetPointer() ? m_Process->GetNameOfClass() : "None" )
191  << " \"" << m_Comment << "\" ";
192  if ( !m_Quiet )
193  {
194  if ( m_Process )
195  {
196  std::cout << m_Process;
197  }
198  else
199  {
200  std::cout << "Null";
201  }
202  }
203  std::cout << ( m_Quiet ? "Progress Quiet " : "Progress " )
204  << std::flush;
205  }
207 
209  virtual void EndFilter()
210  {
211  m_TimeProbe.Stop();
212  std::cout << std::endl << "Filter took "
213  << m_TimeProbe.GetMean()
214  << " seconds.";
215  std::cout << std::endl
216  << "-------- End "
217  << ( m_Process.GetPointer() ? m_Process->GetNameOfClass() : "None" )
218  << " \"" << m_Comment << "\" " << std::endl;
219  if ( !m_Quiet )
220  {
221  if ( m_Process )
222  {
223  std::cout << m_Process;
224  }
225  else
226  {
227  std::cout << "None";
228  }
229  std::cout << std::flush;
230  }
231  if ( m_Steps < 1 )
232  {
233  itkExceptionMacro ("Filter does not have progress.");
234  }
235  }
237 
238 private:
240  int m_Steps{0};
241  int m_Iterations{0};
242  bool m_Quiet{false};
243  bool m_TestAbort{false};
244  std::string m_Comment;
246 
253 
254  unsigned long m_StartTag{0};
255  unsigned long m_EndTag{0};
256  unsigned long m_ProgressTag{0};
257  unsigned long m_IterationTag{0};
258  unsigned long m_AbortTag{0};
259 };
260 } // end namespace itk
261 
262 #endif
CommandType::Pointer m_EndFilterCommand
Computes the time passed between two points in code.
Definition: itkTimeProbe.h:44
The base class for all process objects (source, filters, mappers) in the Insight data processing pipe...
CommandType::Pointer m_IterationFilterCommand
CommandType::Pointer m_ProgressFilterCommand
A Command subclass that calls a pointer to a member function.
Definition: itkCommand.h:232
CommandType::Pointer m_StartFilterCommand
itk::ProcessObject::Pointer m_Process
Simple mechanism for monitoring the pipeline events of a filter and reporting these events to std::co...
CommandType::Pointer m_AbortFilterCommand