File: itkANTSNeighborhoodCorrelationImageToImageMetricv4GetValueAndDerivativeThreader.hxx

package info (click to toggle)
insighttoolkit4 4.13.3withdata-dfsg2-4%2Bdeb12u1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 491,276 kB
  • sloc: cpp: 557,593; ansic: 180,546; fortran: 34,788; python: 16,572; sh: 2,187; lisp: 2,070; tcl: 993; java: 362; perl: 200; makefile: 133; csh: 81; pascal: 69; xml: 19; ruby: 10
file content (603 lines) | stat: -rw-r--r-- 24,271 bytes parent folder | download | duplicates (4)
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
/*=========================================================================
 *
 *  Copyright Insight Software Consortium
 *
 *  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 itkANTSNeighborhoodCorrelationImageToImageMetricv4GetValueAndDerivativeThreader_hxx
#define itkANTSNeighborhoodCorrelationImageToImageMetricv4GetValueAndDerivativeThreader_hxx

#include "itkANTSNeighborhoodCorrelationImageToImageMetricv4GetValueAndDerivativeThreader.h"

namespace itk
{

template < typename TDomainPartitioner, typename TImageToImageMetric, typename TNeighborhoodCorrelationMetric >
void
ANTSNeighborhoodCorrelationImageToImageMetricv4GetValueAndDerivativeThreader< TDomainPartitioner, TImageToImageMetric, TNeighborhoodCorrelationMetric >
::ThreadedExecution_impl(
    IdentityHelper<ThreadedImageRegionPartitioner<TImageToImageMetric::VirtualImageDimension> > itkNotUsed(self),
    const DomainType& virtualImageSubRegion,
    const ThreadIdType threadId )
{
  /* Store the casted pointer to avoid dynamic casting in tight loops. */
  this->m_ANTSAssociate = dynamic_cast< TNeighborhoodCorrelationMetric * >( this->m_Associate );
  if( this->m_ANTSAssociate == ITK_NULLPTR )
    {
    itkExceptionMacro("Dynamic casting of associate pointer failed.");
    }

  VirtualPointType     virtualPoint;
  MeasureType          metricValueResult = NumericTraits< MeasureType >::ZeroValue();
  MeasureType          metricValueSum = NumericTraits< MeasureType >::ZeroValue();
  bool                 pointIsValid;
  ScanIteratorType     scanIt;
  ScanParametersType   scanParameters;
  ScanMemType          scanMem;

  DerivativeType & localDerivativeResult = this->m_GetValueAndDerivativePerThreadVariables[threadId].LocalDerivatives;

  /* Create an iterator over the virtual sub region */
  // this->m_ANTSAssociate->InitializeScanning( virtualImageSubRegion, scanIt, scanMem, scanParameters );
  this->InitializeScanning( virtualImageSubRegion, scanIt, scanMem, scanParameters );

  /* Iterate over the sub region */
  scanIt.GoToBegin();
  while (!scanIt.IsAtEnd())
    {
    /* Get the virtual point */
    this->m_ANTSAssociate->TransformVirtualIndexToPhysicalPoint( scanIt.GetIndex(), virtualPoint );

    /* Call the user method in derived classes to do the specific
     * calculations for value and derivative. */
    try
      {
      this->UpdateQueues(scanIt, scanMem, scanParameters, threadId);
      pointIsValid = this->ComputeInformationFromQueues(scanIt, scanMem, scanParameters, threadId);
      if( pointIsValid )
        {
        this->ComputeMovingTransformDerivative(scanIt, scanMem, scanParameters, localDerivativeResult, metricValueResult, threadId );
        }
      }
    catch (ExceptionObject & exc)
      {
      //NOTE: there must be a cleaner way to do this:
      std::string msg("Caught exception: \n");
      msg += exc.what();
      ExceptionObject err(__FILE__, __LINE__, msg);
      throw err;
      }

    /* Assign the results */
    if ( pointIsValid )
      {
      this->m_GetValueAndDerivativePerThreadVariables[threadId].NumberOfValidPoints++;
      metricValueSum -= metricValueResult;
      /* Store the result. This depends on what type of
       * transform is being used. */
      if( this->GetComputeDerivative() )
        {
        this->StorePointDerivativeResult( scanIt.GetIndex(), threadId );
        }
      }

    //next index
    ++scanIt;
    }

  /* Store metric value result for this thread. */
  this->m_GetValueAndDerivativePerThreadVariables[threadId].Measure = metricValueSum;
}

