Interfaces.h 9.48 KB
Newer Older
Floris Berendsen's avatar
Floris Berendsen committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
/*=========================================================================
 *
 *  Copyright Leiden University Medical Center, Erasmus University Medical 
 *  Center and contributors
 *
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *        http://www.apache.org/licenses/LICENSE-2.0.txt
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 *
 *=========================================================================*/

20
21
22
23
24
#ifndef Interfaces_h
#define Interfaces_h

#include "ComponentBase.h"
#include <typeinfo>
25
#include <string>
26

27
#include "itkProcessObject.h"
28
#include "itkImageToImageFilter.h"
29
#include "itkImageToImageMetricv4.h"
30
#include "itkObjectToObjectOptimizerBase.h"
Floris Berendsen's avatar
Floris Berendsen committed
31
32
33
34

#include "itkImage.h"
#include "itkMesh.h"

35
36
37
#include "selxAnyFileReader.h"
#include "selxAnyFileWriter.h"

Floris Berendsen's avatar
Floris Berendsen committed
38
namespace selx
39
{
Floris Berendsen's avatar
Floris Berendsen committed
40
41
  // Define the providing interfaces abstractly
  class MetricDerivativeInterface {
42
43
  public:
    virtual int GetDerivative() = 0;
Floris Berendsen's avatar
Floris Berendsen committed
44
  };
45

Floris Berendsen's avatar
Floris Berendsen committed
46
  class MetricValueInterface {
47
48
  public:
    virtual int GetValue() = 0;
Floris Berendsen's avatar
Floris Berendsen committed
49
  };
50

Floris Berendsen's avatar
Floris Berendsen committed
51
52
53
54
  class OptimizerUpdateInterface {
  public:
    virtual int Update() = 0;
  };
55

Floris Berendsen's avatar
Floris Berendsen committed
56
57
58
59
  class TransformedImageInterface {
  public:
    virtual int GetTransformedImage() = 0;
  };
60

Floris Berendsen's avatar
Floris Berendsen committed
61
62
63
64
65
  class ConflictinUpdateInterface {
  public:
    // "error" : member function templates cannot be virtual
    // template <class ConflictinUpdateInterface> virtual int Update() = 0;
    //TODO http://en.cppreference.com/w/cpp/language/member_template
66

Floris Berendsen's avatar
Floris Berendsen committed
67
68
69
70
    //TODO solution: http://stackoverflow.com/questions/2004820/inherit-interfaces-which-share-a-method-name
    //TODO better?: http://stackoverflow.com/questions/18398409/c-inherit-from-multiple-base-classes-with-the-same-virtual-function-name
    virtual int Update(ConflictinUpdateInterface*) = 0;
  };
71

72

73
74
  template<int Dimensionality, class TPixel>
  class itkImageInterface {
75
    // An interface that passes the pointer of an output image
76
  public:
77
78
    typedef typename itk::Image<TPixel, Dimensionality> ItkImageType;
    virtual typename ItkImageType::Pointer GetItkImage() = 0;
79
80
  };

81
82
83
84
85
86
87
88
  template<int Dimensionality, class TPixel>
  class itkImageFixedInterface {
	  // An interface that passes the pointer of an output image
  public:
    typedef typename itk::Image<TPixel, Dimensionality> ItkImageType;
    virtual typename ItkImageType::Pointer GetItkImageFixed() = 0;
  };

89
90
91
92
93
94
95
96
  template<int Dimensionality>
  class itkImageDomainFixedInterface {
    // An interface that passes the pointer of an output image
  public:
    typedef typename itk::ImageBase<Dimensionality> ItkImageDomainType;
    virtual typename ItkImageDomainType::Pointer GetItkImageDomainFixed() = 0;
  };

97
98
99
100
101
102
103
  template<int Dimensionality, class TPixel>
  class itkImageMovingInterface {
    // An interface that passes the pointer of an output image
  public:
    typedef typename itk::Image<TPixel, Dimensionality> ItkImageType;
    virtual typename ItkImageType::Pointer GetItkImageMoving() = 0;
  };
104

105
106
  template<int Dimensionality, class TPixel>
  class DisplacementFieldItkImageSourceInterface {
107
    // An interface that passes the pointer of an output image
108
  public:
109
110
    typedef typename itk::Image<itk::Vector< TPixel, Dimensionality >, Dimensionality> ItkImageType;
    virtual typename ItkImageType::Pointer GetDisplacementFieldItkImage() = 0;
Floris Berendsen's avatar
Floris Berendsen committed
111
112
113
114
  };

  template<int Dimensionality, class TPixel>
  class itkMeshInterface {
115
    // An interface that passes the pointer of an output mesh
Floris Berendsen's avatar
Floris Berendsen committed
116
117
  public:
    virtual typename itk::Mesh<TPixel, Dimensionality>::Pointer GetItkMesh() = 0;
118
119
  };

120
  class SourceInterface {
121
122
123
    // A special interface: the Overlord checks components for this type of interface.
    // By this interface only Source Components can to talk to the Overlord.
    // How specific Source Components connect to the graph is up to them, i.e. they might adapt the passed Object to other types.
124
  public:
125
    virtual void SetMiniPipelineInput(itk::DataObject::Pointer) = 0;
126
    virtual AnyFileReader::Pointer GetInputFileReader(void) = 0;
127
  };
128
  class SinkInterface {
129
130
131
132
    // A special interface: the Overlord checks components for this type of interface.
    // By this interface only Sink Components can to talk to the Overlord
    // How specific Sink Components connect to the graph is up to them, i.e. they might adapt the passed Object to other types.
  public:
133
134
135
    typedef itk::DataObject::Pointer DataObjectPointer;
    virtual void SetMiniPipelineOutput(DataObjectPointer) = 0;
    virtual DataObjectPointer GetMiniPipelineOutput(void) = 0;
136
    virtual AnyFileWriter::Pointer GetOutputFileWriter(void) = 0;
137
    virtual DataObjectPointer GetInitializedOutput(void) = 0;
138
139
  };

140
  class RunRegistrationInterface {
141
142
    // A special interface: the Overlord checks components for this type of interface.
    // This interface is for to control the execution of the network
143
144
145
  public:
    virtual void RunRegistration() = 0;
  };
146
147
148
149
150
151
152
153
  
  class AfterRegistrationInterface {
    // A special interface: the Overlord checks components for this type of interface.
    // This interface is for to control the execution of the network
  public:
    virtual void AfterRegistration() = 0;
  };

154
  class RunResolutionInterface {
155
156
    // A special interface: the Overlord checks components for this type of interface.
    // This interface is for to control the execution of the network
157
158
159
160
  public:
    virtual bool RunResolution() = 0;
  };

161
162
163
164
165
166
167
  class ReconnectTransformInterface {
    // A special interface: the Overlord checks components for this type of interface.
    // This interface is for to control the execution of the network
  public:
    virtual void ReconnectTransform() = 0;
  };
  
168
  template<int Dimensionality, class TPixel>
169
  class itkMetricv4Interface {
170
171
172
173
174
  public:
    typedef typename itk::Image<TPixel, Dimensionality> FixedImageType;
    typedef typename itk::Image<TPixel, Dimensionality> MovingImageType;
    typedef typename itk::ImageToImageMetricv4<FixedImageType, MovingImageType> ImageToImageMetricv4Type;

175
    virtual typename ImageToImageMetricv4Type::Pointer GetItkMetricv4() = 0;
176
  };
177

178
179
180
181
182
183
184
185
186
187
  template<class TInternalComputationValueType>
  class itkOptimizerv4Interface {
  public:
    /**  Type of the optimizer. */
    typedef TInternalComputationValueType InternalComputationValueType;
    typedef itk::ObjectToObjectOptimizerBaseTemplate<InternalComputationValueType>               OptimizerType;
    typedef typename OptimizerType::Pointer                             Optimizerv4Pointer;
    virtual Optimizerv4Pointer GetItkOptimizerv4() = 0;
  };

188
189
190
    template<class TInternalComputationValueType, int Dimensionality>
  class itkTransformInterface {
  public:
191
    typedef TInternalComputationValueType InternalComputationValueType;
192
    typedef typename itk::Transform<TInternalComputationValueType, Dimensionality, Dimensionality>       TransformType;
193
194
195
196
197
    typedef typename TransformType::Pointer                             TransformPointer;

    virtual TransformPointer GetItkTransform() = 0;
  };
  
Floris Berendsen's avatar
Floris Berendsen committed
198
  // Define the accepting interfaces as templated by the providing interface
199

Floris Berendsen's avatar
Floris Berendsen committed
200
201
202
203
204
205
206
207
208
209
210
211
212
213
  template<class InterfaceT>
  class InterfaceAcceptor {
  public:

    // Set() is called by a succesfull Connect()
    // The implementation of Set() must be provided by component developers. 
    virtual int Set(InterfaceT*) = 0;

    // Connect tries to connect this accepting interface with all interfaces of the provider component.
    int Connect(ComponentBase*);

  private:
    bool isSet;
  };
214
215
216
217
218

template<typename ... RestInterfaces>
class Accepting
{
  public:
Floris Berendsen's avatar
Floris Berendsen committed
219
    ComponentBase::interfaceStatus ConnectFromImpl(const char *, ComponentBase*) { return ComponentBase::interfaceStatus::noaccepter; }; //no interface called interfacename ;
220
    int ConnectFromImpl(ComponentBase*) { return 0; }; //Empty RestInterfaces does 0 successful connects ;
221
222
protected:
  bool HasInterface(const char *) { return false; };
223
224
225
226
227
228
};

template<typename FirstInterface, typename ... RestInterfaces>
class Accepting<FirstInterface, RestInterfaces... > : public InterfaceAcceptor<FirstInterface>, public Accepting< RestInterfaces ... >
{
public:
Floris Berendsen's avatar
Floris Berendsen committed
229
  ComponentBase::interfaceStatus ConnectFromImpl(const char *, ComponentBase*);
230
  int ConnectFromImpl(ComponentBase*);
231
232
233
protected:
  bool HasInterface(const char *);

234
235
};

236
237
template<typename ... RestInterfaces>
class Providing
238
{
239
240
protected:
  bool HasInterface(const char *) { return false; };
241
242
};

243
244
245
246
247
248
249
250
251
252
253
254
255
template<typename FirstInterface, typename ... RestInterfaces>
class Providing<FirstInterface, RestInterfaces... > : public FirstInterface, public Providing < RestInterfaces ... >
{
protected:
  bool HasInterface(const char *);

};

//template<typename... Interfaces>
//class Providing : public Interfaces...
//{
//};

256
//TODO rename Implements to SuperElastixComponent
257
258
259
260
template<typename AcceptingInterfaces, typename ProvidingInterfaces>
class Implements : public AcceptingInterfaces, public ProvidingInterfaces, public ComponentBase
{
  public:
261
262
    virtual interfaceStatus AcceptConnectionFrom(const char *, ComponentBase*);
    virtual int AcceptConnectionFrom(ComponentBase*);
263
264
265
protected: 
  virtual bool HasAcceptingInterface(const char *);
  virtual bool HasProvidingInterface(const char *);
266
267
};

268
269
270



Floris Berendsen's avatar
Floris Berendsen committed
271
} // end namespace selx
272
273
274
275
276
277

#ifndef ITK_MANUAL_INSTANTIATION
#include "Interfaces.hxx"
#endif

#endif // #define Interfaces_h