File: itkParzenWindowMutualInformationImageToImageMetric.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 (1068 lines) | stat: -rw-r--r-- 41,868 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
/*=========================================================================
 *
 *  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 itkParzenWindowMutualInformationImageToImageMetric_hxx
#define itkParzenWindowMutualInformationImageToImageMetric_hxx

#include "itkParzenWindowMutualInformationImageToImageMetric.h"

#include "itkImageLinearConstIteratorWithIndex.h"
#include "itkImageScanlineConstIterator.h"
#include <vnl/vnl_math.h>
#include "itkMatrix.h"
#include <vnl/vnl_inverse.h>
#include <vnl/vnl_det.h>

#include <cassert>

namespace itk
{
/**
 * ********************* Constructor ******************************
 */

template <class TFixedImage, class TMovingImage>
ParzenWindowMutualInformationImageToImageMetric<TFixedImage,
                                                TMovingImage>::ParzenWindowMutualInformationImageToImageMetric()
{
  /** Initialize the m_ParzenWindowHistogramThreaderParameters. */
  this->m_ParzenWindowMutualInformationThreaderParameters.m_Metric = this;

} // end constructor


/**
 * ********************* InitializeHistograms ******************************
 */

template <class TFixedImage, class TMovingImage>
void
ParzenWindowMutualInformationImageToImageMetric<TFixedImage, TMovingImage>::InitializeHistograms()
{
  /** Call Superclass implementation. */
  this->Superclass::InitializeHistograms();

  /** Allocate small amount of memory for the m_PRatioArray. */
  if (!this->GetUseExplicitPDFDerivatives())
  {
    this->m_PRatioArray.set_size(this->GetNumberOfFixedHistogramBins(), this->GetNumberOfMovingHistogramBins());
  }

} // end InitializeHistograms()


/**
 * ************************** GetValue **************************
 */

template <class TFixedImage, class TMovingImage>
auto
ParzenWindowMutualInformationImageToImageMetric<TFixedImage, TMovingImage>::GetValue(
  const ParametersType & parameters) const -> MeasureType
{
  /** Construct the JointPDF and Alpha. */
  this->ComputePDFs(parameters);

  /** Normalize the pdfs: p = alpha h. */
  this->NormalizeJointPDF(this->m_JointPDF, this->m_Alpha);

  /** Compute the fixed and moving marginal pdfs, by summing over the joint pdf. */
  this->ComputeMarginalPDF(this->m_JointPDF, this->m_FixedImageMarginalPDF, 0);
  this->ComputeMarginalPDF(this->m_JointPDF, this->m_MovingImageMarginalPDF, 1);

  /** Compute the metric by double summation over histogram. */

  /** Setup iterators */
  using JointPDFIteratorType = ImageLinearConstIteratorWithIndex<JointPDFType>;
  using MarginalPDFIteratorType = typename MarginalPDFType::const_iterator;

  JointPDFIteratorType jointPDFit(this->m_JointPDF, this->m_JointPDF->GetLargestPossibleRegion());
  jointPDFit.SetDirection(0);
  jointPDFit.GoToBegin();
  MarginalPDFIteratorType       fixedPDFit = this->m_FixedImageMarginalPDF.begin();
  const MarginalPDFIteratorType fixedPDFend = this->m_FixedImageMarginalPDF.end();
  MarginalPDFIteratorType       movingPDFit = this->m_MovingImageMarginalPDF.begin();
  const MarginalPDFIteratorType movingPDFend = this->m_MovingImageMarginalPDF.end();

  /** Loop over histogram. */
  double MI = 0.0;
  while (fixedPDFit != fixedPDFend)
  {
    const double fixedImagePDFValue = *fixedPDFit;
    movingPDFit = this->m_MovingImageMarginalPDF.begin();
    while (movingPDFit != movingPDFend)
    {
      const double movingImagePDFValue = *movingPDFit;
      const double fixPDFmovPDF = fixedImagePDFValue * movingImagePDFValue;
      const double jointPDFValue = jointPDFit.Get();

      /** Check for non-zero bin contribution. */
      if (jointPDFValue > 1e-16 && fixPDFmovPDF > 1e-16)
      {
        MI += jointPDFValue * std::log(jointPDFValue / fixPDFmovPDF);
      }
      ++movingPDFit;
      ++jointPDFit;
    } // end while-loop over moving index

    ++fixedPDFit;
    jointPDFit.NextLine();

  } // end while-loop over fixed index

  return static_cast<MeasureType>(-1.0 * MI);

} // end GetValue()


/**
 * ******************** GetValueAndAnalyticDerivative *******************
 */

