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