template < typename TDomainPartitioner, typename TImageToImageMetric, typename TNeighborhoodCorrelationMetric >
template < typename T >
void
ANTSNeighborhoodCorrelationImageToImageMetricv4GetValueAndDerivativeThreader< TDomainPartitioner, TImageToImageMetric, TNeighborhoodCorrelationMetric >
::ThreadedExecution_impl(
    IdentityHelper<T> itkNotUsed(self),
    const DomainType& domain,
    const ThreadIdType threadId )
{
    /* call base method */
    /* Store the casted pointer to avoid dynamic casting in tight loops. */
    this->m_ANTSAssociate = dynamic_cast< TNeighborhoodCorrelationMetric * >( this->m_Associate );
    if( this->m_ANTSAssociate == ITK_NULLPTR )
      {
      itkExceptionMacro("Dynamic casting of associate pointer failed.");
      }

    Superclass::ThreadedExecution(domain, threadId);
}

template < typename TDomainPartitioner, typename TImageToImageMetric, typename TNeighborhoodCorrelationMetric >
void
ANTSNeighborhoodCorrelationImageToImageMetricv4GetValueAndDerivativeThreader< TDomainPartitioner, TImageToImageMetric, TNeighborhoodCorrelationMetric >
::UpdateQueuesAtBeginningOfLine( const ScanIteratorType &scanIt, ScanMemType &scanMem, const ScanParametersType &scanParameters, const ThreadIdType ) const
 {
   const SizeValueType numberOfFillZero = scanParameters.numberOfFillZero;
   const SizeValueType hoodlen = scanParameters.windowLength;

   InternalComputationValueType zero = NumericTraits<InternalComputationValueType>::ZeroValue();
   scanMem.QsumFixed2 = SumQueueType(numberOfFillZero, zero);
   scanMem.QsumMoving2 = SumQueueType(numberOfFillZero, zero);
   scanMem.QsumFixed = SumQueueType(numberOfFillZero, zero);
   scanMem.QsumMoving = SumQueueType(numberOfFillZero, zero);
   scanMem.QsumFixedMoving = SumQueueType(numberOfFillZero, zero);
   scanMem.Qcount = SumQueueType(numberOfFillZero, zero);

   typedef InternalComputationValueType LocalRealType;

   // Now add the rest of the values from each hyperplane
   SizeValueType diameter = 2 * scanParameters.radius[0];

   const LocalRealType localZero = NumericTraits<LocalRealType>::ZeroValue();
   for (SizeValueType i = numberOfFillZero; i < ( diameter + NumericTraits<SizeValueType>::OneValue() ); i++)
     {
     LocalRealType sumFixed2      = localZero;
     LocalRealType sumMoving2     = localZero;
     LocalRealType sumFixed       = localZero;
     LocalRealType sumMoving      = localZero;
     LocalRealType sumFixedMoving = localZero;
     LocalRealType count = localZero;

     for ( SizeValueType indct = i; indct < hoodlen; indct += ( diameter + NumericTraits<SizeValueType>::OneValue() ) )
       {
       typename ScanIteratorType::OffsetType internalIndex, offset;
       bool isInBounds = scanIt.IndexInBounds( indct, internalIndex, offset );
       if (!isInBounds)
         {
         // std::cout << "DEBUG: error" << std::endl;
         continue;
         }

       typename VirtualImageType::IndexType index = scanIt.GetIndex(indct);

       VirtualPointType        virtualPoint;
       FixedImagePointType     mappedFixedPoint;
       FixedImagePixelType     fixedImageValue;
       MovingImagePointType    mappedMovingPoint;
       MovingImagePixelType    movingImageValue;
       bool pointIsValid;

       this->m_ANTSAssociate->TransformVirtualIndexToPhysicalPoint(index, virtualPoint);

       try
         {
         pointIsValid = this->m_ANTSAssociate->TransformAndEvaluateFixedPoint( virtualPoint, mappedFixedPoint, fixedImageValue );
         if ( pointIsValid )
           {
           pointIsValid = this->m_ANTSAssociate->TransformAndEvaluateMovingPoint( virtualPoint, mappedMovingPoint, movingImageValue );
           }
         }
       catch (ExceptionObject & exc)
         {
         //NOTE: there must be a cleaner way to do this:
         std::string msg("Caught exception: \n");
         msg += exc.what();
         ExceptionObject err(__FILE__, __LINE__, msg);
         throw err;
         }


       if ( pointIsValid )
         {
         sumFixed2 += fixedImageValue  * fixedImageValue;
         sumMoving2 += movingImageValue * movingImageValue;
         sumFixed += fixedImageValue;
         sumMoving += movingImageValue;
         sumFixedMoving += fixedImageValue * movingImageValue;
         count += NumericTraits<LocalRealType>::OneValue();
         }
       }//for indct

     scanMem.QsumFixed2.push_back(sumFixed2);
     scanMem.QsumMoving2.push_back(sumMoving2);
     scanMem.QsumFixed.push_back(sumFixed);
     scanMem.QsumMoving.push_back(sumMoving);
     scanMem.QsumFixedMoving.push_back(sumFixedMoving);
     scanMem.Qcount.push_back(count);
     }
 }

