MeVisLab Toolbox Reference
mlITKMLBaseWrapper.h
Go to the documentation of this file.
1 /*************************************************************************************
2 **
3 ** Copyright 2007, MeVis Medical Solutions AG
4 **
5 ** The user may use this file in accordance with the license agreement provided with
6 ** the Software or, alternatively, in accordance with the terms contained in a
7 ** written agreement between the user and MeVis Medical Solutions AG.
8 **
9 ** For further information use the contact form at https://www.mevislab.de/contact
10 **
11 **************************************************************************************/
12 
13 #pragma once
14 
16 #include "mlInitSystemITKSupport.h"
17 
19 #include "mlModuleIncludes.h"
20 #include <itkLightObject.h>
21 
22 ML_START_NAMESPACE
23 
24 //----------------------------------------------------------------------------------
28 //----------------------------------------------------------------------------------
30 {
31 public:
34 
35  void setITKPointer(itk::LightObject::Pointer obj)
36  {
37  _itkObject = obj;
38  }
39 
40  template<class T>
41  T::Pointer getITKPointer()
42  {
43  return dynamic_cast<T*>(_itkObject.GetPointer());
44  }
45 
46 private:
47  itk::LightObject::Pointer _itkObject;
48 
49 protected:
52 };
53 
54 
55 
56 //----------------------------------------------------------------------------------
58 //----------------------------------------------------------------------------------
59 template <typename OBJ_TYPE, typename WRAPPER_CLASS_NAME> class ITKTypedPointerWrapper
60 {
61 public:
62  //----------------------------------------------------------------------------------
64  //----------------------------------------------------------------------------------
66  {
67  }
68 
69  //----------------------------------------------------------------------------------
72  //----------------------------------------------------------------------------------
73  void addOutputBaseField(Module& module, const char* outputFieldName)
74  {
75  if (!outputFieldName) {
76  ML_PRINT_ERROR("ITKTypedPointerWrapper::addOutputBaseField", ML_BAD_POINTER_OR_0,
77  "Passed field name pointer is nullptr. Ignoring call.");
78  return;
79  }
80  if (_baseField) {
81  ML_PRINT_ERROR("ITKTypedPointerWrapper::addOutputBaseField", ML_BAD_STATE, "Ignoring multiple field add.");
82  return;
83  }
84 
85  _baseField = module.addBase(outputFieldName);
86  _baseField->addAllowedType<WRAPPER_CLASS_NAME>();
87  _baseField->setBaseValue(&_baseWrapper);
88  }
89 
90  //----------------------------------------------------------------------------------
93  //----------------------------------------------------------------------------------
94  void addInputBaseField(Module& module, const char* inputFieldName)
95  {
96  if (!inputFieldName) {
97  ML_PRINT_ERROR("ITKMLBaseWrapper::addInputBaseField", ML_BAD_POINTER_OR_0,
98  "Passed field name pointer is nullptr. Ignoring call.");
99  return;
100  }
101  if (_baseField) {
102  ML_PRINT_ERROR("ITKMLBaseWrapper::addInputBaseField", ML_BAD_STATE, "Ignoring multiple field add.");
103  return;
104  }
105 
106  _baseField = module.addBase(inputFieldName);
107  _baseField->addAllowedType<WRAPPER_CLASS_NAME>();
108  }
109 
110  //----------------------------------------------------------------------------------
112  //----------------------------------------------------------------------------------
113  void setNewOutputBaseFieldObject(OBJ_TYPE::Pointer newObject)
114  {
115  _baseWrapper.setITKPointer(newObject);
116  if (_baseField) {
117  _baseField->touch();
118  }
119  }
120 
121  //----------------------------------------------------------------------------------
123  //----------------------------------------------------------------------------------
125  {
126  return _baseField;
127  }
128 
129  //----------------------------------------------------------------------------------
131  //----------------------------------------------------------------------------------
133  {
134  return _baseField;
135  }
136 
137  //----------------------------------------------------------------------------------
139  //----------------------------------------------------------------------------------
140  OBJ_TYPE::Pointer getWrappedOutputObject()
141  {
142  return _baseWrapper.template getITKPointer<OBJ_TYPE>();
143  }
144 
145  //----------------------------------------------------------------------------------
148  //----------------------------------------------------------------------------------
149  OBJ_TYPE::Pointer getWrappedInputObject()
150  {
151 
152  if (_baseField) {
153  /* Get the pointer to the connected object and check its type.*/
154  /* If the type is valid then return a the pointer to the wrapped input object.*/
155  // Get base object from connected input.
156  WRAPPER_CLASS_NAME* wrapperBasePtr = _baseField->getTypedBaseValue<WRAPPER_CLASS_NAME*>();
157  return wrapperBasePtr ? wrapperBasePtr->template getITKPointer<OBJ_TYPE>() : nullptr;
158  }
159 
160  /* No valid object connected to base field. */
161  return nullptr;
162  }
163 
164 protected:
166  WRAPPER_CLASS_NAME _baseWrapper;
167 
169  BaseField* _baseField = nullptr;
170 };
171 
172 
173 
174 //----------------------------------------------------------------------------------
179 //----------------------------------------------------------------------------------
180 #define ML_CREATE_ITK_POINTER_WRAPPER_H(CLASS_NAME, PARENT_BASE_CLASS_NAME, ML_EXPORT_MACRO) \
181  \
182  /* Specialization of ITKWrapperBase */ \
183  class ML_EXPORT_MACRO ITK##CLASS_NAME##Wrapper : public PARENT_BASE_CLASS_NAME \
184  { \
185  public: \
186  ITK##CLASS_NAME##Wrapper() = default; \
187  \
188  private: \
189  /* The header part of the runtime type system interface. */ \
190  ML_CLASS_HEADER(ITK##CLASS_NAME##Wrapper); \
191  }; \
192  \
193  typedef ITKTypedPointerWrapper<CLASS_NAME, ITK##CLASS_NAME##Wrapper> CLASS_NAME##Wrapper; \
194 
195 
196 //----------------------------------------------------------------------------------
201 //----------------------------------------------------------------------------------
202 #define ML_CREATE_ITK_POINTER_WRAPPER_CPP(CLASS_NAME, PARENT_BASE_CLASS_NAME) \
203  \
204  /* The .cpp part of the runtime type system interface. */ \
205  ML_CLASS_SOURCE(ITK##CLASS_NAME##Wrapper, PARENT_BASE_CLASS_NAME);
206 
207 
208 ML_END_NAMESPACE
@ T
Definition: SoKeyGrabber.h:71
Field to encapsulate a pointer to an ML base object.
Definition: mlFields.h:729
void addAllowedType(const RuntimeType *allowedType)
Adds type to the list of allowed types.
Class representing general ML objects that support import/export via strings (setPersistentState() an...
Definition: mlBase.h:59
BaseField * addBase(const char *name)
Creates a Base field with name and adds it to the container. Default value is NULL.
Class to create a Base wrapper around an object of a certain template type.
BaseField * getInputBaseField()
For legacy reasons there are two fields for getting the base field.
void addInputBaseField(Module &module, const char *inputFieldName)
Method to add a base field to the passed Module module.
WRAPPER_CLASS_NAME _baseWrapper
Reference to the Base object wrapping the ITK object pointer.
OBJ_TYPE::Pointer getWrappedOutputObject()
Return the pointer to the wrapped object.
BaseField * getOutputBaseField()
For legacy reasons there are two fields for getting the base field.
void addOutputBaseField(Module &module, const char *outputFieldName)
Method to add a base field to the passed Module module.
OBJ_TYPE::Pointer getWrappedInputObject()
Return the pointer to the wrapped input object, if it is of the correct type, otherwise a nullptr.
void setNewOutputBaseFieldObject(OBJ_TYPE::Pointer newObject)
Set base field so that it points to the new object.
Helper class to have a common base class for all derived wrapper.
ML_ABSTRACT_CLASS_HEADER(ITKWrapperBase)
Register this class abstractly.
ITKWrapperBase()
Private on purpose to avoid instantiations.
void setITKPointer(itk::LightObject::Pointer obj)
T::Pointer getITKPointer()
Base class for an image processing module of the ML.
Definition: mlModule.h:151
#define ML_BAD_POINTER_OR_0
A pointer is NULL or a value is NULL or 0 where it should not be.
Definition: mlTypeDefs.h:933
#define ML_BAD_STATE
The current state of an object is not appropriate for an operation.
Definition: mlTypeDefs.h:938
#define ML_PRINT_ERROR(FUNC_NAME, REASON, HANDLING)
Like ML_PRINT_ERROR_DUMP(FUNC_NAME, REASON, HANDLING, RT_OBJ) without a runtime object to be dumped.
#define MLITK_SUPPORT_EXPORT
When included by other libraries MLITK_SUPPORT_EXPORT is compiled as import symbol.