template <class TFixedImage, class TMovingImage>
void
ParzenWindowMutualInformationImageToImageMetric<TFixedImage, TMovingImage>::GetValueAndAnalyticDerivative(
  const ParametersType & parameters,
  MeasureType &          value,
  DerivativeType &       derivative) const
{
  /** Low memory variant. */
  if (!this->GetUseExplicitPDFDerivatives())
  {
    this->GetValueAndAnalyticDerivativeLowMemory(parameters, value, derivative);
    return;
  }

  /** Initialize some variables. */
  value = MeasureType{};
  derivative.set_size(this->GetNumberOfParameters());
  derivative.Fill(0.0);

  /** Construct the JointPDF, JointPDFDerivatives, Alpha and its derivatives. */
  this->ComputePDFsAndPDFDerivatives(parameters);

  /** Normalize the pdfs: p = alpha h. */
  this->NormalizeJointPDF(this->m_JointPDF, this->m_Alpha);

  /** Compute the fixed and moving marginal pdf by summing over the histogram. */
  this->ComputeMarginalPDF(this->m_JointPDF, this->m_FixedImageMarginalPDF, 0);
  this->ComputeMarginalPDF(this->m_JointPDF, this->m_MovingImageMarginalPDF, 1);

  /** Compute the metric and derivatives by double summation over histogram. */

  /** Setup iterators .*/
  using JointPDFIteratorType = ImageLinearConstIteratorWithIndex<JointPDFType>;
  using JointPDFDerivativesIteratorType = ImageLinearConstIteratorWithIndex<JointPDFDerivativesType>;
  using MarginalPDFIteratorType = typename MarginalPDFType::const_iterator;
  using DerivativeIteratorType = typename DerivativeType::iterator;

  JointPDFIteratorType jointPDFit(this->m_JointPDF, this->m_JointPDF->GetLargestPossibleRegion());
  jointPDFit.SetDirection(0);
  jointPDFit.GoToBegin();
  JointPDFDerivativesIteratorType jointPDFDerivativesit(this->m_JointPDFDerivatives,
                                                        this->m_JointPDFDerivatives->GetLargestPossibleRegion());
  jointPDFDerivativesit.SetDirection(0);
  jointPDFDerivativesit.GoToBegin();
  MarginalPDFIteratorType       fixedPDFit = this->m_FixedImageMarginalPDF.begin();
  const MarginalPDFIteratorType fixedPDFend = this->m_FixedImageMarginalPDF.end();
  MarginalPDFIteratorType       movingPDFit = this->m_MovingImageMarginalPDF.begin();
  const MarginalPDFIteratorType movingPDFend = this->m_MovingImageMarginalPDF.end();
  DerivativeIteratorType        derivit = derivative.begin();
  const DerivativeIteratorType  derivbegin = derivative.begin();
  const DerivativeIteratorType  derivend = derivative.end();

  /** Loop over the joint histogram. */
  double MI = 0.0;
  while (fixedPDFit != fixedPDFend)
  {
    const double fixedImagePDFValue = *fixedPDFit;
    movingPDFit = this->m_MovingImageMarginalPDF.begin();
    while (movingPDFit != movingPDFend)
    {
      const double movingImagePDFValue = *movingPDFit;
      const double fixPDFmovPDF = fixedImagePDFValue * movingImagePDFValue;
      const double jointPDFValue = jointPDFit.Get();

      /** Check for non-zero bin contribution. */
      if (jointPDFValue > 1e-16 && fixPDFmovPDF > 1e-16)
      {
        derivit = derivbegin;
        const double pRatio = std::log(jointPDFValue / fixPDFmovPDF);
        const double pRatioAlpha = this->m_Alpha * pRatio;
        MI += jointPDFValue * pRatio;
        while (derivit != derivend)
        {
          /**  Ref: eq 23 of Thevenaz & Unser paper [3]. */
          (*derivit) -= jointPDFDerivativesit.Get() * pRatioAlpha;
          ++derivit;
          ++jointPDFDerivativesit;
        } // end while-loop over parameters
      }   // end if-block to check non-zero bin contribution

      ++movingPDFit;
      ++jointPDFit;
      jointPDFDerivativesit.NextLine();

    } // end while-loop over moving index
    ++fixedPDFit;
    jointPDFit.NextLine();
  } // end while-loop over fixed index

  value = static_cast<MeasureType>(-1.0 * MI);

} // end GetValueAndAnalyticDerivative()


/**
 * ******************** GetValueAndAnalyticDerivativeLowMemory *******************
 */

template <class TFixedImage, class TMovingImage>
void
ParzenWindowMutualInformationImageToImageMetric<TFixedImage, TMovingImage>::GetValueAndAnalyticDerivativeLowMemory(
  const ParametersType & parameters,
  MeasureType &          value,
  DerivativeType &       derivative) const
{
  /** Construct the JointPDF and Alpha.
   * This function contains a loop over the samples.
   * It executes multi-threadedly when m_UseMultiThread == true.
   */
  this->ComputePDFs(parameters);

  /** Normalize the joint histogram by alpha. */
  this->NormalizeJointPDF(this->m_JointPDF, this->m_Alpha);

  /** Compute the fixed and moving marginal pdf by summing over the histogram. */
  this->ComputeMarginalPDF(this->m_JointPDF, this->m_FixedImageMarginalPDF, 0);
  this->ComputeMarginalPDF(this->m_JointPDF, this->m_MovingImageMarginalPDF, 1);

  // \todo: the last three loops over the joint histogram can be done in
  // one loop, maybe also include the next loop to generate m_PRatioArray.
  // The effort is probably not worth the gain in performance.

  /** Compute the metric value and the intermediate m_PRatioArray
   * by summation over the joint histogram.
   */
  double MI = 0.0;
  this->ComputeValueAndPRatioArray(MI);
  value = static_cast<MeasureType>(-1.0 * MI);

  /* Compute the derivative.
   * This function contains a second loop over the samples.
   * It executes multi-threadedly when m_UseMultiThread == true.
   */
  this->ComputeDerivativeLowMemory(derivative);

} // end GetValueAndAnalyticDerivativeLowMemory()


/**
 * ******************** ComputeDerivativeLowMemorySingleThreaded *******************
 */

