Medical Imaging Interaction Toolkit  2018.4.99-389bf124
Medical Imaging Interaction Toolkit
QmitkRegistrationJob.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 (DKFZ)
6 All rights reserved.
7 
8 Use of this source code is governed by a 3-clause BSD license that can be
9 found in the LICENSE file.
10 
11 ============================================================================*/
12 
13 #include "QmitkRegistrationJob.h"
14 
15 // Mitk
16 #include <mitkAlgorithmHelper.h>
17 #include <mitkImageAccessByItk.h>
18 #include <mitkImageMappingHelper.h>
22 #include <mitkUIDHelper.h>
23 
24 // Qt
25 #include <QThreadPool>
26 
27 // MatchPoint
28 #include <mapAlgorithmEvents.h>
29 #include <mapAlgorithmWrapperEvent.h>
30 #include <mapExceptionObjectMacros.h>
31 #include <mapImageRegistrationAlgorithmInterface.h>
32 #include <mapRegistrationAlgorithmInterface.h>
33 
35 {
36  return dynamic_cast<const mitk::Image *>(m_spTargetData.GetPointer());
37 };
38 
40 {
41  return dynamic_cast<const mitk::Image *>(m_spMovingData.GetPointer());
42 };
43 
44 const map::algorithm::RegistrationAlgorithmBase *QmitkRegistrationJob::GetLoadedAlgorithm() const
45 {
46  return m_spLoadedAlgorithm;
47 };
48 
49 void QmitkRegistrationJob::OnMapAlgorithmEvent(::itk::Object *, const itk::EventObject &event)
50 {
51  const map::events::AlgorithmEvent *pAlgEvent = dynamic_cast<const map::events::AlgorithmEvent *>(&event);
52  const map::events::AlgorithmIterationEvent *pIterationEvent =
53  dynamic_cast<const map::events::AlgorithmIterationEvent *>(&event);
54  const map::events::AlgorithmWrapperEvent *pWrapEvent =
55  dynamic_cast<const map::events::AlgorithmWrapperEvent *>(&event);
56  const map::events::AlgorithmResolutionLevelEvent *pLevelEvent =
57  dynamic_cast<const map::events::AlgorithmResolutionLevelEvent *>(&event);
58 
59  const map::events::InitializingAlgorithmEvent *pInitEvent =
60  dynamic_cast<const map::events::InitializingAlgorithmEvent *>(&event);
61  const map::events::StartingAlgorithmEvent *pStartEvent =
62  dynamic_cast<const map::events::StartingAlgorithmEvent *>(&event);
63  const map::events::StoppingAlgorithmEvent *pStoppingEvent =
64  dynamic_cast<const map::events::StoppingAlgorithmEvent *>(&event);
65  const map::events::StoppedAlgorithmEvent *pStoppedEvent =
66  dynamic_cast<const map::events::StoppedAlgorithmEvent *>(&event);
67  const map::events::FinalizingAlgorithmEvent *pFinalizingEvent =
68  dynamic_cast<const map::events::FinalizingAlgorithmEvent *>(&event);
69  const map::events::FinalizedAlgorithmEvent *pFinalizedEvent =
70  dynamic_cast<const map::events::FinalizedAlgorithmEvent *>(&event);
71 
72  if (pInitEvent)
73  {
74  emit AlgorithmStatusChanged(QString("Initializing algorithm ..."));
75  }
76  else if (pStartEvent)
77  {
78  emit AlgorithmStatusChanged(QString("Starting algorithm ..."));
79  }
80  else if (pStoppingEvent)
81  {
82  emit AlgorithmStatusChanged(QString("Stopping algorithm ..."));
83  }
84  else if (pStoppedEvent)
85  {
86  emit AlgorithmStatusChanged(QString("Stopped algorithm ..."));
87 
88  if (!pStoppedEvent->getComment().empty())
89  {
90  emit AlgorithmInfo(QString("Stopping condition: ") + QString::fromStdString(pStoppedEvent->getComment()));
91  }
92  }
93  else if (pFinalizingEvent)
94  {
95  emit AlgorithmStatusChanged(QString("Finalizing algorithm and results ..."));
96  }
97  else if (pFinalizedEvent)
98  {
99  emit AlgorithmStatusChanged(QString("Finalized algorithm ..."));
100  }
101  else if (pIterationEvent)
102  {
103  const IIterativeAlgorithm *pIterative =
104  dynamic_cast<const IIterativeAlgorithm *>(this->m_spLoadedAlgorithm.GetPointer());
105 
106  map::algorithm::facet::IterativeAlgorithmInterface::IterationCountType count = 0;
107  bool hasCount = false;
108 
109  if (pIterative && pIterative->hasIterationCount())
110  {
111  hasCount = true;
112  count = pIterative->getCurrentIteration();
113  }
114 
115  emit AlgorithmIterated(QString::fromStdString(pIterationEvent->getComment()), hasCount, count);
116  }
117  else if (pLevelEvent)
118  {
119  const IMultiResAlgorithm *pResAlg =
120  dynamic_cast<const IMultiResAlgorithm *>(this->m_spLoadedAlgorithm.GetPointer());
121 
122  map::algorithm::facet::MultiResRegistrationAlgorithmInterface::ResolutionLevelCountType count = 0;
123  bool hasCount = false;
124  QString info = QString::fromStdString(pLevelEvent->getComment());
125 
126  if (pResAlg && pResAlg->hasLevelCount())
127  {
128  count = pResAlg->getCurrentLevel() + 1;
129  hasCount = true;
130  info = QString("Level #") + QString::number(pResAlg->getCurrentLevel() + 1) + QString(" ") + info;
131  }
132 
133  emit LevelChanged(info, hasCount, count);
134  }
135  else if (pAlgEvent && !pWrapEvent)
136  {
137  emit AlgorithmInfo(QString::fromStdString(pAlgEvent->getComment()));
138  }
139 }
140 
141 QmitkRegistrationJob::QmitkRegistrationJob(map::algorithm::RegistrationAlgorithmBase *pAlgorithm)
142 {
143  m_MapEntity = false;
144  m_StoreReg = false;
145  m_ErrorOccured = false;
146  m_spLoadedAlgorithm = pAlgorithm;
147  m_JobName = "Unnamed RegJob";
148  m_MovingDataUID = "Missing moving UID";
149  m_TargetDataUID = "Missing target UID";
150 
151  m_spTargetMask = nullptr;
152  m_spMovingMask = nullptr;
153 
154  m_spCommand = ::itk::MemberCommand<QmitkRegistrationJob>::New();
155  m_spCommand->SetCallbackFunction(this, &QmitkRegistrationJob::OnMapAlgorithmEvent);
156  m_ObserverID = m_spLoadedAlgorithm->AddObserver(::map::events::AlgorithmEvent(), m_spCommand);
157 };
158 
160 {
161  m_spLoadedAlgorithm->RemoveObserver(m_ObserverID);
162 };
163 
165 {
166  try
167  {
170 
171  //*@TODO Data Check and failure handle
172  helper.SetData(this->m_spMovingData, this->m_spTargetData);
173  maskedHelper.SetMasks(this->m_spMovingMask, this->m_spTargetMask);
174 
175  // perform registration
177 
178  // wrap the registration in a data node
179  if (m_spResultRegistration.IsNull())
180  {
181  emit Error(QString("Error. No registration was determined. No results to store."));
182  }
183  else
184  {
186  spRegWrapper->SetRegistration(m_spResultRegistration);
187 
188  emit RegResultIsAvailable(spRegWrapper, this);
189  }
190  }
191  catch (::std::exception &e)
192  {
193  emit Error(QString("Error while registering data. Details: ") + QString::fromLatin1(e.what()));
194  }
195  catch (...)
196  {
197  emit Error(QString("Unkown error when registering data."));
198  }
199 
200  emit Finished();
201 };
mitk::Image::ConstPointer m_spMovingMask
MITKAlgorithmHelper.
void LevelChanged(QString info, bool hasLevelCount, unsigned long currentLevel)
::itk::MemberCommand< QmitkRegistrationJob >::Pointer m_spCommand
mitk::BaseData::ConstPointer m_spMovingData
::map::algorithm::facet::MultiResRegistrationAlgorithmInterface IMultiResAlgorithm
void AlgorithmInfo(QString info)
map::core::RegistrationBase::Pointer GetRegistration() const
const mitk::Image * GetMovingDataAsImage() const
mitk::NodeUIDType m_MovingDataUID
static void info(const char *fmt,...)
Definition: svm.cpp:86
::map::core::RegistrationBase::Pointer m_spResultRegistration
void SetData(const mitk::BaseData *moving, const mitk::BaseData *target)
mitk::BaseData::ConstPointer m_spTargetData
Image class for storing images.
Definition: mitkImage.h:72
const mitk::Image * GetTargetDataAsImage() const
::map::algorithm::facet::IterativeAlgorithmInterface IIterativeAlgorithm
mitk::Image::ConstPointer m_spTargetMask
mitk::NodeUIDType m_TargetDataUID
bool SetMasks(const mitk::Image *movingMask, const mitk::Image *targetMask)
::map::algorithm::RegistrationAlgorithmBase::Pointer m_spLoadedAlgorithm
MaskedAlgorithmHelper Helper class as an easy bridge to set mitk images as masks for registration alg...
const ::map::algorithm::RegistrationAlgorithmBase * GetLoadedAlgorithm() const
QmitkRegistrationJob(::map::algorithm::RegistrationAlgorithmBase *pAlgorithm)
void RegResultIsAvailable(mitk::MAPRegistrationWrapper::Pointer spResultRegistration, const QmitkRegistrationJob *pJob)
void AlgorithmIterated(QString info, bool hasIterationCount, unsigned long currentIteration)
void OnMapAlgorithmEvent(::itk::Object *, const itk::EventObject &event)
void Error(QString err)
void AlgorithmStatusChanged(QString info)