File: itkImageToImageMetric.h

package info (click to toggle)
insighttoolkit5 5.4.5-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 704,588 kB
  • sloc: cpp: 784,579; ansic: 628,724; xml: 44,704; fortran: 34,250; python: 22,934; sh: 4,078; pascal: 2,636; lisp: 2,158; makefile: 461; yacc: 328; asm: 205; perl: 203; lex: 146; tcl: 132; javascript: 98; csh: 81
file content (709 lines) | stat: -rw-r--r-- 26,969 bytes parent folder | download | duplicates (2)
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
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
/*=========================================================================
 *
 *  Copyright NumFOCUS
 *
 *  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
 *
 *         https://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 itkImageToImageMetric_h
#define itkImageToImageMetric_h

#include "itkBSplineBaseTransform.h"
#include "itkBSplineInterpolateImageFunction.h"
#include "itkSingleValuedCostFunction.h"
#include "itkGradientRecursiveGaussianImageFilter.h"
#include "itkSpatialObject.h"
#include "itkCentralDifferenceImageFunction.h"
#include "itkMultiThreaderBase.h"

#include <memory> // For unique_ptr.

namespace itk
{
/** \class ImageToImageMetric
 * \brief Computes similarity between regions of two images.
 *
 * This Class is templated over the type of the two input images.
 * It expects a Transform and an Interpolator to be plugged in.
 * This particular class is the base class for a hierarchy of
 * similarity metrics.
 *
 * This class computes a value that measures the similarity
 * between the Fixed image and the transformed Moving image.
 * The Interpolator is used to compute intensity values on
 * non-grid positions resulting from mapping points through
 * the Transform.
 *
 *
 * \ingroup RegistrationMetrics
 *
 * \ingroup ITKRegistrationCommon
 */

template <typename TFixedImage, typename TMovingImage>
class ITK_TEMPLATE_EXPORT ImageToImageMetric : public SingleValuedCostFunction
{
public:
  ITK_DISALLOW_COPY_AND_MOVE(ImageToImageMetric);

  /** Standard class type aliases. */
  using Self = ImageToImageMetric;
  using Superclass = SingleValuedCostFunction;
  using Pointer = SmartPointer<Self>;
  using ConstPointer = SmartPointer<const Self>;

  /** Type used for representing point components  */
  using CoordinateRepresentationType = typename Superclass::ParametersValueType;

  /** \see LightObject::GetNameOfClass() */
  itkOverrideGetNameOfClassMacro(ImageToImageMetric);

  /**  Type of the moving Image. */
  using MovingImageType = TMovingImage;
  using MovingImagePixelType = typename TMovingImage::PixelType;
  using MovingImageConstPointer = typename MovingImageType::ConstPointer;

  /**  Type of the fixed Image. */
  using FixedImageType = TFixedImage;
  using FixedImagePixelType = typename TFixedImage::PixelType;
  using FixedImageConstPointer = typename FixedImageType::ConstPointer;
  using FixedImageRegionType = typename FixedImageType::RegionType;

  /** Constants for the image dimensions */
  static constexpr unsigned int MovingImageDimension = TMovingImage::ImageDimension;
  static constexpr unsigned int FixedImageDimension = TFixedImage::ImageDimension;

  /**  Type of the Transform Base class */
  using TransformType = Transform<CoordinateRepresentationType, Self::MovingImageDimension, Self::FixedImageDimension>;

  using TransformPointer = typename TransformType::Pointer;
  using InputPointType = typename TransformType::InputPointType;
  using OutputPointType = typename TransformType::OutputPointType;
  using TransformParametersType = typename TransformType::ParametersType;
  using TransformJacobianType = typename TransformType::JacobianType;

  /** Index and Point type alias support */
  using FixedImageIndexType = typename FixedImageType::IndexType;
  using FixedImageIndexValueType = typename FixedImageIndexType::IndexValueType;
  using MovingImageIndexType = typename MovingImageType::IndexType;
  using FixedImagePointType = typename TransformType::InputPointType;
  using MovingImagePointType = typename TransformType::OutputPointType;