template < typename TDomainPartitioner, typename TImageToImageMetric, typename TNeighborhoodCorrelationMetric >
 void
 ANTSNeighborhoodCorrelationImageToImageMetricv4GetValueAndDerivativeThreader< TDomainPartitioner, TImageToImageMetric, TNeighborhoodCorrelationMetric >
::UpdateQueuesToNextScanWindow( const ScanIteratorType &scanIt, ScanMemType &scanMem, const ScanParametersType &scanParameters, const ThreadIdType ) const
{
 const SizeValueType hoodlen = scanParameters.windowLength;

 typedef InternalComputationValueType LocalRealType;

 const LocalRealType localZero = NumericTraits<LocalRealType>::ZeroValue();

 LocalRealType sumFixed2      = localZero;
 LocalRealType sumMoving2     = localZero;
 LocalRealType sumFixed       = localZero;
 LocalRealType sumMoving      = localZero;
 LocalRealType sumFixedMoving = localZero;
 LocalRealType count          = localZero;

 SizeValueType diameter = 2 * scanParameters.radius[0];

 for ( SizeValueType indct = diameter; indct < hoodlen; indct += (diameter + NumericTraits<SizeValueType>::OneValue()))
   {
   typename ScanIteratorType::OffsetType internalIndex, offset;
   bool isInBounds = scanIt.IndexInBounds( indct, internalIndex, offset );

   if (!isInBounds)
   {
   continue;
   }

   typename VirtualImageType::IndexType index = scanIt.GetIndex(indct);

   VirtualPointType virtualPoint;
   FixedImagePointType mappedFixedPoint;
   FixedImagePixelType fixedImageValue;
   MovingImagePointType mappedMovingPoint;
   MovingImagePixelType movingImageValue;
   MovingImageGradientType movingImageGradient;
   bool pointIsValid;

   this->m_ANTSAssociate->TransformVirtualIndexToPhysicalPoint(index, virtualPoint);
   try
     {
     pointIsValid = this->m_ANTSAssociate->TransformAndEvaluateFixedPoint( virtualPoint, mappedFixedPoint, fixedImageValue );
     if (pointIsValid)
       {
       pointIsValid = this->m_ANTSAssociate->TransformAndEvaluateMovingPoint( virtualPoint, mappedMovingPoint, movingImageValue );
       }
     }
   catch (ExceptionObject & exc)
     {
     //NOTE: there must be a cleaner way to do this:
     std::string msg("Caught exception: \n");
     msg += exc.what();
     ExceptionObject err(__FILE__, __LINE__, msg);
     throw err;
     }
   if ( pointIsValid )
     {
     sumFixed2 += fixedImageValue  * fixedImageValue;
     sumMoving2 += movingImageValue * movingImageValue;
     sumFixed += fixedImageValue;
     sumMoving += movingImageValue;
     sumFixedMoving += fixedImageValue * movingImageValue;
     count += NumericTraits<LocalRealType>::OneValue();
     }
   }
   scanMem.QsumFixed2.push_back(sumFixed2);
   scanMem.QsumMoving2.push_back(sumMoving2);
   scanMem.QsumFixed.push_back(sumFixed);
   scanMem.QsumMoving.push_back(sumMoving);
   scanMem.QsumFixedMoving.push_back(sumFixedMoving);
   scanMem.Qcount.push_back(count);

   scanMem.QsumFixed2.pop_front();
   scanMem.QsumMoving2.pop_front();
   scanMem.QsumFixed.pop_front();
   scanMem.QsumMoving.pop_front();
   scanMem.QsumFixedMoving.pop_front();
   scanMem.Qcount.pop_front();
}