template <class TFixedImage, class TMovingImage>
void
ParzenWindowMutualInformationImageToImageMetric<TFixedImage, TMovingImage>::ComputeDerivativeLowMemorySingleThreaded(
  DerivativeType & derivative) const
{
  /** Initialize array that stores dM(x)/dmu, and the sparse Jacobian + indices. */
  const NumberOfParametersType nnzji = Superclass::m_AdvancedTransform->GetNumberOfNonZeroJacobianIndices();
  NonZeroJacobianIndicesType   nzji(nnzji);
  DerivativeType               imageJacobian(nzji.size());
  TransformJacobianType        jacobian;
  derivative.Fill(0.0);

  /** Declare and allocate arrays for Jacobian preconditioning. */
  DerivativeType jacobianPreconditioner, preconditioningDivisor;
  if (this->GetUseJacobianPreconditioning())
  {
    jacobianPreconditioner = DerivativeType(nzji.size());
    preconditioningDivisor = DerivativeType(this->GetNumberOfParameters(), 0.0);
  }

  /** Get a handle to the sample container. */
  ImageSampleContainerPointer sampleContainer = this->GetImageSampler()->GetOutput();

  /** Loop over sample container and compute contribution of each sample to pdfs. */
  for (const auto & fixedImageSample : *sampleContainer)
  {
    /** Read fixed coordinates and create some variables. */
    const FixedImagePointType & fixedPoint = fixedImageSample.m_ImageCoordinates;
    RealType                    movingImageValue;
    MovingImageDerivativeType   movingImageDerivative;

    /** Transform point. */
    const MovingImagePointType mappedPoint = this->TransformPoint(fixedPoint);

    /** Check if the point is inside the moving mask. */
    bool sampleOk = this->IsInsideMovingMask(mappedPoint);

    /** Compute the moving image value, its derivative, and check
     * if the point is inside the moving image buffer.
     */
    if (sampleOk)
    {
      sampleOk =
        this->Superclass::EvaluateMovingImageValueAndDerivative(mappedPoint, movingImageValue, &movingImageDerivative);
    }

    if (sampleOk)
    {
      /** Get the fixed image value. */
      auto fixedImageValue = static_cast<RealType>(fixedImageSample.m_ImageValue);

      /** Make sure the values fall within the histogram range. */
      fixedImageValue = this->GetFixedImageLimiter()->Evaluate(fixedImageValue);
      movingImageValue = this->GetMovingImageLimiter()->Evaluate(movingImageValue, movingImageDerivative);

      /** Get the transform Jacobian dT/dmu. */
      this->EvaluateTransformJacobian(fixedPoint, jacobian, nzji);

      /** Compute the inner product (dM/dx)^T (dT/dmu). */
      this->EvaluateTransformJacobianInnerProduct(jacobian, movingImageDerivative, imageJacobian);

      /** If desired, apply the technique introduced by Tustison. */
      if (this->GetUseJacobianPreconditioning())
      {
        this->ComputeJacobianPreconditioner(jacobian, nzji, jacobianPreconditioner, preconditioningDivisor);
        DerivativeValueType * imjacit = imageJacobian.begin();
        DerivativeValueType * jacprecit = jacobianPreconditioner.begin();
        for (unsigned int i = 0; i < nzji.size(); ++i)
        {
          while (imjacit != imageJacobian.end())
          {
            (*imjacit) *= (*jacprecit);
            ++imjacit;
            ++jacprecit;
          }
        }
      }

      /** Compute this sample's contribution to the joint distributions. */
      this->UpdateDerivativeLowMemory(fixedImageValue, movingImageValue, imageJacobian, nzji, derivative);

    } // end sampleOk
  }   // end loop over sample container

  /** If desired, apply the technique introduced by Tustison */
  if (this->GetUseJacobianPreconditioning())
  {
    DerivativeValueType * derivit = derivative.begin();
    DerivativeValueType * divisit = preconditioningDivisor.begin();

    /** This normalization was not in the Tustison paper, but it helps,
     * especially for localized mutual information.
     */
    const double normalizationFactor = preconditioningDivisor.mean();
    while (derivit != derivative.end())
    {
      (*derivit) *= normalizationFactor / ((*divisit) + 1e-14);
      ++derivit;
      ++divisit;
    }
  }

} // end ComputeDerivativeLowMemorySingleThreaded()


/**
 * ******************** ComputeDerivativeLowMemory *******************
 */

template <class TFixedImage, class TMovingImage>
void
ParzenWindowMutualInformationImageToImageMetric<TFixedImage, TMovingImage>::ComputeDerivativeLowMemory(
  DerivativeType & derivative) const
{
  /** Option for now to still use the single threaded code. */
  if (!Superclass::m_UseMultiThread)
  {
    return this->ComputeDerivativeLowMemorySingleThreaded(derivative);
  }

  /** Launch multi-threading derivative computation. */
  this->LaunchComputeDerivativeLowMemoryThreaderCallback();

  /** Gather the results from all threads. */
  this->AfterThreadedComputeDerivativeLowMemory(derivative);

} // end ComputeDerivativeLowMemory()


/**
 * ******************* ThreadedComputeDerivativeLowMemory *******************
 */