  using FixedImageIndexContainer = std::vector<FixedImageIndexType>;

  /**  Type of the Interpolator Base class */
  using InterpolatorType = InterpolateImageFunction<MovingImageType, CoordinateRepresentationType>;

  /** Gaussian filter to compute the gradient of the Moving Image */
  using RealType = typename NumericTraits<MovingImagePixelType>::RealType;
  using GradientPixelType = CovariantVector<RealType, Self::MovingImageDimension>;
  using GradientImageType = Image<GradientPixelType, Self::MovingImageDimension>;
  using GradientImagePointer = SmartPointer<GradientImageType>;
  using GradientImageFilterType = GradientRecursiveGaussianImageFilter<MovingImageType, GradientImageType>;
  using GradientImageFilterPointer = typename GradientImageFilterType::Pointer;

  using InterpolatorPointer = typename InterpolatorType::Pointer;

  /**  Type for the mask of the fixed image. Only pixels that are "inside"
       this mask will be considered for the computation of the metric */
  using FixedImageMaskType = SpatialObject<Self::FixedImageDimension>;
  using FixedImageMaskPointer = typename FixedImageMaskType::Pointer;
  using FixedImageMaskConstPointer = typename FixedImageMaskType::ConstPointer;

  /**  Type for the mask of the moving image. Only pixels that are "inside"
       this mask will be considered for the computation of the metric */
  using MovingImageMaskType = SpatialObject<Self::MovingImageDimension>;
  using MovingImageMaskPointer = typename MovingImageMaskType::Pointer;
  using MovingImageMaskConstPointer = typename MovingImageMaskType::ConstPointer;

  /**  Type of the measure. */
  using typename Superclass::MeasureType;

  /**  Type of the derivative. */
  using typename Superclass::DerivativeType;

  /**  Type of the parameters. */
  using typename Superclass::ParametersType;

  /** Get/Set the Fixed Image.  */
  itkSetConstObjectMacro(FixedImage, FixedImageType);
  itkGetConstObjectMacro(FixedImage, FixedImageType);

  /** Get/Set the Moving Image.  */
  itkSetConstObjectMacro(MovingImage, MovingImageType);
  itkGetConstObjectMacro(MovingImage, MovingImageType);

  /** Connect the Transform. */
  itkSetObjectMacro(Transform, TransformType);

  /** Get a pointer to the Transform.  */
  itkGetModifiableObjectMacro(Transform, TransformType);

  /** Connect the Interpolator. */
  itkSetObjectMacro(Interpolator, InterpolatorType);

  /** Get a pointer to the Interpolator.  */
  itkGetModifiableObjectMacro(Interpolator, InterpolatorType);

  /** Get the number of pixels considered in the computation. */
  SizeValueType
  GetNumberOfMovingImageSamples()
  {
    return this->GetNumberOfPixelsCounted();
  }

  itkGetConstReferenceMacro(NumberOfPixelsCounted, SizeValueType);

  /** Set the region over which the metric will be computed */
  virtual void
  SetFixedImageRegion(const FixedImageRegionType reg);

  /** Get the region over which the metric will be computed */
  itkGetConstReferenceMacro(FixedImageRegion, FixedImageRegionType);

  /** Set/Get the moving image mask. */
#ifndef ITK_FUTURE_LEGACY_REMOVE
  virtual void
  SetMovingImageMask(MovingImageMaskType * const arg)
  {
    const auto * const constArg = arg;
    // Call the overload defined by itkSetConstObjectMacro, or an override.
    this->SetMovingImageMask(constArg);
  }
#endif
  itkSetConstObjectMacro(MovingImageMask, MovingImageMaskType);
  itkGetConstObjectMacro(MovingImageMask, MovingImageMaskType);

