Medical Imaging Interaction Toolkit  2016.11.0
Medical Imaging Interaction Toolkit
mitkRigidRegistrationObserver.cpp
Go to the documentation of this file.
1 /*===================================================================
2 
3 The Medical Imaging Interaction Toolkit (MITK)
4 
5 Copyright (c) German Cancer Research Center,
6 Division of Medical and Biological Informatics.
7 All rights reserved.
8 
9 This software is distributed WITHOUT ANY WARRANTY; without
10 even the implied warranty of MERCHANTABILITY or FITNESS FOR
11 A PARTICULAR PURPOSE.
12 
13 See LICENSE.txt or http://www.mitk.org for details.
14 
15 ===================================================================*/
16 
18 #include "mitkProgressBar.h"
19 
20 mitk::RigidRegistrationObserver::RigidRegistrationObserver() : m_OptimizerValue(0), m_StopOptimization(false)
21 {
22 }
23 
25 {
26  unsigned int iteration = 0;
27 
28  itk::RegularStepGradientDescentBaseOptimizer *rsgdbase =
29  dynamic_cast<itk::RegularStepGradientDescentBaseOptimizer *>(optimizer);
30  if (rsgdbase != nullptr)
31  {
32  m_OptimizerValue = rsgdbase->GetValue();
33  m_Params = rsgdbase->GetCurrentPosition();
34  iteration = rsgdbase->GetCurrentIteration();
35 
36  MITK_INFO << "(" << iteration << ") " << m_OptimizerValue << " :: " << m_Params << std::endl;
37 
38  if (this->m_StopOptimization)
39  {
40  rsgdbase->StopOptimization();
41  m_StopOptimization = false;
42  }
43  return;
44  }
45 
46  itk::GradientDescentOptimizer *gdbase = dynamic_cast<itk::GradientDescentOptimizer *>(optimizer);
47  if (gdbase != nullptr)
48  {
49  m_OptimizerValue = gdbase->GetValue();
50  m_Params = gdbase->GetCurrentPosition();
51  iteration = gdbase->GetCurrentIteration();
52 
53  MITK_INFO << "(" << iteration << ") " << m_OptimizerValue << " :: " << m_Params << std::endl;
54 
55  if (this->m_StopOptimization)
56  {
57  gdbase->StopOptimization();
58  m_StopOptimization = false;
59  }
60  return;
61  }
62 
63  itk::PowellOptimizer *powbase = dynamic_cast<itk::PowellOptimizer *>(optimizer);
64  if (powbase != nullptr)
65  {
66  m_OptimizerValue = powbase->GetValue();
67  m_Params = powbase->GetCurrentPosition();
68  iteration = powbase->GetCurrentIteration();
69 
70  MITK_INFO << "(" << iteration << ") " << m_OptimizerValue << " :: " << m_Params << std::endl;
71 
72  if (this->m_StopOptimization)
73  {
74  powbase->StopOptimization();
75  m_StopOptimization = false;
76  }
77  return;
78  }
79 
80  itk::OnePlusOneEvolutionaryOptimizer *opluso = dynamic_cast<itk::OnePlusOneEvolutionaryOptimizer *>(optimizer);
81  if (opluso != nullptr)
82  {
83  m_OptimizerValue = opluso->GetValue();
84  m_Params = opluso->GetCurrentPosition();
85  iteration = opluso->GetCurrentIteration();
86 
87  MITK_INFO << "(" << iteration << ") " << m_OptimizerValue << " :: " << m_Params << std::endl;
88 
89  if (this->m_StopOptimization)
90  {
91  powbase->StopOptimization();
92  m_StopOptimization = false;
93  }
94  return;
95  }
96 }
97 
98 void mitk::RigidRegistrationObserver::Execute(itk::Object *caller, const itk::EventObject &event)
99 {
100  if (typeid(event) == typeid(itk::IterationEvent))
101  {
102  OptimizerPointer optimizer = dynamic_cast<OptimizerPointer>(caller);
103 
104  if (optimizer != nullptr)
105  {
106  this->HandleOptimizationIterationEvent(optimizer);
107 
108  InvokeEvent(itk::ModifiedEvent());
109  }
110  }
111  else if (typeid(event) == typeid(itk::FunctionEvaluationIterationEvent))
112  {
113  OptimizerPointer optimizer = dynamic_cast<OptimizerPointer>(caller);
114  itk::AmoebaOptimizer *AmoebaOptimizer = dynamic_cast<itk::AmoebaOptimizer *>(optimizer);
115  if (AmoebaOptimizer != nullptr)
116  {
117  m_OptimizerValue = AmoebaOptimizer->GetCachedValue();
118  MITK_INFO << AmoebaOptimizer->GetCachedValue() << " " << AmoebaOptimizer->GetCachedCurrentPosition()
119  << std::endl;
120  m_Params = AmoebaOptimizer->GetCachedCurrentPosition();
121  if (m_StopOptimization)
122  {
123  // AmoebaOptimizer->StopOptimization();
124  m_StopOptimization = false;
125  }
126  InvokeEvent(itk::ModifiedEvent());
127  }
128  }
131 }
132 
133 void mitk::RigidRegistrationObserver::Execute(const itk::Object * /*caller*/, const itk::EventObject & /*event*/)
134 {
135 }
136 
138 {
140 }
141 
143 {
145 }
146 
148 {
149  return m_OptimizerValue;
150 }
151 
153 {
154  return m_Params;
155 }
156 
157 // Sets the stop optimization flag, which is used to call the StopOptimization() method of the optimizer.
158 // Unfortunately it is not implemented for ExhaustiveOptimizer, LBFGSBOptimizer, AmoebaOptimizer,
159 // ConjugateGradientOptimizer and LBFGSOptimizer in ITK.
161 {
162  m_StopOptimization = stopOptimization;
163 }
void Progress(unsigned int steps=1)
Sets the current amount of progress to current progress + steps.
#define MITK_INFO
Definition: mitkLogMacros.h:22
void AddStepsToDo(int steps)
Add new steps to the progress bar.
void HandleOptimizationIterationEvent(OptimizerType *optimizer)
static ProgressBar * GetInstance()
static method to get the GUI dependent ProgressBar-instance so the methods for steps to do and progre...
void SetRemainingProgress(int steps)
Sets the remaining progress to the progress bar when the optimization process is done.
void SetStopOptimization(bool stopOptimization)
Sets the stop optimization flag, which is used to call the StopOptimization() method of the optimizer...
double GetCurrentOptimizerValue()
Returns the current optimizer value. This value is calculated by the used metric and shows...
void AddStepsToDo(unsigned int steps)
Adds steps to totalSteps.
itk::SingleValuedNonLinearOptimizer OptimizerType
itk::Array< double > GetCurrentTranslation()
Returns the current transformation parameters for the moving image to this iteration step...
void Execute(itk::Object *caller, const itk::EventObject &event) override
Reacts on events from ITK optimizers.