template <class TFixedImage, class TMovingImage>
void
ParzenWindowMutualInformationImageToImageMetric<TFixedImage, TMovingImage>::ThreadedComputeDerivativeLowMemory(
  ThreadIdType threadId)
{
  /** Initialize array that stores dM(x)/dmu, and the sparse Jacobian + indices. */
  const NumberOfParametersType nnzji = Superclass::m_AdvancedTransform->GetNumberOfNonZeroJacobianIndices();
  NonZeroJacobianIndicesType   nzji(nnzji);
  DerivativeType               imageJacobian(nzji.size());

  /** Get a handle to the pre-allocated derivative for the current thread.
   * The initialization is performed at the beginning of each resolution in
   * InitializeThreadingParameters(), and at the end of each iteration in
   * AfterThreadedGetValueAndDerivative() and the accumulate functions.
   */
  DerivativeType & derivative = Superclass::m_GetValueAndDerivativePerThreadVariables[threadId].st_Derivative;

  /** Declare and allocate arrays for Jacobian preconditioning. */
  DerivativeType jacobianPreconditioner, preconditioningDivisor;
  if (this->GetUseJacobianPreconditioning())
  {
    jacobianPreconditioner = DerivativeType(nzji.size());
    preconditioningDivisor = DerivativeType(this->GetNumberOfParameters(), 0.0);
  }

  /** Get a handle to the sample container. */
  ImageSampleContainerPointer sampleContainer = this->GetImageSampler()->GetOutput();
  const unsigned long         sampleContainerSize = sampleContainer->Size();

  /** Get the samples for this thread. */
  const unsigned long nrOfSamplesPerThreads = static_cast<unsigned long>(
    std::ceil(static_cast<double>(sampleContainerSize) / static_cast<double>(Self::GetNumberOfWorkUnits())));

  const auto pos_begin = std::min<size_t>(nrOfSamplesPerThreads * threadId, sampleContainerSize);
  const auto pos_end = std::min<size_t>(nrOfSamplesPerThreads * (threadId + 1), sampleContainerSize);

  /** Create iterator over the sample container. */
  const auto beginOfSampleContainer = sampleContainer->cbegin();
  const auto fbegin = beginOfSampleContainer + pos_begin;
  const auto fend = beginOfSampleContainer + pos_end;

  /** Loop over sample container and compute contribution of each sample to pdfs. */
  for (auto fiter = fbegin; fiter != fend; ++fiter)
  {
    /** Read fixed coordinates and create some variables. */
    const FixedImagePointType & fixedPoint = fiter->m_ImageCoordinates;
    RealType                    movingImageValue;
    MovingImageDerivativeType   movingImageDerivative;

    /** Transform point. */
    const MovingImagePointType mappedPoint = this->TransformPoint(fixedPoint);

    /** Check if the point is inside the moving mask. */
    bool sampleOk = this->IsInsideMovingMask(mappedPoint);

    /** Compute the moving image value, its derivative, and check
     * if the point is inside the moving image buffer.
     */
    if (sampleOk)
    {
      sampleOk = this->FastEvaluateMovingImageValueAndDerivative(
        mappedPoint, movingImageValue, &movingImageDerivative, threadId);
    }

    if (sampleOk)
    {
      /** Get the fixed image value. */
      RealType fixedImageValue = static_cast<RealType>(fiter->m_ImageValue);

      /** Make sure the values fall within the histogram range. */
      fixedImageValue = this->GetFixedImageLimiter()->Evaluate(fixedImageValue);
      movingImageValue = this->GetMovingImageLimiter()->Evaluate(movingImageValue, movingImageDerivative);

#if 0
      /** Get the TransformJacobian dT/dmu. */
      this->EvaluateTransformJacobian( fixedPoint, jacobian, nzji );

      /** Compute the inner products (dM/dx)^T (dT/dmu). */
      this->EvaluateTransformJacobianInnerProduct(
        jacobian, movingImageDerivative, imageJacobian );
#else
      /** Compute the inner product of the transform Jacobian dT/dmu and the moving image gradient dM/dx. */
      Superclass::m_AdvancedTransform->EvaluateJacobianWithImageGradientProduct(
        fixedPoint, movingImageDerivative, imageJacobian, nzji);
#endif

      /** If desired, apply the technique introduced by Tustison. */
      TransformJacobianType jacobian;
      if (this->GetUseJacobianPreconditioning())
      {
        this->EvaluateTransformJacobian(fixedPoint, jacobian, nzji);

        this->ComputeJacobianPreconditioner(jacobian, nzji, jacobianPreconditioner, preconditioningDivisor);
        DerivativeValueType * imjacit = imageJacobian.begin();
        DerivativeValueType * jacprecit = jacobianPreconditioner.begin();
        for (unsigned int i = 0; i < nzji.size(); ++i)
        {
          while (imjacit != imageJacobian.end())
          {
            (*imjacit) *= (*jacprecit);
            ++imjacit;
            ++jacprecit;
          }
        }
      }

      /** Compute this sample's contribution to the joint distributions. */
      this->UpdateDerivativeLowMemory(fixedImageValue, movingImageValue, imageJacobian, nzji, derivative);

    } // end sampleOk
  }   // end loop over sample container

  /** If desired, apply the technique introduced by Tustison. */
  if (this->GetUseJacobianPreconditioning())
  {
    DerivativeValueType * derivit = derivative.begin();
    DerivativeValueType * divisit = preconditioningDivisor.begin();

    /** This normalization was not in the Tustison paper, but it helps,
     * especially for localized mutual information.
     */
    const double normalizationFactor = preconditioningDivisor.mean();
    while (derivit != derivative.end())
    {
      (*derivit) *= normalizationFactor / ((*divisit) + 1e-14);
      ++derivit;
      ++divisit;
    }
  }

} // end ThreadedComputeDerivativeLowMemory()


/**
 * ******************* AfterThreadedComputeDerivativeLowMemory *******************
 */

template <class TFixedImage, class TMovingImage>
void
ParzenWindowMutualInformationImageToImageMetric<TFixedImage, TMovingImage>::AfterThreadedComputeDerivativeLowMemory(
  DerivativeType & derivative) const
{
  const ThreadIdType numberOfThreads = Self::GetNumberOfWorkUnits();

  /** Accumulate derivatives. */
  // compute multi-threadedly with itk threads
  Superclass::m_ThreaderMetricParameters.st_DerivativePointer = derivative.begin();
  Superclass::m_ThreaderMetricParameters.st_NormalizationFactor = 1.0;

  this->m_Threader->SetSingleMethodAndExecute(this->AccumulateDerivativesThreaderCallback,
                                              &(Superclass::m_ThreaderMetricParameters));

} // end AfterThreadedComputeDerivativeLowMemory()


/**
 * **************** ComputeDerivativeLowMemoryThreaderCallback *******
 */

