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