File: elxBSplineTransformWithDiffusion.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 (1366 lines) | stat: -rw-r--r-- 50,620 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
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
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
/*=========================================================================
 *
 *  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 elxBSplineTransformWithDiffusion_hxx
#define elxBSplineTransformWithDiffusion_hxx

#include "elxBSplineTransformWithDiffusion.h"

#include "itkBSplineResampleImageFunction.h"
#include "itkBSplineDecompositionImageFilter.h"

#include <cmath>

namespace elastix
{

/**
 * ********************* Constructor ****************************
 */

template <class TElastix>
BSplineTransformWithDiffusion<TElastix>::BSplineTransformWithDiffusion()
{
  /** Set up CombinationTransform */
  this->m_BSplineTransform = BSplineTransformType::New();
  this->SetCurrentTransform(this->m_BSplineTransform);

  /** Initialize some things. */
  this->m_GridSpacingFactor.Fill(8.0);
  this->m_Interpolator = InterpolatorType::New();

  /** Initialize things for diffusion. */
  this->m_Diffusion = nullptr;
  this->m_DeformationField = nullptr;
  this->m_DiffusedField = nullptr;
  this->m_GrayValueImage1 = nullptr;
  this->m_GrayValueImage2 = nullptr;
  this->m_MovingSegmentationImage = nullptr;
  this->m_FixedSegmentationImage = nullptr;
  this->m_MovingSegmentationReader = nullptr;
  this->m_FixedSegmentationReader = nullptr;
  this->m_MovingSegmentationFileName = "";
  this->m_FixedSegmentationFileName = "";
  this->m_Resampler1 = nullptr;
  this->m_Resampler2 = nullptr;
  this->m_WriteDiffusionFiles = false;
  this->m_AlsoFixed = true;
  this->m_ThresholdBool = true;
  this->m_ThresholdHU = static_cast<GrayValuePixelType>(150);
  this->m_UseMovingSegmentation = false;
  this->m_UseFixedSegmentation = false;

  /** Make sure that the TransformBase::WriteToFile() does
   * not write the transformParameters in the file.
   */
  this->SetReadWriteTransformParameters(false);

} // end Constructor


/**
 * ******************* BeforeRegistration ***********************
 */

