Medical Imaging Interaction Toolkit  2018.4.99-389bf124
Medical Imaging Interaction Toolkit
QmitkFramesRegistrationJob.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 
14 
15 // Mitk
16 #include <mitkImageAccessByItk.h>
17 
18 // Qt
19 #include <QThreadPool>
20 
21 // Map4CTK
22 #include <mitkImageMappingHelper.h>
25 #include <mitkUIDHelper.h>
26 
27 // MatchPoint
28 #include <mapAlgorithmEvents.h>
29 #include <mapAlgorithmWrapperEvent.h>
30 #include <mapExceptionObjectMacros.h>
31 #include <mapImageRegistrationAlgorithmInterface.h>
32 #include <mapRegistrationAlgorithmInterface.h>
33 
34 const mitk::Image *QmitkFramesRegistrationJob::GetTargetDataAsImage() const
35 {
36  return dynamic_cast<const mitk::Image *>(m_spTargetData.GetPointer());
37 };
38 
39 const map::algorithm::RegistrationAlgorithmBase *QmitkFramesRegistrationJob::GetLoadedAlgorithm() const
40 {
41  return m_spLoadedAlgorithm;
42 };
43 
44 void QmitkFramesRegistrationJob::OnMapAlgorithmEvent(::itk::Object *, const itk::EventObject &event)
45 {
46  const map::events::AlgorithmEvent *pAlgEvent = dynamic_cast<const map::events::AlgorithmEvent *>(&event);
47  const map::events::AlgorithmIterationEvent *pIterationEvent =
48  dynamic_cast<const map::events::AlgorithmIterationEvent *>(&event);
49  const map::events::AlgorithmWrapperEvent *pWrapEvent =
50  dynamic_cast<const map::events::AlgorithmWrapperEvent *>(&event);
51  const map::events::AlgorithmResolutionLevelEvent *pLevelEvent =
52  dynamic_cast<const map::events::AlgorithmResolutionLevelEvent *>(&event);
53 
54  const map::events::InitializingAlgorithmEvent *pInitEvent =
55  dynamic_cast<const map::events::InitializingAlgorithmEvent *>(&event);
56  const map::events::StartingAlgorithmEvent *pStartEvent =
57  dynamic_cast<const map::events::StartingAlgorithmEvent *>(&event);
58  const map::events::StoppingAlgorithmEvent *pStoppingEvent =
59  dynamic_cast<const map::events::StoppingAlgorithmEvent *>(&event);
60  const map::events::StoppedAlgorithmEvent *pStoppedEvent =
61  dynamic_cast<const map::events::StoppedAlgorithmEvent *>(&event);
62  const map::events::FinalizingAlgorithmEvent *pFinalizingEvent =
63  dynamic_cast<const map::events::FinalizingAlgorithmEvent *>(&event);
64  const map::events::FinalizedAlgorithmEvent *pFinalizedEvent =
65  dynamic_cast<const map::events::FinalizedAlgorithmEvent *>(&event);
66  const itk::ProgressEvent *pProgressEvent = dynamic_cast<const itk::ProgressEvent *>(&event);
67  const mitk::FrameRegistrationEvent *pFrameRegEvent = dynamic_cast<const mitk::FrameRegistrationEvent *>(&event);
68  const mitk::FrameMappingEvent *pFrameMapEvent = dynamic_cast<const mitk::FrameMappingEvent *>(&event);
69 
70  if (pProgressEvent)
71  {
72  emit FrameProcessed(m_helper->GetProgress());
73  }
74  else if (pFrameRegEvent)
75  {
76  emit FrameRegistered(m_helper->GetProgress());
77  }
78  else if (pFrameMapEvent)
79  {
80  emit FrameMapped(m_helper->GetProgress());
81  }
82  else if (pInitEvent)
83  {
84  emit AlgorithmStatusChanged(QString("Initializing algorithm ..."));
85  }
86  else if (pStartEvent)
87  {
88  emit AlgorithmStatusChanged(QString("Starting algorithm ..."));
89  }
90  else if (pStoppingEvent)
91  {
92  emit AlgorithmStatusChanged(QString("Stopping algorithm ..."));
93  }
94  else if (pStoppedEvent)
95  {
96  emit AlgorithmStatusChanged(QString("Stopped algorithm ..."));
97 
98  if (!pStoppedEvent->getComment().empty())
99  {
100  emit AlgorithmInfo(QString("Stopping condition: ") + QString::fromStdString(pStoppedEvent->getComment()));
101  }
102  }
103  else if (pFinalizingEvent)
104  {
105  emit AlgorithmStatusChanged(QString("Finalizing algorithm and results ..."));
106  }
107  else if (pFinalizedEvent)
108  {
109  emit AlgorithmStatusChanged(QString("Finalized algorithm ..."));
110  }
111  else if (pIterationEvent)
112  {
113  const IIterativeAlgorithm *pIterative =
114  dynamic_cast<const IIterativeAlgorithm *>(this->m_spLoadedAlgorithm.GetPointer());
115 
116  map::algorithm::facet::IterativeAlgorithmInterface::IterationCountType count = 0;
117  bool hasCount = false;
118 
119  if (pIterative && pIterative->hasIterationCount())
120  {
121  hasCount = true;
122  count = pIterative->getCurrentIteration();
123  }
124 
125  emit AlgorithmIterated(QString::fromStdString(pIterationEvent->getComment()), hasCount, count);
126  }
127  else if (pLevelEvent)
128  {
129  const IMultiResAlgorithm *pResAlg =
130  dynamic_cast<const IMultiResAlgorithm *>(this->m_spLoadedAlgorithm.GetPointer());
131 
132  map::algorithm::facet::MultiResRegistrationAlgorithmInterface::ResolutionLevelCountType count = 0;
133  bool hasCount = false;
134  QString info = QString::fromStdString(pLevelEvent->getComment());
135 
136  if (pResAlg && pResAlg->hasLevelCount())
137  {
138  count = pResAlg->getCurrentLevel() + 1;
139  hasCount = true;
140  info = QString("Level #") + QString::number(pResAlg->getCurrentLevel() + 1) + QString(" ") + info;
141  }
142 
143  emit LevelChanged(info, hasCount, count);
144  }
145  else if (pAlgEvent && !pWrapEvent)
146  {
147  emit AlgorithmInfo(QString::fromStdString(pAlgEvent->getComment()));
148  }
149 }
150 
151 QmitkFramesRegistrationJob::QmitkFramesRegistrationJob(map::algorithm::RegistrationAlgorithmBase *pAlgorithm)
152  : m_TargetDataUID("Missing target UID"), m_spLoadedAlgorithm(pAlgorithm)
153 {
154  m_MappedName = "Unnamed RegJob";
155 
156  m_spTargetMask = nullptr;
157 
158  m_spCommand = ::itk::MemberCommand<QmitkFramesRegistrationJob>::New();
159  m_spCommand->SetCallbackFunction(this, &QmitkFramesRegistrationJob::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  {
173 
174  m_helper->Set4DImage(this->GetTargetDataAsImage());
175  m_helper->SetTargetMask(this->m_spTargetMask);
176  m_helper->SetAlgorithm(this->m_spLoadedAlgorithm);
177  m_helper->SetIgnoreList(this->m_IgnoreList);
178 
179  m_helper->SetAllowUndefPixels(this->m_allowUndefPixels);
180  m_helper->SetAllowUnregPixels(this->m_allowUnregPixels);
181  m_helper->SetErrorValue(this->m_errorValue);
182  m_helper->SetPaddingValue(this->m_paddingValue);
183  m_helper->SetInterpolatorType(this->m_InterpolatorType);
184 
185  m_helper->AddObserver(::map::events::AnyMatchPointEvent(), m_spCommand);
186  m_helper->AddObserver(::itk::ProgressEvent(), m_spCommand);
187 
188  // perform registration
189  m_spMappedImageNode = m_helper->GetRegisteredImage();
190 
191  // wrap the registration in a data node
192  if (m_spMappedImageNode.IsNull())
193  {
194  emit Error(QString("Error. No registration was determined. No results to store."));
195  }
196  else
197  {
198  emit ResultIsAvailable(m_spMappedImageNode, this);
199  }
200  }
201  catch (::std::exception &e)
202  {
203  emit Error(QString("Error while registering data. Details: ") + QString::fromLatin1(e.what()));
204  }
205  catch (...)
206  {
207  emit Error(QString("Unkown error when registering data."));
208  }
209 
210  emit Finished();
211 };
mitk::BaseData::ConstPointer m_spTargetData
void FrameRegistered(double progress)
void FrameMapped(double progress)
void AlgorithmStatusChanged(QString info)
QmitkFramesRegistrationJob(map::algorithm::RegistrationAlgorithmBase *pAlgorithm)
void AlgorithmIterated(QString info, bool hasIterationCount, unsigned long currentIteration)
static void info(const char *fmt,...)
Definition: svm.cpp:86
void LevelChanged(QString info, bool hasLevelCount, unsigned long currentLevel)
void FrameProcessed(double progress)
void Error(QString err)
const map::algorithm::RegistrationAlgorithmBase * GetLoadedAlgorithm() const
mitk::Image::ConstPointer m_spTargetMask
Image class for storing images.
Definition: mitkImage.h:72
mitk::ImageMappingInterpolator::Type m_InterpolatorType
void AlgorithmInfo(QString info)
void ResultIsAvailable(mitk::Image::Pointer spResult, const QmitkFramesRegistrationJob *pJob)
mitk::TimeFramesRegistrationHelper::IgnoreListType m_IgnoreList