  /** Set/Get the fixed image mask. */
#ifndef ITK_FUTURE_LEGACY_REMOVE
  virtual void
  SetFixedImageMask(FixedImageMaskType * const arg)
  {
    const auto * const constArg = arg;
    // Call the overload defined by itkSetConstObjectMacro, or an override.
    this->SetFixedImageMask(constArg);
  }
#endif
  itkSetConstObjectMacro(FixedImageMask, FixedImageMaskType);
  itkGetConstObjectMacro(FixedImageMask, FixedImageMaskType);

  /** Set the fixed image indexes to be used as the samples when
   *   computing the match metric */
  void
  SetFixedImageIndexes(const FixedImageIndexContainer & indexes);

  void
  SetUseFixedImageIndexes(bool useIndexes);

  itkGetConstReferenceMacro(UseFixedImageIndexes, bool);

  /** Set/Get number of work units to use for computations. */
  void
  SetNumberOfWorkUnits(ThreadIdType numberOfThreads);
  itkGetConstReferenceMacro(NumberOfWorkUnits, ThreadIdType);

  /** Set/Get gradient computation. */
  itkSetMacro(ComputeGradient, bool);
  itkGetConstReferenceMacro(ComputeGradient, bool);
  itkBooleanMacro(ComputeGradient);

  /** Computes the gradient image and assigns it to m_GradientImage */
  virtual void
  ComputeGradient();

  /** Get Gradient Image. */
  itkGetModifiableObjectMacro(GradientImage, GradientImageType);

  /** Set the parameters defining the Transform. */
  void
  SetTransformParameters(const ParametersType & parameters) const;

  /** Return the number of parameters required by the Transform */
  unsigned int
  GetNumberOfParameters() const override
  {
    return m_Transform->GetNumberOfParameters();
  }

  /** Initialize the Metric by making sure that all the components are present and plugged together correctly. */
  virtual void
  Initialize();

  /** Initialize the components related to supporting multiple threads */
  virtual void
  MultiThreadingInitialize();

  /** Number of spatial samples to compute the metric.
   *
   * Sets the number of samples.
   */
  virtual void
  SetNumberOfFixedImageSamples(SizeValueType numSamples);
  itkGetConstReferenceMacro(NumberOfFixedImageSamples, SizeValueType);

  /** Number of spatial samples to used to compute metric
   *   This sets the number of samples.  */
  void
  SetNumberOfSpatialSamples(SizeValueType num)
  {
    this->SetNumberOfFixedImageSamples(num);
  }

  SizeValueType
  GetNumberOfSpatialSamples()
  {
    return this->GetNumberOfFixedImageSamples();
  }

  /** Minimum fixed-image intensity needed for a sample to be used in the
   *  metric computation */
  void
  SetFixedImageSamplesIntensityThreshold(const FixedImagePixelType & thresh);

  itkGetConstReferenceMacro(FixedImageSamplesIntensityThreshold, FixedImagePixelType);

  void
  SetUseFixedImageSamplesIntensityThreshold(bool useThresh);

  itkGetConstReferenceMacro(UseFixedImageSamplesIntensityThreshold, bool);

  /** Select whether the metric will be computed using all the pixels on the
   * fixed image region, or only using a set of randomly selected pixels.
   * This value override IntensityThreshold, Masks, and SequentialSampling. */
  void
  SetUseAllPixels(bool useAllPixels);

  void
  UseAllPixelsOn()
  {
    this->SetUseAllPixels(true);
  }

  void
  UseAllPixelsOff()
  {
    this->SetUseAllPixels(false);
  }

  itkGetConstReferenceMacro(UseAllPixels, bool);

  /** If set to true, then every pixel in the fixed image will be scanned to
   * determine if it should be used in registration metric computation.  A
   * pixel will be chosen if it meets any mask or threshold limits set.  If
   * set to false, then UseAllPixels will be set to false. */
  void
  SetUseSequentialSampling(bool useSequential);

  itkGetConstReferenceMacro(UseSequentialSampling, bool);