template < typename TDomainPartitioner, typename TImageToImageMetric, typename TNeighborhoodCorrelationMetric >
void
ANTSNeighborhoodCorrelationImageToImageMetricv4GetValueAndDerivativeThreader< TDomainPartitioner, TImageToImageMetric, TNeighborhoodCorrelationMetric >
::InitializeScanning( const ImageRegionType &scanRegion, ScanIteratorType &scanIt,
                      ScanMemType & scanMem, ScanParametersType &scanParameters ) const
{
  scanParameters.scanRegion   = scanRegion;
  scanParameters.fixedImage   = this->m_ANTSAssociate->m_FixedImage;
  scanParameters.movingImage  = this->m_ANTSAssociate->m_MovingImage;
  scanParameters.virtualImage = this->m_ANTSAssociate->GetVirtualImage();
  scanParameters.radius       = this->m_ANTSAssociate->GetRadius();

  OffsetValueType numberOfFillZero = this->m_ANTSAssociate->GetVirtualRegion().GetIndex(0)
      - (scanRegion.GetIndex(0) - scanParameters.radius[0]);

  if (numberOfFillZero < NumericTraits<OffsetValueType>::ZeroValue())
    {
    numberOfFillZero = NumericTraits<OffsetValueType>::ZeroValue();
    }

  scanParameters.numberOfFillZero = numberOfFillZero;

  scanIt = ScanIteratorType(scanParameters.radius, scanParameters.virtualImage, scanRegion);
  scanParameters.windowLength = scanIt.Size();
  scanParameters.scanRegionBeginIndexDim0 = scanIt.GetBeginIndex()[0];

  scanMem.fixedA = NumericTraits< QueueRealType >::ZeroValue();
  scanMem.movingA = NumericTraits< QueueRealType >::ZeroValue();
  scanMem.sFixedMoving = NumericTraits< QueueRealType >::ZeroValue();
  scanMem.sFixedFixed = NumericTraits< QueueRealType >::ZeroValue();
  scanMem.sMovingMoving = NumericTraits< QueueRealType >::ZeroValue();

  scanMem.fixedImageGradient.Fill(0.0);
  scanMem.movingImageGradient.Fill(0.0);
  scanMem.mappedMovingPoint.Fill(0.0);
}

