KWStyle - itkSimpleFilterWatcher.h
 
Matrix View
Description

1 /*=========================================================================
2
3   Program:   Insight Segmentation & Registration Toolkit
4   Module:    $RCSfile: itkSimpleFilterWatcher.h.html,v $
5   Language:  C++
6   Date:      $Date: 2006/01/17 19:15:47 $
7   Version:   $Revision: 1.4 $
8
9   Copyright (c) Insight Software Consortium. All rights reserved.
10   See ITKCopyright.txt or http://www.itk.org/HTML/Copyright.htm for details.
11
12      This software is distributed WITHOUT ANY WARRANTY; without even 
13      the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR 
14      PURPOSE.  See the above copyright notices for more information.
15
16 =========================================================================*/
17 DEF #ifndef _itkSimpleFilterWatcher_h
18 DEF #define _itkSimpleFilterWatcher_h
19
20 #include "itkCommand.h"
21 #include "itkProcessObject.h"
22 #include "itkTimeProbe.h"
23
24
25 namespace itk
26 {
27
28 /** \class SimpleFilterWatcher
29 LEN  * \brief Simple mechanism for monitoring the pipeline events of a filter and reporting these events to std::cout
30  *
31  * SimpleFilterWatcher provides a simple mechanism for monitoring the
32  * execution of filter.  SimpleFilterWatcher is a stack-based object
33  * which takes a pointer to a ProcessObject at constructor
34  * time. SimpleFilterWatcher creates a series of commands that are
35  * registered as observers to the specified ProcessObject. The events
36  * monitored are:
37  *
38  *      StartEvent
39  *      EndEvent
40  *      ProgressEvent
41  *      IterationEvent
42  *      AbortEvent
43  *
44  * The callbacks routines registered for these events emit a simple
45  * message to std::cout.
46  *
47  * Example of use:
48  *
49  * typedef itk::BinaryThresholdImageFilter<ImageType> FilterType;
50  * FilterType::Pointer thresholdFilter = FilterType::New();
51  *
52  * SimpleFilterWatcher watcher(thresholdFilter, "Threshold");
53  *
54  * The second argument to the constructor to SimpleFilterWatcher is an
55  * optional string that is prepended to the event messages. This
56  * allows the user to associate the emitted messages to a particular
57  * filter/variable.
58  *
59  *
60  * \todo Allow any stream object to be used for the output (not just std::cout)
61  * 
62  */
63 class ITKCommon_EXPORT SimpleFilterWatcher
64 {
65 public:
66   /** Constructor. Takes a ProcessObject to monitor and an optional
67    * comment string that is prepended to each event message. */
68   SimpleFilterWatcher(itk::ProcessObject* o, const char *comment="");
69
70   /** Copy constructor */
71   SimpleFilterWatcher(const SimpleFilterWatcher& );
72
73   /** Default constructor. Only provided so that you can have
74    * std::vectors of SimpleFilterWatchers. */
75   SimpleFilterWatcher();
76
77   /** operator=  */
78   void operator=(const SimpleFilterWatcher& );
79
80   /** Destructor. */
81   virtual ~SimpleFilterWatcher();
82
83   /** Method to get the name of the class be monitored by this
84    *  SimpleFilterWatcher */
85   const char *GetNameOfClass ()
86     {
87 IND ******return (m_Process.GetPointer() ? m_Process->GetNameOfClass() : "None");
88     }
89
90   /** Methods to control the verbosity of the messages. Quiet
91    * reporting limits the information emitted at a ProgressEvent. */
92   void QuietOn() {m_Quiet = true;};
93   void QuietOff() {m_Quiet = false;};
94
95   /** Methods to use to test the AbortEvent of the a filter. If
96    * TestAbort is on, the filter being watched will be aborted when
97    * the progress reaches 30%.*/
98   void TestAbortOn() {m_TestAbort = true;};
99   void TestAbortOff() {m_TestAbort = false;};
100
101   
102 protected:
103
104   /** Callback method to show the ProgressEvent */
105   virtual void ShowProgress()
106 IND **{
107     if (m_Process)
108       {
109       m_Steps++;
110       if (!m_Quiet)
111         {
112         std::cout << " | " << m_Process->GetProgress() << std::flush;
113         if ((m_Steps % 10) == 0)
114           {
115           std::cout << std::endl;
116           }
117         }
118       if (m_TestAbort)
119         {
120         if (m_Process->GetProgress() > .03)
121           {
122           m_Process->AbortGenerateDataOn();
123           }
124         }
125       }
126 IND **}
127
128   /** Callback method to show the AbortEvent */
129   virtual void ShowAbort()
130 IND **{
131     std::cout << std::endl << "      ABORT" << std::endl << std::flush;
132   }
133
134   /** Callback method to show the IterationEvent */
135   virtual void ShowIteration()
136   {
137     std::cout << " # " << std::flush;
138     m_Iterations++;
139   }
140
141   /** Callback method to show the StartEvent */
142   virtual void StartFilter()
143   {
144     m_Steps = 0;
145     m_Iterations = 0;
146     m_TimeProbe.Start();
147     std::cout << "-------- Start "
148               << (m_Process.GetPointer() ? m_Process->GetNameOfClass() : "None")
149 IND **************<< " \"" << m_Comment << "\" ";
150     if (!m_Quiet)
151       {
152       if (m_Process)
153         {
154         std::cout << m_Process;
155         }
156       else
157         {
158         std::cout << "Null";
159         }
160       }
161     std::cout << (m_Quiet ? "Progress Quiet " : "Progress ")
162               << std::flush;
163   }
164
165   /** Callback method to show the EndEvent */
166   virtual void EndFilter()
167   {
168     m_TimeProbe.Stop();
169     std::cout << std::endl << "Filter took "
170               << m_TimeProbe.GetMeanTime()
171               << " seconds.";
172     std::cout << std::endl
173               << "-------- End "
174               << (m_Process.GetPointer() ? m_Process->GetNameOfClass() : "None")
175 IND **************<< " \"" << m_Comment << "\" " << std::endl;
176     if (!m_Quiet)
177       {
178       if (m_Process)
179         {
180         std::cout << m_Process;
181         }
182       else
183         {
184         std::cout << "None";
185         }
186       std::cout << std::flush;
187       }
188     if (m_Steps < 1)
189       {
190       itkExceptionMacro ("Filter does not have progress.");
191       }
192     }
193
194 private:
195   TimeProbe m_TimeProbe;
196   int m_Steps;
197   int m_Iterations;
198   bool m_Quiet;
199   bool m_TestAbort;
200   std::string m_Comment;
201   itk::ProcessObject::Pointer m_Process;
202
203   typedef SimpleMemberCommand<SimpleFilterWatcher> CommandType;
204   CommandType::Pointer m_StartFilterCommand;
205   CommandType::Pointer m_EndFilterCommand;
206   CommandType::Pointer m_ProgressFilterCommand;
207   CommandType::Pointer m_IterationFilterCommand;
208   CommandType::Pointer m_AbortFilterCommand;
209
210   unsigned long m_StartTag;
211   unsigned long m_EndTag;
212   unsigned long m_ProgressTag;
213   unsigned long m_IterationTag;
214   unsigned long m_AbortTag;
215 };
216
217 // end namespace itk
218
219 #endif
220

Generated by KWStyle 1.0b on Tuesday January,17 at 02:14:46PM
© Kitware Inc.