ITK  4.4.0
Insight Segmentation and Registration Toolkit
itkCommandIterationUpdate.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 __itkCommandIterationUpdate_h
19 #define __itkCommandIterationUpdate_h
20 
21 #include "itkCommand.h"
22 #include "itkWeakPointer.h"
23 
24 namespace itk {
25 
31 template < class TOptimizer >
32 class ITK_EXPORT CommandIterationUpdate : public Command
33 {
34 public:
35 
40 
41 
46 
47 
52 
57 
61  void Execute(itk::Object *caller, const itk::EventObject & event)
62  {
63  Execute( (const itk::Object *)caller, event);
64  }
65 
66  void Execute(const itk::Object *, const itk::EventObject & event)
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  std::cout << m_Optimizer->GetCurrentPosition() << std::endl;
78  }
79  else if( typeid( event ) == typeid( itk::EndEvent ) )
80  {
81  std::cout << std::endl << std::endl;
82  std::cout << "After " << m_Optimizer->GetCurrentIteration();
83  std::cout << " iterations " << std::endl;
84  std::cout << "Solution is = " << m_Optimizer->GetCurrentPosition();
85  std::cout << std::endl;
86  std::cout << "With value = " << m_Optimizer->GetValue();
87  std::cout << std::endl;
88  std::cout << "Stop condition = " << m_Optimizer->GetStopCondition();
89  std::cout << std::endl;
90  }
91 
92  }
93 
94 
98  itkTypeMacro( CommandIterationUpdate, ::itk::Command );
99 
100 
104  itkNewMacro( Self );
105 
106 
110  typedef TOptimizer OptimizerType;
111 
112 
116  void SetOptimizer( OptimizerType * optimizer )
117  {
118  m_Optimizer = optimizer;
119  m_Optimizer->AddObserver( itk::IterationEvent(), this );
120  }
122 
123 protected:
124 
129 
130 private:
131 
135  WeakPointer<OptimizerType> m_Optimizer;
136 };
137 
138 } // end namespace itk
139 
140 #endif
141