template < typename TDomainPartitioner, typename TImageToImageMetric, typename TNeighborhoodCorrelationMetric >
void
ANTSNeighborhoodCorrelationImageToImageMetricv4GetValueAndDerivativeThreader< TDomainPartitioner, TImageToImageMetric, TNeighborhoodCorrelationMetric >
::UpdateQueues( const ScanIteratorType &scanIt, ScanMemType &scanMem,
                const ScanParametersType &scanParameters, const ThreadIdType threadId) const
{
  if (scanIt.GetIndex()[0] == scanParameters.scanRegionBeginIndexDim0 )
    {
    this->UpdateQueuesAtBeginningOfLine(scanIt, scanMem, scanParameters, threadId);
    }
  else
    {
    this->UpdateQueuesToNextScanWindow(scanIt, scanMem, scanParameters, threadId);
    }
}


template < typename TDomainPartitioner, typename TImageToImageMetric, typename TNeighborhoodCorrelationMetric >
bool
ANTSNeighborhoodCorrelationImageToImageMetricv4GetValueAndDerivativeThreader< TDomainPartitioner, TImageToImageMetric, TNeighborhoodCorrelationMetric >
::ComputeInformationFromQueues( const ScanIteratorType &scanIt, ScanMemType &scanMem, const ScanParametersType &, const ThreadIdType ) const
{
 typedef InternalComputationValueType LocalRealType;

 const LocalRealType localZero = NumericTraits<LocalRealType>::ZeroValue();

 LocalRealType count = localZero;

 typename SumQueueType::iterator itcount = scanMem.Qcount.begin();
 while (itcount != scanMem.Qcount.end())
   {
   count += *itcount;
   ++itcount;
   }

 if (count <= localZero)
   {
   // no points available in the queue, perhaps out of image region
   return false;
   }

 // If there are values, we need to calculate the different quantities
 LocalRealType sumFixed2      = localZero;
 LocalRealType sumMoving2     = localZero;
 LocalRealType sumFixed       = localZero;
 LocalRealType sumMoving      = localZero;
 LocalRealType sumFixedMoving = localZero;
 typename SumQueueType::iterator itFixed2      = scanMem.QsumFixed2.begin();
 typename SumQueueType::iterator itMoving2     = scanMem.QsumMoving2.begin();
 typename SumQueueType::iterator itFixed       = scanMem.QsumFixed.begin();
 typename SumQueueType::iterator itMoving      = scanMem.QsumMoving.begin();
 typename SumQueueType::iterator itFixedMoving = scanMem.QsumFixedMoving.begin();

 while (itFixed2 != scanMem.QsumFixed2.end())
   {
   sumFixed2 += *itFixed2;
   sumMoving2 += *itMoving2;
   sumFixed += *itFixed;
   sumMoving += *itMoving;
   sumFixedMoving += *itFixedMoving;

   ++itFixed2;
   ++itMoving2;
   ++itFixed;
   ++itMoving;
   ++itFixedMoving;
   }

 LocalRealType fixedMean  = sumFixed  / count;
 LocalRealType movingMean = sumMoving / count;

 LocalRealType sFixedFixed   = sumFixed2 - fixedMean * sumFixed - fixedMean * sumFixed + count * fixedMean * fixedMean;
 LocalRealType sMovingMoving = sumMoving2 - movingMean * sumMoving - movingMean * sumMoving + count * movingMean * movingMean;
 LocalRealType sFixedMoving  = sumFixedMoving - movingMean * sumFixed - fixedMean * sumMoving + count * movingMean * fixedMean;

 typename VirtualImageType::IndexType oindex = scanIt.GetIndex();

 VirtualPointType        virtualPoint;
 FixedImagePointType     mappedFixedPoint;
 FixedImagePixelType     fixedImageValue;
 FixedImageGradientType  fixedImageGradient;
 MovingImagePointType    mappedMovingPoint;
 MovingImagePixelType    movingImageValue;
 MovingImageGradientType movingImageGradient;
 bool pointIsValid;

 this->m_ANTSAssociate->TransformVirtualIndexToPhysicalPoint(oindex, virtualPoint);

 try
   {
   pointIsValid = this->m_ANTSAssociate->TransformAndEvaluateFixedPoint( virtualPoint, mappedFixedPoint, fixedImageValue );
   if ( pointIsValid )
     {
     pointIsValid = this->m_ANTSAssociate->TransformAndEvaluateMovingPoint( virtualPoint, mappedMovingPoint, movingImageValue );
     if( pointIsValid && this->m_ANTSAssociate->GetComputeDerivative() )
       {
       if( this->m_ANTSAssociate->GetGradientSourceIncludesFixed() )
         {
         this->m_ANTSAssociate->ComputeFixedImageGradientAtPoint( mappedFixedPoint, fixedImageGradient );
         }
       if( this->m_ANTSAssociate->GetGradientSourceIncludesMoving() )
         {
         this->m_ANTSAssociate->ComputeMovingImageGradientAtPoint( mappedMovingPoint, movingImageGradient );
         }
       }
     }
   }
 catch (ExceptionObject & exc)
   {
   //NOTE: there must be a cleaner way to do this:
   std::string msg("Caught exception: \n");
   msg += exc.what();
   ExceptionObject err(__FILE__, __LINE__, msg);
   throw err;
   }

 if ( pointIsValid )
   {
   scanMem.fixedA        = fixedImageValue  - fixedMean;
   scanMem.movingA       = movingImageValue - movingMean;
   scanMem.sFixedMoving  = sFixedMoving;
   scanMem.sFixedFixed   = sFixedFixed;
   scanMem.sMovingMoving = sMovingMoving;

   scanMem.fixedImageGradient  = fixedImageGradient;
   scanMem.movingImageGradient = movingImageGradient;

   scanMem.mappedFixedPoint  = mappedFixedPoint;
   scanMem.mappedMovingPoint = mappedMovingPoint;
   scanMem.virtualPoint      = virtualPoint;
   }

 return pointIsValid;
}

