File: itkMultiInputImageToImageMetricBase.hxx

package info (click to toggle)
elastix 5.2.0-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 42,480 kB
  • sloc: cpp: 68,403; lisp: 4,118; python: 1,013; xml: 182; sh: 177; makefile: 33
file content (331 lines) | stat: -rw-r--r-- 14,973 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
/*=========================================================================
 *
 *  Copyright UMC Utrecht 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.
 *
 *=========================================================================*/
#ifndef _itkMultiInputImageToImageMetricBase_hxx
#define _itkMultiInputImageToImageMetricBase_hxx

#include "itkMultiInputImageToImageMetricBase.h"

/** Macros to reduce some copy-paste work.
 * These macros provide the implementation of
 * all Set/GetFixedImage, Set/GetInterpolator etc methods
 *
 * The macros are undef'ed at the end of this file
 */

/** Macro for setting objects. */
#define itkImplementationSetObjectMacro(_name, _type)                                                                  \
  template <class TFixedImage, class TMovingImage>                                                                     \
  void MultiInputImageToImageMetricBase<TFixedImage, TMovingImage>::Set##_name(_type * _arg, unsigned int pos)         \
  {                                                                                                                    \
    if (this->m_##_name##Vector.size() < pos + 1)                                                                      \
    {                                                                                                                  \
      this->m_##_name##Vector.resize(pos + 1);                                                                         \
      this->m_NumberOf##_name##s = pos + 1;                                                                            \
    }                                                                                                                  \
    if (pos == 0)                                                                                                      \
    {                                                                                                                  \
      this->Superclass::Set##_name(_arg);                                                                              \
    }                                                                                                                  \
    if (this->m_##_name##Vector[pos] != _arg)                                                                          \
    {                                                                                                                  \
      this->m_##_name##Vector[pos] = _arg;                                                                             \
      this->Modified();                                                                                                \
    }                                                                                                                  \
  } // comments for allowing ; after calling the macro

/** Macro for setting objects. */
#define itkImplementationSetObjectMacro2(_name, _type)                                                                 \
  template <class TFixedImage, class TMovingImage>                                                                     \
  void MultiInputImageToImageMetricBase<TFixedImage, TMovingImage>::Set##_name(_type * _arg, unsigned int pos)         \
  {                                                                                                                    \
    if (this->m_##_name##Vector.size() < pos + 1)                                                                      \
    {                                                                                                                  \
      this->m_##_name##Vector.resize(pos + 1);                                                                         \
      this->m_NumberOf##_name##s = pos + 1;                                                                            \
    }                                                                                                                  \
    if (this->m_##_name##Vector[pos] != _arg)                                                                          \
    {                                                                                                                  \
      this->m_##_name##Vector[pos] = _arg;                                                                             \
      this->Modified();                                                                                                \
    }                                                                                                                  \
  } // comments for allowing ; after calling the macro

/** Macro for getting objects. */
#define itkImplementationGetObjectMacro(_name, _type)                                                                  \
  template <class TFixedImage, class TMovingImage>                                                                     \
  auto MultiInputImageToImageMetricBase<TFixedImage, TMovingImage>::Get##_name(unsigned int pos) const->_type *        \
  {                                                                                                                    \
    if (this->m_##_name##Vector.size() < pos + 1)                                                                      \
    {                                                                                                                  \
      return 0;                                                                                                        \
    }                                                                                                                  \
    return this->m_##_name##Vector[pos];                                                                               \
  } // comments for allowing ; after calling the macro

/** Macro for getting const objects. */
#define itkImplementationGetConstObjectMacro(_name, _type)                                                             \
  template <class TFixedImage, class TMovingImage>                                                                     \
  auto MultiInputImageToImageMetricBase<TFixedImage, TMovingImage>::Get##_name(unsigned int pos) const->const _type *  \
  {                                                                                                                    \
    if (this->m_##_name##Vector.size() < pos + 1)                                                                      \
    {                                                                                                                  \
      return 0;                                                                                                        \
    }                                                                                                                  \
    return this->m_##_name##Vector[pos];                                                                               \
  } // comments for allowing ; after calling the macro

namespace itk
{

/** Set components. */
itkImplementationSetObjectMacro(FixedImage, const FixedImageType);
itkImplementationSetObjectMacro(FixedImageMask, const FixedImageMaskType);
itkImplementationSetObjectMacro(MovingImage, const MovingImageType);
itkImplementationSetObjectMacro(MovingImageMask, const MovingImageMaskType);
itkImplementationSetObjectMacro(Interpolator, InterpolatorType);
itkImplementationSetObjectMacro2(FixedImageInterpolator, FixedImageInterpolatorType);

/** Get components. */
itkImplementationGetConstObjectMacro(FixedImage, FixedImageType);
itkImplementationGetConstObjectMacro(FixedImageMask, FixedImageMaskType);
itkImplementationGetConstObjectMacro(MovingImage, MovingImageType);
itkImplementationGetConstObjectMacro(MovingImageMask, MovingImageMaskType);
itkImplementationGetObjectMacro(Interpolator, InterpolatorType);
itkImplementationGetObjectMacro(FixedImageInterpolator, FixedImageInterpolatorType);


/**
 * ************************ SetFixedImageRegion *************************
 */

template <class TFixedImage, class TMovingImage>
void
MultiInputImageToImageMetricBase<TFixedImage, TMovingImage>::SetFixedImageRegion(const FixedImageRegionType _arg,
                                                                                 unsigned int               pos)
{
  if (this->m_FixedImageRegionVector.size() < pos + 1)
  {
    this->m_FixedImageRegionVector.resize(pos + 1);
    this->m_NumberOfFixedImageRegions = pos + 1;
  }
  if (pos == 0)
  {
    this->Superclass::SetFixedImageRegion(_arg);
  }
  if (this->m_FixedImageRegionVector[pos] != _arg)
  {
    this->m_FixedImageRegionVector[pos] = _arg;
    this->Modified();
  }

} // end SetFixedImageRegion()


/**
 * ************************ GetFixedImageRegion *************************
 */

template <class TFixedImage, class TMovingImage>
auto
MultiInputImageToImageMetricBase<TFixedImage, TMovingImage>::GetFixedImageRegion(unsigned int pos) const
  -> const FixedImageRegionType &
{
  if (this->m_FixedImageRegionVector.size() < pos)
  {
    return this->m_DummyFixedImageRegion;
  }

  return this->m_FixedImageRegionVector[pos];

} // end GetFixedImageRegion()


/**
 * ****************** CheckForBSplineInterpolators **********************
 */

template <class TFixedImage, class TMovingImage>
void
MultiInputImageToImageMetricBase<TFixedImage, TMovingImage>::CheckForBSplineInterpolators()
{
  /** Check if the interpolators are of type BSplineInterpolateImageFunction.
   * If so, we can make use of its EvaluateDerivatives method.
   * Otherwise, an exception is thrown.
   */
  this->m_InterpolatorsAreBSpline = true;
  this->m_BSplineInterpolatorVector.resize(this->m_NumberOfMovingImages);

  for (unsigned int i = 0; i < this->m_NumberOfMovingImages; ++i)
  {
    BSplineInterpolatorType * testPtr =
      dynamic_cast<BSplineInterpolatorType *>(this->m_InterpolatorVector[i].GetPointer());

    if (testPtr)
    {
      this->m_BSplineInterpolatorVector[i] = testPtr;
      itkDebugMacro("Interpolator " << i << " is B-spline.");
    }
    else
    {
      this->m_InterpolatorsAreBSpline = false;
      itkDebugMacro("Interpolator " << i << " is NOT B-spline.");
      itkExceptionMacro("Interpolator " << i << " is NOT B-spline.");
    }
  } // end for-loop

} // end CheckForBSplineInterpolators()


/**
 * ****************** Initialize **********************
 */

template <class TFixedImage, class TMovingImage>
void
MultiInputImageToImageMetricBase<TFixedImage, TMovingImage>::Initialize()
{
  /** Connect the interpolators. */
  for (unsigned int i = 0; i < this->GetNumberOfInterpolators(); ++i)
  {
    this->m_InterpolatorVector[i]->SetInputImage(this->m_MovingImageVector[i]);
  }

  /** Connect the fixed image interpolators. */
  for (unsigned int i = 0; i < this->GetNumberOfFixedImageInterpolators(); ++i)
  {
    this->m_FixedImageInterpolatorVector[i]->SetInputImage(this->m_FixedImageVector[i]);
  }

  /** Check for B-spline interpolators. */
  this->CheckForBSplineInterpolators();

  /** Call the superclass' implementation. */
  this->Superclass::Initialize();

} // end Initialize()


/**
 * ********************* InitializeImageSampler ****************************
 */

template <class TFixedImage, class TMovingImage>
void
MultiInputImageToImageMetricBase<TFixedImage, TMovingImage>::InitializeImageSampler()
{
  if (this->GetUseImageSampler())
  {
    /** Check if the ImageSampler is set. */
    if (!Superclass::m_ImageSampler)
    {
      itkExceptionMacro("ImageSampler is not present");
    }

    /** Initialize the Image Sampler: set the fixed images. */
    for (unsigned int i = 0; i < this->GetNumberOfFixedImages(); ++i)
    {
      Superclass::m_ImageSampler->SetInput(i, this->m_FixedImageVector[i]);
    }

    /** Initialize the Image Sampler: set the fixed image masks. */
    for (unsigned int i = 0; i < this->GetNumberOfFixedImageMasks(); ++i)
    {
      Superclass::m_ImageSampler->SetMask(this->m_FixedImageMaskVector[i], i);
    }

    /** Initialize the Image Sampler: set the fixed image regions. */
    for (unsigned int i = 0; i < this->GetNumberOfFixedImages(); ++i)
    {
      Superclass::m_ImageSampler->SetInputImageRegion(this->m_FixedImageRegionVector[i], i);
    }
  }

} // end InitializeImageSampler()


/**
 * ******************* EvaluateMovingImageValueAndDerivative ******************
 */

template <class TFixedImage, class TMovingImage>
bool
MultiInputImageToImageMetricBase<TFixedImage, TMovingImage>::EvaluateMovingImageValueAndDerivative(
  const MovingImagePointType & mappedPoint,
  RealType &                   movingImageValue,
  MovingImageDerivativeType *  gradient) const
{
  /** Check if the mapped point is inside the moving image buffers of the feature images. */
  bool sampleOk = true;
  for (unsigned int i = 1; i < this->GetNumberOfInterpolators(); ++i)
  {
    sampleOk &= this->GetInterpolator(i)->IsInsideBuffer(mappedPoint);

    /** If not inside this buffer we can quit. */
    if (!sampleOk)
    {
      return false;
    }
  }

  /** Compute value and possibly derivative of the moving image. */
  return this->Superclass::EvaluateMovingImageValueAndDerivative(mappedPoint, movingImageValue, gradient);

} // end EvaluateMovingImageValueAndDerivative()


/**
 * ************************ IsInsideMovingMask *************************
 */

template <class TFixedImage, class TMovingImage>
bool
MultiInputImageToImageMetricBase<TFixedImage, TMovingImage>::IsInsideMovingMask(
  const MovingImagePointType & mappedPoint) const
{
  /** If no moving image masks are present 'true' is returned,
   * meaning that this sample is taken into account. Otherwise, the
   * AND of all masks is returned, i.e. the sample should be inside
   * all masks.
   */
  bool inside = true;
  for (unsigned int i = 0; i < this->GetNumberOfMovingImageMasks(); ++i)
  {
    if (const auto * const movingImageMask = this->GetMovingImageMask(i))
    {
      inside &= movingImageMask->IsInsideInWorldSpace(mappedPoint);
    }

    /** If the point falls outside one mask, we can skip the rest. */
    if (!inside)
    {
      return false;
    }
  }
  return inside;

} // end IsInsideMovingMask()


} // end namespace itk

#undef itkImplementationSetObjectMacro
#undef itkImplementationSetObjectMacro2
#undef itkImplementationGetObjectMacro
#undef itkImplementationGetConstObjectMacro

#endif // end #ifndef _itkMultiInputImageToImageMetricBase_hxx