MeVisLab Toolbox Reference
Loading...
Searching...
No Matches
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
17
19#include "mlModuleIncludes.h"
20#include <itkLightObject.h>
21
23
24//----------------------------------------------------------------------------------
28//----------------------------------------------------------------------------------
30{
31public:
34
35 void setITKPointer(itk::LightObject::Pointer obj)
36 {
37 _itkObject = obj;
38 }
39
40 template <class T> T::Pointer getITKPointer()
41 {
42 return dynamic_cast<T*>(_itkObject.GetPointer());
43 }
44
45private:
46 itk::LightObject::Pointer _itkObject;
47
48protected:
51};
52
53
54//----------------------------------------------------------------------------------
56//----------------------------------------------------------------------------------
57template <typename OBJ_TYPE, typename WRAPPER_CLASS_NAME> class ITKTypedPointerWrapper
58{
59public:
60 //----------------------------------------------------------------------------------
62 //----------------------------------------------------------------------------------
65
66 //----------------------------------------------------------------------------------
69 //----------------------------------------------------------------------------------
70 void addOutputBaseField(Module& module, const char* outputFieldName)
71 {
72 if (!outputFieldName) {
73 ML_PRINT_ERROR("ITKTypedPointerWrapper::addOutputBaseField", ML_BAD_POINTER_OR_0,
74 "Passed field name pointer is nullptr. Ignoring call.");
75 return;
76 }
77 if (_baseField) {
78 ML_PRINT_ERROR("ITKTypedPointerWrapper::addOutputBaseField", ML_BAD_STATE, "Ignoring multiple field add.");
79 return;
80 }
81
82 _baseField = module.addBase(outputFieldName);
83 _baseField->addAllowedType<WRAPPER_CLASS_NAME>();
84 _baseField->setBaseValue(&_baseWrapper);
85 }
86
87 //----------------------------------------------------------------------------------
90 //----------------------------------------------------------------------------------
91 void addInputBaseField(Module& module, const char* inputFieldName)
92 {
93 if (!inputFieldName) {
94 ML_PRINT_ERROR("ITKMLBaseWrapper::addInputBaseField", ML_BAD_POINTER_OR_0,
95 "Passed field name pointer is nullptr. Ignoring call.");
96 return;
97 }
98 if (_baseField) {
99 ML_PRINT_ERROR("ITKMLBaseWrapper::addInputBaseField", ML_BAD_STATE, "Ignoring multiple field add.");
100 return;
101 }
102
103 _baseField = module.addBase(inputFieldName);
104 _baseField->addAllowedType<WRAPPER_CLASS_NAME>();
105 }
106
107 //----------------------------------------------------------------------------------
109 //----------------------------------------------------------------------------------
111 {
112 _baseWrapper.setITKPointer(newObject);
113 if (_baseField) {
114 _baseField->touch();
115 }
116 }
117
118 //----------------------------------------------------------------------------------
120 //----------------------------------------------------------------------------------
122 {
123 return _baseField;
124 }
125
126 //----------------------------------------------------------------------------------
128 //----------------------------------------------------------------------------------
130 {
131 return _baseField;
132 }
133
134 //----------------------------------------------------------------------------------
136 //----------------------------------------------------------------------------------
137 OBJ_TYPE::Pointer getWrappedOutputObject()
138 {
139 return _baseWrapper.template getITKPointer<OBJ_TYPE>();
140 }
141
142 //----------------------------------------------------------------------------------
145 //----------------------------------------------------------------------------------
146 OBJ_TYPE::Pointer getWrappedInputObject()
147 {
148
149 if (_baseField) {
150 /* Get the pointer to the connected object and check its type.*/
151 /* If the type is valid then return a the pointer to the wrapped input object.*/
152 // Get base object from connected input.
153 WRAPPER_CLASS_NAME* wrapperBasePtr = _baseField->getTypedBaseValue<WRAPPER_CLASS_NAME*>();
154 return wrapperBasePtr ? wrapperBasePtr->template getITKPointer<OBJ_TYPE>() : nullptr;
155 }
156
157 /* No valid object connected to base field. */
158 return nullptr;
159 }
160
161protected:
164
166 BaseField* _baseField = nullptr;
167};
168
169
170//----------------------------------------------------------------------------------
175//----------------------------------------------------------------------------------
176#define ML_CREATE_ITK_POINTER_WRAPPER_H(CLASS_NAME, PARENT_BASE_CLASS_NAME, ML_EXPORT_MACRO) \
177 \
178 /* Specialization of ITKWrapperBase */ \
179 class ML_EXPORT_MACRO ITK##CLASS_NAME##Wrapper : public PARENT_BASE_CLASS_NAME \
180 { \
181 public: \
182 ITK##CLASS_NAME##Wrapper() = default; \
183 \
184 private: \
185 /* The header part of the runtime type system interface. */ \
186 ML_CLASS_HEADER(ITK##CLASS_NAME##Wrapper); \
187 }; \
188 \
189 typedef ITKTypedPointerWrapper<CLASS_NAME, ITK##CLASS_NAME##Wrapper> CLASS_NAME##Wrapper;
190
191
192//----------------------------------------------------------------------------------
197//----------------------------------------------------------------------------------
198#define ML_CREATE_ITK_POINTER_WRAPPER_CPP(CLASS_NAME, PARENT_BASE_CLASS_NAME) \
199 \
200 /* The .cpp part of the runtime type system interface. */ \
201 ML_CLASS_SOURCE(ITK##CLASS_NAME##Wrapper, PARENT_BASE_CLASS_NAME);
202
203
@ T
Field to encapsulate a pointer to an ML base object.
Definition mlFields.h:813
Class representing general ML objects that support import/export via strings (setPersistentState() an...
Definition mlBase.h:63
Class to create a Base wrapper around an object of a certain template type.
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 * getInputBaseField()
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.
BaseField * getOutputBaseField()
For legacy reasons there are two fields for getting the base field.
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)
Base class for an image processing module of the ML.
Definition mlModule.h:150
#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:989
#define ML_BAD_STATE
The current state of an object is not appropriate for an operation.
Definition mlTypeDefs.h:994
#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.
Target mlrange_cast(Source arg)
Generic version of checked ML casts.