template <class TFixedImage, class TMovingImage>
ITK_THREAD_RETURN_FUNCTION_CALL_CONVENTION
ParzenWindowMutualInformationImageToImageMetric<TFixedImage, TMovingImage>::ComputeDerivativeLowMemoryThreaderCallback(
  void * arg)
{
  assert(arg);
  const auto & infoStruct = *static_cast<ThreadInfoType *>(arg);
  ThreadIdType threadId = infoStruct.WorkUnitID;

  assert(infoStruct.UserData);
  const auto & userData = *static_cast<ParzenWindowMutualInformationMultiThreaderParameterType *>(infoStruct.UserData);

  userData.m_Metric->ThreadedComputeDerivativeLowMemory(threadId);

  return ITK_THREAD_RETURN_DEFAULT_VALUE;

} // end ComputeDerivativeLowMemoryThreaderCallback()


/**
 * *********************** LaunchComputeDerivativeLowMemoryThreaderCallback***************
 */

template <class TFixedImage, class TMovingImage>
void
ParzenWindowMutualInformationImageToImageMetric<TFixedImage,
                                                TMovingImage>::LaunchComputeDerivativeLowMemoryThreaderCallback() const
{
  /** Setup threader and launch. */
  this->m_Threader->SetSingleMethodAndExecute(
    this->ComputeDerivativeLowMemoryThreaderCallback,
    const_cast<void *>(static_cast<const void *>(&this->m_ParzenWindowMutualInformationThreaderParameters)));

} // end LaunchComputeDerivativeLowMemoryThreaderCallback()


/**
 * ******************* ComputeValueAndPRatioArray *******************
 */

template <class TFixedImage, class TMovingImage>
void
ParzenWindowMutualInformationImageToImageMetric<TFixedImage, TMovingImage>::ComputeValueAndPRatioArray(
  double & MI) const
{
  /** Setup iterators. */
  using JointPDFIteratorType = ImageScanlineConstIterator<JointPDFType>;
  using MarginalPDFIteratorType = typename MarginalPDFType::const_iterator;

  JointPDFIteratorType          jointPDFit(this->m_JointPDF, this->m_JointPDF->GetLargestPossibleRegion());
  MarginalPDFIteratorType       fixedPDFit = this->m_FixedImageMarginalPDF.begin();
  const MarginalPDFIteratorType fixedPDFend = this->m_FixedImageMarginalPDF.end();
  MarginalPDFIteratorType       movingPDFit;
  const MarginalPDFIteratorType movingPDFbegin = this->m_MovingImageMarginalPDF.begin();
  const MarginalPDFIteratorType movingPDFend = this->m_MovingImageMarginalPDF.end();

  /** Initialize */
  this->m_PRatioArray.fill(PRatioType{});

  /** Loop over the joint histogram. */
  PDFValueType sum = 0.0;
  unsigned int fixedIndex = 0;
  unsigned int movingIndex = 0;
  while (fixedPDFit != fixedPDFend)
  {
    const double fixedPDFValue = *fixedPDFit;
    double       logFixedPDFValue = 0.0;
    if (fixedPDFValue > 1e-16)
    {
      logFixedPDFValue = std::log(fixedPDFValue);
    }
    movingPDFit = movingPDFbegin;
    movingIndex = 0;

    while (movingPDFit != movingPDFend)
    {
      const PDFValueType movingPDFValue = *movingPDFit;
      const PDFValueType jointPDFValue = jointPDFit.Value();

      /** Check for non-zero bin contribution. */
      if (jointPDFValue > 1e-16 && movingPDFValue > 1e-16)
      {
        const PDFValueType pRatio = std::log(jointPDFValue / movingPDFValue);
        this->m_PRatioArray[fixedIndex][movingIndex] = static_cast<PRatioType>(this->m_Alpha * pRatio);

        if (fixedPDFValue > 1e-16)
        {
          sum += jointPDFValue * (pRatio - logFixedPDFValue);
        }
      } // end if-block to check non-zero bin contribution

      /** Update iterators. */
      ++movingPDFit;
      ++jointPDFit;
      ++movingIndex;

    } // end while-loop over moving index

    /** Update iterators. */
    ++fixedPDFit;
    jointPDFit.NextLine();
    ++fixedIndex;

  } // end while-loop over fixed index

  // Assign
  MI = sum;

} // end ComputeValueAndPRatioArray()


/**
 * ******************* UpdateDerivativeLowMemory *******************
 */

