ITK 6.0.0
Insight Toolkit
 
Loading...
Searching...
No Matches
rtkIterationCommands.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 rtkIterationCommands_h
20#define rtkIterationCommands_h
21
22#include <itkImageFileWriter.h>
23
24namespace rtk
25{
26
37template <typename TCaller>
38class ITK_TEMPLATE_EXPORT IterationCommand : public itk::Command
39{
40public:
45
46 itkSetMacro(TriggerEvery, unsigned int);
47
48 void
49 Execute(itk::Object * caller, const itk::EventObject & event) override
50 {
51 Execute((const itk::Object *)caller, event);
52 }
53
54 void
55 Execute(const itk::Object * caller, const itk::EventObject & event) override
56 {
57 const auto * cCaller = dynamic_cast<const TCaller *>(caller);
58 if (cCaller)
59 {
60 if (itk::EndEvent().CheckEvent(&event))
61 {
62 End(cCaller);
63 return;
64 }
65 if (itk::IterationEvent().CheckEvent(&event))
66 {
69 {
70 Run(cCaller);
71 }
72 }
73 } // TODO fail when cast fails
74 }
75
76protected:
78 unsigned int m_IterationCount = 0;
79
81 unsigned int m_TriggerEvery = 1;
82
84 virtual void
85 Run(const TCaller * caller) = 0;
86
88 virtual void
89 End(const TCaller * itkNotUsed(caller))
90 { /* Default implementation: do nothing */
91 }
92};
93
102template <typename TCaller>
103class ITK_TEMPLATE_EXPORT VerboseIterationCommand : public IterationCommand<TCaller>
104{
105public:
110 itkNewMacro(Self);
111
112protected:
113 void
114 Run(const TCaller * itkNotUsed(caller)) override
115 {
116 // TODO allow string personnalization?
117 std::cout << "\rIteration " << this->m_IterationCount << " completed." << std::flush;
118 }
119
120 void
121 End(const TCaller * itkNotUsed(caller)) override
122 {
123 std::cout << std::endl; // new line when execution ends
124 }
125};
126
138template <typename TCaller, typename TOutputImage>
139class ITK_TEMPLATE_EXPORT OutputIterationCommand : public IterationCommand<TCaller>
140{
141public:
146 itkNewMacro(Self);
147
148 itkSetMacro(FileFormat, std::string);
149
150protected:
152 std::string m_FileFormat;
153
154 void
155 Run(const TCaller * caller) override
156 {
157 using WriterType = itk::ImageFileWriter<TOutputImage>;
158 auto writer = WriterType::New();
159
160 char buffer[1024];
161#if defined(__GNUC__) || defined(__clang__)
162# pragma GCC diagnostic push
163# pragma GCC diagnostic ignored "-Wformat-nonliteral"
164#endif
165 unsigned int size = snprintf(buffer, 1024, m_FileFormat.c_str(), this->m_IterationCount);
166#if defined(__GNUC__) || defined(__clang__)
167# pragma GCC diagnostic pop
168#endif
169 writer->SetFileName(std::string(buffer, size));
170
171 writer->SetInput(caller->GetOutput());
172 writer->Update();
173 }
174};
175
176} // end namespace rtk
177
178#endif
Superclass for callback/observer methods.
Definition itkCommand.h:46
Abstraction of the Events used to communicating among filters and with GUIs.
Writes image data to a single file.
Base class for most ITK classes.
Definition itkObject.h:62
Implements transparent reference counting.
Abstract superclass to all iteration callbacks. Derived classes must implement the Run() method....
itk::SmartPointer< Self > Pointer
void Execute(itk::Object *caller, const itk::EventObject &event) override
virtual void Run(const TCaller *caller)=0
virtual void End(const TCaller *caller)
void Execute(const itk::Object *caller, const itk::EventObject &event) override
Output intermediate iterations in a file. This class is useful to check convergence of an iterative m...
IterationCommand< TCaller > Superclass
void Run(const TCaller *caller) override
itk::SmartPointer< Self > Pointer
Outputs to standard output when an iteration completes.
itk::SmartPointer< Self > Pointer
IterationCommand< TCaller > Superclass
void End(const TCaller *caller) override
void Run(const TCaller *caller) override
#define itkSetMacro(name, type)