template <class TElastix>
void
BSplineTransformWithDiffusion<TElastix>::BeforeRegistration()
{
  /** Set initial transform parameters to a 1x1x1 grid, with deformation (0,0,0).
   * In the method BeforeEachResolution() this will be replaced by the right grid size.
   *
   * This seems not logical, but it is required, since the registration
   * class checks if the number of parameters in the transform is equal to
   * the number of parameters in the registration class. This check is done
   * before calling the BeforeEachResolution() methods.
   */

  /** Task 1 - Set the Grid. */

  /** Declarations. */
  RegionType  gridregion;
  SizeType    gridsize;
  IndexType   gridindex;
  SpacingType gridspacing;
  OriginType  gridorigin;

  /** Fill everything with default values. */
  gridsize.Fill(1);
  gridindex.Fill(0);
  gridspacing.Fill(1.0);
  gridorigin.Fill(0.0);

  /** Set it all. */
  gridregion.SetIndex(gridindex);
  gridregion.SetSize(gridsize);
  this->m_BSplineTransform->SetGridRegion(gridregion);
  this->m_BSplineTransform->SetGridSpacing(gridspacing);
  this->m_BSplineTransform->SetGridOrigin(gridorigin);

  /** Task 2 - Give the registration an initial parameter-array. */
  ParametersType dummyInitialParameters(this->GetNumberOfParameters(), 0.0);

  /** Put parameters in the registration. */
  this->m_Registration->GetAsITKBaseType()->SetInitialTransformParameters(dummyInitialParameters);

  /** Task 3: This registration uses a diffusion of the deformation field
   * every n-th iteration; the diffusion filter must be created.
   * Also allocate this->m_DeformationField and this->m_DiffusedField.
   */

  /** Get diffusion information: radius. */
  unsigned int radius1D = 1;
  this->m_Configuration->ReadParameter(radius1D, "Radius", 0);
  RadiusType radius;
  for (unsigned int i = 0; i < this->FixedImageDimension; ++i)
  {
    radius[i] = static_cast<long unsigned int>(radius1D);
  }

  /** Get diffusion information: Number of iterations. */
  unsigned int iterations = 1;
  this->m_Configuration->ReadParameter(iterations, "NumberOfDiffusionIterations", 0);
  if (iterations < 1)
  {
    log::warn("WARNING: NumberOfDiffusionIterations == 0");
  }

  /** Get diffusion information: threshold information. */
  std::string thresholdbooltmp = "true";
  this->m_Configuration->ReadParameter(thresholdbooltmp, "ThresholdBool", 0);
  if (thresholdbooltmp == "false")
  {
    this->m_ThresholdBool = false;
  }

  float tempThresholdHU = this->m_ThresholdHU;
  this->m_Configuration->ReadParameter(tempThresholdHU, "ThresholdHU", 0);
  this->m_ThresholdHU = static_cast<GrayValuePixelType>(tempThresholdHU);

  /** Get diffusion information: is it wanted to also take the
   * fixed image into account for the derivation of the GrayValueImage.
   */
  std::string alsoFixed = "true";
  this->m_Configuration->ReadParameter(alsoFixed, "GrayValueImageAlsoBasedOnFixedImage", 0);
  if (alsoFixed == "false")
  {
    this->m_AlsoFixed = false;
  }

  /** Get diffusion information: is it wanted to base the GrayValueImage
   * on a segmentation of the moving image.
   */
  std::string useMovingSegmentation = "false";
  this->m_Configuration->ReadParameter(useMovingSegmentation, "UseMovingSegmentation", 0);
  if (useMovingSegmentation == "true")
  {
    this->m_UseMovingSegmentation = true;
  }

  /** Get diffusion information: in case m_UseMovingSegmentation = true,
   * get the filename.
   */
  if (this->m_UseMovingSegmentation)
  {
    this->m_Configuration->ReadParameter(this->m_MovingSegmentationFileName, "MovingSegmentationFileName", 0);
    if (m_MovingSegmentationFileName.empty())
    {
      log::error("ERROR: No MovingSegmentation filename specified.");
      /** Create and throw an exception. */
      itkExceptionMacro("ERROR: No MovingSegmentation filename specified.");
    }
  }

  /** Get diffusion information: is it wanted to base the GrayValueImage
   * on a segmentation of the fixed image.
   */
  std::string useFixedSegmentation = "false";
  this->m_Configuration->ReadParameter(useFixedSegmentation, "UseFixedSegmentation", 0);
  if (useFixedSegmentation == "true")
  {
    this->m_UseFixedSegmentation = true;
  }

  /** Get diffusion information: in case m_UseFixedSegmentation = true,
   * get the filename.
   */
  if (this->m_UseFixedSegmentation)
  {
    this->m_Configuration->ReadParameter(this->m_FixedSegmentationFileName, "FixedSegmentationFileName", 0);
    if (m_FixedSegmentationFileName.empty())
    {
      log::error("ERROR: No FixedSegmentation filename specified.");
      /** Create and throw an exception. */
      itkExceptionMacro("ERROR: No FixedSegmentation filename specified.");
    }
  }

  /** Get diffusion information: Find out if the user wants
   * to write the diffusion files:
   * deformationField, GrayvalueImage, diffusedField.
   */
  std::string writetofile = "false";
  this->m_Configuration->ReadParameter(writetofile, "WriteDiffusionFiles", 0);
  if (writetofile == "true")
  {
    this->m_WriteDiffusionFiles = true;
  }

  /** Get the appropriate image information. */
  this->m_DeformationOrigin = this->m_Elastix->GetElxResamplerBase()->GetAsITKBaseType()->GetOutputOrigin();
  this->m_DeformationSpacing = this->m_Elastix->GetElxResamplerBase()->GetAsITKBaseType()->GetOutputSpacing();
  this->m_DeformationRegion.SetIndex(this->m_Elastix->GetElxResamplerBase()->GetAsITKBaseType()->GetOutputStartIndex());
  this->m_DeformationRegion.SetSize(this->m_Elastix->GetElxResamplerBase()->GetAsITKBaseType()->GetSize());

  /** Set it in the DeformationFieldRegulizer class. */
  this->SetDeformationFieldRegion(this->m_DeformationRegion);
  this->SetDeformationFieldOrigin(this->m_DeformationOrigin);
  this->SetDeformationFieldSpacing(this->m_DeformationSpacing);

  /** Initialize the this->m_IntermediaryDeformationFieldTransform,
   * which is in the DeformationFieldRegulizer class.
   */
  this->InitializeDeformationFields();

  /** Create this->m_DeformationField and allocate memory. */
  this->m_DeformationField = VectorImageType::New();
  this->m_DeformationField->SetRegions(this->m_DeformationRegion);
  this->m_DeformationField->SetOrigin(this->m_DeformationOrigin);
  this->m_DeformationField->SetSpacing(this->m_DeformationSpacing);
  this->m_DeformationField->Allocate();

  /** Create this->m_DeformationField and allocate memory. */
  this->m_DiffusedField = VectorImageType::New();
  this->m_DiffusedField->SetRegions(this->m_DeformationRegion);
  this->m_DiffusedField->SetOrigin(this->m_DeformationOrigin);
  this->m_DiffusedField->SetSpacing(this->m_DeformationSpacing);
  this->m_DiffusedField->Allocate();

  /** Create the GrayValueImages and allocate memory. */
  if (this->m_UseMovingSegmentation && !this->m_ThresholdBool)
  {
    /** In this case we have to read in the m_MovingSegmentationImage. */
    this->m_MovingSegmentationReader = GrayValueImageReaderType::New();
    this->m_MovingSegmentationReader->SetFileName(m_MovingSegmentationFileName);
    this->m_MovingSegmentationImage = m_MovingSegmentationReader->GetOutput();

    /** Read the MovingSegmentation. */
    try
    {
      this->m_MovingSegmentationImage->Update();
    }
    catch (itk::ExceptionObject & excp)
    {
      /** Add information to the exception. */
      excp.SetLocation("BSplineTransformWithDiffusion - BeforeRegistration()");
      std::string err_str = excp.GetDescription();
      err_str += "\nError occurred while reading the MovingSegmentationImage.\n";
      excp.SetDescription(err_str);
      /** Pass the exception to an higher level. */
      throw;
    }

    /** In this case: check if a FixedSegmentationImage is needed. */
    if (this->m_UseFixedSegmentation)
    {
      /** In this case we have to read in the m_FixedSegmentationImage. */
      this->m_FixedSegmentationReader = GrayValueImageReaderType::New();
      this->m_FixedSegmentationReader->SetFileName(m_FixedSegmentationFileName);
      this->m_FixedSegmentationImage = m_FixedSegmentationReader->GetOutput();

      /** Read the FixedSegmentation. */
      try
      {
        this->m_FixedSegmentationImage->Update();
      }
      catch (itk::ExceptionObject & excp)
      {
        /** Add information to the exception. */
        excp.SetLocation("BSplineTransformWithDiffusion - BeforeRegistration()");
        std::string err_str = excp.GetDescription();
        err_str += "\nError occurred while reading the FixedSegmentationImage.\n";
        excp.SetDescription(err_str);
        /** Pass the exception to an higher level. */
        throw;
      } // end try/catch
    }   // end if fixed segmentation
  }     // end if moving segmentation
  /** Otherwise defining rigid object is based on thresholding the resampled moving image. */
  else if (!this->m_UseMovingSegmentation && this->m_ThresholdBool)
  {
    this->m_GrayValueImage1 = GrayValueImageType::New();
    this->m_GrayValueImage1->SetRegions(this->m_DeformationRegion);
    this->m_GrayValueImage1->SetOrigin(this->m_DeformationOrigin);
    this->m_GrayValueImage1->SetSpacing(this->m_DeformationSpacing);
    this->m_GrayValueImage1->Allocate();
    this->m_GrayValueImage2 = GrayValueImageType::New();
  }
  else
  {
    log::error(std::ostringstream{} << "ERROR: So what are you using for the GrayValueImage,\n"
                                    << "either a threshold or a segmentation, make a choice!");

    /** Create and throw an exception. */
    itkExceptionMacro("ERROR: Difficulty determining how to create the GrayValueImage. Check your parameter file.");
  }

  /** Set the interpolator. */
  if (this->m_UseMovingSegmentation)
  {
    this->m_Interpolator->SetSplineOrder(0);
  }
  else
  {
    this->m_Interpolator->SetSplineOrder(1);
  }

  /** Create a resampler. */
  if (this->m_UseMovingSegmentation)
  {
    this->m_Resampler2 = ResamplerType2::New();
    this->m_Resampler2->SetTransform(this->GetIntermediaryDeformationFieldTransform());
    this->m_Resampler2->SetInterpolator(this->m_Interpolator); // default = LinearInterpolateImageFunction
  }
  else
  {
    this->m_Resampler1 = ResamplerType1::New();
    this->m_Resampler1->SetTransform(this->GetIntermediaryDeformationFieldTransform());
    this->m_Resampler1->SetInterpolator(this->m_Interpolator); // default = LinearInterpolateImageFunction
  }

  /** What are we using for defining rigid structures? */
  if (this->m_UseMovingSegmentation && !this->m_ThresholdBool)
  {
    this->m_Resampler2->SetInput(this->m_MovingSegmentationImage);
  }
  else
  {
    this->m_Resampler1->SetInput(this->m_Elastix->GetMovingImage());
  }

  /** Get the default pixel value. */
  float defaultPixelValueForGVI = 0.0f;
  if (this->m_UseMovingSegmentation && !this->m_ThresholdBool)
  {
    this->m_Configuration->ReadParameter(defaultPixelValueForGVI, "DefaultPixelValueForGVI", 0);
    this->m_Resampler2->SetDefaultPixelValue(static_cast<GrayValuePixelType>(defaultPixelValueForGVI));
  }
  else
  {
    this->m_Configuration->ReadParameter(defaultPixelValueForGVI, "DefaultPixelValue", 0);
    this->m_Resampler1->SetDefaultPixelValue(static_cast<GrayValuePixelType>(defaultPixelValueForGVI));
  }

  /** Set other stuff. */
  if (this->m_UseMovingSegmentation)
  {
    this->m_Resampler2->SetSize(this->m_DeformationRegion.GetSize());
    this->m_Resampler2->SetOutputStartIndex(this->m_DeformationRegion.GetIndex());
    this->m_Resampler2->SetOutputOrigin(this->m_DeformationOrigin);
    this->m_Resampler2->SetOutputSpacing(this->m_DeformationSpacing);
  }
  else
  {
    this->m_Resampler1->SetSize(this->m_DeformationRegion.GetSize());
    this->m_Resampler1->SetOutputStartIndex(this->m_DeformationRegion.GetIndex());
    this->m_Resampler1->SetOutputOrigin(this->m_DeformationOrigin);
    this->m_Resampler1->SetOutputSpacing(this->m_DeformationSpacing);
  }

  /** Create this->m_Diffusion, the diffusion filter. */
  this->m_Diffusion = DiffusionFilterType::New();
  this->m_Diffusion->SetRadius(radius);
  this->m_Diffusion->SetNumberOfIterations(iterations);
  this->m_Diffusion->SetGrayValueImage(this->m_GrayValueImage1);
  this->m_Diffusion->SetInput(this->m_DeformationField);

} // end BeforeRegistration()


