Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019 #ifndef MITKMATRIXCONVERT_H_HEADER_INCLUDED_C1EBD0AD
00020 #define MITKMATRIXCONVERT_H_HEADER_INCLUDED_C1EBD0AD
00021
00022 #include "mitkGeometry3D.h"
00023 #include "mitkItkMatrixHack.h"
00024 #include <vtkMatrix4x4.h>
00025
00026 namespace mitk
00027 {
00028 template <class TTransformType>
00029 void TransferVtkMatrixToItkTransform(const vtkMatrix4x4* vtkmatrix, TTransformType * itkTransform)
00030 {
00031 if(itkTransform==NULL)
00032 return;
00033
00034 typename TTransformType::MatrixType::InternalMatrixType& vnlMatrix =
00035 const_cast<typename TTransformType::MatrixType::InternalMatrixType&>(itkTransform->GetMatrix().GetVnlMatrix());
00036
00037 for ( int i=0; i < 3; ++i)
00038 for( int j=0; j < 3; ++j )
00039 vnlMatrix[i][j] = vtkmatrix->GetElement( i, j );
00040
00041
00042
00043 static_cast<mitk::ItkMatrixHack<TTransformType>*>(itkTransform)->MatrixChanged();
00044
00045 typename TTransformType::OffsetType offset;
00046 offset[0] = vtkmatrix->GetElement( 0, 3 );
00047 offset[1] = vtkmatrix->GetElement( 1, 3 );
00048 offset[2] = vtkmatrix->GetElement( 2, 3 );
00049 itkTransform->SetOffset( offset );
00050 }
00051
00052 template <class TTransformType>
00053 void TransferItkTransformToVtkMatrix(const TTransformType * itkTransform, vtkMatrix4x4* vtkmatrix)
00054 {
00055 int i,j;
00056 for(i=0;i<3;++i)
00057 for(j=0;j<3;++j)
00058 vtkmatrix->SetElement(i, j, itkTransform->GetMatrix().GetVnlMatrix().get(i, j));
00059 for(i=0;i<3;++i)
00060 vtkmatrix->SetElement(i, 3, itkTransform->GetOffset()[i]);
00061 vtkmatrix->Modified();
00062 }
00063
00064 template <class TTransformType1, class TTransformType2>
00065 void ConvertItkTransform(const TTransformType1* sourceTransform, TTransformType2* destTransform)
00066 {
00067 if((sourceTransform==NULL) || (destTransform==NULL))
00068 return;
00069
00070
00071 const typename TTransformType1::OutputVectorType& sourceOffset =
00072 sourceTransform->GetOffset();
00073
00074 typename TTransformType2::OutputVectorType offset;
00075 offset[0] = sourceOffset[0]; offset[1] = sourceOffset[1]; offset[2] = sourceOffset[2];
00076 destTransform->SetOffset( offset );
00077
00078 typename TTransformType1::MatrixType::InternalMatrixType& sourceVnlMatrix =
00079 const_cast<typename TTransformType1::MatrixType::InternalMatrixType&>(sourceTransform->GetMatrix().GetVnlMatrix());
00080
00081
00082 typename TTransformType2::MatrixType::InternalMatrixType& destVnlMatrix =
00083 const_cast<typename TTransformType2::MatrixType::InternalMatrixType&>(destTransform->GetMatrix().GetVnlMatrix());
00084
00085 for ( int i=0; i < 3; ++i)
00086 for( int j=0; j < 3; ++j )
00087 destVnlMatrix[i][j] = sourceVnlMatrix[i][j];
00088
00089
00090
00091
00092 static_cast<mitk::ItkMatrixHack<TTransformType2>*>(destTransform)->MatrixChanged();
00093 }
00094
00095 template <class TMatrixType>
00096 void GetRotation(const mitk::Geometry3D * geometry, TMatrixType& itkmatrix)
00097 {
00098 const mitk::Vector3D& spacing = geometry->GetSpacing();
00099 typename mitk::Geometry3D::TransformType::MatrixType::InternalMatrixType& geometryVnlMatrix =
00100 const_cast<typename mitk::Geometry3D::TransformType::MatrixType::InternalMatrixType&>(geometry->GetIndexToWorldTransform()->GetMatrix().GetVnlMatrix());
00101
00102 typename TMatrixType::InternalMatrixType& outputVnlMatrix =
00103 const_cast<typename TMatrixType::InternalMatrixType&>(itkmatrix.GetVnlMatrix());
00104
00105 for ( int i=0; i < 3; ++i)
00106 for( int j=0; j < 3; ++j )
00107 outputVnlMatrix [i][j] = geometryVnlMatrix [i][j] / spacing[j];
00108 }
00109
00110 template <class TTransformType>
00111 void GetWorldToItkPhysicalTransform(const mitk::Geometry3D * geometry, TTransformType* itkTransform)
00112 {
00113 if(itkTransform==NULL)
00114 return;
00115
00116
00117 typename TTransformType::MatrixType rotationMatrix;
00118 GetRotation(geometry, rotationMatrix);
00119
00120 const typename mitk::Geometry3D::TransformType::OffsetType& geometryOffset =
00121 geometry->GetIndexToWorldTransform()->GetOffset();
00122
00123 vnl_vector<typename TTransformType::MatrixType::ValueType> vnlOffset(3);
00124 vnlOffset[0] = geometryOffset[0]; vnlOffset[1] = geometryOffset[1]; vnlOffset[2] = geometryOffset[2];
00125
00126
00127 typename TTransformType::MatrixType::InternalMatrixType inverseRotationVnlMatrix = rotationMatrix.GetTranspose();
00128
00129 vnlOffset -= inverseRotationVnlMatrix*vnlOffset;
00130
00131 typename TTransformType::OutputVectorType offset;
00132 offset[0] = vnlOffset[0]; offset[1] = vnlOffset[1]; offset[2] = vnlOffset[2];
00133 itkTransform->SetOffset( offset );
00134
00135
00136 typename TTransformType::MatrixType::InternalMatrixType& destVnlMatrix =
00137 const_cast<typename TTransformType::MatrixType::InternalMatrixType&>(itkTransform->GetMatrix().GetVnlMatrix());
00138
00139 for ( int i=0; i < 3; ++i)
00140 for( int j=0; j < 3; ++j )
00141 destVnlMatrix[i][j] = inverseRotationVnlMatrix[i][j];
00142
00143
00144
00145 static_cast<mitk::ItkMatrixHack<TTransformType>*>(itkTransform)->MatrixChanged();
00146 }
00147
00148 }
00149
00150 #endif