template <class TFixedImage, class TMovingImage>
void
ParzenWindowMutualInformationImageToImageMetric<TFixedImage, TMovingImage>::UpdateDerivativeLowMemory(
  const RealType                     fixedImageValue,
  const RealType                     movingImageValue,
  const DerivativeType &             imageJacobian,
  const NonZeroJacobianIndicesType & nzji,
  DerivativeType &                   derivative) const
{
  /** In this function we need to do (see eq. 24 of Thevenaz [3]):
   *      derivative -= constant * imageJacobian *
   *          \sum_i \sum_k PRatio(i,k) * dB/dxi(xi,i,k),
   * with i, k, the fixed and moving histogram bins,
   * PRatio the precomputed log( p(i,k) / p(i) ), and
   * dB/dxi the B-spline derivative.
   *
   * Note (1) that we only have to loop over i,k within the support
   * of the B-spline Parzen-window.
   * Note (2) that imageJacobian may be sparse.
   */

  /** Determine the affected region. */

  /** Determine Parzen window arguments (see eq. 6 of Mattes paper [2]). */
  const double fixedImageParzenWindowTerm =
    fixedImageValue / this->m_FixedImageBinSize - this->m_FixedImageNormalizedMin;
  const double movingImageParzenWindowTerm =
    movingImageValue / this->m_MovingImageBinSize - this->m_MovingImageNormalizedMin;

  /** The lowest bin numbers affected by this pixel: */
  const int fixedParzenWindowIndex =
    static_cast<int>(std::floor(fixedImageParzenWindowTerm + this->m_FixedParzenTermToIndexOffset));
  const int movingParzenWindowIndex =
    static_cast<int>(std::floor(movingImageParzenWindowTerm + this->m_MovingParzenTermToIndexOffset));

  const auto numberOfFixedParzenValues = Superclass::m_JointPDFWindow.GetSize()[1];
  const auto numberOfDerivedMovingParzenValues = Superclass::m_JointPDFWindow.GetSize()[0];

  // Create a buffer of Parzen values for both the fixed and the moving image.
  const auto parzenValues =
    std::make_unique<PDFValueType[]>(numberOfFixedParzenValues + numberOfDerivedMovingParzenValues);

  /** Compute the fixed Parzen values. */
  PDFValueType * const fixedParzenValues = parzenValues.get();
  Superclass::EvaluateParzenValues(
    fixedImageParzenWindowTerm, fixedParzenWindowIndex, *Superclass::m_FixedKernel, fixedParzenValues);

  /** Compute the derivatives of the moving Parzen window. */
  PDFValueType * const derivativeMovingParzenValues = parzenValues.get() + numberOfFixedParzenValues;
  Superclass::EvaluateParzenValues(movingImageParzenWindowTerm,
                                   movingParzenWindowIndex,
                                   *Superclass::m_DerivativeMovingKernel,
                                   derivativeMovingParzenValues);

  /** Get the moving image bin size. */
  const double et = static_cast<double>(this->m_MovingImageBinSize);

  /** Loop over the Parzen window region and increment sum. */
  PDFValueType sum = 0.0;
  for (unsigned int f = 0; f < numberOfFixedParzenValues; ++f)
  {
    const double fv_et = fixedParzenValues[f] / et;
    for (unsigned int m = 0; m < numberOfDerivedMovingParzenValues; ++m)
    {
      sum += this->m_PRatioArray[f + fixedParzenWindowIndex][m + movingParzenWindowIndex] * fv_et *
             derivativeMovingParzenValues[m];
    }
  }

  const auto numberOfParameters = this->GetNumberOfParameters();

  /** Now compute derivative -= sum * imageJacobian. */
  if (nzji.size() == numberOfParameters)
  {
    /** Loop over all Jacobians. */
    for (unsigned int mu = 0; mu < numberOfParameters; ++mu)
    {
      derivative[mu] += static_cast<DerivativeValueType>(imageJacobian[mu] * sum);
    }
  }
  else
  {
    /** Loop only over the non-zero Jacobians. */
    for (unsigned int i = 0; i < imageJacobian.GetSize(); ++i)
    {
      const unsigned int mu = nzji[i];
      derivative[mu] += static_cast<DerivativeValueType>(imageJacobian[i] * sum);
    }
  }

} // end UpdateDerivativeLowMemory()


/**
 * ******************** GetValueAndFiniteDifferenceDerivative *******************
 */