/**
 * ***************** BeforeEachResolution ***********************
 */

template <class TElastix>
void
BSplineTransformWithDiffusion<TElastix>::BeforeEachResolution()
{
  /** What is the current resolution level? */
  unsigned int level = this->m_Registration->GetAsITKBaseType()->GetCurrentLevel();

  /** What is the UpsampleGridOption?
   * This option defines the user's wish:
   * - true: For lower resolution levels (i.e. smaller images),
   *         the GridSpacing is made larger, as a power of 2.
   * - false: The GridSpacing remains equal for each resolution level.
   */
  std::string upsampleBSplineGridOption("true");
  bool        upsampleGridOption = true;
  this->m_Configuration->ReadParameter(upsampleBSplineGridOption, "UpsampleGridOption", 0, false);
  if (upsampleBSplineGridOption == "true")
  {
    upsampleGridOption = true;
  }
  else if (upsampleBSplineGridOption == "false")
  {
    upsampleGridOption = false;
  }

  /** Resample the grid. */
  if (level == 0)
  {
    /** Set grid equal to lowest resolution fixed image. */
    this->SetInitialGrid(upsampleGridOption);
  }
  else
  {
    /** If wanted, we upsample the grid of control points. */
    if (upsampleGridOption)
    {
      this->IncreaseScale();
    }
    /** Otherwise, nothing is done with the B-spline grid. */
  }

} // end BeforeEachResolution()


/**
 * ********************* AfterEachIteration *********************
 */

template <class TElastix>
void
BSplineTransformWithDiffusion<TElastix>::AfterEachIteration()
{
  /** Declare boolean. */
  bool DiffusionNow = false;

  /** Find out filter pattern. */
  unsigned int filterPattern = 1;
  this->m_Configuration->ReadParameter(filterPattern, "FilterPattern", 0);
  if (filterPattern != 1 && filterPattern != 2)
  {
    filterPattern = 1;
    log::warn("WARNING: filterPattern set to 1");
  }

  /** Get the current iteration number. */
  unsigned int CurrentIterationNumber = this->m_Elastix->GetIterationCounter();

  /** Get the MaximumNumberOfIterations of this resolution level. */
  unsigned int ResNr = this->m_Elastix->GetElxRegistrationBase()->GetAsITKBaseType()->GetCurrentLevel();
  unsigned int maximumNumberOfIterations = 0;
  this->m_Configuration->ReadParameter(maximumNumberOfIterations, "MaximumNumberOfIterations", ResNr);

  /** Find out if we have to filter now.
   * FilterPattern1: diffusion every n iterations
   * FilterPattern2: start with diffusion every n1 iterations,
   *    followed by diffusion every n2 iterations, and ended
   *    by by diffusion every n3 iterations.
   */
  if (filterPattern == 1)
  {
    /** Find out after how many iterations a diffusion is wanted. */
    unsigned int diffusionEachNIterations = 0;
    this->m_Configuration->ReadParameter(diffusionEachNIterations, "DiffusionEachNIterations", 0);

    /** Checking DiffusionEachNIterations. */
    if (diffusionEachNIterations < 1)
    {
      log::warn(std::ostringstream{} << "WARNING: DiffusionEachNIterations < 1\n"
                                     << "\t\tDiffusionEachNIterations is set to 1");
      diffusionEachNIterations = 1;
    }

    /** Determine if diffusion is wanted after this iteration:
     * Do it every n iterations, but not at the first iteration
     * of a resolution, and also at the last iteration.
     */
    DiffusionNow = ((CurrentIterationNumber + 1) % diffusionEachNIterations == 0);
    DiffusionNow &= (CurrentIterationNumber != 0);
    DiffusionNow |= (CurrentIterationNumber == (maximumNumberOfIterations - 1));
  }
  else if (filterPattern == 2)
  {
    /** Find out after how many iterations a change in n_i is needed. */
    unsigned int afterIterations0 = 50;
    unsigned int afterIterations1 = 100;
    this->m_Configuration->ReadParameter(afterIterations0, "AfterIterations", 0);
    this->m_Configuration->ReadParameter(afterIterations1, "AfterIterations", 1);

    /** Find out n1, n2 and n3. */
    unsigned int howManyIterations0 = 1;
    unsigned int howManyIterations1 = 5;
    unsigned int howManyIterations2 = 10;
    this->m_Configuration->ReadParameter(howManyIterations0, "HowManyIterations", 0);
    this->m_Configuration->ReadParameter(howManyIterations1, "HowManyIterations", 1);
    this->m_Configuration->ReadParameter(howManyIterations2, "HowManyIterations", 2);

    /** The first afterIterations0 the deformationField is filtered
     * every howManyIterations0 iterations. Then, for iterations between
     * afterIterations0 and afterIterations1 , the deformationField
     * is filtered after every howManyIterations1 iterations. Finally,
     * the deformationField is filtered every howManyIterations2 iterations.
     */
    unsigned int diffusionEachNIterations;
    if (CurrentIterationNumber < afterIterations0)
    {
      diffusionEachNIterations = howManyIterations0;
    }
    else if (CurrentIterationNumber >= afterIterations0 && CurrentIterationNumber < afterIterations1)
    {
      diffusionEachNIterations = howManyIterations1;
    }
    else
    {
      diffusionEachNIterations = howManyIterations2;
    }

    /** Filter the current iteration? Also filter after the last iteration. */
    DiffusionNow = ((CurrentIterationNumber + 1) % diffusionEachNIterations == 0);
    DiffusionNow |= (CurrentIterationNumber == (maximumNumberOfIterations - 1));

  } // end if filterpattern

  /** If wanted (DiffusionNow == true), do a diffusion. */
  if (DiffusionNow)
  {
    this->DiffuseDeformationField();
  }

} // end AfterEachIteration()


