Medical Imaging Interaction Toolkit  2016.11.0
Medical Imaging Interaction Toolkit
mitkNonBlockingAlgorithm.h
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 #ifndef MITK_NON_BLOCKING_ALGORITHM_H_INCLUDED_DFARdfWN1tr
18 #define MITK_NON_BLOCKING_ALGORITHM_H_INCLUDED_DFARdfWN1tr
19 
21 #include <itkFastMutexLock.h>
22 #include <itkImage.h>
23 #include <itkMacro.h>
24 #include <itkMultiThreader.h>
25 #include <itkObjectFactory.h>
26 
27 #include "mitkCommon.h"
28 #include "mitkDataStorage.h"
29 #include "mitkProperties.h"
30 #include "mitkPropertyList.h"
32 #include "mitkWeakPointer.h"
33 
34 #include "mitkImage.h"
35 #include "mitkSurface.h"
36 
37 #include <stdexcept>
38 #include <string>
39 
43 #define mitkAlgorithmNewMacro(classname) \
44  \
45 static Pointer \
46  New(void) \
47  { \
48  classname *rawPtr = new classname(); \
49  Pointer smartPtr = rawPtr; \
50  rawPtr->UnRegister(); \
51  rawPtr->Initialize(); \
52  return smartPtr; \
53  \
54 } \
55  \
56 virtual::itk::LightObject::Pointer \
57  CreateAnother(void) const override \
58  \
59 { \
60  Pointer smartPtr = classname::New(); \
61  ::itk::LightObject::Pointer lightPtr = smartPtr.GetPointer(); \
62  smartPtr->Initialize(this); \
63  return lightPtr; \
64  \
65 }
66 
67 namespace mitk
68 {
84  {
85  public:
86  // for threading
88  {
89  public:
91  };
92 
94 
95  void SetDataStorage(DataStorage &storage);
97 
98  // parameter setting
99 
101  template <typename T>
102  void SetParameter(const char *parameter, const T &value)
103  {
104  // MITK_INFO << "SetParameter(" << parameter << ") " << typeid(T).name() << std::endl;
105  // m_ParameterListMutex->Lock();
106  m_Parameters->SetProperty(parameter, GenericProperty<T>::New(value));
107  // m_ParameterListMutex->Unlock();
108  }
109 
111  template <typename T>
112  void SetPointerParameter(const char *parameter, const itk::SmartPointer<T> &value)
113  {
114  // MITK_INFO << this << "->SetParameter smartpointer(" << parameter << ") " << typeid(itk::SmartPointer<T>).name()
115  // << std::endl;
116  m_ParameterListMutex->Lock();
117  m_Parameters->SetProperty(parameter, SmartPointerProperty::New(value.GetPointer()));
118  m_ParameterListMutex->Unlock();
119  }
120  // virtual void SetParameter( const char*, mitk::BaseProperty* ); // for "number of iterations", ...
121  // create some property observing to inform algorithm object about changes
122  // perhaps some TriggerParameter(string) macro that creates an observer for changes in a specific property like
123  // "2ndPoint" for LineAlgorithms
124 
126  void SetPointerParameter(const char *parameter, BaseData *value);
127 
129  template <typename TPixel, unsigned int VImageDimension>
130  void SetItkImageAsMITKImagePointerParameter(const char *parameter, itk::Image<TPixel, VImageDimension> *itkImage)
131  {
132  // MITK_INFO << "SetParameter ITK image(" << parameter << ") " << typeid(itk::Image<TPixel,
133  // VImageDimension>).name() << std::endl;
134  // create an MITK image for that
136  mitkImage = ImportItkImage(itkImage);
137  SetPointerParameter(parameter, mitkImage);
138  }
139 
141  template <typename TPixel, unsigned int VImageDimension>
142  void SetItkImageAsMITKImagePointerParameter(const char *parameter,
143  const itk::SmartPointer<itk::Image<TPixel, VImageDimension>> &itkImage)
144  {
145  // MITK_INFO << "SetParameter ITK image(" << parameter << ") " << typeid(itk::SmartPointer<itk::Image<TPixel,
146  // VImageDimension> >).name() << std::endl;
147  // create an MITK image for that
149  mitkImage = ImportItkImage(itkImage);
150  SetPointerParameter(parameter, mitkImage);
151  }
152 
153  // parameter getting
154 
155  template <typename T>
156  void GetParameter(const char *parameter, T &value) const
157  {
158  // MITK_INFO << "GetParameter normal(" << parameter << ") " << typeid(T).name() << std::endl;
159  // m_ParameterListMutex->Lock();
160  BaseProperty *p = m_Parameters->GetProperty(parameter);
161  GenericProperty<T> *gp = dynamic_cast<GenericProperty<T> *>(p);
162  if (gp)
163  {
164  value = gp->GetValue();
165  // m_ParameterListMutex->Unlock();
166  return;
167  }
168  // m_ParameterListMutex->Unlock();
169 
170  std::string error("There is no parameter \"");
171  error += parameter;
172  error += '"';
173  throw std::invalid_argument(error);
174  }
175 
176  template <typename T>
177  void GetPointerParameter(const char *parameter, itk::SmartPointer<T> &value) const
178  {
179  // MITK_INFO << this << "->GetParameter smartpointer(" << parameter << ") " << typeid(itk::SmartPointer<T>).name()
180  // << std::endl;
181  // m_ParameterListMutex->Lock();
182  BaseProperty *p = m_Parameters->GetProperty(parameter);
183  if (p)
184  {
185  SmartPointerProperty *spp = dynamic_cast<SmartPointerProperty *>(p);
186  if (spp)
187  {
188  T *t = dynamic_cast<T *>(spp->GetSmartPointer().GetPointer());
189  value = t;
190  // m_ParameterListMutex->Unlock();
191  return;
192  }
193  }
194  // m_ParameterListMutex->Unlock();
195 
196  std::string error("There is no parameter \"");
197  error += parameter;
198  error += '"';
199  throw std::invalid_argument(error);
200  }
201 
202  // start/stop functions
203 
204  virtual void Reset();
205 
206  void StartAlgorithm(); // for those who want to trigger calculations on their own
207  // --> need for an OPTION: manual/automatic starting
208  void StartBlockingAlgorithm(); // for those who want to trigger calculations on their own
209  void StopAlgorithm();
210 
211  void TriggerParameterModified(const itk::EventObject &);
212 
213  void ThreadedUpdateSuccessful(const itk::EventObject &);
214  void ThreadedUpdateFailed(const itk::EventObject &);
215 
216  protected:
217  NonBlockingAlgorithm(); // use smart pointers
218  virtual ~NonBlockingAlgorithm();
219 
220  void DefineTriggerParameter(const char *);
221  void UnDefineTriggerParameter(const char *);
222 
223  virtual void Initialize(const NonBlockingAlgorithm *other = nullptr);
224  virtual bool ReadyToRun();
225 
226  virtual bool ThreadedUpdateFunction(); // will be called from a thread after calling StartAlgorithm
227  virtual void ThreadedUpdateSuccessful(); // will be called after the ThreadedUpdateFunction() returned
228  virtual void ThreadedUpdateFailed(); // will when ThreadedUpdateFunction() returns false
229 
231 
233 
234  private:
235  static ITK_THREAD_RETURN_TYPE StaticNonBlockingAlgorithmThread(void *param);
236 
237  typedef std::map<std::string, unsigned long> MapTypeStringUInt;
238 
239  MapTypeStringUInt m_TriggerPropertyConnections;
240 
241  itk::FastMutexLock::Pointer m_ParameterListMutex;
242 
243  int m_ThreadID;
244  int m_UpdateRequests;
245  ThreadParameters m_ThreadParameters;
246  itk::MultiThreader::Pointer m_MultiThreader;
247 
248  bool m_KillRequest;
249  };
250 
251 } // namespace
252 
254 
255 #endif
void SetItkImageAsMITKImagePointerParameter(const char *parameter, const itk::SmartPointer< itk::Image< TPixel, VImageDimension >> &itkImage)
For any kind of ITK images (smartpointers)
itk::SmartPointer< NonBlockingAlgorithm > m_Algorithm
Data management class that handles 'was created by' relations.
itk::SmartPointer< Self > Pointer
Base of all data objects.
Definition: mitkBaseData.h:39
void SetItkImageAsMITKImagePointerParameter(const char *parameter, itk::Image< TPixel, VImageDimension > *itkImage)
For any kind of ITK images (C pointers)
DataCollection - Class to facilitate loading/accessing structured data.
static mitk::DataStorage::Pointer GetDataStorage()
WeakPointer< DataStorage > m_DataStorage
Property containing a smart-pointer.
Image::Pointer ImportItkImage(const itk::SmartPointer< ItkOutputImageType > &itkimage, const BaseGeometry *geometry=nullptr, bool update=true)
Imports an itk::Image (with a specific type) as an mitk::Image.Instantiates instance of ITKImageImpor...
#define MITKALGORITHMSEXT_EXPORT
itk::Object::Pointer GetSmartPointer() const
Abstract base class for properties.
#define mitkClassMacroItkParent(className, SuperClassName)
Definition: mitkCommon.h:53
Implements a weak reference to an object.
static Pointer New()
void SetPointerParameter(const char *parameter, const itk::SmartPointer< T > &value)
For any kind of smart pointers.
void GetPointerParameter(const char *parameter, itk::SmartPointer< T > &value) const
void GetParameter(const char *parameter, T &value) const
virtual T GetValue() const