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