RTK  2.6.0
Reconstruction Toolkit
rtkProgressCommands.h
Go to the documentation of this file.
1 /*=========================================================================
2  *
3  * Copyright RTK 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  * 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 
19 #ifndef rtkProgressCommands_h
20 #define rtkProgressCommands_h
21 
22 #include <itkCommand.h>
23 
24 namespace rtk
25 {
26 
36 template <typename TCaller>
37 class ITK_TEMPLATE_EXPORT ProgressCommand : public itk::Command
38 {
39 public:
44 
45  void
46  Execute(itk::Object * caller, const itk::EventObject & event) override
47  {
48  Execute((const itk::Object *)caller, event);
49  }
50 
51  void
52  Execute(const itk::Object * caller, const itk::EventObject & event) override
53  {
54  const auto * cCaller = dynamic_cast<const TCaller *>(caller);
55  if (cCaller)
56  {
57  if (itk::EndEvent().CheckEvent(&event))
58  {
59  End(cCaller);
60  return;
61  }
62  if (itk::ProgressEvent().CheckEvent(&event))
63  {
64  Run(cCaller);
65  }
66  } // TODO fail when cast fails
67  }
68 
69 
70 protected:
72  virtual void
73  Run(const TCaller * caller) = 0;
74 
76  virtual void
77  End(const TCaller * itkNotUsed(caller))
78  { /* Default implementation: do nothing */
79  }
80 };
81 
90 template <typename TCaller>
91 class ITK_TEMPLATE_EXPORT PercentageProgressCommand : public ProgressCommand<TCaller>
92 {
93 public:
98  itkNewMacro(Self);
99 
100  int percentage = -1;
101 
102 protected:
103  void
104  Run(const TCaller * caller) override
105  {
106  int newPercentage = (int)(caller->GetProgress() * 100.);
107  if (newPercentage > percentage)
108  {
109  // TODO allow string personnalization?
110  std::cout << "\r" << caller->GetNameOfClass() << " " << newPercentage << "% completed." << std::flush;
111  percentage = newPercentage;
112  }
113  }
114 
115  void
116  End(const TCaller * itkNotUsed(caller)) override
117  {
118  std::cout << std::endl; // new line when execution ends
119  }
120 };
121 
122 } // end namespace rtk
123 
124 #endif
void Execute(const itk::Object *caller, const itk::EventObject &event) override
itk::SmartPointer< Self > Pointer
Outputs every time a filter progresses by at least one percent.
void End(const TCaller *) override
ProgressCommand< TCaller > Superclass
itk::SmartPointer< Self > Pointer
virtual void End(const TCaller *)
void Run(const TCaller *caller) override
PercentageProgressCommand Self
Abstract superclass to all progress callbacks. Derived classes must implement the Run() method...
void Execute(itk::Object *caller, const itk::EventObject &event) override