template <class TFixedImage, class TMovingImage>
void
ParzenWindowMutualInformationImageToImageMetric<TFixedImage, TMovingImage>::GetValueAndFiniteDifferenceDerivative(
  const ParametersType & parameters,
  MeasureType &          value,
  DerivativeType &       derivative) const
{
  /** Initialize some variables. */
  value = MeasureType{};
  derivative.set_size(this->GetNumberOfParameters());
  derivative.Fill(0.0);

  /** Construct the JointPDF, JointPDFDerivatives, Alpha and its derivatives. */
  this->ComputePDFsAndIncrementalPDFs(parameters);

  /** Compute the fixed and moving marginal pdf by summing over the histogram. */
  this->ComputeMarginalPDF(this->m_JointPDF, this->m_FixedImageMarginalPDF, 0);
  this->ComputeMarginalPDF(this->m_JointPDF, this->m_MovingImageMarginalPDF, 1);

  /** Compute the fixed and moving incremental marginal pdfs by summing over the
   * incremental histogram. Do it for Right and Left.
   */
  this->ComputeIncrementalMarginalPDFs(this->m_IncrementalJointPDFRight,
                                       this->m_FixedIncrementalMarginalPDFRight,
                                       this->m_MovingIncrementalMarginalPDFRight);
  this->ComputeIncrementalMarginalPDFs(
    this->m_IncrementalJointPDFLeft, this->m_FixedIncrementalMarginalPDFLeft, this->m_MovingIncrementalMarginalPDFLeft);

  /** Compute the metric and derivatives by double summation over histogram. */

  /** Setup iterators */
  using JointPDFIteratorType = ImageLinearConstIteratorWithIndex<JointPDFType>;
  using IncrementalJointPDFIteratorType = ImageLinearConstIteratorWithIndex<JointPDFDerivativesType>;
  using MarginalPDFIteratorType = typename MarginalPDFType::const_iterator;
  using IncrementalMarginalPDFIteratorType = ImageLinearConstIteratorWithIndex<IncrementalMarginalPDFType>;
  using DerivativeIteratorType = typename DerivativeType::iterator;
  using DerivativeConstIteratorType = typename DerivativeType::const_iterator;

  JointPDFIteratorType jointPDFit(this->m_JointPDF, this->m_JointPDF->GetLargestPossibleRegion());
  jointPDFit.GoToBegin();

  IncrementalJointPDFIteratorType jointIncPDFRightit(this->m_IncrementalJointPDFRight,
                                                     this->m_IncrementalJointPDFRight->GetLargestPossibleRegion());
  IncrementalJointPDFIteratorType jointIncPDFLeftit(this->m_IncrementalJointPDFLeft,
                                                    this->m_IncrementalJointPDFLeft->GetLargestPossibleRegion());
  jointIncPDFRightit.GoToBegin();
  jointIncPDFLeftit.GoToBegin();

  MarginalPDFIteratorType       fixedPDFit = this->m_FixedImageMarginalPDF.begin();
  const MarginalPDFIteratorType fixedPDFend = this->m_FixedImageMarginalPDF.end();
  MarginalPDFIteratorType       movingPDFit = this->m_MovingImageMarginalPDF.begin();
  const MarginalPDFIteratorType movingPDFend = this->m_MovingImageMarginalPDF.end();

  IncrementalMarginalPDFIteratorType fixedIncPDFRightit(
    this->m_FixedIncrementalMarginalPDFRight, this->m_FixedIncrementalMarginalPDFRight->GetLargestPossibleRegion());
  IncrementalMarginalPDFIteratorType movingIncPDFRightit(
    this->m_MovingIncrementalMarginalPDFRight, this->m_MovingIncrementalMarginalPDFRight->GetLargestPossibleRegion());
  IncrementalMarginalPDFIteratorType fixedIncPDFLeftit(
    this->m_FixedIncrementalMarginalPDFLeft, this->m_FixedIncrementalMarginalPDFLeft->GetLargestPossibleRegion());
  IncrementalMarginalPDFIteratorType movingIncPDFLeftit(
    this->m_MovingIncrementalMarginalPDFLeft, this->m_MovingIncrementalMarginalPDFLeft->GetLargestPossibleRegion());
  fixedIncPDFRightit.GoToBegin();
  movingIncPDFRightit.GoToBegin();
  fixedIncPDFLeftit.GoToBegin();
  movingIncPDFLeftit.GoToBegin();

  DerivativeIteratorType       derivit = derivative.begin();
  const DerivativeIteratorType derivbegin = derivative.begin();
  const DerivativeIteratorType derivend = derivative.end();

  DerivativeConstIteratorType       perturbedAlphaRightit = this->m_PerturbedAlphaRight.begin();
  const DerivativeConstIteratorType perturbedAlphaRightbegin = this->m_PerturbedAlphaRight.begin();
  DerivativeConstIteratorType       perturbedAlphaLeftit = this->m_PerturbedAlphaLeft.begin();
  const DerivativeConstIteratorType perturbedAlphaLeftbegin = this->m_PerturbedAlphaLeft.begin();

  double MI = 0.0;
  while (fixedPDFit != fixedPDFend)
  {
    const double fixedPDFValue = *fixedPDFit;

    while (movingPDFit != movingPDFend)
    {
      const double movingPDFValue = *movingPDFit;
      const double jointPDFValue = jointPDFit.Get();
      const double fixPDFmovPDFAlpha = fixedPDFValue * movingPDFValue * this->m_Alpha;

      /** Check for non-zero bin contribution and update the mutual information value. */
      if (jointPDFValue > 1e-16 && fixPDFmovPDFAlpha > 1e-16)
      {
        MI += this->m_Alpha * jointPDFValue * std::log(jointPDFValue / fixPDFmovPDFAlpha);
      }

      /** Update the derivative. */
      derivit = derivbegin;
      perturbedAlphaRightit = perturbedAlphaRightbegin;
      perturbedAlphaLeftit = perturbedAlphaLeftbegin;
      while (derivit != derivend)
      {
        /** Initialize. */
        double contrib = 0.0;

        /** For clarity, get some values and give them a name.
         * \todo Does this cost a lot of computation time?
         */
        const double jointIncPDFRightValue = jointIncPDFRightit.Get();
        const double fixedIncPDFRightValue = fixedIncPDFRightit.Get();
        const double movingIncPDFRightValue = movingIncPDFRightit.Get();
        const double perturbedAlphaRightValue = *perturbedAlphaRightit;

        /** Compute the contribution of the Right-perturbation to the derivative. */
        const double perturbedJointPDFRightValue = jointIncPDFRightValue + jointPDFValue;
        const double perturbedFixedPDFRightValue = fixedPDFValue + fixedIncPDFRightValue;
        const double perturbedMovingPDFRightValue = movingPDFValue + movingIncPDFRightValue;
        const double perturbedfixPDFmovPDFAlphaRight =
          perturbedFixedPDFRightValue * perturbedMovingPDFRightValue * perturbedAlphaRightValue;

        if (perturbedJointPDFRightValue > 1e-16 && perturbedfixPDFmovPDFAlphaRight > 1e-16)
        {
          contrib += perturbedAlphaRightValue * perturbedJointPDFRightValue *
                     std::log(perturbedJointPDFRightValue / perturbedfixPDFmovPDFAlphaRight);
        }

        /** For clarity, get some values and give them a name. */
        const double jointIncPDFLeftValue = jointIncPDFLeftit.Get();
        const double fixedIncPDFLeftValue = fixedIncPDFLeftit.Get();
        const double movingIncPDFLeftValue = movingIncPDFLeftit.Get();
        const double perturbedAlphaLeftValue = *perturbedAlphaLeftit;

        /** Compute the contribution of the Left-perturbation to the derivative. */
        const double perturbedJointPDFLeftValue = jointIncPDFLeftValue + jointPDFValue;
        const double perturbedFixedPDFLeftValue = fixedPDFValue + fixedIncPDFLeftValue;
        const double perturbedMovingPDFLeftValue = movingPDFValue + movingIncPDFLeftValue;
        const double perturbedfixPDFmovPDFAlphaLeft =
          perturbedFixedPDFLeftValue * perturbedMovingPDFLeftValue * perturbedAlphaLeftValue;

        if (perturbedJointPDFLeftValue > 1e-16 && perturbedfixPDFmovPDFAlphaLeft > 1e-16)
        {
          contrib -= perturbedAlphaLeftValue * perturbedJointPDFLeftValue *
                     std::log(perturbedJointPDFLeftValue / perturbedfixPDFmovPDFAlphaLeft);
        }

        /** Update the derivative component. */
        (*derivit) += contrib;

        /** Move the iterators to the next parameter. */
        ++derivit;
        ++perturbedAlphaRightit;
        ++perturbedAlphaLeftit;
        ++jointIncPDFRightit;
        ++jointIncPDFLeftit;
        ++fixedIncPDFRightit;
        ++movingIncPDFRightit;
        ++fixedIncPDFLeftit;
        ++movingIncPDFLeftit;
      } // end while-loop over parameters

      ++jointPDFit;                         // next moving bin
      ++movingPDFit;                        // next moving bin
      jointIncPDFRightit.NextLine();        // next moving bin
      jointIncPDFLeftit.NextLine();         // next moving bin
      fixedIncPDFRightit.GoToBeginOfLine(); // same fixed bin
      fixedIncPDFLeftit.GoToBeginOfLine();  // same fixed bin
      movingIncPDFRightit.NextLine();       // next moving bin
      movingIncPDFLeftit.NextLine();        // next moving bin

    } // end while-loop over moving index

    jointPDFit.NextLine();                                // next fixed bin
    ++fixedPDFit;                                         // next fixed bin
    movingPDFit = this->m_MovingImageMarginalPDF.begin(); // first moving bin
    fixedIncPDFRightit.NextLine();                        // next fixed bin
    fixedIncPDFLeftit.NextLine();                         // next fixed bin
    movingIncPDFRightit.GoToBegin();                      // first moving bin
    movingIncPDFLeftit.GoToBegin();                       // first moving bin

  } // end while-loop over fixed index

  value = static_cast<MeasureType>(-1.0 * MI);

  /** Divide the derivative by -delta*2. */
  const double delta2 = -1.0 / (this->GetFiniteDifferencePerturbation() * 2.0);
  derivit = derivative.begin();
  while (derivit != derivend)
  {
    (*derivit) *= delta2;
    ++derivit;
  }

} // end GetValueAndFiniteDifferenceDerivative