/**
 * ******************* AfterRegistration ***********************
 */

template <class TElastix>
void
BSplineTransformWithDiffusion<TElastix>::AfterRegistration()
{
  /** Destruct some member variables that are not necessary to keep in
   * memory. Only those variables needed for the transform parameters
   * have to be kept.
   */
  this->m_GrayValueImage1 = nullptr;
  this->m_GrayValueImage2 = nullptr;
  this->m_Resampler1 = nullptr;
  this->m_Resampler2 = nullptr;
  this->m_MovingSegmentationReader = nullptr;
  this->m_FixedSegmentationReader = nullptr;
  this->m_MovingSegmentationImage = nullptr;
  this->m_FixedSegmentationImage = nullptr;
  this->m_Diffusion = nullptr;

  /** In the very last iteration of the registration in the function
   * DiffuseDeformationField() the intermediary deformation field is updated:
   * this->UpdateIntermediaryDeformationFieldTransform( this->m_DiffusedField );
   * This function copies the deformation field into number of dimensions
   * coefficient images (the DeformationFieldTransform is actually a
   * BSplineTransform).
   * Therefore the memory of the deformation fields can be freed.
   */
  this->m_DeformationField = nullptr;
  this->m_DiffusedField = nullptr;

} // end AfterRegistration()


/**
 * ********************* SetInitialGrid *************************
 *
 * Set the size of the initial control point grid.
 *
 * If multiresolution (UpsampleGridOption is "true") then the
 * grid size is equal to the size of the fixed image, divided by
 * desired final gridspacing and a factor 2^(NrOfImageResolutions-1).
 * Otherwise it's equal to the size of the fixed image, divided by
 * the desired final gridspacing.
 *
 * In both cases some extra grid points are put at the edges,
 * to take into account the support region of the B-splines.
 */

template <class TElastix>
void
BSplineTransformWithDiffusion<TElastix>::SetInitialGrid(bool upsampleGridOption)
{
  /** Declarations. */
  RegionType  gridregion;
  SizeType    gridsize;
  IndexType   gridindex;
  SpacingType gridspacing;
  OriginType  gridorigin;

  /** Get the fixed image. */
  typename FixedImageType::Pointer fixedimage;
  fixedimage = const_cast<FixedImageType *>(this->m_Registration->GetAsITKBaseType()->GetFixedImage());

  /** Get the size etc. of this image. */

  /** In elastix <=3.001: gridregion  = fixedimage->GetRequestedRegion();  */
  /** later (because requested regions were not supported anyway consistently: */
  gridregion = fixedimage->GetLargestPossibleRegion();
  /** \todo: allow the user to enter a region of interest for the registration.
   * Especially the boundary conditions have to be dealt with carefully then.
   */
  gridindex = gridregion.GetIndex();
  /** \todo: always 0? doesn't a largest possible region have an index 0 by definition? */
  gridsize = gridregion.GetSize();
  gridspacing = fixedimage->GetSpacing();
  gridorigin = fixedimage->GetOrigin();

  /** Read the desired grid spacing for each dimension. If only one gridspacing factor
   * is given, that one is used for each dimension.
   */
  this->m_GridSpacingFactor[0] = 8.0;
  this->m_Configuration->ReadParameter(this->m_GridSpacingFactor[0], "FinalGridSpacing", 0);
  this->m_GridSpacingFactor.Fill(this->m_GridSpacingFactor[0]);
  for (unsigned int j = 1; j < SpaceDimension; ++j)
  {
    this->m_Configuration->ReadParameter(this->m_GridSpacingFactor[j], "FinalGridSpacing", j);
  }

  /** If multigrid, then start with a lower resolution grid. */
  if (upsampleGridOption)
  {
    int nrOfResolutions = static_cast<int>(this->GetRegistration()->GetAsITKBaseType()->GetNumberOfLevels());
    this->m_GridSpacingFactor *= std::pow(2.0, static_cast<double>(nrOfResolutions - 1));
  }

  /** Determine the correct grid size. */
  for (unsigned int j = 0; j < SpaceDimension; ++j)
  {
    gridspacing[j] = gridspacing[j] * this->m_GridSpacingFactor[j];
    gridorigin[j] -= gridspacing[j] * std::floor(static_cast<double>(SplineOrder) / 2.0);
    gridindex[j] = 0; // isn't this always the case anyway?
    gridsize[j] = static_cast<typename RegionType::SizeValueType>(
      std::ceil(gridsize[j] / this->m_GridSpacingFactor[j]) + SplineOrder);
  }

  /** Set the size data in the transform. */
  gridregion.SetSize(gridsize);
  gridregion.SetIndex(gridindex);
  this->m_BSplineTransform->SetGridRegion(gridregion);
  this->m_BSplineTransform->SetGridSpacing(gridspacing);
  this->m_BSplineTransform->SetGridOrigin(gridorigin);

  /** Set initial parameters to 0.0. */
  ParametersType initialParameters(this->GetNumberOfParameters(), 0.0);
  this->m_Registration->GetAsITKBaseType()->SetInitialTransformParametersOfNextLevel(initialParameters);

} // end SetInitialGrid()


/**
 * *********************** IncreaseScale ************************
 *
 * Upsample the grid of control points.
 */

