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