Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018 #ifndef MITK_NON_BLOCKING_ALGORITHM_H_INCLUDED_DFARdfWN1tr
00019 #define MITK_NON_BLOCKING_ALGORITHM_H_INCLUDED_DFARdfWN1tr
00020
00021 #include <itkObjectFactory.h>
00022 #include "MitkExtExports.h"
00023 #include <itkMacro.h>
00024 #include <itkMultiThreader.h>
00025 #include <itkFastMutexLock.h>
00026 #include <itkImage.h>
00027
00028 #include "mitkCommon.h"
00029 #include "mitkPropertyList.h"
00030 #include "mitkProperties.h"
00031 #include "mitkSmartPointerProperty.h"
00032 #include "mitkDataStorage.h"
00033 #include "mitkWeakPointer.h"
00034
00035 #include "mitkImage.h"
00036 #include "mitkSurface.h"
00037
00038 #include <string>
00039 #include <stdexcept>
00040
00043 #define mitkAlgorithmNewMacro( classname ) \
00044 static Pointer New(void) { \
00045 classname* rawPtr = new classname(); \
00046 Pointer smartPtr = rawPtr; \
00047 rawPtr->UnRegister(); \
00048 rawPtr->Initialize(); \
00049 return smartPtr; \
00050 }\
00051 virtual ::itk::LightObject::Pointer CreateAnother(void) const\
00052 {\
00053 Pointer smartPtr = classname::New();\
00054 ::itk::LightObject::Pointer lightPtr = smartPtr.GetPointer();\
00055 smartPtr->Initialize(this);\
00056 return lightPtr;\
00057 }
00058
00059 namespace mitk
00060 {
00061
00073 class MitkExt_EXPORT NonBlockingAlgorithm : public itk::Object
00074 {
00075 public:
00076
00077
00078 class MitkExt_EXPORT ThreadParameters
00079 {
00080 public:
00081 itk::SmartPointer<NonBlockingAlgorithm> m_Algorithm;
00082 };
00083
00084
00085 mitkClassMacro( NonBlockingAlgorithm, itk::Object )
00086
00087 void SetDataStorage(DataStorage& storage);
00088 DataStorage* GetDataStorage();
00089
00090
00091
00093 template <typename T>
00094 void SetParameter(const char* parameter, const T& value)
00095 {
00096
00097
00098 m_Parameters->SetProperty(parameter, GenericProperty<T>::New(value) );
00099
00100 }
00101
00103 template <typename T>
00104 void SetPointerParameter(const char* parameter, const itk::SmartPointer<T>& value)
00105 {
00106
00107 m_ParameterListMutex->Lock();
00108 m_Parameters->SetProperty(parameter, SmartPointerProperty::New(value.GetPointer()) );
00109 m_ParameterListMutex->Unlock();
00110 }
00111
00112
00113
00114
00116 void SetPointerParameter(const char* parameter, BaseData* value);
00117
00119 template <typename TPixel, unsigned int VImageDimension>
00120 void SetItkImageAsMITKImagePointerParameter(const char* parameter, itk::Image<TPixel, VImageDimension>* itkImage)
00121 {
00122
00123
00124 mitk::Image::Pointer mitkImage = mitk::Image::New();
00125 mitkImage = ImportItkImage( itkImage );
00126 SetPointerParameter( parameter, mitkImage );
00127 }
00128
00130 template <typename TPixel, unsigned int VImageDimension>
00131 void SetItkImageAsMITKImagePointerParameter(const char* parameter, const itk::SmartPointer<itk::Image<TPixel, VImageDimension> >& itkImage)
00132 {
00133
00134
00135 mitk::Image::Pointer mitkImage = mitk::Image::New();
00136 mitkImage = ImportItkImage( itkImage );
00137 SetPointerParameter( parameter, mitkImage );
00138 }
00139
00140
00141
00142 template <typename T>
00143 void GetParameter(const char* parameter, T& value) const
00144 {
00145
00146
00147 BaseProperty* p = m_Parameters->GetProperty(parameter);
00148 GenericProperty<T>* gp = dynamic_cast< GenericProperty<T>* >( p );
00149 if ( gp )
00150 {
00151 value = gp->GetValue();
00152
00153 return;
00154 }
00155
00156
00157 std::string error("There is no parameter \"");
00158 error += parameter;
00159 error += '"';
00160 throw std::invalid_argument( error );
00161 }
00162
00163 template <typename T>
00164 void GetPointerParameter(const char* parameter, itk::SmartPointer<T>& value) const
00165 {
00166
00167
00168 BaseProperty* p = m_Parameters->GetProperty(parameter);
00169 if (p)
00170 {
00171 SmartPointerProperty* spp = dynamic_cast< SmartPointerProperty* >( p );
00172 if ( spp )
00173 {
00174 T* t = dynamic_cast<T*>( spp->GetSmartPointer().GetPointer() );
00175 value = t;
00176
00177 return;
00178 }
00179 }
00180
00181
00182 std::string error("There is no parameter \"");
00183 error += parameter;
00184 error += '"';
00185 throw std::invalid_argument( error );
00186 }
00187
00188
00189
00190 virtual void Reset();
00191
00192 void StartAlgorithm();
00193
00194 void StartBlockingAlgorithm();
00195 void StopAlgorithm();
00196
00197 void TriggerParameterModified(const itk::EventObject&);
00198
00199 void ThreadedUpdateSuccessful(const itk::EventObject&);
00200 void ThreadedUpdateFailed(const itk::EventObject&);
00201
00202 protected:
00203
00204 NonBlockingAlgorithm();
00205 virtual ~NonBlockingAlgorithm();
00206
00207 void DefineTriggerParameter(const char*);
00208 void UnDefineTriggerParameter(const char*);
00209
00210 virtual void Initialize(const NonBlockingAlgorithm* other = NULL);
00211 virtual bool ReadyToRun();
00212
00213 virtual bool ThreadedUpdateFunction();
00214 virtual void ThreadedUpdateSuccessful();
00215 virtual void ThreadedUpdateFailed();
00216
00217 PropertyList::Pointer m_Parameters;
00218
00219 WeakPointer<DataStorage> m_DataStorage;
00220
00221 private:
00222
00223 static ITK_THREAD_RETURN_TYPE StaticNonBlockingAlgorithmThread(void* param);
00224
00225 typedef std::map< std::string, unsigned long > MapTypeStringUInt;
00226
00227 MapTypeStringUInt m_TriggerPropertyConnections;
00228
00229 itk::FastMutexLock::Pointer m_ParameterListMutex;
00230
00231 int m_ThreadID;
00232 int m_UpdateRequests;
00233 ThreadParameters m_ThreadParameters;
00234 itk::MultiThreader::Pointer m_MultiThreader;
00235
00236 bool m_KillRequest;
00237
00238 };
00239
00240 }
00241
00242 #include "mitkNonBlockingAlgorithmEvents.h"
00243
00244 #endif
00245
00246