template <class TElastix>
void
BSplineTransformWithDiffusion<TElastix>::IncreaseScale()
{
  /** Typedefs. */
  using UpsampleFilterType = itk::ResampleImageFilter<ImageType, ImageType>;
  using IdentityTransformType = itk::IdentityTransform<CoordRepType, SpaceDimension>;
  using CoefficientUpsampleFunctionType = itk::BSplineResampleImageFunction<ImageType, CoordRepType>;
  using DecompositionFilterType = itk::BSplineDecompositionImageFilter<ImageType, ImageType>;

  /** The current region/spacing settings of the grid. */
  RegionType gridregionLow = this->m_BSplineTransform->GetGridRegion();
  // SizeType gridsizeLow = gridregionLow.GetSize();
  // IndexType gridindexLow = gridregionLow.GetIndex();
  // SpacingType gridspacingLow = this->m_BSplineTransform->GetGridSpacing();
  // OriginType gridoriginLow = this->m_BSplineTransform->GetGridOrigin();

  /** Get the fixed image. */
  typename FixedImageType::Pointer fixedimage;
  fixedimage = const_cast<FixedImageType *>(this->m_Registration->GetAsITKBaseType()->GetFixedImage());

  /** Set start values for computing the new grid size. */
  RegionType  gridregionHigh = fixedimage->GetLargestPossibleRegion();
  IndexType   gridindexHigh = gridregionHigh.GetIndex();
  SizeType    gridsizeHigh = gridregionHigh.GetSize();
  SpacingType gridspacingHigh = fixedimage->GetSpacing();
  OriginType  gridoriginHigh = fixedimage->GetOrigin();

  /** A twice as dense grid: */
  this->m_GridSpacingFactor /= 2;

  /** Determine the correct grid size. */
  for (unsigned int j = 0; j < SpaceDimension; ++j)
  {
    gridspacingHigh[j] = gridspacingHigh[j] * this->m_GridSpacingFactor[j];
    gridoriginHigh[j] -= gridspacingHigh[j] * std::floor(static_cast<double>(SplineOrder) / 2.0);
    gridindexHigh[j] = 0; // isn't this always the case anyway?
    gridsizeHigh[j] = static_cast<typename RegionType::SizeValueType>(
      std::ceil(gridsizeHigh[j] / this->m_GridSpacingFactor[j]) + SplineOrder);
  }
  gridregionHigh.SetSize(gridsizeHigh);
  gridregionHigh.SetIndex(gridindexHigh);

  /** Get the latest transform parameters. */
  ParametersType latestParameters = this->m_Registration->GetAsITKBaseType()->GetLastTransformParameters();

  /** Get the pointer to the data in latestParameters. */
  PixelType * dataPointer = static_cast<PixelType *>(latestParameters.data_block());
  /** Get the number of pixels that should go into one coefficient image. */
  unsigned int numberOfPixels = (this->m_BSplineTransform->GetGridRegion()).GetNumberOfPixels();

  /** Set the correct region/size info of the coefficient image
   * that will be filled with the current parameters.
   */
  ImagePointer coeffs1 = ImageType::New();
  coeffs1->SetRegions(this->m_BSplineTransform->GetGridRegion());
  coeffs1->SetOrigin((this->m_BSplineTransform->GetGridOrigin()).GetDataPointer());
  coeffs1->SetSpacing((this->m_BSplineTransform->GetGridSpacing()).GetDataPointer());
  // coeffs1->Allocate() not needed because the data is set by directly pointing
  // to an existing piece of memory.

  /**
   * Create the new vector of parameters, with the
   * correct size (which is now approx 2^dim as big as the
   * size in the previous resolution!).
   */
  ParametersType parameters_out(gridregionHigh.GetNumberOfPixels() * SpaceDimension);

  /** Initialize iterator in the parameters_out. */
  unsigned int i = 0;

  /** Loop over dimension. */
  for (unsigned int j = 0; j < SpaceDimension; ++j)
  {
    /** Fill the coefficient image with parameter data (displacements
     * of the control points in the direction of dimension j).
     */
    coeffs1->GetPixelContainer()->SetImportPointer(dataPointer, numberOfPixels);
    dataPointer += numberOfPixels;

    /*
     * Set this image as the input of the upsampler filter. The
     * upsampler samples the deformation field at the locations
     * of the new control points (note: it does not just interpolate
     * the coefficient image, which would be wrong). The B-spline
     * coefficients that describe the resulting image are computed
     * by the decomposition filter.
     *
     * This code is copied from the itk-example
     * DeformableRegistration6.cxx .
     */

    auto upsampler = UpsampleFilterType::New();
    auto identity = IdentityTransformType::New();
    auto coeffUpsampleFunction = CoefficientUpsampleFunctionType::New();
    auto decompositionFilter = DecompositionFilterType::New();

    upsampler->SetInterpolator(coeffUpsampleFunction);
    upsampler->SetTransform(identity);
    upsampler->SetSize(gridsizeHigh);
    upsampler->SetOutputStartIndex(gridindexHigh);
    upsampler->SetOutputSpacing(gridspacingHigh);
    upsampler->SetOutputOrigin(gridoriginHigh);
    upsampler->SetInput(coeffs1);

    decompositionFilter->SetSplineOrder(SplineOrder);
    decompositionFilter->SetInput(upsampler->GetOutput());

    /** Do the upsampling. */
    try
    {
      decompositionFilter->UpdateLargestPossibleRegion();
    }
    catch (itk::ExceptionObject & excp)
    {
      /** Add information to the exception. */
      excp.SetLocation("BSplineTransform - IncreaseScale()");
      std::string err_str = excp.GetDescription();
      err_str += "\nError occurred while using decompositionFilter.\n";
      excp.SetDescription(err_str);
      /** Pass the exception to an higher level. */
      throw;
    }

    /** Create an upsampled image. */
    ImagePointer coeffs2 = decompositionFilter->GetOutput();

    /** Create an iterator on the new coefficient image. */
    itk::ImageRegionConstIterator<ImageType> iterator(coeffs2, gridregionHigh);
    iterator.GoToBegin();
    while (!iterator.IsAtEnd())
    {
      /** Copy the contents of coeffs2 in a ParametersType array. */
      parameters_out[i] = iterator.Get();
      ++iterator;
      ++i;
    } // end while coeff2 iterator loop

  } // end for dimension loop

  /** Set the initial parameters for the next resolution level. */
  this->m_BSplineTransform->SetGridRegion(gridregionHigh);
  this->m_BSplineTransform->SetGridOrigin(gridoriginHigh);
  this->m_BSplineTransform->SetGridSpacing(gridspacingHigh);
  this->m_Registration->GetAsITKBaseType()->SetInitialTransformParametersOfNextLevel(parameters_out);

} // end IncreaseScale()


