Slicer  5.1
Slicer is a multi-platform, free and open source software package for visualization and medical image computing
itkPluginFilterWatcher.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  * https://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 itkPluginFilterWatcher_h
19 #define itkPluginFilterWatcher_h
20 
21 // ModuleDescriptionParser includes
22 #include <ModuleProcessInformation.h>
23 
24 // ITK includes
25 #include <itkSimpleFilterWatcher.h>
26 
27 namespace itk
28 {
29 
35 class PluginFilterWatcher: public SimpleFilterWatcher
36 {
37 public:
38  PluginFilterWatcher(itk::ProcessObject* o,
39  const char *comment="",
40  ModuleProcessInformation *inf=nullptr,
41  double fraction = 1.0,
42  double start = 0.0)
43  : SimpleFilterWatcher(o, comment)
44  {
46  m_Fraction = fraction;
47  m_Start = start;
48  }
49 
50 protected:
51 
52 //-----------------------------------------------------------------------------
54 void ShowProgress() override
55 {
56  if (this->GetProcess())
57  {
58  this->SetSteps( this->GetSteps()+1 );
59  if (!this->GetQuiet())
60  {
62  {
63  strncpy(m_ProcessInformation->ProgressMessage,
64  this->GetComment().c_str(), 1023);
65  m_ProcessInformation->Progress =
66  (this->GetProcess()->GetProgress() * m_Fraction + m_Start);
67  if (m_Fraction != 1.0)
68  {
69  m_ProcessInformation->StageProgress = this->GetProcess()->GetProgress();
70  }
71 
72  try
73  {
74  this->GetTimeProbe().Stop();
75  m_ProcessInformation->ElapsedTime
76  = this->GetTimeProbe().GetMean()
77  * this->GetTimeProbe().GetNumberOfStops();
78  }
79  catch(...)
80  {
81  // ignore time probe exceptions
82  }
83  this->GetTimeProbe().Start();
84 
85  if (m_ProcessInformation->Abort)
86  {
87  this->GetProcess()->AbortGenerateDataOn();
88  m_ProcessInformation->Progress = 0;
89  m_ProcessInformation->StageProgress = 0;
90  }
91 
92  if (m_ProcessInformation->ProgressCallbackFunction
93  && m_ProcessInformation->ProgressCallbackClientData)
94  {
95  (*(m_ProcessInformation->ProgressCallbackFunction))(m_ProcessInformation->ProgressCallbackClientData);
96  }
97  }
98  else
99  {
100  std::cout << "<filter-progress>"
101  << (this->GetProcess()->GetProgress() * m_Fraction) + m_Start
102  << "</filter-progress>"
103  << std::endl;
104  if (m_Fraction != 1.0)
105  {
106  std::cout << "<filter-stage-progress>"
107  << this->GetProcess()->GetProgress()
108  << "</filter-stage-progress>"
109  << std::endl;
110  }
111  std::cout << std::flush;
112  }
113  }
114  }
115 }
116 
117 //-----------------------------------------------------------------------------
119 void StartFilter() override
120 {
121  this->SetSteps(0);
122  this->SetIterations(0);
123  this->GetTimeProbe().Start();
124  if (!this->GetQuiet())
125  {
127  {
128  m_ProcessInformation->Progress = 0;
129  m_ProcessInformation->StageProgress = 0;
130  strncpy(m_ProcessInformation->ProgressMessage,
131  this->GetComment().c_str(), 1023);
132 
133  if (m_ProcessInformation->ProgressCallbackFunction
134  && m_ProcessInformation->ProgressCallbackClientData)
135  {
136  (*(m_ProcessInformation->ProgressCallbackFunction))(m_ProcessInformation->ProgressCallbackClientData);
137  }
138  }
139  else
140  {
141  std::cout << "<filter-start>"
142  << std::endl;
143  std::cout << "<filter-name>"
144  << (this->GetProcess()
145  ? this->GetProcess()->GetNameOfClass() : "None")
146  << "</filter-name>"
147  << std::endl;
148  std::cout << "<filter-comment>"
149  << " \"" << this->GetComment() << "\" "
150  << "</filter-comment>"
151  << std::endl;
152  std::cout << "</filter-start>"
153  << std::endl;
154  std::cout << std::flush;
155  }
156  }
157 }
158 
159 //-----------------------------------------------------------------------------
161 void EndFilter() override
162 {
163  this->GetTimeProbe().Stop();
164  if (!this->GetQuiet())
165  {
167  {
168  m_ProcessInformation->Progress = 0;
169  m_ProcessInformation->StageProgress = 0;
170 
171  m_ProcessInformation->ElapsedTime
172  = this->GetTimeProbe().GetMean()
173  * this->GetTimeProbe().GetNumberOfStops();
174 
175  if (m_ProcessInformation->ProgressCallbackFunction
176  && m_ProcessInformation->ProgressCallbackClientData)
177  {
178  (*(m_ProcessInformation->ProgressCallbackFunction))(m_ProcessInformation->ProgressCallbackClientData);
179  }
180  }
181  else
182  {
183  std::cout << "<filter-end>"
184  << std::endl;
185  std::cout << "<filter-name>"
186  << (this->GetProcess()
187  ? this->GetProcess()->GetNameOfClass() : "None")
188  << "</filter-name>"
189  << std::endl;
190  std::cout << "<filter-time>"
191  << this->GetTimeProbe().GetMean()
192  << "</filter-time>"
193  << std::endl;
194  std::cout << "</filter-end>";
195  std::cout << std::flush;
196  }
197  }
198 }
199 
200 
201  ModuleProcessInformation *m_ProcessInformation;
202 
203  double m_Fraction;
204  double m_Start;
205 };
206 
207 } // end namespace itk
208 
209 #endif
Simplified inverse ITK transforms.
PluginFilterWatcher(itk::ProcessObject *o, const char *comment="", ModuleProcessInformation *inf=nullptr, double fraction=1.0, double start=0.0)
ModuleProcessInformation * m_ProcessInformation
Simple mechanism for monitoring the pipeline events of a filter and reporting these events to std::co...