17 #ifndef __itkMeanSquaresPointSetToImageMetric_txx
18 #define __itkMeanSquaresPointSetToImageMetric_txx
29 template <
class TFixedPo
intSet,
class TMovingImage>
38 template <
class TFixedPo
intSet,
class TMovingImage>
48 itkExceptionMacro( <<
"Fixed point set has not been assigned" );
57 MeasureType measure = NumericTraits< MeasureType >::Zero;
59 this->m_NumberOfPixelsCounted = 0;
61 this->SetTransformParameters( parameters );
63 typedef typename NumericTraits< MeasureType >::AccumulateType AccumulateType;
65 while( pointItr != pointEnd && pointDataItr != pointDataEnd )
68 inputPoint.
CastFrom( pointItr.Value() );
70 this->m_Transform->TransformPoint( inputPoint );
72 if( this->m_Interpolator->IsInsideBuffer( transformedPoint ) )
74 const RealType movingValue = this->m_Interpolator->Evaluate( transformedPoint );
75 const RealType fixedValue = pointDataItr.Value();
76 const RealType diff = movingValue - fixedValue;
77 measure += diff * diff;
78 this->m_NumberOfPixelsCounted++;
85 if( !this->m_NumberOfPixelsCounted )
87 itkExceptionMacro(<<
"All the points mapped to outside of the moving image");
91 measure /= this->m_NumberOfPixelsCounted;
102 template <
class TFixedPo
intSet,
class TMovingImage>
109 if( !this->GetGradientImage() )
111 itkExceptionMacro(<<
"The gradient image is null, maybe you forgot to call Initialize()");
118 itkExceptionMacro( <<
"Fixed image has not been assigned" );
121 this->m_NumberOfPixelsCounted = 0;
123 this->SetTransformParameters( parameters );
125 typedef typename NumericTraits< MeasureType >::AccumulateType AccumulateType;
127 const unsigned int ParametersDimension = this->GetNumberOfParameters();
129 derivative.
Fill( NumericTraits<ITK_TYPENAME DerivativeType::ValueType>::Zero );
131 PointIterator pointItr = fixedPointSet->GetPoints()->Begin();
137 while( pointItr != pointEnd && pointDataItr != pointDataEnd )
140 inputPoint.
CastFrom( pointItr.Value() );
142 this->m_Transform->TransformPoint( inputPoint );
144 if( this->m_Interpolator->IsInsideBuffer( transformedPoint ) )
146 const RealType movingValue = this->m_Interpolator->Evaluate( transformedPoint );
147 const RealType fixedValue = pointDataItr.Value();
149 this->m_NumberOfPixelsCounted++;
150 const RealType diff = movingValue - fixedValue;
155 this->m_Transform->GetJacobian( inputPoint );
161 MovingImageContinuousIndexType;
163 MovingImageContinuousIndexType tempIndex;
164 this->m_MovingImage->TransformPhysicalPointToContinuousIndex( transformedPoint, tempIndex );
166 typename MovingImageType::IndexType mappedIndex;
167 mappedIndex.CopyWithRound( tempIndex );
170 this->GetGradientImage()->GetPixel( mappedIndex );
172 for(
unsigned int par=0; par<ParametersDimension; par++)
174 RealType sum = NumericTraits< RealType >::Zero;
175 for(
unsigned int dim=0; dim<Self::FixedPointSetDimension; dim++)
177 sum += 2.0 * diff * jacobian( dim, par ) * gradient[dim];
179 derivative[par] += sum;
187 if( !this->m_NumberOfPixelsCounted )
189 itkExceptionMacro(<<
"All the points mapped to outside of the moving image");
193 for(
unsigned int i=0; i<ParametersDimension; i++)
195 derivative[i] /= this->m_NumberOfPixelsCounted;
204 template <
class TFixedPo
intSet,
class TMovingImage>
211 if( !this->GetGradientImage() )
213 itkExceptionMacro(<<
"The gradient image is null, maybe you forgot to call Initialize()");
220 itkExceptionMacro( <<
"Fixed image has not been assigned" );
223 this->m_NumberOfPixelsCounted = 0;
224 MeasureType measure = NumericTraits< MeasureType >::Zero;
226 this->SetTransformParameters( parameters );
228 typedef typename NumericTraits< MeasureType >::AccumulateType AccumulateType;
230 const unsigned int ParametersDimension = this->GetNumberOfParameters();
232 derivative.
Fill( NumericTraits<ITK_TYPENAME DerivativeType::ValueType>::Zero );
234 PointIterator pointItr = fixedPointSet->GetPoints()->Begin();
240 while( pointItr != pointEnd && pointDataItr != pointDataEnd )
243 inputPoint.
CastFrom( pointItr.Value() );
245 this->m_Transform->TransformPoint( inputPoint );
247 if( this->m_Interpolator->IsInsideBuffer( transformedPoint ) )
249 const RealType movingValue = this->m_Interpolator->Evaluate( transformedPoint );
250 const RealType fixedValue = pointDataItr.Value();
252 this->m_NumberOfPixelsCounted++;
256 this->m_Transform->GetJacobian( inputPoint );
258 const RealType diff = movingValue - fixedValue;
260 measure += diff * diff;
266 MovingImageContinuousIndexType;
268 MovingImageContinuousIndexType tempIndex;
269 this->m_MovingImage->TransformPhysicalPointToContinuousIndex( transformedPoint, tempIndex );
271 typename MovingImageType::IndexType mappedIndex;
272 mappedIndex.CopyWithRound( tempIndex );
275 this->GetGradientImage()->GetPixel( mappedIndex );
277 for(
unsigned int par=0; par<ParametersDimension; par++)
279 RealType sum = NumericTraits< RealType >::Zero;
280 for(
unsigned int dim=0; dim<Self::FixedPointSetDimension; dim++)
282 sum += 2.0 * diff * jacobian( dim, par ) * gradient[dim];
284 derivative[par] += sum;
293 if( !this->m_NumberOfPixelsCounted )
295 itkExceptionMacro(<<
"All the points mapped to outside of the moving image");
299 for(
unsigned int i=0; i<ParametersDimension; i++)
301 derivative[i] /= this->m_NumberOfPixelsCounted;
303 measure /= this->m_NumberOfPixelsCounted;