/**
 * ************************* ReadFromFile ************************
 */

template <class TElastix>
void
BSplineTransformWithDiffusion<TElastix>::ReadFromFile()
{
  /** Task 1 - Get and Set the DeformationField Image. */

  /** Read the name of the deformationFieldImage. */
  std::string fileName = "";
  this->m_Configuration->ReadParameter(fileName, "DeformationFieldFileName", 0);

  /** Error checking ... */
  if (fileName.empty())
  {
    log::error(std::ostringstream{} << "ERROR: DeformationFieldFileName not specified.\n"
                                    << "Unable to read and set the transform parameters.");
    // \todo quit program nicely or throw an exception
  }

  /** Read in the deformationField image. */
  auto vectorReader = VectorReaderType::New();
  vectorReader->SetFileName(fileName);

  /** Do the reading. */
  try
  {
    vectorReader->Update();
  }
  catch (itk::ExceptionObject & excp)
  {
    /** Add information to the exception. */
    excp.SetLocation("BSplineTransformWithDiffusion - ReadFromFile()");
    std::string err_str = excp.GetDescription();
    err_str += "\nError while reading the deformationFieldImage.\n";
    excp.SetDescription(err_str);
    /** Pass the exception to an higher level. */
    throw;
  }

  /** Get image information and set it in the DeformationFieldTransform. */
  RegionType  region = vectorReader->GetOutput()->GetLargestPossibleRegion();
  SpacingType spacing = vectorReader->GetOutput()->GetSpacing();
  OriginType  origin = vectorReader->GetOutput()->GetOrigin();
  this->SetDeformationFieldRegion(region);
  this->SetDeformationFieldSpacing(spacing);
  this->SetDeformationFieldOrigin(origin);
  this->InitializeDeformationFields();

  /** Set the deformationFieldImage in the itkDeformationVectorFieldTransform. */
  this->UpdateIntermediaryDeformationFieldTransform(vectorReader->GetOutput());

  /** Task 2 - Get and Set the B-spline part of this transform. */

  /** Declarations of the B-spline grid and fill everything with default values. */
  RegionType  gridregion;
  SizeType    gridsize;
  IndexType   gridindex;
  SpacingType gridspacing;
  OriginType  gridorigin;
  gridsize.Fill(1);
  gridindex.Fill(0);
  gridspacing.Fill(1.0);
  gridorigin.Fill(0.0);

  /** Get GridSize, GridIndex, GridSpacing and GridOrigin. */
  for (unsigned int i = 0; i < SpaceDimension; ++i)
  {
    this->m_Configuration->ReadParameter(gridsize[i], "GridSize", i);
    this->m_Configuration->ReadParameter(gridindex[i], "GridIndex", i);
    this->m_Configuration->ReadParameter(gridspacing[i], "GridSpacing", i);
    this->m_Configuration->ReadParameter(gridorigin[i], "GridOrigin", i);
  }

  /** Set the B-spline grid. */
  gridregion.SetIndex(gridindex);
  gridregion.SetSize(gridsize);
  this->m_BSplineTransform->SetGridRegion(gridregion);
  this->m_BSplineTransform->SetGridSpacing(gridspacing);
  this->m_BSplineTransform->SetGridOrigin(gridorigin);

  /** Fill and set the B-spline parameters. */
  unsigned int nop = 0;
  this->m_Configuration->ReadParameter(nop, "NumberOfParameters", 0);
  this->m_BSplineParameters.SetSize(nop);
  this->m_BSplineParameters.Fill(0.0);
  this->SetParameters(this->m_BSplineParameters);

  /** Do not call the ReadFromFile from the TransformBase,
   * because that function tries to read parameters from the file,
   * which is not necessary in this case, because the parameters are
   * in the vectorImage.
   * NOT: this->Superclass2::ReadFromFile();
   * However, we do have to copy some of the functionality:
   */

  /** Task 3 - Get and Set the Initial Transform. */

  /** Get the name of the parameter file that specifies the initial transform. */

  // Retrieve the parameter by its current (preferred) parameter name:
  const auto initialTransformParameterFileName =
    this->m_Configuration->RetrieveParameterStringValue({}, "InitialTransformParameterFileName", 0, false);
  // Retrieve the parameter by its old (deprecated) parameter name as well:
  const auto initialTransformParametersFileName =
    this->m_Configuration->RetrieveParameterStringValue({}, "InitialTransformParametersFileName", 0, false);

  if (!initialTransformParametersFileName.empty())
  {
    log::warn("WARNING: The parameter name \"InitialTransformParametersFileName\" is deprecated. Please use "
              "\"InitialTransformParameterFileName\" (without letter 's') instead.");
  }

  // Prefer the value from the current parameter name, otherwise use the old parameter name.
  fileName =
    initialTransformParameterFileName.empty() ? initialTransformParametersFileName : initialTransformParameterFileName;

  /** Call the function ReadInitialTransformFromFile.*/
  if (!fileName.empty() && fileName != "NoInitialTransform")
  {
    this->ReadInitialTransformFromFile(fileName);
  }

  /** Task 3 - Read from the configuration file how to combine the
   * initial transform with the current transform.
   */
  std::string howToCombineTransforms = "Add"; // default
  this->m_Configuration->ReadParameter(howToCombineTransforms, "HowToCombineTransforms", 0, false);

  /** Convert 'this' to a pointer to a CombinationTransformType and set how
   * to combine the current transform with the initial transform */
  /** Cast to transform grouper. */
  CombinationTransformType * thisAsGrouper = dynamic_cast<CombinationTransformType *>(this);
  if (thisAsGrouper)
  {
    if (howToCombineTransforms == "Compose")
    {
      thisAsGrouper->SetUseComposition(true);
    }
    else
    {
      thisAsGrouper->SetUseComposition(false);
    }
  }

  /** Task 4 - Remember the name of the TransformParameterFileName.
   * This will be needed when another transform will use this transform
   * as an initial transform (see the WriteToFile method)
   */
  this->SetTransformParameterFileName(this->GetConfiguration()->GetCommandLineArgument("-tp"));

} // end ReadFromFile()


/**
 * ************************* WriteDerivedTransformDataToFile ************************
 *
 * Saves the TransformParameters as a vector and if wanted
 * also as a deformation field.
 */

template <class TElastix>
void
BSplineTransformWithDiffusion<TElastix>::WriteDerivedTransformDataToFile() const
{
  /** Write the deformation field image. */
  try
  {
    itk::WriteImage(m_DiffusedField, TransformIO::MakeDeformationFieldFileName(*this));
  }
  catch (itk::ExceptionObject & excp)
  {
    /** Add information to the exception. */
    excp.SetLocation("BSplineTransformWithDiffusion - WriteDerivedTransformDataToFile()");
    std::string err_str = excp.GetDescription();
    err_str += "\nError while writing the deformationFieldImage.\n";
    excp.SetDescription(err_str);
    /** Print the exception. */
    log::error(std::ostringstream{} << excp);
  }

} // end WriteDerivedTransformDataToFile()


