ITK  5.0.0
Insight Segmentation and Registration Toolkit
itkCommandIterationUpdatev4.h
Go to the documentation of this file.
1 /*=========================================================================
2  *
3  * Copyright Insight Software 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  * http://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 itkCommandIterationUpdatev4_h
19 #define itkCommandIterationUpdatev4_h
20 
21 #include "itkCommand.h"
22 #include "itkWeakPointer.h"
23 
24 namespace itk {
25 
31 template < typename TOptimizer >
33 {
34 public:
35 
40 
41 
46 
47 
52 
57 
61  void Execute(itk::Object *caller, const itk::EventObject & event) override
62  {
63  Execute( (const itk::Object *)caller, event);
64  }
65 
66  void Execute(const itk::Object *, const itk::EventObject & event) override
67  {
68  if( typeid( event ) == typeid( itk::StartEvent ) )
69  {
70  std::cout << std::endl << "Position Value";
71  std::cout << std::endl << std::endl;
72  }
73  else if( typeid( event ) == typeid( itk::IterationEvent ) )
74  {
75  std::cout << m_Optimizer->GetCurrentIteration() << " = ";
76  std::cout << m_Optimizer->GetValue();
77  if( m_PrintParameters )
78  {
79  std::cout << " : " << m_Optimizer->GetCurrentPosition();
80  }
81  std::cout << std::endl;
82  }
83  else if( typeid( event ) == typeid( itk::EndEvent ) )
84  {
85  std::cout << std::endl << std::endl;
86  std::cout << "After " << m_Optimizer->GetCurrentIteration();
87  std::cout << " iterations " << std::endl;
88  if( m_PrintParameters )
89  {
90  std::cout << " Solution is = " << m_Optimizer->GetCurrentPosition();
91  std::cout << std::endl;
92  }
93  std::cout << "Solution value = " << m_Optimizer->GetValue();
94  std::cout << std::endl;
95  std::cout << "Stop condition = " << m_Optimizer->GetStopConditionDescription();
96  std::cout << std::endl;
97  }
98 
99  }
100 
101 
105  itkTypeMacro( CommandIterationUpdatev4, ::itk::Command );
106 
107 
111  itkNewMacro( Self );
112 
113 
117  using OptimizerType = TOptimizer;
118 
119 
123  void SetOptimizer( OptimizerType * optimizer )
124  {
125  m_Optimizer = optimizer;
126  m_Optimizer->AddObserver( itk::IterationEvent(), this );
127  }
129 
133  itkSetMacro( PrintParameters, bool);
134  itkGetMacro( PrintParameters, bool);
135  itkBooleanMacro( PrintParameters );
137 
138 
139 protected:
140 
145 
146 private:
147 
152 
153  bool m_PrintParameters{ false };
154 };
155 
156 } // end namespace itk
157 
158 #endif
void Execute(itk::Object *caller, const itk::EventObject &event) override
void Execute(const itk::Object *, const itk::EventObject &event) override
void SetOptimizer(OptimizerType *optimizer)
Abstraction of the Events used to communicating among filters and with GUIs.
Implementation of the Command Pattern to be invoked every iteration.
WeakPointer< OptimizerType > m_Optimizer
class ITK_FORWARD_EXPORT Command
Definition: itkObject.h:41
Base class for most ITK classes.
Definition: itkObject.h:60
Superclass for callback/observer methods.
Definition: itkCommand.h:44