template < typename TDomainPartitioner, typename TImageToImageMetric, typename TNeighborhoodCorrelationMetric >
void
ANTSNeighborhoodCorrelationImageToImageMetricv4GetValueAndDerivativeThreader< TDomainPartitioner, TImageToImageMetric, TNeighborhoodCorrelationMetric >
::ComputeMovingTransformDerivative( const ScanIteratorType &, ScanMemType &scanMem, const ScanParametersType &, DerivativeType &deriv, MeasureType &localCC, const ThreadIdType threadId) const
{
  MovingImageGradientType derivWRTImage;
  localCC = NumericTraits<MeasureType>::OneValue();

  typedef InternalComputationValueType LocalRealType;

  LocalRealType sFixedFixed   = scanMem.sFixedFixed;
  LocalRealType sMovingMoving = scanMem.sMovingMoving;
  LocalRealType sFixedMoving  = scanMem.sFixedMoving;
  LocalRealType fixedI        = scanMem.fixedA;
  LocalRealType movingI       = scanMem.movingA;

  LocalRealType sFixedFixed_sMovingMoving = sFixedFixed * sMovingMoving;

  if ( fabs(sFixedFixed_sMovingMoving) > NumericTraits< LocalRealType >::epsilon() )
    {
    localCC = sFixedMoving * sFixedMoving / (sFixedFixed_sMovingMoving);
    }

  if( this->m_ANTSAssociate->GetComputeDerivative() )
    {
    const MovingImageGradientType movingImageGradient = scanMem.movingImageGradient;

    if ( ! (sFixedFixed > NumericTraits<LocalRealType>::epsilon() && sMovingMoving > NumericTraits<LocalRealType>::epsilon() ) )
      {
      deriv.Fill( NumericTraits<DerivativeValueType>::ZeroValue() );
      return;
      }

    for (ImageDimensionType qq = 0; qq < TImageToImageMetric::VirtualImageDimension; qq++)
      {
      derivWRTImage[qq] = 2.0 * sFixedMoving / (sFixedFixed_sMovingMoving) * (fixedI - sFixedMoving / sMovingMoving * movingI) * movingImageGradient[qq];
      }

    /* Use a pre-allocated jacobian object for efficiency */
    typedef JacobianType & JacobianReferenceType;
    JacobianReferenceType jacobian = this->m_GetValueAndDerivativePerThreadVariables[threadId].MovingTransformJacobian;
    JacobianReferenceType jacobianPositional = this->m_GetValueAndDerivativePerThreadVariables[threadId].MovingTransformJacobianPositional;

    /** For dense transforms, this returns identity */
    this->m_Associate->GetMovingTransform()->
      ComputeJacobianWithRespectToParametersCachedTemporaries(scanMem.virtualPoint,
                                                              jacobian,
                                                              jacobianPositional);

    NumberOfParametersType numberOfLocalParameters = this->m_Associate->GetMovingTransform()->GetNumberOfLocalParameters();

    for (NumberOfParametersType par = 0; par < numberOfLocalParameters; par++)
      {
      deriv[par] = NumericTraits<DerivativeValueType>::ZeroValue();
      for (ImageDimensionType dim = 0; dim < TImageToImageMetric::MovingImageDimension; dim++)
        {
        deriv[par] += derivWRTImage[dim] * jacobian(dim, par);
        }
      }
    }
}