  /** Reinitialize the seed of the random number generator that selects the
   * sample of pixels used for estimating the image histograms and the joint
   * histogram. By nature, this metric is not deterministic, since at each run
   * it may select a different set of pixels. By initializing the random number
   * generator seed to the same value you can restore determinism. On the other
   * hand, calling the method ReinitializeSeed() without arguments will use the
   * clock from your machine in order to have a very random initialization of
   * the seed. This will indeed increase the non-deterministic behavior of the
   * metric. */
  void
  ReinitializeSeed();
  void
  ReinitializeSeed(int seed);

  /** This boolean flag is only relevant when this metric is used along
   * with a BSplineBaseTransform. The flag enables/disables the
   * caching of values computed when a physical point is mapped through
   * the BSplineBaseTransform. In particular it will cache the
   * values of the BSpline weights for that points, and the indexes
   * indicating what BSpline-grid nodes are relevant for that specific
   * point. This caching is made optional due to the fact that the
   * memory arrays used for the caching can reach large sizes even for
   * moderate image size problems. For example, for a 3D image of
   * 256^3, using 20% of pixels, these arrays will take about 1
   * Gigabyte of RAM for storage. The ratio of computing time between
   * using the cache or not using the cache can reach 1:5, meaning that
   * using the caching can provide a five times speed up. It is
   * therefore, interesting to enable the caching, if enough memory is
   * available for it. The caching is enabled by default, in order to
   * preserve backward compatibility with previous versions of ITK. */
  itkSetMacro(UseCachingOfBSplineWeights, bool);
  itkGetConstReferenceMacro(UseCachingOfBSplineWeights, bool);
  itkBooleanMacro(UseCachingOfBSplineWeights);

  using MultiThreaderType = MultiThreaderBase;
  /** Get the Threader. */
  itkGetModifiableObjectMacro(Threader, MultiThreaderType);
  const TransformPointer *
  GetThreaderTransform()
  {
    return m_ThreaderTransform.get();
  }

protected:
  ImageToImageMetric();
  ~ImageToImageMetric() override = default;

  void
  PrintSelf(std::ostream & os, Indent indent) const override;

  /** \class FixedImageSamplePoint
   * A fixed image spatial sample consists of the fixed domain point
   * and the fixed image value at that point.
   * \ingroup ITKRegistrationCommon
   */
  class FixedImageSamplePoint
  {
  public:
    FixedImageSamplePoint()
    {
      point.Fill(0.0);
      value = 0;
      valueIndex = 0;
    }

    ~FixedImageSamplePoint() = default;

    inline friend std::ostream &
    operator<<(std::ostream & os, const FixedImageSamplePoint & val)
    {
      os << "point: " << static_cast<typename NumericTraits<FixedImagePointType>::PrintType>(val.point) << std::endl;
      os << "value: " << val.value << std::endl;
      os << "valueIndex: " << val.valueIndex << std::endl;

      return os;
    }

  public:
    FixedImagePointType point;
    double              value;
    unsigned int        valueIndex;
  };

  bool                     m_UseFixedImageIndexes{ false };
  FixedImageIndexContainer m_FixedImageIndexes{};

  bool                m_UseFixedImageSamplesIntensityThreshold{ false };
  FixedImagePixelType m_FixedImageSamplesIntensityThreshold{};

  /** FixedImageSamplePoint type alias support */
  using FixedImageSampleContainer = std::vector<FixedImageSamplePoint>;

  /** Uniformly select a sample set from the fixed image domain.
   *
   * Samples the fixed image using a random walk.
   */
  virtual void
  SampleFixedImageRegion(FixedImageSampleContainer & samples) const;

  /** Use the indexes that have been passed to the metric. */
  virtual void
  SampleFixedImageIndexes(FixedImageSampleContainer & samples) const;

  /** Sample the fixed image domain using all pixels in the Fixed image region.
   *
   * Gathers all the pixels from the fixed image domain.
   */
  virtual void
  SampleFullFixedImageRegion(FixedImageSampleContainer & samples) const;

  /** Container to store a set of points and fixed image values. */
  FixedImageSampleContainer m_FixedImageSamples{};