/**
 * ******************** ComputeJacobianPreconditioner *******************
 */

template <class TFixedImage, class TMovingImage>
void
ParzenWindowMutualInformationImageToImageMetric<TFixedImage, TMovingImage>::ComputeJacobianPreconditioner(
  const TransformJacobianType &      jac,
  const NonZeroJacobianIndicesType & nzji,
  DerivativeType &                   preconditioner,
  DerivativeType &                   divisor) const
{
  using TransformJacobianValueType = typename TransformJacobianType::ValueType;
  const unsigned int M = nzji.size();
  using MatrixType = Matrix<double, MovingImageDimension, MovingImageDimension>;
  MatrixType jacjact;

  /** Compute jac * jac' */
  for (unsigned int drow = 0; drow < MovingImageDimension; ++drow)
  {
    for (unsigned int dcol = drow; dcol < MovingImageDimension; ++dcol)
    {
      const TransformJacobianValueType * jacit1 = jac[drow];
      const TransformJacobianValueType * jacit2 = jac[dcol];
      double                             sum = 0.0;
      for (unsigned int mu = 0; mu < M; ++mu)
      {
        sum += (*jacit1) * (*jacit2);
        ++jacit1;
        ++jacit2;
      }
      jacjact(drow, dcol) = sum;
      jacjact(dcol, drow) = sum;
    }
  }

  /** Invert */
  const double addtodiag = 1e-10;
  for (unsigned int drow = 0; drow < MovingImageDimension; ++drow)
  {
    jacjact(drow, drow) += addtodiag;
  }
  jacjact = vnl_inverse(jacjact.GetVnlMatrix());

  /** Compute preconditioner = diag( jac' * m * jac ),
   * with m = inv(jacjact)
   * implementation:
   * preconditioner = sum_dr sum_dc m(dr,dc) jac(dr,:) * jac(dc,:)
   */
  preconditioner.Fill(0.0);
  for (unsigned int drow = 0; drow < MovingImageDimension; ++drow)
  {
    for (unsigned int dcol = drow; dcol < MovingImageDimension; ++dcol)
    {
      DerivativeValueType *              precondit = preconditioner.begin();
      const TransformJacobianValueType * jacit1 = jac[drow];
      const TransformJacobianValueType * jacit2 = jac[dcol];
      /** count twice if off-diagonal */
      const double fac = drow == dcol ? 1.0 : 2.0;
      const double m = fac * jacjact(drow, dcol);
      for (unsigned int mu = 0; mu < M; ++mu)
      {
        *precondit += m * (*jacit1) * (*jacit2);
        ++precondit;
        ++jacit1;
        ++jacit2;
      }
    }
  }

  /** Update divisor = sum_samples diag(jac'*jac) */
  DerivativeType temp(M, 0.0);
  /** Compute this sample's contribution */
  for (unsigned int drow = 0; drow < MovingImageDimension; ++drow)
  {
    DerivativeValueType *              tempit = temp.begin();
    const TransformJacobianValueType * jacit1 = jac[drow];
    for (unsigned int mu = 0; mu < M; ++mu)
    {
      *tempit += vnl_math::sqr(*jacit1);
      ++tempit;
      ++jacit1;
    }
  }
  /** Update divisor */
  for (unsigned int mu = 0; mu < M; ++mu)
  {
    divisor[nzji[mu]] += temp[mu];
  }

} // end ComputeJacobianPreconditioner()


} // end namespace itk

#endif // end #ifndef itkParzenWindowMutualInformationImageToImageMetric_hxx