/*
 * Specific implementation for sparse threader. It reuse most of the routine from the dense threader by
 * reinitializing the scanning at every point.
 */
template < typename TDomainPartitioner, typename TImageToImageMetric, typename TNeighborhoodCorrelationMetric >
bool
ANTSNeighborhoodCorrelationImageToImageMetricv4GetValueAndDerivativeThreader< TDomainPartitioner, TImageToImageMetric, TNeighborhoodCorrelationMetric >
::ProcessVirtualPoint_impl(IdentityHelper<ThreadedIndexedContainerPartitioner> itkNotUsed(self),
    const VirtualIndexType & virtualIndex, const VirtualPointType & itkNotUsed(virtualPoint),
    const ThreadIdType threadId )
{

  MeasureType          metricValueResult = NumericTraits< MeasureType >::ZeroValue();
  bool                 pointIsValid;
  ScanIteratorType     scanIt;
  ScanParametersType   scanParameters;
  ScanMemType          scanMem;

  DerivativeType & localDerivativeResult = this->m_GetValueAndDerivativePerThreadVariables[threadId].LocalDerivatives;


  // convert virtualPoint to a single point region
  ImageRegionType singlePointRegion;
  singlePointRegion.SetIndex(virtualIndex);
  typename ImageRegionType::SizeType singlePointSize;
  singlePointSize.Fill(1);
  singlePointRegion.SetSize(singlePointSize);

  // use scanning variables just for a single point region
  // iterate over the single point and initialize the scanning variables
  this->InitializeScanning( singlePointRegion, scanIt, scanMem, scanParameters );

  /* Iterate over the sub region of a single point*/
  scanIt.GoToBegin();

  try
    {
    pointIsValid = false;

    this->UpdateQueues(scanIt, scanMem, scanParameters, threadId);
    pointIsValid = this->ComputeInformationFromQueues(scanIt, scanMem, scanParameters, threadId);
    if( pointIsValid )
      {
      this->ComputeMovingTransformDerivative(scanIt, scanMem, scanParameters, localDerivativeResult, metricValueResult, threadId );
      }
    }
  catch (ExceptionObject & exc)
    {
    //NOTE: there must be a cleaner way to do this:
    std::string msg("Caught exception: \n");
    msg += exc.what();
    ExceptionObject err(__FILE__, __LINE__, msg);
    throw err;
    }

  /* Assign the results */
  if ( pointIsValid )
    {
    this->m_GetValueAndDerivativePerThreadVariables[threadId].NumberOfValidPoints++;
    this->m_GetValueAndDerivativePerThreadVariables[threadId].Measure -= metricValueResult;
    /* Store the result. This depends on what type of
     * transform is being used. */
    if( this->GetComputeDerivative() )
      {
      this->StorePointDerivativeResult( scanIt.GetIndex(), threadId );
      }
    }

  return pointIsValid;
}


} // end namespace itk

#endif