/**
 * ************************* CustomizeTransformParameterMap ************************
 */

template <class TElastix>
auto
BSplineTransformWithDiffusion<TElastix>::CreateDerivedTransformParameterMap() const -> ParameterMapType
{
  const auto & itkTransform = *m_BSplineTransform;
  const auto   gridRegion = itkTransform.GetGridRegion();

  // TODO If necessary, add possibly missing parameters:
  // - "GridDirection" (as returned by RecursiveBSplineTransform).
  // - "ThresholdBool", "ThresholdHU", etc. (as retrieved by ReadParameter).
  return { { "DeformationFieldFileName", { TransformIO::MakeDeformationFieldFileName(*this) } },
           { "GridSize", Conversion::ToVectorOfStrings(gridRegion.GetSize()) },
           { "GridIndex", Conversion::ToVectorOfStrings(gridRegion.GetIndex()) },
           { "GridSpacing", Conversion::ToVectorOfStrings(itkTransform.GetGridSpacing()) },
           { "GridOrigin", Conversion::ToVectorOfStrings(itkTransform.GetGridOrigin()) } };

} // end CustomizeTransformParameterMap()


/**
 * ******************* DiffuseDeformationField ******************
 */

template <class TElastix>
void
BSplineTransformWithDiffusion<TElastix>::DiffuseDeformationField()
{
  /** This function does:
   * 1) Calculate current deformation field.
   * 2) Update the intermediary deformationFieldTransform
   *    with this deformation field.
   * 3) Calculate the GrayValueImage with the resampler,
   *    which is over the intermediary deformationFieldTransform.
   * 4) Diffuse the current deformation field.
   * 5) Update the intermediary deformationFieldTransform
   *    with this diffused deformation field.
   * 6) Reset the parameters of the BSplineTransform
   *    and the optimizer. Reset the initial transform.
   * 7) If wanted, write the deformationField, the
   *    GrayValueImage and the diffusedField.
   */

  /** ------------- 1: Create deformationField. ------------- */

  /** First, create a dummyImage with the right region info, so
   * that the TransformIndexToPhysicalPoint-functions will be right.
   */
  auto dummyImage = DummyImageType::New();
  dummyImage->SetRegions(this->m_DeformationRegion);
  dummyImage->SetOrigin(this->m_DeformationOrigin);
  dummyImage->SetSpacing(this->m_DeformationSpacing);

  /** Setup an iterator over dummyImage and outputImage. */
  DummyIteratorType       iter(dummyImage, this->m_DeformationRegion);
  VectorImageIteratorType iterout(this->m_DeformationField, this->m_DeformationRegion);

  /** Declare stuff. */
  InputPointType  inputPoint;
  OutputPointType outputPoint;
  VectorType      diff_point;
  IndexType       inputIndex;

  /** Calculate the TransformPoint of all voxels of the image. */
  iter.GoToBegin();
  iterout.GoToBegin();
  while (!iter.IsAtEnd())
  {
    inputIndex = iter.GetIndex();
    /** Transform the points to physical space. */
    dummyImage->TransformIndexToPhysicalPoint(inputIndex, inputPoint);
    /** Call TransformPoint. */
    outputPoint = this->TransformPoint(inputPoint);
    /** Calculate the difference. */
    for (unsigned int i = 0; i < this->FixedImageDimension; ++i)
    {
      diff_point[i] = outputPoint[i] - inputPoint[i];
    }
    iterout.Set(diff_point);
    ++iter;
    ++iterout;
  }

  /** ------------- 2: Update the intermediary deformationFieldTransform. ------------- */

  this->UpdateIntermediaryDeformationFieldTransform(this->m_DeformationField);

  /** ------------- 3: Create GrayValueImage. ------------- */

  /** This gives:
   * - either a deformed moving image, in case that the grayValueImage
   *   is based on a threshold and not on a segmentation,
   * - or a deformed segmentation of the moving image, in case that
   *   the grayValueImage is based on a segmentation and not on a threshold.
   */
  if (this->m_UseMovingSegmentation)
  {
    this->m_Resampler2->Modified();
    this->m_GrayValueImage1 = this->m_Resampler2->GetOutput();
  }
  else
  {
    this->m_Resampler1->Modified();
    this->m_GrayValueImage1 = this->m_Resampler1->GetOutput();
  }

  /** Do the resampling. */
  try
  {
    this->m_GrayValueImage1->Update();
  }
  catch (itk::ExceptionObject & excp)
  {
    /** Add information to the exception. */
    excp.SetLocation("BSplineTransformWithDiffusion - DiffuseDeformationField()");
    std::string err_str = excp.GetDescription();
    err_str += "\nError occurred while resampling the grayValue image.\n";
    excp.SetDescription(err_str);
    /** Pass the exception to an higher level. */
    throw;
  }

  /** First we make a distinction between using segmentation or not. */
  if (!this->m_UseMovingSegmentation)
  {
    /** If wanted also take the fixed image into account
     * for the derivation of the GrayValueImage, by taking the maximum.
     */
    typename MaximumImageFilterType::Pointer maximumImageFilter;
    if (this->m_AlsoFixed)
    {
      maximumImageFilter = MaximumImageFilterType::New();
      maximumImageFilter->SetInput(0, this->m_GrayValueImage1);
      maximumImageFilter->SetInput(1, this->m_Elastix->GetFixedImage());
      this->m_GrayValueImage2 = maximumImageFilter->GetOutput();

      /** Do the maximum (OR filter). */
      try
      {
        this->m_GrayValueImage2->Update();
      }
      catch (itk::ExceptionObject & excp)
      {
        /** Add information to the exception. */
        excp.SetLocation("BSplineTransformWithDiffusion - DiffuseDeformationField()");
        std::string err_str = excp.GetDescription();
        err_str += "\nError occurred when using the maximumImageFilter to get the grayValue image.\n";
        excp.SetDescription(err_str);
        /** Pass the exception to an higher level. */
        throw;
      }
    } // end if alsoFixed

    if (this->m_ThresholdBool)
    {
      /** Setup iterator. */
      GrayValueImageIteratorType it(this->m_GrayValueImage2, this->m_GrayValueImage2->GetLargestPossibleRegion());
      it.GoToBegin();
      while (!it.IsAtEnd())
      {
        /** Threshold or just make sure everything is between 0 and 100. */
        // \todo Possibly combine this with the rescaleIntensity filter of
        //    the vectorMeanDiffusionImageFilter, in order to speed up.
        if (it.Get() < this->m_ThresholdHU)
        {
          it.Set(0);
        }
        if (it.Get() >= this->m_ThresholdHU)
        {
          it.Set(100);
        }
        /** Update iterator. */
        ++it;
      } // end while
    }   // end if
  }
  /** In case we do use a segmentation of the moving image: */
  else
  {
    typename MaximumImageFilterType::Pointer maximumImageFilter;
    /** Check if we also want to use a segmentation of the fixed image. */
    if (this->m_UseFixedSegmentation)
    {
      maximumImageFilter = MaximumImageFilterType::New();
      maximumImageFilter->SetInput(0, this->m_GrayValueImage1);
      maximumImageFilter->SetInput(1, this->m_FixedSegmentationImage);
      this->m_GrayValueImage2 = maximumImageFilter->GetOutput();

      /** Do the maximum (OR filter). */
      try
      {
        this->m_GrayValueImage2->Update();
      }
      catch (itk::ExceptionObject & excp)
      {
        /** Add information to the exception. */
        excp.SetLocation("BSplineTransformWithDiffusion - DiffuseDeformationField()");
        std::string err_str = excp.GetDescription();
        err_str += "\nError occurred when using the maximumImageFilter to get the grayValue image.\n";
        excp.SetDescription(err_str);
        /** Pass the exception to an higher level. */
        throw;
      }
    }
  }

  /** ------------- 4: Setup the diffusion. ------------- */

  if (this->m_AlsoFixed || this->m_UseFixedSegmentation)
  {
    this->m_Diffusion->SetGrayValueImage(this->m_GrayValueImage2);
  }
  else
  {
    this->m_Diffusion->SetGrayValueImage(this->m_GrayValueImage1);
  }
  this->m_Diffusion->SetInput(this->m_DeformationField);
  this->m_Diffusion->Modified();
  this->m_DiffusedField = this->m_Diffusion->GetOutput();

  /** Diffuse deformationField. */
  try
  {
    this->m_DiffusedField->Update();
  }
  catch (itk::ExceptionObject & excp)
  {
    /** Add information to the exception. */
    excp.SetLocation("BSplineTransformWithDiffusion - DiffuseDeformationField()");
    std::string err_str = excp.GetDescription();
    err_str += "\nError occurred while diffusing the deformation field.\n";
    excp.SetDescription(err_str);
    /** Pass the exception to an higher level. */
    throw;
  }

  /** ------------- 5: Update the intermediary transform. ------------- */

  this->UpdateIntermediaryDeformationFieldTransform(this->m_DiffusedField);

  /** ------------- 6: Reset the current transform parameters of the optimizer. ------------- */

  /** Create a vector of zeros in order to reset the B-spline transform. */
  ParametersType dummyParameters(this->GetNumberOfParameters(), 0.0);
  this->SetParameters(dummyParameters);

  /** Reset the optimizer.
   * We had to create the SetCurrentPositionPublic-function, because
   * SetCurrentPosition() is protected.
   */
  this->m_Elastix->GetElxOptimizerBase()->SetCurrentPositionPublic(dummyParameters);

  /** Get rid of the initial transform, because this is now captured
   * within the DeformationFieldTransform.
   */
  this->Superclass2::SetInitialTransform(nullptr);

  /** ------------- 7: Write images. ------------- */

  /** If wanted, write the deformationField, the GrayValueImage and the diffusedField. */
  if (this->m_WriteDiffusionFiles)
  {
    /** Create parts of the filenames. */
    std::string resultImageFormat = "mhd";
    this->m_Configuration->ReadParameter(resultImageFormat, "ResultImageFormat", 0, false);
    std::ostringstream makeFileName1, begin, end;
    begin << this->m_Configuration->GetCommandLineArgument("-out");
    end << ".R" << this->m_Elastix->GetElxRegistrationBase()->GetAsITKBaseType()->GetCurrentLevel() << ".It"
        << this->m_Elastix->GetIterationCounter() << "." << resultImageFormat;

    /** Write the deformationFieldImage. */
    makeFileName1 << begin.str() << "deformationField" << end.str();

    /** Do the writing. */
    try
    {
      itk::WriteImage(m_DeformationField, makeFileName1.str());
    }
    catch (itk::ExceptionObject & excp)
    {
      /** Add information to the exception. */
      excp.SetLocation("BSplineTransformWithDiffusion - DiffuseDeformationField()");
      std::string err_str = excp.GetDescription();
      err_str += "\nError occurred while writing the deformationField image.\n";
      excp.SetDescription(err_str);
      /** Print the exception. */
      log::error(std::ostringstream{} << excp);
    }

    /** Write the GrayValueImage. */
    std::ostringstream makeFileName2;
    makeFileName2 << begin.str() << "GrayValueImage" << end.str();

    /** Do the writing. */
    try
    {
      const auto image = (m_AlsoFixed || m_UseFixedSegmentation) ? m_GrayValueImage2 : m_GrayValueImage1;
      itk::WriteImage(image, makeFileName2.str());
    }
    catch (itk::ExceptionObject & excp)
    {
      /** Add information to the exception. */
      excp.SetLocation("BSplineTransformWithDiffusion - DiffuseDeformationField()");
      std::string err_str = excp.GetDescription();
      err_str += "\nError occurred while writing the grayValue image.\n";
      excp.SetDescription(err_str);
      /** Print the exception. */
      log::error(std::ostringstream{} << excp);
    }

    /** Write the diffusedFieldImage. */
    std::ostringstream makeFileName3;
    makeFileName3 << begin.str() << "diffusedField" << end.str();

    /** Do the writing. */
    try
    {
      itk::WriteImage(m_DiffusedField, makeFileName3.str());
    }
    catch (itk::ExceptionObject & excp)
    {
      /** Add information to the exception. */
      excp.SetLocation("BSplineTransformWithDiffusion - DiffuseDeformationField()");
      std::string err_str = excp.GetDescription();
      err_str += "\nError occurred while writing the diffusedField image.\n";
      excp.SetDescription(err_str);
      /** Print the exception. */
      log::error(std::ostringstream{} << excp);
    }

  } // end if this->m_WriteDiffusionFiles

} // end DiffuseDeformationField()


/**
 * ******************* TransformPoint ******************
 */

template <class TElastix>
auto
BSplineTransformWithDiffusion<TElastix>::TransformPoint(const InputPointType & point) const -> OutputPointType
{
  return this->GenericDeformationFieldRegulizer::TransformPoint(point);

} // end TransformPoint()


} // end namespace elastix

#endif // end #ifndef elxBSplineTransformWithDiffusion_hxx