ITK  6.0.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 * 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 itkSimpleFilterWatcher_h
19#define itkSimpleFilterWatcher_h
20
21#include "itkCommand.h"
22#include "itkProcessObject.h"
23#include "itkTimeProbe.h"
24#include <mutex>
25
26namespace itk
27{
68class ITKCommon_EXPORT SimpleFilterWatcher
69{
70public:
73 SimpleFilterWatcher(itk::ProcessObject * o, const char * comment = "");
74
77
81
85
88
91 const char *
93 {
94 return (m_Process ? m_Process->GetNameOfClass() : "None");
95 }
96
99 void
101 {
102 m_Quiet = true;
103 }
104 void
106 {
107 m_Quiet = false;
108 }
114 void
116 {
117 m_TestAbort = true;
118 }
119 void
121 {
122 m_TestAbort = false;
123 }
131 {
132 return m_Process.GetPointer();
133 }
134
136 void
137 SetSteps(int val)
138 {
139 m_Steps = val;
140 }
141 int
142 GetSteps() const
143 {
144 return m_Steps;
145 }
149 void
151 {
152 m_Iterations = val;
153 }
154 int
156 {
157 return m_Iterations;
158 }
163 void
164 SetQuiet(bool val)
165 {
166 m_Quiet = val;
167 }
168 bool
169 GetQuiet() const
170 {
171 return m_Quiet;
172 }
176 std::string
178 {
179 return m_Comment;
180 }
181
183 TimeProbe &
185 {
186 return m_TimeProbe;
187 }
188
189protected:
191 virtual void
193 {
194 if (m_Process)
195 {
196 const std::lock_guard<std::mutex> lockGuard(m_ProgressOutput);
197 ++m_Steps;
198 if (!m_Quiet)
199 {
200 std::cout << " | " << m_Process->GetProgress() << std::flush;
201 if ((m_Steps % 10) == 0)
202 {
203 std::cout << std::endl;
204 }
205 }
206 if (m_TestAbort)
207 {
208 if (m_Process->GetProgress() > .03)
209 {
210 m_Process->AbortGenerateDataOn();
211 }
212 }
213 }
214 }
218 void
220
222 void
224
226 void
228
230 virtual void
232 {
233 std::cout << std::endl << "-------Aborted" << std::endl << std::flush;
234 }
235
237 virtual void
239 {
240 std::cout << " #" << std::flush;
241 ++m_Iterations;
242 }
243
245 virtual void
247 {
248 m_Steps = 0;
249 m_Iterations = 0;
250 m_TimeProbe.Start();
251 std::cout << "-------- Start " << (m_Process.GetPointer() ? m_Process->GetNameOfClass() : "None") << " \""
252 << m_Comment << "\" ";
253 if (!m_Quiet)
254 {
255 if (m_Process)
256 {
257 std::cout << m_Process;
258 }
259 else
260 {
261 std::cout << "Null";
262 }
263 }
264 std::cout << (m_Quiet ? "Progress Quiet " : "Progress ") << std::flush;
265 }
269 virtual void
271 {
272 m_TimeProbe.Stop();
273 std::cout << std::endl
274 << "Filter took " << m_TimeProbe.GetMean() << " seconds." << std::endl
275 << "-------- End " << (m_Process.GetPointer() ? m_Process->GetNameOfClass() : "None") << " \""
276 << m_Comment << "\" " << std::endl;
277 if (!m_Quiet)
278 {
279 if (m_Process)
280 {
281 std::cout << m_Process;
282 }
283 else
284 {
285 std::cout << "None";
286 }
287 std::cout << std::flush;
288 }
289 if (m_Steps < 1)
290 {
291 itkExceptionMacro("Filter does not have progress.");
292 }
293 }
296private:
297 TimeProbe m_TimeProbe{};
298 int m_Steps{ 0 };
299 int m_Iterations{ 0 };
300 bool m_Quiet{ false };
301 bool m_TestAbort{ false };
302 std::string m_Comment{};
304 std::mutex m_ProgressOutput{};
305
307 CommandType::Pointer m_StartFilterCommand{};
308 CommandType::Pointer m_EndFilterCommand{};
309 CommandType::Pointer m_ProgressFilterCommand{};
310 CommandType::Pointer m_IterationFilterCommand{};
311 CommandType::Pointer m_AbortFilterCommand{};
312
313 unsigned long m_StartTag{ 0 };
314 unsigned long m_EndTag{ 0 };
315 unsigned long m_ProgressTag{ 0 };
316 unsigned long m_IterationTag{ 0 };
317 unsigned long m_AbortTag{ 0 };
318};
319} // end namespace itk
320
321#endif
The base class for all process objects (source, filters, mappers) in the Insight data processing pipe...
Simple mechanism for monitoring the pipeline events of a filter and reporting these events to std::co...
void DeepCopy(const SimpleFilterWatcher &watch)
SimpleFilterWatcher(const SimpleFilterWatcher &)
SimpleFilterWatcher(itk::ProcessObject *o, const char *comment="")
SimpleFilterWatcher & operator=(const SimpleFilterWatcher &)
A Command subclass that calls a pointer to a member function.
Definition: itkCommand.h:233
Computes the time passed between two points in code.
Definition: itkTimeProbe.h:45
The "itk" namespace contains all Insight Segmentation and Registration Toolkit (ITK) classes....