17 #ifndef __itkMeanReciprocalSquareDifferencePointSetToImageMetric_txx
18 #define __itkMeanReciprocalSquareDifferencePointSetToImageMetric_txx
29 template <
class TFixedPo
intSet,
class TMovingImage>
39 template <
class TFixedPo
intSet,
class TMovingImage>
49 itkExceptionMacro( <<
"Fixed point set has not been assigned" );
58 MeasureType measure = NumericTraits< MeasureType >::Zero;
60 this->m_NumberOfPixelsCounted = 0;
61 double lambdaSquared = vcl_pow(this->m_Lambda, 2);
63 this->SetTransformParameters( parameters );
65 typedef typename NumericTraits< MeasureType >::AccumulateType AccumulateType;
67 while( pointItr != pointEnd && pointDataItr != pointDataEnd )
70 inputPoint.
CastFrom( pointItr.Value() );
72 this->m_Transform->TransformPoint( inputPoint );
74 if( this->m_Interpolator->IsInsideBuffer( transformedPoint ) )
76 const RealType movingValue = this->m_Interpolator->Evaluate( transformedPoint );
77 const RealType fixedValue = pointDataItr.Value();
78 const RealType diff = movingValue - fixedValue;
79 const double diffSquared = diff * diff;
80 measure += 1.0 / ( lambdaSquared + diffSquared );
81 this->m_NumberOfPixelsCounted++;
88 if( !this->m_NumberOfPixelsCounted )
90 itkExceptionMacro(<<
"All the points mapped to outside of the moving image");
94 measure *= (lambdaSquared / this->m_NumberOfPixelsCounted);
105 template <
class TFixedPo
intSet,
class TMovingImage>
112 if( !this->GetGradientImage() )
114 itkExceptionMacro(<<
"The gradient image is null, maybe you forgot to call Initialize()");
121 itkExceptionMacro( <<
"Fixed image has not been assigned" );
124 this->m_NumberOfPixelsCounted = 0;
126 double lambdaSquared = vcl_pow(this->m_Lambda, 2);
128 this->SetTransformParameters( parameters );
130 typedef typename NumericTraits< MeasureType >::AccumulateType AccumulateType;
132 const unsigned int ParametersDimension = this->GetNumberOfParameters();
134 derivative.
Fill( NumericTraits<ITK_TYPENAME DerivativeType::ValueType>::Zero );
136 PointIterator pointItr = fixedPointSet->GetPoints()->Begin();
142 while( pointItr != pointEnd && pointDataItr != pointDataEnd )
145 inputPoint.
CastFrom( pointItr.Value() );
147 this->m_Transform->TransformPoint( inputPoint );
149 if( this->m_Interpolator->IsInsideBuffer( transformedPoint ) )
151 const RealType movingValue = this->m_Interpolator->Evaluate( transformedPoint );
152 const RealType fixedValue = pointDataItr.Value();
154 this->m_NumberOfPixelsCounted++;
155 const RealType diff = movingValue - fixedValue;
156 const RealType diffSquared = diff * diff;
160 this->m_Transform->GetJacobian( inputPoint );
166 MovingImageContinuousIndexType;
168 MovingImageContinuousIndexType tempIndex;
169 this->m_MovingImage->TransformPhysicalPointToContinuousIndex( transformedPoint, tempIndex );
171 typename MovingImageType::IndexType mappedIndex;
172 mappedIndex.CopyWithRound( tempIndex );
175 this->GetGradientImage()->GetPixel( mappedIndex );
177 for(
unsigned int par=0; par<ParametersDimension; par++)
179 RealType sum = NumericTraits< RealType >::Zero;
180 for(
unsigned int dim=0; dim<Self::FixedPointSetDimension; dim++)
184 sum -= jacobian( dim, par ) *
185 gradient[dim] / (vcl_pow( lambdaSquared + diffSquared , 2));
187 derivative[par] += diff * sum;
195 if( !this->m_NumberOfPixelsCounted )
197 itkExceptionMacro(<<
"All the points mapped to outside of the moving image");
201 for(
unsigned int i=0; i<ParametersDimension; i++)
203 derivative[i] *= 2.0 * lambdaSquared / this->m_NumberOfPixelsCounted;
211 template <
class TFixedPo
intSet,
class TMovingImage>
218 if( !this->GetGradientImage() )
220 itkExceptionMacro(<<
"The gradient image is null, maybe you forgot to call Initialize()");
227 itkExceptionMacro( <<
"Fixed image has not been assigned" );
230 this->m_NumberOfPixelsCounted = 0;
231 MeasureType measure = NumericTraits< MeasureType >::Zero;
233 this->SetTransformParameters( parameters );
234 double lambdaSquared = vcl_pow(this->m_Lambda, 2);
236 typedef typename NumericTraits< MeasureType >::AccumulateType AccumulateType;
238 const unsigned int ParametersDimension = this->GetNumberOfParameters();
240 derivative.
Fill( NumericTraits<ITK_TYPENAME DerivativeType::ValueType>::Zero );
242 PointIterator pointItr = fixedPointSet->GetPoints()->Begin();
248 while( pointItr != pointEnd && pointDataItr != pointDataEnd )
251 inputPoint.
CastFrom( pointItr.Value() );
253 this->m_Transform->TransformPoint( inputPoint );
255 if( this->m_Interpolator->IsInsideBuffer( transformedPoint ) )
257 const RealType movingValue = this->m_Interpolator->Evaluate( transformedPoint );
258 const RealType fixedValue = pointDataItr.Value();
260 this->m_NumberOfPixelsCounted++;
264 this->m_Transform->GetJacobian( inputPoint );
266 const RealType diff = movingValue - fixedValue;
267 const RealType diffSquared = diff * diff;
268 measure += 1.0 / ( lambdaSquared + diffSquared );
274 MovingImageContinuousIndexType;
276 MovingImageContinuousIndexType tempIndex;
277 this->m_MovingImage->TransformPhysicalPointToContinuousIndex( transformedPoint, tempIndex );
279 typename MovingImageType::IndexType mappedIndex;
280 mappedIndex.CopyWithRound( tempIndex );
283 this->GetGradientImage()->GetPixel( mappedIndex );
285 for(
unsigned int par=0; par<ParametersDimension; par++)
287 RealType sum = NumericTraits< RealType >::Zero;
288 for(
unsigned int dim=0; dim<Self::FixedPointSetDimension; dim++)
290 sum -= jacobian( dim, par ) * gradient[dim] *
291 vcl_pow(lambdaSquared + diffSquared, 2 );
293 derivative[par] += diff * sum;
302 if( !this->m_NumberOfPixelsCounted )
304 itkExceptionMacro(<<
"All the points mapped to outside of the moving image");
308 for(
unsigned int i=0; i<ParametersDimension; i++)
310 derivative[i] *= 2.0 * lambdaSquared / this->m_NumberOfPixelsCounted;
312 measure *= lambdaSquared / this->m_NumberOfPixelsCounted;
321 template <
class TFixedPo
intSet,
class TMovingImage>
326 Superclass::PrintSelf( os, indent );
327 os <<
"Lambda factor = " << m_Lambda << std::endl;