ANIMA  4.0
animaTensorMeanSquaresImageToImageMetric.hxx
Go to the documentation of this file.
1 #pragma once
3 
4 #include <itkImageRegionConstIteratorWithIndex.h>
5 #include <animaBaseTensorTools.h>
6 
7 namespace anima
8 {
9 
10 template < class TFixedImagePixelType, class TMovingImagePixelType, unsigned int ImageDimension >
13 {
14 }
15 
19 template < class TFixedImagePixelType, class TMovingImagePixelType, unsigned int ImageDimension >
22 ::GetValue( const TransformParametersType & parameters ) const
23 {
24  itkDebugMacro("GetValue( " << parameters << " ) ");
25 
26  FixedImageConstPointer fixedImage = this->m_FixedImage;
27 
28  if( !fixedImage )
29  {
30  itkExceptionMacro( << "Fixed image has not been assigned" );
31  }
32 
33  typedef itk::ImageRegionConstIteratorWithIndex<FixedImageType> FixedIteratorType;
34 
35  FixedIteratorType ti( fixedImage, this->GetFixedImageRegion() );
36 
37  typename FixedImageType::IndexType index;
38 
39  MeasureType measure = itk::NumericTraits< MeasureType >::Zero;
40 
41  this->m_NumberOfPixelsCounted = this->GetFixedImageRegion().GetNumberOfPixels();
42 
43  this->SetTransformParameters( parameters );
44 
45  unsigned int vectorSize = fixedImage->GetNumberOfComponentsPerPixel();
46  ContinuousIndexType transformedIndex;
47  OutputPointType transformedPoint, inputPoint;
48 
49  unsigned int tensorDimension = 3;
50  vnl_matrix <double> tmpMat(tensorDimension, tensorDimension);
51  vnl_matrix <double> currentTensor(tensorDimension, tensorDimension);
52  vnl_matrix <double> ppdOrientationMatrix(tensorDimension, tensorDimension);
53  typedef itk::Matrix <double, 3, 3> EigVecMatrixType;
54  typedef vnl_vector_fixed <double,3> EigValVectorType;
55  itk::SymmetricEigenAnalysis < EigVecMatrixType, EigValVectorType, EigVecMatrixType> eigenComputer(3);
56  EigVecMatrixType eigVecs;
57  EigValVectorType eigVals;
58  PixelType movingValue;
59 
60  while(!ti.IsAtEnd())
61  {
62  index = ti.GetIndex();
63  fixedImage->TransformIndexToPhysicalPoint(index,inputPoint);
64 
65  transformedPoint = this->m_Transform->TransformPoint( inputPoint );
66  this->m_Interpolator->GetInputImage()->TransformPhysicalPointToContinuousIndex(transformedPoint,transformedIndex);
67 
68  if( this->m_Interpolator->IsInsideBuffer( transformedIndex ) )
69  {
70  movingValue = this->m_Interpolator->EvaluateAtContinuousIndex(transformedIndex);
71 
72  if (this->GetModelRotation() != Superclass::NONE)
73  {
74  // Rotating tensor
75  anima::GetTensorFromVectorRepresentation(movingValue,tmpMat,tensorDimension,true);
76 
77  if (this->GetModelRotation() == Superclass::FINITE_STRAIN)
78  anima::RotateSymmetricMatrix(tmpMat,this->m_OrientationMatrix,currentTensor);
79  else
80  {
81  eigenComputer.ComputeEigenValuesAndVectors(tmpMat,eigVals,eigVecs);
82  anima::ExtractPPDRotationFromJacobianMatrix(this->m_OrientationMatrix,ppdOrientationMatrix,eigVecs);
83  anima::RotateSymmetricMatrix(tmpMat,ppdOrientationMatrix,currentTensor);
84  }
85 
86  anima::GetVectorRepresentation(currentTensor,movingValue,vectorSize,true);
87  }
88  }
89  else
90  {
91  movingValue.SetSize(vectorSize);
92  movingValue.Fill(0.0);
93  }
94 
95  const PixelType fixedValue = ti.Get();
96  for (unsigned int i = 0;i < vectorSize;++i)
97  {
98  double diff = movingValue[i] - fixedValue[i];
99  measure += diff * diff;
100  }
101 
102  ++ti;
103  }
104 
105  measure /= this->m_NumberOfPixelsCounted;
106 
107  return measure;
108 }
109 
110 } // end namespace anima
void GetVectorRepresentation(const vnl_matrix< T1 > &tensor, itk::VariableLengthVector< T2 > &vector, unsigned int vecDim=0, bool scale=false)
void RotateSymmetricMatrix(T1 &tensor, T2 &rotationMatrix, T3 &rotated_tensor, unsigned int tensorDim)
Rotates a symmetric matrix by performing R^T D R where R is a rotation matrix and D the symmetric mat...
itk::ContinuousIndex< double, TFixedImage::ImageDimension > ContinuousIndexType
void GetTensorFromVectorRepresentation(const itk::VariableLengthVector< T1 > &vector, vnl_matrix< T2 > &tensor, unsigned int tensDim=0, bool scale=false)
MeasureType GetValue(const TransformParametersType &parameters) const ITK_OVERRIDE
void ExtractPPDRotationFromJacobianMatrix(vnl_matrix< RealType > &jacobianMatrix, vnl_matrix< RealType > &rotationMatrix, MatrixType &eigenVectors)