  SizeValueType m_NumberOfParameters{ 0 };

  SizeValueType m_NumberOfFixedImageSamples{ 50000 };
  // m_NumberOfPixelsCounted must be mutable because the const
  // thread consolidation functions merge each work unit's values
  // onto this accumulator variable.
  mutable SizeValueType m_NumberOfPixelsCounted{ 0 };

  FixedImageConstPointer  m_FixedImage{};
  MovingImageConstPointer m_MovingImage{};

  /** Main transform to be used in thread = 0 */
  TransformPointer m_Transform{};
  /** Copies of Transform helpers per thread (N-1 of them, since m_Transform
   * will do the work for thread=0. */
  std::unique_ptr<TransformPointer[]> m_ThreaderTransform;

  InterpolatorPointer m_Interpolator{};

  bool                 m_ComputeGradient{ true };
  GradientImagePointer m_GradientImage{};

  FixedImageMaskConstPointer  m_FixedImageMask{};
  MovingImageMaskConstPointer m_MovingImageMask{};

  ThreadIdType m_NumberOfWorkUnits{ 1 };

  bool m_UseAllPixels{ false };
  bool m_UseSequentialSampling{ false };

  bool m_ReseedIterator{ false };

  mutable int m_RandomSeed{};

  /** Types and variables related to BSpline deformable transforms.
   * If the transform is of type third order BSplineBaseTransform,
   * then we can speed up the metric derivative calculation by
   * only inspecting the parameters within the support region
   * of a mapped point.  */

#ifndef ITK_FUTURE_LEGACY_REMOVE
  /** Boolean to indicate if the transform is BSpline deformable.
  \deprecated `m_TransformIsBSpline` is intended to be removed, in the future. Please use `m_BSplineTransform`
  instead. For example, `if (m_TransformIsBSpline)` may be rewritten as `if (m_BSplineTransform)`. */
  bool m_TransformIsBSpline{ false };
#endif

  /** The number of BSpline transform weights is the number of
   * of parameter in the support region (per dimension ). */
  SizeValueType m_NumBSplineWeights{ 0 };

  static constexpr unsigned int DeformationSplineOrder = 3;

  using BSplineTransformType =
    BSplineBaseTransform<CoordinateRepresentationType, FixedImageType::ImageDimension, Self::DeformationSplineOrder>;

  using BSplineTransformWeightsType = typename BSplineTransformType::WeightsType;
  using WeightsValueType = typename BSplineTransformWeightsType::ValueType;
  using BSplineTransformWeightsArrayType = Array2D<WeightsValueType>;

  using BSplineTransformIndexArrayType = typename BSplineTransformType::ParameterIndexArrayType;
  using IndexValueType = typename BSplineTransformIndexArrayType::ValueType;
  using BSplineTransformIndicesArrayType = Array2D<IndexValueType>;

  using MovingImagePointArrayType = std::vector<MovingImagePointType>;
  using BooleanArrayType = std::vector<bool>;
  using BSplineParametersOffsetType = FixedArray<SizeValueType, FixedImageType::ImageDimension>;
  /**
   * If a BSplineInterpolationFunction is used, this class obtain
   * image derivatives from the BSpline interpolator. Otherwise,
   * image derivatives are computed using central differencing.
   */
  using BSplineInterpolatorType = BSplineInterpolateImageFunction<MovingImageType, CoordinateRepresentationType>;
  /** Typedefs for using central difference calculator. */
  using DerivativeFunctionType = CentralDifferenceImageFunction<MovingImageType, CoordinateRepresentationType>;
  using ImageDerivativesType = CovariantVector<double, Self::MovingImageDimension>;

  typename BSplineTransformType::Pointer m_BSplineTransform{};

  BSplineTransformWeightsArrayType m_BSplineTransformWeightsArray{};
  BSplineTransformIndicesArrayType m_BSplineTransformIndicesArray{};
  MovingImagePointArrayType        m_BSplinePreTransformPointsArray{};
  BooleanArrayType                 m_WithinBSplineSupportRegionArray{};

  BSplineParametersOffsetType m_BSplineParametersOffset{};

  // Variables needed for optionally caching values when using a BSpline
  // transform.
  bool                                   m_UseCachingOfBSplineWeights{ true };
  mutable BSplineTransformWeightsType    m_BSplineTransformWeights{};
  mutable BSplineTransformIndexArrayType m_BSplineTransformIndices{};

  mutable std::unique_ptr<BSplineTransformWeightsType[]>    m_ThreaderBSplineTransformWeights;
  mutable std::unique_ptr<BSplineTransformIndexArrayType[]> m_ThreaderBSplineTransformIndices;

  /** Cache pre-transformed points, weights and indices. */
  virtual void
  PreComputeTransformValues();

  /** Transform a point from FixedImage domain to MovingImage domain.
   *
   * This function also checks if mapped point is within support region.
   */
  virtual void
  TransformPoint(unsigned int           sampleNumber,
                 MovingImagePointType & mappedPoint,
                 bool &                 sampleOk,
                 double &               movingImageValue,
                 ThreadIdType           threadId) const;

  /** Transform a point from FixedImage domain to MovingImage domain.
   *
   * This function also checks if mapped point is within support region.
   */
  virtual void
  TransformPointWithDerivatives(unsigned int           sampleNumber,
                                MovingImagePointType & mappedPoint,
                                bool &                 sampleOk,
                                double &               movingImageValue,
                                ImageDerivativesType & movingImageGradient,
                                ThreadIdType           threadId) const;

#ifndef ITK_FUTURE_LEGACY_REMOVE
  /** Boolean to indicate if the interpolator BSpline.
  \deprecated `m_InterpolatorIsBSpline` is intended to be removed, in the future. Please use `m_BSplineInterpolator`
  instead. For example, `if (m_InterpolatorIsBSpline)` may be rewritten as `if (m_BSplineInterpolator)`. */
  bool m_InterpolatorIsBSpline{ false };
#endif

  /** Pointer to BSplineInterpolator. */
  typename BSplineInterpolatorType::Pointer m_BSplineInterpolator{};

  /** Pointer to central difference calculator. */
  typename DerivativeFunctionType::Pointer m_DerivativeCalculator{};

  /** Compute image derivatives at a point using a central difference function if we are not using a
   * BSplineInterpolator, which includes derivatives.
   */
  virtual void
  ComputeImageDerivatives(const MovingImagePointType & mappedPoint,
                          ImageDerivativesType &       gradient,
                          ThreadIdType                 threadId) const;

  /**
   * Types and variables related to multi-threading
   */

  /**
   * \class  ConstantPointerWrapper
   * A class to wrap around a const pointer that can be passed
   * as a non-const object to the SetSingleMethod function
   * as a non-const void *.
   * Do not allow inheritance for objects that are intended for static_cast<void *>
   * \ingroup ITKRegistrationCommon
   */
  class ConstantPointerWrapper final
  {
  public:
    ConstantPointerWrapper(ImageToImageMetric * i2i_metricPointer)
      : m_ConstMetricPointer{ i2i_metricPointer }
    {}
    const ImageToImageMetric *
    GetConstMetricPointer() const
    {
      return m_ConstMetricPointer;
    }

  private:
    const ImageToImageMetric * m_ConstMetricPointer;
  };

  /**
   * \class MultiThreaderWorkUnitInfoImageToImageMetricWrapper
   * This helper local class is used to extract information from the
   * MultiThreaderType::WorkUnitInfo info type
   * Do not allow inheritance for objects that are intended for static_cast<void *>
   * \ingroup ITKRegistrationCommon
   */
  class MultiThreaderWorkUnitInfoImageToImageMetricWrapper final
  {
  public:
    MultiThreaderWorkUnitInfoImageToImageMetricWrapper(const void * workunitInfoAsVoid)
      : m_WorkUnitInfo(static_cast<const typename MultiThreaderType::WorkUnitInfo *>(workunitInfoAsVoid))
    {}
    ThreadIdType
    GetThreadId() const
    {
      return m_WorkUnitInfo->WorkUnitID;
    }
    const ImageToImageMetric *
    GetConstImageToImageMetricPointer() const
    {
      return (static_cast<ConstantPointerWrapper *>(m_WorkUnitInfo->UserData))->GetConstMetricPointer();
    }

  private:
    const typename MultiThreaderType::WorkUnitInfo * m_WorkUnitInfo;
  };

  MultiThreaderType::Pointer              m_Threader{};
  std::unique_ptr<ConstantPointerWrapper> m_ConstSelfWrapper;
  mutable std::unique_ptr<unsigned int[]> m_ThreaderNumberOfMovingImageSamples;
  bool                                    m_WithinThreadPreProcess{ false };
  bool                                    m_WithinThreadPostProcess{ false };

  void
  GetValueMultiThreadedInitiate() const;

  void
  GetValueMultiThreadedPostProcessInitiate() const;

  /** Get the match Measure. */
  static ITK_THREAD_RETURN_FUNCTION_CALL_CONVENTION
  GetValueMultiThreaded(void * workunitInfoAsVoid);

  /** Get the match Measure. */
  static ITK_THREAD_RETURN_FUNCTION_CALL_CONVENTION
  GetValueMultiThreadedPostProcess(void * workunitInfoAsVoid);

  /** Get the match Measure. */
  virtual inline void
  GetValueThread(ThreadIdType threadId) const;

  /** Get the match Measure. */
  virtual inline void
  GetValueThreadPreProcess(ThreadIdType itkNotUsed(threadId), bool itkNotUsed(withinSampleThread)) const
  {}
  virtual inline bool
  GetValueThreadProcessSample(ThreadIdType                 itkNotUsed(threadId),
                              SizeValueType                itkNotUsed(fixedImageSample),
                              const MovingImagePointType & itkNotUsed(mappedPoint),
                              double                       itkNotUsed(movingImageValue)) const
  {
    return false;
  }
  virtual inline void
  GetValueThreadPostProcess(ThreadIdType itkNotUsed(threadId), bool itkNotUsed(withinSampleThread)) const
  {}

  void
  GetValueAndDerivativeMultiThreadedInitiate() const;

  void
  GetValueAndDerivativeMultiThreadedPostProcessInitiate() const;

  static ITK_THREAD_RETURN_FUNCTION_CALL_CONVENTION
  GetValueAndDerivativeMultiThreaded(void * workunitInfoAsVoid);

  static ITK_THREAD_RETURN_FUNCTION_CALL_CONVENTION
  GetValueAndDerivativeMultiThreadedPostProcess(void * workunitInfoAsVoid);

  virtual inline void
  GetValueAndDerivativeThread(ThreadIdType threadId) const;

  virtual inline void
  GetValueAndDerivativeThreadPreProcess(ThreadIdType itkNotUsed(threadId), bool itkNotUsed(withinSampleThread)) const
  {}
  virtual inline bool
  GetValueAndDerivativeThreadProcessSample(ThreadIdType                 itkNotUsed(threadId),
                                           SizeValueType                itkNotUsed(fixedImageSample),
                                           const MovingImagePointType & itkNotUsed(mappedPoint),
                                           double                       itkNotUsed(movingImageValue),
                                           const ImageDerivativesType & itkNotUsed(movingImageGradientValue)) const
  {
    return false;
  }
  virtual inline void
  GetValueAndDerivativeThreadPostProcess(ThreadIdType itkNotUsed(threadId), bool itkNotUsed(withinSampleThread)) const
  {}

  /** Synchronizes the threader transforms with the transform member variable.
   *
   * This method can be const because we are not altering the m_ThreaderTransform pointer. We are altering the object
   * that m_ThreaderTransform[idx] points at.* This is allowed under C++ const rules.
   */
  virtual void
  SynchronizeTransforms() const;

private:
  FixedImageRegionType m_FixedImageRegion{};
};
} // end namespace itk

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

#endif